From 0467e938a2c063e817405450bf190d27da0b14a7 Mon Sep 17 00:00:00 2001 From: Lauri Kasanen Date: Wed, 25 Aug 2021 13:08:42 +0300 Subject: [PATCH] Input --- unix/kasmxproxy/CMakeLists.txt | 2 +- unix/kasmxproxy/kasmxproxy.c | 45 +++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/unix/kasmxproxy/CMakeLists.txt b/unix/kasmxproxy/CMakeLists.txt index 648438e..9319bb9 100644 --- a/unix/kasmxproxy/CMakeLists.txt +++ b/unix/kasmxproxy/CMakeLists.txt @@ -3,6 +3,6 @@ include_directories(${X11_INCLUDE_DIR}) add_executable(kasmxproxy kasmxproxy.c) -target_link_libraries(kasmxproxy ${X11_LIBRARIES}) +target_link_libraries(kasmxproxy ${X11_LIBRARIES} ${X11_XTest_LIB}) install(TARGETS kasmxproxy DESTINATION ${BIN_DIR}) diff --git a/unix/kasmxproxy/kasmxproxy.c b/unix/kasmxproxy/kasmxproxy.c index f74371f..bf07570 100644 --- a/unix/kasmxproxy/kasmxproxy.c +++ b/unix/kasmxproxy/kasmxproxy.c @@ -27,6 +27,7 @@ #include #include #include +#include #define min(a, b) ((a) < (b) ? (a) : (b)) @@ -109,7 +110,7 @@ int main(int argc, char **argv) { const int appscreen = DefaultScreen(appdisp); const int vncscreen = DefaultScreen(vncdisp); Visual *appvis = DefaultVisual(appdisp, appscreen); - Visual *vncvis = DefaultVisual(vncdisp, vncscreen); + //Visual *vncvis = DefaultVisual(vncdisp, vncscreen); const int appdepth = DefaultDepth(appdisp, appscreen); const int vncdepth = DefaultDepth(vncdisp, vncscreen); if (appdepth != vncdepth) { @@ -130,6 +131,15 @@ int main(int argc, char **argv) { XShmSegmentInfo shminfo; unsigned imgw = 0, imgh = 0; + if (XGrabPointer(vncdisp, vncroot, False, + ButtonPressMask | ButtonReleaseMask | PointerMotionMask, + GrabModeAsync, GrabModeAsync, None, None, + CurrentTime) != Success) + return 1; + if (XGrabKeyboard(vncdisp, vncroot, False, GrabModeAsync, GrabModeAsync, + CurrentTime) != Success) + return 1; + const unsigned sleeptime = 1000 * 1000 / fps; while (1) { @@ -174,8 +184,41 @@ int main(int argc, char **argv) { XShmGetImage(appdisp, approot, img, 0, 0, 0xffffffff); XPutImage(vncdisp, vncroot, gc, img, 0, 0, 0, 0, w, h); + // Handle events + while (XPending(vncdisp)) { + XEvent ev; + XNextEvent(vncdisp, &ev); + + switch (ev.type) { + case KeyPress: + case KeyRelease: + XTestFakeKeyEvent(appdisp, ev.xkey.keycode, + ev.type == KeyPress, + CurrentTime); + break; + case ButtonPress: + case ButtonRelease: + XTestFakeButtonEvent(appdisp, ev.xbutton.button, + ev.type == ButtonPress, + CurrentTime); + break; + case MotionNotify: + XTestFakeMotionEvent(appdisp, appscreen, + ev.xmotion.x, + ev.xmotion.y, + CurrentTime); + break; + default: + printf("Unexpected event type %u\n", ev.type); + break; + } + } + usleep(sleeptime); } + XCloseDisplay(appdisp); + XCloseDisplay(vncdisp); + return 0; }