summaryrefslogtreecommitdiff
path: root/guest/commands/vsoc_input_service/vsoc_input_service.cpp
blob: d100a527355874f864eda991f5fbe7505d149d8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "vsoc_input_service.h"

#include <linux/input.h>
#include <linux/uinput.h>

#include <thread>

#include "log/log.h"

#include "common/vsoc/lib/screen_region_view.h"
#include "common/vsoc/lib/input_events_region_view.h"

using vsoc::screen::ScreenRegionView;
using vsoc::input_events::InputEvent;
using vsoc::input_events::InputEventsRegionView;
using vsoc_input_service::VirtualDeviceBase;
using vsoc_input_service::VirtualKeyboard;
using vsoc_input_service::VirtualPowerButton;
using vsoc_input_service::VirtualTouchScreen;
using vsoc_input_service::VSoCInputService;

namespace {

void EventLoop(std::shared_ptr<VirtualDeviceBase> device,
               std::function<int(InputEvent*, int)> next_events) {
  while (1) {
    InputEvent events[InputEventsRegionView::kMaxEventsPerPacket];
    int count = next_events(events, InputEventsRegionView::kMaxEventsPerPacket);
    if (count <= 0) {
      SLOGE("Error getting events from the queue: Maybe check packet size");
      continue;
    }
    for (int i = 0; i < count; ++i) {
      device->EmitEvent(events[i].type, events[i].code, events[i].value);
    }
  }
}

}  // namespace

bool VSoCInputService::SetUpDevices() {
  virtual_power_button_.reset(new VirtualPowerButton());
  if (!virtual_power_button_->SetUp()) {
    return false;
  }
  virtual_keyboard_.reset(new VirtualKeyboard());
  if (!virtual_keyboard_->SetUp()) {
    return false;
  }

  auto screen_view = ScreenRegionView::GetInstance();
  if (!screen_view) {
    SLOGE("Failed to open framebuffer broadcast region");
    return false;
  }

  virtual_touchscreen_.reset(
      new VirtualTouchScreen(screen_view->x_res(), screen_view->y_res()));
  if (!virtual_touchscreen_->SetUp()) {
    return false;
  }

  return true;
}

bool VSoCInputService::ProcessEvents() {
  auto input_events_rv = InputEventsRegionView::GetInstance();
  // TODO(jemoreira): Post available devices to region
  auto worker = input_events_rv->StartWorker();

  // Start device threads
  std::thread screen_thread([this]() {
    EventLoop(
        virtual_touchscreen_, [](InputEvent* event_buffer, int max_events) {
          return InputEventsRegionView::GetInstance()->GetScreenEventsOrWait(
              event_buffer, max_events);
        });
  });
  std::thread keyboard_thread([this]() {
    EventLoop(virtual_keyboard_, [](InputEvent* event_buffer, int max_events) {
      return InputEventsRegionView::GetInstance()->GetKeyboardEventsOrWait(
          event_buffer, max_events);
    });
  });
  std::thread button_thread([this]() {
    EventLoop(virtual_power_button_,
              [](InputEvent* event_buffer, int max_events) {
                return InputEventsRegionView::GetInstance()
                    ->GetPowerButtonEventsOrWait(event_buffer, max_events);
              });
  });

  screen_thread.join();
  keyboard_thread.join();
  button_thread.join();

  // Should never return
  return false;
}