-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary-mouse.py
133 lines (96 loc) · 3.13 KB
/
binary-mouse.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#! /usr/bin/python3
import sys
import subprocess as sp
import json
import os
DIV_CONST = 2
def get_state():
state = {}
try:
with open("/tmp/binary_mouse.tmp") as file:
state = json.loads(file.read())
except:
pass
print(state)
cmd = "xdotool getmouselocation"
print(cmd)
out = sp.run(cmd.split(" "), capture_output=True).stdout.decode("UTF-8")
mousepos = {x.split(":")[0]:int(x.split(":")[1]) for x in out.split()}
print(mousepos)
if "x" in state and "y" in state:
if state["x"] != mousepos["x"] or state["y"] != mousepos["y"]:
print("has_moved")
# mouse has moved; reset binary search
for x in ["left", "right", "up", "down"]:
if x in state:
state.pop(x)
state.update(mousepos)
cmd = "xrandr | grep 'Screen {}' | sed -E 's/^.*current ([0-9]+) x ([0-9]+).*$/\\1 \\2/'".format(state["screen"])
print(cmd)
out = sp.run(cmd,capture_output=True,shell=True)
state.update({x:int(y) for x,y in zip(["screen_width","screen_height"],out.stdout.split())})
print(state)
return state
def update_file(state):
with open("/tmp/binary_mouse.tmp","w") as file:
file.write(json.dumps(state))
print(sys.argv)
if len(sys.argv) < 2:
exit()
if sys.argv[1] == "bin-left":
# sp.run("notify-send bin-left",shell=True)
state = get_state()
left = int(state["x"] / DIV_CONST)
if "left" in state:
left = state["left"]
cmd = "xdotool mousemove_relative -- {} {}".format(-left, 0)
print(cmd)
state["x"] -= left
state["left"] = int(left / DIV_CONST)
state["right"] = int(left / DIV_CONST)
update_file(state)
sp.run(cmd.split())
if sys.argv[1] == "bin-right":
# sp.run("notify-send bin-right",shell=True)
state = get_state()
right = int((state["screen_width"] - state["x"]) / DIV_CONST)
if "right" in state:
right = state["right"]
cmd = "xdotool mousemove_relative -- {} {}".format(right, 0)
print(cmd)
state["x"] += right
state["left"] = int(right / DIV_CONST)
state["right"] = int(right / DIV_CONST)
update_file(state)
sp.run(cmd.split())
if sys.argv[1] == "bin-down":
# sp.run("notify-send bin-down",shell=True)
state = get_state()
down = int((state["screen_height"] - state["y"]) / DIV_CONST)
if "down" in state:
down = state["down"]
cmd = "xdotool mousemove_relative -- {} {}".format(0, down)
print(cmd)
state["y"] += down
state["up"] = int(down / DIV_CONST)
state["down"] = int(down / DIV_CONST)
update_file(state)
sp.run(cmd.split())
if sys.argv[1] == "bin-up":
# sp.run("notify-send bin-up",shell=True)
state = get_state()
up = int(state["y"] / DIV_CONST)
if "up" in state:
up = state["up"]
cmd = "xdotool mousemove_relative -- {} {}".format(0, -up)
print(cmd)
state["y"] -= up
state["up"] = int(up / DIV_CONST)
state["down"] = int(up / DIV_CONST)
update_file(state)
sp.run(cmd.split())
if sys.argv[1] == "clear":
try:
os.remove("/tmp/binary_mouse.tmp")
except:
pass