Repository navigation
Expand file tree
/
Copy pathrun.py
More file actions
342 lines (270 loc) · 10.1 KB
/
Copy pathrun.py
File metadata and controls
342 lines (270 loc) · 10.1 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/env python3
import sys
import random
import time
import math
import argparse
import pyautogui
# Optional dependency (only used if you re-enable multi-monitor logic)
# from screeninfo import get_monitors
# ----------------------------
# Config
# ----------------------------
INACTIVITY_TIMEOUT_MIN = 70
INACTIVITY_TIMEOUT_MAX = 100
TEST_MODE = False
RADIUS = 50
# ----------------------------
# Arg parsing
# ----------------------------
def parse_range(range_str: str):
"""
Parse and validate the range string in the format 'min-max'.
Returns: (min, max) as ints
"""
try:
parts = range_str.split("-")
if len(parts) != 2:
raise ValueError
a = int(parts[0])
b = int(parts[1])
except ValueError:
raise argparse.ArgumentTypeError(
f"Range '{range_str}' is invalid. It must be in the format 'min-max' where min and max are integers."
)
if not (1 <= a < b <= 1199):
raise argparse.ArgumentTypeError(
f"Invalid range '{range_str}'. Ensure that 1 <= min < max <= 1199."
)
return (a, b)
def parse_arguments():
parser = argparse.ArgumentParser(
description="Moves the mouse after a set inactivity timeout."
)
parser.add_argument(
"range",
type=parse_range,
nargs="?",
default=None,
help="The timeout range in the format 'min-max' in seconds where 1 <= min < max <= 1199.",
)
parser.add_argument(
"--test",
action="store_true",
help="Enable test mode.",
)
args = parser.parse_args()
global INACTIVITY_TIMEOUT_MAX, INACTIVITY_TIMEOUT_MIN, TEST_MODE
if args.range:
mn, mx = args.range
INACTIVITY_TIMEOUT_MIN = mn
INACTIVITY_TIMEOUT_MAX = mx
TEST_MODE = args.test
# ----------------------------
# Platform mouse movement
# ----------------------------
_MOVE_BACKEND = None
def _setup_mouse_backend():
"""
Selects a low-level-ish backend per platform:
- macOS: Quartz.CGEventCreateMouseEvent + CGEventPost
- Windows: SendInput (ctypes)
- Linux: python-xlib + XTEST
- Other/Unavailable: raise backend-not-found error
Sets global _MOVE_BACKEND callable: move_mouse(x,y)
"""
global _MOVE_BACKEND
if sys.platform == "darwin":
# macOS Quartz backend
import Quartz # type: ignore
def move_mouse_mac(x, y):
ev = Quartz.CGEventCreateMouseEvent(
None, Quartz.kCGEventMouseMoved, (int(x), int(y)), 0
)
Quartz.CGEventPost(Quartz.kCGHIDEventTap, ev)
_MOVE_BACKEND = move_mouse_mac
return
if sys.platform == "win32":
# Windows SendInput backend (supports multi-monitor / negative coords)
import ctypes
from ctypes import wintypes
user32 = ctypes.WinDLL("user32", use_last_error=True)
# (Optional but helpful) Make process DPI-aware so coordinates match what Windows uses.
# This avoids "cursor moves to wrong spot" under scaling in many setups.
try:
# Per-monitor DPI awareness (Win 8.1+ / 10+)
shcore = ctypes.WinDLL("shcore", use_last_error=True)
shcore.SetProcessDpiAwareness(2) # PROCESS_PER_MONITOR_DPI_AWARE
except Exception:
try:
user32.SetProcessDPIAware()
except Exception:
pass
ULONG_PTR = wintypes.WPARAM
INPUT_MOUSE = 0
MOUSEEVENTF_MOVE = 0x0001
MOUSEEVENTF_ABSOLUTE = 0x8000
MOUSEEVENTF_VIRTUALDESK = 0x4000
SM_XVIRTUALSCREEN = 76
SM_YVIRTUALSCREEN = 77
SM_CXVIRTUALSCREEN = 78
SM_CYVIRTUALSCREEN = 79
class MOUSEINPUT(ctypes.Structure):
_fields_ = [
("dx", wintypes.LONG),
("dy", wintypes.LONG),
("mouseData", wintypes.DWORD),
("dwFlags", wintypes.DWORD),
("time", wintypes.DWORD),
("dwExtraInfo", ULONG_PTR),
]
class INPUT(ctypes.Structure):
_fields_ = [("type", wintypes.DWORD), ("mi", MOUSEINPUT)]
def _get_virtual_screen_rect():
vx = user32.GetSystemMetrics(SM_XVIRTUALSCREEN)
vy = user32.GetSystemMetrics(SM_YVIRTUALSCREEN)
vw = user32.GetSystemMetrics(SM_CXVIRTUALSCREEN)
vh = user32.GetSystemMetrics(SM_CYVIRTUALSCREEN)
return vx, vy, vw, vh
def move_mouse_win(x, y):
vx, vy, vw, vh = _get_virtual_screen_rect()
# Normalize to 0..65535 across *virtual desktop* (inclusive)
nx = int((int(x) - vx) * 65535 / (vw - 1)) if vw > 1 else 0
ny = int((int(y) - vy) * 65535 / (vh - 1)) if vh > 1 else 0
inp = INPUT(
type=INPUT_MOUSE,
mi=MOUSEINPUT(
dx=nx,
dy=ny,
mouseData=0,
dwFlags=MOUSEEVENTF_MOVE
| MOUSEEVENTF_ABSOLUTE
| MOUSEEVENTF_VIRTUALDESK,
time=0,
dwExtraInfo=0,
),
)
n = user32.SendInput(1, ctypes.byref(inp), ctypes.sizeof(inp))
if n != 1:
raise ctypes.WinError(ctypes.get_last_error())
_MOVE_BACKEND = move_mouse_win
return
if sys.platform.startswith("linux"):
# Prefer python-xlib + XTEST on Linux for low-level event injection on X11.
try:
from Xlib import display as xdisplay # type: ignore
from Xlib.ext import xtest # type: ignore
disp = xdisplay.Display()
root = disp.screen().root
def move_mouse_linux_xlib(x, y):
root.warp_pointer(int(x), int(y))
xtest.fake_input(disp, 6, 0, x=int(x), y=int(y))
disp.sync()
_MOVE_BACKEND = move_mouse_linux_xlib
return
except Exception:
pass
message = (
"No compatible mouse backend found. "
"On Linux/X11 install python-xlib; "
"on macOS ensure Quartz is available; on Windows SendInput is built-in."
)
print(f"Warning: {message}", file=sys.stderr)
raise RuntimeError(message)
def move_mouse(x, y):
if _MOVE_BACKEND is None:
_setup_mouse_backend()
_MOVE_BACKEND(x, y)
# ----------------------------
# Movement path logic
# ----------------------------
def move_coordinates(start_x, start_y, coordinates):
for x, y in coordinates:
move_mouse(int(start_x + x), int(start_y + y))
time.sleep(0.003)
def infinity_points(radius, center_distance):
points = []
center1_x, center2_x = center_distance // 2, 3 * center_distance // 2
center_y = radius
# Upper half of the left circle (right to left)
for theta in range(0, 181, 1):
radian = math.radians(theta)
x = int(radius * math.cos(radian)) + center1_x
y = int(radius * math.sin(radian)) + center_y
points.append((x, y))
# Lower half of the left circle (left to right)
for theta in range(180, 361, 1):
radian = math.radians(theta)
x = int(radius * math.cos(radian)) + center1_x
y = int(radius * math.sin(radian)) + center_y
points.append((x, y))
# Upper half of the right circle (left to right)
for theta in range(180, -1, -1):
radian = math.radians(theta)
x = int(radius * math.cos(radian)) + center2_x
y = int(radius * math.sin(radian)) + center_y
points.append((x, y))
# Lower half of the right circle (right to left)
for theta in range(360, 179, -1):
radian = math.radians(theta)
x = int(radius * math.cos(radian)) + center2_x
y = int(radius * math.sin(radian)) + center_y
points.append((x, y))
return points
# ----------------------------
# Monitor / positioning helpers
# ----------------------------
def get_active_monitor_middle():
w, h = pyautogui.size()
return (w / 2, h / 2)
def get_start_points(middle_x, middle_y):
return middle_x - RADIUS * 2, middle_y - RADIUS
def disable_icon_macos():
try:
from AppKit import NSApplication, NSApplicationActivationPolicyAccessory # type: ignore
app = NSApplication.sharedApplication()
app.setActivationPolicy_(NSApplicationActivationPolicyAccessory)
except Exception as e:
print("Failed to remove dock icon:", e)
# ----------------------------
# Main loop
# ----------------------------
def infinity_movement():
parse_arguments()
if sys.platform == "darwin":
disable_icon_macos()
# init backend once
_setup_mouse_backend()
active_middle_x, active_middle_y = get_active_monitor_middle()
movement_coordinates = infinity_points(RADIUS, RADIUS * 2)
if TEST_MODE:
print("Started test!")
start_x, start_y = get_start_points(active_middle_x, active_middle_y)
move_coordinates(start_x, start_y, movement_coordinates)
print("Finished...")
sys.exit(0)
try:
print("Started!")
# Test if movement possible (permissions etc.)
move_mouse(active_middle_x, active_middle_y)
while True:
delay = random.uniform(INACTIVITY_TIMEOUT_MIN, INACTIVITY_TIMEOUT_MAX)
last_pos = pyautogui.position()
start_time = time.monotonic()
while True:
current_pos = pyautogui.position()
if current_pos != last_pos:
last_pos = current_pos
start_time = time.monotonic()
elif time.monotonic() - start_time >= delay:
break
time.sleep(0.3)
# reset middle before move
active_middle_x, active_middle_y = get_active_monitor_middle()
start_x, start_y = get_start_points(active_middle_x, active_middle_y)
move_coordinates(start_x, start_y, movement_coordinates)
print(".", end="", flush=True)
except KeyboardInterrupt:
print("\nExit...")
if __name__ == "__main__":
infinity_movement()