Deskflow 1.26.0.0
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:
32 MSWindowsScreen(bool isPrimary, bool useHooks, IEventQueue *events, bool enableLangSync = false);
33 ~MSWindowsScreen() override;
34
36
37
39
43 static void init(HINSTANCE);
44
46
48
50
53 static HINSTANCE getWindowInstance();
54
56
57 // IScreen overrides
58 void *getEventTarget() const override;
59 bool getClipboard(ClipboardID id, IClipboard *) const override;
60 void getShape(int32_t &x, int32_t &y, int32_t &width, int32_t &height) const override;
61 void getCursorPos(int32_t &x, int32_t &y) const override;
62
68 virtual bool getThisCursorPos(LPPOINT pos);
75 virtual bool setThisCursorPos(int x, int y);
76
81 virtual void updateDesktopThread();
82
83 // IPrimaryScreen overrides
84 void reconfigure(uint32_t activeSides) override;
85 uint32_t activeSides() override;
86 void warpCursor(int32_t x, int32_t y) override;
87 uint32_t registerHotKey(KeyID key, KeyModifierMask mask) override;
88 void unregisterHotKey(uint32_t id) override;
89 void fakeInputBegin() override;
90 void fakeInputEnd() override;
91 int32_t getJumpZoneSize() const override;
92 bool isAnyMouseButtonDown(uint32_t &buttonID) const override;
93 void getCursorCenter(int32_t &x, int32_t &y) const override;
94
95 // ISecondaryScreen overrides
96 void fakeMouseButton(ButtonID id, bool press) override;
97 void fakeMouseMove(int32_t x, int32_t y) override;
98 void fakeMouseRelativeMove(int32_t dx, int32_t dy) const override;
99 void fakeMouseWheel(ScrollDelta delta) const override;
100
101 // IKeyState overrides
102 virtual void updateKeys();
103 void fakeKeyDown(KeyID id, KeyModifierMask mask, KeyButton button, const std::string &lang) override;
104 bool fakeKeyRepeat(KeyID id, KeyModifierMask mask, int32_t count, KeyButton button, const std::string &lang) override;
105 bool fakeKeyUp(KeyButton button) override;
106 void fakeAllKeysUp() override;
107
108 // IPlatformScreen overrides
109 void enable() override;
110 void disable() override;
111 void enter() override;
112 bool canLeave() override;
113 void leave() override;
114 bool setClipboard(ClipboardID, const IClipboard *) override;
115 void checkClipboards() override;
116 void openScreensaver(bool notify) override;
117 void closeScreensaver() override;
118 void screensaver(bool activate) override;
119 void resetOptions() override;
120 void setOptions(const OptionsList &options) override;
121 void setSequenceNumber(uint32_t) override;
122 bool isPrimary() const override;
123 std::string getSecureInputApp() const override;
124
125protected:
126 // IPlatformScreen overrides
127 void handleSystemEvent(const Event &event) override;
128 void updateButtons() override;
129 IKeyState *getKeyState() const override;
130
131 // simulate a local key to the system directly
132 void fakeLocalKey(KeyButton button, bool press) const;
133
134private:
135 // initialization and shutdown operations
136 HCURSOR createBlankCursor() const;
137 void destroyCursor(HCURSOR cursor) const;
138 ATOM createWindowClass() const;
139 ATOM createDeskWindowClass(bool isPrimary) const;
140 void destroyClass(ATOM windowClass) const;
141 HWND createWindow(ATOM windowClass, const wchar_t *name) const;
142 void destroyWindow(HWND) const;
143
144 // convenience function to send events
145public: // HACK
146 void sendEvent(EventTypes type, void * = nullptr);
147
148private: // HACK
149 void sendClipboardEvent(EventTypes type, ClipboardID id);
150
151 // handle message before it gets dispatched. returns true iff
152 // the message should not be dispatched.
153 bool onPreDispatch(HWND, UINT, WPARAM, LPARAM);
154
155 // handle message before it gets dispatched. returns true iff
156 // the message should not be dispatched.
157 bool onPreDispatchPrimary(HWND, UINT, WPARAM, LPARAM);
158
159 // handle secondary message before it gets dispatched. returns true iff
160 // the message should not be dispatched.
161 bool onPreDispatchSecondary(HWND, UINT, WPARAM, LPARAM);
162
163 // handle message. returns true iff handled and optionally sets
164 // \c *result (which defaults to 0).
165 bool onEvent(HWND, UINT, WPARAM, LPARAM, LRESULT *result);
166
167 // message handlers
168 bool onMark(uint32_t mark);
169 bool onKey(WPARAM, LPARAM);
170 bool onHotKey(WPARAM, LPARAM);
171 bool onMouseButton(WPARAM, LPARAM);
172 bool onMouseMove(int32_t x, int32_t y);
173 bool onMouseWheel(int32_t xDelta, int32_t yDelta);
174 bool onScreensaver(bool activated);
175 bool onDisplayChange();
176 void onClipboardChange();
177
178 // warp cursor without discarding queued events
179 void warpCursorNoFlush(int32_t x, int32_t y);
180
181 // discard posted messages
182 void nextMark();
183
184 // test if event should be ignored
185 bool ignore() const;
186
187 // update screen size cache
188 void updateScreenShape();
189
190 // fix timer callback
191 void handleFixes();
192
193 // fix the clipboard viewer chain
194 void fixClipboardViewer();
195
196 // enable/disable special key combinations so we can catch/pass them
197 void enableSpecialKeys(bool) const;
198
199 // map a button event to a button ID
200 ButtonID mapButtonFromEvent(WPARAM msg, LPARAM button) const;
201
202 // map a button event to a press (true) or release (false)
203 bool mapPressFromEvent(WPARAM msg, LPARAM button) const;
204
205 // job to update the key state
206 void updateKeysCB(const void *);
207
208 // determine whether the mouse is hidden by the system.
209 // if true and on secondary screen, enable mouse keys to show the cursor.
210 // we were previously restoring the old mouse key settings when not needed, but this was causing
211 // issues where the mouse cursor becomes permanently hidden, even if there is a real mouse
212 // attached to the system; this could be a windows bug, but losing your mouse is a nightmare
213 // so we shouldn't risk doing that.
214 void setupMouseKeys();
215
216 // enables the mouse keys accessibility feature to to ensure the
217 // mouse cursor can be shown.
218 void updateMouseKeys();
219
220 // our window proc
221 static LRESULT CALLBACK wndProc(HWND, UINT, WPARAM, LPARAM);
222
223 // save last position of mouse to compute next delta movement
224 void saveMousePosition(int32_t x, int32_t y);
225
226 // check if it is a modifier key repeating message
227 bool isModifierRepeat(KeyModifierMask oldState, KeyModifierMask state, WPARAM wParam) const;
228
229private:
230 struct HotKeyItem
231 {
232 public:
233 HotKeyItem(UINT vk, UINT modifiers);
234
235 UINT getVirtualKey() const;
236
237 bool operator<(const HotKeyItem &) const;
238
239 private:
240 UINT m_keycode;
241 UINT m_mask;
242 };
243 using HotKeyMap = std::map<uint32_t, HotKeyItem>;
244 using HotKeyIDList = std::vector<uint32_t>;
245 using HotKeyToIDMap = std::map<HotKeyItem, uint32_t>;
246 using PrimaryKeyDownList = std::vector<KeyButton>;
247
248 static HINSTANCE s_windowInstance;
249
250 // true if screen is being used as a primary screen, false otherwise
251 bool m_isPrimary;
252
253 // true if hooks are not to be installed (useful for debugging)
254 bool m_useHooks;
255
256 // true if mouse has entered the screen
257 bool m_isOnScreen;
258
259 // true if the screen is enabled
260 bool m_isEnabled = false;
261
262 // our resources
263 ATOM m_class = 0;
264
265 // screen shape stuff
266 int32_t m_x = 0;
267 int32_t m_y = 0;
268 int32_t m_w = 0;
269 int32_t m_h = 0;
270 int32_t m_xCenter = 0;
271 int32_t m_yCenter = 0;
272
273 // true if system appears to have multiple monitors
274 bool m_multimon = false;
275
276 // last mouse position
277 int32_t m_xCursor = 0;
278 int32_t m_yCursor = 0;
279
280 // last clipboard
281 uint32_t m_sequenceNumber = 0;
282
283 // used to discard queued messages that are no longer needed
284 uint32_t m_mark = 0;
285 uint32_t m_markReceived = 0;
286
287 // the main loop's thread id
288 DWORD m_threadID;
289
290 // timer for periodically checking stuff that requires polling
291 EventQueueTimer *m_fixTimer = nullptr;
292
293 // the keyboard layout to use when off primary screen
294 HKL m_keyLayout = nullptr;
295
296 // screen saver stuff
297 MSWindowsScreenSaver *m_screensaver = nullptr;
298 bool m_screensaverNotify = false;
299 bool m_screensaverActive = false;
300
301 // clipboard stuff. our window is used mainly as a clipboard
302 // owner and as a link in the clipboard viewer chain.
303 HWND m_window = nullptr;
304 DWORD m_clipboardSequenceNumber = 0;
305 bool m_ownClipboard = false;
306
307 // one desk per desktop and a cond var to communicate with it
308 MSWindowsDesks *m_desks = nullptr;
309
310 // keyboard stuff
311 MSWindowsKeyState *m_keyState = nullptr;
312
313 // hot key stuff
314 HotKeyMap m_hotKeys;
315 HotKeyIDList m_oldHotKeyIDs;
316 HotKeyToIDMap m_hotKeyToIDMap;
317
318 // map of button state
319 bool m_buttons[NumButtonIDs];
320
321 // m_hasMouse is true if there's a mouse attached to the system or
322 // mouse keys is simulating one. we track this so we can force the
323 // cursor to be displayed when the user has entered this screen.
324 bool m_hasMouse;
325
326 bool m_gotMouseKeys = false;
327 MOUSEKEYS m_mouseKeys;
328
329 MSWindowsHook m_hook;
330
331 static MSWindowsScreen *s_screen;
332
333 IEventQueue *m_events;
334
335 std::string m_desktopPath;
336
337 PrimaryKeyDownList m_primaryKeyDownList;
338 MSWindowsPowerManager m_powerManager;
339};
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
Coordinate ScrollDelta
Definition Coordinate.h:21
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 uint8_t NumButtonIDs
Definition MouseTypes.h:37
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:704
MSWindowsScreen(bool isPrimary, bool useHooks, IEventQueue *events, bool enableLangSync=false)
Definition MSWindowsScreen.cpp:82
void unregisterHotKey(uint32_t id) override
Unregister a system hotkey.
Definition MSWindowsScreen.cpp:616
uint32_t registerHotKey(KeyID key, KeyModifierMask mask) override
Register a system hotkey.
Definition MSWindowsScreen.cpp:539
static HINSTANCE getWindowInstance()
Get instance.
Definition MSWindowsScreen.cpp:168
void warpCursor(int32_t x, int32_t y) override
Warp cursor.
Definition MSWindowsScreen.cpp:516
bool canLeave() override
Leave screen.
Definition MSWindowsScreen.cpp:265
void getShape(int32_t &x, int32_t &y, int32_t &width, int32_t &height) const override
Get screen shape.
Definition MSWindowsScreen.cpp:426
void leave() override
Leave screen.
Definition MSWindowsScreen.cpp:278
void handleSystemEvent(const Event &event) override
Handle system event.
Definition MSWindowsScreen.cpp:834
void enter() override
Enter screen.
Definition MSWindowsScreen.cpp:235
void setSequenceNumber(uint32_t) override
Set clipboard sequence number.
Definition MSWindowsScreen.cpp:404
~MSWindowsScreen() override
Definition MSWindowsScreen.cpp:141
bool isAnyMouseButtonDown(uint32_t &buttonID) const override
Test if mouse is pressed.
Definition MSWindowsScreen.cpp:668
void enable() override
Enable screen.
Definition MSWindowsScreen.cpp:173
void fakeInputBegin() override
Prepare to synthesize input on primary screen.
Definition MSWindowsScreen.cpp:643
static void init(HINSTANCE)
Initialize.
Definition MSWindowsScreen.cpp:159
void * getEventTarget() const override
Get event target.
Definition MSWindowsScreen.cpp:414
bool fakeKeyRepeat(KeyID id, KeyModifierMask mask, int32_t count, KeyButton button, const std::string &lang) override
Fake a key repeat.
Definition MSWindowsScreen.cpp:726
void updateButtons() override
Update mouse buttons.
Definition MSWindowsScreen.cpp:846
void getCursorPos(int32_t &x, int32_t &y) const override
Get cursor position.
Definition MSWindowsScreen.cpp:436
void sendEvent(EventTypes type, void *=nullptr)
Definition MSWindowsScreen.cpp:817
void closeScreensaver() override
Close screen saver.
Definition MSWindowsScreen.cpp:369
virtual void updateKeys()
Definition MSWindowsScreen.cpp:715
void screensaver(bool activate) override
Activate/deactivate screen saver.
Definition MSWindowsScreen.cpp:381
IKeyState * getKeyState() const override
Get the key state.
Definition MSWindowsScreen.cpp:857
void getCursorCenter(int32_t &x, int32_t &y) const override
Get cursor center position.
Definition MSWindowsScreen.cpp:684
void openScreensaver(bool notify) override
Open screen saver.
Definition MSWindowsScreen.cpp:357
void fakeMouseMove(int32_t x, int32_t y) override
Fake mouse move.
Definition MSWindowsScreen.cpp:699
bool setClipboard(ClipboardID, const IClipboard *) override
Set clipboard.
Definition MSWindowsScreen.cpp:319
void resetOptions() override
Notify of options changes.
Definition MSWindowsScreen.cpp:394
std::string getSecureInputApp() const override
Determine the name of the app causing a secure input state.
Definition MSWindowsScreen.cpp:1709
void fakeInputEnd() override
Done synthesizing input on primary screen.
Definition MSWindowsScreen.cpp:653
void setOptions(const OptionsList &options) override
Notify of options changes.
Definition MSWindowsScreen.cpp:399
void fakeMouseWheel(ScrollDelta delta) const override
Synthesize a mouse wheel event of amount This Implmentation for this method should call applyScrollMo...
Definition MSWindowsScreen.cpp:709
void fakeAllKeysUp() override
Fake key releases for all fake pressed keys.
Definition MSWindowsScreen.cpp:742
void checkClipboards() override
Check clipboard owner.
Definition MSWindowsScreen.cpp:336
virtual bool setThisCursorPos(int x, int y)
Sets the cursor position on the current machine.
Definition MSWindowsScreen.cpp:466
bool isPrimary() const override
Test if is primary screen.
Definition MSWindowsScreen.cpp:409
int32_t getJumpZoneSize() const override
Get jump zone size.
Definition MSWindowsScreen.cpp:663
void disable() override
Disable screen.
Definition MSWindowsScreen.cpp:201
virtual bool getThisCursorPos(LPPOINT pos)
Get the position of the cursor on the current machine.
Definition MSWindowsScreen.cpp:448
void fakeMouseButton(ButtonID id, bool press) override
Fake mouse press/release.
Definition MSWindowsScreen.cpp:690
bool getClipboard(ClipboardID id, IClipboard *) const override
Get clipboard.
Definition MSWindowsScreen.cpp:419
void fakeKeyDown(KeyID id, KeyModifierMask mask, KeyButton button, const std::string &lang) override
Fake a key press.
Definition MSWindowsScreen.cpp:720
void fakeLocalKey(KeyButton button, bool press) const
Definition MSWindowsScreen.cpp:1678
void reconfigure(uint32_t activeSides) override
Update configuration.
Definition MSWindowsScreen.cpp:503
bool fakeKeyUp(KeyButton button) override
Fake a key release.
Definition MSWindowsScreen.cpp:735
uint32_t activeSides() override
activeSides
Definition MSWindowsScreen.cpp:511
virtual void updateDesktopThread()
This function will attempt to switch to the current desktop the mouse is located on.
Definition MSWindowsScreen.cpp:484
PlatformScreen(IEventQueue *events)
Definition PlatformScreen.cpp:13
Thread handle.
Definition Thread.h:33
EventTypes
Definition EventTypes.h:13