2 * Copyright (c) 2010, GraÅžvydas Ignotas
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.
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.
31 #include <sys/types.h>
36 #include <linux/input.h>
41 #define array_size(x) (sizeof(x) / sizeof(x[0]))
46 unsigned int active:1;
47 unsigned int tested:1;
51 static key_item key_items[] =
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" },
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" },
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" },
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" },
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" },
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" },
120 #define KI_COUNT (sizeof(key_items) / sizeof(key_items[0]))
122 static int nub_range_hit, all_btns_tested, lid_tested, ts_tested;
123 static int ts_old_x, ts_old_y;
124 static int lid_closed;
126 static void draw_ts_cross(unsigned short *fb, unsigned short col, int x, int y)
130 if (y >= 0 && y < 480)
131 for (x1 = x - 5, i = 0; i < 11; i++, x1++)
132 if (x1 >= 0 && x1 < 800) fb[y * 800 + x1] = col;
134 if (x >= 0 && x < 800)
135 for (y1 = y - 5, i = 0; i < 11; i++, y1++)
136 if (y1 >= 0 && y1 < 480) fb[y1 * 800 + x] = col;
138 if (x != ts_old_x && y != ts_old_y)
145 static void text_out16(unsigned short *fb, int x, int y, unsigned short col, const char *text)
151 for (i = 0; i < strlen(text); i++)
155 #define pix(fdmask,add) \
156 if (fontdata8x8[((text[i])*8)+l]&fdmask) \
157 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
172 static void text_out16_small(unsigned short *fb, int x, int y, unsigned short col, const char *text)
178 for (i = 0; i < strlen(text); i++)
182 #define pix(fdmask,add) \
183 if (fontdata8x8[((text[i])*8)+l]&fdmask) \
184 fb[l*800+add]=fb[l*800+800+add]=col
199 static void redraw_nubs(unsigned short *fb, int x1, int y1, int x2, int y2)
206 if (nub_range_hit == 0xff && all_btns_tested && lid_tested && ts_tested)
210 for (y = 0; y < 140; y++) {
211 memset(fb + 800*y + 200, fill, (64+8)*2*2);
212 memset(fb + 800*y + 450, fill, (64+8)*2*2);
215 text_out16(fb + 200, 32*2 + x1/8, 32*2 + y1/8, 0x001f, "@");
216 text_out16(fb + 450, 32*2 + x2/8, 32*2 + y2/8, 0x001f, "@");
218 if (x1 == -256) nub_range_hit |= 0x01;
219 if (x1 == 256) nub_range_hit |= 0x02;
220 if (y1 == -256) nub_range_hit |= 0x04;
221 if (y1 == 256) nub_range_hit |= 0x08;
222 if (x2 == -256) nub_range_hit |= 0x10;
223 if (x2 == 256) nub_range_hit |= 0x20;
224 if (y2 == -256) nub_range_hit |= 0x40;
225 if (y2 == 256) nub_range_hit |= 0x80;
227 snprintf(buff, sizeof(buff), "%3i", x1/8);
228 text_out16_small(fb, 235, 130, (nub_range_hit & 0x03) == 0x03 ? 0xce6f : 0x7bef, buff);
229 snprintf(buff, sizeof(buff), "%3i", y1/8);
230 text_out16_small(fb, 270, 130, (nub_range_hit & 0x0c) == 0x0c ? 0xce6f : 0x7bef, buff);
232 snprintf(buff, sizeof(buff), "%3i", x2/8);
233 text_out16_small(fb, 485, 130, (nub_range_hit & 0x30) == 0x30 ? 0xce6f : 0x7bef, buff);
234 snprintf(buff, sizeof(buff), "%3i", y2/8);
235 text_out16_small(fb, 520, 130, (nub_range_hit & 0xc0) == 0xc0 ? 0xce6f : 0x7bef, buff);
238 static void redraw_keys_lid(unsigned short *fb)
243 for (i = 0; i < KI_COUNT; i++)
247 text_out16(fb, key->x, key->y,
248 key->active ? 0x07e0 : (key->tested ? 0xce6f : 0x7bef),
252 text_out16(fb, 10, 260, lid_closed ? 0x07e0 : (lid_tested ? 0xce6f : 0x7bef), "LID");
255 static void set_key(int code, int val)
257 key_item *key = NULL;
260 for (i = 0; i < KI_COUNT; i++)
262 if (key_items[i].code == code)
271 printf("%c unexpected key? (%i)\n", val ? '+' : '-', code);
276 key->tested |= !!val;
277 printf("%c %s\n", val ? '+' : '-', key->name);
283 for (i = 0; i < KI_COUNT; i++)
285 if (key_items[i].code == KEY_MENU)
287 if (key_items[i].code == KEY_KPPLUS || key_items[i].code == KEY_KPMINUS)
289 if (!key_items[i].tested)
298 #include <sys/soundcard.h>
301 static int snd_test_l, snd_test_r, snd_test_quit;
302 static pthread_cond_t snd_cond = PTHREAD_COND_INITIALIZER;
303 static pthread_mutex_t snd_mutex = PTHREAD_MUTEX_INITIALIZER;
305 static void *sound_thread(void *arg)
307 int i, ret, randfd, dspfd;
308 int frag, bits, stereo, rate;
309 short buf[8*1024];//2*22050 / 10];
311 randfd = open("/dev/urandom", O_RDONLY);
313 perror("open(\"/dev/urandom\")");
317 dspfd = open("/dev/dsp", O_WRONLY);
319 perror("open(\"/dev/dsp\")");
324 frag = (2 << 16) | 13;
325 ret = ioctl(dspfd, SNDCTL_DSP_SETFRAGMENT, &frag);
327 perror("SNDCTL_DSP_SETFRAGMENT");
329 stereo = 1; bits = 16; rate = 22050;
330 ret = ioctl(dspfd, SNDCTL_DSP_STEREO, &stereo);
332 ret = ioctl(dspfd, SNDCTL_DSP_SETFMT, &bits);
334 ret = ioctl(dspfd, SNDCTL_DSP_SPEED, &rate);
336 perror("failed to set audio format");
339 pthread_mutex_lock(&snd_mutex);
340 if (!snd_test_l && !snd_test_r)
341 pthread_cond_wait(&snd_cond, &snd_mutex);
342 pthread_mutex_unlock(&snd_mutex);
347 ret = read(randfd, buf, sizeof(buf));
349 perror("urandom read");
354 for (i = 0; i < array_size(buf) / 2; i++)
357 for (i = 0; i < array_size(buf) / 2; i++)
360 ret = write(dspfd, buf, sizeof(buf));
373 static void sound_init(void)
378 ret = pthread_create(&tid, NULL, sound_thread, NULL);
380 fprintf(stderr, "pthread_create sound_thread: %d\n", ret);
386 static void sound_do(int l, int r)
388 pthread_mutex_lock(&snd_mutex);
391 if (l || r || snd_test_quit)
392 pthread_cond_signal(&snd_cond);
393 pthread_mutex_unlock(&snd_mutex);
406 int main(int argc, char *argv[])
408 unsigned short *screen;
409 int fbdev, ifd[DEVS_TOTAL] = { -1, -1, -1, -1, -1, -1 };
410 int i, id, imaxfd = 0, ts_x = 0, ts_y = 0;
411 int nubx[2] = {0,0}, nuby[2] = {0,0};
412 int pressed_l = 0, pressed_r = 0;
413 struct tsdev *ts = NULL;
415 fbdev = open("/dev/fb0", O_RDWR);
418 perror("open(\"/dev/fb0\") failed");
422 screen = mmap(0, 800*480*2, PROT_WRITE|PROT_READ, MAP_SHARED, fbdev, 0);
423 if (screen == MAP_FAILED)
425 perror("mmap(fbptr) failed");
429 memset(screen, 0, 800*480*2);
436 char name[256] = { 0, };
439 snprintf(fname, sizeof(fname), "/dev/input/event%i", id);
440 fd = open(fname, O_RDONLY);
446 ioctl(fd, EVIOCGNAME(sizeof(name)), name);
448 if (strcasestr(name, "power") != NULL || strcasestr(name, "pwrbutton") != NULL)
450 ifd[DEV_PWRBTN] = fd;
452 else if (strcasestr(name, "keypad") != NULL)
454 ifd[DEV_KEYPAD] = fd;
456 else if (strcmp(name, "gpio-keys") == 0)
458 ifd[DEV_BUTTONS] = fd;
460 else if (strcasestr(name, "touchscreen") != NULL)
463 ts = ts_open(fname, 0);
474 ifd[DEV_TS] = ts_fd(ts);
476 else if (strcmp(name, "nub0") == 0)
480 else if (strcmp(name, "nub1") == 0)
486 printf("skipping \"%s\"\n", name);
490 if (imaxfd < fd) imaxfd = fd;
493 if (ifd[DEV_PWRBTN] == -1) printf("Warning: couldn't find pwrbutton device\n");
494 if (ifd[DEV_KEYPAD] == -1) printf("Warning: couldn't find keypad device\n");
495 if (ifd[DEV_BUTTONS] == -1) printf("Warning: couldn't find button device\n");
496 if (ifd[DEV_TS] == -1) printf("Warning: couldn't find touchscreen device\n");
497 if (ifd[DEV_LNUB] == -1) printf("Warning: couldn't find nub1 device\n");
498 if (ifd[DEV_RNUB] == -1) printf("Warning: couldn't find nub2 device\n");
501 int early_leave_attempt = 0;
502 text_out16_small(screen, 220, 3, 0x7bef,
503 "press menu/pandora to exit");
505 text_out16_small(screen, 320, 3, 0x7bef, "Press L+R to exit");
511 while (!pressed_l || !pressed_r)
514 struct input_event ev[64];
515 int fd = -1, rd, which, ret;
519 if (early_leave_attempt) {
521 snprintf(buff, sizeof(buff), "not everything tested (%s%s%s%s), L+R to override",
522 nub_range_hit == 0xff ? "" : "nubs", all_btns_tested ? "" : " buttons",
523 lid_tested ? "" : " lid_switch", ts_tested ? "" : " touchscreen");
524 memset(screen, 0, 800*12*2);
525 if (nub_range_hit == 0xff && all_btns_tested && lid_tested && ts_tested)
526 text_out16_small(screen, 220, 3, 0x07c0,
527 "press menu/pandora to shutdown, L+Start to exit");
529 text_out16_small(screen, 4, 3, 0xf800, buff);
532 draw_ts_cross(screen, 0x0000, ts_old_x, ts_old_y);
533 redraw_keys_lid(screen);
534 redraw_nubs(screen, nubx[0], nuby[0], nubx[1], nuby[1]);
535 draw_ts_cross(screen, 0xf800, ts_x, ts_y);
538 for (i = 0; i < DEVS_TOTAL; i++)
540 FD_SET(ifd[i], &fdset);
542 ret = select(imaxfd + 1, &fdset, NULL, NULL, NULL);
549 for (i = 0; i < DEVS_TOTAL; i++)
550 if (ifd[i] != -1 && FD_ISSET(ifd[i], &fdset))
554 if (fd == ifd[DEV_TS])
556 struct ts_sample samp;
558 ret = ts_read(ts, &samp, 1);
567 ts_x = samp.x; ts_y = samp.y;
568 //printf("ts: %6d %6d %6d\n", samp.x, samp.y, samp.pressure);
572 /* buttons or keypad */
573 rd = read(fd, ev, sizeof(ev));
574 if (rd < (int) sizeof(ev[0])) {
575 perror("\nevtest: error reading");
579 for (i = 0; i < rd / sizeof(ev[0]); i++)
581 switch (ev[i].type) {
585 set_key(ev[i].code, ev[i].value);
587 if (ev[i].code == KEY_RIGHTSHIFT)
588 pressed_l = !!ev[i].value;
589 if (ev[i].code == KEY_RIGHTCTRL)
590 pressed_r = !!ev[i].value;
592 if (ev[i].code == KEY_LEFTALT && !!ev[i].value && pressed_l) {
593 // L+Start, legacy code
594 if (nub_range_hit == 0xff && all_btns_tested && lid_tested && ts_tested)
596 if (pressed_l && pressed_r)
598 early_leave_attempt = 1;
600 if (ev[i].code == KEY_MENU && !!ev[i].value) {
601 if ((nub_range_hit == 0xff && all_btns_tested && lid_tested && ts_tested) ||
602 (pressed_l && pressed_r)) {
604 //system("poweroff -f");
607 early_leave_attempt = 1;
610 if (ev[i].code == KEY_LEFT)
611 sound_do(!!ev[i].value, snd_test_r);
612 if (ev[i].code == KEY_RIGHT)
613 sound_do(snd_test_l, !!ev[i].value);
616 which = (fd == ifd[DEV_LNUB]) ? 0 : 1;
617 if (ev[i].code == ABS_X)
618 nubx[which] = ev[i].value;
619 else if (ev[i].code == ABS_Y)
620 nuby[which] = ev[i].value;
622 printf("unexpected EV_ABS code: %i\n", ev[i].code);
625 if (ev[i].code == SW_LID) {
626 lid_closed = ev[i].value;
627 lid_tested |= ev[i].value;
630 printf("unexpected EV_SW code: %i\n", ev[i].code);
633 if (ev[i].code == MSC_SCAN)
637 printf("unexpected event: type %i, code %d\n", ev[i].type, ev[i].code);
649 for (i = 0; i < DEVS_TOTAL; i++)
650 if (i != DEV_TS && ifd[i] != -1)
652 munmap(screen, 800*480*2);