-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcursor_app.py
More file actions
254 lines (216 loc) · 8.62 KB
/
Copy pathcursor_app.py
File metadata and controls
254 lines (216 loc) · 8.62 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
import sys
import ctypes
import ctypes.wintypes as wintypes
import base64
import io
import threading
import tkinter as tk
from PIL import Image, ImageDraw, ImageTk
import pystray
CURSOR_B64 = (
"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAACXBIWXMAAA7DAAAOwwHH"
"b6hkAAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAAORJREFUOI3N"
"kT1OAzEQhd9YFkq/HCAnsI9AqhyAA1BQcID0XCEdSBSR6NJtHaqVUtBQrDRuuAEdTRIh"
"JORHkyDiXXuhy6s8P+/TjAc4OanqXFXfVfWWpAz1dxpU9QPACABI1gCuvPe7HMD05L5+"
"6CKXIvLctu34P4BNEjtr7UsIYfIngIhs0xzJc5JPIYTrQQDJdIKDzkguVPWhaRqbBaC7"
"QqqbqqoeS4DOCj26ODxsWiG5FTm67ieAJYDXfT2KyCoLMMZsSP42T51z69woxU8kOSuZ"
"ewExxhrAG4A77/19yXwa+gaMWlo+Fi1JRgAAAABJRU5ErkJggg=="
)
EASE_BASE = 0.16
SIZE_IDLE = 32
SIZE_CLICK = 43
SNAP = 0.04
CLICK_SHRINK_MS = 300
CHROMA_TK = '#FF00FF'
CHROMA_BGR = 0xFF00FF
GWL_EXSTYLE = -20
WS_EX_LAYERED = 0x00080000
WS_EX_TRANSPARENT = 0x00000020
WS_EX_NOACTIVATE = 0x08000000
LWA_COLORKEY = 0x00000001
HWND_TOPMOST = -1
SWP_NOMOVE = 0x0002
SWP_NOSIZE = 0x0001
SWP_NOACTIVATE = 0x0010
_ALL_CURSOR_IDS = [
32512, 32513, 32514, 32515, 32516,
32640, 32641, 32642, 32643, 32644,
32645, 32646, 32648, 32649, 32650,
]
_OVERLAY_TITLE = "MysterioCursor_Overlay_8k3z9"
_mutex = ctypes.windll.kernel32.CreateMutexW(None, True, "Global\\MysterioCursorSingleInstance")
if ctypes.windll.kernel32.GetLastError() == 183:
ctypes.windll.user32.MessageBoxW(
0, "Mysterio Cursor is already running.", "Mysterio Cursor", 0x40
)
sys.exit(0)
_CCHDEVICENAME = 32
_CCHFORMNAME = 32
class _DEVMODEW(ctypes.Structure):
_fields_ = [
("dmDeviceName", wintypes.WCHAR * _CCHDEVICENAME),
("dmSpecVersion", wintypes.WORD),
("dmDriverVersion", wintypes.WORD),
("dmSize", wintypes.WORD),
("dmDriverExtra", wintypes.WORD),
("dmFields", wintypes.DWORD),
("dmPositionX", wintypes.LONG),
("dmPositionY", wintypes.LONG),
("dmDisplayOrientation", wintypes.DWORD),
("dmDisplayFixedOutput", wintypes.DWORD),
("dmColor", ctypes.c_short),
("dmDuplex", ctypes.c_short),
("dmYResolution", ctypes.c_short),
("dmTTOption", ctypes.c_short),
("dmCollate", ctypes.c_short),
("dmFormName", wintypes.WCHAR * _CCHFORMNAME),
("dmLogPixels", wintypes.WORD),
("dmBitsPerPel", wintypes.DWORD),
("dmPelsWidth", wintypes.DWORD),
("dmPelsHeight", wintypes.DWORD),
("dmDisplayFlags", wintypes.DWORD),
("dmDisplayFrequency", wintypes.DWORD),
]
def _detect_fps():
dm = _DEVMODEW()
dm.dmSize = ctypes.sizeof(_DEVMODEW)
if ctypes.windll.user32.EnumDisplaySettingsW(None, -1, ctypes.byref(dm)):
hz = dm.dmDisplayFrequency
if 24 <= hz <= 500:
return hz
return 60
def _scaled_ease(fps):
return 1.0 - (1.0 - EASE_BASE) ** (60.0 / fps)
_FPS = _detect_fps()
EASE = _scaled_ease(_FPS)
FRAME_MS = max(1, 1000 // _FPS)
class _POINT(ctypes.Structure):
_fields_ = [("x", ctypes.c_long), ("y", ctypes.c_long)]
def _get_mouse_pos():
pt = _POINT()
ctypes.windll.user32.GetCursorPos(ctypes.byref(pt))
return float(pt.x), float(pt.y)
def _hide_system_cursor():
cx = ctypes.windll.user32.GetSystemMetrics(13)
cy = ctypes.windll.user32.GetSystemMetrics(14)
n = ((cx + 7) // 8) * cy
and_m = (ctypes.c_byte * n)(*([0xFF] * n))
xor_m = (ctypes.c_byte * n)(*([0x00] * n))
for cid in _ALL_CURSOR_IDS:
blank = ctypes.windll.user32.CreateCursor(None, 0, 0, cx, cy, and_m, xor_m)
ctypes.windll.user32.SetSystemCursor(blank, cid)
def _show_system_cursor():
ctypes.windll.user32.SystemParametersInfoW(0x0057, 0, None, 0)
def _build_sprite(size):
raw = base64.b64decode(CURSOR_B64)
src = Image.open(io.BytesIO(raw)).convert("RGBA")
src = src.resize((size, size), Image.LANCZOS)
r, g, b, a = src.split()
mask = a.point(lambda v: 255 if v > 127 else 0)
bg = Image.new("RGB", (size, size), (255, 0, 255))
bg.paste(Image.merge("RGB", (r, g, b)), mask=mask)
return ImageTk.PhotoImage(bg)
def _setup_overlay(hwnd):
style = ctypes.windll.user32.GetWindowLongW(hwnd, GWL_EXSTYLE)
ctypes.windll.user32.SetWindowLongW(
hwnd, GWL_EXSTYLE,
style | WS_EX_LAYERED | WS_EX_TRANSPARENT | WS_EX_NOACTIVATE,
)
ctypes.windll.user32.SetLayeredWindowAttributes(hwnd, CHROMA_BGR, 0, LWA_COLORKEY)
ctypes.windll.user32.SetWindowPos(
hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE,
)
def _make_tray_image():
img = Image.new("RGBA", (64, 64), (0, 0, 0, 0))
draw = ImageDraw.Draw(img)
draw.ellipse([4, 4, 60, 60], fill=(129, 140, 248, 255))
return img
class MysterioCursor:
def __init__(self):
self._shrink_after = None
self._was_pressing = False
self._frame = 0
sw = ctypes.windll.user32.GetSystemMetrics(0)
sh = ctypes.windll.user32.GetSystemMetrics(1)
self._cx = float(sw // 2)
self._cy = float(sh // 2)
self._tx, self._ty = self._cx, self._cy
self._root = tk.Tk()
self._root.title(_OVERLAY_TITLE)
self._root.overrideredirect(True)
self._root.configure(bg=CHROMA_TK)
self._root.geometry(f"{SIZE_IDLE}x{SIZE_IDLE}+-300+-300")
self._canvas = tk.Canvas(
self._root, width=SIZE_IDLE, height=SIZE_IDLE,
bg=CHROMA_TK, highlightthickness=0, bd=0,
)
self._canvas.pack()
self._photo_idle = _build_sprite(SIZE_IDLE)
self._photo_click = _build_sprite(SIZE_CLICK)
self._img_item = self._canvas.create_image(0, 0, anchor="nw", image=self._photo_idle)
self._root.update()
self._hwnd = ctypes.windll.user32.FindWindowW(None, _OVERLAY_TITLE)
self._root.title("")
if not self._hwnd:
raw = self._root.winfo_id()
self._hwnd = ctypes.windll.user32.GetAncestor(raw, 2) or raw
_setup_overlay(self._hwnd)
self._start_tray()
_hide_system_cursor()
self._root.after(FRAME_MS, self._tick)
def _start_tray(self):
def on_quit(icon, _item):
icon.stop()
self._root.after(0, self._quit)
menu = pystray.Menu(pystray.MenuItem("Quit Mysterio Cursor", on_quit))
self._tray = pystray.Icon("MysterioCursor", _make_tray_image(), "Mysterio Cursor", menu)
threading.Thread(target=self._tray.run, daemon=True).start()
def _set_size(self, size):
img = self._photo_click if size == SIZE_CLICK else self._photo_idle
self._canvas.configure(width=size, height=size)
self._root.geometry(f"{size}x{size}+{int(self._cx)}+{int(self._cy)}")
self._canvas.itemconfigure(self._img_item, image=img)
def _shrink(self):
self._shrink_after = None
self._set_size(SIZE_IDLE)
def _tick(self):
self._tx, self._ty = _get_mouse_pos()
self._cx += (self._tx - self._cx) * EASE
self._cy += (self._ty - self._cy) * EASE
if abs(self._tx - self._cx) < SNAP:
self._cx = self._tx
if abs(self._ty - self._cy) < SNAP:
self._cy = self._ty
pressing = bool(ctypes.windll.user32.GetAsyncKeyState(0x01) & 0x8000)
if pressing and not self._was_pressing:
if self._shrink_after is not None:
self._root.after_cancel(self._shrink_after)
self._shrink_after = None
self._set_size(SIZE_CLICK)
elif not pressing and self._was_pressing:
if self._shrink_after is not None:
self._root.after_cancel(self._shrink_after)
self._shrink_after = self._root.after(CLICK_SHRINK_MS, self._shrink)
self._was_pressing = pressing
self._frame += 1
if self._frame % _FPS == 0:
ctypes.windll.user32.SetWindowPos(
self._hwnd, HWND_TOPMOST, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE,
)
_hide_system_cursor()
self._root.geometry(f"+{int(self._cx)}+{int(self._cy)}")
self._root.after(FRAME_MS, self._tick)
def _quit(self, _=None):
_show_system_cursor()
if hasattr(self, "_tray"):
self._tray.stop()
self._root.destroy()
def run(self):
try:
self._root.mainloop()
finally:
_show_system_cursor()
if __name__ == "__main__":
app = MysterioCursor()
app.run()