Deskflow 1.26.0.314
Keyboard and mouse sharing utility
Loading...
Searching...
No Matches
MSWindowsDesks.h
Go to the documentation of this file.
1/*
2 * Deskflow -- mouse and keyboard sharing utility
3 * SPDX-FileCopyrightText: (C) 2025 - 2026 Deskflow Developers
4 * SPDX-FileCopyrightText: (C) 2012 - 2016 Synergy App Ltd
5 * SPDX-FileCopyrightText: (C) 2004 Chris Schoeneman
6 * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
7 */
8
9#pragma once
10
11#include "deskflow/MouseTypes.h"
13#include "mt/CondVar.h"
14#include "mt/Mutex.h"
15
16#include <map>
17#include <string>
18
19#define WIN32_LEAN_AND_MEAN
20#include <Windows.h>
21
22class Event;
23class EventQueueTimer;
24class Thread;
25class IJob;
26class IScreenSaver;
27class IEventQueue;
28
30
46{
47public:
49
57 MSWindowsDesks(bool isPrimary, bool useHooks, const IScreenSaver *screensaver, IEventQueue *events, IJob *updateKeys);
59
61
62
64
70 void enable();
71
73
76 void disable();
77
79
82 void enter();
83
85
88 void leave(HKL keyLayout);
89
91
94 void resetOptions();
95
97
101 void setOptions(const OptionsList &options);
102
104
108 void updateKeys();
109
111
114 void setShape(int32_t x, int32_t y, int32_t width, int32_t height, int32_t xCenter, int32_t yCenter, bool isMultimon);
115
117
122 void installScreensaverHooks(bool install);
123
125
128 void fakeInputBegin();
129
131
134 void fakeInputEnd();
135
137
139
141
144 void getCursorPos(int32_t &x, int32_t &y) const;
145
147
150 void fakeKeyEvent(WORD virtualKey, WORD scanCode, DWORD flags, bool isAutoRepeat) const;
151
153
156 void fakeMouseButton(ButtonID id, bool press);
157
159
162 void fakeMouseMove(int32_t x, int32_t y) const;
163
165
168 void fakeMouseRelativeMove(int32_t dx, int32_t dy) const;
169
171
174 void fakeMouseWheel(int32_t xDelta, int32_t yDelta) const;
175
177
178private:
179 class Desk
180 {
181 public:
182 std::wstring m_name;
183 Thread *m_thread;
184 DWORD m_threadID;
185 DWORD m_targetID;
186 HDESK m_desk;
187 HWND m_window;
188 HWND m_foregroundWindow;
189 bool m_lowLevel;
190 bool m_hasRelativeRestorePosition = false;
191 int32_t m_relativeRestoreX = 0;
192 int32_t m_relativeRestoreY = 0;
193 };
194 using Desks = std::map<std::wstring, Desk *>;
195
196 // initialization and shutdown operations
197 HCURSOR createBlankCursor() const;
198 void destroyCursor(HCURSOR cursor) const;
199 ATOM createDeskWindowClass(bool isPrimary) const;
200 void destroyClass(ATOM windowClass) const;
201 HWND createWindow(ATOM windowClass, const wchar_t *name) const;
202 void destroyWindow(HWND) const;
203
204 // message handlers
205 void deskMouseMove(int32_t x, int32_t y) const;
206 void deskMouseRelativeMove(int32_t dx, int32_t dy) const;
207 void saveRelativeRestorePosition(Desk *desk) const;
208 bool restoreRelativeCursorPosition(Desk *desk) const;
209 void deskEnter(Desk *desk);
210 void deskLeave(Desk *desk, HKL keyLayout);
211 void deskThread(const void *vdesk);
212
213 // desk switch checking and handling
214 Desk *addDesk(const std::wstring &name, HDESK hdesk);
215 void removeDesks();
216 void checkDesk();
217 bool isDeskAccessible(const Desk *desk) const;
218 void handleCheckDesk();
219
220 // communication with desk threads
221 void waitForDesk() const;
222 void sendMessage(UINT, WPARAM, LPARAM) const;
223
224 // work around for messed up keyboard events from low-level hooks
225 HWND getForegroundWindow() const;
226
227 // desk API wrappers
228 HDESK openInputDesktop();
229 void closeDesktop(HDESK);
230 std::wstring getDesktopName(HDESK);
231
232 // our desk window procs
233 static LRESULT CALLBACK primaryDeskProc(HWND, UINT, WPARAM, LPARAM);
234 static LRESULT CALLBACK secondaryDeskProc(HWND, UINT, WPARAM, LPARAM);
235
236private:
237 // true if screen is being used as a primary screen, false otherwise
238 bool m_isPrimary;
239
240 // true if hooks are to be installed (when debugging, it can be useful to disable hooks)
241 bool m_useHooks;
242
243 // true if mouse has entered the screen
244 bool m_isOnScreen;
245
246 // our resources
247 ATOM m_deskClass;
248 HCURSOR m_cursor;
249
250 // screen shape stuff
251 int32_t m_x = 0;
252 int32_t m_y = 9;
253 int32_t m_w = 0;
254 int32_t m_h = 0;
255 int32_t m_xCenter = 0;
256 int32_t m_yCenter = 0;
257
258 // true if system appears to have multiple monitors
259 bool m_multimon = false;
260
261 // the timer used to check for desktop switching
262 EventQueueTimer *m_timer = nullptr;
263
264 // screen saver stuff
265 DWORD m_threadID;
266 const IScreenSaver *m_screensaver;
267 bool m_screensaverNotify = false;
268
269 // the current desk and it's name
270 Desk *m_activeDesk = nullptr;
271 std::wstring m_activeDeskName;
272
273 // one desk per desktop and a cond var to communicate with it
274 Mutex m_mutex;
275 CondVar<bool> m_deskReady;
276 Desks m_desks;
277
278 // keyboard stuff
279 IJob *m_updateKeys;
280 HKL m_keyLayout;
281
282 // options
283 bool m_leaveForegroundOption;
284 bool m_relativeMouseMoves = false;
285
286 IEventQueue *m_events;
287};
static const struct sockaddr FAR * name
Definition ArchNetworkWinsock.cpp:28
static const WSAEVENT FAR DWORD
Definition ArchNetworkWinsock.cpp:55
static void FAR int int flags
Definition ArchNetworkWinsock.cpp:38
uint8_t ButtonID
Mouse button ID.
Definition MouseTypes.h:16
std::vector< uint32_t > OptionsList
Definition OptionTypes.h:26
int y
Definition ServerConfig.cpp:24
int x
Definition ServerConfig.cpp:23
Condition variable.
Definition CondVar.h:121
Definition EventQueueTimer.h:12
Event.
Definition Event.h:43
Event queue interface.
Definition IEventQueue.h:29
Job interface.
Definition IJob.h:16
Screen saver interface.
Definition IScreenSaver.h:16
void enter()
Notify of entering a desk.
Definition MSWindowsDesks.cpp:171
void fakeInputEnd()
Stop ignoring user input.
Definition MSWindowsDesks.cpp:233
void fakeMouseWheel(int32_t xDelta, int32_t yDelta) const
Fake mouse wheel.
Definition MSWindowsDesks.cpp:313
MSWindowsDesks(bool isPrimary, bool useHooks, const IScreenSaver *screensaver, IEventQueue *events, IJob *updateKeys)
Constructor.
Definition MSWindowsDesks.cpp:113
void leave(HKL keyLayout)
Notify of leaving a desk.
Definition MSWindowsDesks.cpp:176
void disable()
Disable desk tracking.
Definition MSWindowsDesks.cpp:156
void fakeMouseButton(ButtonID id, bool press)
Fake mouse press/release.
Definition MSWindowsDesks.cpp:251
void fakeInputBegin()
Start ignoring user input.
Definition MSWindowsDesks.cpp:228
void setOptions(const OptionsList &options)
Notify of options changes.
Definition MSWindowsDesks.cpp:190
void enable()
Enable desk tracking.
Definition MSWindowsDesks.cpp:139
void updateKeys()
Update the key state.
Definition MSWindowsDesks.cpp:202
void resetOptions()
Notify of options changes.
Definition MSWindowsDesks.cpp:181
void fakeMouseRelativeMove(int32_t dx, int32_t dy) const
Fake mouse move.
Definition MSWindowsDesks.cpp:308
void fakeMouseMove(int32_t x, int32_t y) const
Fake mouse move.
Definition MSWindowsDesks.cpp:303
void fakeKeyEvent(WORD virtualKey, WORD scanCode, DWORD flags, bool isAutoRepeat) const
Fake key press/release.
Definition MSWindowsDesks.cpp:246
void installScreensaverHooks(bool install)
Install/uninstall screensaver hooks.
Definition MSWindowsDesks.cpp:220
~MSWindowsDesks()
Definition MSWindowsDesks.cpp:131
void setShape(int32_t x, int32_t y, int32_t width, int32_t height, int32_t xCenter, int32_t yCenter, bool isMultimon)
Tell desk about new size.
Definition MSWindowsDesks.cpp:207
void getCursorPos(int32_t &x, int32_t &y) const
Get cursor position.
Definition MSWindowsDesks.cpp:238
Mutual exclusion.
Definition Mutex.h:22
Thread handle.
Definition Thread.h:33