-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevtablet.hpp
198 lines (179 loc) · 5.19 KB
/
evtablet.hpp
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
*The MIT License (MIT)
*
*Copyright (c) 2014 Raphael Sousa Santos, http://raphaelss.com
*
*Permission is hereby granted, free of charge, to any person obtaining a copy
*of this software and associated documentation files (the "Software"), to deal
*in the Software without restriction, including without limitation the rights
*to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*copies of the Software, and to permit persons to whom the Software is
*furnished to do so, subject to the following conditions:
*
*The above copyright notice and this permission notice shall be included in all
*copies or substantial portions of the Software.
*
*THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
*SOFTWARE.
*/
#ifndef EVTABLET_HPP_INCLUDED
#define EVTABLET_HPP_INCLUDED
#include <memory>
#include <cstring>
#include <libevdev/libevdev.h>
#include <fcntl.h>
namespace ev {
namespace impl {
struct evdev_deleter {
void operator()(libevdev *ptr)
{
libevdev_free(ptr);
}
};
}
template <class T>
struct Tablet {
Tablet(): good(false) {}
bool open(const char *path)
{
libevdev *tmp;
int fd = ::open(path, O_RDONLY | O_NONBLOCK);
if (fd < 0) {
std::fputs("Failed to open device\n", stderr);
dev.reset();
good = false;
return false;
}
int rc = libevdev_new_from_fd(fd, &tmp);
if (rc < 0) {
std::fprintf(stderr,"Failed to init libevdev (%s)\n", std::strerror(-rc));
dev.reset();
good = false;
return false;
}
dev.reset(tmp);
good = true;
return true;
}
void close()
{
dev.reset();
good = false;
}
bool process_events()
{
input_event ev;
T *derived = static_cast<T*>(this);
int rc;
do {
rc = libevdev_next_event(dev.get(), LIBEVDEV_READ_FLAG_NORMAL, &ev);
if (rc == LIBEVDEV_READ_STATUS_SYNC) {
derived->event_sync(ev.code != SYN_DROPPED);
while (rc == LIBEVDEV_READ_STATUS_SYNC) {
rc = libevdev_next_event(dev.get(), LIBEVDEV_READ_FLAG_SYNC, &ev);
}
} else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
switch(ev.type) {
case EV_SYN:
derived->event_sync(ev.code != SYN_DROPPED);
break;
case EV_ABS:
switch(ev.code) {
case ABS_X:
derived->event_x(ev.value);
break;
case ABS_Y:
derived->event_y(ev.value);
break;
case ABS_PRESSURE:
derived->event_pressure(ev.value);
break;
case ABS_DISTANCE:
derived->event_distance(ev.value);
break;
}
case EV_KEY:
switch(ev.code) {
case BTN_TOOL_PEN:
derived->event_in_range(ev.value);
break;
case BTN_TOOL_RUBBER:
derived->event_rubber_in_range(ev.value);
break;
case BTN_TOUCH:
derived->event_stylus_contact(ev.value);
break;
case BTN_STYLUS:
derived->event_stylus_button1(ev.value);
break;
case BTN_STYLUS2:
derived->event_stylus_button2(ev.value);
break;
}
}
}
} while(rc == LIBEVDEV_READ_STATUS_SYNC
|| rc == LIBEVDEV_READ_STATUS_SUCCESS);
if (rc != LIBEVDEV_READ_STATUS_SUCCESS && rc != -EAGAIN) {
std::fprintf(stderr, "Failed to handle events: %s\n", std::strerror(rc));
good = false;
return false;
}
return true;
}
const input_absinfo *query_properties(int axis)
{
return libevdev_get_abs_info(dev.get(), axis);
}
unsigned x_min() const
{
return libevdev_get_abs_info(dev.get(), ABS_X)->minimum;
}
unsigned x_max() const
{
return libevdev_get_abs_info(dev.get(), ABS_X)->maximum;
}
unsigned y_min() const
{
return libevdev_get_abs_info(dev.get(), ABS_Y)->minimum;
}
unsigned y_max() const
{
return libevdev_get_abs_info(dev.get(), ABS_Y)->maximum;
}
unsigned pressure_min() const
{
return libevdev_get_abs_info(dev.get(), ABS_PRESSURE)->minimum;
}
unsigned pressure_max() const
{
return libevdev_get_abs_info(dev.get(), ABS_PRESSURE)->maximum;
}
unsigned distance_min() const
{
return libevdev_get_abs_info(dev.get(), ABS_DISTANCE)->minimum;
}
unsigned distance_max() const
{
return libevdev_get_abs_info(dev.get(), ABS_DISTANCE)->maximum;
}
void event_sync(bool report) {}
void event_x(unsigned x) {}
void event_y(unsigned x) {}
void event_pressure(unsigned x) {}
void event_distance(unsigned x) {}
void event_in_range(bool x) {}
void event_rubber_in_range(bool x) {}
void event_stylus_contact(bool x) {}
void event_stylus_button1(bool x) {}
void event_stylus_button2(bool x) {}
bool good;
std::unique_ptr<libevdev, impl::evdev_deleter> dev;
};
}
#endif