Deskflow 1.24.0.365
Keyboard and mouse sharing utility
Loading...
Searching...
No Matches
MSWindowsScreen.h
Go to the documentation of this file.
1/*
2 * Deskflow -- mouse and keyboard sharing utility
3 * SPDX-FileCopyrightText: (C) 2025 Deskflow Developers
4 * SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
5 * SPDX-FileCopyrightText: (C) 2002 Chris Schoeneman
6 * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
7 */
8
9#pragma once
10
14
15#include <map>
16#include <string>
17
18#define WIN32_LEAN_AND_MEAN
19#include <Windows.h>
20
21class EventQueueTimer;
22class MSWindowsDesks;
25class Thread;
26class MSWindowsDropTarget;
27
30{
31public:
33 bool isPrimary, bool useHooks, IEventQueue *events, bool enableLangSync = false, bool invetScrolling = false
34 );
35 ~MSWindowsScreen() override;
36
38
39
41
45 static void init(HINSTANCE);
46
48
50
52
55 static HINSTANCE getWindowInstance();
56
58
59 // IScreen overrides
60 void *getEventTarget() const override;
61 bool getClipboard(ClipboardID id, IClipboard *) const override;
62 void getShape(int32_t &x, int32_t &y, int32_t &width, int32_t &height) const override;
63 void getCursorPos(int32_t &x, int32_t &y) const override;
64
70 virtual bool getThisCursorPos(LPPOINT pos);
77 virtual bool setThisCursorPos(int x, int y);
78
83 virtual void updateDesktopThread();
84
85 // IPrimaryScreen overrides
86 void reconfigure(uint32_t activeSides) override;
87 uint32_t activeSides() override;
88 void warpCursor(int32_t x, int32_t y) override;
89 uint32_t registerHotKey(KeyID key, KeyModifierMask mask) override;
90 void unregisterHotKey(uint32_t id) override;
91 void fakeInputBegin() override;
92 void fakeInputEnd() override;
93 int32_t getJumpZoneSize() const override;
94 bool isAnyMouseButtonDown(uint32_t &buttonID) const override;
95 void getCursorCenter(int32_t &x, int32_t &y) const override;
96
97 // ISecondaryScreen overrides
98 void fakeMouseButton(ButtonID id, bool press) override;
99 void fakeMouseMove(int32_t x, int32_t y) override;
100 void fakeMouseRelativeMove(int32_t dx, int32_t dy) const override;
101 void fakeMouseWheel(int32_t xDelta, int32_t yDelta) const override;
102
103 // IKeyState overrides
104 virtual void updateKeys();
105 void fakeKeyDown(KeyID id, KeyModifierMask mask, KeyButton button, const std::string &lang) override;
106 bool fakeKeyRepeat(KeyID id, KeyModifierMask mask, int32_t count, KeyButton button, const std::string &lang) override;
107 bool fakeKeyUp(KeyButton button) override;
108 void fakeAllKeysUp() override;
109
110 // IPlatformScreen overrides
111 void enable() override;
112 void disable() override;
113 void enter() override;
114 bool canLeave() override;
115 void leave() override;
116 bool setClipboard(ClipboardID, const IClipboard *) override;
117 void checkClipboards() override;
118 void openScreensaver(bool notify) override;
119 void closeScreensaver() override;
120 void screensaver(bool activate) override;
121 void resetOptions() override;
122 void setOptions(const OptionsList &options) override;
123 void setSequenceNumber(uint32_t) override;
124 bool isPrimary() const override;
125 std::string getSecureInputApp() const override;
126
127protected:
128 // IPlatformScreen overrides
129 void handleSystemEvent(const Event &event) override;
130 void updateButtons() override;
131 IKeyState *getKeyState() const override;
132
133 // simulate a local key to the system directly
134 void fakeLocalKey(KeyButton button, bool press) const;
135
136private:
137 // initialization and shutdown operations
138 HCURSOR createBlankCursor() const;
139 void destroyCursor(HCURSOR cursor) const;
140 ATOM createWindowClass() const;
141 ATOM createDeskWindowClass(bool isPrimary) const;
142 void destroyClass(ATOM windowClass) const;
143 HWND createWindow(ATOM windowClass, const wchar_t *name) const;
144 void destroyWindow(HWND) const;
145
146 // convenience function to send events
147public: // HACK
148 void sendEvent(EventTypes type, void * = nullptr);
149
150private: // HACK
151 void sendClipboardEvent(EventTypes type, ClipboardID id);
152
153 // handle message before it gets dispatched. returns true iff
154 // the message should not be dispatched.
155 bool onPreDispatch(HWND, UINT, WPARAM, LPARAM);
156
157 // handle message before it gets dispatched. returns true iff
158 // the message should not be dispatched.
159 bool onPreDispatchPrimary(HWND, UINT, WPARAM, LPARAM);
160
161 // handle secondary message before it gets dispatched. returns true iff
162 // the message should not be dispatched.
163 bool onPreDispatchSecondary(HWND, UINT, WPARAM, LPARAM);
164
165 // handle message. returns true iff handled and optionally sets
166 // \c *result (which defaults to 0).
167 bool onEvent(HWND, UINT, WPARAM, LPARAM, LRESULT *result);
168
169 // message handlers
170 bool onMark(uint32_t mark);
171 bool onKey(WPARAM, LPARAM);
172 bool onHotKey(WPARAM, LPARAM);
173 bool onMouseButton(WPARAM, LPARAM);
174 bool onMouseMove(int32_t x, int32_t y);
175 bool onMouseWheel(int32_t xDelta, int32_t yDelta);
176 bool onScreensaver(bool activated);
177 bool onDisplayChange();
178 void onClipboardChange();
179
180 // warp cursor without discarding queued events
181 void warpCursorNoFlush(int32_t x, int32_t y);
182
183 // discard posted messages
184 void nextMark();
185
186 // test if event should be ignored
187 bool ignore() const;
188
189 // update screen size cache
190 void updateScreenShape();
191
192 // fix timer callback
193 void handleFixes();
194
195 // fix the clipboard viewer chain
196 void fixClipboardViewer();
197
198 // enable/disable special key combinations so we can catch/pass them
199 void enableSpecialKeys(bool) const;
200
201 // map a button event to a button ID
202 ButtonID mapButtonFromEvent(WPARAM msg, LPARAM button) const;
203
204 // map a button event to a press (true) or release (false)
205 bool mapPressFromEvent(WPARAM msg, LPARAM button) const;
206
207 // job to update the key state
208 void updateKeysCB(const void *);
209
210 // determine whether the mouse is hidden by the system.
211 // if true and on secondary screen, enable mouse keys to show the cursor.
212 // we were previously restoring the old mouse key settings when not needed, but this was causing
213 // issues where the mouse cursor becomes permanently hidden, even if there is a real mouse
214 // attached to the system; this could be a windows bug, but losing your mouse is a nightmare
215 // so we shouldn't risk doing that.
216 void setupMouseKeys();
217
218 // enables the mouse keys accessibility feature to to ensure the
219 // mouse cursor can be shown.
220 void updateMouseKeys();
221
222 // our window proc
223 static LRESULT CALLBACK wndProc(HWND, UINT, WPARAM, LPARAM);
224
225 // save last position of mouse to compute next delta movement
226 void saveMousePosition(int32_t x, int32_t y);
227
228 // check if it is a modifier key repeating message
229 bool isModifierRepeat(KeyModifierMask oldState, KeyModifierMask state, WPARAM wParam) const;
230
231private:
232 struct HotKeyItem
233 {
234 public:
235 HotKeyItem(UINT vk, UINT modifiers);
236
237 UINT getVirtualKey() const;
238
239 bool operator<(const HotKeyItem &) const;
240
241 private:
242 UINT m_keycode;
243 UINT m_mask;
244 };
245 using HotKeyMap = std::map<uint32_t, HotKeyItem>;
246 using HotKeyIDList = std::vector<uint32_t>;
247 using HotKeyToIDMap = std::map<HotKeyItem, uint32_t>;
248 using PrimaryKeyDownList = std::vector<KeyButton>;
249
250 static HINSTANCE s_windowInstance;
251
252 // true if screen is being used as a primary screen, false otherwise
253 bool m_isPrimary;
254
255 // true if hooks are not to be installed (useful for debugging)
256 bool m_useHooks;
257
258 // true if mouse has entered the screen
259 bool m_isOnScreen;
260
261 // true if the screen is enabled
262 bool m_isEnabled = false;
263
264 // our resources
265 ATOM m_class = 0;
266
267 // screen shape stuff
268 int32_t m_x = 0;
269 int32_t m_y = 0;
270 int32_t m_w = 0;
271 int32_t m_h = 0;
272 int32_t m_xCenter = 0;
273 int32_t m_yCenter = 0;
274
275 // true if system appears to have multiple monitors
276 bool m_multimon = false;
277
278 // last mouse position
279 int32_t m_xCursor = 0;
280 int32_t m_yCursor = 0;
281
282 // last clipboard
283 uint32_t m_sequenceNumber = 0;
284
285 // used to discard queued messages that are no longer needed
286 uint32_t m_mark = 0;
287 uint32_t m_markReceived = 0;
288
289 // the main loop's thread id
290 DWORD m_threadID;
291
292 // timer for periodically checking stuff that requires polling
293 EventQueueTimer *m_fixTimer = nullptr;
294
295 // the keyboard layout to use when off primary screen
296 HKL m_keyLayout = nullptr;
297
298 // screen saver stuff
299 MSWindowsScreenSaver *m_screensaver = nullptr;
300 bool m_screensaverNotify = false;
301 bool m_screensaverActive = false;
302
303 // clipboard stuff. our window is used mainly as a clipboard
304 // owner and as a link in the clipboard viewer chain.
305 HWND m_window = nullptr;
306 DWORD m_clipboardSequenceNumber = 0;
307 bool m_ownClipboard = false;
308
309 // one desk per desktop and a cond var to communicate with it
310 MSWindowsDesks *m_desks = nullptr;
311
312 // keyboard stuff
313 MSWindowsKeyState *m_keyState = nullptr;
314
315 // hot key stuff
316 HotKeyMap m_hotKeys;
317 HotKeyIDList m_oldHotKeyIDs;
318 HotKeyToIDMap m_hotKeyToIDMap;
319
320 // map of button state
321 bool m_buttons[1 + kButtonExtra0 + 1];
322
323 // m_hasMouse is true if there's a mouse attached to the system or
324 // mouse keys is simulating one. we track this so we can force the
325 // cursor to be displayed when the user has entered this screen.
326 bool m_hasMouse;
327
328 bool m_gotMouseKeys = false;
329 MOUSEKEYS m_mouseKeys;
330
331 MSWindowsHook m_hook;
332
333 static MSWindowsScreen *s_screen;
334
335 IEventQueue *m_events;
336
337 std::string m_desktopPath;
338
339 PrimaryKeyDownList m_primaryKeyDownList;
340 MSWindowsPowerManager m_powerManager;
341};
static const struct sockaddr FAR * name
Definition ArchNetworkWinsock.cpp:28
static const WSAEVENT FAR DWORD
Definition ArchNetworkWinsock.cpp:55
static int type
Definition ArchNetworkWinsock.cpp:45
uint8_t ClipboardID
Clipboard ID.
Definition ClipboardTypes.h:16
int key
Definition KeySequence.cpp:15
uint32_t KeyID
Key ID.
Definition KeyTypes.h:21
uint16_t KeyButton
Key Code.
Definition KeyTypes.h:34
uint32_t KeyModifierMask
Modifier key mask.
Definition KeyTypes.h:44
uint8_t ButtonID
Mouse button ID.
Definition MouseTypes.h:16
static const ButtonID kButtonExtra0
Definition MouseTypes.h:24
std::vector< uint32_t > OptionsList
Definition OptionTypes.h:26
int y
Definition ServerConfig.cpp:25
int x
Definition ServerConfig.cpp:24
Definition EventQueueTimer.h:12
Event.
Definition Event.h:29
Clipboard interface.
Definition IClipboard.h:19
Event queue interface.
Definition IEventQueue.h:29
IKeyState(const IEventQueue *events)
Definition IKeyState.cpp:17
Microsoft Windows desk handling.
Definition MSWindowsDesks.h:46
Loads and provides functions for the Windows hook.
Definition MSWindowsHook.h:47
Microsoft Windows key mapper.
Definition MSWindowsKeyState.h:28
Definition MSWindowsPowerManager.h:11
Microsoft windows screen saver implementation.
Definition MSWindowsScreenSaver.h:19
void fakeMouseRelativeMove(int32_t dx, int32_t dy) const override
Fake mouse move.
Definition MSWindowsScreen.cpp:706
void unregisterHotKey(uint32_t id) override
Unregister a system hotkey.
Definition MSWindowsScreen.cpp:618
uint32_t registerHotKey(KeyID key, KeyModifierMask mask) override
Register a system hotkey.
Definition MSWindowsScreen.cpp:541
static HINSTANCE getWindowInstance()
Get instance.
Definition MSWindowsScreen.cpp:170
MSWindowsScreen(bool isPrimary, bool useHooks, IEventQueue *events, bool enableLangSync=false, bool invetScrolling=false)
Definition MSWindowsScreen.cpp:82
void warpCursor(int32_t x, int32_t y) override
Warp cursor.
Definition MSWindowsScreen.cpp:518
bool canLeave() override
Leave screen.
Definition MSWindowsScreen.cpp:267
void getShape(int32_t &x, int32_t &y, int32_t &width, int32_t &height) const override
Get screen shape.
Definition MSWindowsScreen.cpp:428
void leave() override
Leave screen.
Definition MSWindowsScreen.cpp:280
void handleSystemEvent(const Event &event) override
Handle system event.
Definition MSWindowsScreen.cpp:837
void enter() override
Enter screen.
Definition MSWindowsScreen.cpp:237
void setSequenceNumber(uint32_t) override
Set clipboard sequence number.
Definition MSWindowsScreen.cpp:406
~MSWindowsScreen() override
Definition MSWindowsScreen.cpp:143
bool isAnyMouseButtonDown(uint32_t &buttonID) const override
Test if mouse is pressed.
Definition MSWindowsScreen.cpp:670
void enable() override
Enable screen.
Definition MSWindowsScreen.cpp:175
void fakeInputBegin() override
Prepare to synthesize input on primary screen.
Definition MSWindowsScreen.cpp:645
static void init(HINSTANCE)
Initialize.
Definition MSWindowsScreen.cpp:161
void * getEventTarget() const override
Get event target.
Definition MSWindowsScreen.cpp:416
bool fakeKeyRepeat(KeyID id, KeyModifierMask mask, int32_t count, KeyButton button, const std::string &lang) override
Fake a key repeat.
Definition MSWindowsScreen.cpp:729
void updateButtons() override
Update mouse buttons.
Definition MSWindowsScreen.cpp:849
void getCursorPos(int32_t &x, int32_t &y) const override
Get cursor position.
Definition MSWindowsScreen.cpp:438
void sendEvent(EventTypes type, void *=nullptr)
Definition MSWindowsScreen.cpp:820
void closeScreensaver() override
Close screen saver.
Definition MSWindowsScreen.cpp:371
virtual void updateKeys()
Definition MSWindowsScreen.cpp:718
void screensaver(bool activate) override
Activate/deactivate screen saver.
Definition MSWindowsScreen.cpp:383
IKeyState * getKeyState() const override
Get the key state.
Definition MSWindowsScreen.cpp:860
void getCursorCenter(int32_t &x, int32_t &y) const override
Get cursor center position.
Definition MSWindowsScreen.cpp:686
void openScreensaver(bool notify) override
Open screen saver.
Definition MSWindowsScreen.cpp:359
void fakeMouseMove(int32_t x, int32_t y) override
Fake mouse move.
Definition MSWindowsScreen.cpp:701
bool setClipboard(ClipboardID, const IClipboard *) override
Set clipboard.
Definition MSWindowsScreen.cpp:321
void resetOptions() override
Notify of options changes.
Definition MSWindowsScreen.cpp:396
std::string getSecureInputApp() const override
Determine the name of the app causing a secure input state.
Definition MSWindowsScreen.cpp:1712
void fakeInputEnd() override
Done synthesizing input on primary screen.
Definition MSWindowsScreen.cpp:655
void setOptions(const OptionsList &options) override
Notify of options changes.
Definition MSWindowsScreen.cpp:401
void fakeMouseWheel(int32_t xDelta, int32_t yDelta) const override
Fake mouse wheel.
Definition MSWindowsScreen.cpp:711
void fakeAllKeysUp() override
Fake key releases for all fake pressed keys.
Definition MSWindowsScreen.cpp:745
void checkClipboards() override
Check clipboard owner.
Definition MSWindowsScreen.cpp:338
virtual bool setThisCursorPos(int x, int y)
Sets the cursor position on the current machine.
Definition MSWindowsScreen.cpp:468
bool isPrimary() const override
Test if is primary screen.
Definition MSWindowsScreen.cpp:411
int32_t getJumpZoneSize() const override
Get jump zone size.
Definition MSWindowsScreen.cpp:665
void disable() override
Disable screen.
Definition MSWindowsScreen.cpp:203
virtual bool getThisCursorPos(LPPOINT pos)
Get the position of the cursor on the current machine.
Definition MSWindowsScreen.cpp:450
void fakeMouseButton(ButtonID id, bool press) override
Fake mouse press/release.
Definition MSWindowsScreen.cpp:692
bool getClipboard(ClipboardID id, IClipboard *) const override
Get clipboard.
Definition MSWindowsScreen.cpp:421
void fakeKeyDown(KeyID id, KeyModifierMask mask, KeyButton button, const std::string &lang) override
Fake a key press.
Definition MSWindowsScreen.cpp:723
void fakeLocalKey(KeyButton button, bool press) const
Definition MSWindowsScreen.cpp:1681
void reconfigure(uint32_t activeSides) override
Update configuration.
Definition MSWindowsScreen.cpp:505
bool fakeKeyUp(KeyButton button) override
Fake a key release.
Definition MSWindowsScreen.cpp:738
uint32_t activeSides() override
activeSides
Definition MSWindowsScreen.cpp:513
virtual void updateDesktopThread()
This function will attempt to switch to the current desktop the mouse is located on.
Definition MSWindowsScreen.cpp:486
PlatformScreen(IEventQueue *events, bool invertScrollDirection)
Definition PlatformScreen.cpp:12
Thread handle.
Definition Thread.h:33
EventTypes
Definition EventTypes.h:13