• 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 "vsoc_input_service.h"
18 
19 #include <linux/input.h>
20 #include <linux/uinput.h>
21 #include <linux/virtio_input.h>
22 
23 #include <thread>
24 
25 #include <android-base/logging.h>
26 #include <gflags/gflags.h>
27 #include <log/log.h>
28 
29 #include "common/libs/fs/shared_fd.h"
30 #include "common/libs/device_config/device_config.h"
31 
32 using cuttlefish::input_events::InputEvent;
33 using cuttlefish_input_service::VirtualDeviceBase;
34 using cuttlefish_input_service::VirtualKeyboard;
35 using cuttlefish_input_service::VirtualPowerButton;
36 using cuttlefish_input_service::VirtualTouchScreen;
37 using cuttlefish_input_service::VSoCInputService;
38 
39 DEFINE_uint32(keyboard_port, 0, "keyboard vsock port");
40 DEFINE_uint32(touch_port, 0, "keyboard vsock port");
41 
42 namespace {
43 
EventLoop(std::shared_ptr<VirtualDeviceBase> device,std::function<InputEvent ()> next_event)44 void EventLoop(std::shared_ptr<VirtualDeviceBase> device,
45                std::function<InputEvent()> next_event) {
46   while (1) {
47     InputEvent event = next_event();
48     device->EmitEvent(event.type, event.code, event.value);
49   }
50 }
51 
52 }  // namespace
53 
SetUpDevices()54 bool VSoCInputService::SetUpDevices() {
55   virtual_power_button_.reset(new VirtualPowerButton());
56   if (!virtual_power_button_->SetUp()) {
57     return false;
58   }
59   virtual_keyboard_.reset(new VirtualKeyboard());
60   if (!virtual_keyboard_->SetUp()) {
61     return false;
62   }
63 
64   auto config_helper = cuttlefish::DeviceConfigHelper::Get();
65   if (!config_helper) {
66     LOG(ERROR) << "Failed to open device config";
67     return false;
68   }
69 
70   const auto& device_config = config_helper->GetDeviceConfig();
71   CHECK_GE(device_config.display_config_size(), 1);
72   const auto& display_config = device_config.display_config(0);
73 
74   virtual_touchscreen_.reset(
75       new VirtualTouchScreen(display_config.width(),
76                              display_config.height()));
77   if (!virtual_touchscreen_->SetUp()) {
78     return false;
79   }
80 
81   return true;
82 }
83 
ProcessEvents()84 bool VSoCInputService::ProcessEvents() {
85   cuttlefish::SharedFD keyboard_fd;
86   cuttlefish::SharedFD touch_fd;
87 
88   LOG(INFO) << "Connecting to the keyboard at " << FLAGS_keyboard_port;
89   if (FLAGS_keyboard_port) {
90     keyboard_fd = cuttlefish::SharedFD::VsockClient(2, FLAGS_keyboard_port, SOCK_STREAM);
91     if (!keyboard_fd->IsOpen()) {
92       LOG(ERROR) << "Could not connect to the keyboard at vsock:2:" << FLAGS_keyboard_port;
93     }
94     LOG(INFO) << "Connected to keyboard";
95   }
96   LOG(INFO) << "Connecting to the touchscreen at " << FLAGS_keyboard_port;
97   if (FLAGS_touch_port) {
98     touch_fd = cuttlefish::SharedFD::VsockClient(2, FLAGS_touch_port, SOCK_STREAM);
99     if (!touch_fd->IsOpen()) {
100       LOG(ERROR) << "Could not connect to the touch at vsock:2:" << FLAGS_touch_port;
101     }
102     LOG(INFO) << "Connected to touch";
103   }
104 
105   // Start device threads
106   std::thread screen_thread([this, touch_fd]() {
107     EventLoop(virtual_touchscreen_, [touch_fd]() {
108       struct virtio_input_event event;
109       if (touch_fd->Read(&event, sizeof(event)) != sizeof(event)) {
110         LOG(FATAL) << "Could not read touch event: " << touch_fd->StrError();
111       }
112       return InputEvent {
113         .type = event.type,
114         .code = event.code,
115         .value = event.value,
116       };
117     });
118   });
119   std::thread keyboard_thread([this, keyboard_fd]() {
120     EventLoop(virtual_keyboard_, [keyboard_fd]() {
121       struct virtio_input_event event;
122       if (keyboard_fd->Read(&event, sizeof(event)) != sizeof(event)) {
123         LOG(FATAL) << "Could not read keyboard event: " << keyboard_fd->StrError();
124       }
125       return InputEvent {
126         .type = event.type,
127         .code = event.code,
128         .value = event.value,
129       };
130     });
131   });
132 
133   screen_thread.join();
134   keyboard_thread.join();
135 
136   // Should never return
137   return false;
138 }
139