Jorge E. Moreira | ae2fcae | 2017-11-28 11:16:59 -0800 | [diff] [blame] | 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> |
Cody Schuffelen | 038d4d9 | 2019-11-04 15:09:03 -0800 | [diff] [blame] | 21 | #include <linux/virtio_input.h> |
Jorge E. Moreira | ae2fcae | 2017-11-28 11:16:59 -0800 | [diff] [blame] | 22 | |
| 23 | #include <thread> |
| 24 | |
Cody Schuffelen | 038d4d9 | 2019-11-04 15:09:03 -0800 | [diff] [blame] | 25 | #include <gflags/gflags.h> |
Jorge E. Moreira | ae2fcae | 2017-11-28 11:16:59 -0800 | [diff] [blame] | 26 | #include "log/log.h" |
Cody Schuffelen | 038d4d9 | 2019-11-04 15:09:03 -0800 | [diff] [blame] | 27 | #include <glog/logging.h> |
Jorge E. Moreira | ae2fcae | 2017-11-28 11:16:59 -0800 | [diff] [blame] | 28 | |
Cody Schuffelen | 038d4d9 | 2019-11-04 15:09:03 -0800 | [diff] [blame] | 29 | #include "common/libs/fs/shared_fd.h" |
| 30 | #include "common/libs/device_config/device_config.h" |
Cody Schuffelen | 134ff03 | 2019-11-22 00:25:32 -0800 | [diff] [blame^] | 31 | #include "common/vsoc/lib/input_events_region_view.h" |
Jorge E. Moreira | ae2fcae | 2017-11-28 11:16:59 -0800 | [diff] [blame] | 32 | |
Jorge E. Moreira | ae2fcae | 2017-11-28 11:16:59 -0800 | [diff] [blame] | 33 | using vsoc::input_events::InputEvent; |
Jorge E. Moreira | ae2fcae | 2017-11-28 11:16:59 -0800 | [diff] [blame] | 34 | using vsoc_input_service::VirtualDeviceBase; |
| 35 | using vsoc_input_service::VirtualKeyboard; |
| 36 | using vsoc_input_service::VirtualPowerButton; |
| 37 | using vsoc_input_service::VirtualTouchScreen; |
| 38 | using vsoc_input_service::VSoCInputService; |
| 39 | |
Cody Schuffelen | 038d4d9 | 2019-11-04 15:09:03 -0800 | [diff] [blame] | 40 | DEFINE_uint32(keyboard_port, 0, "keyboard vsock port"); |
| 41 | DEFINE_uint32(touch_port, 0, "keyboard vsock port"); |
| 42 | |
Jorge E. Moreira | ae2fcae | 2017-11-28 11:16:59 -0800 | [diff] [blame] | 43 | namespace { |
| 44 | |
Jorge E. Moreira | ae2fcae | 2017-11-28 11:16:59 -0800 | [diff] [blame] | 45 | void EventLoop(std::shared_ptr<VirtualDeviceBase> device, |
Cody Schuffelen | 038d4d9 | 2019-11-04 15:09:03 -0800 | [diff] [blame] | 46 | std::function<InputEvent()> next_event) { |
Jorge E. Moreira | ae2fcae | 2017-11-28 11:16:59 -0800 | [diff] [blame] | 47 | while (1) { |
Cody Schuffelen | 038d4d9 | 2019-11-04 15:09:03 -0800 | [diff] [blame] | 48 | InputEvent event = next_event(); |
| 49 | device->EmitEvent(event.type, event.code, event.value); |
Jorge E. Moreira | ae2fcae | 2017-11-28 11:16:59 -0800 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
| 53 | } // namespace |
| 54 | |
| 55 | bool 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 Schuffelen | 038d4d9 | 2019-11-04 15:09:03 -0800 | [diff] [blame] | 65 | auto config = cvd::DeviceConfig::Get(); |
| 66 | if (!config) { |
| 67 | LOG(ERROR) << "Failed to open device config"; |
Jorge E. Moreira | ae2fcae | 2017-11-28 11:16:59 -0800 | [diff] [blame] | 68 | return false; |
| 69 | } |
| 70 | |
| 71 | virtual_touchscreen_.reset( |
Cody Schuffelen | 038d4d9 | 2019-11-04 15:09:03 -0800 | [diff] [blame] | 72 | new VirtualTouchScreen(config->screen_x_res(), config->screen_y_res())); |
Jorge E. Moreira | ae2fcae | 2017-11-28 11:16:59 -0800 | [diff] [blame] | 73 | if (!virtual_touchscreen_->SetUp()) { |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | bool VSoCInputService::ProcessEvents() { |
Cody Schuffelen | 038d4d9 | 2019-11-04 15:09:03 -0800 | [diff] [blame] | 81 | 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. Moreira | ae2fcae | 2017-11-28 11:16:59 -0800 | [diff] [blame] | 100 | |
| 101 | // Start device threads |
Cody Schuffelen | 038d4d9 | 2019-11-04 15:09:03 -0800 | [diff] [blame] | 102 | 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. Moreira | ae2fcae | 2017-11-28 11:16:59 -0800 | [diff] [blame] | 113 | }); |
| 114 | }); |
Cody Schuffelen | 038d4d9 | 2019-11-04 15:09:03 -0800 | [diff] [blame] | 115 | 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. Moreira | ae2fcae | 2017-11-28 11:16:59 -0800 | [diff] [blame] | 127 | }); |
| 128 | |
| 129 | screen_thread.join(); |
| 130 | keyboard_thread.join(); |
Jorge E. Moreira | ae2fcae | 2017-11-28 11:16:59 -0800 | [diff] [blame] | 131 | |
| 132 | // Should never return |
| 133 | return false; |
| 134 | } |