Repository navigation
Expand file tree
/
Copy pathevdevsource.cc
More file actions
48 lines (40 loc) · 1.5 KB
/
Copy pathevdevsource.cc
File metadata and controls
48 lines (40 loc) · 1.5 KB
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
// Copyright 2016 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "evdevsource.h"
namespace touch_keyboard {
bool EvdevSource::OpenSourceDevice(std::string const &source_device_path) {
source_fd_ = syscall_handler_->open(source_device_path.c_str(), O_RDONLY);
if (source_fd_ < 0) {
PLOG(ERROR) << "Failed to open() source device " << source_device_path << ". (" << source_fd_ << ")\n";
return false;
}
return true;
}
bool EvdevSource::GetNextEvent(int timeout_ms, struct input_event *ev) const {
if (timeout_ms > 0) {
int num_ready;
struct timeval timeout = {0, timeout_ms * 1000};
fd_set set;
// Block until there's something to read or we hit a timeout.
FD_ZERO(&set);
FD_SET(source_fd_, &set);
num_ready = syscall_handler_->select(source_fd_ + 1, &set,
NULL, NULL, &timeout);
// If the timeout triggered, return false instead of waiting forever.
if (num_ready != 1) {
return false;
}
}
int num_bytes_read = syscall_handler_->read(source_fd_, ev, sizeof(*ev));
if (num_bytes_read != sizeof(*ev)) {
PLOG(ERROR) << "ERROR: A read failed to read an entire event. Only read " <<
num_bytes_read << " of " << sizeof(*ev) << "expected bytes.\n";
if (num_bytes_read == -1) {
throw "Evdev read() failed";
}
return false;
}
return true;
}
} // namespace touch_keyboard