allow arguments and fix environment handling
[ginge.git] / loader / host_pnd.c
CommitLineData
3d295a9f 1// vim:shiftwidth=2:expandtab
2#define _GNU_SOURCE
3#include <stdio.h>
4#include <string.h>
5#include <sys/types.h>
6#include <sys/stat.h>
7#include <fcntl.h>
8#include <unistd.h>
9#include <sys/ioctl.h>
10#include <errno.h>
11#include <linux/input.h>
12
13#include "header.h"
14
15static int ifd = -1;
16static int keystate;
17
18static void init(void)
19{
20 char buff[64];
21 int i;
22
23 for (ifd = -1, i = 0; ; i++) {
24 snprintf(buff, sizeof(buff), "/dev/input/event%i", i);
25 ifd = open(buff, O_RDONLY | O_NONBLOCK);
26 if (ifd == -1)
27 break;
28
29 ioctl(ifd, EVIOCGNAME(sizeof(buff)), buff);
30
31 if (strcasestr(buff, "gpio") != NULL)
32 break;
33 close(ifd);
34 }
35
36 if (ifd < 0)
37 printf("no input device\n");
38}
39
40static const struct {
41 unsigned short key;
42 unsigned short btn;
43} key_map[] = {
44 { KEY_LEFT, GP2X_LEFT },
45 { KEY_RIGHT, GP2X_RIGHT },
46 { KEY_UP, GP2X_UP },
47 { KEY_DOWN, GP2X_DOWN },
48 { KEY_PAGEUP, GP2X_Y },
49 { BTN_BASE, GP2X_Y },
50 { KEY_END, GP2X_B },
51 { BTN_BASE2, GP2X_B },
52 { KEY_PAGEDOWN, GP2X_X },
53 { BTN_BASE3, GP2X_X },
54 { KEY_HOME, GP2X_A },
55 { BTN_BASE4, GP2X_A },
56 { KEY_RIGHTSHIFT, GP2X_L },
57 { BTN_TL, GP2X_L },
58 { KEY_RIGHTCTRL, GP2X_R },
59 { BTN_TR, GP2X_R },
60 { KEY_LEFTALT, GP2X_START },
61 { BTN_START, GP2X_START },
62 { KEY_LEFTCTRL, GP2X_SELECT },
63 { BTN_SELECT, GP2X_SELECT },
64};
65
66int host_read_btns(void)
67{
68 struct input_event ev;
69 int i, ret;
70
71 if (ifd < 0)
72 init();
73 if (ifd < 0)
74 return keystate;
75
76 while (1)
77 {
78 ret = read(ifd, &ev, sizeof(ev));
79 if (ret < (int) sizeof(ev)) {
80 if (errno != EAGAIN && errno != EWOULDBLOCK)
81 perror("evtest: read error");
82
83 return keystate;
84 }
85
86 if (ev.type != EV_KEY)
87 continue;
88
89 for (i = 0; i < sizeof(key_map) / sizeof(key_map[0]); i++) {
90 if (key_map[i].key != ev.code)
91 continue;
92 if (ev.value)
93 keystate |= (1 << key_map[i].btn);
94 else
95 keystate &= ~(1 << key_map[i].btn);
96 break;
97 }
98 }
99
100 return keystate;
101}
102