forked from zrafa/onscreenkeyboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
osk_mouse.c
387 lines (294 loc) · 9.46 KB
/
osk_mouse.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
/*
* osk_mouse.c : framebuffer on-screen keyboard. Mouse or trackball version
*
* 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
*
* osk_mouse.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
*/
#include <fcntl.h>
#include <linux/fb.h>
#include <linux/input.h>
#include <linux/ioctl.h>
#include <linux/uinput.h>
#include <stdint.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>
#include <stdbool.h>
#define UMBRAL 10
/* key codes reference in /usr/include/linux/input-event-codes.h */
unsigned char abc[] = {KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9, KEY_0, KEY_Q, KEY_W, KEY_E, KEY_R, KEY_T, KEY_Y, KEY_U, KEY_I, KEY_O, KEY_P, KEY_A, KEY_S, KEY_D, KEY_F, KEY_G, KEY_H, KEY_J, KEY_K, KEY_L, KEY_ENTER, KEY_Z, KEY_X, KEY_C, KEY_V, KEY_B, KEY_N, KEY_M, KEY_COMMA, KEY_DOT, KEY_ENTER, 0, KEY_SPACE, KEY_SPACE, KEY_SPACE, KEY_COMMA, KEY_DOT, KEY_DOT, KEY_DOT, KEY_DOT, KEY_BACKSPACE};
extern uint8_t _binary_image_mouse_ppm_start[];
extern uint8_t _binary_image_mouse_ppm_end;
extern uint8_t _binary_image_mouse_ppm_size;
/*
* Draw image from PPM file
*/
static void fb_drawimage(FILE *theme_file, char *fbp, int xo, int yo, int bpp, int length, int xres)
{
char head[256];
char s[80];
unsigned char *pixline;
unsigned i, j, width, height, line_size;
memset(head, 0, sizeof(head));
/* parse ppm header */
while (1) {
if (fgets(s, sizeof(s), theme_file) == NULL) {
printf("bad PPM file");
exit(EXIT_FAILURE);
}
if (s[0] == '#')
continue;
if (strlen(head) + strlen(s) >= sizeof(head)) {
printf("bad PPM file");
exit(EXIT_FAILURE);
}
strcat(head, s);
if (head[0] != 'P' || head[1] != '6') {
printf("bad PPM file");
exit(EXIT_FAILURE);
}
/* width, height, max_color_val */
if (sscanf(head, "P6 %u %u %u", &width, &height, &i) == 3)
break;
/* TODO: i must be <= 255! */
}
line_size = width*3;
long int location = 0;
pixline = malloc(line_size);
for (j = 0; j < height; j++) {
unsigned char *pixel = pixline;
if (fread(pixline, 1, line_size, theme_file) != line_size) {
printf("bad PPM file");
exit(EXIT_FAILURE);
}
for (i = 0; i < width; i++) {
pixel += 3;
location = ((xres-width)+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);
}
int send_event(int fd, __u16 type, __u16 code, __s32 value)
{
struct input_event event;
memset(&event, 0, sizeof(event));
event.type = type;
event.code = code;
event.value = value;
if (write(fd, &event, sizeof(event)) != sizeof(event)) {
fprintf(stderr, "Error on send_event");
return -1;
}
return 0;
}
static void fb_drawbox(char *fbp, int x1, int y1, int x2, int y2, int bpp, int length)
{
int x, y;
long int location = 0;
for (y = y1; y < y2; y++)
for (x = x1; x < x2; x++) {
location = (x) * (bpp/8) + (y) * length;
*(fbp + location) = (unsigned char) 100;
*(fbp + location + 1) = (unsigned char) 100;
*(fbp + location + 2) = (unsigned char) 200;
*(fbp + location + 3) = (unsigned char) 255; /* No transparency */
}
}
int main(int argc, char *argv[]) {
if( argc != 2 ) {
printf("Error: Invalid number of arguments.\n");
printf("Usage: %s /dev/input/mice\n", argv[0]);
exit(EXIT_FAILURE);
}
int fbfd = 0;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
long int screensize = 0;
char *fbp = 0;
/* uinput init */
int i,fd;
struct uinput_user_dev device;
memset(&device, 0, sizeof device);
fd=open("/dev/uinput",O_WRONLY);
strcpy(device.name,"test keyboard");
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 setup\n");
}
if (ioctl(fd,UI_SET_EVBIT,EV_KEY) < 0)
fprintf(stderr, "error evbit key\n");
for (i=0;i<50;i++)
if (ioctl(fd,UI_SET_KEYBIT, abc[i]) < 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 create\n");
}
sleep(2);
/* end uinput init */
/* init framebuffer */
/* Open the file for reading and writing */
fbfd = open("/dev/fb0", O_RDWR);
if (fbfd == -1) {
perror("Error: cannot open framebuffer device");
exit(EXIT_FAILURE);
}
printf("The framebuffer device was opened successfully.\n");
/* Get fixed screen information */
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1) {
perror("Error reading fixed information");
exit(EXIT_FAILURE);
}
/* Get variable screen information */
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1) {
perror("Error reading variable information");
exit(EXIT_FAILURE);
}
printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);
/* Figure out the size of the screen in bytes */
screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
/* Map the device to memory */
fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,
fbfd, 0);
if (fbp == MAP_FAILED) {
perror("Error: failed to map framebuffer device to memory");
exit(EXIT_FAILURE);
}
printf("The framebuffer device was mapped to memory successfully.\n");
/* end init framebuffer */
/* init mouse */
int fdm;
struct input_event evm;
if ((fdm = open(argv[1], O_RDONLY | O_NONBLOCK)) == -1) {
printf("Device open ERROR\n");
exit(EXIT_FAILURE);
}
int ret, xp=0, yp=0, rkp=0, ckp=0;
unsigned char button,bLeft,bMiddle,bRight;
char xm,ym;
int absolute_x,absolute_y;
/* end init mouse */
bool pressedLeft=0;
bool pressedMiddle=0;
bool pressedRight=0;
size_t pressedKey=0;
while(1) {
FILE *theme_file=fmemopen(_binary_image_mouse_ppm_start, (size_t)&_binary_image_mouse_ppm_size, "r");
fb_drawimage(theme_file, fbp, vinfo.xoffset, vinfo.yoffset, vinfo.bits_per_pixel, finfo.line_length, vinfo.xres);
/* check mouse */
ret=read(fdm, &evm, sizeof(struct input_event));
if (ret!=-1) {
unsigned char *ptr = (unsigned char*)&evm;
button=ptr[0];
bLeft = button & 0x1;
bMiddle = ( button & 0x4 ) > 0;
bRight = ( button & 0x2 ) > 0;
xm=(char) ptr[1];ym=(char) ptr[2];
if (xm>0) xp++;
else if(xm<0) xp--;
if (ym>0) yp--;
else if(ym<0) yp++;
if (xp==UMBRAL) {
xp=0;
ckp++; if (ckp>9) ckp=9;
} else if (xp==-UMBRAL) {
xp=0;
ckp--; if (ckp<0) ckp=0;
}
if (yp==UMBRAL) {
yp=0;
rkp++; if (rkp>4) rkp=4;
} else if (yp==-UMBRAL) {
yp=0;
rkp--; if (rkp<0) rkp=0;
}
absolute_x+=xm;
absolute_y-=ym;
if (bLeft==1) {
pressedLeft=true;
pressedKey=(rkp*10)+ckp;
} else if ((bLeft==0) && (pressedLeft==true)) {
if (abc[pressedKey] == 0) break;
pressedLeft=false;
send_event(fd, EV_KEY, abc[pressedKey], 1);
send_event(fd, EV_SYN, SYN_REPORT, 0);
send_event(fd, EV_KEY, abc[pressedKey], 0);
send_event(fd, EV_SYN, SYN_REPORT, 0);
}
if (bMiddle==1) {
pressedMiddle=true;
pressedKey=(rkp*10)+ckp;
} else if ((bMiddle==0) && (pressedMiddle==true)) {
if (abc[pressedKey] == 0) break;
pressedMiddle=false;
send_event(fd, EV_KEY, abc[pressedKey], 1);
send_event(fd, EV_SYN, SYN_REPORT, 0);
send_event(fd, EV_KEY, abc[pressedKey], 0);
send_event(fd, EV_SYN, SYN_REPORT, 0);
}
if (bRight==1) {
pressedRight=true;
pressedKey=(rkp*10)+ckp;
} else if ((bRight==0) && (pressedRight==true)) {
if (abc[pressedKey] == 0) break;
pressedRight=false;
send_event(fd, EV_KEY, abc[pressedKey], 1);
send_event(fd, EV_SYN, SYN_REPORT, 0);
send_event(fd, EV_KEY, abc[pressedKey], 0);
send_event(fd, EV_SYN, SYN_REPORT, 0);
}
}
/* the whole mess to draw the pointer/cursor */
int x1, x2, y1, y2;
int anchoi = 240; /* width of keyboard image */
int altoi = 125; /* height of keyboard image */
int nro_columnas=10;
int nro_filas=5;
x1=(vinfo.xres-anchoi)+((anchoi/nro_columnas)*(ckp));
x2=x1+(anchoi/nro_columnas);
y1=(altoi/nro_filas)*(rkp);
y2=y1+4;
fb_drawbox(fbp, x1, y1, x2, y2, vinfo.bits_per_pixel, finfo.line_length);
y1=(altoi/nro_filas)*(rkp)+(altoi/nro_filas-4); // The "4" here is a ugly hardcoded number of pixels less
y2=y1+4; /* 4 is the height of the line */
fb_drawbox(fbp, x1, y1, x2, y2, vinfo.bits_per_pixel, finfo.line_length);
usleep(1000);
}
close(fd);
close(fdm);
munmap(fbp, screensize);
close(fbfd);
return 0;
}