From 1350e85b3361b7df8bda8e4956425870f9e1ea82 Mon Sep 17 00:00:00 2001 From: tianduanrui Date: Sat, 31 Aug 2019 09:49:35 +0800 Subject: [PATCH] update test case, mouse locker, linux code. --- test/testmouselocker11/qqtmouselocker_win.cpp | 29 ++-- test/testmouselocker11/qqtmouselocker_x11.cpp | 143 ++++++++++++++++++ test/testmouselocker11/testmouselocker11.pro | 2 + 3 files changed, 161 insertions(+), 13 deletions(-) diff --git a/test/testmouselocker11/qqtmouselocker_win.cpp b/test/testmouselocker11/qqtmouselocker_win.cpp index 8e648feb..51a3f818 100644 --- a/test/testmouselocker11/qqtmouselocker_win.cpp +++ b/test/testmouselocker11/qqtmouselocker_win.cpp @@ -47,13 +47,15 @@ void QQtMouseLockerImpl::focusInEvent ( QFocusEvent* event, QWidget* target ) p0 = w.mapToGlobal ( p0 ); p1 = w.mapToGlobal ( p1 ); - QRect r0 = QRect ( p0, p1 ); - mainWinRect.left = ( LONG ) r0.left(); - mainWinRect.right = ( LONG ) r0.right(); - mainWinRect.top = ( LONG ) r0.top(); - mainWinRect.bottom = ( LONG ) r0.bottom(); + qreal ratio = w.devicePixelRatioF(); - qr0 = r0; + QRect r0 = QRect ( p0, p1 ); + mainWinRect.left = ( LONG ) r0.left() * ratio; + mainWinRect.right = ( LONG ) r0.right() * ratio; + mainWinRect.top = ( LONG ) r0.top() * ratio; + mainWinRect.bottom = ( LONG ) r0.bottom() * ratio; + + qr0 = QRect ( QPoint ( r0.left() * ratio, r0.top() * ratio ), QPoint ( r0.right() * ratio, r0.bottom() * ratio ) ); } #endif @@ -68,7 +70,6 @@ void QQtMouseLockerImpl::focusInEvent ( QFocusEvent* event, QWidget* target ) && r0.top != mainWinRect.top && r0.bottom != mainWinRect.bottom ) { - //BUG, NEED MOD! ClipCursor ( NULL ); ClipCursor ( &mainWinRect ); //这是Windows API #if 1 @@ -120,13 +121,15 @@ void QQtMouseLockerImpl::focusOutEvent ( QFocusEvent* event, QWidget* target ) p0 = w.mapToGlobal ( p0 ); p1 = w.mapToGlobal ( p1 ); - QRect r0 = QRect ( p0, p1 ); - mainWinRect.left = ( LONG ) r0.left(); - mainWinRect.right = ( LONG ) r0.right(); - mainWinRect.top = ( LONG ) r0.top(); - mainWinRect.bottom = ( LONG ) r0.bottom(); + qreal ratio = w.devicePixelRatioF(); - qr0 = r0; + QRect r0 = QRect ( p0, p1 ); + mainWinRect.left = ( LONG ) r0.left() * ratio; + mainWinRect.right = ( LONG ) r0.right() * ratio; + mainWinRect.top = ( LONG ) r0.top() * ratio; + mainWinRect.bottom = ( LONG ) r0.bottom() * ratio; + + qr0 = QRect ( QPoint ( r0.left() * ratio, r0.top() * ratio ), QPoint ( r0.right() * ratio, r0.bottom() * ratio ) ); } #endif diff --git a/test/testmouselocker11/qqtmouselocker_x11.cpp b/test/testmouselocker11/qqtmouselocker_x11.cpp index 504efac0..0400fbd7 100644 --- a/test/testmouselocker11/qqtmouselocker_x11.cpp +++ b/test/testmouselocker11/qqtmouselocker_x11.cpp @@ -5,6 +5,149 @@ #include #include +#include +#include +#include +#include +#include +#include +#include +#include + +Display *display; +bool usenotify = true; + +// Get the current window the mouse is over +Window GetMouseWindow( Display *d ) { + Window unusedwin; + Window win; + int unuseddata; + unsigned int mask; + + do { + XQueryPointer(d, DefaultRootWindow(d), &unusedwin, &win, + &unuseddata, &unuseddata, &unuseddata, &unuseddata, &mask); + } while(win <= 0); + + return(win); +} + +// Check if there is already a running process using a pid file +bool ison() { + int pid_file = open("/tmp/mousetrap.pid", O_CREAT | O_RDWR, 0666); + int rc = flock(pid_file, LOCK_EX | LOCK_NB); + if( rc ) { + if( errno == EWOULDBLOCK ) { + //process is running + return true; + } + } else { + //process is not running, write the pid file + pid_t pid = getpid(); + FILE *fp = fopen("/tmp/mousetrap.pid", "w"); + if( !fp ) { exit(1); } + fprintf(fp, "%d\n", pid); + fclose(fp); + + return false; + } +} + +int main_function( int argc, char* argv[] ) { + + int opt; + int offset_top = 34; // offset the window decorations on top + int offset_bot = 12; + int offset_right = 12; + int offset_left = 12; + + while( (opt = getopt( argc, argv, "nt:b:l:r:hv")) != -1 ) { + switch(opt) { + case 'n': + usenotify = false; + break; + case 't': + offset_top = atoi(optarg); + break; + case 'b': + offset_bot = atoi(optarg); + break; + case 'l': + offset_left = atoi(optarg); + break; + case 'r': + offset_right = atoi(optarg); + break; + case 'v': + printf("Mousetrap v1.0\nCopyright (C) 2015 Greg Holkenbrink\nMIT License see README for details\n"); + break; + case 'h': + default: + fprintf(stderr, "Usage: %s [-h] display this message [-n] turn off notify [-t offset_top=34] [-b offset_bot=12] [-l offset_left=12] [-r offset_right=12] [-v] display version info\n", argv[0]); + exit(EXIT_FAILURE); + } + } + + // We want mousetrap to act as a toggle. If it's already + // running kill the process, otherwise start up + if( ison() ) { + // kill other process and exit + pid_t pid = 0; + FILE *fp = fopen("/tmp/mousetrap.pid", "r"); + if(!fp) { exit(1); } + if( fscanf(fp, "%d\n", &pid) == 1) { + kill(pid, SIGKILL); + } + unlink("/tmp/mousetrap.pid"); + if( usenotify ) { int ret = system("notify-send -t 2400 \"Mousetrap: OFF\""); } + exit(0); + } + if( usenotify ) { int ret = system("notify-send -t 2400 \"Mousetrap: ON\""); } + + display = XOpenDisplay(""); + if( display == NULL ) { + printf("Cannot connect to xserver\n"); + exit(-1); + } + + Window root_window = DefaultRootWindow(display); + Window current_window = GetMouseWindow(display); + XWindowAttributes info_data; + Status info = XGetWindowAttributes( display, current_window, &info_data); + + int max_x = info_data.x+info_data.width; + int max_y = info_data.y+info_data.height; + int min_x = info_data.x; + int min_y = info_data.y; + + current_window = GetMouseWindow(display); + + Window unusedwin; + Window win; + int unuseddata; + int cur_x; + int cur_y; + unsigned int mask; + + do { + XQueryPointer(display, current_window, &unusedwin, &win, &cur_x, &cur_y, &unuseddata, &unuseddata, &mask); + + bool warp = false; + + if( cur_x > max_x-offset_right ) { warp = true; cur_x = max_x-offset_right; } + if( cur_y > max_y-offset_bot ) { warp = true; cur_y = max_y-offset_bot; } + if( cur_x < min_x+offset_left ) { warp = true; cur_x = min_x+offset_left; } + if( cur_y < min_y+offset_top ) { warp = true; cur_y = min_y+offset_top; } + + if( warp ) { XWarpPointer( display, None, root_window, 0, 0, 0, 0, cur_x, cur_y ); } + + usleep(300); + } while(true); + + XCloseDisplay(display); + return 0; +} + QQtMouseLockerImpl::QQtMouseLockerImpl() {} void QQtMouseLockerImpl::focusInEvent ( QFocusEvent* event, QWidget* target ) diff --git a/test/testmouselocker11/testmouselocker11.pro b/test/testmouselocker11/testmouselocker11.pro index e813572d..4d3497aa 100644 --- a/test/testmouselocker11/testmouselocker11.pro +++ b/test/testmouselocker11/testmouselocker11.pro @@ -88,6 +88,8 @@ win32:!cross_compile { SOURCES += \ qqtmouselocker_mac.cpp } else:linux:!cross_compile { + LIBS += -lX11 + QT += x11extras HEADERS += \ qqtmouselocker_x11.h SOURCES += \