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> |
| 21 | |
| 22 | #include <thread> |
| 23 | |
| 24 | #include "log/log.h" |
| 25 | |
| 26 | #include "common/vsoc/lib/fb_bcast_region_view.h" |
| 27 | #include "common/vsoc/lib/input_events_region_view.h" |
| 28 | |
| 29 | using vsoc::framebuffer::FBBroadcastRegionView; |
| 30 | using vsoc::input_events::InputEvent; |
| 31 | using vsoc::input_events::InputEventsRegionView; |
| 32 | using vsoc_input_service::VirtualDeviceBase; |
| 33 | using vsoc_input_service::VirtualKeyboard; |
| 34 | using vsoc_input_service::VirtualPowerButton; |
| 35 | using vsoc_input_service::VirtualTouchScreen; |
| 36 | using vsoc_input_service::VSoCInputService; |
| 37 | |
| 38 | namespace { |
| 39 | |
Jorge E. Moreira | ae2fcae | 2017-11-28 11:16:59 -0800 | [diff] [blame] | 40 | void EventLoop(std::shared_ptr<VirtualDeviceBase> device, |
| 41 | std::function<int(InputEvent*, int)> next_events) { |
| 42 | while (1) { |
| 43 | InputEvent events[InputEventsRegionView::kMaxEventsPerPacket]; |
| 44 | int count = next_events(events, InputEventsRegionView::kMaxEventsPerPacket); |
| 45 | if (count <= 0) { |
| 46 | SLOGE("Error getting events from the queue: Maybe check packet size"); |
| 47 | continue; |
| 48 | } |
| 49 | for (int i = 0; i < count; ++i) { |
| 50 | device->EmitEvent(events[i].type, events[i].code, events[i].value); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | } // namespace |
| 56 | |
| 57 | bool VSoCInputService::SetUpDevices() { |
| 58 | virtual_power_button_.reset(new VirtualPowerButton()); |
| 59 | if (!virtual_power_button_->SetUp()) { |
| 60 | return false; |
| 61 | } |
| 62 | virtual_keyboard_.reset(new VirtualKeyboard()); |
| 63 | if (!virtual_keyboard_->SetUp()) { |
| 64 | return false; |
| 65 | } |
| 66 | |
Ryan Haining | 59063a9 | 2018-02-08 17:32:22 -0800 | [diff] [blame^] | 67 | auto fb_broadcast = FBBroadcastRegionView::GetInstance(); |
Jorge E. Moreira | 4b9bbb1 | 2017-12-28 15:49:06 -0800 | [diff] [blame] | 68 | if (!fb_broadcast) { |
Jorge E. Moreira | ae2fcae | 2017-11-28 11:16:59 -0800 | [diff] [blame] | 69 | SLOGE("Failed to open framebuffer broadcast region"); |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | virtual_touchscreen_.reset( |
| 74 | new VirtualTouchScreen(fb_broadcast->x_res(), fb_broadcast->y_res())); |
| 75 | if (!virtual_touchscreen_->SetUp()) { |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | bool VSoCInputService::ProcessEvents() { |
Ryan Haining | 59063a9 | 2018-02-08 17:32:22 -0800 | [diff] [blame^] | 83 | auto input_events_rv = InputEventsRegionView::GetInstance(); |
Jorge E. Moreira | ae2fcae | 2017-11-28 11:16:59 -0800 | [diff] [blame] | 84 | // TODO(jemoreira): Post available devices to region |
| 85 | input_events_rv->StartWorker(); |
| 86 | |
| 87 | // Start device threads |
| 88 | std::thread screen_thread([this]() { |
| 89 | EventLoop( |
| 90 | virtual_touchscreen_, [](InputEvent* event_buffer, int max_events) { |
Jorge E. Moreira | 4b9bbb1 | 2017-12-28 15:49:06 -0800 | [diff] [blame] | 91 | return InputEventsRegionView::GetInstance()->GetScreenEventsOrWait( |
Jorge E. Moreira | ae2fcae | 2017-11-28 11:16:59 -0800 | [diff] [blame] | 92 | event_buffer, max_events); |
| 93 | }); |
| 94 | }); |
| 95 | std::thread keyboard_thread([this]() { |
| 96 | EventLoop(virtual_keyboard_, [](InputEvent* event_buffer, int max_events) { |
Jorge E. Moreira | 4b9bbb1 | 2017-12-28 15:49:06 -0800 | [diff] [blame] | 97 | return InputEventsRegionView::GetInstance()->GetKeyboardEventsOrWait( |
Jorge E. Moreira | ae2fcae | 2017-11-28 11:16:59 -0800 | [diff] [blame] | 98 | event_buffer, max_events); |
| 99 | }); |
| 100 | }); |
| 101 | std::thread button_thread([this]() { |
Jorge E. Moreira | 4b9bbb1 | 2017-12-28 15:49:06 -0800 | [diff] [blame] | 102 | EventLoop(virtual_power_button_, |
| 103 | [](InputEvent* event_buffer, int max_events) { |
| 104 | return InputEventsRegionView::GetInstance() |
| 105 | ->GetPowerButtonEventsOrWait(event_buffer, max_events); |
| 106 | }); |
Jorge E. Moreira | ae2fcae | 2017-11-28 11:16:59 -0800 | [diff] [blame] | 107 | }); |
| 108 | |
| 109 | screen_thread.join(); |
| 110 | keyboard_thread.join(); |
| 111 | button_thread.join(); |
| 112 | |
| 113 | // Should never return |
| 114 | return false; |
| 115 | } |