forked from zrafa/onscreenkeyboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
osk_touch.c
executable file
·488 lines (429 loc) · 12.1 KB
/
osk_touch.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
* osk_touch.c : framebuffer on-screen keyboard, touchscreen version for Steam Deck tty
*
* Copyright (C) 2014 Rafael Ignacio Zurita <rafaelignacio.zurita@gmail.com>
*
* Based on : http://thiemonge.org/getting-started-with-uinput
* and www.cubieforums.com/index.php?topic=33.0
* and fb_drawimage() from https://svn.mcs.anl.gov/repos/ZeptoOS/trunk/BGP/packages/busybox/src/miscutils/fbsplash.c
* and evtest from https://cgit.freedesktop.org/evtest/tree/evtest.c
* and fbkeyboard from https://github.com/julianwi/fbkeyboard/blob/master/fbkeyboard.c
*
* osk_touch.c is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* You should have received a copy of the GNU Library General Public
* License along with this software (check COPYING); if not, write to the Free
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __BIONIC__
#define _GNU_SOURCE
#endif
#include <fcntl.h>
#include <linux/fb.h>
#include <linux/input.h>
#include <linux/ioctl.h>
#include <linux/uinput.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/select.h>
#include <sys/time.h>
#include <termios.h>
#include <unistd.h>
#ifdef __BIONIC__
// Android-x86 typically is more likely to come with features like
// CONFIG_VT and root, both of which this program depends on,
// than other families of Android
// Android-x86 distro tested: BlissOS Zenith 16.9.4
#define FRAMEBUFFER_FILE "/dev/graphics/fb0"
#else
#define FRAMEBUFFER_FILE "/dev/fb0"
#endif
#define KEYBOARD_FILE "/dev/uinput"
#define DEV_INPUT_EVENT "/dev/input"
#define EVENT_DEV_NAME "event"
#define TOUCHSCREEN_NAME "FTS3528:00 2808:1015"
#define BITMAP_FILE "deck_keyboard.ppm"
#define TOTAL_KEYS 59
typedef struct
{
unsigned short key;
unsigned int y1, x1, y2, x2;
} Key;
/* TODO: add escape */
Key keyboard[TOTAL_KEYS] = {
{KEY_GRAVE, 5, 285, 46, 350},
{KEY_1, 51, 285, 135, 350},
{KEY_2, 141, 285, 225, 350},
{KEY_3, 230, 285, 314, 350},
{KEY_4, 320, 285, 404, 350},
{KEY_5, 409, 285, 493, 350},
{KEY_6, 499, 285, 583, 350},
{KEY_7, 588, 285, 673, 350},
{KEY_8, 678, 285, 762, 350},
{KEY_9, 768, 285, 852, 350},
{KEY_0, 857, 285, 941, 350},
{KEY_MINUS, 947, 285, 1031, 350},
{KEY_EQUAL, 1036, 285, 1120, 350},
{KEY_BACKSPACE, 1126, 285, 1274, 350},
{KEY_TAB, 5, 215, 93, 280},
{KEY_Q, 98, 215, 184, 280},
{KEY_W, 189, 215, 274, 280},
{KEY_E, 280, 215, 365, 280},
{KEY_R, 371, 215, 456, 280},
{KEY_T, 462, 215, 547, 280},
{KEY_Y, 553, 215, 638, 280},
{KEY_U, 643, 215, 729, 280},
{KEY_I, 734, 215, 820, 280},
{KEY_O, 825, 215, 910, 280},
{KEY_P, 916, 215, 1001, 280},
{KEY_LEFTBRACE, 1007, 215, 1092, 280},
{KEY_RIGHTBRACE, 1098, 215, 1183, 280},
{KEY_BACKSLASH, 1189, 215, 1274, 280},
{KEY_CAPSLOCK, 5, 144, 123, 209},
{KEY_A, 129, 144, 214, 209},
{KEY_S, 219, 144, 305, 209},
{KEY_D, 310, 144, 395, 209},
{KEY_F, 401, 144, 486, 209},
{KEY_G, 491, 144, 577, 209},
{KEY_H, 582, 144, 667, 209},
{KEY_J, 673, 144, 758, 209},
{KEY_K, 763, 144, 848, 209},
{KEY_L, 854, 144, 939, 209},
{KEY_SEMICOLON, 945, 144, 1030, 209},
{KEY_APOSTROPHE, 1035, 144, 1120, 209},
{KEY_ENTER, 1126, 144, 1274, 209},
{KEY_LEFTSHIFT, 5, 74, 182, 139},
{KEY_Z, 188, 74, 273, 139},
{KEY_X, 278, 74, 364, 139},
{KEY_C, 369, 74, 455, 139},
{KEY_V, 460, 74, 546, 139},
{KEY_B, 551, 74, 637, 139},
{KEY_N, 642, 74, 727, 139},
{KEY_M, 733, 74, 818, 139},
{KEY_COMMA, 824, 74, 909, 139},
{KEY_DOT, 915, 74, 1000, 139},
{KEY_SLASH, 1006, 74, 1091, 139},
{KEY_RIGHTSHIFT, 1097, 74, 1274, 139},
{KEY_LEFTCTRL, 5, 3, 108, 68},
{KEY_SPACE, 114, 3, 838, 68},
{KEY_LEFT, 843, 3, 947, 68},
{KEY_RIGHT, 952, 3, 1056, 68},
{KEY_UP, 1061, 3, 1165, 68},
{KEY_DOWN, 1170, 3, 1274, 68}};
/*
* Draw image from PPM file
*/
static void fb_drawimage(const char *filename, char *fbp, int xo, int yo, int bpp, int length, int xres, unsigned *img_height, unsigned *img_width)
{
char head[256];
char s[80];
FILE *theme_file;
unsigned char *pixline;
unsigned i, j, line_size;
memset(head, 0, sizeof(head));
theme_file = fopen(filename, "r");
/* parse ppm header */
while (1)
{
if (fgets(s, sizeof(s), theme_file) == NULL)
{
printf("bad PPM file '%s'", filename);
exit(1);
}
if (s[0] == '#')
continue;
if (strlen(head) + strlen(s) >= sizeof(head))
{
printf("bad PPM file '%s'", filename);
exit(1);
}
strcat(head, s);
if (head[0] != 'P' || head[1] != '6')
{
printf("bad PPM file '%s'", filename);
exit(1);
}
/* width, height, max_color_val */
if (sscanf(head, "P6 %u %u %u", img_width, img_height, &i) == 3)
break;
/* TODO: i must be <= 255! */
}
line_size = *img_width * 3;
long int location = 0;
pixline = malloc(line_size);
for (j = 0; j < *img_height; j++)
{
unsigned char *pixel = pixline;
if (fread(pixline, 1, line_size, theme_file) != line_size)
{
printf("bad PPM file '%s'", filename);
exit(1);
}
for (i = 0; i < *img_width; i++)
{
pixel += 3;
location = (i + xo) * (bpp / 8) + (j + yo) * length;
*(fbp + location) = (((unsigned)pixel[0]));
*(fbp + location + 1) = (((unsigned)pixel[1]));
*(fbp + location + 2) = (((unsigned)pixel[2]));
*(fbp + location + 3) = -1; /* No transparency */
}
}
free(pixline);
fclose(theme_file);
}
void send_key(int fd, unsigned short code)
{
struct input_event ev;
ev.type = EV_KEY;
ev.code = code;
ev.value = 1;
if (write(fd, &ev, sizeof(ev)) != sizeof(ev))
fprintf(stderr, "error: sending uinput event\n");
ev.value = 0;
if (write(fd, &ev, sizeof(ev)) != sizeof(ev))
fprintf(stderr, "error: sending uinput event\n");
ev.type = EV_SYN;
ev.code = SYN_REPORT;
if (write(fd, &ev, sizeof(ev)) != sizeof(ev))
fprintf(stderr, "error: sending uinput event\n");
}
unsigned short get_key(unsigned int abs_x, unsigned int abs_y)
{
for (int i = 0; i < TOTAL_KEYS; i++)
if (abs_x >= keyboard[i].x1 && abs_x <= keyboard[i].x2 && abs_y >= keyboard[i].y1 && abs_y <= keyboard[i].y2)
return keyboard[i].key;
return 0;
}
/* Detect the Steam Deck's touchscreen device. Code copied from evtest. */
/**
* Filter for the AutoDevProbe scandir on /dev/input.
*
* @param dir The current directory entry provided by scandir.
*
* @return Non-zero if the given directory entry starts with "event", or zero
* otherwise.
*/
static int is_event_device(const struct dirent *dir) {
return strncmp(EVENT_DEV_NAME, dir->d_name, 5) == 0;
}
/**
* Scans all /dev/input/event* and automatically returns the one matching
* "FTS3528:00 2808:1015", the Steam Deck's touchscreen, or /dev/input/event0
* if it was not found.
*
* @return The event device file name of the device file selected. This
* string is allocated and must be freed by the caller.
*/
static char* find_touchscreen(void)
{
struct dirent **namelist;
int ndev, devnum = 0, found = 0;
char *device_path;
#ifdef __BIONIC__
ndev = scandir(DEV_INPUT_EVENT, &namelist, is_event_device, alphasort);
#else
ndev = scandir(DEV_INPUT_EVENT, &namelist, is_event_device, versionsort);
#endif
for (int i = 0; i < ndev && ndev > 0; i++)
{
if (found) break;
char fname[64];
int fd = -1;
char name[256] = "???";
snprintf(fname, sizeof(fname),
"%s/%s", DEV_INPUT_EVENT, namelist[i]->d_name);
fd = open(fname, O_RDONLY);
if (fd < 0)
continue;
ioctl(fd, EVIOCGNAME(sizeof(name)), name);
close(fd);
if (!strcmp(name, TOUCHSCREEN_NAME))
{
found = 1;
}
sscanf(namelist[i]->d_name, "event%d", &devnum);
free(namelist[i]);
}
asprintf(&device_path, "%s/%s%d",
DEV_INPUT_EVENT, EVENT_DEV_NAME,
devnum);
return device_path;
}
int main(void)
{
int fbfd = 0;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
long int screensize = 0;
char *fbp = 0;
/* img dimensions set in fb_drawimage */
unsigned img_height = 0;
unsigned img_width = 0;
/* uinput init */
int i, fd;
struct uinput_user_dev device;
memset(&device, 0, sizeof device);
fd = open(KEYBOARD_FILE, O_WRONLY);
if (fd == -1)
{
perror("Error: cannot open uinput device\n");
exit(1);
}
strcpy(device.name, "osk-touch");
device.id.bustype = BUS_USB;
device.id.vendor = 1;
device.id.product = 1;
device.id.version = 1;
if (write(fd, &device, sizeof(device)) != sizeof(device))
{
fprintf(stderr, "error setting up uinput device\n");
}
if (ioctl(fd, UI_SET_EVBIT, EV_KEY) < 0)
fprintf(stderr, "error: SET_EVBIT EV_KEY\n");
if (ioctl(fd, UI_SET_EVBIT, EV_SYN) < 0)
fprintf(stderr, "error: SET_EVBIT EV_SYN\n");
for (i = 0; i < TOTAL_KEYS; i++)
if (ioctl(fd, UI_SET_KEYBIT, keyboard[i].key) < 0)
fprintf(stderr, "error evbit key\n");
if (ioctl(fd, UI_SET_EVBIT, EV_REL) < 0)
fprintf(stderr, "error evbit rel\n");
for (i = REL_X; i < REL_MAX; i++)
if (ioctl(fd, UI_SET_RELBIT, i) < 0)
fprintf(stderr, "error setrelbit %d\n", i);
if (ioctl(fd, UI_DEV_CREATE) < 0)
{
fprintf(stderr, "error creating uinput device\n");
}
/* end uinput init */
/* init framebuffer */
/* Open the file for reading and writing */
fbfd = open(FRAMEBUFFER_FILE, O_RDWR);
if (fbfd == -1)
{
perror("Error: cannot open framebuffer device");
exit(2);
}
/* Get fixed screen information */
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1)
{
perror("Error reading fixed information");
exit(3);
}
/* Get variable screen information */
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1)
{
perror("Error reading variable information");
exit(4);
}
/* Map the device to memory */
fbp = (char *)mmap(0, finfo.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED,
fbfd, (off_t)0);
if (fbp == MAP_FAILED)
{
perror("Error: failed to map framebuffer device to memory");
exit(5);
}
/* end init framebuffer */
/* init touchscreen */
int fdm;
struct input_event ev;
char *touchscreen_filename = find_touchscreen();
if ((fdm = open(touchscreen_filename, O_RDONLY | O_NONBLOCK)) == -1)
{
printf("Device open ERROR\n");
exit(6);
}
free(touchscreen_filename);
fd_set rdfs;
FD_ZERO(&rdfs);
FD_SET(fdm, &rdfs);
/* end init touchscreen */
int pressed = 0, released = 0, abs_x = 0, abs_y = 0, shift = 0, caps = 0, ctrl = 0;
unsigned short pressedkey = 0;
/* TODO: exit condition */
while (1)
{
/* draw keyboard texture */
/* TODO: swap texture for capslock/shift/ctrl, and maybe overlay texture for any key pressed? */
fb_drawimage(BITMAP_FILE, fbp, vinfo.xoffset, vinfo.yoffset, vinfo.bits_per_pixel, finfo.line_length, vinfo.xres, &img_height, &img_width);
/* read and parse input events from touchscreen for coordinates and touch state */
/* TODO: multitouch, n-key rollover (?) */
select(fdm + 1, &rdfs, NULL, NULL, NULL);
released = 0;
while (read(fdm, &ev, sizeof(struct input_event)) && !(ev.type == EV_SYN && ev.code == SYN_REPORT))
{
if (ev.type == EV_ABS)
{
switch (ev.code)
{
case ABS_MT_POSITION_X:
abs_x = ev.value;
pressed = 1;
break;
case ABS_MT_POSITION_Y:
abs_y = ev.value;
pressed = 1;
break;
case ABS_MT_TRACKING_ID:
if (ev.value == -1)
{
pressed = 0;
released = 1;
}
break;
}
}
else if (ev.type == EV_SYN && ev.code == SYN_MT_REPORT)
{
pressed = 0;
released = 1;
}
}
/* push corresponding keys */
if (released && pressedkey)
{
if (pressedkey == KEY_LEFTSHIFT || pressedkey == KEY_RIGHTSHIFT)
{
shift ^= 1;
ev.type = EV_KEY;
ev.code = KEY_LEFTSHIFT;
ev.value = shift;
if (write(fd, &ev, sizeof(ev)) != sizeof(ev))
fprintf(stderr, "error sending uinput event\n");
}
else if (pressedkey == KEY_LEFTCTRL)
{
ctrl ^= 1;
ev.type = EV_KEY;
ev.code = KEY_LEFTCTRL;
ev.value = ctrl;
if (write(fd, &ev, sizeof(ev)) != sizeof(ev))
fprintf(stderr, "error sending uinput event\n");
}
else
{
if (pressedkey == KEY_CAPSLOCK)
caps ^= 1;
send_key(fd, pressedkey);
}
}
/* cross-reference state with table */
pressedkey = 0;
if (pressed)
pressedkey = get_key(abs_x, abs_y);
}
close(fd);
ioctl(fdm, EVIOCGRAB, (void *)0);
close(fdm);
munmap(fbp, screensize);
close(fbfd);
return 0;
}