Deskflow 1.22.0.197
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
11#include "deskflow/ClientArgs.h"
15
16#include <map>
17#include <string>
18
19#define WIN32_LEAN_AND_MEAN
20#include <Windows.h>
21
22class EventQueueTimer;
23class MSWindowsDesks;
26class Thread;
27class MSWindowsDropTarget;
28
31{
32public:
34 bool isPrimary, bool noHooks, IEventQueue *events, bool enableLangSync = false,
36 );
37 ~MSWindowsScreen() override;
38
40
41
43
47 static void init(HINSTANCE);
48
50
52
54
57 static HINSTANCE getWindowInstance();
58
60
61 // IScreen overrides
62 void *getEventTarget() const override;
63 bool getClipboard(ClipboardID id, IClipboard *) const override;
64 void getShape(int32_t &x, int32_t &y, int32_t &width, int32_t &height) const override;
65 void getCursorPos(int32_t &x, int32_t &y) const override;
66
72 virtual bool getThisCursorPos(LPPOINT pos);
79 virtual bool setThisCursorPos(int x, int y);
80
85 virtual void updateDesktopThread();
86
87 // IPrimaryScreen overrides
88 void reconfigure(uint32_t activeSides) override;
89 uint32_t activeSides() override;
90 void warpCursor(int32_t x, int32_t y) override;
91 uint32_t registerHotKey(KeyID key, KeyModifierMask mask) override;
92 void unregisterHotKey(uint32_t id) override;
93 void fakeInputBegin() override;
94 void fakeInputEnd() override;
95 int32_t getJumpZoneSize() const override;
96 bool isAnyMouseButtonDown(uint32_t &buttonID) const override;
97 void getCursorCenter(int32_t &x, int32_t &y) const override;
98
99 // ISecondaryScreen overrides
100 void fakeMouseButton(ButtonID id, bool press) override;
101 void fakeMouseMove(int32_t x, int32_t y) override;
102 void fakeMouseRelativeMove(int32_t dx, int32_t dy) const override;
103 void fakeMouseWheel(int32_t xDelta, int32_t yDelta) const override;
104
105 // IKeyState overrides
106 virtual void updateKeys();
107 void fakeKeyDown(KeyID id, KeyModifierMask mask, KeyButton button, const std::string &lang) override;
108 bool fakeKeyRepeat(KeyID id, KeyModifierMask mask, int32_t count, KeyButton button, const std::string &lang) override;
109 bool fakeKeyUp(KeyButton button) override;
110 void fakeAllKeysUp() override;
111
112 // IPlatformScreen overrides
113 void enable() override;
114 void disable() override;
115 void enter() override;
116 bool canLeave() override;
117 void leave() override;
118 bool setClipboard(ClipboardID, const IClipboard *) override;
119 void checkClipboards() override;
120 void openScreensaver(bool notify) override;
121 void closeScreensaver() override;
122 void screensaver(bool activate) override;
123 void resetOptions() override;
124 void setOptions(const OptionsList &options) override;
125 void setSequenceNumber(uint32_t) override;
126 bool isPrimary() const override;
127 std::string getSecureInputApp() const override;
128
129protected:
130 // IPlatformScreen overrides
131 void handleSystemEvent(const Event &event) override;
132 void updateButtons() override;
133 IKeyState *getKeyState() const override;
134
135 // simulate a local key to the system directly
136 void fakeLocalKey(KeyButton button, bool press) const;
137
138private:
139 // initialization and shutdown operations
140 HCURSOR createBlankCursor() const;
141 void destroyCursor(HCURSOR cursor) const;
142 ATOM createWindowClass() const;
143 ATOM createDeskWindowClass(bool isPrimary) const;
144 void destroyClass(ATOM windowClass) const;
145 HWND createWindow(ATOM windowClass, const char *name) const;
146 void destroyWindow(HWND) const;
147
148 // convenience function to send events
149public: // HACK
150 void sendEvent(EventTypes type, void * = nullptr);
151
152private: // HACK
153 void sendClipboardEvent(EventTypes type, ClipboardID id);
154
155 // handle message before it gets dispatched. returns true iff
156 // the message should not be dispatched.
157 bool onPreDispatch(HWND, UINT, WPARAM, LPARAM);
158
159 // handle message before it gets dispatched. returns true iff
160 // the message should not be dispatched.
161 bool onPreDispatchPrimary(HWND, UINT, WPARAM, LPARAM);
162
163 // handle secondary message before it gets dispatched. returns true iff
164 // the message should not be dispatched.
165 bool onPreDispatchSecondary(HWND, UINT, WPARAM, LPARAM);
166
167 // handle message. returns true iff handled and optionally sets
168 // \c *result (which defaults to 0).
169 bool onEvent(HWND, UINT, WPARAM, LPARAM, LRESULT *result);
170
171 // message handlers
172 bool onMark(uint32_t mark);
173 bool onKey(WPARAM, LPARAM);
174 bool onHotKey(WPARAM, LPARAM);
175 bool onMouseButton(WPARAM, LPARAM);
176 bool onMouseMove(int32_t x, int32_t y);
177 bool onMouseWheel(int32_t xDelta, int32_t yDelta);
178 bool onScreensaver(bool activated);
179 bool onDisplayChange();
180 void onClipboardChange();
181
182 // warp cursor without discarding queued events
183 void warpCursorNoFlush(int32_t x, int32_t y);
184
185 // discard posted messages
186 void nextMark();
187
188 // test if event should be ignored
189 bool ignore() const;
190
191 // update screen size cache
192 void updateScreenShape();
193
194 // fix timer callback
195 void handleFixes();
196
197 // fix the clipboard viewer chain
198 void fixClipboardViewer();
199
200 // enable/disable special key combinations so we can catch/pass them
201 void enableSpecialKeys(bool) const;
202
203 // map a button event to a button ID
204 ButtonID mapButtonFromEvent(WPARAM msg, LPARAM button) const;
205
206 // map a button event to a press (true) or release (false)
207 bool mapPressFromEvent(WPARAM msg, LPARAM button) const;
208
209 // job to update the key state
210 void updateKeysCB(void *);
211
212 // determine whether the mouse is hidden by the system and force
213 // it to be displayed if user has entered this secondary screen.
214 void forceShowCursor();
215
216 // forceShowCursor uses MouseKeys to show the cursor. since we
217 // don't actually want MouseKeys behavior we have to make sure
218 // it applies when NumLock is in whatever state it's not in now.
219 // this method does that.
220 void updateForceShowCursor();
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_noHooks;
257
258 // true if mouse has entered the screen
259 bool m_isOnScreen;
260
261 // our resources
262 ATOM m_class = 0;
263
264 // screen shape stuff
265 int32_t m_x = 0;
266 int32_t m_y = 0;
267 int32_t m_w = 0;
268 int32_t m_h = 0;
269 int32_t m_xCenter = 0;
270 int32_t m_yCenter = 0;
271
272 // true if system appears to have multiple monitors
273 bool m_multimon = false;
274
275 // last mouse position
276 int32_t m_xCursor = 0;
277 int32_t m_yCursor = 0;
278
279 // last clipboard
280 uint32_t m_sequenceNumber = 0;
281
282 // used to discard queued messages that are no longer needed
283 uint32_t m_mark = 0;
284 uint32_t m_markReceived = 0;
285
286 // the main loop's thread id
287 DWORD m_threadID;
288
289 // timer for periodically checking stuff that requires polling
290 EventQueueTimer *m_fixTimer = nullptr;
291
292 // the keyboard layout to use when off primary screen
293 HKL m_keyLayout = nullptr;
294
295 // screen saver stuff
296 MSWindowsScreenSaver *m_screensaver = nullptr;
297 bool m_screensaverNotify = false;
298 bool m_screensaverActive = false;
299
300 // clipboard stuff. our window is used mainly as a clipboard
301 // owner and as a link in the clipboard viewer chain.
302 HWND m_window = nullptr;
303 DWORD m_clipboardSequenceNumber = 0;
304 bool m_ownClipboard = false;
305
306 // one desk per desktop and a cond var to communicate with it
307 MSWindowsDesks *m_desks = nullptr;
308
309 // keyboard stuff
310 MSWindowsKeyState *m_keyState = nullptr;
311
312 // hot key stuff
313 HotKeyMap m_hotKeys;
314 HotKeyIDList m_oldHotKeyIDs;
315 HotKeyToIDMap m_hotKeyToIDMap;
316
317 // map of button state
318 bool m_buttons[1 + kButtonExtra0 + 1];
319
320 // the system shows the mouse cursor when an internal display count
321 // is >= 0. this count is maintained per application but there's
322 // apparently a system wide count added to the application's count.
323 // this system count is 0 if there's a mouse attached to the system
324 // and -1 otherwise. the MouseKeys accessibility feature can modify
325 // this system count by making the system appear to have a mouse.
326 //
327 // m_hasMouse is true iff there's a mouse attached to the system or
328 // MouseKeys is simulating one. we track this so we can force the
329 // cursor to be displayed when the user has entered this screen.
330 // m_showingMouse is true when we're doing that.
331 bool m_hasMouse;
332 bool m_showingMouse = false;
333 bool m_gotOldMouseKeys;
334 MOUSEKEYS m_mouseKeys;
335 MOUSEKEYS m_oldMouseKeys;
336
337 MSWindowsHook m_hook;
338
339 static MSWindowsScreen *s_screen;
340
341 IEventQueue *m_events;
342
343 std::string m_desktopPath;
344
345 PrimaryKeyDownList m_primaryKeyDownList;
346 MSWindowsPowerManager m_powerManager;
347};
static const struct sockaddr FAR * name
Definition ArchNetworkWinsock.cpp:27
static const WSAEVENT FAR DWORD
Definition ArchNetworkWinsock.cpp:54
static int type
Definition ArchNetworkWinsock.cpp:44
uint8_t ClipboardID
Clipboard ID.
Definition ClipboardTypes.h:16
int key
Definition KeySequence.cpp:15
uint32_t KeyID
Key ID.
Definition KeyTypes.h:22
uint16_t KeyButton
Key Code.
Definition KeyTypes.h:35
uint32_t KeyModifierMask
Modifier key mask.
Definition KeyTypes.h:45
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:27
int y
Definition ServerConfig.cpp:27
int x
Definition ServerConfig.cpp:26
Definition SimpleEventQueueBuffer.cpp:13
Event.
Definition Event.h:27
Clipboard interface.
Definition IClipboard.h:20
Event queue interface.
Definition IEventQueue.h:32
IKeyState(const IEventQueue *events)
Definition IKeyState.cpp:18
Microsoft Windows desk handling.
Definition MSWindowsDesks.h:47
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
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:538
static HINSTANCE getWindowInstance()
Get instance.
Definition MSWindowsScreen.cpp:169
void warpCursor(int32_t x, int32_t y) override
Warp cursor.
Definition MSWindowsScreen.cpp:515
bool canLeave() override
Leave screen.
Definition MSWindowsScreen.cpp:261
void getShape(int32_t &x, int32_t &y, int32_t &width, int32_t &height) const override
Get screen shape.
Definition MSWindowsScreen.cpp:425
void leave() override
Leave screen.
Definition MSWindowsScreen.cpp:274
void handleSystemEvent(const Event &event) override
Handle system event.
Definition MSWindowsScreen.cpp:835
void enter() override
Enter screen.
Definition MSWindowsScreen.cpp:231
void setSequenceNumber(uint32_t) override
Set clipboard sequence number.
Definition MSWindowsScreen.cpp:403
~MSWindowsScreen() override
Definition MSWindowsScreen.cpp:142
bool isAnyMouseButtonDown(uint32_t &buttonID) const override
Test if mouse is pressed.
Definition MSWindowsScreen.cpp:668
void enable() override
Enable screen.
Definition MSWindowsScreen.cpp:174
void fakeInputBegin() override
Prepare to synthesize input on primary screen.
Definition MSWindowsScreen.cpp:643
static void init(HINSTANCE)
Initialize.
Definition MSWindowsScreen.cpp:160
void * getEventTarget() const override
Get event target.
Definition MSWindowsScreen.cpp:413
bool fakeKeyRepeat(KeyID id, KeyModifierMask mask, int32_t count, KeyButton button, const std::string &lang) override
Fake a key repeat.
Definition MSWindowsScreen.cpp:727
void updateButtons() override
Update mouse buttons.
Definition MSWindowsScreen.cpp:847
void getCursorPos(int32_t &x, int32_t &y) const override
Get cursor position.
Definition MSWindowsScreen.cpp:435
void sendEvent(EventTypes type, void *=nullptr)
Definition MSWindowsScreen.cpp:818
void closeScreensaver() override
Close screen saver.
Definition MSWindowsScreen.cpp:368
virtual void updateKeys()
Definition MSWindowsScreen.cpp:716
void screensaver(bool activate) override
Activate/deactivate screen saver.
Definition MSWindowsScreen.cpp:380
IKeyState * getKeyState() const override
Get the key state.
Definition MSWindowsScreen.cpp:858
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:356
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:318
void resetOptions() override
Notify of options changes.
Definition MSWindowsScreen.cpp:393
MSWindowsScreen(bool isPrimary, bool noHooks, IEventQueue *events, bool enableLangSync=false, deskflow::ClientScrollDirection scrollDirection=deskflow::ClientScrollDirection::Normal)
Definition MSWindowsScreen.cpp:82
std::string getSecureInputApp() const override
Determine the name of the app causing a secure input state.
Definition MSWindowsScreen.cpp:1674
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:398
void fakeMouseWheel(int32_t xDelta, int32_t yDelta) const override
Fake mouse wheel.
Definition MSWindowsScreen.cpp:709
void fakeAllKeysUp() override
Fake key releases for all fake pressed keys.
Definition MSWindowsScreen.cpp:743
void checkClipboards() override
Check clipboard owner.
Definition MSWindowsScreen.cpp:335
virtual bool setThisCursorPos(int x, int y)
Sets the cursor position on the current machine.
Definition MSWindowsScreen.cpp:465
bool isPrimary() const override
Test if is primary screen.
Definition MSWindowsScreen.cpp:408
int32_t getJumpZoneSize() const override
Get jump zone size.
Definition MSWindowsScreen.cpp:663
void disable() override
Disable screen.
Definition MSWindowsScreen.cpp:199
virtual bool getThisCursorPos(LPPOINT pos)
Get the position of the cursor on the current machine.
Definition MSWindowsScreen.cpp:447
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:418
void fakeKeyDown(KeyID id, KeyModifierMask mask, KeyButton button, const std::string &lang) override
Fake a key press.
Definition MSWindowsScreen.cpp:721
void fakeLocalKey(KeyButton button, bool press) const
Definition MSWindowsScreen.cpp:1643
void reconfigure(uint32_t activeSides) override
Update configuration.
Definition MSWindowsScreen.cpp:502
bool fakeKeyUp(KeyButton button) override
Fake a key release.
Definition MSWindowsScreen.cpp:736
uint32_t activeSides() override
activeSides
Definition MSWindowsScreen.cpp:510
virtual void updateDesktopThread()
This function will attempt to switch to the current desktop the mouse is located on.
Definition MSWindowsScreen.cpp:483
PlatformScreen(IEventQueue *events, deskflow::ClientScrollDirection scrollDirection=deskflow::ClientScrollDirection::Normal)
Definition PlatformScreen.cpp:12
Thread handle.
Definition Thread.h:33
ClientScrollDirection
Definition ClientArgs.h:13
@ Normal
Same direction as the server.
Definition ClientArgs.h:14
EventTypes
Definition EventTypes.h:13