frontend: key config: save it and make more intuitive
[pcsx_rearmed.git] / frontend / pandora.c
1 /*
2  * (C) notaz, 2011
3  *
4  * This work is licensed under the terms of the GNU GPLv2 or later.
5  * See the COPYING file in the top-level directory.
6  */
7
8 #include <stdio.h>
9 #include <string.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <sys/ioctl.h>
14 #include <unistd.h>
15 #include <linux/input.h>
16 #include <errno.h>
17
18 #include "common/input.h"
19 #include "plugin_lib.h"
20
21 static int fdnub[2];
22 static int analog_init_done;
23
24 static const char * const pandora_gpio_keys[KEY_MAX + 1] = {
25         [0 ... KEY_MAX] = NULL,
26         [KEY_UP]        = "Up",
27         [KEY_LEFT]      = "Left",
28         [KEY_RIGHT]     = "Right",
29         [KEY_DOWN]      = "Down",
30         [KEY_HOME]      = "(A)",
31         [KEY_PAGEDOWN]  = "(X)",
32         [KEY_END]       = "(B)",
33         [KEY_PAGEUP]    = "(Y)",
34         [KEY_RIGHTSHIFT]= "(L)",
35         [KEY_RIGHTCTRL] = "(R)",
36         [KEY_LEFTALT]   = "Start",
37         [KEY_LEFTCTRL]  = "Select",
38         [KEY_MENU]      = "Pandora",
39 };
40
41 struct in_default_bind in_evdev_defbinds[] = {
42         { KEY_UP,       IN_BINDTYPE_PLAYER12, DKEY_UP },
43         { KEY_DOWN,     IN_BINDTYPE_PLAYER12, DKEY_DOWN },
44         { KEY_LEFT,     IN_BINDTYPE_PLAYER12, DKEY_LEFT },
45         { KEY_RIGHT,    IN_BINDTYPE_PLAYER12, DKEY_RIGHT },
46         { KEY_SPACE,    IN_BINDTYPE_EMU, PEVB_MENU },
47         { KEY_PAGEUP,   IN_BINDTYPE_PLAYER12, DKEY_TRIANGLE },
48         { KEY_PAGEDOWN, IN_BINDTYPE_PLAYER12, DKEY_CROSS },
49         { KEY_END,      IN_BINDTYPE_PLAYER12, DKEY_CIRCLE },
50         { KEY_HOME,     IN_BINDTYPE_PLAYER12, DKEY_SQUARE },
51         { KEY_LEFTALT,  IN_BINDTYPE_PLAYER12, DKEY_START },
52         { KEY_LEFTCTRL, IN_BINDTYPE_PLAYER12, DKEY_SELECT },
53         { KEY_RIGHTSHIFT,IN_BINDTYPE_PLAYER12, DKEY_L1 },
54         { KEY_RIGHTCTRL, IN_BINDTYPE_PLAYER12, DKEY_R1 },
55         { KEY_Q,        IN_BINDTYPE_PLAYER12, DKEY_L2 },
56         { KEY_P,        IN_BINDTYPE_PLAYER12, DKEY_R2 },
57         { 0, 0, 0 }
58 };
59
60 static void analog_init(void)
61 {
62         int i, nub;
63
64         fdnub[0] = fdnub[1] = -1;
65
66         for (i = nub = 0; nub < 2; i++)
67         {
68                 long absbits[(ABS_MAX+1) / sizeof(long) / 8];
69                 int ret, fd, support = 0;
70                 char name[64];
71
72                 snprintf(name, sizeof(name), "/dev/input/event%d", i);
73                 fd = open(name, O_RDONLY|O_NONBLOCK);
74                 if (fd == -1) {
75                         if (errno == EACCES)
76                                 continue;       /* maybe we can access next one */
77                         break;
78                 }
79
80                 /* check supported events */
81                 ret = ioctl(fd, EVIOCGBIT(0, sizeof(support)), &support);
82                 if (ret == -1) {
83                         printf("pandora: ioctl failed on %s\n", name);
84                         goto skip;
85                 }
86
87                 if (!(support & (1 << EV_ABS)))
88                         goto skip;
89
90                 ret = ioctl(fd, EVIOCGNAME(sizeof(name)), name);
91                 if (ret == -1 || strncmp(name, "nub", 3) != 0)
92                         goto skip;
93
94                 ret = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbits)), absbits);
95                 if (ret == -1)
96                         goto skip;
97                 if ((absbits[0] & ((1 << ABS_X)|(1 << ABS_Y))) != ((1 << ABS_X)|(1 << ABS_Y)))
98                         goto skip;
99
100                 printf("pandora: found analog #%d \"%s\"\n", nub, name);
101                 fdnub[nub++] = fd;
102                 continue;
103
104 skip:
105                 close(fd);
106         }
107
108         if (nub != 2)
109                 printf("pandora: warning: not all nubs found: %d\n", nub);
110
111         analog_init_done = 1;
112 }
113
114 void in_update_analogs(void)
115 {
116         int *nubp[2] = { in_a1, in_a2 };
117         struct input_absinfo ainfo;
118         int i, fd, v, ret;
119
120         if (!analog_init_done)
121                 analog_init();
122
123         for (i = 0; i < 2; i++) {
124                 fd = fdnub[i];
125                 if (fd < 0)
126                         continue;
127
128                 ret = ioctl(fd, EVIOCGABS(ABS_X), &ainfo);
129                 if (ret == -1) {
130                         perror("ioctl");
131                         continue;
132                 }
133                 v = ainfo.value / 2 + 127;
134                 nubp[i][0] = v < 0 ? 0 : v;
135
136                 ret = ioctl(fd, EVIOCGABS(ABS_Y), &ainfo);
137                 if (ret == -1) {
138                         perror("ioctl");
139                         continue;
140                 }
141                 v = ainfo.value / 2 + 127;
142                 nubp[i][1] = v < 0 ? 0 : v;
143         }
144         //printf("%4d %4d %4d %4d\n", in_a1[0], in_a1[1], in_a2[0], in_a2[1]);
145 }
146
147 int pandora_init(void)
148 {
149         in_set_config(in_name_to_id("evdev:gpio-keys"), IN_CFG_KEY_NAMES,
150                       pandora_gpio_keys, sizeof(pandora_gpio_keys));
151
152         return 0;
153 }