Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix linux segfault when no display #84

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions mouse/_nixmouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,25 @@
x11 = None
def build_display():
global display, window, x11
if display and window and x11: return
if x11: return
x11 = ctypes.cdll.LoadLibrary(ctypes.util.find_library('X11'))
# Required because we will have multiple threads calling x11,
# such as the listener thread and then main using "move_to".
x11.XInitThreads()
# Explicitly set XOpenDisplay.restype to avoid segfault on 64 bit OS.
# http://stackoverflow.com/questions/35137007/get-mouse-position-on-linux-pure-python
x11.XOpenDisplay.restype = c_void_p
display = c_void_p(x11.XOpenDisplay(0))
open_display = x11.XOpenDisplay(0)
if open_display is None:
print('Warning: no display')
return
display = c_void_p(open_display)
window = x11.XDefaultRootWindow(display)

def get_position():
build_display()
if display is None:
return -1, -1
root_id, child_id = c_void_p(), c_void_p()
root_x, root_y, win_x, win_y = c_int(), c_int(), c_int(), c_int()
mask = c_uint()
Expand All @@ -37,6 +43,8 @@ def get_position():

def move_to(x, y):
build_display()
if display is None:
raise ValueError('Unable to move as no display available')
x11.XWarpPointer(display, None, window, 0, 0, 0, 0, x, y)
x11.XFlush(display)

Expand Down