Deskflow 1.22.0.197
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/KeyTypes.h"
12#include "deskflow/MouseTypes.h"
14#include "mt/CondVar.h"
15#include "mt/Mutex.h"
16
17#include <map>
18#include <string>
19
20#define WIN32_LEAN_AND_MEAN
21#include <Windows.h>
22
23class Event;
24class EventQueueTimer;
25class Thread;
26class IJob;
27class IScreenSaver;
28class IEventQueue;
29
31
47{
48public:
50
58 MSWindowsDesks(bool isPrimary, bool noHooks, const IScreenSaver *screensaver, IEventQueue *events, IJob *updateKeys);
60
62
63
65
71 void enable();
72
74
77 void disable();
78
80
83 void enter();
84
86
89 void leave(HKL keyLayout);
90
92
95 void resetOptions();
96
98
102 void setOptions(const OptionsList &options);
103
105
109 void updateKeys();
110
112
115 void setShape(int32_t x, int32_t y, int32_t width, int32_t height, int32_t xCenter, int32_t yCenter, bool isMultimon);
116
118
123 void installScreensaverHooks(bool install);
124
126
129 void fakeInputBegin();
130
132
135 void fakeInputEnd();
136
138
140
142
145 void getCursorPos(int32_t &x, int32_t &y) const;
146
148
151 void fakeKeyEvent(WORD virtualKey, WORD scanCode, DWORD flags, bool isAutoRepeat) const;
152
154
157 void fakeMouseButton(ButtonID id, bool press);
158
160
163 void fakeMouseMove(int32_t x, int32_t y) const;
164
166
169 void fakeMouseRelativeMove(int32_t dx, int32_t dy) const;
170
172
175 void fakeMouseWheel(int32_t xDelta, int32_t yDelta) const;
176
178
179private:
180 class Desk
181 {
182 public:
183 std::string m_name;
184 Thread *m_thread;
185 DWORD m_threadID;
186 DWORD m_targetID;
187 HDESK m_desk;
188 HWND m_window;
189 HWND m_foregroundWindow;
190 bool m_lowLevel;
191 };
192 using Desks = std::map<std::string, Desk *>;
193
194 // initialization and shutdown operations
195 HCURSOR createBlankCursor() const;
196 void destroyCursor(HCURSOR cursor) const;
197 ATOM createDeskWindowClass(bool isPrimary) const;
198 void destroyClass(ATOM windowClass) const;
199 HWND createWindow(ATOM windowClass, const char *name) const;
200 void destroyWindow(HWND) const;
201
202 // message handlers
203 void deskMouseMove(int32_t x, int32_t y) const;
204 void deskMouseRelativeMove(int32_t dx, int32_t dy) const;
205 void deskEnter(Desk *desk);
206 void deskLeave(Desk *desk, HKL keyLayout);
207 void deskThread(void *vdesk);
208
209 // desk switch checking and handling
210 Desk *addDesk(const std::string &name, HDESK hdesk);
211 void removeDesks();
212 void checkDesk();
213 bool isDeskAccessible(const Desk *desk) const;
214 void handleCheckDesk();
215
216 // communication with desk threads
217 void waitForDesk() const;
218 void sendMessage(UINT, WPARAM, LPARAM) const;
219
220 // work around for messed up keyboard events from low-level hooks
221 HWND getForegroundWindow() const;
222
223 // desk API wrappers
224 HDESK openInputDesktop();
225 void closeDesktop(HDESK);
226 std::string getDesktopName(HDESK);
227
228 // our desk window procs
229 static LRESULT CALLBACK primaryDeskProc(HWND, UINT, WPARAM, LPARAM);
230 static LRESULT CALLBACK secondaryDeskProc(HWND, UINT, WPARAM, LPARAM);
231
232private:
233 // true if screen is being used as a primary screen, false otherwise
234 bool m_isPrimary;
235
236 // true if hooks are not to be installed (useful for debugging)
237 bool m_noHooks;
238
239 // true if mouse has entered the screen
240 bool m_isOnScreen;
241
242 // our resources
243 ATOM m_deskClass;
244 HCURSOR m_cursor;
245
246 // screen shape stuff
247 int32_t m_x = 0;
248 int32_t m_y = 9;
249 int32_t m_w = 0;
250 int32_t m_h = 0;
251 int32_t m_xCenter = 0;
252 int32_t m_yCenter = 0;
253
254 // true if system appears to have multiple monitors
255 bool m_multimon = false;
256
257 // the timer used to check for desktop switching
258 EventQueueTimer *m_timer = nullptr;
259
260 // screen saver stuff
261 DWORD m_threadID;
262 const IScreenSaver *m_screensaver;
263 bool m_screensaverNotify = false;
264
265 // the current desk and it's name
266 Desk *m_activeDesk = nullptr;
267 std::string m_activeDeskName;
268
269 // one desk per desktop and a cond var to communicate with it
270 Mutex m_mutex;
271 CondVar<bool> m_deskReady;
272 Desks m_desks;
273
274 // keyboard stuff
275 IJob *m_updateKeys;
276 HKL m_keyLayout;
277
278 // options
279 bool m_leaveForegroundOption;
280
281 IEventQueue *m_events;
282};
static const struct sockaddr FAR * name
Definition ArchNetworkWinsock.cpp:27
static const WSAEVENT FAR DWORD
Definition ArchNetworkWinsock.cpp:54
static void FAR int int flags
Definition ArchNetworkWinsock.cpp:37
uint8_t ButtonID
Mouse button ID.
Definition MouseTypes.h:16
std::vector< uint32_t > OptionsList
Definition OptionTypes.h:27
int y
Definition ServerConfig.cpp:27
int x
Definition ServerConfig.cpp:26
Condition variable.
Definition CondVar.h:122
Definition SimpleEventQueueBuffer.cpp:13
Event.
Definition Event.h:27
Event queue interface.
Definition IEventQueue.h:32
Job interface.
Definition IJob.h:17
Screen saver interface.
Definition IScreenSaver.h:18
MSWindowsDesks(bool isPrimary, bool noHooks, const IScreenSaver *screensaver, IEventQueue *events, IJob *updateKeys)
Constructor.
Definition MSWindowsDesks.cpp:109
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
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