-
Notifications
You must be signed in to change notification settings - Fork 34
/
main.c
225 lines (198 loc) · 8.15 KB
/
main.c
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <string.h>
#include <fcntl.h>
#include <linux/input.h>
#include <time.h>
#include "screenlocations.h"
#define PEN_DEVICE "/dev/input/event1"
#define TOUCH_DEVICE "/dev/input/event2"
//const struct input_event tool_touch_off = { .type = EV_KEY, .code = BTN_TOUCH, .value = 0}; //these might be used in the future to improve press and hold mode
//const struct input_event tool_pen_on = { .type = EV_KEY, .code = BTN_TOOL_PEN, .value = 1}; //used when pen approaches the screen
//const struct input_event tool_pen_off = { .type = EV_KEY, .code = BTN_TOOL_PEN, .value = 0};
const struct input_event tool_rubber_on = { .type = EV_KEY, .code = BTN_TOOL_RUBBER, .value = 1}; //used when rubber approaches the screen
const struct input_event tool_rubber_off = { .type = EV_KEY, .code = BTN_TOOL_RUBBER, .value = 0};
void writeEvent(int fd, struct input_event event)
{
struct timeval tv;
gettimeofday(&tv, NULL);
event.time = tv;
//debug: printf("writing: seconds = %ld, usec= %ld, type = %d, code = %d, value = %d\n", event.time.tv_sec, event.time.tv_usec, event.type, event.code, event.value);
write(fd, &event, sizeof(struct input_event));
}
void writeTapWithTouch(int fd, int location[2], bool left_handed) {
struct input_event event;
//this is the minimum (probably) seqeunce of events that must be sent to tap the screen in a location.
event = (struct input_event) {.type = EV_ABS, .code = ABS_MT_SLOT, .value = 0x7FFFFFFF}; //Use max signed int slot
//printf("Writing ABS_MT_SLOT: %d\n", event.value);
writeEvent(fd, event);
event = (struct input_event) {.type = EV_ABS, .code = ABS_MT_TRACKING_ID, .value = time(NULL)};
//printf("Writing Tracking ID: %d\n", event.value);
writeEvent(fd, event);
int x = location[0];
if (left_handed) {
x = WIDTH - x;
}
event = (struct input_event) {.type = EV_ABS, .code = ABS_MT_POSITION_X, .value = x};
//printf("Writing Touch X: %d\n", event.value);
writeEvent(fd, event);
event = (struct input_event) {.type = EV_ABS, .code = ABS_MT_POSITION_Y, .value = location[1]};
//printf("Writing Touch Y: %d\n", event.value);
writeEvent(fd, event);
event = (struct input_event) {.type = EV_SYN, .code = SYN_REPORT, .value = 1};
//printf("Writing SYN Report\n");
writeEvent(fd, event);
event = (struct input_event) {.type = EV_ABS, .code = ABS_MT_TRACKING_ID, .value = -1};
//printf("Writing Tracking ID: -1\n");
writeEvent(fd, event);
event = (struct input_event) {.type = EV_SYN, .code = SYN_REPORT, .value = 1};
//printf("Writing SYN Report\n");
writeEvent(fd, event);
}
void toggleMode(struct input_event ev, int fd) {
static int toggle = 0;
if (ev.code == BTN_STYLUS && ev.value == 1) { // change state of toggle on button press
toggle = !toggle;
printf("toggle: %d \n", toggle);
}
if (toggle)
if (ev.code == BTN_TOOL_PEN) { //when toggle is on, we write these events following the pen state
if (ev.value == 1) {
printf("writing eraser on\n");
writeEvent(fd, tool_rubber_on);
}
else {
printf("writing eraser off\n");
writeEvent(fd, tool_rubber_off);
}
}
}
void pressMode(struct input_event ev, int fd) {
if (ev.code == BTN_STYLUS) {
ev.code = BTN_TOOL_RUBBER; //value will follow the button, so we can reuse the message
writeEvent(fd, ev);
}
}
bool doublePressHandler(struct input_event ev) { //returns true if a double press has happened
static struct timeval prevTime;
const double maxDoublePressTime = .5; //500ms seems to be the standard double press time
double elapsedTime = (ev.time.tv_sec + ev.time.tv_usec/1000000.0) - (prevTime.tv_sec + prevTime.tv_usec/1000000.0);
if (ev.code == BTN_STYLUS && ev.value == 1) {
//printf("Current Time: %f | ", ev.time.tv_sec + ev.time.tv_usec/1000000.0);
//printf("Prev Time: %f |", prevTime.tv_sec + prevTime.tv_usec/1000000.0);
//printf("Elapsed Time: %f\n", elapsedTime);
if (elapsedTime <= maxDoublePressTime) {
return true;
}
prevTime = ev.time;
}
return false;
}
int main(int argc, char *argv[]) {
int mode = 0, doublePressAction = 0;
struct input_event ev_pen;
int fd_pen, fd_touch;
bool left_handed = false;
char name[256] = "Unknown";
printf("RemarkableLamyEraser 1.2\n");
//check our input args
for(int i = 1; i < argc; i++) {
if (!strncmp(argv[i], "--toggle", 8)) {
printf("MODE: TOGGLE\n");
mode = TOGGLE_MODE;
}
else if (!strncmp(argv[i], "--press", 7)) {
printf("MODE: PRESS AND HOLD\n");
mode = PRESS_MODE;
}
else if (!strncmp(argv[i], "--left-handed", 13)) {
printf("LEFT HANDED ACTIVATED\n");
left_handed = true;
}
else if (!strncmp(argv[i], "--double-press", 14)) {
if (!strncmp(argv[i+1], "undo", 4)) {
printf("DOUBLE CLICK ACTION: UNDO\n");
doublePressAction = UNDO;
i++; //manually increment i so we skip interpretting the double press action arg
}
else if (!strncmp(argv[i+1], "redo", 4)) {
printf("DOUBLE CLICK ACTION: REDO\n");
doublePressAction = REDO;
i++; //manually increment i so we skip interpretting the double press action arg
}
else {
printf("Unknown double press action <%s>. Using default.\n", argv[i+1]);
printf("DOUBLE CLICK ACTION: UNDO\n");
doublePressAction = UNDO;
}
}
else {
printf("Unknown argument %s\nExiting...\n", argv[i]);
exit(EXIT_FAILURE);
}
}
if (!mode) {
printf("No mode specified! Using default.\nMODE: PRESS AND HOLD\n");
mode = PRESS_MODE;
}
if (!doublePressAction) {
printf("No double click action specified! Using default.\nDOUBLE CLICK ACTION: UNDO\n");
doublePressAction = UNDO;
}
/* Open Device: Pen */
fd_pen = open(PEN_DEVICE, O_RDWR);
if (fd_pen == -1) {
fprintf(stderr, "%s is not a vaild device\n", PEN_DEVICE);
exit(EXIT_FAILURE);
}
/* Open Device: Touch */
fd_touch = open(TOUCH_DEVICE, O_WRONLY);
if (fd_touch == -1) {
fprintf(stderr, "%s is not a vaild device\n", PEN_DEVICE);
exit(EXIT_FAILURE);
}
/* Print Device Name */
ioctl(fd_pen, EVIOCGNAME(sizeof(name)), name);
printf("Using Devices:\n");
printf("1. device file = %s\n", PEN_DEVICE);
printf(" device name = %s\n", name);
ioctl(fd_touch, EVIOCGNAME(sizeof(name)), name);
printf("2. device file = %s\n", TOUCH_DEVICE);
printf(" device name = %s\n", name);
for (;;) {
const size_t ev_pen_size = sizeof(struct input_event);
read(fd_pen, &ev_pen, ev_pen_size); //note: read pauses until there is data
if(doublePressHandler(ev_pen)) {
switch(doublePressAction) {
case UNDO :
printf("writing undo\n");
writeTapWithTouch(fd_touch, undoTouch, left_handed);
writeTapWithTouch(fd_touch, panelTouch, left_handed);
writeTapWithTouch(fd_touch, undoTouch, left_handed);
writeTapWithTouch(fd_touch, panelTouch, left_handed); //doing it like this ensures that the panel returns to it's starting state
break;
case REDO :
printf("writing redo\n");
writeTapWithTouch(fd_touch, redoTouch, left_handed);
writeTapWithTouch(fd_touch, panelTouch, left_handed);
writeTapWithTouch(fd_touch, redoTouch, left_handed);
writeTapWithTouch(fd_touch, panelTouch, left_handed);
break;
}
}
switch(mode) {
case TOGGLE_MODE :
toggleMode(ev_pen, fd_pen);
break;
case PRESS_MODE :
pressMode(ev_pen, fd_pen);
break;
default :
printf("Somehow a mode wasn't set? Exiting...\n");
exit(EXIT_FAILURE);
}
}
return EXIT_SUCCESS;
}