-
Notifications
You must be signed in to change notification settings - Fork 13
/
recept.cpp
179 lines (127 loc) · 3.3 KB
/
recept.cpp
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
#include <cstdint>
#include <array>
#include <limits>
#ifndef RING_SIZE
#define RING_SIZE 16
#endif
template<typename T, int Size>
class ring {
public:
inline void add(const T& value) {
if (empty()) {
fill(value);
return;
}
_index = (_index + 1) % Size;
const T& oldest = _values[_index];
_sum = _sum + value - oldest;
_values[_index] = value;
}
inline void clear() {
_sum = std::numeric_limits<T>::max();
}
inline bool empty() const {
return _sum == std::numeric_limits<T>::max();
}
/**
* Return the last (i.e., newest) item added to the ring.
*/
inline const T& front() const {
return _values[_index];
}
inline void fill(const T& value) {
_values.fill(value);
_sum = value*Size;
}
inline T sum() const {
return _sum;
}
inline T average() const {
return _sum/Size;
}
private:
std::array<T, Size> _values;
uint8_t _index;
T _sum;
};
static ring<uint32_t, RING_SIZE> ring_x;
static ring<uint32_t, RING_SIZE> ring_y;
template<typename Ring>
inline uint32_t update_pos(Ring& ring, uint32_t value) {
ring.add(value);
return ring.average();
}
void filter(char* buf) {
const uint8_t type = (uint8_t)buf[8];
const uint16_t code = (
(uint16_t)buf[10] |
(uint16_t)buf[11] << 8);
uint32_t value = (
(uint32_t)buf[12] |
(uint32_t)buf[13] << 8 |
(uint32_t)buf[14] << 16 |
(uint32_t)buf[15] << 24);
// type == 1 && code == 320 && value == 1 -> pen in
// type == 1 && code == 320 && value == 0 -> pen out
// type == 1 && code == 321 && value == 1 -> eraser in
// type == 1 && code == 321 && value == 0 -> eraser out
//
// type == 1 && code == 330 && value == 1 -> pen/eraser down
// type == 1 && code == 330 && value == 0 -> pen/eraser up
//
// type == 3 && code == 0 -> value == x
// type == 3 && code == 1 -> value == y
// type == 3 && code == 24 -> value == pressure
// type == 3 && code == 25 -> value == distance
// type == 3 && code == 26 -> value == tilt x
// type == 3 && code == 27 -> value == tilt y
// pen/eraser in
if (type == 1 && ((code == 320 && value == 1) || (code == 321 && value == 1))) {
ring_x.clear();
ring_y.clear();
}
if (type == 3) {
if (code == 0) {
value = update_pos(ring_x, value);
} else if (code == 1) {
value = update_pos(ring_y, value);
}
// copy value back to buffer
buf[12] = (uint8_t)value;
buf[13] = (uint8_t)(value >> 8);
buf[14] = (uint8_t)(value >> 16);
buf[15] = (uint8_t)(value >> 24);
}
}
extern "C" {
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <string.h>
#include <dlfcn.h>
typedef int (*t_open)(const char*, int);
typedef ssize_t (*t_read)(int, void*, size_t);
static int event_fd = 0;
int open(const char* filename, int flags) {
static t_open real_open = NULL;
if (!real_open)
real_open = (t_open)dlsym(RTLD_NEXT, "open");
int fd = real_open(filename, flags);
if (strcmp(filename, "/dev/input/event1") == 0) {
printf(">| someone is trying to open event1, let's remember the fd!\n");
printf(">| (psssst: it is %d)\n", fd);
event_fd = fd;
}
return fd;
}
ssize_t read(int fd, void* buf, size_t count) {
static t_read real_read = NULL;
if (!real_read)
real_read = (t_read)dlsym(RTLD_NEXT, "read");
ssize_t ret = real_read(fd, buf, count);
if (fd != 0 && fd == event_fd && ret == 16)
filter((char*)buf);
return ret;
}
}