-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.cpp
More file actions
110 lines (97 loc) · 2.62 KB
/
Copy pathInput.cpp
File metadata and controls
110 lines (97 loc) · 2.62 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
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
/*
* This file is part of the Eclipse2017 project. It is subject to the GPLv3
* license terms in the LICENSE file found in the top-level directory of this
* distribution and at
* https://github.com/jjackowski/eclipse2017/blob/master/LICENSE.
* No part of the Eclipse2017 project, including this file, may be copied,
* modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*
* Copyright (C) 2017 Jeff Jackowski
*/
#include "Input.hpp"
#include <boost/exception/errinfo_file_name.hpp>
#include <boost/exception/errinfo_errno.hpp>
// for open() and related items; may be more than needed
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//Evdev::Evdev() : dev(nullptr), fd(-1) { }
Evdev::Evdev(const std::string &path) {
fd = open(path.c_str(), O_RDONLY);
if (fd < 0) {
BOOST_THROW_EXCEPTION(EvdevFileOpenError() <<
boost::errinfo_file_name(path)
);
}
int result = libevdev_new_from_fd(fd, &dev);
if (result < 0) {
close(fd);
BOOST_THROW_EXCEPTION(EvdevInitError() <<
boost::errinfo_errno(-result) <<
// the file may have nothing to do with the error, but it will
// add context
boost::errinfo_file_name(path)
);
}
}
Evdev::Evdev(Evdev &&e) : dev(e.dev), fd(e.fd) {
e.dev = nullptr;
e.fd = -1;
}
Evdev::~Evdev() {
if (dev) {
libevdev_free(dev);
}
if (fd > 0) {
close(fd);
}
}
Evdev &Evdev::operator=(Evdev &&old) {
dev = old.dev;
old.dev = nullptr;
fd = old.fd;
old.fd = -1;
}
void Evdev::respond(int) {
input_event ie;
int result;
do {
result = libevdev_next_event(
dev,
LIBEVDEV_READ_FLAG_NORMAL | LIBEVDEV_READ_FLAG_BLOCKING,
&ie
);
if (result == LIBEVDEV_READ_STATUS_SUCCESS) {
EventTypeCode etc(ie.type, ie.code);
InputMap::const_iterator iter = receivers.find(etc);
if (iter != receivers.end()) {
iter->second(etc, ie.value);
}
}
} while ((result >= 0) && (libevdev_has_event_pending(dev) > 0));
}
std::string Evdev::name() const {
return libevdev_get_name(dev);
}
bool Evdev::hasEventType(unsigned int et) const {
return libevdev_has_event_type(dev, et) == 1;
}
bool Evdev::hasEventCode(unsigned int et, unsigned int ec) const {
return libevdev_has_event_code(dev, et, ec) == 1;
}
bool Evdev::hasEvent(EventTypeCode etc) const {
return libevdev_has_event_code(dev, etc.type, etc.code) == 1;
}
int Evdev::value(unsigned int et, unsigned int ec) const {
int val;
if (!libevdev_fetch_event_value(dev, et, ec, &val)) {
BOOST_THROW_EXCEPTION(EvdevUnsupportedEvent() <<
EvdevEventType(et) << EvdevEventCode(ec)
);
}
return val;
}
void Evdev::usePoller(Poller &p) {
p.add(shared_from_this(), fd);
}