ScreenMagnifier 1.11.0.post8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of ScreenMagnifier might be problematic. Click here for more details.

Files changed (117) hide show
  1. ScreenMagnifier/__init__.py +68 -0
  2. ScreenMagnifier/__main__.py +17 -0
  3. ScreenMagnifier/advancedoverlay.py +593 -0
  4. ScreenMagnifier/advancedpanel.py +521 -0
  5. ScreenMagnifier/analysiscontroller.py +533 -0
  6. ScreenMagnifier/analysisserver.py +404 -0
  7. ScreenMagnifier/colordetector/__init__.py +28 -0
  8. ScreenMagnifier/colordetector/colordetector.py +570 -0
  9. ScreenMagnifier/colordetector/core.py +434 -0
  10. ScreenMagnifier/colordetector/main.py +23 -0
  11. ScreenMagnifier/colordetector/mainwindow.py +182 -0
  12. ScreenMagnifier/colorpanel.py +424 -0
  13. ScreenMagnifier/colorswatches/__init__.py +16 -0
  14. ScreenMagnifier/colorswatches/colorswatches.py +316 -0
  15. ScreenMagnifier/colorswatches/main.py +23 -0
  16. ScreenMagnifier/colorswatches/mainwindow.py +60 -0
  17. ScreenMagnifier/controlpanel.py +234 -0
  18. ScreenMagnifier/detectionpanel.py +790 -0
  19. ScreenMagnifier/facedetector/__init__.py +24 -0
  20. ScreenMagnifier/facedetector/core.py +314 -0
  21. ScreenMagnifier/facedetector/facedetector.py +288 -0
  22. ScreenMagnifier/facedetector/main.py +23 -0
  23. ScreenMagnifier/facedetector/mainwindow.py +122 -0
  24. ScreenMagnifier/facedetector/models/__init__.py +4 -0
  25. ScreenMagnifier/facepointer.py +1121 -0
  26. ScreenMagnifier/features.py +50 -0
  27. ScreenMagnifier/hotkeys/__init__.py +16 -0
  28. ScreenMagnifier/hotkeys/hotkeys.py +379 -0
  29. ScreenMagnifier/hotkeys/main.py +23 -0
  30. ScreenMagnifier/hotkeys/mainwindow.py +56 -0
  31. ScreenMagnifier/imagefilters/__init__.py +24 -0
  32. ScreenMagnifier/imagefilters/core.py +172 -0
  33. ScreenMagnifier/imagefilters/imagefilters.py +220 -0
  34. ScreenMagnifier/imagefilters/main.py +23 -0
  35. ScreenMagnifier/imagefilters/mainwindow.py +120 -0
  36. ScreenMagnifier/magnifierview/__init__.py +16 -0
  37. ScreenMagnifier/magnifierview/magnifierview.py +1249 -0
  38. ScreenMagnifier/magnifierview/main.py +23 -0
  39. ScreenMagnifier/magnifierview/mainwindow.py +91 -0
  40. ScreenMagnifier/main.py +157 -0
  41. ScreenMagnifier/mainwindow.py +1368 -0
  42. ScreenMagnifier/mousecatcher/__init__.py +16 -0
  43. ScreenMagnifier/mousecatcher/main.py +23 -0
  44. ScreenMagnifier/mousecatcher/mainwindow.py +78 -0
  45. ScreenMagnifier/mousecatcher/mousecatcher.py +251 -0
  46. ScreenMagnifier/mousemove.py +259 -0
  47. ScreenMagnifier/netprotocol/__init__.py +25 -0
  48. ScreenMagnifier/netprotocol/main.py +23 -0
  49. ScreenMagnifier/netprotocol/mainwindow.py +91 -0
  50. ScreenMagnifier/netprotocol/netprotocol.py +372 -0
  51. ScreenMagnifier/netprotocol/wire.py +189 -0
  52. ScreenMagnifier/regionselector/__init__.py +16 -0
  53. ScreenMagnifier/regionselector/main.py +23 -0
  54. ScreenMagnifier/regionselector/mainwindow.py +70 -0
  55. ScreenMagnifier/regionselector/regionselector.py +549 -0
  56. ScreenMagnifier/remoteclient.py +636 -0
  57. ScreenMagnifier/resources/icon-128.png +0 -0
  58. ScreenMagnifier/resources/icon-16.png +0 -0
  59. ScreenMagnifier/resources/icon-24.png +0 -0
  60. ScreenMagnifier/resources/icon-256.png +0 -0
  61. ScreenMagnifier/resources/icon-32.png +0 -0
  62. ScreenMagnifier/resources/icon-48.png +0 -0
  63. ScreenMagnifier/resources/icon-512.png +0 -0
  64. ScreenMagnifier/resources/icon-64.png +0 -0
  65. ScreenMagnifier/resources/icon.icns +0 -0
  66. ScreenMagnifier/resources/icon.ico +0 -0
  67. ScreenMagnifier/screengrabber/__init__.py +16 -0
  68. ScreenMagnifier/screengrabber/main.py +23 -0
  69. ScreenMagnifier/screengrabber/mainwindow.py +79 -0
  70. ScreenMagnifier/screengrabber/screengrabber.py +558 -0
  71. ScreenMagnifier/server.py +150 -0
  72. ScreenMagnifier/serverpanel.py +379 -0
  73. ScreenMagnifier/shortcuteditor/__init__.py +16 -0
  74. ScreenMagnifier/shortcuteditor/main.py +23 -0
  75. ScreenMagnifier/shortcuteditor/mainwindow.py +54 -0
  76. ScreenMagnifier/shortcuteditor/shortcuteditor.py +411 -0
  77. ScreenMagnifier/shortcuts.py +118 -0
  78. ScreenMagnifier/shortcutspanel.py +140 -0
  79. ScreenMagnifier/support.py +127 -0
  80. ScreenMagnifier/translations/ar.json +387 -0
  81. ScreenMagnifier/translations/de.json +387 -0
  82. ScreenMagnifier/translations/es.json +387 -0
  83. ScreenMagnifier/translations/fr.json +387 -0
  84. ScreenMagnifier/translations/it.json +387 -0
  85. ScreenMagnifier/translations/ja.json +387 -0
  86. ScreenMagnifier/translations/ko.json +387 -0
  87. ScreenMagnifier/translations/pt_BR.json +387 -0
  88. ScreenMagnifier/translations/ru.json +387 -0
  89. ScreenMagnifier/translations/tr.json +387 -0
  90. ScreenMagnifier/translations/zh_CN.json +387 -0
  91. ScreenMagnifier/translator/__init__.py +16 -0
  92. ScreenMagnifier/translator/main.py +23 -0
  93. ScreenMagnifier/translator/mainwindow.py +62 -0
  94. ScreenMagnifier/translator/translator.py +284 -0
  95. ScreenMagnifier/upscaler/__init__.py +24 -0
  96. ScreenMagnifier/upscaler/core.py +315 -0
  97. ScreenMagnifier/upscaler/main.py +23 -0
  98. ScreenMagnifier/upscaler/mainwindow.py +128 -0
  99. ScreenMagnifier/upscaler/models/FSRCNN-small_x2.pb +0 -0
  100. ScreenMagnifier/upscaler/models/FSRCNN-small_x3.pb +0 -0
  101. ScreenMagnifier/upscaler/models/FSRCNN-small_x4.pb +0 -0
  102. ScreenMagnifier/upscaler/models/FSRCNN_x2.pb +0 -0
  103. ScreenMagnifier/upscaler/models/FSRCNN_x3.pb +0 -0
  104. ScreenMagnifier/upscaler/models/FSRCNN_x4.pb +0 -0
  105. ScreenMagnifier/upscaler/models/LICENSE-FSRCNN +201 -0
  106. ScreenMagnifier/upscaler/models/__init__.py +4 -0
  107. ScreenMagnifier/upscaler/upscaler.py +285 -0
  108. ScreenMagnifier/wincheck.py +65 -0
  109. ScreenMagnifier/zoomoverlay/__init__.py +16 -0
  110. ScreenMagnifier/zoomoverlay/main.py +23 -0
  111. ScreenMagnifier/zoomoverlay/mainwindow.py +99 -0
  112. ScreenMagnifier/zoomoverlay/zoomoverlay.py +1779 -0
  113. screenmagnifier-1.11.0.post8.dist-info/METADATA +591 -0
  114. screenmagnifier-1.11.0.post8.dist-info/RECORD +117 -0
  115. screenmagnifier-1.11.0.post8.dist-info/WHEEL +5 -0
  116. screenmagnifier-1.11.0.post8.dist-info/entry_points.txt +5 -0
  117. screenmagnifier-1.11.0.post8.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1121 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ FacePointer: the mouse pointer - or a game's view - goes to the faces on the screen (films, offline games).
4
+ Films and games that show the pointer (MOTION_POINTER): a few times per second the screen of the pointer is scanned
5
+ for faces (on a background thread); the pointer glides to the largest face, or the one nearest it, and stays with a
6
+ face while it moves. Moving the mouse yourself pauses it. FOLLOW_ZOOMER moves only the zoomer's target.
7
+ Games that capture the mouse (MOTION_VIEW; first-person, 3D): such a game hides the pointer, turns its view with the
8
+ raw mouse movements and puts the pointer back in the middle, so moving the pointer does nothing there. In real time
9
+ (~30 times per second) the area around the crosshair (the screen center) is captured at full resolution; mouse
10
+ movements are sent, like a real mouse, until the face is at the crosshair.
11
+ - How far the view turns per mouse count (the game's sensitivity, per axis, inverted axes included) is measured
12
+ from the picture itself: the scene shift between two captures (phase correlation) divided by the movement
13
+ sent between them. Until it is known, only small test movements are sent.
14
+ - A face that stays still while the scene turns is a portrait of the game's interface: ignored for a while.
15
+ - Movements sent while the scene never turns: the game does not receive them (e.g. it runs as administrator).
16
+ - HOLD_RIGHT_BUTTON assists only while the right mouse button (aim) is held.
17
+ MOTION_AUTO moves the pointer, and centres the face on the screen (turns the view) while a game captures the mouse:
18
+ the system says the pointer is confined (Windows) or hidden - and it stays hidden while it is moved (a video player
19
+ shows it again) - or the game puts it back on the same spot after each move. When the pointer comes back (a menu),
20
+ the pointer is moved again.
21
+ Capture: on Windows the optional dxcam package (DXGI desktop duplication) is used when installed, else the screen
22
+ grab, else the game window itself (PrintWindow); a black capture is reported (exclusive full-screen games).
23
+ """
24
+ from ManyQt.QtCore import QObject, QTimer, QPoint, QRect, QElapsedTimer, Qt, pyqtSignal, pyqtSlot, pyqtProperty
25
+ from ManyQt.QtGui import QCursor
26
+ from threading import Thread, Lock
27
+ from sys import path, platform
28
+ from math import exp, hypot
29
+ from os.path import dirname
30
+
31
+ if not __package__ and dirname(__file__) not in path:
32
+ path.append(dirname(__file__))
33
+
34
+ try:
35
+ from .screengrabber.screengrabber import ScreenGrabber, screenAtPoint, winWindowAt, winGrabWindowRect, isBlankImage
36
+ from .mousemove import sendRelativeMove, canSendRelative, ButtonWatcher, mouseCapture
37
+ from .facedetector.facedetector import qimageToArray, appDataDir
38
+ from .facedetector.core import FaceEngine
39
+ except:
40
+ from screengrabber.screengrabber import ScreenGrabber, screenAtPoint, winWindowAt, winGrabWindowRect, isBlankImage
41
+ from mousemove import sendRelativeMove, canSendRelative, ButtonWatcher, mouseCapture
42
+ from facedetector.facedetector import qimageToArray, appDataDir
43
+ from facedetector.core import FaceEngine
44
+ try:
45
+ from cv2 import createHanningWindow, CV_32F, resize, INTER_AREA, cvtColor, COLOR_RGB2GRAY
46
+ from numpy import float32, fft, conj, real, abs, unravel_index, argmax, clip, mgrid
47
+
48
+ cv2exists = True # type: bool
49
+ except:
50
+ cv2exists = False # type: bool
51
+
52
+ FOLLOW_POINTER, FOLLOW_ZOOMER = 0, 1 # type: int, int
53
+ CHOOSE_LARGEST, CHOOSE_NEAREST = 0, 1 # type: int, int
54
+ MOTION_POINTER, MOTION_VIEW, MOTION_AUTO = 0, 1, 2 # type: int, int, int
55
+ HOLD_ALWAYS, HOLD_RIGHT_BUTTON = 0, 1 # type: int, int
56
+ SCAN_WIDTH = 960 # type: int # Whole screens are scanned at most this wide: faces from ~48 px on a 1080p screen.
57
+ VIEW_FRAME_MS = 33 # type: int # Games: the area around the crosshair, ~30 times per second.
58
+ LOST_MS = 1200 # type: int # A followed face may vanish this long (cut, profile view) before another is chosen.
59
+ LOST_VIEW_MS = 500 # type: int # (in a game).
60
+ USER_MOVE_PX = 3 # type: int # The pointer moved more than this by itself: the user took the mouse.
61
+ MAX_STEP = 120 # type: int # Mouse counts sent at most per move (~60 per second): never a violent spin.
62
+ PROBE = 40 # type: int # First test movement per axis until the game's sensitivity is known (doubled while the.
63
+ PROBE_MAX = 640 # type: int # View does not visibly turn: very low sensitivities are found too).
64
+ CORRELATION = 256 # type: int # Side of the small gray captures compared for the scene's motion.
65
+ RETRY_MS = 1500 # type: int # The game did not react: test again this often (never a drift).
66
+ LEAD_S = 0.06 # type: float # Moving enemies: lead by the time since the capture + this (the movement's landing).
67
+ ACQUIRE_SIDE = 640 # type: int # The whole assist area is searched at most this wide (speed).
68
+ FULL_EVERY = 8 # type: int # While following, every 8th scan searches the whole assist area again.
69
+ STATIC_MS = 6000 # type: int # A face that does not move with the view is ignored this long.
70
+ NO_REACTION_SCANS = 8 # type: int # Movements sent, scene never turned: the game does not receive them.
71
+ BLANK_SCANS = 8 # type: int # Black captures in a row: the screen cannot be captured.
72
+ _DXCAM = [] # type: list # [camera | None] (Windows, optional)
73
+
74
+
75
+ def clamp(x, a, b):
76
+ """
77
+ :param x: float | int
78
+ :param a: float | int
79
+ :param b: float | int
80
+ :return: float | int
81
+ """
82
+ return a if x < a else b if x > b else x
83
+
84
+
85
+ def sceneShift(previous, current, sent, k, known):
86
+ """
87
+ How far the scene moved between two small gray captures - robust to what does not move with a game's view (the
88
+ weapon, the interface): the phase-correlation surface is searched for several peaks; a peak at zero shift is the
89
+ static layer, a clear non-zero peak is the turning world; with a known sensitivity, the expected shift only
90
+ breaks near-ties between strong peaks (repeating textures), never overriding a clearly stronger peak.
91
+ :param previous: (ndarray) N x N float32.
92
+ :param current: (ndarray) N x N float32.
93
+ :param sent: (tuple[int, int]) Mouse counts sent between the two captures.
94
+ :param k: (tuple[float, float]) Screen px per count (in these images' pixels).
95
+ :param known: (tuple[bool, bool]) The sensitivity is measured on that axis.
96
+ :return: (tuple[float, float] | None) Shift of the scene in these images' pixels, None if unclear.
97
+ """
98
+ n = previous.shape[0] # type: int
99
+ window = createHanningWindow((n, n), CV_32F)
100
+ a, b = fft.fft2(previous * window), fft.fft2(current * window)
101
+ cross = b * conj(a)
102
+ surface = fft.fftshift(real(fft.ifft2(cross / (abs(cross) + 1e-9))))
103
+ peaks = [] # type: list # (value, dx, dy)
104
+ work = surface.copy()
105
+ for _ in range(6):
106
+ y, x = unravel_index(int(argmax(work)), work.shape)
107
+ value = float(work[y, x]) # type: float
108
+ if value <= 0:
109
+ break
110
+ y0, y1, x0, x1 = max(0, y - 1), min(n, y + 2), max(0, x - 1), min(n, x + 2)
111
+ patch = clip(surface[y0:y1, x0:x1], 0, None)
112
+ total = float(patch.sum()) or 1.0 # type: float # sub-pixel: centroid of the 3 x 3 peak
113
+ ys, xs = mgrid[y0:y1, x0:x1]
114
+ peaks.append((value, float((patch * xs).sum() / total) - n // 2, float((patch * ys).sum() / total) - n // 2))
115
+ work[max(0, y - 3):y + 4, max(0, x - 3):x + 4] = -1.0
116
+ if not peaks:
117
+ return None
118
+ best = peaks[0][0] # type: float
119
+ if max(abs(sent[0]), abs(sent[1])) < 8:
120
+ return peaks[0][1], peaks[0][2]
121
+ moving = [p for p in peaks if hypot(p[1], p[2]) > 0.75 and p[0] >= 0.25 * best] # type: list
122
+ if not moving:
123
+ return 0.0, 0.0 # only the static layer: the scene did not turn
124
+ strongest = max(p[0] for p in moving) # type: float
125
+ if strongest < 0.03:
126
+ return None # no clear match (a cut, an explosion...): unclear, not "no motion"
127
+ strong = [p for p in moving if p[0] >= 0.6 * strongest] # type: list # a clearly stronger peak always wins:
128
+ if len(strong) > 1 and (known[0] or known[1]): # the expectation only breaks near-ties (it must never
129
+ expected = (-sent[0] * k[0], -sent[1] * k[1]) # type: tuple # confirm a wrong estimate)
130
+ return min(((p[1], p[2]) for p in strong), key=lambda q: hypot(q[0] - expected[0], q[1] - expected[1]))
131
+ top = max(strong, key=lambda p: p[0])
132
+ return top[1], top[2]
133
+
134
+
135
+ def _dxcam():
136
+ """
137
+ :return: dxcam camera of the primary output | None (not Windows, not installed, or failed once).
138
+ """
139
+ if not _DXCAM:
140
+ camera = None
141
+ if platform == 'win32':
142
+ try:
143
+ from dxcam import create
144
+ camera = create(output_color='RGB')
145
+ except:
146
+ camera = None
147
+ _DXCAM.append(camera)
148
+ return _DXCAM[0]
149
+
150
+
151
+ class FacePointer(QObject):
152
+ """
153
+ FacePointer class.
154
+ """
155
+ statusChanged = pyqtSignal('QString') # type: pyqtSignal # off, searching, following, paused, waiting,
156
+ enabledChanged = pyqtSignal(bool) # type: pyqtSignal # static, blocked, blank, noReaction, unavailable,
157
+ _found = pyqtSignal(object) # type: pyqtSignal # error (_found: worker thread -> GUI thread)
158
+
159
+ def __init__(self, *args, **kwargs):
160
+ """
161
+ :param follow: (int) FOLLOW_POINTER (move the mouse pointer) or FOLLOW_ZOOMER (only the zoomer's target).
162
+ :param choose: (int) CHOOSE_LARGEST or CHOOSE_NEAREST (nearest the pointer / the crosshair).
163
+ :param motion: (int) MOTION_AUTO, MOTION_POINTER or MOTION_VIEW.
164
+ :param hold: (int) HOLD_ALWAYS or HOLD_RIGHT_BUTTON (games: assist only while the right button is held).
165
+ :param smoothness: (int) 0 .. 100: 0 jumps to the face, 100 glides slowly.
166
+ :param pauseSeconds: (float | int) Control given back after you move the mouse yourself (films).
167
+ :param scanRate: (int) Whole-screen scans per second (films); games run at ~30.
168
+ :param assistArea: (int) 10 .. 100: radius around the crosshair, in % of half the screen height (games).
169
+ :param strength: (int) 10 .. 100: share of the distance corrected per scan (games).
170
+ :param detector: (FaceDetector | None) Its backend and confidence are used (the Detection tab settings).
171
+ :param ignoreRects: (callable | None) f() -> list of global QRect never scanned (the lens, this window).
172
+ :param moveZoomer: (callable | None) f(QPoint) puts the zoomer's target there (FOLLOW_ZOOMER).
173
+ :param statusChanged: (callable | None)
174
+ :param args: any
175
+ :param kwargs: any
176
+ """
177
+ follow = kwargs.pop('follow', FOLLOW_POINTER) # type: int
178
+ choose = kwargs.pop('choose', CHOOSE_LARGEST) # type: int
179
+ motion = kwargs.pop('motion', MOTION_AUTO) # type: int
180
+ hold = kwargs.pop('hold', HOLD_ALWAYS) # type: int
181
+ smoothness = kwargs.pop('smoothness', 40) # type: int
182
+ pauseSeconds = kwargs.pop('pauseSeconds', 3.0) # type: float
183
+ scanRate = kwargs.pop('scanRate', 5) # type: int
184
+ assistArea = kwargs.pop('assistArea', 35) # type: int
185
+ strength = kwargs.pop('strength', 60) # type: int
186
+ detector = kwargs.pop('detector', None)
187
+ ignoreRects = kwargs.pop('ignoreRects', None) # type: callable | None
188
+ moveZoomer = kwargs.pop('moveZoomer', None) # type: callable | None
189
+ statusChanged = kwargs.pop('statusChanged', None) # type: callable | None
190
+ super(FacePointer, self).__init__(*args, **kwargs)
191
+ if callable(statusChanged):
192
+ self.statusChanged.connect(statusChanged)
193
+ self.__m_follow = FOLLOW_ZOOMER if int(follow) == FOLLOW_ZOOMER else FOLLOW_POINTER # type: int
194
+ self.__m_choose = CHOOSE_NEAREST if int(choose) == CHOOSE_NEAREST else CHOOSE_LARGEST # type: int
195
+ self.__m_motion = int(motion) if int(motion) in (MOTION_POINTER, MOTION_VIEW, MOTION_AUTO) else \
196
+ MOTION_AUTO # type: int
197
+ self.__m_hold = HOLD_RIGHT_BUTTON if int(hold) == HOLD_RIGHT_BUTTON else HOLD_ALWAYS # type: int
198
+ self.__m_smoothness = int(clamp(int(smoothness), 0, 100)) # type: int
199
+ self.__m_pauseSeconds = float(clamp(float(pauseSeconds), 0.5, 30.0)) # type: float
200
+ self.__m_scanRate = int(clamp(int(scanRate), 1, 15)) # type: int
201
+ self.__m_assistArea = int(clamp(int(assistArea), 10, 100)) # type: int
202
+ self.__m_strength = int(clamp(int(strength), 10, 100)) # type: int
203
+ self.__m_detector = detector
204
+ self.__m_ignoreRects = ignoreRects if callable(ignoreRects) else None # type: callable | None
205
+ self.__m_moveZoomer = moveZoomer if callable(moveZoomer) else None # type: callable | None
206
+ # State.
207
+ self.__m_engine = FaceEngine(
208
+ dataDir=appDataDir(), maxInputSize=SCAN_WIDTH, haarMaxInput=SCAN_WIDTH) # type: FaceEngine
209
+ self.__m_enabled = False # type: bool
210
+ self.__m_busy = False # type: bool # a worker thread is detecting (one at a time: OpenCV objects are
211
+ self.__m_lock = Lock() # type: Lock # not thread-safe)
212
+ self.__m_generation = 0 # type: int # results of an earlier on / off period are dropped
213
+ self.__m_status = 'off' # type: str
214
+ self.__m_detail = {} # type: dict
215
+ self.__m_target = None # type: QPoint | None # followed face center (global, device independent)
216
+ self.__m_targetSize = 0.0 # type: float
217
+ self.__m_lastSeen = 0 # type: int
218
+ self.__m_lastSet = None # type: QPoint | None # where we last put the pointer
219
+ self.__m_zoomPos = None # type: tuple | None # smoothed zoomer target (float x, y)
220
+ self.__m_pausedUntil = 0 # type: int
221
+ self.__m_blocked = 0 # type: int # consecutive pointer moves the system refused
222
+ self.__m_blank = 0 # type: int # consecutive black captures
223
+ self.__m_lastTick = 0 # type: int
224
+ self.__m_captured = False # type: bool # MOTION_AUTO: this game captures the mouse
225
+ self.__m_snap = None # type: tuple | None # (QPoint, ms, count): where the pointer was put back
226
+ self.__m_captureBy = '' # type: str # 'signal' (the system said so) or 'snap' (put back on one spot)
227
+ self.__m_hiddenSince = None # type: int | None
228
+ self.__m_visibleSince = None # type: int | None
229
+ self.__m_lastCheck = 0 # type: int
230
+ self.__m_lastMoveMs = -10000 # type: int # last time the pointer was moved by us
231
+ # Game (view) state.
232
+ self.__m_watcher = ButtonWatcher() # type: ButtonWatcher
233
+ self.__m_k = [1.0, 1.0] # type: list[float] # screen px per mouse count, per axis (measured)
234
+ self.__m_kKnown = [False, False] # type: list[bool]
235
+ self.__m_probe = [PROBE, PROBE] # type: list[int]
236
+ self.__m_nextProbe = 0 # type: int
237
+ self.__m_history = [] # type: list
238
+ self.__m_samples = [[], []] # type: list
239
+ self.__m_lastOffset = None # type: tuple | None
240
+ self.__m_velocity, self.__m_tracked, self.__m_prevGrabMs = (0.0, 0.0), 0, None
241
+ self.__m_lead = True # type: bool
242
+ self.__m_scans = 0 # type: int
243
+ self.__m_sent = [0, 0] # type: list[int] # mouse counts sent in total
244
+ self.__m_pending = [0.0, 0.0] # type: list[float] # counts still to send
245
+ self.__m_lastGrabSent = None # type: tuple | None # counts sent at the previous capture
246
+ self.__m_previousFace = None # type: tuple | None # (x, y) of the followed face at the previous capture
247
+ self.__m_prevGray = None # worker thread only: the previous capture, small and gray
248
+ self.__m_staticHits = 0 # type: int
249
+ self.__m_noReaction = 0 # type: int
250
+ self.__m_ignored = [] # type: list # [(QPoint, until ms)] faces that do not move with the view
251
+ self.__m_acquired = None # type: tuple | None # (distance px, counts sent) when the face was chosen
252
+ self.__m_clock = QElapsedTimer() # type: QElapsedTimer
253
+ self.__m_clock.start()
254
+ self.__m_scanTimer = QTimer(self) # type: QTimer
255
+ self.__m_scanTimer.timeout.connect(self._scan)
256
+ self.__m_moveTimer = QTimer(self) # type: QTimer
257
+ self.__m_moveTimer.setInterval(16) # ~60 moves per second: smooth
258
+ self.__m_moveTimer.timeout.connect(self._move)
259
+ self._found.connect(self._onFound, Qt.QueuedConnection)
260
+ if detector is not None and hasattr(detector, 'downloadFinished'):
261
+ detector.downloadFinished.connect(lambda *a: self.__m_engine.resetModel())
262
+
263
+ # ---------- Helpers ----------
264
+ @staticmethod
265
+ def isAvailable():
266
+ """
267
+ :return: (bool) Face detection is possible (NumPy + OpenCV).
268
+ """
269
+ return FaceEngine.isAvailable()
270
+
271
+ def _now(self):
272
+ """
273
+ :return: (int) ms
274
+ """
275
+ return int(self.__m_clock.elapsed())
276
+
277
+ def _setStatus(self, status, **detail):
278
+ """
279
+ :param status: str | unicode | QString
280
+ :param detail: (dict) faces, backend, timeMs, seconds, message...
281
+ :return:
282
+ """
283
+ self.__m_detail = dict(detail) # type: dict
284
+ if status != self.__m_status or detail:
285
+ self.__m_status = status # type: str
286
+ self.statusChanged.emit(status)
287
+
288
+ def _isView(self):
289
+ """
290
+ :return: (bool) The game's view is turned (MOTION_VIEW, or MOTION_AUTO in a game that captures the mouse).
291
+ """
292
+ return self.__m_motion == MOTION_VIEW or (self.__m_motion == MOTION_AUTO and self.__m_captured)
293
+
294
+ def _resetView(self):
295
+ """
296
+ Forget what was learned about a game (another game, or the face pointer switched on again).
297
+ :return:
298
+ """
299
+ self.__m_k, self.__m_kKnown, self.__m_probe = [1.0, 1.0], [False, False], [PROBE, PROBE]
300
+ self.__m_history = [] # type: list # [(ms, counts sent, total scene shift)] for _learn()
301
+ self.__m_samples = [[], []] # type: list # recent sensitivity readings per axis (their median is used)
302
+ self.__m_lastOffset = None # type: tuple | None # the followed face's offset at the previous capture
303
+ self.__m_velocity = (0.0, 0.0) # type: tuple # the followed enemy's own motion (px / s, on screen)
304
+ self.__m_tracked = 0 # type: int # velocity readings of the followed enemy
305
+ self.__m_prevGrabMs = None # type: int | None
306
+ self.__m_lead = True # type: bool # lead moving enemies
307
+ self.__m_pending, self.__m_lastGrabSent, self.__m_previousFace = [0.0, 0.0], None, None
308
+ self.__m_staticHits, self.__m_noReaction, self.__m_acquired = 0, 0, None
309
+ with self.__m_lock:
310
+ self.__m_prevGray = None
311
+
312
+ def _restartScans(self):
313
+ """
314
+ :return:
315
+ """
316
+ if self.__m_enabled:
317
+ self.__m_scanTimer.start(VIEW_FRAME_MS if self._isView() else int(1000 / self.__m_scanRate))
318
+
319
+ def _grab(self, area, width):
320
+ """
321
+ Capture a global rect (the black-out of the ignored windows included), with the fallbacks of Windows.
322
+ :param area: QRect
323
+ :param width: (int) At most this wide.
324
+ :return: (tuple[ndarray, float, float] | None) RGB array and its scale (px per device-independent px).
325
+ """
326
+ arr, sx, sy = None, 1.0, 1.0
327
+ camera = _dxcam() if platform == 'win32' else None
328
+ screen = screenAtPoint(area.center())
329
+ if camera is not None and screen is not None and screen.geometry().topLeft() == QPoint(0, 0):
330
+ try: # DXGI desktop duplication: also games that GDI sees black (primary screen only)
331
+ dpr = float(screen.devicePixelRatio()) # type: float
332
+ frame = camera.grab(region=(int(area.left() * dpr), int(area.top() * dpr),
333
+ int((area.right() + 1) * dpr), int((area.bottom() + 1) * dpr)))
334
+ if frame is not None:
335
+ arr, sx, sy = frame, dpr, dpr
336
+ except Exception:
337
+ _DXCAM[0] = None # never again
338
+ if arr is None:
339
+ image, dpr = ScreenGrabber.grabRect(area)
340
+ if platform == 'win32' and isBlankImage(image): # exclusive full-screen, hardware overlays...
341
+ hwnd = winWindowAt(area.center())
342
+ if hwnd:
343
+ image, dpr = winGrabWindowRect(hwnd, area)
344
+ if isBlankImage(image):
345
+ self.__m_blank += 1
346
+ if self.__m_blank >= BLANK_SCANS and self.__m_status != 'blank':
347
+ self._setStatus('blank')
348
+ return None
349
+ if image.width() > width:
350
+ image = image.scaledToWidth(width, Qt.SmoothTransformation)
351
+ arr = qimageToArray(image)
352
+ sx, sy = image.width() / float(area.width()), image.height() / float(area.height())
353
+ elif arr.shape[1] > width and cv2exists:
354
+ f = width / float(arr.shape[1]) # type: float
355
+ arr = resize(arr, (width, max(1, int(arr.shape[0] * f))), interpolation=INTER_AREA)
356
+ sx, sy = sx * f, sy * f
357
+ self.__m_blank = 0 # type: int
358
+ if self.__m_status == 'blank':
359
+ self._setStatus('searching')
360
+ if self.__m_ignoreRects is not None: # this application's windows: a magnified face is not a face
361
+ arr = arr.copy()
362
+ for r in self.__m_ignoreRects() or []:
363
+ if r is not None and r.isValid() and r.intersects(area):
364
+ x0, y0 = max(0, int((r.x() - area.x()) * sx)), max(0, int((r.y() - area.y()) * sy))
365
+ arr[y0:int((r.bottom() + 1 - area.y()) * sy) + 1, x0:int((r.right() + 1 - area.x()) * sx) + 1] = 0
366
+ return arr, sx, sy
367
+
368
+ @pyqtSlot()
369
+ def _scan(self):
370
+ """
371
+ GUI thread: capture (games: around the crosshair, full resolution; films: the whole screen), then detect on a
372
+ worker thread.
373
+ :return:
374
+ """
375
+ if not self.__m_enabled or self.__m_busy:
376
+ return
377
+ view = self._isView() # type: bool
378
+ screen = screenAtPoint(self.__m_target if self.__m_target is not None and not view else QCursor.pos())
379
+ g = screen.geometry() # type: QRect
380
+ if view:
381
+ radius = int(self.__m_assistArea / 100.0 * g.height() / 2.0 * 1.25) # type: int # (a margin)
382
+ area = QRect(g.center().x() - radius, g.center().y() - radius, 2 * radius, 2 * radius).intersected(g)
383
+ else:
384
+ area = QRect(g) # type: QRect
385
+ grabbed = self._grab(area, SCAN_WIDTH)
386
+ if grabbed is None:
387
+ return
388
+ arr, sx, sy = grabbed
389
+ if self.__m_detector is not None: # the Detection tab settings (no worker is running: not busy)
390
+ self.__m_engine.backend = u'{}'.format(self.__m_detector.getBackend()) # type str
391
+ self.__m_engine.scoreThreshold = float(self.__m_detector.getScoreThreshold()) # type float
392
+ self.__m_busy = True # type: bool
393
+ crop = None # games: once a face is followed, detect only around it, at full resolution (fast)
394
+ self.__m_scans += 1
395
+ if view and self.__m_target is not None and self.__m_scans % FULL_EVERY and \
396
+ self._now() - self.__m_lastSeen < LOST_VIEW_MS:
397
+ half = max(100.0, 1.75 * self.__m_targetSize) # type: float
398
+ t = self.__m_target # type: QPoint
399
+ x0, y0 = int(max(0, (t.x() - half - area.x()) * sx)), int(max(0, (t.y() - half - area.y()) * sy))
400
+ x1 = int(min(arr.shape[1], (t.x() + half - area.x()) * sx))
401
+ y1 = int(min(arr.shape[0], (t.y() + half - area.y()) * sy))
402
+ if x1 - x0 >= 48 and y1 - y0 >= 48:
403
+ crop = (x0, y0, x1, y1)
404
+ job = {'arr': arr, 'area': QRect(area), 'sx': sx, 'sy': sy, 'aim': QPoint(g.center()), 'view': view,
405
+ 'grabMs': self._now(),
406
+ 'crop': crop,
407
+ 'k': tuple(self.__m_k), 'known': tuple(self.__m_kKnown),
408
+ 'radius': self.__m_assistArea / 100.0 * g.height() / 2.0,
409
+ 'generation': self.__m_generation, 'sentAtGrab': tuple(self.__m_sent)} # type: dict
410
+ worker = Thread(target=self._detect, args=(job,), name='FacePointer')
411
+ worker.daemon = True
412
+ worker.start()
413
+
414
+ def _detect(self, job):
415
+ """
416
+ Worker thread: faces, and (games) how far the scene moved since the previous capture.
417
+ :param job: dict
418
+ :return:
419
+ """
420
+ with self.__m_lock:
421
+ try:
422
+ arr, engine = job['arr'], self.__m_engine
423
+ crop = job.get('crop')
424
+ if crop is not None: # around the followed face, full resolution
425
+ engine.maxInputSize = engine.haarMaxInput = max(160, crop[2] - crop[0], crop[3] - crop[1])
426
+ result = engine.detect(arr[crop[1]:crop[3], crop[0]:crop[2]])
427
+ for face in result.get('faces', []):
428
+ face['rect'] = [face['rect'][0] + crop[0], face['rect'][1] + crop[1]] + list(face['rect'][2:])
429
+ face['landmarks'] = [[px + crop[0], py + crop[1]] for px, py in face.get('landmarks', [])]
430
+ else: # games: the whole assist area, at most ACQUIRE_SIDE wide; films: the screen
431
+ engine.maxInputSize = engine.haarMaxInput = ACQUIRE_SIDE if job['view'] else SCAN_WIDTH
432
+ result = engine.detect(arr)
433
+ job['result'] = result
434
+ except Exception as e:
435
+ job['result'] = {'faces': [], 'error': u'{}'.format(e)}
436
+ if not job['view']:
437
+ job.pop('arr', None)
438
+ job['shift'] = None
439
+ if job['view'] and cv2 is not None:
440
+ try:
441
+ arr = job.pop('arr')
442
+ small = resize(cvtColor(arr, COLOR_RGB2GRAY), (CORRELATION, CORRELATION),
443
+ interpolation=INTER_AREA).astype(float32)
444
+ fill = float(small.mean()) # type: float # people are blanked: the world alone is measured
445
+ c = float(CORRELATION) # type: float # (a walking enemy's body would pull the measurement)
446
+ for face in job['result'].get('faces', []): # head + body: half a head above, ~4 heads below
447
+ x, y, w, h = face['rect'][:4]
448
+ x0, x1 = int((x - 1.0 * w) * c / arr.shape[1]), int((x + 2.0 * w) * c / arr.shape[1])
449
+ y0, y1 = int((y - 0.5 * h) * c / arr.shape[0]), int((y + 5.0 * h) * c / arr.shape[0])
450
+ small[max(0, y0):max(0, y1), max(0, x0):max(0, x1)] = fill
451
+ previous, self.__m_prevGray = self.__m_prevGray, (small, job['area'].size(), job['sentAtGrab'])
452
+ if previous is not None and previous[1] == job['area'].size():
453
+ fx = job['area'].width() / float(CORRELATION) # type: float # small px -> screen px
454
+ fy = job['area'].height() / float(CORRELATION) # type: float
455
+ sent = (job['sentAtGrab'][0] - previous[2][0], job['sentAtGrab'][1] - previous[2][1])
456
+ shift = sceneShift(previous[0], small, sent, (job['k'][0] / fx, job['k'][1] / fy),
457
+ job['known'])
458
+ if shift is not None:
459
+ job['shift'], job['response'] = (shift[0] * fx, shift[1] * fy), 1.0
460
+ except Exception:
461
+ job['shift'] = None
462
+ job.pop('arr', None)
463
+ self._found.emit(job)
464
+
465
+ @pyqtSlot(object)
466
+ def _onFound(self, job):
467
+ """
468
+ GUI thread: choose the face, plan the movement.
469
+ :param job: dict
470
+ :return: None
471
+ """
472
+ self.__m_busy = False # type: bool # (only here: the worker has finished)
473
+ if not self.__m_enabled or job['generation'] != self.__m_generation:
474
+ return # Switched off, or a result from before the last on / off.
475
+ result, area, sx, sy = job['result'], job['area'], job['sx'], job['sy']
476
+ faces = [] # type: list[tuple] # Global center x, y, size, and the head's aim point x, y (between the eyes).
477
+ for face in result.get('faces', []):
478
+ x, y, w, h = face['rect'][:4]
479
+ eyes = face.get('landmarks') or [] # type: list # YuNet: right eye, left eye, nose, mouth corners
480
+ ex, ey = ((eyes[0][0] + eyes[1][0]) / 2.0, (eyes[0][1] + eyes[1][1]) / 2.0) if len(eyes) >= 2 else \
481
+ (x + w / 2.0, y + 0.40 * h) # (Haar: eye level of the box)
482
+ faces.append((area.x() + (x + w / 2.0) / sx, area.y() + (y + h / 2.0) / sy, max(w / sx, h / sy),
483
+ area.x() + ex / sx, area.y() + ey / sy))
484
+ detail = {'faces': len(faces), 'backend': result.get('backend', ''),
485
+ 'timeMs': float(result.get('timeMs', 0.0)), 'auto': self.__m_captured} # type: dict
486
+ if result.get('error'):
487
+ self._setStatus('error', message=result['error'])
488
+ elif job['view']:
489
+ self._onFoundView(job, faces, detail)
490
+ else:
491
+ self._onFoundPointer(faces, detail)
492
+
493
+ def _onFoundPointer(self, faces, detail):
494
+ """
495
+ Films: keep the followed face while it moves, else choose one.
496
+ :param faces: list[tuple[float, float, float]]
497
+ :param detail: dict
498
+ :return:
499
+ """
500
+ now = self._now() # type: int
501
+ chosen = None
502
+ if self.__m_target is not None and faces:
503
+ tx, ty = self.__m_target.x(), self.__m_target.y()
504
+ near = min(faces, key=lambda f: hypot(f[0] - tx, f[1] - ty))
505
+ if hypot(near[0] - tx, near[1] - ty) <= max(60.0, 0.9 * max(self.__m_targetSize, near[2])):
506
+ chosen = near
507
+ if chosen is None and faces and (self.__m_target is None or now - self.__m_lastSeen > LOST_MS):
508
+ if self.__m_choose == CHOOSE_NEAREST:
509
+ c = self._position() # type: QPoint
510
+ chosen = min(faces, key=lambda f: hypot(f[0] - c.x(), f[1] - c.y()))
511
+ else:
512
+ chosen = max(faces, key=lambda f: f[2])
513
+ if chosen is not None:
514
+ self.__m_target = QPoint(int(round(chosen[0])), int(round(chosen[1]))) # type: QPoint
515
+ self.__m_targetSize, self.__m_lastSeen = chosen[2], now
516
+ elif self.__m_target is not None and now - self.__m_lastSeen > LOST_MS:
517
+ self.__m_target = None
518
+ if self.__m_status in ('paused', 'blocked', 'blank'):
519
+ self.__m_detail.update(detail)
520
+ else:
521
+ self._setStatus('following' if self.__m_target is not None else 'searching', **detail)
522
+
523
+ def _onFoundView(self, job, faces, detail):
524
+ """
525
+ Games: learn the game's sensitivity from the scene's motion, choose a face near the crosshair, plan the
526
+ mouse movements that bring it there.
527
+ :param job: dict
528
+ :param faces: list[tuple[float, float, float]]
529
+ :param detail: dict
530
+ :return: None
531
+ """
532
+ now = self._now() # type: int
533
+ aim = job['aim'] # type: QPoint # the crosshair
534
+ grabSent = job['sentAtGrab'] # type: tuple[int, int]
535
+ sent = (0, 0) if self.__m_lastGrabSent is None else \
536
+ (grabSent[0] - self.__m_lastGrabSent[0], grabSent[1] - self.__m_lastGrabSent[1])
537
+ self.__m_lastGrabSent = tuple(grabSent) # type: tuple
538
+ shift = job.get('shift') if job.get('response', 0.0) >= 0.03 else None # scene motion between captures
539
+ self._learn(now, grabSent, shift)
540
+ radius = job['radius'] # type: float # the assist area around the crosshair
541
+ self.__m_ignored = [(p, until) for p, until in self.__m_ignored if until > now] # type: list
542
+ candidates = [f for f in faces if hypot(f[0] - aim.x(), f[1] - aim.y()) <= radius and
543
+ not any(hypot(f[0] - p.x(), f[1] - p.y()) < max(40.0, f[2]) for p, u in self.__m_ignored)]
544
+ detail['faces'] = len(candidates)
545
+ chosen = None
546
+ previous = self.__m_previousFace # type: tuple | None
547
+ if self.__m_target is not None and previous is not None and candidates:
548
+ sx, sy = shift if shift is not None else (-sent[0] * self.__m_k[0], -sent[1] * self.__m_k[1])
549
+ px, py = previous[0] + sx, previous[1] + sy # where the face should be after the turn
550
+ near = min(candidates, key=lambda f: hypot(f[0] - px, f[1] - py))
551
+ if hypot(near[0] - px, near[1] - py) <= max(60.0, 1.2 * max(self.__m_targetSize, near[2])):
552
+ chosen = near
553
+ if shift is not None and hypot(*shift) >= 8.0: # the scene turned: did the face turn with it?
554
+ faceMove = hypot(near[0] - previous[0], near[1] - previous[1]) # type: float
555
+ if faceMove < 0.3 * hypot(*shift):
556
+ self.__m_staticHits += 1
557
+ if self.__m_staticHits >= 3: # a portrait of the game's interface: ignore it for a while
558
+ self.__m_ignored.append((QPoint(int(near[0]), int(near[1])), now + STATIC_MS))
559
+ self.__m_target, self.__m_previousFace, self.__m_staticHits = None, None, 0
560
+ self.__m_pending = [0.0, 0.0] # type: list[float]
561
+ self._setStatus('static', **detail)
562
+ return
563
+ else:
564
+ self.__m_staticHits = 0 # type: int
565
+ if chosen is None and candidates and (self.__m_target is None or now - self.__m_lastSeen > LOST_VIEW_MS):
566
+ chosen = min(candidates, key=lambda f: hypot(f[3] - aim.x(), f[4] - aim.y())) \
567
+ if self.__m_choose == CHOOSE_NEAREST else max(candidates, key=lambda f: f[2])
568
+ self.__m_staticHits, self.__m_lastOffset, previous = 0, None, None # (a new enemy: no velocity yet)
569
+ self.__m_acquired = (hypot(chosen[3] - aim.x(), chosen[4] - aim.y()), tuple(grabSent)) # type: tuple
570
+ elif chosen is not None and self.__m_acquired is not None and \
571
+ hypot(chosen[3] - aim.x(), chosen[4] - aim.y()) <= max(20.0, 0.5 * chosen[2]):
572
+ self.__m_acquired = None # on the head: the approach is over (tracking may turn on and on)
573
+ elif chosen is not None and self.__m_acquired is not None and self.__m_noReaction < NO_REACTION_SCANS:
574
+ start, counts = self.__m_acquired # never a runaway, whatever was measured: far more sent than should
575
+ spent = (grabSent[0] - counts[0], grabSent[1] - counts[1]) # have centred it, and no closer
576
+ turned = hypot(spent[0] * self.__m_k[0] if self.__m_kKnown[0] else 0.0, # (measured axes only)
577
+ spent[1] * self.__m_k[1] if self.__m_kKnown[1] else 0.0) # type: float
578
+ closer = hypot(chosen[3] - aim.x(), chosen[4] - aim.y()) <= 0.6 * start # type: bool
579
+ if not closer and (turned > 2.5 * max(start, 40.0) or abs(spent[0]) + abs(spent[1]) > 4000):
580
+ self.__m_ignored.append((QPoint(int(chosen[0]), int(chosen[1])), now + STATIC_MS))
581
+ self.__m_target, self.__m_previousFace, self.__m_acquired = None, None, None
582
+ self._resetView() # what was learned towards it is not trusted
583
+ self._setStatus('static', **detail)
584
+ return
585
+ if chosen is None:
586
+ if self.__m_target is not None and now - self.__m_lastSeen > LOST_VIEW_MS:
587
+ self.__m_target, self.__m_previousFace = None, None
588
+ self.__m_pending = [0.0, 0.0] # type: list[float]
589
+ if self.__m_status not in ('blocked', 'waiting', 'blank'):
590
+ quiet = 'static' if self.__m_ignored else 'searching' # type: str
591
+ self._setStatus('following' if self.__m_target is not None else quiet, **detail)
592
+ return
593
+ grabMs = job.get('grabMs', now) # type: int
594
+ if previous is not None and shift is not None and self.__m_prevGrabMs is not None and \
595
+ grabMs > self.__m_prevGrabMs: # the enemy's own motion: on screen, minus the view's turn
596
+ dt = (grabMs - self.__m_prevGrabMs) / 1000.0 # type: float
597
+ own = ((chosen[0] - previous[0] - shift[0]) / dt, (chosen[1] - previous[1] - shift[1]) / dt)
598
+ v = self.__m_velocity # type: tuple
599
+ self.__m_velocity = (0.5 * v[0] + 0.5 * own[0], 0.5 * v[1] + 0.5 * own[1]) # type: tuple
600
+ self.__m_tracked += 1
601
+ elif previous is None:
602
+ self.__m_velocity, self.__m_tracked = (0.0, 0.0), 0
603
+ self.__m_prevGrabMs = grabMs # type: int
604
+ self.__m_target = QPoint(int(round(chosen[0])), int(round(chosen[1]))) # type: QPoint
605
+ self.__m_targetSize, self.__m_lastSeen, self.__m_previousFace = chosen[2], now, (chosen[0], chosen[1])
606
+ share = self.__m_strength / 100.0 # type: float
607
+ offset = (chosen[3] - aim.x(), chosen[4] - aim.y()) # type: tuple[float, float] # the head, not the face
608
+ if self.__m_lead and self.__m_tracked >= 2: # lead a moving enemy: where the head will be when we land
609
+ lead = (now - grabMs) / 1000.0 + LEAD_S # type: float
610
+ dx, dy = self.__m_velocity[0] * lead, self.__m_velocity[1] * lead
611
+ limit = 1.5 * chosen[2] # type: float # never far from what is seen (noise)
612
+ length = hypot(dx, dy) # type: float
613
+ if length > limit:
614
+ dx, dy = dx * limit / length, dy * limit / length
615
+ offset = (offset[0] + dx, offset[1] + dy)
616
+ last = self.__m_lastOffset # type: tuple | None
617
+ still = hypot(*self.__m_velocity) < 40.0 or self.__m_tracked < 2 # type: bool # (a walking enemy crosses
618
+ for i in (0, 1): # the crosshair by itself) crossed it: the sensitivity was too low (it would overshoot again)
619
+ if still and last is not None and self.__m_kKnown[i] and abs(offset[i]) > max(8.0, 0.5 * abs(last[i])) \
620
+ and (offset[i] > 0) != (last[i] > 0):
621
+ self.__m_k[i] *= 1.6
622
+ self.__m_samples[i] = (self.__m_samples[i] + [self.__m_k[i]])[-7:]
623
+ self.__m_pending[i] = 0.0
624
+ self.__m_lastOffset = offset # type: tuple
625
+ plan = [0.0, 0.0] # type: list[float]
626
+ for i in (0, 1): # what is still to correct, minus what was sent since this capture
627
+ inFlight = (self.__m_sent[i] - grabSent[i]) * self.__m_k[i] # type: float # px
628
+ counts = (offset[i] - inFlight) * share / self.__m_k[i] # type: float
629
+ if len(self.__m_samples[i]) < 3: # still learning: a cautious share
630
+ counts *= 0.6
631
+ plan[i] = counts if self.__m_kKnown[i] else clamp(counts, -self.__m_probe[i], self.__m_probe[i])
632
+ if self.__m_noReaction >= NO_REACTION_SCANS:
633
+ if now < self.__m_nextProbe:
634
+ plan = [0.0, 0.0] # type: list[float]
635
+ else:
636
+ self.__m_nextProbe = now + RETRY_MS # type: int # one test movement, then wait
637
+ plan = [clamp(p, -PROBE, PROBE) for p in plan] # type: list[float]
638
+ self.__m_pending = plan # type: list[float]
639
+ if self.__m_noReaction >= NO_REACTION_SCANS:
640
+ self._setStatus('noReaction', **detail)
641
+ elif self.__m_status not in ('blocked', 'waiting', 'blank'):
642
+ self._setStatus('following', **detail)
643
+
644
+ def _learn(self, now, grabSent, shift):
645
+ """
646
+ The game's sensitivity, measured over a window of ~0.4 - 0.9 s: the scene's total shift divided by the
647
+ mouse counts sent - a game shows a movement one or more frames later, so comparing two captures alone would
648
+ pair the wrong movement with the wrong shift.
649
+ :param now: int
650
+ :param grabSent: (tuple[int, int]) Counts sent when this capture was taken.
651
+ :param shift: (tuple[float, float] | None) Scene shift since the previous capture (px).
652
+ :return: None
653
+ """
654
+ if shift is None:
655
+ self.__m_history = [] # type: list # the chain is broken: start again
656
+ return
657
+ total = self.__m_history[-1][2] if self.__m_history else (0.0, 0.0)
658
+ total = (total[0] + shift[0], total[1] + shift[1]) # type: tuple
659
+ self.__m_history.append((now, tuple(grabSent), total))
660
+ self.__m_history = [h for h in self.__m_history if now - h[0] <= 900] # type: list
661
+ old = next((h for h in self.__m_history if now - h[0] >= 400), None) # (long: latency is a small share)
662
+ if old is None:
663
+ return
664
+ sent = (grabSent[0] - old[1][0], grabSent[1] - old[1][1]) # type: tuple
665
+ moved = (total[0] - old[2][0], total[1] - old[2][1]) # type: tuple
666
+ if max(abs(sent[0]), abs(sent[1])) < 20:
667
+ return
668
+ visible = False # type: bool
669
+ chasing = self.__m_tracked >= 2 and hypot(*self.__m_velocity) >= 40.0 # type: bool # a walking enemy
670
+ for axis in (0, 1):
671
+ if abs(sent[axis]) < 20:
672
+ continue
673
+ if chasing and len(self.__m_samples[axis]) >= 3: # The least reliable moments - and a game's.
674
+ visible = True # type: bool # Sensitivity does not change while playing: keep what was learned.
675
+ continue
676
+ if abs(moved[axis]) < 1.5: # No visible turn on this axis: a bigger test movement next time.
677
+ if not self.__m_kKnown[axis]:
678
+ self.__m_probe[axis] = min(PROBE_MAX, self.__m_probe[axis] * 2)
679
+ continue
680
+ visible = True # type: bool
681
+ measured = -moved[axis] / float(sent[axis]) # type: float # Turning right moves the scene left
682
+ sign = 1.0 if measured > 0 else -1.0 # type: float # (an inverted axis is negative)
683
+ samples = (self.__m_samples[axis] + [sign * clamp(abs(measured), 0.005, 50.0)])[-7:] # type: list
684
+ self.__m_samples[axis] = samples
685
+ self.__m_k[axis] = sorted(samples)[len(samples) // 2] # The median: one wrong reading changes nothing.
686
+ self.__m_kKnown[axis] = True
687
+ self.__m_probe[axis] = PROBE # type: int
688
+ self.__m_noReaction = 0 if visible else self.__m_noReaction + 1 # type: int
689
+ if self.__m_noReaction >= NO_REACTION_SCANS: # Test again now and then: it recovers when the game reacts
690
+ self.__m_kKnown = [False, False] # type: list[bool] # and never drifts meanwhile
691
+ if self.__m_noReaction == NO_REACTION_SCANS:
692
+ self.__m_nextProbe = now + RETRY_MS # type: int
693
+
694
+ def _position(self):
695
+ """
696
+ :return: (QPoint) What follows the face: the pointer, or the zoomer's target.
697
+ """
698
+ if self.__m_follow == FOLLOW_ZOOMER and self.__m_zoomPos is not None:
699
+ return QPoint(int(round(self.__m_zoomPos[0])), int(round(self.__m_zoomPos[1])))
700
+ return QCursor.pos()
701
+
702
+ @pyqtSlot()
703
+ def _move(self):
704
+ """
705
+ ~60 times per second: glide towards the face (films) or send part of the planned movement (games).
706
+ :return: None
707
+ """
708
+ now = self._now() # type: int
709
+ dt = max(1, now - self.__m_lastTick) / 1000.0 # type: float
710
+ self.__m_lastTick = now # type: int
711
+ if not self.__m_enabled:
712
+ return None
713
+ if self.__m_motion == MOTION_AUTO and now - self.__m_lastCheck >= 200:
714
+ self.__m_lastCheck = now # type: int
715
+ self._watchCapture(now)
716
+ if self._isView():
717
+ return self._moveView(dt)
718
+ if self.__m_follow == FOLLOW_POINTER:
719
+ current = QCursor.pos() # type: QPoint
720
+ last = self.__m_lastSet # type: QPoint | None
721
+ testing = False # type: bool # MOTION_AUTO: a tiny test step tells a game from a hand
722
+ if last is not None and (current - last).manhattanLength() > USER_MOVE_PX:
723
+ testing = self.__m_motion == MOTION_AUTO and self._putBack(current, now)
724
+ if self._isView():
725
+ return None
726
+ if not testing: # The user moved it.
727
+ self._pause(now)
728
+ return None
729
+ elif self.__m_snap is not None: # After the test step: hold still and watch.
730
+ if now - self.__m_snap[1] > 120:
731
+ self._pause(now) # It stayed where it was put: it was the user.
732
+ return None
733
+ if now < self.__m_pausedUntil:
734
+ self.__m_lastSet = None
735
+ return None
736
+ if self.__m_status == 'paused':
737
+ self._setStatus('following' if self.__m_target is not None else 'searching')
738
+ else:
739
+ testing = False # type: bool
740
+ if self.__m_target is None:
741
+ return None
742
+ tau = 0.6 * self.__m_smoothness / 100.0 # type: float # Seconds to cover ~63 % of the way.
743
+ k = 1.0 if tau <= 0.0 else 1.0 - exp(-dt / tau) # type: float
744
+ if self.__m_follow == FOLLOW_ZOOMER:
745
+ x, y = self.__m_zoomPos if self.__m_zoomPos is not None else (self.__m_target.x(), self.__m_target.y())
746
+ x += (self.__m_target.x() - x) * k
747
+ y += (self.__m_target.y() - y) * k
748
+ self.__m_zoomPos = (x, y) # type: tuple
749
+ if self.__m_moveZoomer is not None:
750
+ self.__m_moveZoomer(QPoint(int(round(x)), int(round(y))))
751
+ return None
752
+ d = self.__m_target - current # type: QPoint
753
+ if d.manhattanLength() <= 1:
754
+ self.__m_lastSet = current # type: QPoint
755
+ return None
756
+ step = QPoint(int(round(d.x() * k)), int(round(d.y() * k))) # type: QPoint
757
+ if testing: # only a few pixels: if it was the user, the pointer stays (almost) where they left it
758
+ length = max(1.0, hypot(d.x(), d.y())) # type: float
759
+ step = QPoint(int(round(3 * d.x() / length)), int(round(3 * d.y() / length)))
760
+ if step.isNull(): # always progress at least one pixel
761
+ step = QPoint((d.x() > 0) - (d.x() < 0), (d.y() > 0) - (d.y() < 0))
762
+ wanted = current + step # type: QPoint
763
+ QCursor.setPos(wanted)
764
+ self.__m_lastMoveMs = self._now() # type: int
765
+ actual = QCursor.pos() # type: QPoint
766
+ if (actual - wanted).manhattanLength() > 2: # the system refused (Wayland, macOS permission)
767
+ self.__m_blocked += 1
768
+ if self.__m_blocked >= 15 and self.__m_status != 'blocked':
769
+ self._setStatus('blocked')
770
+ else:
771
+ self.__m_blocked = 0 # type: int
772
+ self.__m_lastSet = actual # type: QPoint # (a refused move is not the user's).
773
+ return None
774
+
775
+ def _pause(self, now):
776
+ """
777
+ The user took the mouse: give it back for a while.
778
+ :param now: int
779
+ :return:
780
+ """
781
+ self.__m_pausedUntil = now + int(self.__m_pauseSeconds * 1000) # type: int
782
+ self.__m_lastSet, self.__m_snap = None, None
783
+ self._setStatus('paused', seconds=self.__m_pauseSeconds)
784
+
785
+ def _putBack(self, current, now):
786
+ """
787
+ MOTION_AUTO: the pointer moved by itself. The first time, a tiny test step follows; put back on the same
788
+ spot again, it is a game that captures the mouse (a hand never lands on the same pixel twice): its view is
789
+ turned from now on.
790
+ :param current: QPoint
791
+ :param now: int
792
+ :return: (bool) Testing (no pause yet).
793
+ """
794
+ snap = self.__m_snap # type: tuple | None
795
+ if snap is not None and (current - snap[0]).manhattanLength() <= 2 and now - snap[1] < 1500:
796
+ self._setCaptured(True, 'snap')
797
+ return False
798
+ if snap is None:
799
+ self.__m_snap = (QPoint(current), now) # type: tuple
800
+ self.__m_lastSet = current # type: QPoint
801
+ return True
802
+ return False # moved again, elsewhere: the user
803
+
804
+ def _setCaptured(self, captured, by=''):
805
+ """
806
+ MOTION_AUTO: a game captures the mouse (center the face on the screen) - or no longer (move the pointer).
807
+ :param captured: bool
808
+ :param by: (str | unicode | QString) 'signal' or 'snap'.
809
+ :return:
810
+ """
811
+ self.__m_captured, self.__m_captureBy = bool(captured), by if captured else ''
812
+ self.__m_snap, self.__m_lastSet, self.__m_pausedUntil = None, None, 0
813
+ self.__m_target, self.__m_zoomPos, self.__m_hiddenSince, self.__m_visibleSince = None, None, None, None
814
+ self._resetView()
815
+ self._restartScans()
816
+ self._setStatus('searching', auto=bool(captured))
817
+
818
+ def _watchCapture(self, now):
819
+ """
820
+ MOTION_AUTO, every 200 ms: does a game capture the mouse? Confined (Windows): yes. Hidden: yes if it stays
821
+ hidden while it is moved (a resting video player shows it again). Visible again for 1 s: a menu - the pointer
822
+ is moved again (only when the system's signal decided, not the 'put back' test).
823
+ :param now: int
824
+ :return:
825
+ """
826
+ state = mouseCapture() # type: str
827
+ if not self.__m_captured:
828
+ moving = now - self.__m_lastMoveMs < 400 # type: bool # we are moving it: a video player would show it
829
+ if state == 'clipped' or (state == 'hidden' and moving and self.__m_hiddenSince is not None and
830
+ now - self.__m_hiddenSince >= 1000):
831
+ self._setCaptured(True, 'signal')
832
+ elif state == 'hidden' and moving:
833
+ self.__m_hiddenSince = self.__m_hiddenSince if self.__m_hiddenSince is not None else now
834
+ elif state != 'hidden':
835
+ self.__m_hiddenSince = None
836
+ elif self.__m_captureBy == 'signal':
837
+ if state == '':
838
+ self.__m_visibleSince = self.__m_visibleSince if self.__m_visibleSince is not None else now
839
+ if now - self.__m_visibleSince >= 1000:
840
+ self._setCaptured(False)
841
+ else:
842
+ self.__m_visibleSince = None
843
+
844
+ def _moveView(self, dt):
845
+ """
846
+ Games, ~60 times per second: send part of the planned mouse movement (smoothly, capped).
847
+ :param dt: (float | int) s since the last move.
848
+ :return: None
849
+ """
850
+ if self.__m_hold == HOLD_RIGHT_BUTTON and not self.__m_watcher.isHeld():
851
+ self.__m_pending = [0.0, 0.0] # type: list[float]
852
+ if self.__m_status != 'waiting':
853
+ self._setStatus('waiting')
854
+ return
855
+ if self.__m_status == 'waiting':
856
+ self._setStatus('following' if self.__m_target is not None else 'searching')
857
+ tau = max(0.016, 0.15 * self.__m_smoothness / 100.0) # type: float
858
+ k = 1.0 - exp(-dt / tau) # type: float
859
+ step = [int(round(clamp(p * k, -MAX_STEP, MAX_STEP))) for p in self.__m_pending] # type: list[int]
860
+ for i in (0, 1):
861
+ if step[i] == 0 and abs(self.__m_pending[i]) >= 1.0: # always progress
862
+ step[i] = 1 if self.__m_pending[i] > 0 else -1
863
+ if not step[0] and not step[1]:
864
+ return
865
+ if not sendRelativeMove(step[0], step[1]):
866
+ if self.__m_status != 'blocked':
867
+ self._setStatus('blocked')
868
+ return
869
+ for i in (0, 1):
870
+ self.__m_sent[i] += step[i]
871
+ self.__m_pending[i] -= step[i]
872
+
873
+ def _syncWatcher(self):
874
+ """
875
+ Watch the right mouse button only while it matters.
876
+ :return:
877
+ """
878
+ if self.__m_enabled and self.__m_motion != MOTION_POINTER and self.__m_hold == HOLD_RIGHT_BUTTON:
879
+ self.__m_watcher.start()
880
+ else:
881
+ self.__m_watcher.stop()
882
+
883
+ # ---------- Public API ----------
884
+ def isEnabled(self):
885
+ """
886
+ :return: bool
887
+ """
888
+ return self.__m_enabled
889
+
890
+ @pyqtSlot(bool)
891
+ @pyqtSlot(int)
892
+ def setEnabled(self, enabled):
893
+ """
894
+ :param enabled: bool
895
+ :return: None
896
+ """
897
+ enabled = bool(enabled) and FacePointer.isAvailable() # type: bool
898
+ if enabled == self.__m_enabled:
899
+ if not FacePointer.isAvailable():
900
+ self._setStatus('unavailable')
901
+ return
902
+ self.__m_enabled = enabled # type: bool
903
+ self.__m_target, self.__m_lastSet, self.__m_zoomPos, self.__m_snap = None, None, None, None
904
+ self.__m_pausedUntil, self.__m_blocked, self.__m_blank = 0, 0, 0
905
+ self.__m_captured, self.__m_captureBy = False, '' # MOTION_AUTO finds out again (another game, perhaps).
906
+ self.__m_hiddenSince, self.__m_visibleSince = None, None
907
+ self.__m_generation += 1 # a running worker's result will be dropped (it keeps 'busy' until it ends).
908
+ self._resetView()
909
+ self._syncWatcher()
910
+ if enabled:
911
+ self._restartScans()
912
+ self.__m_moveTimer.start()
913
+ self._setStatus('searching')
914
+ self._scan()
915
+ else:
916
+ self.__m_scanTimer.stop()
917
+ self.__m_moveTimer.stop()
918
+ self._setStatus('off')
919
+ self.enabledChanged.emit(enabled)
920
+
921
+ @pyqtSlot()
922
+ def toggle(self):
923
+ """
924
+ :return:
925
+ """
926
+ self.setEnabled(not self.__m_enabled)
927
+
928
+ def getStatus(self):
929
+ """
930
+ :return: (str | unicode | QString)
931
+ off, searching, following, paused, waiting, static, blocked, blank, noReaction,
932
+ unavailable or error.
933
+ """
934
+ return self.__m_status if FacePointer.isAvailable() else 'unavailable'
935
+
936
+ def getStatusDetail(self):
937
+ """
938
+ :return: (dict) faces, backend, timeMs, auto, seconds, message (when known).
939
+ """
940
+ return dict(self.__m_detail)
941
+
942
+ def isTurningView(self):
943
+ """
944
+ :return: (bool) The game's view is being turned (MOTION_VIEW, or MOTION_AUTO found a game).
945
+ """
946
+ return self._isView()
947
+
948
+ def getTarget(self):
949
+ """
950
+ :return: (QPoint | None) The followed face (global center).
951
+ """
952
+ return QPoint(self.__m_target) if self.__m_target is not None else None
953
+
954
+ def getSensitivity(self):
955
+ """
956
+ :return: (tuple[float, float]) Measured screen pixels per mouse count (x, y; negative = inverted axis).
957
+ """
958
+ return tuple(self.__m_k)
959
+
960
+ @staticmethod
961
+ def canTurnView():
962
+ """
963
+ :return: (bool) This system lets the application send mouse movements (not Wayland).
964
+ """
965
+ return canSendRelative()
966
+
967
+ def getFollow(self):
968
+ """
969
+ :return: int
970
+ """
971
+ return self.__m_follow
972
+
973
+ @pyqtSlot(int)
974
+ def setFollow(self, follow):
975
+ """
976
+ :param follow: (int) FOLLOW_POINTER or FOLLOW_ZOOMER.
977
+ :return:
978
+ """
979
+ self.__m_follow = FOLLOW_ZOOMER if int(follow) == FOLLOW_ZOOMER else FOLLOW_POINTER # type: int
980
+ self.__m_lastSet, self.__m_zoomPos, self.__m_pausedUntil, self.__m_blocked = None, None, 0, 0
981
+ if self.__m_status in ('paused', 'blocked'):
982
+ self._setStatus('following' if self.__m_target is not None else 'searching')
983
+
984
+ def getChoose(self):
985
+ """
986
+ :return: int
987
+ """
988
+ return self.__m_choose
989
+
990
+ @pyqtSlot(int)
991
+ def setChoose(self, choose):
992
+ """
993
+ :param choose: (int) CHOOSE_LARGEST or CHOOSE_NEAREST.
994
+ :return:
995
+ """
996
+ self.__m_choose = CHOOSE_NEAREST if int(choose) == CHOOSE_NEAREST else CHOOSE_LARGEST # type: int
997
+
998
+ def getMotion(self):
999
+ """
1000
+ :return: int
1001
+ """
1002
+ return self.__m_motion
1003
+
1004
+ @pyqtSlot(int)
1005
+ def setMotion(self, motion):
1006
+ """
1007
+ :param motion: (int) MOTION_AUTO, MOTION_POINTER or MOTION_VIEW.
1008
+ :return:
1009
+ """
1010
+ motion = int(motion) if int(motion) in (MOTION_POINTER, MOTION_VIEW, MOTION_AUTO) else MOTION_AUTO
1011
+ if motion != self.__m_motion:
1012
+ self.__m_motion = motion # type: int
1013
+ self.__m_captured, self.__m_snap = False, None
1014
+ self.__m_target, self.__m_lastSet, self.__m_pausedUntil = None, None, 0
1015
+ self._resetView()
1016
+ self._syncWatcher()
1017
+ self._restartScans()
1018
+ if self.__m_status in ('paused', 'blocked', 'waiting', 'static', 'noReaction'):
1019
+ self._setStatus('searching' if self.__m_enabled else 'off')
1020
+
1021
+ def getHold(self):
1022
+ """
1023
+ :return: int
1024
+ """
1025
+ return self.__m_hold
1026
+
1027
+ @pyqtSlot(int)
1028
+ def setHold(self, hold):
1029
+ """
1030
+ :param hold: (int) HOLD_ALWAYS or HOLD_RIGHT_BUTTON.
1031
+ :return:
1032
+ """
1033
+ self.__m_hold = HOLD_RIGHT_BUTTON if int(hold) == HOLD_RIGHT_BUTTON else HOLD_ALWAYS # type: int
1034
+ self._syncWatcher()
1035
+
1036
+ def getSmoothness(self):
1037
+ """
1038
+ :return: int
1039
+ """
1040
+ return self.__m_smoothness
1041
+
1042
+ @pyqtSlot(int)
1043
+ def setSmoothness(self, smoothness):
1044
+ """
1045
+ :param smoothness: (int) 0 .. 100.
1046
+ :return:
1047
+ """
1048
+ self.__m_smoothness = int(clamp(int(smoothness), 0, 100)) # type: int
1049
+
1050
+ def getPauseSeconds(self):
1051
+ """
1052
+ :return: float | int
1053
+ """
1054
+ return self.__m_pauseSeconds
1055
+
1056
+ @pyqtSlot(float)
1057
+ @pyqtSlot(int)
1058
+ def setPauseSeconds(self, pauseSeconds):
1059
+ """
1060
+ :param pauseSeconds: float | int
1061
+ :return:
1062
+ """
1063
+ self.__m_pauseSeconds = float(clamp(float(pauseSeconds), 0.5, 30.0)) # type: float
1064
+
1065
+ def getScanRate(self):
1066
+ """
1067
+ :return: int
1068
+ """
1069
+ return self.__m_scanRate
1070
+
1071
+ @pyqtSlot(int)
1072
+ def setScanRate(self, scanRate):
1073
+ """
1074
+ :param scanRate: (int) Whole-screen scans per second, 1 .. 15 (films; games run at ~30).
1075
+ :return:
1076
+ """
1077
+ self.__m_scanRate = int(clamp(int(scanRate), 1, 15)) # type: int
1078
+ if self.__m_scanTimer.isActive():
1079
+ self._restartScans()
1080
+
1081
+ def getAssistArea(self):
1082
+ """
1083
+ :return: int
1084
+ """
1085
+ return self.__m_assistArea
1086
+
1087
+ @pyqtSlot(int)
1088
+ def setAssistArea(self, assistArea):
1089
+ """
1090
+ :param assistArea: (int) 10 .. 100 (% of half the screen height).
1091
+ :return:
1092
+ """
1093
+ self.__m_assistArea = int(clamp(int(assistArea), 10, 100)) # type: int
1094
+ with self.__m_lock:
1095
+ self.__m_prevGray = None # (another capture size)
1096
+
1097
+ def getStrength(self):
1098
+ """
1099
+ :return: int
1100
+ """
1101
+ return self.__m_strength
1102
+
1103
+ @pyqtSlot(int)
1104
+ def setStrength(self, strength):
1105
+ """
1106
+ :param strength: (int) 10 .. 100 (%).
1107
+ :return:
1108
+ """
1109
+ self.__m_strength = int(clamp(int(strength), 10, 100)) # type: int
1110
+
1111
+ # ---------- Properties ----------
1112
+ enabled = pyqtProperty(bool, fget=isEnabled, fset=setEnabled, notify=enabledChanged) # type: pyqtProperty
1113
+ follow = pyqtProperty(int, fget=getFollow, fset=setFollow) # type: pyqtProperty
1114
+ choose = pyqtProperty(int, fget=getChoose, fset=setChoose) # type: pyqtProperty
1115
+ motion = pyqtProperty(int, fget=getMotion, fset=setMotion) # type: pyqtProperty
1116
+ hold = pyqtProperty(int, fget=getHold, fset=setHold) # type: pyqtProperty
1117
+ smoothness = pyqtProperty(int, fget=getSmoothness, fset=setSmoothness) # type: pyqtProperty
1118
+ pauseSeconds = pyqtProperty(float, fget=getPauseSeconds, fset=setPauseSeconds) # type: pyqtProperty
1119
+ scanRate = pyqtProperty(int, fget=getScanRate, fset=setScanRate) # type: pyqtProperty
1120
+ assistArea = pyqtProperty(int, fget=getAssistArea, fset=setAssistArea) # type: pyqtProperty
1121
+ strength = pyqtProperty(int, fget=getStrength, fset=setStrength) # type: pyqtProperty