Permalink
Browse files

check the return values of the grab functions correctly

  • Loading branch information...
1 parent 510b888 commit 50a8522392809a5688638d074fb9f84264c8b58d @sebastianriese sebastianriese committed Sep 5, 2013
Showing with 31 additions and 14 deletions.
  1. +2 −0 CHANGELOG
  2. +14 −4 lib/xcb.py
  3. +15 −10 pyxtrlock
View
@@ -4,3 +4,5 @@ Release 0.1:
Paul Lhussiez.
Development Version:
• Enhancement: Report missing libraries when loading via ctypes
+• Security: Check correctly for the result of the
+ xcb_grab_{pointer,keyboard} commands
View
@@ -412,8 +412,8 @@ def grab_keyboard_sync(conn, owner_events, grab_window, time, ptr_mode,
"""
Synchronously grab the keyboard.
- Wrapper function for grab_pointer and grab_pointer_reply.
- Raises ``XCBError`` on error, otherwise returns ``GrabKeyboardReply``.
+ Wrapper function for grab_pointer and grab_pointer_reply. Returns
+ the status field from the reply. Raises ``XCBError`` on error.
"""
owner_events = 1 if owner_events else 0
@@ -424,7 +424,9 @@ def grab_keyboard_sync(conn, owner_events, grab_window, time, ptr_mode,
if error_p:
raise XCBError(error_p.contents)
- return kbd_grab
+ status = kbd_grab.contents.status
+ free(kbd_grab)
+ return status
grab_pointer = libxcb.xcb_grab_pointer
@@ -449,6 +451,12 @@ def grab_keyboard_sync(conn, owner_events, grab_window, time, ptr_mode,
]
grab_pointer_reply.restype = POINTER(GrabPointerReply)
+# constants to interpret grab results
+GrabSuccess = 0
+AlreadyGrabbed = 1
+GrabInvalidTime = 2
+GrabNotViewable = 3
+GrabFrozen = 4
def grab_pointer_sync(conn, owner_events, window, event_mask, ptr_mode,
kbd_mode, confine_to, cursor, timestamp):
@@ -466,7 +474,9 @@ def grab_pointer_sync(conn, owner_events, window, event_mask, ptr_mode,
ptr_grab = grab_pointer_reply(conn, cookie, byref(error_p))
if error_p:
raise XCBError(error_p.contents)
- return ptr_grab
+ status = ptr_grab.contents.status
+ free(ptr_grab)
+ return status
wait_for_event_ = libxcb.xcb_wait_for_event
wait_for_event_.argtypes = [POINTER(Connection)]
View
@@ -117,9 +117,11 @@ xcb.map_window(conn, window)
# Grab keyboard
try:
- kbd_grab = xcb.grab_keyboard_sync(conn, 0, window, xcb.CURRENT_TIME,
- xcb.GRAB_MODE_ASYNC, xcb.GRAB_MODE_ASYNC)
- xcb.free(kbd_grab)
+ status = xcb.grab_keyboard_sync(conn, 0, window, xcb.CURRENT_TIME,
+ xcb.GRAB_MODE_ASYNC, xcb.GRAB_MODE_ASYNC)
+
+ if status != xcb.GrabSuccess:
+ panic("pyxtrlock: Could not get grab keyboard")
except xcb.XCBError as e:
print("pyxtrlock: Could not get grab keyboard", file=sys.stderr)
sys.exit(1)
@@ -133,13 +135,16 @@ except xcb.XCBError as e:
# (i.e. after 1s in total), then give up, and emit an error"
for i in range(100):
try:
- ptr_grab = xcb.grab_pointer_sync(conn, False, window, 0,
- xcb.GRAB_MODE_ASYNC,
- xcb.GRAB_MODE_ASYNC,
- xcb.WINDOW_NONE, cursor,
- xcb.CURRENT_TIME)
- xcb.free(ptr_grab)
- break
+ status = xcb.grab_pointer_sync(conn, False, window, 0,
+ xcb.GRAB_MODE_ASYNC,
+ xcb.GRAB_MODE_ASYNC,
+ xcb.WINDOW_NONE, cursor,
+ xcb.CURRENT_TIME)
+
+ if status == xcb.GrabSuccess:
+ break
+ else:
+ time.sleep(0.01)
except xcb.XCBError as e:
time.sleep(0.01)
else:

0 comments on commit 50a8522

Please sign in to comment.