Deskflow 1.22.0.197
Keyboard and mouse sharing utility
Loading...
Searching...
No Matches
XWindowsScreen.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 "arch/Arch.h"
12#include "deskflow/ClientArgs.h"
13#include "deskflow/KeyMap.h"
16
17#include <set>
18#include <vector>
19
20#include <X11/Xlib.h>
21
25
28{
29public:
31 const char *displayName, bool isPrimary, int mouseScrollDelta, IEventQueue *events,
33 );
34 ~XWindowsScreen() override;
35
37
38
40
41 // IScreen overrides
42 void *getEventTarget() const final;
43 bool getClipboard(ClipboardID id, IClipboard *) const override;
44 void getShape(int32_t &x, int32_t &y, int32_t &width, int32_t &height) const override;
45 void getCursorPos(int32_t &x, int32_t &y) const override;
46
47 // IPrimaryScreen overrides
48 void reconfigure(uint32_t activeSides) override;
49 uint32_t activeSides() override;
50 void warpCursor(int32_t x, int32_t y) override;
51 uint32_t registerHotKey(KeyID key, KeyModifierMask mask) override;
52 void unregisterHotKey(uint32_t id) override;
53 void fakeInputBegin() override;
54 void fakeInputEnd() override;
55 int32_t getJumpZoneSize() const override;
56 bool isAnyMouseButtonDown(uint32_t &buttonID) const override;
57 void getCursorCenter(int32_t &x, int32_t &y) const override;
58
59 // ISecondaryScreen overrides
60 void fakeMouseButton(ButtonID id, bool press) override;
61 void fakeMouseMove(int32_t x, int32_t y) override;
62 void fakeMouseRelativeMove(int32_t dx, int32_t dy) const override;
63 void fakeMouseWheel(int32_t xDelta, int32_t yDelta) const override;
64
65 // IPlatformScreen overrides
66 void enable() override;
67 void disable() override;
68 void enter() override;
69 bool canLeave() override;
70 void leave() override;
71 bool setClipboard(ClipboardID, const IClipboard *) override;
72 void checkClipboards() override;
73 void openScreensaver(bool notify) override;
74 void closeScreensaver() override;
75 void screensaver(bool activate) override;
76 void resetOptions() override;
77 void setOptions(const OptionsList &options) override;
78 void setSequenceNumber(uint32_t) override;
79 bool isPrimary() const override;
80 std::string getSecureInputApp() const override;
81
82protected:
83 // IPlatformScreen overrides
84 void handleSystemEvent(const Event &event) override;
85 void updateButtons() override;
86 IKeyState *getKeyState() const override;
87
88private:
89 // event sending
90 void sendEvent(EventTypes, void * = nullptr);
91 void sendClipboardEvent(EventTypes, ClipboardID);
92
93 // create the transparent cursor
94 Cursor createBlankCursor() const;
95
96 // determine the clipboard from the X selection. returns
97 // kClipboardEnd if no such clipboard.
98 ClipboardID getClipboardID(Atom selection) const;
99
100 // continue processing a selection request
101 void processClipboardRequest(Window window, Time time, Atom property) const;
102
103 // terminate a selection request
104 void destroyClipboardRequest(Window window) const;
105
106 // X I/O error handler
107 void onError();
108 static int ioErrorHandler(Display *);
109
110 class KeyEventFilter
111 {
112 public:
113 int m_event;
114 Window m_window;
115 Time m_time;
116 KeyCode m_keycode;
117 };
118
119 Display *openDisplay(const char *displayName);
120 void saveShape();
121 void setShape(int32_t width, int32_t height);
122 Window openWindow() const;
123 void openIM();
124
125 bool grabMouseAndKeyboard();
126 void onKeyPress(XKeyEvent &);
127 void onKeyRelease(XKeyEvent &, bool isRepeat);
128 bool onHotKey(const XKeyEvent &, bool isRepeat);
129 void onMousePress(const XButtonEvent &);
130 void onMouseRelease(const XButtonEvent &);
131 void onMouseMove(const XMotionEvent &);
132
133 bool detectXI2();
134#ifdef HAVE_XI2
135 void selectXIRawMotion();
136#endif
137 void selectEvents(Window) const;
138 void doSelectEvents(Window) const;
139
140 KeyID mapKeyFromX(XKeyEvent *) const;
141 ButtonID mapButtonFromX(const XButtonEvent *) const;
142 unsigned int mapButtonToX(ButtonID id) const;
143
144 void warpCursorNoFlush(int32_t x, int32_t y);
145
146 void refreshKeyboard(XEvent *);
147
148 static Bool findKeyEvent(Display *, XEvent *xevent, XPointer arg);
149
150private:
151 struct HotKeyItem
152 {
153 public:
154 HotKeyItem(int, unsigned int);
155
156 bool operator<(const HotKeyItem &) const;
157
158 private:
159 int m_keycode;
160 unsigned int m_mask;
161 };
162 using FilteredKeycodes = std::set<bool>;
163 using HotKeyList = std::vector<std::pair<int, unsigned int>>;
164 using HotKeyMap = std::map<uint32_t, HotKeyList>;
165 using HotKeyIDList = std::vector<uint32_t>;
166 using HotKeyToIDMap = std::map<HotKeyItem, uint32_t>;
167
168 // true if screen is being used as a primary screen, false otherwise
169 bool m_isPrimary;
170 int m_mouseScrollDelta;
171
172 Display *m_display = nullptr;
173 Window m_root = None;
174 Window m_window = None;
175
176 // true if mouse has entered the screen
177 bool m_isOnScreen;
178
179 uint32_t m_activeSides = 0;
180 // screen shape stuff
181 int32_t m_x = 0;
182 int32_t m_y = 0;
183 int32_t m_w = 0;
184 int32_t m_h = 0;
185 int32_t m_xCenter = 0;
186 int32_t m_yCenter = 0;
187
188 // last mouse position
189 int32_t m_xCursor = 0;
190 int32_t m_yCursor = 0;
191
192 // keyboard stuff
193 XWindowsKeyState *m_keyState = nullptr;
194
195 // hot key stuff
196 HotKeyMap m_hotKeys;
197 HotKeyIDList m_oldHotKeyIDs;
198 HotKeyToIDMap m_hotKeyToIDMap;
199
200 // input focus stuff
201 Window m_lastFocus = None;
202 int m_lastFocusRevert = RevertToNone;
203
204 // input method stuff
205 XIM m_im = nullptr;
206 XIC m_ic = nullptr;
207 KeyCode m_lastKeycode = 0;
208 FilteredKeycodes m_filtered;
209
210 // clipboards
211 XWindowsClipboard *m_clipboard[kClipboardEnd];
212 uint32_t m_sequenceNumber = 0;
213
214 // screen saver stuff
215 XWindowsScreenSaver *m_screensaver = nullptr;
216 bool m_screensaverNotify = false;
217
218 // logical to physical button mapping. m_buttons[i] gives the
219 // physical button for logical button i+1.
220 std::vector<unsigned char> m_buttons;
221
222 // true if global auto-repeat was enabled before we turned it off
223 bool m_autoRepeat;
224
225 // stuff to workaround xtest being xinerama unaware. attempting
226 // to fake a mouse motion under xinerama may behave strangely,
227 // especially if screen 0 is not at 0,0 or if faking a motion on
228 // a screen other than screen 0.
229 bool m_xtestIsXineramaUnaware = true;
230 bool m_xinerama;
231
232 // stuff to work around lost focus issues on certain systems
233 // (ie: a MythTV front-end).
234 bool m_preserveFocus = false;
235
236 // XKB extension stuff
237 bool m_xkb = false;
238 int m_xkbEventBase;
239
240 bool m_xi2detected = false;
241
242 // XRandR extension stuff
243 bool m_xrandr = false;
244 int m_xrandrEventBase;
245
246 IEventQueue *m_events = nullptr;
247 deskflow::KeyMap m_keyMap;
248
249 // pointer to (singleton) screen. this is only needed by
250 // ioErrorHandler().
251 static XWindowsScreen *s_screen;
252 XWindowsPowerManager m_powerManager;
253};
static const ClipboardID kClipboardEnd
Definition ClipboardTypes.h:30
uint8_t ClipboardID
Clipboard ID.
Definition ClipboardTypes.h:16
int key
Definition KeySequence.cpp:15
uint32_t KeyID
Key ID.
Definition KeyTypes.h:22
uint32_t KeyModifierMask
Modifier key mask.
Definition KeyTypes.h:45
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
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
PlatformScreen(IEventQueue *events, deskflow::ClientScrollDirection scrollDirection=deskflow::ClientScrollDirection::Normal)
Definition PlatformScreen.cpp:12
X11 clipboard implementation.
Definition XWindowsClipboard.h:23
X Windows key state.
Definition XWindowsKeyState.h:32
Definition XWindowsPowerManager.h:13
X11 screen saver implementation.
Definition XWindowsScreenSaver.h:23
void warpCursor(int32_t x, int32_t y) override
Warp cursor.
Definition XWindowsScreen.cpp:490
void handleSystemEvent(const Event &event) override
Handle system event.
Definition XWindowsScreen.cpp:1118
bool getClipboard(ClipboardID id, IClipboard *) const override
Get clipboard.
Definition XWindowsScreen.cpp:438
void fakeMouseRelativeMove(int32_t dx, int32_t dy) const override
Fake mouse move.
Definition XWindowsScreen.cpp:781
uint32_t registerHotKey(KeyID key, KeyModifierMask mask) override
Register a system hotkey.
Definition XWindowsScreen.cpp:509
void closeScreensaver() override
Close screen saver.
Definition XWindowsScreen.cpp:382
void updateButtons() override
Update mouse buttons.
Definition XWindowsScreen.cpp:1850
void setOptions(const OptionsList &options) override
Notify of options changes.
Definition XWindowsScreen.cpp:404
XWindowsScreen(const char *displayName, bool isPrimary, int mouseScrollDelta, IEventQueue *events, deskflow::ClientScrollDirection m_clientScrollDirection=deskflow::ClientScrollDirection::Normal)
Definition XWindowsScreen.cpp:84
bool isPrimary() const override
Test if is primary screen.
Definition XWindowsScreen.cpp:422
void fakeInputEnd() override
Done synthesizing input on primary screen.
Definition XWindowsScreen.cpp:729
void screensaver(bool activate) override
Activate/deactivate screen saver.
Definition XWindowsScreen.cpp:389
void resetOptions() override
Notify of options changes.
Definition XWindowsScreen.cpp:398
void * getEventTarget() const final
Get event target.
Definition XWindowsScreen.cpp:433
~XWindowsScreen() override
Definition XWindowsScreen.cpp:162
std::string getSecureInputApp() const override
Determine the name of the app causing a secure input state.
Definition XWindowsScreen.cpp:427
void enable() override
Enable screen.
Definition XWindowsScreen.cpp:192
int32_t getJumpZoneSize() const override
Get jump zone size.
Definition XWindowsScreen.cpp:734
void getShape(int32_t &x, int32_t &y, int32_t &width, int32_t &height) const override
Get screen shape.
Definition XWindowsScreen.cpp:454
uint32_t activeSides() override
activeSides
Definition XWindowsScreen.cpp:485
void getCursorCenter(int32_t &x, int32_t &y) const override
Get cursor center position.
Definition XWindowsScreen.cpp:756
void checkClipboards() override
Check clipboard owner.
Definition XWindowsScreen.cpp:369
void fakeMouseMove(int32_t x, int32_t y) override
Fake mouse move.
Definition XWindowsScreen.cpp:771
void unregisterHotKey(uint32_t id) override
Unregister a system hotkey.
Definition XWindowsScreen.cpp:695
void leave() override
Leave screen.
Definition XWindowsScreen.cpp:293
bool canLeave() override
Leave screen.
Definition XWindowsScreen.cpp:284
void enter() override
Enter screen.
Definition XWindowsScreen.cpp:230
void setSequenceNumber(uint32_t) override
Set clipboard sequence number.
Definition XWindowsScreen.cpp:417
void disable() override
Disable screen.
Definition XWindowsScreen.cpp:213
void fakeInputBegin() override
Prepare to synthesize input on primary screen.
Definition XWindowsScreen.cpp:724
void fakeMouseButton(ButtonID id, bool press) override
Fake mouse press/release.
Definition XWindowsScreen.cpp:762
void reconfigure(uint32_t activeSides) override
Update configuration.
Definition XWindowsScreen.cpp:480
bool setClipboard(ClipboardID, const IClipboard *) override
Set clipboard.
Definition XWindowsScreen.cpp:345
void getCursorPos(int32_t &x, int32_t &y) const override
Get cursor position.
Definition XWindowsScreen.cpp:462
IKeyState * getKeyState() const override
Get the key state.
Definition XWindowsScreen.cpp:1104
void openScreensaver(bool notify) override
Open screen saver.
Definition XWindowsScreen.cpp:374
void fakeMouseWheel(int32_t xDelta, int32_t yDelta) const override
Fake mouse wheel.
Definition XWindowsScreen.cpp:788
bool isAnyMouseButtonDown(uint32_t &buttonID) const override
Test if mouse is pressed.
Definition XWindowsScreen.cpp:739
Key map.
Definition KeyMap.h:24
ClientScrollDirection
Definition ClientArgs.h:13
@ Normal
Same direction as the server.
Definition ClientArgs.h:14
EventTypes
Definition EventTypes.h:13