show keys that have been checked
[pandora-misc.git] / op_test_inputs.c
1 /*
2  * Copyright (c) 2010, GraÅžvydas Ignotas
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of the organization nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 #define _GNU_SOURCE
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <sys/mman.h>
35 #include <unistd.h>
36 #include <linux/input.h>
37 #include <tslib.h>
38
39 #include "font.c"
40
41 #define array_size(x) (sizeof(x) / sizeof(x[0]))
42
43 typedef struct {
44         int x, y;
45         int code;
46         unsigned int active:1;
47         unsigned int tested:1;
48         const char *name;
49 } key_item;
50
51 static key_item key_items[] =
52 {
53         {  66,  26, KEY_1, 0, 0, "1" },
54         { 133,  26, KEY_2, 0, 0, "2" },
55         { 200,  26, KEY_3, 0, 0, "3" },
56         { 266,  26, KEY_4, 0, 0, "4" },
57         { 333,  26, KEY_5, 0, 0, "5" },
58         { 400,  26, KEY_6, 0, 0, "6" },
59         { 466,  26, KEY_7, 0, 0, "7" },
60         { 533,  26, KEY_8, 0, 0, "8" },
61         { 600,  26, KEY_9, 0, 0, "9" },
62         { 666,  26, KEY_0, 0, 0, "0" },
63         { 733,  26, KEY_BACKSPACE, 0, 0, "DEL" },
64         //
65         {  10,  55, KEY_RIGHTSHIFT,0, 0, "L" },
66         {  10,  75, KEY_KPPLUS,    0, 0, "L2" },
67         { 758,  55, KEY_RIGHTCTRL, 0, 0, "R" },
68         { 758,  75, KEY_KPMINUS,   0, 0, "R2" },
69         {  10, 220, KEY_POWER,     0, 0, "POWER" },
70         {  10, 240, KEY_COFFEE,    0, 0, "HOLD" },
71         //
72         { 130,  94, KEY_UP,     0, 0, "U" },
73         {  80, 163, KEY_LEFT,   0, 0, "L" },
74         { 180, 163, KEY_RIGHT,  0, 0, "R" },
75         { 130, 232, KEY_DOWN,   0, 0, "D" },
76         { 376,  94, KEY_LEFTALT,0, 0, "ALT" },
77         { 368, 163, KEY_LEFTCTRL,0,0, "CTRL" },
78         { 368, 232, KEY_MENU,   0, 0, "MENU" },
79         { 700,  94, KEY_PAGEUP, 0, 0, "Y" },
80         { 650, 163, KEY_HOME,   0, 0, "A" },
81         { 750, 163, KEY_END,    0, 0, "B" },
82         { 700, 232, KEY_PAGEDOWN,0,0, "X" },
83         //
84         {  92, 300, KEY_Q,      0, 0, "Q" },
85         { 158, 300, KEY_W,      0, 0, "W" },
86         { 225, 300, KEY_E,      0, 0, "E" },
87         { 292, 300, KEY_R,      0, 0, "R" },
88         { 358, 300, KEY_T,      0, 0, "T" },
89         { 425, 300, KEY_Y,      0, 0, "Y" },
90         { 492, 300, KEY_U,      0, 0, "U" },
91         { 558, 300, KEY_I,      0, 0, "I" },
92         { 625, 300, KEY_O,      0, 0, "O" },
93         { 692, 300, KEY_P,      0, 0, "P" },
94         //
95         {  30, 369, KEY_LEFTSHIFT, 0, 0, "SHIFT" },
96         { 133, 369, KEY_A,      0, 0, "A" },
97         { 200, 369, KEY_S,      0, 0, "S" },
98         { 266, 369, KEY_D,      0, 0, "D" },
99         { 333, 369, KEY_F,      0, 0, "F" },
100         { 400, 369, KEY_G,      0, 0, "G" },
101         { 466, 369, KEY_H,      0, 0, "H" },
102         { 533, 369, KEY_J,      0, 0, "J" },
103         { 600, 369, KEY_K,      0, 0, "K" },
104         { 666, 369, KEY_L,      0, 0, "L" },
105         { 710, 369, KEY_ENTER,  0, 0, "ENTER" },
106         //
107         {  25, 437, KEY_COMMA,  0, 0, "," },
108         {  92, 437, KEY_DOT,    0, 0, "." },
109         { 158, 437, KEY_Z,      0, 0, "Z" },
110         { 225, 437, KEY_X,      0, 0, "X" },
111         { 292, 437, KEY_C,      0, 0, "C" },
112         { 358, 437, KEY_V,      0, 0, "V" },
113         { 425, 437, KEY_B,      0, 0, "B" },
114         { 492, 437, KEY_N,      0, 0, "N" },
115         { 558, 437, KEY_M,      0, 0, "M" },
116         { 625, 437, KEY_SPACE,  0, 0, "SPACE" },
117         { 758, 437, KEY_FN,     0, 0, "Fn" },
118 };
119
120 #define KI_COUNT (sizeof(key_items) / sizeof(key_items[0]))
121
122 static int ts_old_x = 0, ts_old_y = 0;
123 static int lid_closed = 0;
124
125 static void draw_ts_cross(unsigned short *fb, unsigned short col, int x, int y)
126 {
127         int i, x1, y1;
128
129         if (y >= 0 && y < 480)
130                 for (x1 = x - 5, i = 0; i < 11; i++, x1++)
131                         if (x1 >= 0 && x1 < 800) fb[y * 800 + x1] = col;
132         
133         if (x >= 0 && x < 800)
134                 for (y1 = y - 5, i = 0; i < 11; i++, y1++)
135                         if (y1 >= 0 && y1 < 480) fb[y1 * 800 + x] = col;
136         
137         ts_old_x = x;
138         ts_old_y = y;
139 }
140
141 static void text_out16(unsigned short *fb, int x, int y, unsigned short col, const char *text)
142 {
143         int i,l;
144
145         fb = fb + x + y*800;
146
147         for (i = 0; i < strlen(text); i++)
148         {
149                 for (l=0;l<8;l++)
150                 {
151                         #define pix(fdmask,add) \
152                                 if (fontdata8x8[((text[i])*8)+l]&fdmask) \
153                                         fb[l*2*800+add]=fb[l*2*800+add+1]=fb[l*2*800+800+add]=fb[l*2*800+800+add+1]=col
154                         pix(0x80,  0);
155                         pix(0x40,  2);
156                         pix(0x20,  4);
157                         pix(0x10,  6);
158                         pix(0x08,  8);
159                         pix(0x04, 10);
160                         pix(0x02, 12);
161                         pix(0x01, 14);
162                         #undef pix
163                 }
164                 fb += 8*2;
165         }
166 }
167
168 static void text_out16_small(unsigned short *fb, int x, int y, unsigned short col, const char *text)
169 {
170         int i,l;
171
172         fb = fb + x + y*800;
173
174         for (i = 0; i < strlen(text); i++)
175         {
176                 for (l=0;l<8;l++)
177                 {
178                         #define pix(fdmask,add) \
179                                 if (fontdata8x8[((text[i])*8)+l]&fdmask) \
180                                         fb[l*800+add]=fb[l*800+800+add]=col
181                         pix(0x80,  0);
182                         pix(0x40,  1);
183                         pix(0x20,  2);
184                         pix(0x10,  3);
185                         pix(0x08,  4);
186                         pix(0x04,  5);
187                         pix(0x02,  6);
188                         pix(0x01,  7);
189                         #undef pix
190                 }
191                 fb += 8;
192         }
193 }
194
195 static void redraw_nubs(unsigned short *fb, int x1, int y1, int x2, int y2)
196 {
197         static int range_hit;
198         char buff[32];
199         int y;
200
201         fb += 90*800;
202
203         // clear areas
204         for (y = 0; y < 140; y++) {
205                 memset(fb + 800*y + 200, 0, (64+8)*2*2);
206                 memset(fb + 800*y + 450, 0, (64+8)*2*2);
207         }
208
209         text_out16(fb + 200, 32*2 + x1/8, 32*2 + y1/8, 0x001f, "@");
210         text_out16(fb + 450, 32*2 + x2/8, 32*2 + y2/8, 0x001f, "@");
211
212         if (x1 == -256) range_hit |= 0x01;
213         if (x1 ==  256) range_hit |= 0x02;
214         if (y1 == -256) range_hit |= 0x04;
215         if (y1 ==  256) range_hit |= 0x08;
216         if (x2 == -256) range_hit |= 0x10;
217         if (x2 ==  256) range_hit |= 0x20;
218         if (y2 == -256) range_hit |= 0x40;
219         if (y2 ==  256) range_hit |= 0x80;
220
221         snprintf(buff, sizeof(buff), "%3i", x1/8);
222         text_out16_small(fb, 235, 130, (range_hit & 0x03) == 0x03 ? 0xce6f : 0x7bef, buff);
223         snprintf(buff, sizeof(buff), "%3i", y1/8);
224         text_out16_small(fb, 270, 130, (range_hit & 0x0c) == 0x0c ? 0xce6f : 0x7bef, buff);
225
226         snprintf(buff, sizeof(buff), "%3i", x2/8);
227         text_out16_small(fb, 485, 130, (range_hit & 0x30) == 0x30 ? 0xce6f : 0x7bef, buff);
228         snprintf(buff, sizeof(buff), "%3i", y2/8);
229         text_out16_small(fb, 520, 130, (range_hit & 0xc0) == 0xc0 ? 0xce6f : 0x7bef, buff);
230 }
231
232 static void redraw_keys_lid(unsigned short *fb)
233 {
234         const key_item *key;
235         int i;
236
237         for (i = 0; i < KI_COUNT; i++)
238         {
239                 key = &key_items[i];
240
241                 text_out16(fb, key->x, key->y,
242                         key->active ? 0x07e0 : (key->tested ? 0xce6f : 0x7bef),
243                         key->name);
244         }
245
246         text_out16(fb, 10, 260, lid_closed ? 0x07e0 : 0x7bef, "LID");
247 }
248
249 static void set_key(int code, int val)
250 {
251         key_item *key = NULL;
252         int i;
253
254         for (i = 0; i < KI_COUNT; i++)
255         {
256                 if (key_items[i].code == code)
257                 {
258                         key = &key_items[i];
259                         break;
260                 }
261         }
262
263         if (key == NULL)
264         {
265                 printf("%c unexpected key? (%i)\n", val ? '+' : '-', code);
266         }
267         else
268         {
269                 key->active = !!val;
270                 key->tested |= !!val;
271                 printf("%c %s\n", val ? '+' : '-', key->name);
272         }
273 }
274
275 /* sound test */
276 #include <sys/soundcard.h>
277 #include <pthread.h>
278
279 static int snd_test_l, snd_test_r, snd_test_quit;
280 static pthread_cond_t snd_cond = PTHREAD_COND_INITIALIZER;
281 static pthread_mutex_t snd_mutex = PTHREAD_MUTEX_INITIALIZER;
282
283 static void *sound_thread(void *arg)
284 {
285         int i, ret, randfd, dspfd;
286         int frag, bits, stereo, rate;
287         short buf[8*1024];//2*22050 / 10];
288
289         randfd = open("/dev/urandom", O_RDONLY);
290         if (randfd == -1) {
291                 perror("open(\"/dev/urandom\")");
292                 return NULL;
293         }
294
295         dspfd = open("/dev/dsp", O_WRONLY);
296         if (dspfd == -1) {
297                 perror("open(\"/dev/dsp\")");
298                 close(randfd);
299                 return NULL;
300         }
301
302         frag = (2 << 16) | 13;
303         ret = ioctl(dspfd, SNDCTL_DSP_SETFRAGMENT, &frag);
304         if (ret < 0)
305                 perror("SNDCTL_DSP_SETFRAGMENT");
306
307         stereo = 1; bits = 16; rate = 22050;
308         ret = ioctl(dspfd, SNDCTL_DSP_STEREO, &stereo);
309         if (ret == 0)
310                 ret = ioctl(dspfd, SNDCTL_DSP_SETFMT, &bits);
311         if (ret == 0)
312                 ret = ioctl(dspfd, SNDCTL_DSP_SPEED, &rate);
313         if (ret < 0)
314                 perror("failed to set audio format");
315
316         while (1) {
317                 pthread_mutex_lock(&snd_mutex);
318                 if (!snd_test_l && !snd_test_r)
319                         pthread_cond_wait(&snd_cond, &snd_mutex);
320                 pthread_mutex_unlock(&snd_mutex);
321
322                 if (snd_test_quit)
323                         break;
324
325                 ret = read(randfd, buf, sizeof(buf));
326                 if (ret == -1) {
327                         perror("urandom read");
328                         break;
329                 }
330
331                 if (!snd_test_l)
332                         for (i = 0; i < array_size(buf) / 2; i++)
333                                 buf[i * 2] = 0;
334                 if (!snd_test_r)
335                         for (i = 0; i < array_size(buf) / 2; i++)
336                                 buf[i * 2 + 1] = 0;
337
338                 ret = write(dspfd, buf, sizeof(buf));
339                 if (ret == -1) {
340                         perror("dsp write");
341                         break;
342                 }
343         }
344
345         close(randfd);
346         close(dspfd);
347
348         return NULL;
349 }
350
351 static void sound_init(void)
352 {
353         pthread_t tid;
354         int ret;
355
356         ret = pthread_create(&tid, NULL, sound_thread, NULL);
357         if (ret != 0) {
358                 fprintf(stderr, "pthread_create sound_thread: %d\n", ret);
359                 return;
360         }
361         pthread_detach(tid);
362 }
363
364 static void sound_do(int l, int r)
365 {
366         pthread_mutex_lock(&snd_mutex);
367         snd_test_l = l;
368         snd_test_r = r;
369         if (l || r || snd_test_quit)
370                 pthread_cond_signal(&snd_cond);
371         pthread_mutex_unlock(&snd_mutex);
372 }
373
374 enum {
375         DEV_PWRBTN,
376         DEV_KEYPAD,
377         DEV_BUTTONS,
378         DEV_TS,
379         DEV_LNUB,
380         DEV_RNUB,
381         DEVS_TOTAL
382 };
383
384 int main(int argc, char *argv[])
385 {
386         unsigned short *screen;
387         int fbdev, ifd[DEVS_TOTAL] = { -1, -1, -1, -1, -1, -1 };
388         int i, id, imaxfd = 0, ts_x = 0, ts_y = 0;
389         int nubx[2] = {0,0}, nuby[2] = {0,0};
390         int pressed_l = 0, pressed_r = 0;
391         struct tsdev *ts = NULL;
392
393         fbdev = open("/dev/fb0", O_RDWR);
394         if (fbdev == -1)
395         {
396                 perror("open(\"/dev/fb0\") failed");
397                 return 1;
398         }
399
400         screen = mmap(0, 800*480*2, PROT_WRITE|PROT_READ, MAP_SHARED, fbdev, 0);
401         if (screen == MAP_FAILED)
402         {
403                 perror("mmap(fbptr) failed");
404                 return 1;
405         }
406
407         memset(screen, 0, 800*480*2);
408
409         sound_init();
410
411         for (id = 0; ; id++)
412         {
413                 char fname[64];
414                 char name[256] = { 0, };
415                 int fd;
416
417                 snprintf(fname, sizeof(fname), "/dev/input/event%i", id);
418                 fd = open(fname, O_RDONLY);
419                 if (fd == -1)
420                 {
421                         break;
422                 }
423
424                 ioctl(fd, EVIOCGNAME(sizeof(name)), name);
425
426                 if (strcasestr(name, "power") != NULL)
427                 {
428                         ifd[DEV_PWRBTN] = fd;
429                 }
430                 else if (strcasestr(name, "keypad") != NULL)
431                 {
432                         ifd[DEV_KEYPAD] = fd;
433                 }
434                 else if (strcmp(name, "gpio-keys") == 0)
435                 {
436                         ifd[DEV_BUTTONS] = fd;
437                 }
438                 else if (strcasestr(name, "touchscreen") != NULL)
439                 {
440                         close(fd);
441                         ts = ts_open(fname, 0);
442                         if (ts == NULL)
443                         {
444                                 perror("ts_open");
445                                 goto end;
446                         }
447                         if (ts_config(ts))
448                         {
449                                 perror("ts_config");
450                                 goto end;
451                         }
452                         ifd[DEV_TS] = ts_fd(ts);
453                 }
454                 else if (strcmp(name, "nub0") == 0)
455                 {
456                         ifd[DEV_LNUB] = fd;
457                 }
458                 else if (strcmp(name, "nub1") == 0)
459                 {
460                         ifd[DEV_RNUB] = fd;
461                 }
462                 else
463                 {
464                         printf("skipping \"%s\"\n", name);
465                         close(fd);
466                         continue;
467                 }
468                 if (imaxfd < fd) imaxfd = fd;
469         }
470
471         if (ifd[DEV_PWRBTN]  == -1) printf("Warning: couldn't find pwrbutton device\n");
472         if (ifd[DEV_KEYPAD]  == -1) printf("Warning: couldn't find keypad device\n");
473         if (ifd[DEV_BUTTONS] == -1) printf("Warning: couldn't find button device\n");
474         if (ifd[DEV_TS]      == -1) printf("Warning: couldn't find touchscreen device\n");
475         if (ifd[DEV_LNUB]    == -1) printf("Warning: couldn't find nub1 device\n");
476         if (ifd[DEV_RNUB]    == -1) printf("Warning: couldn't find nub2 device\n");
477
478 #ifdef VALIDATION
479         text_out16_small(screen, 220, 3, 0x7bef, "press menu/pandora to shutdown, alt/start to exit");
480 #else
481         text_out16_small(screen, 320, 3, 0x7bef, "Press L+R to exit");
482 #endif
483
484         while (!pressed_l || !pressed_r)
485         {
486                 struct input_event ev[64];
487                 int fd = -1, rd, which, ret;
488                 fd_set fdset;
489
490                 draw_ts_cross(screen, 0x0000, ts_old_x, ts_old_y);
491                 redraw_keys_lid(screen);
492                 redraw_nubs(screen, nubx[0], nuby[0], nubx[1], nuby[1]);
493                 draw_ts_cross(screen, 0xf800, ts_x, ts_y);
494
495                 FD_ZERO(&fdset);
496                 for (i = 0; i < DEVS_TOTAL; i++)
497                         if (ifd[i] != -1)
498                                 FD_SET(ifd[i], &fdset);
499
500                 ret = select(imaxfd + 1, &fdset, NULL, NULL, NULL);
501                 if (ret == -1)
502                 {
503                         perror("select");
504                         break;
505                 }
506
507                 for (i = 0; i < DEVS_TOTAL; i++)
508                         if (ifd[i] != -1 && FD_ISSET(ifd[i], &fdset))
509                                 fd = ifd[i];
510
511                 /* touch event? */
512                 if (fd == ifd[DEV_TS])
513                 {
514                         struct ts_sample samp;
515
516                         ret = ts_read(ts, &samp, 1);
517                         if (ret < 0) {
518                                 perror("ts_read");
519                                 break;
520                         }
521
522                         if (ret != 1)
523                                 continue;
524
525                         ts_x = samp.x; ts_y = samp.y;
526                         //printf("ts: %6d %6d %6d\n", samp.x, samp.y, samp.pressure);
527                         continue;
528                 }
529
530                 /* buttons or keypad */
531                 rd = read(fd, ev, sizeof(ev));
532                 if (rd < (int) sizeof(ev[0])) {
533                         perror("\nevtest: error reading");
534                         break;
535                 }
536
537                 for (i = 0; i < rd / sizeof(ev[0]); i++)
538                 {
539                         switch (ev[i].type) {
540                         case EV_SYN:
541                                 break;
542                         case EV_KEY:
543                                 set_key(ev[i].code, ev[i].value);
544 #ifndef VALIDATION
545                                 if (ev[i].code == KEY_RIGHTSHIFT)
546                                         pressed_l = !!ev[i].value;
547                                 if (ev[i].code == KEY_RIGHTCTRL)
548                                         pressed_r = !!ev[i].value;
549 #else
550                                 if (ev[i].code == KEY_LEFTALT && !!ev[i].value)
551                                         goto end;
552                                 if (ev[i].code == KEY_MENU && !!ev[i].value) {
553                                         sync();
554                                         system("poweroff -f");
555                                         goto end;
556                                 }
557 #endif
558                                 if (ev[i].code == KEY_LEFT)
559                                         sound_do(!!ev[i].value, snd_test_r);
560                                 if (ev[i].code == KEY_RIGHT)
561                                         sound_do(snd_test_l, !!ev[i].value);
562                                 break;
563                         case EV_ABS:
564                                 which = (fd == ifd[DEV_LNUB]) ? 0 : 1;
565                                 if (ev[i].code == ABS_X)
566                                         nubx[which] = ev[i].value;
567                                 else if (ev[i].code == ABS_Y)
568                                         nuby[which] = ev[i].value;
569                                 else
570                                         printf("unexpected EV_ABS code: %i\n", ev[i].code);
571                                 break;
572                         case EV_SW:
573                                 if (ev[i].code == SW_LID)
574                                         lid_closed = ev[i].value;
575                                 else
576                                         printf("unexpected EV_SW code: %i\n", ev[i].code);
577                                 break;
578                         case EV_MSC:
579                                 if (ev[i].code == MSC_SCAN)
580                                         break;
581                                 /* fallthrough */
582                         default:
583                                 printf("unexpected event: type %i, code %d\n", ev[i].type, ev[i].code);
584                                 break;
585                         }
586                 }
587         }
588
589         snd_test_quit = 1;
590         sound_do(0, 0);
591
592 end:
593         if (ts != NULL)
594                 ts_close(ts);
595         for (i = 0; i < DEVS_TOTAL; i++)
596                 if (i != DEV_TS && ifd[i] != -1)
597                         close(ifd[i]);
598         munmap(screen, 800*480*2);
599         close(fbdev);
600         return 0;
601 }
602