From 84cfff24ab73228a91b3f0a8661fa3df0b6578ca Mon Sep 17 00:00:00 2001 From: nia Date: Fri, 11 Jul 2014 20:49:12 +0800 Subject: init --- mouse_action.py | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100755 mouse_action.py (limited to 'mouse_action.py') diff --git a/mouse_action.py b/mouse_action.py new file mode 100755 index 0000000..bcd48a1 --- /dev/null +++ b/mouse_action.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python2 + +from ctypes import * + +libxdo = None +xdo_handle = None +xdo_func_new = None +xdo_func_mouse_move = None +xdo_func_mouse_click = None + +class xdo_t(Structure): + pass + +def init_libxdo(): + global libxdo + global xdo_func_new + global xdo_func_mouse_move + global xdo_func_mouse_click + + try: + libxdo = cdll.LoadLibrary('libxdo.so.3') + xdo_func_new = getattr(libxdo, 'xdo_new') + xdo_func_mouse_move = getattr(libxdo, 'xdo_move_mouse_relative') + xdo_func_mouse_click = getattr(libxdo, 'xdo_click_window') + return True + except OSError: + pass + + try: + libxdo = cdll.LoadLibrary('libxdo.so.2') + xdo_func_new = getattr(libxdo, 'xdo_new') + xdo_func_mouse_move = getattr(libxdo, 'xdo_mousemove_relative') + xdo_func_mouse_click = getattr(libxdo, 'xdo_click') + return True + except OSError: + pass + + return False + + +def init_func_args(): + xdo_func_new.argtypes = [c_char_p] + xdo_func_new.restype = POINTER(xdo_t) + xdo_func_mouse_move.argtypes = [POINTER(xdo_t), c_int, c_int] + xdo_func_mouse_move.restype = c_int + xdo_func_mouse_click.argtypes = [POINTER(xdo_t), c_long, c_int] + xdo_func_mouse_click.restype = c_int + +def init_xdo_handle(): + global xdo_handle + xdo_handle = xdo_func_new(None) + if xdo_handle == None: + return False + return True + +def mouse_action(action): + if libxdo == None: + if not init_libxdo(): + raise 'Cannot load library: libxdo' + else: + init_func_args() + + if xdo_handle == None: + if not init_xdo_handle(): + raise 'Cannot create object: xdo_t' + + if action >= 1 and action <= 5: + xdo_func_mouse_click(xdo_handle, 0, action) + + +if __name__ == '__main__': + import sys + mouse_action(int(sys.argv[1])) -- cgit