Kaynağa Gözat

Made slop be really aggressive with mouse and keyboard grabs. Failing to grab the mouse results in a fatal shutdown now.

naelstrof 7 yıl önce
ebeveyn
işleme
3634e20f0e
3 değiştirilmiş dosya ile 27 ekleme ve 5 silme
  1. 1
    1
      CMakeLists.txt
  2. 10
    1
      src/keyboard.cpp
  3. 16
    3
      src/mouse.cpp

+ 1
- 1
CMakeLists.txt Dosyayı Görüntüle

@@ -21,7 +21,7 @@ endif()
21 21
 
22 22
 include_directories("${PROJECT_BINARY_DIR}")
23 23
 
24
-add_definitions(-DSLOP_VERSION="v5.3.31")
24
+add_definitions(-DSLOP_VERSION="v5.3.32")
25 25
 
26 26
 # The names have to be unique unfortunately.
27 27
 set(EXECUTABLE_NAME "slop")

+ 10
- 1
src/keyboard.cpp Dosyayı Görüntüle

@@ -1,3 +1,5 @@
1
+#include <chrono>
2
+#include <thread>
1 3
 #include "keyboard.hpp"
2 4
 
3 5
 bool slop::Keyboard::getKey( KeySym key ) {
@@ -41,8 +43,15 @@ void slop::Keyboard::update() {
41 43
 slop::Keyboard::Keyboard( X11* x11 ) {
42 44
     this->x11 = x11;
43 45
     int err = XGrabKeyboard( x11->display, x11->root, False, GrabModeAsync, GrabModeAsync, CurrentTime );
46
+    int tries = 0;
47
+    while( err != GrabSuccess && tries < 5 ) {
48
+        std::this_thread::sleep_for(std::chrono::milliseconds(100));
49
+        err = XGrabKeyboard( x11->display, x11->root, False, GrabModeAsync, GrabModeAsync, CurrentTime );
50
+        tries++;
51
+    }
52
+    // Non-fatal.
44 53
     if ( err != GrabSuccess ) {
45
-        //throw new std::runtime_error( "Failed to grab keyboard.\n" );
54
+        //throw new std::runtime_error( "Couldn't grab the keyboard after 10 tries." );
46 55
     }
47 56
     XQueryKeymap( x11->display, deltaState );
48 57
     keyDown = false;

+ 16
- 3
src/mouse.cpp Dosyayı Görüntüle

@@ -1,3 +1,5 @@
1
+#include <chrono>
2
+#include <thread>
1 3
 #include "mouse.hpp"
2 4
 
3 5
 void slop::Mouse::setButton( int button, int state ) {
@@ -44,9 +46,20 @@ slop::Mouse::Mouse(X11* x11, int nodecorations, Window ignoreWindow ) {
44 46
     currentCursor = XC_cross;
45 47
     xcursor = XCreateFontCursor( x11->display, XC_cross );
46 48
     hoverWindow = None;
47
-    XGrabPointer( x11->display, x11->root, True,
48
-                  PointerMotionMask | ButtonPressMask | ButtonReleaseMask | EnterWindowMask,
49
-                  GrabModeAsync, GrabModeAsync, None, xcursor, CurrentTime );
49
+    int err = XGrabPointer( x11->display, x11->root, True,
50
+                            PointerMotionMask | ButtonPressMask | ButtonReleaseMask | EnterWindowMask,
51
+                            GrabModeAsync, GrabModeAsync, None, xcursor, CurrentTime );
52
+    int tries = 0;
53
+    while( err != GrabSuccess && tries < 5 ) {
54
+        std::this_thread::sleep_for(std::chrono::milliseconds(100));
55
+        err = XGrabPointer( x11->display, x11->root, True,
56
+                                PointerMotionMask | ButtonPressMask | ButtonReleaseMask | EnterWindowMask,
57
+                                GrabModeAsync, GrabModeAsync, None, xcursor, CurrentTime );
58
+        tries++;
59
+    }
60
+    if ( err != GrabSuccess ) {
61
+        throw new std::runtime_error( "Couldn't grab the mouse after 10 tries." );
62
+    }
50 63
     this->nodecorations = nodecorations;
51 64
     this->ignoreWindow = ignoreWindow;
52 65
     hoverWindow = findWindow(x11->root);