Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
kwin_input.cpp
Go to the documentation of this file.
1
5#include "kwin_input.h"
7
8#include "src/logging.h"
10
11#include <algorithm>
12#include <boost/locale.hpp>
13#include <libevdev/libevdev.h>
14#include <map>
15#include <mutex>
16#include <stdexcept>
17
18namespace platf {
19 namespace {
20 // evdev keys to the Moonlight key codes (Windows virtual keys) they serve.
21 // An evdev key can serve more than one, so the lookup goes by virtual key.
22 const std::multimap<short, short> key_mappings = {
23 {KEY_BACKSPACE, 0x08},
24 {KEY_TAB, 0x09},
25 {KEY_ENTER, 0x0D},
26 {KEY_LEFTSHIFT, 0x10},
27 {KEY_LEFTCTRL, 0x11},
28 {KEY_LEFTALT, 0x12},
29 {KEY_CAPSLOCK, 0x14},
30 {KEY_ESC, 0x1B},
31 {KEY_SPACE, 0x20},
32 {KEY_PAGEUP, 0x21},
33 {KEY_PAGEDOWN, 0x22},
34 {KEY_END, 0x23},
35 {KEY_HOME, 0x24},
36 {KEY_LEFT, 0x25},
37 {KEY_UP, 0x26},
38 {KEY_RIGHT, 0x27},
39 {KEY_DOWN, 0x28},
40 {KEY_SYSRQ, 0x2C},
41 {KEY_INSERT, 0x2D},
42 {KEY_DELETE, 0x2E},
43 {KEY_0, 0x30},
44 {KEY_1, 0x31},
45 {KEY_2, 0x32},
46 {KEY_3, 0x33},
47 {KEY_4, 0x34},
48 {KEY_5, 0x35},
49 {KEY_6, 0x36},
50 {KEY_7, 0x37},
51 {KEY_8, 0x38},
52 {KEY_9, 0x39},
53 {KEY_A, 0x41},
54 {KEY_B, 0x42},
55 {KEY_C, 0x43},
56 {KEY_D, 0x44},
57 {KEY_E, 0x45},
58 {KEY_F, 0x46},
59 {KEY_G, 0x47},
60 {KEY_H, 0x48},
61 {KEY_I, 0x49},
62 {KEY_J, 0x4A},
63 {KEY_K, 0x4B},
64 {KEY_L, 0x4C},
65 {KEY_M, 0x4D},
66 {KEY_N, 0x4E},
67 {KEY_O, 0x4F},
68 {KEY_P, 0x50},
69 {KEY_Q, 0x51},
70 {KEY_R, 0x52},
71 {KEY_S, 0x53},
72 {KEY_T, 0x54},
73 {KEY_U, 0x55},
74 {KEY_V, 0x56},
75 {KEY_W, 0x57},
76 {KEY_X, 0x58},
77 {KEY_Y, 0x59},
78 {KEY_Z, 0x5A},
79 {KEY_LEFTMETA, 0x5B},
80 {KEY_RIGHTMETA, 0x5C},
81 {KEY_KP0, 0x60},
82 {KEY_KP1, 0x61},
83 {KEY_KP2, 0x62},
84 {KEY_KP3, 0x63},
85 {KEY_KP4, 0x64},
86 {KEY_KP5, 0x65},
87 {KEY_KP6, 0x66},
88 {KEY_KP7, 0x67},
89 {KEY_KP8, 0x68},
90 {KEY_KP9, 0x69},
91 {KEY_KPASTERISK, 0x6A},
92 {KEY_KPPLUS, 0x6B},
93 {KEY_KPMINUS, 0x6D},
94 {KEY_KPDOT, 0x6E},
95 {KEY_KPSLASH, 0x6F},
96 {KEY_F1, 0x70},
97 {KEY_F2, 0x71},
98 {KEY_F3, 0x72},
99 {KEY_F4, 0x73},
100 {KEY_F5, 0x74},
101 {KEY_F6, 0x75},
102 {KEY_F7, 0x76},
103 {KEY_F8, 0x77},
104 {KEY_F9, 0x78},
105 {KEY_F10, 0x79},
106 {KEY_F11, 0x7A},
107 {KEY_F12, 0x7B},
108 {KEY_NUMLOCK, 0x90},
109 {KEY_SCROLLLOCK, 0x91},
110 {KEY_LEFTSHIFT, 0xA0},
111 {KEY_RIGHTSHIFT, 0xA1},
112 {KEY_LEFTCTRL, 0xA2},
113 {KEY_RIGHTCTRL, 0xA3},
114 {KEY_LEFTALT, 0xA4},
115 {KEY_RIGHTALT, 0xA5},
116 {KEY_SEMICOLON, 0xBA},
117 {KEY_EQUAL, 0xBB},
118 {KEY_COMMA, 0xBC},
119 {KEY_MINUS, 0xBD},
120 {KEY_DOT, 0xBE},
121 {KEY_SLASH, 0xBF},
122 {KEY_GRAVE, 0xC0},
123 {KEY_LEFTBRACE, 0xDB},
124 {KEY_BACKSLASH, 0xDC},
125 {KEY_RIGHTBRACE, 0xDD},
126 {KEY_APOSTROPHE, 0xDE},
127 {KEY_102ND, 0xE2}
128 };
129
130 } // namespace
131
132 input_raw_t::input_raw_t(std::shared_ptr<kwin::connection_t> private_kwin, std::string input_session_tag):
133 session_tag(std::move(input_session_tag)),
134 kwin(std::move(private_kwin)),
135 runtime(session_tag.empty() ? nullptr : gamepad::runtime_for(session_tag)),
136 gamepads(MAX_GAMEPADS) {
137 if (!session_tag.empty() && !runtime) {
138 BOOST_LOG(error) << "[libvirtualhid] Failed to create the detached desktop's gamepad runtime";
139 }
140 }
141
142 namespace {
143 // One input connection per detached desktop, for its whole life. KWin
144 // gives the desktop's seat a pointer and keyboard only while such a
145 // connection exists; with none, a game's pointer lock leaves Xwayland
146 // asking for a lock on no pointer, and it exits.
147 std::mutex kept_kwin_input_mutex;
148 std::map<std::string, std::shared_ptr<kwin::connection_t>> kept_kwin_input;
149 } // namespace
150
151 input_t input_private_kwin(const std::string &wayland_socket, const std::string &session_tag) {
152 std::lock_guard lock {kept_kwin_input_mutex};
153 auto &connection = kept_kwin_input[wayland_socket];
154 // A connection KWin has dropped is no use to the next stream.
155 if (connection && !connection->dispatch(std::chrono::milliseconds(0))) {
156 connection.reset();
157 }
158 if (!connection) {
159 std::string error;
160 connection = kwin::connection_t::open({wayland_socket, {}, {}}, false, true, error);
161 if (!connection) {
162 kept_kwin_input.erase(wayland_socket);
163 throw std::runtime_error("Private KWin input: " + error);
164 }
165 }
166 return {new input_raw_t(connection, session_tag)};
167 }
168
169 void forget_private_kwin_input(const std::string &wayland_socket) {
170 std::shared_ptr<kwin::connection_t> connection;
171 {
172 std::lock_guard lock {kept_kwin_input_mutex};
173 const auto found = kept_kwin_input.find(wayland_socket);
174 if (found == kept_kwin_input.end()) {
175 return;
176 }
177 connection = std::move(found->second);
178 kept_kwin_input.erase(found);
179 }
180 }
181} // namespace platf
182
183namespace platf::kwin_input {
184 bool active(const input_raw_t *raw) {
185 return raw && raw->kwin;
186 }
187
188 void move(input_raw_t *raw, int delta_x, int delta_y) {
189 const auto &o = raw->kwin->output();
190 raw->kwin->pointer_motion(double(delta_x) / o.scale, double(delta_y) / o.scale);
191 }
192
193 void move_abs(input_raw_t *raw, float x, float y) {
194 const auto &o = raw->kwin->output();
195 raw->kwin->pointer_absolute(o.x + double(x) / o.scale, o.y + double(y) / o.scale);
196 }
197
198 void button(input_raw_t *raw, int button, bool release) {
199 int code;
200 switch (button) {
201 case BUTTON_LEFT:
202 code = BTN_LEFT;
203 break;
204 case BUTTON_MIDDLE:
205 code = BTN_MIDDLE;
206 break;
207 case BUTTON_RIGHT:
208 code = BTN_RIGHT;
209 break;
210 case BUTTON_X1:
211 code = BTN_SIDE;
212 break;
213 case BUTTON_X2:
214 code = BTN_EXTRA;
215 break;
216 default:
217 return;
218 }
219 raw->kwin->button(code, !release);
220 }
221
222 void scroll(input_raw_t *raw, int high_resolution_distance, bool horizontal) {
223 // Moonlight scrolls up with a positive distance, Wayland axes grow downwards.
224 raw->kwin->axis(horizontal, (horizontal ? 1.0 : -1.0) * double(high_resolution_distance) / 8.0);
225 }
226
227 void key(input_raw_t *raw, std::uint16_t key_code, bool release) {
228 const auto key = std::find_if(key_mappings.begin(), key_mappings.end(), [key_code](const auto &entry) {
229 return entry.second == key_code;
230 });
231 if (key != key_mappings.end()) {
232 raw->kwin->key(key->first, !release);
233 }
234 }
235
236 void unicode(input_raw_t *raw, const char *utf8, int size) {
237 if (!utf8 || size <= 0) {
238 return;
239 }
240 try {
241 const auto text = boost::locale::conv::utf_to_utf<char32_t>(utf8, utf8 + size);
242 for (char32_t character : text) {
243 const std::uint32_t symbol = character <= 0xff ? character : (0x01000000u | character);
244 if (!raw->kwin->keysym(symbol, true) || !raw->kwin->keysym(symbol, false)) {
245 BOOST_LOG(warning) << "Private KWin Unicode input failed";
246 break;
247 }
248 }
249 } catch (const std::exception &error) {
250 BOOST_LOG(warning) << "Private KWin text input: " << error.what();
251 }
252 }
253
254 void touch(client_input_raw_t *raw, const touch_input_t &touch) {
255 auto &kwin = *raw->global->kwin;
256 const auto &o = kwin.output();
257 // touch.x/y are normalized to the streamed output, which is the private
258 // desktop's only one.
259 const double x = o.x + touch.x * o.width / o.scale;
260 const double y = o.y + touch.y * o.height / o.scale;
261 switch (touch.eventType) {
262 case LI_TOUCH_EVENT_DOWN:
263 case LI_TOUCH_EVENT_MOVE:
264 if (raw->kwin_touches.insert(touch.pointerId).second) {
265 kwin.touch_down(touch.pointerId, x, y);
266 } else {
267 kwin.touch_motion(touch.pointerId, x, y);
268 }
269 break;
270 case LI_TOUCH_EVENT_UP:
271 case LI_TOUCH_EVENT_CANCEL:
272 if (raw->kwin_touches.erase(touch.pointerId)) {
273 kwin.touch_up(touch.pointerId);
274 }
275 break;
276 case LI_TOUCH_EVENT_CANCEL_ALL:
277 for (const auto id : raw->kwin_touches) {
278 kwin.touch_up(id);
279 }
280 raw->kwin_touches.clear();
281 break;
282 default:
283 // Hover has no equivalent in the fake-input protocol.
284 break;
285 }
286 }
287} // namespace platf::kwin_input
@ error
Error.
Definition common.h:440
Keyboard, mouse and touch for a private KWin desktop, through its fake-input protocol.
Explicit, generation-bound connections to a private KWin session.
Declarations for logging related functions.
std::optional< std::string > wayland_socket(const std::string &client_uuid)
The name of the Wayland socket the session's compositor created.
Gamepad translation for the Linux libvirtualhid backend.