Deskflow 1.24.0.365
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 Deskflow Developers
4 * SPDX-FileCopyrightText: (C) 2012 - 2016 Symless 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 };
191 using Desks = std::map<std::wstring, Desk *>;
192
193 // initialization and shutdown operations
194 HCURSOR createBlankCursor() const;
195 void destroyCursor(HCURSOR cursor) const;
196 ATOM createDeskWindowClass(bool isPrimary) const;
197 void destroyClass(ATOM windowClass) const;
198 HWND createWindow(ATOM windowClass, const wchar_t *name) const;
199 void destroyWindow(HWND) const;
200
201 // message handlers
202 void deskMouseMove(int32_t x, int32_t y) const;
203 void deskMouseRelativeMove(int32_t dx, int32_t dy) const;
204 void deskEnter(Desk *desk);
205 void deskLeave(Desk *desk, HKL keyLayout);
206 void deskThread(const void *vdesk);
207
208 // desk switch checking and handling
209 Desk *addDesk(const std::wstring &name, HDESK hdesk);
210 void removeDesks();
211 void checkDesk();
212 bool isDeskAccessible(const Desk *desk) const;
213 void handleCheckDesk();
214
215 // communication with desk threads
216 void waitForDesk() const;
217 void sendMessage(UINT, WPARAM, LPARAM) const;
218
219 // work around for messed up keyboard events from low-level hooks
220 HWND getForegroundWindow() const;
221
222 // desk API wrappers
223 HDESK openInputDesktop();
224 void closeDesktop(HDESK);
225 std::wstring getDesktopName(HDESK);
226
227 // our desk window procs
228 static LRESULT CALLBACK primaryDeskProc(HWND, UINT, WPARAM, LPARAM);
229 static LRESULT CALLBACK secondaryDeskProc(HWND, UINT, WPARAM, LPARAM);
230
231private:
232 // true if screen is being used as a primary screen, false otherwise
233 bool m_isPrimary;
234
235 // true if hooks are to be installed (when debugging, it can be useful to disable hooks)
236 bool m_useHooks;
237
238 // true if mouse has entered the screen
239 bool m_isOnScreen;
240
241 // our resources
242 ATOM m_deskClass;
243 HCURSOR m_cursor;
244
245 // screen shape stuff
246 int32_t m_x = 0;
247 int32_t m_y = 9;
248 int32_t m_w = 0;
249 int32_t m_h = 0;
250 int32_t m_xCenter = 0;
251 int32_t m_yCenter = 0;
252
253 // true if system appears to have multiple monitors
254 bool m_multimon = false;
255
256 // the timer used to check for desktop switching
257 EventQueueTimer *m_timer = nullptr;
258
259 // screen saver stuff
260 DWORD m_threadID;
261 const IScreenSaver *m_screensaver;
262 bool m_screensaverNotify = false;
263
264 // the current desk and it's name
265 Desk *m_activeDesk = nullptr;
266 std::wstring m_activeDeskName;
267
268 // one desk per desktop and a cond var to communicate with it
269 Mutex m_mutex;
270 CondVar<bool> m_deskReady;
271 Desks m_desks;
272
273 // keyboard stuff
274 IJob *m_updateKeys;
275 HKL m_keyLayout;
276
277 // options
278 bool m_leaveForegroundOption;
279
280 IEventQueue *m_events;
281};
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:25
int x
Definition ServerConfig.cpp:24
Condition variable.
Definition CondVar.h:121
Definition EventQueueTimer.h:12
Event.
Definition Event.h:29
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:167
void fakeInputEnd()
Stop ignoring user input.
Definition MSWindowsDesks.cpp:223
void fakeMouseWheel(int32_t xDelta, int32_t yDelta) const
Fake mouse wheel.
Definition MSWindowsDesks.cpp:303
MSWindowsDesks(bool isPrimary, bool useHooks, const IScreenSaver *screensaver, IEventQueue *events, IJob *updateKeys)
Constructor.
Definition MSWindowsDesks.cpp:109
void leave(HKL keyLayout)
Notify of leaving a desk.
Definition MSWindowsDesks.cpp:172
void disable()
Disable desk tracking.
Definition MSWindowsDesks.cpp:152
void fakeMouseButton(ButtonID id, bool press)
Fake mouse press/release.
Definition MSWindowsDesks.cpp:241
void fakeInputBegin()
Start ignoring user input.
Definition MSWindowsDesks.cpp:218
void setOptions(const OptionsList &options)
Notify of options changes.
Definition MSWindowsDesks.cpp:182
void enable()
Enable desk tracking.
Definition MSWindowsDesks.cpp:135
void updateKeys()
Update the key state.
Definition MSWindowsDesks.cpp:192
void resetOptions()
Notify of options changes.
Definition MSWindowsDesks.cpp:177
void fakeMouseRelativeMove(int32_t dx, int32_t dy) const
Fake mouse move.
Definition MSWindowsDesks.cpp:298
void fakeMouseMove(int32_t x, int32_t y) const
Fake mouse move.
Definition MSWindowsDesks.cpp:293
void fakeKeyEvent(WORD virtualKey, WORD scanCode, DWORD flags, bool isAutoRepeat) const
Fake key press/release.
Definition MSWindowsDesks.cpp:236
void installScreensaverHooks(bool install)
Install/uninstall screensaver hooks.
Definition MSWindowsDesks.cpp:210
~MSWindowsDesks()
Definition MSWindowsDesks.cpp:127
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:197
void getCursorPos(int32_t &x, int32_t &y) const
Get cursor position.
Definition MSWindowsDesks.cpp:228
Mutual exclusion.
Definition Mutex.h:22
Thread handle.
Definition Thread.h:33