forked from deskflow/deskflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMSWindowsScreen.h
More file actions
341 lines (267 loc) · 10.2 KB
/
Copy pathMSWindowsScreen.h
File metadata and controls
341 lines (267 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2025 Deskflow Developers
* SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
* SPDX-FileCopyrightText: (C) 2002 Chris Schoeneman
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
#pragma once
#include "deskflow/PlatformScreen.h"
#include "platform/MSWindowsHook.h"
#include "platform/MSWindowsPowerManager.h"
#include <map>
#include <string>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
class EventQueueTimer;
class MSWindowsDesks;
class MSWindowsKeyState;
class MSWindowsScreenSaver;
class Thread;
class MSWindowsDropTarget;
//! Implementation of IPlatformScreen for Microsoft Windows
class MSWindowsScreen : public PlatformScreen
{
public:
MSWindowsScreen(
bool isPrimary, bool useHooks, IEventQueue *events, bool enableLangSync = false, bool invetScrolling = false
);
~MSWindowsScreen() override;
//! @name manipulators
//@{
//! Initialize
/*!
Saves the application's HINSTANCE. This \b must be called by
WinMain with the HINSTANCE it was passed.
*/
static void init(HINSTANCE);
//@}
//! @name accessors
//@{
//! Get instance
/*!
Returns the application instance handle passed to init().
*/
static HINSTANCE getWindowInstance();
//@}
// IScreen overrides
void *getEventTarget() const override;
bool getClipboard(ClipboardID id, IClipboard *) const override;
void getShape(int32_t &x, int32_t &y, int32_t &width, int32_t &height) const override;
void getCursorPos(int32_t &x, int32_t &y) const override;
/**
* \brief Get the position of the cursor on the current machine
* \param pos the object that the function will use to store the position of
* the cursor \return true if the function was successful
*/
virtual bool getThisCursorPos(LPPOINT pos);
/**
* \brief Sets the cursor position on the current machine
* \param x The x coordinate of the cursor
* \param y The Y coordinate of the cursor
* \return True is successful
*/
virtual bool setThisCursorPos(int x, int y);
/**
* \brief This function will attempt to switch to the current desktop the
* mouse is located on
*/
virtual void updateDesktopThread();
// IPrimaryScreen overrides
void reconfigure(uint32_t activeSides) override;
uint32_t activeSides() override;
void warpCursor(int32_t x, int32_t y) override;
uint32_t registerHotKey(KeyID key, KeyModifierMask mask, bool registerGlobalHotkey) override;
void unregisterHotKey(uint32_t id, bool unregisterGlobalHotkey) override;
void fakeInputBegin() override;
void fakeInputEnd() override;
int32_t getJumpZoneSize() const override;
bool isAnyMouseButtonDown(uint32_t &buttonID) const override;
void getCursorCenter(int32_t &x, int32_t &y) const override;
// ISecondaryScreen overrides
void fakeMouseButton(ButtonID id, bool press) override;
void fakeMouseMove(int32_t x, int32_t y) override;
void fakeMouseRelativeMove(int32_t dx, int32_t dy) const override;
void fakeMouseWheel(int32_t xDelta, int32_t yDelta) const override;
// IKeyState overrides
virtual void updateKeys();
void fakeKeyDown(KeyID id, KeyModifierMask mask, KeyButton button, const std::string &lang) override;
bool fakeKeyRepeat(KeyID id, KeyModifierMask mask, int32_t count, KeyButton button, const std::string &lang) override;
bool fakeKeyUp(KeyButton button) override;
void fakeAllKeysUp() override;
// IPlatformScreen overrides
void enable() override;
void disable() override;
void enter() override;
bool canLeave() override;
void leave() override;
bool setClipboard(ClipboardID, const IClipboard *) override;
void checkClipboards() override;
void openScreensaver(bool notify) override;
void closeScreensaver() override;
void screensaver(bool activate) override;
void resetOptions() override;
void setOptions(const OptionsList &options) override;
void setSequenceNumber(uint32_t) override;
bool isPrimary() const override;
std::string getSecureInputApp() const override;
protected:
// IPlatformScreen overrides
void handleSystemEvent(const Event &event) override;
void updateButtons() override;
IKeyState *getKeyState() const override;
// simulate a local key to the system directly
void fakeLocalKey(KeyButton button, bool press) const;
private:
// initialization and shutdown operations
HCURSOR createBlankCursor() const;
void destroyCursor(HCURSOR cursor) const;
ATOM createWindowClass() const;
ATOM createDeskWindowClass(bool isPrimary) const;
void destroyClass(ATOM windowClass) const;
HWND createWindow(ATOM windowClass, const wchar_t *name) const;
void destroyWindow(HWND) const;
// convenience function to send events
public: // HACK
void sendEvent(EventTypes type, void * = nullptr);
private: // HACK
void sendClipboardEvent(EventTypes type, ClipboardID id);
// handle message before it gets dispatched. returns true iff
// the message should not be dispatched.
bool onPreDispatch(HWND, UINT, WPARAM, LPARAM);
// handle message before it gets dispatched. returns true iff
// the message should not be dispatched.
bool onPreDispatchPrimary(HWND, UINT, WPARAM, LPARAM);
// handle secondary message before it gets dispatched. returns true iff
// the message should not be dispatched.
bool onPreDispatchSecondary(HWND, UINT, WPARAM, LPARAM);
// handle message. returns true iff handled and optionally sets
// \c *result (which defaults to 0).
bool onEvent(HWND, UINT, WPARAM, LPARAM, LRESULT *result);
// message handlers
bool onMark(uint32_t mark);
bool onKey(WPARAM, LPARAM);
bool onHotKey(WPARAM, LPARAM);
bool onMouseButton(WPARAM, LPARAM);
bool onMouseMove(int32_t x, int32_t y);
bool onMouseWheel(int32_t xDelta, int32_t yDelta);
bool onScreensaver(bool activated);
bool onDisplayChange();
void onClipboardChange();
// warp cursor without discarding queued events
void warpCursorNoFlush(int32_t x, int32_t y);
// discard posted messages
void nextMark();
// test if event should be ignored
bool ignore() const;
// update screen size cache
void updateScreenShape();
// fix timer callback
void handleFixes();
// fix the clipboard viewer chain
void fixClipboardViewer();
// enable/disable special key combinations so we can catch/pass them
void enableSpecialKeys(bool) const;
// map a button event to a button ID
ButtonID mapButtonFromEvent(WPARAM msg, LPARAM button) const;
// map a button event to a press (true) or release (false)
bool mapPressFromEvent(WPARAM msg, LPARAM button) const;
// job to update the key state
void updateKeysCB(const void *);
// determine whether the mouse is hidden by the system.
// if true and on secondary screen, enable mouse keys to show the cursor.
// we were previously restoring the old mouse key settings when not needed, but this was causing
// issues where the mouse cursor becomes permanently hidden, even if there is a real mouse
// attached to the system; this could be a windows bug, but losing your mouse is a nightmare
// so we shouldn't risk doing that.
void setupMouseKeys();
// enables the mouse keys accessibility feature to to ensure the
// mouse cursor can be shown.
void updateMouseKeys();
// our window proc
static LRESULT CALLBACK wndProc(HWND, UINT, WPARAM, LPARAM);
// save last position of mouse to compute next delta movement
void saveMousePosition(int32_t x, int32_t y);
// check if it is a modifier key repeating message
bool isModifierRepeat(KeyModifierMask oldState, KeyModifierMask state, WPARAM wParam) const;
private:
struct HotKeyItem
{
public:
HotKeyItem(UINT vk, UINT modifiers);
UINT getVirtualKey() const;
bool operator<(const HotKeyItem &) const;
private:
UINT m_keycode;
UINT m_mask;
};
using HotKeyMap = std::map<uint32_t, HotKeyItem>;
using HotKeyIDList = std::vector<uint32_t>;
using HotKeyToIDMap = std::map<HotKeyItem, uint32_t>;
using PrimaryKeyDownList = std::vector<KeyButton>;
static HINSTANCE s_windowInstance;
// true if screen is being used as a primary screen, false otherwise
bool m_isPrimary;
// true if hooks are not to be installed (useful for debugging)
bool m_useHooks;
// true if mouse has entered the screen
bool m_isOnScreen;
// true if the screen is enabled
bool m_isEnabled = false;
// our resources
ATOM m_class = 0;
// screen shape stuff
int32_t m_x = 0;
int32_t m_y = 0;
int32_t m_w = 0;
int32_t m_h = 0;
int32_t m_xCenter = 0;
int32_t m_yCenter = 0;
// true if system appears to have multiple monitors
bool m_multimon = false;
// last mouse position
int32_t m_xCursor = 0;
int32_t m_yCursor = 0;
// last clipboard
uint32_t m_sequenceNumber = 0;
// used to discard queued messages that are no longer needed
uint32_t m_mark = 0;
uint32_t m_markReceived = 0;
// the main loop's thread id
DWORD m_threadID;
// timer for periodically checking stuff that requires polling
EventQueueTimer *m_fixTimer = nullptr;
// the keyboard layout to use when off primary screen
HKL m_keyLayout = nullptr;
// screen saver stuff
MSWindowsScreenSaver *m_screensaver = nullptr;
bool m_screensaverNotify = false;
bool m_screensaverActive = false;
// clipboard stuff. our window is used mainly as a clipboard
// owner and as a link in the clipboard viewer chain.
HWND m_window = nullptr;
DWORD m_clipboardSequenceNumber = 0;
bool m_ownClipboard = false;
// one desk per desktop and a cond var to communicate with it
MSWindowsDesks *m_desks = nullptr;
// keyboard stuff
MSWindowsKeyState *m_keyState = nullptr;
// hot key stuff
HotKeyMap m_hotKeys;
HotKeyIDList m_oldHotKeyIDs;
HotKeyToIDMap m_hotKeyToIDMap;
// map of button state
bool m_buttons[1 + kButtonExtra0 + 1];
// m_hasMouse is true if there's a mouse attached to the system or
// mouse keys is simulating one. we track this so we can force the
// cursor to be displayed when the user has entered this screen.
bool m_hasMouse;
bool m_gotMouseKeys = false;
MOUSEKEYS m_mouseKeys;
MSWindowsHook m_hook;
static MSWindowsScreen *s_screen;
IEventQueue *m_events;
std::string m_desktopPath;
PrimaryKeyDownList m_primaryKeyDownList;
MSWindowsPowerManager m_powerManager;
};