Home | History | Annotate | Download | only in vnc_server
      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 using cvd::vnc::VirtualInputs;
     26 using vsoc::input_events::InputEventsRegionView;
     27 
     28 namespace {
     29 void AddKeyMappings(std::map<uint32_t, uint32_t>* key_mapping) {
     30   (*key_mapping)[cvd::xk::AltLeft] = KEY_LEFTALT;
     31   (*key_mapping)[cvd::xk::ControlLeft] = KEY_LEFTCTRL;
     32   (*key_mapping)[cvd::xk::ShiftLeft] = KEY_LEFTSHIFT;
     33   (*key_mapping)[cvd::xk::AltRight] = KEY_RIGHTALT;
     34   (*key_mapping)[cvd::xk::ControlRight] = KEY_RIGHTCTRL;
     35   (*key_mapping)[cvd::xk::ShiftRight] = KEY_RIGHTSHIFT;
     36   (*key_mapping)[cvd::xk::MetaLeft] = KEY_LEFTMETA;
     37   (*key_mapping)[cvd::xk::MetaRight] = KEY_RIGHTMETA;
     38   (*key_mapping)[cvd::xk::MultiKey] = KEY_COMPOSE;
     39 
     40   (*key_mapping)[cvd::xk::CapsLock] = KEY_CAPSLOCK;
     41   (*key_mapping)[cvd::xk::NumLock] = KEY_NUMLOCK;
     42   (*key_mapping)[cvd::xk::ScrollLock] = KEY_SCROLLLOCK;
     43 
     44   (*key_mapping)[cvd::xk::BackSpace] = KEY_BACKSPACE;
     45   (*key_mapping)[cvd::xk::Tab] = KEY_TAB;
     46   (*key_mapping)[cvd::xk::Return] = KEY_ENTER;
     47   (*key_mapping)[cvd::xk::Escape] = KEY_ESC;
     48 
     49   (*key_mapping)[' '] = KEY_SPACE;
     50   (*key_mapping)['!'] = KEY_1;
     51   (*key_mapping)['"'] = KEY_APOSTROPHE;
     52   (*key_mapping)['#'] = KEY_3;
     53   (*key_mapping)['$'] = KEY_4;
     54   (*key_mapping)['%'] = KEY_5;
     55   (*key_mapping)['^'] = KEY_6;
     56   (*key_mapping)['&'] = KEY_7;
     57   (*key_mapping)['\''] = KEY_APOSTROPHE;
     58   (*key_mapping)['('] = KEY_9;
     59   (*key_mapping)[')'] = KEY_0;
     60   (*key_mapping)['*'] = KEY_8;
     61   (*key_mapping)['+'] = KEY_EQUAL;
     62   (*key_mapping)[','] = KEY_COMMA;
     63   (*key_mapping)['-'] = KEY_MINUS;
     64   (*key_mapping)['.'] = KEY_DOT;
     65   (*key_mapping)['/'] = KEY_SLASH;
     66   (*key_mapping)['0'] = KEY_0;
     67   (*key_mapping)['1'] = KEY_1;
     68   (*key_mapping)['2'] = KEY_2;
     69   (*key_mapping)['3'] = KEY_3;
     70   (*key_mapping)['4'] = KEY_4;
     71   (*key_mapping)['5'] = KEY_5;
     72   (*key_mapping)['6'] = KEY_6;
     73   (*key_mapping)['7'] = KEY_7;
     74   (*key_mapping)['8'] = KEY_8;
     75   (*key_mapping)['9'] = KEY_9;
     76   (*key_mapping)[':'] = KEY_SEMICOLON;
     77   (*key_mapping)[';'] = KEY_SEMICOLON;
     78   (*key_mapping)['<'] = KEY_COMMA;
     79   (*key_mapping)['='] = KEY_EQUAL;
     80   (*key_mapping)['>'] = KEY_DOT;
     81   (*key_mapping)['?'] = KEY_SLASH;
     82   (*key_mapping)['@'] = KEY_2;
     83   (*key_mapping)['A'] = KEY_A;
     84   (*key_mapping)['B'] = KEY_B;
     85   (*key_mapping)['C'] = KEY_C;
     86   (*key_mapping)['D'] = KEY_D;
     87   (*key_mapping)['E'] = KEY_E;
     88   (*key_mapping)['F'] = KEY_F;
     89   (*key_mapping)['G'] = KEY_G;
     90   (*key_mapping)['H'] = KEY_H;
     91   (*key_mapping)['I'] = KEY_I;
     92   (*key_mapping)['J'] = KEY_J;
     93   (*key_mapping)['K'] = KEY_K;
     94   (*key_mapping)['L'] = KEY_L;
     95   (*key_mapping)['M'] = KEY_M;
     96   (*key_mapping)['N'] = KEY_N;
     97   (*key_mapping)['O'] = KEY_O;
     98   (*key_mapping)['P'] = KEY_P;
     99   (*key_mapping)['Q'] = KEY_Q;
    100   (*key_mapping)['R'] = KEY_R;
    101   (*key_mapping)['S'] = KEY_S;
    102   (*key_mapping)['T'] = KEY_T;
    103   (*key_mapping)['U'] = KEY_U;
    104   (*key_mapping)['V'] = KEY_V;
    105   (*key_mapping)['W'] = KEY_W;
    106   (*key_mapping)['X'] = KEY_X;
    107   (*key_mapping)['Y'] = KEY_Y;
    108   (*key_mapping)['Z'] = KEY_Z;
    109   (*key_mapping)['['] = KEY_LEFTBRACE;
    110   (*key_mapping)['\\'] = KEY_BACKSLASH;
    111   (*key_mapping)[']'] = KEY_RIGHTBRACE;
    112   (*key_mapping)['-'] = KEY_MINUS;
    113   (*key_mapping)['_'] = KEY_MINUS;
    114   (*key_mapping)['`'] = KEY_GRAVE;
    115   (*key_mapping)['a'] = KEY_A;
    116   (*key_mapping)['b'] = KEY_B;
    117   (*key_mapping)['c'] = KEY_C;
    118   (*key_mapping)['d'] = KEY_D;
    119   (*key_mapping)['e'] = KEY_E;
    120   (*key_mapping)['f'] = KEY_F;
    121   (*key_mapping)['g'] = KEY_G;
    122   (*key_mapping)['h'] = KEY_H;
    123   (*key_mapping)['i'] = KEY_I;
    124   (*key_mapping)['j'] = KEY_J;
    125   (*key_mapping)['k'] = KEY_K;
    126   (*key_mapping)['l'] = KEY_L;
    127   (*key_mapping)['m'] = KEY_M;
    128   (*key_mapping)['n'] = KEY_N;
    129   (*key_mapping)['o'] = KEY_O;
    130   (*key_mapping)['p'] = KEY_P;
    131   (*key_mapping)['q'] = KEY_Q;
    132   (*key_mapping)['r'] = KEY_R;
    133   (*key_mapping)['s'] = KEY_S;
    134   (*key_mapping)['t'] = KEY_T;
    135   (*key_mapping)['u'] = KEY_U;
    136   (*key_mapping)['v'] = KEY_V;
    137   (*key_mapping)['w'] = KEY_W;
    138   (*key_mapping)['x'] = KEY_X;
    139   (*key_mapping)['y'] = KEY_Y;
    140   (*key_mapping)['z'] = KEY_Z;
    141   (*key_mapping)['{'] = KEY_LEFTBRACE;
    142   (*key_mapping)['\\'] = KEY_BACKSLASH;
    143   (*key_mapping)['|'] = KEY_BACKSLASH;
    144   (*key_mapping)['}'] = KEY_RIGHTBRACE;
    145   (*key_mapping)['~'] = KEY_GRAVE;
    146 
    147   (*key_mapping)[cvd::xk::F1] = KEY_F1;
    148   (*key_mapping)[cvd::xk::F2] = KEY_F2;
    149   (*key_mapping)[cvd::xk::F3] = KEY_F3;
    150   (*key_mapping)[cvd::xk::F4] = KEY_F4;
    151   (*key_mapping)[cvd::xk::F5] = KEY_F5;
    152   (*key_mapping)[cvd::xk::F6] = KEY_F6;
    153   (*key_mapping)[cvd::xk::F7] = KEY_F7;
    154   (*key_mapping)[cvd::xk::F8] = KEY_F8;
    155   (*key_mapping)[cvd::xk::F9] = KEY_F9;
    156   (*key_mapping)[cvd::xk::F10] = KEY_F10;
    157   (*key_mapping)[cvd::xk::F11] = KEY_F11;
    158   (*key_mapping)[cvd::xk::F12] = KEY_F12;
    159   (*key_mapping)[cvd::xk::F13] = KEY_F13;
    160   (*key_mapping)[cvd::xk::F14] = KEY_F14;
    161   (*key_mapping)[cvd::xk::F15] = KEY_F15;
    162   (*key_mapping)[cvd::xk::F16] = KEY_F16;
    163   (*key_mapping)[cvd::xk::F17] = KEY_F17;
    164   (*key_mapping)[cvd::xk::F18] = KEY_F18;
    165   (*key_mapping)[cvd::xk::F19] = KEY_F19;
    166   (*key_mapping)[cvd::xk::F20] = KEY_F20;
    167   (*key_mapping)[cvd::xk::F21] = KEY_F21;
    168   (*key_mapping)[cvd::xk::F22] = KEY_F22;
    169   (*key_mapping)[cvd::xk::F23] = KEY_F23;
    170   (*key_mapping)[cvd::xk::F24] = KEY_F24;
    171 
    172   (*key_mapping)[cvd::xk::Keypad0] = KEY_KP0;
    173   (*key_mapping)[cvd::xk::Keypad1] = KEY_KP1;
    174   (*key_mapping)[cvd::xk::Keypad2] = KEY_KP2;
    175   (*key_mapping)[cvd::xk::Keypad3] = KEY_KP3;
    176   (*key_mapping)[cvd::xk::Keypad4] = KEY_KP4;
    177   (*key_mapping)[cvd::xk::Keypad5] = KEY_KP5;
    178   (*key_mapping)[cvd::xk::Keypad6] = KEY_KP6;
    179   (*key_mapping)[cvd::xk::Keypad7] = KEY_KP7;
    180   (*key_mapping)[cvd::xk::Keypad8] = KEY_KP8;
    181   (*key_mapping)[cvd::xk::Keypad9] = KEY_KP9;
    182   (*key_mapping)[cvd::xk::KeypadMultiply] = KEY_KPASTERISK;
    183   (*key_mapping)[cvd::xk::KeypadSubtract] = KEY_KPMINUS;
    184   (*key_mapping)[cvd::xk::KeypadAdd] = KEY_KPPLUS;
    185   (*key_mapping)[cvd::xk::KeypadDecimal] = KEY_KPDOT;
    186   (*key_mapping)[cvd::xk::KeypadEnter] = KEY_KPENTER;
    187   (*key_mapping)[cvd::xk::KeypadDivide] = KEY_KPSLASH;
    188   (*key_mapping)[cvd::xk::KeypadEqual] = KEY_KPEQUAL;
    189   (*key_mapping)[cvd::xk::PlusMinus] = KEY_KPPLUSMINUS;
    190 
    191   (*key_mapping)[cvd::xk::SysReq] = KEY_SYSRQ;
    192   (*key_mapping)[cvd::xk::LineFeed] = KEY_LINEFEED;
    193   (*key_mapping)[cvd::xk::Home] = KEY_HOME;
    194   (*key_mapping)[cvd::xk::Up] = KEY_UP;
    195   (*key_mapping)[cvd::xk::PageUp] = KEY_PAGEUP;
    196   (*key_mapping)[cvd::xk::Left] = KEY_LEFT;
    197   (*key_mapping)[cvd::xk::Right] = KEY_RIGHT;
    198   (*key_mapping)[cvd::xk::End] = KEY_END;
    199   (*key_mapping)[cvd::xk::Down] = KEY_DOWN;
    200   (*key_mapping)[cvd::xk::PageDown] = KEY_PAGEDOWN;
    201   (*key_mapping)[cvd::xk::Insert] = KEY_INSERT;
    202   (*key_mapping)[cvd::xk::Delete] = KEY_DELETE;
    203   (*key_mapping)[cvd::xk::Pause] = KEY_PAUSE;
    204   (*key_mapping)[cvd::xk::KeypadSeparator] = KEY_KPCOMMA;
    205   (*key_mapping)[cvd::xk::Yen] = KEY_YEN;
    206   (*key_mapping)[cvd::xk::Cancel] = KEY_STOP;
    207   (*key_mapping)[cvd::xk::Redo] = KEY_AGAIN;
    208   (*key_mapping)[cvd::xk::Undo] = KEY_UNDO;
    209   (*key_mapping)[cvd::xk::Find] = KEY_FIND;
    210   (*key_mapping)[cvd::xk::Print] = KEY_PRINT;
    211   (*key_mapping)[cvd::xk::VolumeDown] = KEY_VOLUMEDOWN;
    212   (*key_mapping)[cvd::xk::Mute] = KEY_MUTE;
    213   (*key_mapping)[cvd::xk::VolumeUp] = KEY_VOLUMEUP;
    214   (*key_mapping)[cvd::xk::Menu] = KEY_MENU;
    215   (*key_mapping)[cvd::xk::VNCMenu] = KEY_MENU;
    216 }
    217 }  // namespace
    218 
    219 VirtualInputs::VirtualInputs()
    220   : input_events_region_view_{
    221       vsoc::input_events::InputEventsRegionView::GetInstance(
    222           vsoc::GetDomain().c_str())} {
    223   if (!input_events_region_view_) {
    224     LOG(FATAL) << "Failed to open Input events region view";
    225   }
    226   AddKeyMappings(&keymapping_);
    227 }
    228 
    229 void VirtualInputs::GenerateKeyPressEvent(int key_code, bool down) {
    230   if (keymapping_.count(key_code)) {
    231     input_events_region_view_->HandleKeyboardEvent(down, keymapping_[key_code]);
    232   } else {
    233     LOG(INFO) << "Unknown keycode" << key_code;
    234   }
    235 }
    236 
    237 void VirtualInputs::PressPowerButton(bool down) {
    238   input_events_region_view_->HandlePowerButtonEvent(down);
    239 }
    240 
    241 void VirtualInputs::HandlePointerEvent(bool touch_down, int x, int y) {
    242   input_events_region_view_->HandleSingleTouchEvent(touch_down, x, y);
    243 }
    244