summaryrefslogtreecommitdiffstats
path: root/tomit
blob: 429f89dd31a8c30c64c4d5960c60034abf6a235a (plain)
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
#!/usr/bin/python2
import signal
import time, sys
import subprocess, shlex
import daemon
import atexit

import setproctitle as setp

from Xlib.display import Display
from Xlib import X
from Xlib.protocol import request 
from Xlib.protocol import request,rq

class Tomit(object):
    def __init__(self,daemonize):
        setp.setproctitle("tomit")
        self.disp = Display()
        self.mask = X.PointerMotionMask | X.PointerMotionHintMask | X.LeaveWindowMask | X.EnterWindowMask
        self.root = self.disp.screen().root
        self.root.change_attributes(event_mask = self.mask)
        self.root.grab_pointer(1,self.mask,X.GrabModeSync,X.GrabModeAsync,X.NONE,X.NONE,X.CurrentTime)

        self.xin='xinput set-prop "PIXART USB OPTICAL MOUSE" "Device Enabled"'
        signal.signal(signal.SIGUSR1,self.renable)
        atexit.register(self.cleanup)
        if (daemonize == 1):
            with daemon.DaemonContext():
                self.loop()
        else:
            self.loop()

    def cleanup(self):
        proc = \
            subprocess.Popen(shlex.split(self.xin+' "1"'))
        proc.communicate()
        return proc.returncode

    def loop(self):
        while True:
            subprocess.Popen(shlex.split(self.xin+' "0"'))
            ev = self.disp.next_event()
            print repr(ev)
            time.sleep(100)

    def renable(self,signum,frame):
        subprocess.Popen(shlex.split(self.xin+' "1"'))
        self.disp.ungrab_pointer(X.CurrentTime)
        while True:
            proc = subprocess.Popen(shlex.split("inotifywait -t 60 \
                    /dev/input/by-id/usb-PIXART_USB_OPTICAL_MOUSE-event-mouse"))
            proc.communicate()
            if (proc.returncode == 2):
                print "Re-disabling pointer"
                subprocess.Popen(shlex.split(self.xin+' "0"'))
                self.root.grab_pointer(1,self.mask,X.GrabModeSync,X.GrabModeAsync,X.NONE,X.NONE,X.CurrentTime)
                break
            else:
                time.sleep(20)


if __name__ == "__main__":
    daemonize = 1
    if len(sys.argv)>1:
        daemonize = int(sys.argv[1])
    sys.exit(Tomit(daemonize))