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