Deskflow 1.26.0.418
Keyboard and mouse sharing utility
Loading...
Searching...
No Matches
EiScreen.h
Go to the documentation of this file.
1/*
2 * Deskflow -- mouse and keyboard sharing utility
3 * SPDX-FileCopyrightText: (C) 2024, 2026 Synergy App Ltd
4 * SPDX-FileCopyrightText: (C) 2022, 2026 Red Hat, Inc.
5 * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
6 */
7
8#pragma once
9
10#include "deskflow/IScreen.h"
13
14#include <climits>
15#include <libei.h>
16#include <map>
17#include <mutex>
18#include <vector>
19
20struct ei;
21struct ei_event;
22struct ei_seat;
23struct ei_device;
24
25class EventQueueTimer;
26
27namespace deskflow {
28
29class EiKeyState;
32class EiClipboard;
33
35
38{
39public:
40 EiScreen(bool isPrimary, IEventQueue *events, bool usePortal);
41 ~EiScreen() override;
42
43 // IScreen overrides
44 void *getEventTarget() const final;
45 bool getClipboard(ClipboardID id, IClipboard *) const override;
46 void getShape(std::int32_t &x, std::int32_t &y, std::int32_t &width, std::int32_t &height) const override;
47 void getCursorPos(std::int32_t &x, std::int32_t &y) const override;
48
49 // IPrimaryScreen overrides
50 void reconfigure(std::uint32_t activeSides) override;
51 std::uint32_t activeSides() override;
52 void warpCursor(std::int32_t x, std::int32_t y) override;
53 std::uint32_t registerHotKey(KeyID key, KeyModifierMask mask) override;
54 void unregisterHotKey(std::uint32_t id) override;
55 void fakeInputBegin() override;
56 void fakeInputEnd() override;
57 std::int32_t getJumpZoneSize() const override;
58 bool isAnyMouseButtonDown(std::uint32_t &buttonID) const override;
59 void getCursorCenter(std::int32_t &x, std::int32_t &y) const override;
60
61 // ISecondaryScreen overrides
62 void fakeMouseButton(ButtonID id, bool press) override;
63 void fakeMouseMove(std::int32_t x, std::int32_t y) override;
64 void fakeMouseRelativeMove(std::int32_t dx, std::int32_t dy) const override;
65 void fakeMouseWheel(ScrollDelta delta) const override;
66 void fakeKey(std::uint32_t keycode, bool isDown) const;
67
68 // IPlatformScreen overrides
69 void enable() override;
70 void disable() override;
71 void enter() override;
72 bool canLeave() override;
73 void leave() override;
74 bool setClipboard(ClipboardID, const IClipboard *) override;
75 void checkClipboards() override;
76 void openScreensaver(bool notify) override;
77 void closeScreensaver() override;
78 void screensaver(bool activate) override;
79 void resetOptions() override;
80 void setOptions(const OptionsList &options) override;
81 void setSequenceNumber(std::uint32_t) override;
82 bool isPrimary() const override;
83
84 // Send clipboard event (needed by PortalInputCapture)
86
87 // Local clipboard cache (used by PortalRemoteDesktop to land selection reads)
89 {
90 return m_clipboard;
91 }
92
93 // Maximum clipboard size in KB, configured by the server
94 size_t maximumClipboardSize() const
95 {
96 return m_maximumClipboardSize;
97 }
98
99protected:
100 // IPlatformScreen overrides
101 void handleSystemEvent(const Event &event) override;
102 void updateButtons() override;
103 IKeyState *getKeyState() const override;
104 std::string getSecureInputApp() const override;
105
106 void updateShape();
107 void addDevice(ei_device *device);
108 void removeDevice(ei_device *device);
109
110private:
111 void initEi();
112 void cleanupEi();
113 void sendEvent(EventTypes type, void *data);
114 ButtonID mapButtonFromEvdev(ei_event *event) const;
115 void onKeyEvent(ei_event *event);
116 void onButtonEvent(ei_event *event);
117 void sendWheelEvents(ei_device *device, const int threshold, double dx, double dy, bool is_discrete);
118 void onPointerScrollEvent(ei_event *event);
119 void onPointerScrollDiscreteEvent(ei_event *event);
120 void onMotionEvent(ei_event *event);
121 void onAbsMotionEvent(const ei_event *) const;
122 bool onHotkey(KeyID key, bool isPressed, KeyModifierMask mask);
123 void eiLogEvent(ei_log_priority priority, const char *message) const;
124
125 void handleConnectedToEisEvent(const Event &event);
126 void handlePortalSessionClosed();
127 void ensureEmulating() const;
128 void stopEmulating() const;
129 void cancelIdleEmulationTimer() const;
130
131 static void handleEiLogEvent(ei *ei, const ei_log_priority priority, const char *message, ei_log_context *)
132 {
133 auto screen = static_cast<EiScreen *>(ei_get_user_data(ei));
134 screen->eiLogEvent(priority, message);
135 }
136
137private:
138 // true if screen is being used as a primary screen, false otherwise
139 bool m_isPrimary = false;
140 IEventQueue *m_events = nullptr;
141
142 // keyboard stuff
143 EiKeyState *m_keyState = nullptr;
144
145 KeyID m_lastPressed = kKeyNone;
146
147 // clipboard stuff
148 EiClipboard *m_clipboard = nullptr;
149 size_t m_maximumClipboardSize = INT_MAX;
150
151 std::vector<ei_device *> m_eiDevices;
152
153 ei *m_ei = nullptr;
154 ei_seat *m_eiSeat = nullptr;
155 ei_device *m_eiPointer = nullptr;
156 ei_device *m_eiKeyboard = nullptr;
157 ei_device *m_eiAbs = nullptr;
158
159 mutable std::uint32_t m_sequenceNumber = 0;
160
161 // Lazily-started EIS emulation: only grab while relayed input is actually
162 // flowing, and release after a short idle so the compositor can DPMS-sleep
163 // this screen even while the deskflow cursor logically sits on it.
164 mutable bool m_isEmulating = false;
165 mutable EventQueueTimer *m_idleEmulationTimer = nullptr;
166 // Chosen empirically on one machine in 2026-06; not derived from any protocol constant.
167 static constexpr double s_idleEmulationTimeout = 4.0;
168
169 std::uint32_t m_activeSides = 0;
170 std::uint32_t m_x = 0;
171 std::uint32_t m_y = 0;
172 std::uint32_t m_w = 0;
173 std::uint32_t m_h = 0;
174 bool m_isShapeInitialized = false;
175
176 // true if mouse has entered the screen
177 bool m_isOnScreen;
178
179 // server: last pointer position
180 // client: position sent before enter()
181 std::int32_t m_cursorX = 0;
182 std::int32_t m_cursorY = 0;
183
184 double m_bufferDX = 0;
185 double m_bufferDY = 0;
186
187 mutable std::mutex m_mutex;
188
189 PortalRemoteDesktop *m_portalRemoteDesktop = nullptr;
190 PortalInputCapture *m_portalInputCapture = nullptr;
191
192 struct HotKeyItem
193 {
194 public:
195 HotKeyItem(std::uint32_t mask, std::uint32_t id);
196 auto operator<=>(const HotKeyItem &other) const
197 {
198 return mask <=> other.mask;
199 }
200
201 public:
202 std::uint32_t mask = 0;
203 std::uint32_t id = 0; // for registering the hotkey
204 };
205
206 class HotKeySet
207 {
208 public:
209 explicit HotKeySet(KeyID keyid);
210 KeyID keyid() const
211 {
212 return m_id;
213 }
214 bool removeById(std::uint32_t id);
215 void addItem(HotKeyItem item);
216 std::uint32_t findByMask(std::uint32_t mask) const;
217
218 private:
219 KeyID m_id = 0;
220 std::vector<HotKeyItem> m_set;
221 };
222
223 using HotKeyMap = std::map<KeyID, HotKeySet>;
224
225 HotKeyMap m_hotkeys;
226 [[no_unique_address]] XDGPowerManager m_powerManager;
227};
228
229} // namespace deskflow
static int void FAR * data
Definition ArchNetworkWinsock.cpp:36
static int type
Definition ArchNetworkWinsock.cpp:46
uint8_t ClipboardID
Clipboard ID.
Definition ClipboardTypes.h:16
Coordinate ScrollDelta
Definition Coordinate.h:21
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
Definition EventQueueTimer.h:12
Event.
Definition Event.h:43
Clipboard interface.
Definition IClipboard.h:19
Event queue interface.
Definition IEventQueue.h:29
Key state interface.
Definition IKeyState.h:23
PlatformScreen(IEventQueue *events)
Definition PlatformScreen.cpp:13
EI/Portal clipboard implementation.
Definition EiClipboard.h:21
A key state for Ei.
Definition EiKeyState.h:23
Implementation of IPlatformScreen for X11.
Definition EiScreen.h:38
void leave() override
Leave screen.
Definition EiScreen.cpp:438
void checkClipboards() override
Check clipboard owner.
Definition EiScreen.cpp:476
void unregisterHotKey(std::uint32_t id) override
Definition EiScreen.cpp:242
void enter() override
Enter screen.
Definition EiScreen.cpp:419
size_t maximumClipboardSize() const
Definition EiScreen.h:94
void setOptions(const OptionsList &options) override
Notify of options changes.
Definition EiScreen.cpp:504
bool isAnyMouseButtonDown(std::uint32_t &buttonID) const override
Definition EiScreen.cpp:267
std::int32_t getJumpZoneSize() const override
Get jump zone size.
Definition EiScreen.cpp:262
void warpCursor(std::int32_t x, std::int32_t y) override
Definition EiScreen.cpp:219
void sendClipboardEvent(EventTypes type, ClipboardID id) const
Definition EiScreen.cpp:655
void * getEventTarget() const final
Get event target.
Definition EiScreen.cpp:169
std::uint32_t activeSides() override
activeSides
Definition EiScreen.cpp:214
void openScreensaver(bool notify) override
Open screen saver.
Definition EiScreen.cpp:483
IKeyState * getKeyState() const override
Get the key state.
Definition EiScreen.cpp:1027
void addDevice(ei_device *device)
Definition EiScreen.cpp:569
void fakeMouseWheel(ScrollDelta delta) const override
Synthesize a mouse wheel event of amount This Implmentation for this method should call applyScrollMo...
Definition EiScreen.cpp:332
void getCursorPos(std::int32_t &x, std::int32_t &y) const override
Definition EiScreen.cpp:201
void fakeInputBegin() override
Prepare to synthesize input on primary screen.
Definition EiScreen.cpp:252
std::uint32_t registerHotKey(KeyID key, KeyModifierMask mask) override
Register a system hotkey.
Definition EiScreen.cpp:225
void resetOptions() override
Notify of options changes.
Definition EiScreen.cpp:498
EiScreen(bool isPrimary, IEventQueue *events, bool usePortal)
Definition EiScreen.cpp:41
void closeScreensaver() override
Close screen saver.
Definition EiScreen.cpp:488
void updateButtons() override
Update mouse buttons.
Definition EiScreen.cpp:1021
void reconfigure(std::uint32_t activeSides) override
Definition EiScreen.cpp:207
void fakeInputEnd() override
Done synthesizing input on primary screen.
Definition EiScreen.cpp:257
bool getClipboard(ClipboardID id, IClipboard *) const override
Get clipboard.
Definition EiScreen.cpp:174
void setSequenceNumber(std::uint32_t) override
Definition EiScreen.cpp:669
void getShape(std::int32_t &x, std::int32_t &y, std::int32_t &width, std::int32_t &height) const override
Definition EiScreen.cpp:193
void screensaver(bool activate) override
Activate/deactivate screen saver.
Definition EiScreen.cpp:493
void handleSystemEvent(const Event &event) override
Handle system event.
Definition EiScreen.cpp:900
bool canLeave() override
Leave screen.
Definition EiScreen.cpp:433
void enable() override
Enable screen.
Definition EiScreen.cpp:358
void fakeMouseRelativeMove(std::int32_t dx, std::int32_t dy) const override
Definition EiScreen.cpp:322
void removeDevice(ei_device *device)
Definition EiScreen.cpp:616
void fakeMouseButton(ButtonID id, bool press) override
Fake mouse press/release.
Definition EiScreen.cpp:278
std::string getSecureInputApp() const override
Determine the name of the app causing a secure input state.
Definition EiScreen.cpp:1032
void updateShape()
Definition EiScreen.cpp:522
bool isPrimary() const override
Test if is primary screen.
Definition EiScreen.cpp:517
void getCursorCenter(std::int32_t &x, std::int32_t &y) const override
Definition EiScreen.cpp:272
bool setClipboard(ClipboardID, const IClipboard *) override
Set clipboard.
Definition EiScreen.cpp:447
EiClipboard * getClipboardCache() const
Definition EiScreen.h:88
void fakeMouseMove(std::int32_t x, std::int32_t y) override
Definition EiScreen.cpp:305
void fakeKey(std::uint32_t keycode, bool isDown) const
Definition EiScreen.cpp:346
void disable() override
Disable screen.
Definition EiScreen.cpp:365
Definition PortalInputCapture.h:30
Definition PortalRemoteDesktop.h:21
Definition DaemonApp.h:18
EventTypes
Definition EventTypes.h:13
IScreen::ClipboardInfo ClipboardInfo
Definition EiScreen.h:34
Definition IScreen.h:24