Skip to content

Instantly share code, notes, and snippets.

@prideout
Created May 22, 2012 22:45
Show Gist options
  • Save prideout/2772103 to your computer and use it in GitHub Desktop.
Save prideout/2772103 to your computer and use it in GitHub Desktop.
use libxdo to simulate the mouse
#!/usr/bin/python
from ctypes import *
import time, random
import os
CURRENTWINDOW = 0
# nm -D /usr/lib64/libxdo.so.2 | grep mouselocation
# http://python.net/crew/theller/ctypes/tutorial.html#passing-pointers-or-passing-parameters-by-reference
# http://www.semicomplete.com/files/xdotool/docs/html/xdo_8h.html
if __name__ == '__main__':
libxdo = CDLL('libxdo.so.2')
xdo = libxdo.xdo_new(':0.0')
#print('close display when freed: {0}\n'.format(cast(xdo, POINTER(xdo_t)).contents.close_display_when_freed))
#libxdo.xdo_type(xdo, CURRENTWINDOW, 'hello', 10)
print 'Move Slim flush to the left side of your screen, vertically sized to one-third your desktop height.'
print 'Make sure your appearance hierarchy is big enough to fill the pane'
print 'Move the divider to Y = 733 and bottom edge of window to 1208'
print 'Move the mouse to left side of the screen to start the Drag-And-Drop madness.'
cur_x, cur_y, cur_screen, cur_window = c_int(), c_int(), c_int(), c_int()
while True:
libxdo.xdo_mouselocation(xdo, byref(cur_x), byref(cur_y), byref(cur_screen))
print "Mouse: ", cur_x.value, cur_y.value, cur_screen.value
time.sleep(1)
if cur_x.value == 0:
break
dnd_x = 20
dnd_y_min = 771
dnd_y_max = 1208
MMB = 2 # 2 or 5
print 'Starting the test. Move mouse to left side of the screen to stop.'
while True:
# Move the mouse to starting point of drag
dnd_y = random.randint(dnd_y_min, dnd_y_max)
libxdo.xdo_mousemove(xdo, dnd_x, dnd_y, cur_screen)
# Push MMB, move, and release
libxdo.xdo_mousedown(xdo, 0, MMB)
drag_target = random.randint(dnd_y_min, dnd_y_max)
y = float(dnd_y)
dy = 0.1 * (drag_target - y)
timeout = 100
while abs(y - drag_target) > 1 and timeout > 0:
libxdo.xdo_mousemove(xdo, dnd_x, int(y), cur_screen)
time.sleep(0.05)
y += dy
timeout = timeout - 1
libxdo.xdo_mouseup(xdo, 0, MMB)
# Relax in case the user wants to interrupt
time.sleep(1)
cur_x, cur_y, cur_screen = c_int(), c_int(), c_int()
libxdo.xdo_mouselocation(xdo, byref(cur_x), byref(cur_y), byref(cur_screen))
if cur_x.value == 0:
break
libxdo.xdo_free(xdo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment