blob: 8753735c1d66833a37bd20ede9077eb70a713701 [file] [log] [blame]
Jorge E. Moreiraae2fcae2017-11-28 11:16:59 -08001/*
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>
Cody Schuffelen038d4d92019-11-04 15:09:03 -080021#include <linux/virtio_input.h>
Jorge E. Moreiraae2fcae2017-11-28 11:16:59 -080022
23#include <thread>
24
Cody Schuffelen038d4d92019-11-04 15:09:03 -080025#include <gflags/gflags.h>
Jorge E. Moreiraae2fcae2017-11-28 11:16:59 -080026#include "log/log.h"
Cody Schuffelen038d4d92019-11-04 15:09:03 -080027#include <glog/logging.h>
Jorge E. Moreiraae2fcae2017-11-28 11:16:59 -080028
Cody Schuffelen038d4d92019-11-04 15:09:03 -080029#include "common/libs/fs/shared_fd.h"
30#include "common/libs/device_config/device_config.h"
Cody Schuffelen134ff032019-11-22 00:25:32 -080031#include "common/vsoc/lib/input_events_region_view.h"
Jorge E. Moreiraae2fcae2017-11-28 11:16:59 -080032
Jorge E. Moreiraae2fcae2017-11-28 11:16:59 -080033using vsoc::input_events::InputEvent;
Jorge E. Moreiraae2fcae2017-11-28 11:16:59 -080034using vsoc_input_service::VirtualDeviceBase;
35using vsoc_input_service::VirtualKeyboard;
36using vsoc_input_service::VirtualPowerButton;
37using vsoc_input_service::VirtualTouchScreen;
38using vsoc_input_service::VSoCInputService;
39
Cody Schuffelen038d4d92019-11-04 15:09:03 -080040DEFINE_uint32(keyboard_port, 0, "keyboard vsock port");
41DEFINE_uint32(touch_port, 0, "keyboard vsock port");
42
Jorge E. Moreiraae2fcae2017-11-28 11:16:59 -080043namespace {
44
Jorge E. Moreiraae2fcae2017-11-28 11:16:59 -080045void EventLoop(std::shared_ptr<VirtualDeviceBase> device,
Cody Schuffelen038d4d92019-11-04 15:09:03 -080046 std::function<InputEvent()> next_event) {
Jorge E. Moreiraae2fcae2017-11-28 11:16:59 -080047 while (1) {
Cody Schuffelen038d4d92019-11-04 15:09:03 -080048 InputEvent event = next_event();
49 device->EmitEvent(event.type, event.code, event.value);
Jorge E. Moreiraae2fcae2017-11-28 11:16:59 -080050 }
51}
52
53} // namespace
54
55bool VSoCInputService::SetUpDevices() {
56 virtual_power_button_.reset(new VirtualPowerButton());
57 if (!virtual_power_button_->SetUp()) {
58 return false;
59 }
60 virtual_keyboard_.reset(new VirtualKeyboard());
61 if (!virtual_keyboard_->SetUp()) {
62 return false;
63 }
64
Cody Schuffelen038d4d92019-11-04 15:09:03 -080065 auto config = cvd::DeviceConfig::Get();
66 if (!config) {
67 LOG(ERROR) << "Failed to open device config";
Jorge E. Moreiraae2fcae2017-11-28 11:16:59 -080068 return false;
69 }
70
71 virtual_touchscreen_.reset(
Cody Schuffelen038d4d92019-11-04 15:09:03 -080072 new VirtualTouchScreen(config->screen_x_res(), config->screen_y_res()));
Jorge E. Moreiraae2fcae2017-11-28 11:16:59 -080073 if (!virtual_touchscreen_->SetUp()) {
74 return false;
75 }
76
77 return true;
78}
79
80bool VSoCInputService::ProcessEvents() {
Cody Schuffelen038d4d92019-11-04 15:09:03 -080081 cvd::SharedFD keyboard_fd;
82 cvd::SharedFD touch_fd;
83
84 LOG(INFO) << "Connecting to the keyboard at " << FLAGS_keyboard_port;
85 if (FLAGS_keyboard_port) {
86 keyboard_fd = cvd::SharedFD::VsockClient(2, FLAGS_keyboard_port, SOCK_STREAM);
87 if (!keyboard_fd->IsOpen()) {
88 LOG(ERROR) << "Could not connect to the keyboard at vsock:2:" << FLAGS_keyboard_port;
89 }
90 LOG(INFO) << "Connected to keyboard";
91 }
92 LOG(INFO) << "Connecting to the touchscreen at " << FLAGS_keyboard_port;
93 if (FLAGS_touch_port) {
94 touch_fd = cvd::SharedFD::VsockClient(2, FLAGS_touch_port, SOCK_STREAM);
95 if (!touch_fd->IsOpen()) {
96 LOG(ERROR) << "Could not connect to the touch at vsock:2:" << FLAGS_touch_port;
97 }
98 LOG(INFO) << "Connected to touch";
99 }
Jorge E. Moreiraae2fcae2017-11-28 11:16:59 -0800100
101 // Start device threads
Cody Schuffelen038d4d92019-11-04 15:09:03 -0800102 std::thread screen_thread([this, touch_fd]() {
103 EventLoop(virtual_touchscreen_, [touch_fd]() {
104 struct virtio_input_event event;
105 if (touch_fd->Read(&event, sizeof(event)) != sizeof(event)) {
106 LOG(FATAL) << "Could not read touch event: " << touch_fd->StrError();
107 }
108 return InputEvent {
109 .type = event.type,
110 .code = event.code,
111 .value = event.value,
112 };
Jorge E. Moreiraae2fcae2017-11-28 11:16:59 -0800113 });
114 });
Cody Schuffelen038d4d92019-11-04 15:09:03 -0800115 std::thread keyboard_thread([this, keyboard_fd]() {
116 EventLoop(virtual_keyboard_, [keyboard_fd]() {
117 struct virtio_input_event event;
118 if (keyboard_fd->Read(&event, sizeof(event)) != sizeof(event)) {
119 LOG(FATAL) << "Could not read keyboard event: " << keyboard_fd->StrError();
120 }
121 return InputEvent {
122 .type = event.type,
123 .code = event.code,
124 .value = event.value,
125 };
126 });
Jorge E. Moreiraae2fcae2017-11-28 11:16:59 -0800127 });
128
129 screen_thread.join();
130 keyboard_thread.join();
Jorge E. Moreiraae2fcae2017-11-28 11:16:59 -0800131
132 // Should never return
133 return false;
134}