1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "host/frontend/vnc_server/virtual_inputs.h"
18 #include <gflags/gflags.h>
19 #include <android-base/logging.h>
20 #include <linux/input.h>
21 #include <linux/uinput.h>
22
23 #include <cstdint>
24 #include <mutex>
25 #include <thread>
26 #include "keysyms.h"
27
28 #include <common/libs/fs/shared_select.h>
29 #include <host/libs/config/cuttlefish_config.h>
30
31 using cuttlefish::vnc::VirtualInputs;
32
33 DEFINE_int32(touch_fd, -1,
34 "A fd for a socket where to accept touch connections");
35
36 DEFINE_int32(keyboard_fd, -1,
37 "A fd for a socket where to accept keyboard connections");
38
39 DEFINE_bool(write_virtio_input, false,
40 "Whether to write the virtio_input struct over the socket");
41
42 namespace {
43 // Necessary to define here as the virtio_input.h header is not available
44 // in the host glibc.
45 struct virtio_input_event {
46 std::uint16_t type;
47 std::uint16_t code;
48 std::int32_t value;
49 };
50
AddKeyMappings(std::map<uint32_t,uint16_t> * key_mapping)51 void AddKeyMappings(std::map<uint32_t, uint16_t>* key_mapping) {
52 (*key_mapping)[cuttlefish::xk::AltLeft] = KEY_LEFTALT;
53 (*key_mapping)[cuttlefish::xk::ControlLeft] = KEY_LEFTCTRL;
54 (*key_mapping)[cuttlefish::xk::ShiftLeft] = KEY_LEFTSHIFT;
55 (*key_mapping)[cuttlefish::xk::AltRight] = KEY_RIGHTALT;
56 (*key_mapping)[cuttlefish::xk::ControlRight] = KEY_RIGHTCTRL;
57 (*key_mapping)[cuttlefish::xk::ShiftRight] = KEY_RIGHTSHIFT;
58 (*key_mapping)[cuttlefish::xk::MetaLeft] = KEY_LEFTMETA;
59 (*key_mapping)[cuttlefish::xk::MetaRight] = KEY_RIGHTMETA;
60 (*key_mapping)[cuttlefish::xk::MultiKey] = KEY_COMPOSE;
61
62 (*key_mapping)[cuttlefish::xk::CapsLock] = KEY_CAPSLOCK;
63 (*key_mapping)[cuttlefish::xk::NumLock] = KEY_NUMLOCK;
64 (*key_mapping)[cuttlefish::xk::ScrollLock] = KEY_SCROLLLOCK;
65
66 (*key_mapping)[cuttlefish::xk::BackSpace] = KEY_BACKSPACE;
67 (*key_mapping)[cuttlefish::xk::Tab] = KEY_TAB;
68 (*key_mapping)[cuttlefish::xk::Return] = KEY_ENTER;
69 (*key_mapping)[cuttlefish::xk::Escape] = KEY_ESC;
70
71 (*key_mapping)[' '] = KEY_SPACE;
72 (*key_mapping)['!'] = KEY_1;
73 (*key_mapping)['"'] = KEY_APOSTROPHE;
74 (*key_mapping)['#'] = KEY_3;
75 (*key_mapping)['$'] = KEY_4;
76 (*key_mapping)['%'] = KEY_5;
77 (*key_mapping)['^'] = KEY_6;
78 (*key_mapping)['&'] = KEY_7;
79 (*key_mapping)['\''] = KEY_APOSTROPHE;
80 (*key_mapping)['('] = KEY_9;
81 (*key_mapping)[')'] = KEY_0;
82 (*key_mapping)['*'] = KEY_8;
83 (*key_mapping)['+'] = KEY_EQUAL;
84 (*key_mapping)[','] = KEY_COMMA;
85 (*key_mapping)['-'] = KEY_MINUS;
86 (*key_mapping)['.'] = KEY_DOT;
87 (*key_mapping)['/'] = KEY_SLASH;
88 (*key_mapping)['0'] = KEY_0;
89 (*key_mapping)['1'] = KEY_1;
90 (*key_mapping)['2'] = KEY_2;
91 (*key_mapping)['3'] = KEY_3;
92 (*key_mapping)['4'] = KEY_4;
93 (*key_mapping)['5'] = KEY_5;
94 (*key_mapping)['6'] = KEY_6;
95 (*key_mapping)['7'] = KEY_7;
96 (*key_mapping)['8'] = KEY_8;
97 (*key_mapping)['9'] = KEY_9;
98 (*key_mapping)[':'] = KEY_SEMICOLON;
99 (*key_mapping)[';'] = KEY_SEMICOLON;
100 (*key_mapping)['<'] = KEY_COMMA;
101 (*key_mapping)['='] = KEY_EQUAL;
102 (*key_mapping)['>'] = KEY_DOT;
103 (*key_mapping)['?'] = KEY_SLASH;
104 (*key_mapping)['@'] = KEY_2;
105 (*key_mapping)['A'] = KEY_A;
106 (*key_mapping)['B'] = KEY_B;
107 (*key_mapping)['C'] = KEY_C;
108 (*key_mapping)['D'] = KEY_D;
109 (*key_mapping)['E'] = KEY_E;
110 (*key_mapping)['F'] = KEY_F;
111 (*key_mapping)['G'] = KEY_G;
112 (*key_mapping)['H'] = KEY_H;
113 (*key_mapping)['I'] = KEY_I;
114 (*key_mapping)['J'] = KEY_J;
115 (*key_mapping)['K'] = KEY_K;
116 (*key_mapping)['L'] = KEY_L;
117 (*key_mapping)['M'] = KEY_M;
118 (*key_mapping)['N'] = KEY_N;
119 (*key_mapping)['O'] = KEY_O;
120 (*key_mapping)['P'] = KEY_P;
121 (*key_mapping)['Q'] = KEY_Q;
122 (*key_mapping)['R'] = KEY_R;
123 (*key_mapping)['S'] = KEY_S;
124 (*key_mapping)['T'] = KEY_T;
125 (*key_mapping)['U'] = KEY_U;
126 (*key_mapping)['V'] = KEY_V;
127 (*key_mapping)['W'] = KEY_W;
128 (*key_mapping)['X'] = KEY_X;
129 (*key_mapping)['Y'] = KEY_Y;
130 (*key_mapping)['Z'] = KEY_Z;
131 (*key_mapping)['['] = KEY_LEFTBRACE;
132 (*key_mapping)['\\'] = KEY_BACKSLASH;
133 (*key_mapping)[']'] = KEY_RIGHTBRACE;
134 (*key_mapping)['-'] = KEY_MINUS;
135 (*key_mapping)['_'] = KEY_MINUS;
136 (*key_mapping)['`'] = KEY_GRAVE;
137 (*key_mapping)['a'] = KEY_A;
138 (*key_mapping)['b'] = KEY_B;
139 (*key_mapping)['c'] = KEY_C;
140 (*key_mapping)['d'] = KEY_D;
141 (*key_mapping)['e'] = KEY_E;
142 (*key_mapping)['f'] = KEY_F;
143 (*key_mapping)['g'] = KEY_G;
144 (*key_mapping)['h'] = KEY_H;
145 (*key_mapping)['i'] = KEY_I;
146 (*key_mapping)['j'] = KEY_J;
147 (*key_mapping)['k'] = KEY_K;
148 (*key_mapping)['l'] = KEY_L;
149 (*key_mapping)['m'] = KEY_M;
150 (*key_mapping)['n'] = KEY_N;
151 (*key_mapping)['o'] = KEY_O;
152 (*key_mapping)['p'] = KEY_P;
153 (*key_mapping)['q'] = KEY_Q;
154 (*key_mapping)['r'] = KEY_R;
155 (*key_mapping)['s'] = KEY_S;
156 (*key_mapping)['t'] = KEY_T;
157 (*key_mapping)['u'] = KEY_U;
158 (*key_mapping)['v'] = KEY_V;
159 (*key_mapping)['w'] = KEY_W;
160 (*key_mapping)['x'] = KEY_X;
161 (*key_mapping)['y'] = KEY_Y;
162 (*key_mapping)['z'] = KEY_Z;
163 (*key_mapping)['{'] = KEY_LEFTBRACE;
164 (*key_mapping)['\\'] = KEY_BACKSLASH;
165 (*key_mapping)['|'] = KEY_BACKSLASH;
166 (*key_mapping)['}'] = KEY_RIGHTBRACE;
167 (*key_mapping)['~'] = KEY_GRAVE;
168
169 (*key_mapping)[cuttlefish::xk::F1] = KEY_F1;
170 (*key_mapping)[cuttlefish::xk::F2] = KEY_F2;
171 (*key_mapping)[cuttlefish::xk::F3] = KEY_F3;
172 (*key_mapping)[cuttlefish::xk::F4] = KEY_F4;
173 (*key_mapping)[cuttlefish::xk::F5] = KEY_F5;
174 (*key_mapping)[cuttlefish::xk::F6] = KEY_F6;
175 (*key_mapping)[cuttlefish::xk::F7] = KEY_F7;
176 (*key_mapping)[cuttlefish::xk::F8] = KEY_F8;
177 (*key_mapping)[cuttlefish::xk::F9] = KEY_F9;
178 (*key_mapping)[cuttlefish::xk::F10] = KEY_F10;
179 (*key_mapping)[cuttlefish::xk::F11] = KEY_F11;
180 (*key_mapping)[cuttlefish::xk::F12] = KEY_F12;
181 (*key_mapping)[cuttlefish::xk::F13] = KEY_F13;
182 (*key_mapping)[cuttlefish::xk::F14] = KEY_F14;
183 (*key_mapping)[cuttlefish::xk::F15] = KEY_F15;
184 (*key_mapping)[cuttlefish::xk::F16] = KEY_F16;
185 (*key_mapping)[cuttlefish::xk::F17] = KEY_F17;
186 (*key_mapping)[cuttlefish::xk::F18] = KEY_F18;
187 (*key_mapping)[cuttlefish::xk::F19] = KEY_F19;
188 (*key_mapping)[cuttlefish::xk::F20] = KEY_F20;
189 (*key_mapping)[cuttlefish::xk::F21] = KEY_F21;
190 (*key_mapping)[cuttlefish::xk::F22] = KEY_F22;
191 (*key_mapping)[cuttlefish::xk::F23] = KEY_F23;
192 (*key_mapping)[cuttlefish::xk::F24] = KEY_F24;
193
194 (*key_mapping)[cuttlefish::xk::Keypad0] = KEY_KP0;
195 (*key_mapping)[cuttlefish::xk::Keypad1] = KEY_KP1;
196 (*key_mapping)[cuttlefish::xk::Keypad2] = KEY_KP2;
197 (*key_mapping)[cuttlefish::xk::Keypad3] = KEY_KP3;
198 (*key_mapping)[cuttlefish::xk::Keypad4] = KEY_KP4;
199 (*key_mapping)[cuttlefish::xk::Keypad5] = KEY_KP5;
200 (*key_mapping)[cuttlefish::xk::Keypad6] = KEY_KP6;
201 (*key_mapping)[cuttlefish::xk::Keypad7] = KEY_KP7;
202 (*key_mapping)[cuttlefish::xk::Keypad8] = KEY_KP8;
203 (*key_mapping)[cuttlefish::xk::Keypad9] = KEY_KP9;
204 (*key_mapping)[cuttlefish::xk::KeypadMultiply] = KEY_KPASTERISK;
205 (*key_mapping)[cuttlefish::xk::KeypadSubtract] = KEY_KPMINUS;
206 (*key_mapping)[cuttlefish::xk::KeypadAdd] = KEY_KPPLUS;
207 (*key_mapping)[cuttlefish::xk::KeypadDecimal] = KEY_KPDOT;
208 (*key_mapping)[cuttlefish::xk::KeypadEnter] = KEY_KPENTER;
209 (*key_mapping)[cuttlefish::xk::KeypadDivide] = KEY_KPSLASH;
210 (*key_mapping)[cuttlefish::xk::KeypadEqual] = KEY_KPEQUAL;
211 (*key_mapping)[cuttlefish::xk::PlusMinus] = KEY_KPPLUSMINUS;
212
213 (*key_mapping)[cuttlefish::xk::SysReq] = KEY_SYSRQ;
214 (*key_mapping)[cuttlefish::xk::LineFeed] = KEY_LINEFEED;
215 (*key_mapping)[cuttlefish::xk::Home] = KEY_HOME;
216 (*key_mapping)[cuttlefish::xk::Up] = KEY_UP;
217 (*key_mapping)[cuttlefish::xk::PageUp] = KEY_PAGEUP;
218 (*key_mapping)[cuttlefish::xk::Left] = KEY_LEFT;
219 (*key_mapping)[cuttlefish::xk::Right] = KEY_RIGHT;
220 (*key_mapping)[cuttlefish::xk::End] = KEY_END;
221 (*key_mapping)[cuttlefish::xk::Down] = KEY_DOWN;
222 (*key_mapping)[cuttlefish::xk::PageDown] = KEY_PAGEDOWN;
223 (*key_mapping)[cuttlefish::xk::Insert] = KEY_INSERT;
224 (*key_mapping)[cuttlefish::xk::Delete] = KEY_DELETE;
225 (*key_mapping)[cuttlefish::xk::Pause] = KEY_PAUSE;
226 (*key_mapping)[cuttlefish::xk::KeypadSeparator] = KEY_KPCOMMA;
227 (*key_mapping)[cuttlefish::xk::Yen] = KEY_YEN;
228 (*key_mapping)[cuttlefish::xk::Cancel] = KEY_STOP;
229 (*key_mapping)[cuttlefish::xk::Redo] = KEY_AGAIN;
230 (*key_mapping)[cuttlefish::xk::Undo] = KEY_UNDO;
231 (*key_mapping)[cuttlefish::xk::Find] = KEY_FIND;
232 (*key_mapping)[cuttlefish::xk::Print] = KEY_PRINT;
233 (*key_mapping)[cuttlefish::xk::VolumeDown] = KEY_VOLUMEDOWN;
234 (*key_mapping)[cuttlefish::xk::Mute] = KEY_MUTE;
235 (*key_mapping)[cuttlefish::xk::VolumeUp] = KEY_VOLUMEUP;
236 (*key_mapping)[cuttlefish::xk::Menu] = KEY_MENU;
237 (*key_mapping)[cuttlefish::xk::VNCMenu] = KEY_MENU;
238 }
239
InitInputEvent(struct input_event * evt,uint16_t type,uint16_t code,int32_t value)240 void InitInputEvent(struct input_event* evt, uint16_t type, uint16_t code,
241 int32_t value) {
242 evt->type = type;
243 evt->code = code;
244 evt->value = value;
245 }
246
247 } // namespace
248
249 class SocketVirtualInputs : public VirtualInputs {
250 public:
SocketVirtualInputs()251 SocketVirtualInputs()
252 : client_connector_([this]() { ClientConnectorLoop(); }) {}
253
GenerateKeyPressEvent(int key_code,bool down)254 void GenerateKeyPressEvent(int key_code, bool down) override {
255 struct input_event events[2];
256 InitInputEvent(&events[0], EV_KEY, keymapping_[key_code], down);
257 InitInputEvent(&events[1], EV_SYN, 0, 0);
258
259 SendEvents(keyboard_socket_, events);
260 }
261
PressPowerButton(bool down)262 void PressPowerButton(bool down) override {
263 struct input_event events[2];
264 InitInputEvent(&events[0], EV_KEY, KEY_POWER, down);
265 InitInputEvent(&events[1], EV_SYN, 0, 0);
266
267 SendEvents(keyboard_socket_, events);
268 }
269
HandlePointerEvent(bool touch_down,int x,int y)270 void HandlePointerEvent(bool touch_down, int x, int y) override {
271 // TODO(b/124121375): Use multitouch when available
272 struct input_event events[4];
273 InitInputEvent(&events[0], EV_ABS, ABS_X, x);
274 InitInputEvent(&events[1], EV_ABS, ABS_Y, y);
275 InitInputEvent(&events[2], EV_KEY, BTN_TOUCH, touch_down);
276 InitInputEvent(&events[3], EV_SYN, 0, 0);
277
278 SendEvents(touch_socket_, events);
279 }
280
281 private:
282 template<size_t num_events>
SendEvents(cuttlefish::SharedFD socket,struct input_event (& event_buffer)[num_events])283 void SendEvents(cuttlefish::SharedFD socket, struct input_event (&event_buffer)[num_events]) {
284 std::lock_guard<std::mutex> lock(socket_mutex_);
285 if (!socket->IsOpen()) {
286 // This is unlikely as it would only happen between the start of the vnc
287 // server and the connection of the VMM to the socket.
288 // If it happens, just drop the events as the VM is not yet ready to
289 // handle it.
290 return;
291 }
292
293 if (FLAGS_write_virtio_input) {
294 struct virtio_input_event virtio_events[num_events];
295 for (size_t i = 0; i < num_events; i++) {
296 virtio_events[i] = (struct virtio_input_event) {
297 .type = event_buffer[i].type,
298 .code = event_buffer[i].code,
299 .value = event_buffer[i].value,
300 };
301 }
302 auto ret = socket->Write(virtio_events, sizeof(virtio_events));
303 if (ret < 0) {
304 LOG(ERROR) << "Error sending input events: " << socket->StrError();
305 }
306 } else {
307 auto ret = socket->Write(event_buffer, sizeof(event_buffer));
308 if (ret < 0) {
309 LOG(ERROR) << "Error sending input events: " << socket->StrError();
310 }
311 }
312 }
313
ClientConnectorLoop()314 void ClientConnectorLoop() {
315 auto touch_server = cuttlefish::SharedFD::Dup(FLAGS_touch_fd);
316 close(FLAGS_touch_fd);
317 FLAGS_touch_fd = -1;
318
319 auto keyboard_server = cuttlefish::SharedFD::Dup(FLAGS_keyboard_fd);
320 close(FLAGS_keyboard_fd);
321 FLAGS_keyboard_fd = -1;
322 LOG(DEBUG) << "Input socket host accepting connections...";
323
324 while (1) {
325 cuttlefish::SharedFDSet read_set;
326 read_set.Set(touch_server);
327 read_set.Set(keyboard_server);
328 cuttlefish::Select(&read_set, nullptr, nullptr, nullptr);
329 {
330 std::lock_guard<std::mutex> lock(socket_mutex_);
331 if (read_set.IsSet(touch_server)) {
332 touch_socket_ = cuttlefish::SharedFD::Accept(*touch_server);
333 LOG(DEBUG) << "connected to touch";
334 }
335 if (read_set.IsSet(keyboard_server)) {
336 keyboard_socket_ = cuttlefish::SharedFD::Accept(*keyboard_server);
337 LOG(DEBUG) << "connected to keyboard";
338 }
339 }
340 }
341 }
342 cuttlefish::SharedFD touch_socket_;
343 cuttlefish::SharedFD keyboard_socket_;
344 std::thread client_connector_;
345 std::mutex socket_mutex_;
346 };
347
VirtualInputs()348 VirtualInputs::VirtualInputs() { AddKeyMappings(&keymapping_); }
349
Get()350 VirtualInputs* VirtualInputs::Get() {
351 return new SocketVirtualInputs();
352 }
353