aboutsummaryrefslogtreecommitdiffstats
path: root/mouse_action.py
blob: bcd48a1ea3614e8dbe8a6c67072cdd286264879b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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]))