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