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