rcnt: remove gate guess for cnt 0 and 1
[pcsx_rearmed.git] / frontend / plat_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 <stdlib.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <sys/ioctl.h>
15 #include <unistd.h>
16 #include <linux/input.h>
17 #include <errno.h>
18
19 #include "common/input.h"
20 #include "plugin_lib.h"
21 #include "plat.h"
22 #include "main.h"
23
24 static int fdnub[2];
25 static int analog_init_done;
26
27 static const char * const pandora_gpio_keys[KEY_MAX + 1] = {
28         [0 ... KEY_MAX] = NULL,
29         [KEY_UP]        = "Up",
30         [KEY_LEFT]      = "Left",
31         [KEY_RIGHT]     = "Right",
32         [KEY_DOWN]      = "Down",
33         [KEY_HOME]      = "(A)",
34         [KEY_PAGEDOWN]  = "(X)",
35         [KEY_END]       = "(B)",
36         [KEY_PAGEUP]    = "(Y)",
37         [KEY_RIGHTSHIFT]= "(L)",
38         [KEY_RIGHTCTRL] = "(R)",
39         [KEY_LEFTALT]   = "Start",
40         [KEY_LEFTCTRL]  = "Select",
41         [KEY_MENU]      = "Pandora",
42 };
43
44 struct in_default_bind in_evdev_defbinds[] = {
45         { KEY_UP,       IN_BINDTYPE_PLAYER12, DKEY_UP },
46         { KEY_DOWN,     IN_BINDTYPE_PLAYER12, DKEY_DOWN },
47         { KEY_LEFT,     IN_BINDTYPE_PLAYER12, DKEY_LEFT },
48         { KEY_RIGHT,    IN_BINDTYPE_PLAYER12, DKEY_RIGHT },
49         { KEY_PAGEUP,   IN_BINDTYPE_PLAYER12, DKEY_TRIANGLE },
50         { KEY_PAGEDOWN, IN_BINDTYPE_PLAYER12, DKEY_CROSS },
51         { KEY_END,      IN_BINDTYPE_PLAYER12, DKEY_CIRCLE },
52         { KEY_HOME,     IN_BINDTYPE_PLAYER12, DKEY_SQUARE },
53         { KEY_LEFTALT,  IN_BINDTYPE_PLAYER12, DKEY_START },
54         { KEY_LEFTCTRL, IN_BINDTYPE_PLAYER12, DKEY_SELECT },
55         { KEY_RIGHTSHIFT,IN_BINDTYPE_PLAYER12, DKEY_L1 },
56         { KEY_RIGHTCTRL, IN_BINDTYPE_PLAYER12, DKEY_R1 },
57         { KEY_Q,        IN_BINDTYPE_PLAYER12, DKEY_L2 },
58         { KEY_P,        IN_BINDTYPE_PLAYER12, DKEY_R2 },
59         { KEY_SPACE,    IN_BINDTYPE_EMU, SACTION_ENTER_MENU },
60         { KEY_1,        IN_BINDTYPE_EMU, SACTION_SAVE_STATE },
61         { KEY_2,        IN_BINDTYPE_EMU, SACTION_LOAD_STATE },
62         { KEY_3,        IN_BINDTYPE_EMU, SACTION_PREV_SSLOT },
63         { KEY_4,        IN_BINDTYPE_EMU, SACTION_NEXT_SSLOT },
64         { KEY_5,        IN_BINDTYPE_EMU, SACTION_TOGGLE_FSKIP },
65         { KEY_6,        IN_BINDTYPE_EMU, SACTION_SCREENSHOT },
66         { 0, 0, 0 }
67 };
68
69 static void analog_init(void)
70 {
71         int i, nub;
72
73         fdnub[0] = fdnub[1] = -1;
74
75         for (i = nub = 0; nub < 2; i++)
76         {
77                 long absbits[(ABS_MAX+1) / sizeof(long) / 8];
78                 int ret, fd, support = 0;
79                 char name[64];
80
81                 snprintf(name, sizeof(name), "/dev/input/event%d", i);
82                 fd = open(name, O_RDONLY|O_NONBLOCK);
83                 if (fd == -1) {
84                         if (errno == EACCES)
85                                 continue;       /* maybe we can access next one */
86                         break;
87                 }
88
89                 /* check supported events */
90                 ret = ioctl(fd, EVIOCGBIT(0, sizeof(support)), &support);
91                 if (ret == -1) {
92                         printf("pandora: ioctl failed on %s\n", name);
93                         goto skip;
94                 }
95
96                 if (!(support & (1 << EV_ABS)))
97                         goto skip;
98
99                 ret = ioctl(fd, EVIOCGNAME(sizeof(name)), name);
100                 if (ret == -1 || strncmp(name, "nub", 3) != 0)
101                         goto skip;
102
103                 ret = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbits)), absbits);
104                 if (ret == -1)
105                         goto skip;
106                 if ((absbits[0] & ((1 << ABS_X)|(1 << ABS_Y))) != ((1 << ABS_X)|(1 << ABS_Y)))
107                         goto skip;
108
109                 printf("pandora: found analog #%d \"%s\"\n", nub, name);
110                 fdnub[nub++] = fd;
111                 continue;
112
113 skip:
114                 close(fd);
115         }
116
117         if (nub != 2)
118                 printf("pandora: warning: not all nubs found: %d\n", nub);
119
120         analog_init_done = 1;
121 }
122
123 void in_update_analogs(void)
124 {
125         int *nubp[2] = { in_a1, in_a2 };
126         struct input_absinfo ainfo;
127         int i, fd, v, ret;
128
129         if (!analog_init_done)
130                 analog_init();
131
132         for (i = 0; i < 2; i++) {
133                 fd = fdnub[i];
134                 if (fd < 0)
135                         continue;
136
137                 ret = ioctl(fd, EVIOCGABS(ABS_X), &ainfo);
138                 if (ret == -1) {
139                         perror("ioctl");
140                         continue;
141                 }
142                 v = ainfo.value / 2 + 127;
143                 nubp[i][0] = v < 0 ? 0 : v;
144
145                 ret = ioctl(fd, EVIOCGABS(ABS_Y), &ainfo);
146                 if (ret == -1) {
147                         perror("ioctl");
148                         continue;
149                 }
150                 v = ainfo.value / 2 + 127;
151                 nubp[i][1] = v < 0 ? 0 : v;
152         }
153         //printf("%4d %4d %4d %4d\n", in_a1[0], in_a1[1], in_a2[0], in_a2[1]);
154 }
155
156 int plat_rescan_inputs(void)
157 {
158         in_probe();
159         in_set_config(in_name_to_id("evdev:gpio-keys"), IN_CFG_KEY_NAMES,
160                       pandora_gpio_keys, sizeof(pandora_gpio_keys));
161
162         return 0;
163 }
164
165 static const char pnd_script_base[] = "sudo -n /usr/pandora/scripts";
166
167 int plat_cpu_clock_get(void)
168 {
169         FILE *f;
170         int ret = 0;
171         f = fopen("/proc/pandora/cpu_mhz_max", "r");
172         if (f) {
173                 fscanf(f, "%d", &ret);
174                 fclose(f);
175         }
176         return ret;
177 }
178
179 int plat_cpu_clock_apply(int cpu_clock)
180 {
181         char buf[128];
182
183         if (cpu_clock != 0 && cpu_clock != plat_cpu_clock_get()) {
184                 snprintf(buf, sizeof(buf), "unset DISPLAY; echo y | %s/op_cpuspeed.sh %d",
185                          pnd_script_base, cpu_clock);
186                 system(buf);
187         }
188         return 0;
189 }
190
191 int plat_get_bat_capacity(void)
192 {
193         FILE *f;
194         int ret = 0;
195         f = fopen("/sys/class/power_supply/bq27500-0/capacity", "r");
196         if (f) {
197                 fscanf(f, "%d", &ret);
198                 fclose(f);
199         }
200         return ret;
201 }