Skip to content
This repository has been archived by the owner on Nov 4, 2021. It is now read-only.

Commit

Permalink
check the return values of the grab functions correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianriese committed Sep 5, 2013
1 parent 510b888 commit 50a8522
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Expand Up @@ -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
18 changes: 14 additions & 4 deletions lib/xcb.py
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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):
Expand All @@ -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)]
Expand Down
25 changes: 15 additions & 10 deletions pyxtrlock
Expand Up @@ -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)
Expand All @@ -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:
Expand Down

0 comments on commit 50a8522

Please sign in to comment.