summaryrefslogtreecommitdiff
path: root/guest/commands/vsoc_input_service/vsoc_input_service.cpp
blob: 8753735c1d66833a37bd20ede9077eb70a713701 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * 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 <linux/virtio_input.h>

#include <thread>

#include <gflags/gflags.h>
#include "log/log.h"
#include <glog/logging.h>

#include "common/libs/fs/shared_fd.h"
#include "common/libs/device_config/device_config.h"
#include "common/vsoc/lib/input_events_region_view.h"

using vsoc::input_events::InputEvent;
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;

DEFINE_uint32(keyboard_port, 0, "keyboard vsock port");
DEFINE_uint32(touch_port, 0, "keyboard vsock port");

namespace {

void EventLoop(std::shared_ptr<VirtualDeviceBase> device,
               std::function<InputEvent()> next_event) {
  while (1) {
    InputEvent event = next_event();
    device->EmitEvent(event.type, event.code, event.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 config = cvd::DeviceConfig::Get();
  if (!config) {
    LOG(ERROR) << "Failed to open device config";
    return false;
  }

  virtual_touchscreen_.reset(
      new VirtualTouchScreen(config->screen_x_res(), config->screen_y_res()));
  if (!virtual_touchscreen_->SetUp()) {
    return false;
  }

  return true;
}

bool VSoCInputService::ProcessEvents() {
  cvd::SharedFD keyboard_fd;
  cvd::SharedFD touch_fd;

  LOG(INFO) << "Connecting to the keyboard at " << FLAGS_keyboard_port;
  if (FLAGS_keyboard_port) {
    keyboard_fd = cvd::SharedFD::VsockClient(2, FLAGS_keyboard_port, SOCK_STREAM);
    if (!keyboard_fd->IsOpen()) {
      LOG(ERROR) << "Could not connect to the keyboard at vsock:2:" << FLAGS_keyboard_port;
    }
    LOG(INFO) << "Connected to keyboard";
  }
  LOG(INFO) << "Connecting to the touchscreen at " << FLAGS_keyboard_port;
  if (FLAGS_touch_port) {
    touch_fd = cvd::SharedFD::VsockClient(2, FLAGS_touch_port, SOCK_STREAM);
    if (!touch_fd->IsOpen()) {
      LOG(ERROR) << "Could not connect to the touch at vsock:2:" << FLAGS_touch_port;
    }
    LOG(INFO) << "Connected to touch";
  }

  // Start device threads
  std::thread screen_thread([this, touch_fd]() {
    EventLoop(virtual_touchscreen_, [touch_fd]() {
      struct virtio_input_event event;
      if (touch_fd->Read(&event, sizeof(event)) != sizeof(event)) {
        LOG(FATAL) << "Could not read touch event: " << touch_fd->StrError();
      }
      return InputEvent {
        .type = event.type,
        .code = event.code,
        .value = event.value,
      };
    });
  });
  std::thread keyboard_thread([this, keyboard_fd]() {
    EventLoop(virtual_keyboard_, [keyboard_fd]() {
      struct virtio_input_event event;
      if (keyboard_fd->Read(&event, sizeof(event)) != sizeof(event)) {
        LOG(FATAL) << "Could not read keyboard event: " << keyboard_fd->StrError();
      }
      return InputEvent {
        .type = event.type,
        .code = event.code,
        .value = event.value,
      };
    });
  });

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

  // Should never return
  return false;
}