Browse Source

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

naelstrof 7 years ago
parent
commit
3634e20f0e
3 changed files with 27 additions and 5 deletions
  1. 1
    1
      CMakeLists.txt
  2. 10
    1
      src/keyboard.cpp
  3. 16
    3
      src/mouse.cpp

+ 1
- 1
CMakeLists.txt View File

21
 
21
 
22
 include_directories("${PROJECT_BINARY_DIR}")
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
 # The names have to be unique unfortunately.
26
 # The names have to be unique unfortunately.
27
 set(EXECUTABLE_NAME "slop")
27
 set(EXECUTABLE_NAME "slop")

+ 10
- 1
src/keyboard.cpp View File

1
+#include <chrono>
2
+#include <thread>
1
 #include "keyboard.hpp"
3
 #include "keyboard.hpp"
2
 
4
 
3
 bool slop::Keyboard::getKey( KeySym key ) {
5
 bool slop::Keyboard::getKey( KeySym key ) {
41
 slop::Keyboard::Keyboard( X11* x11 ) {
43
 slop::Keyboard::Keyboard( X11* x11 ) {
42
     this->x11 = x11;
44
     this->x11 = x11;
43
     int err = XGrabKeyboard( x11->display, x11->root, False, GrabModeAsync, GrabModeAsync, CurrentTime );
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
     if ( err != GrabSuccess ) {
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
     XQueryKeymap( x11->display, deltaState );
56
     XQueryKeymap( x11->display, deltaState );
48
     keyDown = false;
57
     keyDown = false;

+ 16
- 3
src/mouse.cpp View File

1
+#include <chrono>
2
+#include <thread>
1
 #include "mouse.hpp"
3
 #include "mouse.hpp"
2
 
4
 
3
 void slop::Mouse::setButton( int button, int state ) {
5
 void slop::Mouse::setButton( int button, int state ) {
44
     currentCursor = XC_cross;
46
     currentCursor = XC_cross;
45
     xcursor = XCreateFontCursor( x11->display, XC_cross );
47
     xcursor = XCreateFontCursor( x11->display, XC_cross );
46
     hoverWindow = None;
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
     this->nodecorations = nodecorations;
63
     this->nodecorations = nodecorations;
51
     this->ignoreWindow = ignoreWindow;
64
     this->ignoreWindow = ignoreWindow;
52
     hoverWindow = findWindow(x11->root);
65
     hoverWindow = findWindow(x11->root);