frontend: minor tweaks
[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 "linux/in_evdev.h"
21 #include "plugin_lib.h"
22 #include "plat.h"
23 #include "main.h"
24
25 static const char * const pandora_gpio_keys[KEY_MAX + 1] = {
26         [0 ... KEY_MAX] = NULL,
27         [KEY_UP]        = "Up",
28         [KEY_LEFT]      = "Left",
29         [KEY_RIGHT]     = "Right",
30         [KEY_DOWN]      = "Down",
31         [KEY_HOME]      = "(A)",
32         [KEY_PAGEDOWN]  = "(X)",
33         [KEY_END]       = "(B)",
34         [KEY_PAGEUP]    = "(Y)",
35         [KEY_RIGHTSHIFT]= "(L)",
36         [KEY_RIGHTCTRL] = "(R)",
37         [KEY_LEFTALT]   = "Start",
38         [KEY_LEFTCTRL]  = "Select",
39         [KEY_MENU]      = "Pandora",
40 };
41
42 static const struct in_default_bind in_evdev_defbinds[] = {
43         { KEY_UP,       IN_BINDTYPE_PLAYER12, DKEY_UP },
44         { KEY_DOWN,     IN_BINDTYPE_PLAYER12, DKEY_DOWN },
45         { KEY_LEFT,     IN_BINDTYPE_PLAYER12, DKEY_LEFT },
46         { KEY_RIGHT,    IN_BINDTYPE_PLAYER12, DKEY_RIGHT },
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         { KEY_TAB,      IN_BINDTYPE_EMU, SACTION_MINIMIZE },
58         { KEY_SPACE,    IN_BINDTYPE_EMU, SACTION_ENTER_MENU },
59         { KEY_1,        IN_BINDTYPE_EMU, SACTION_SAVE_STATE },
60         { KEY_2,        IN_BINDTYPE_EMU, SACTION_LOAD_STATE },
61         { KEY_3,        IN_BINDTYPE_EMU, SACTION_PREV_SSLOT },
62         { KEY_4,        IN_BINDTYPE_EMU, SACTION_NEXT_SSLOT },
63         { KEY_5,        IN_BINDTYPE_EMU, SACTION_TOGGLE_FSKIP },
64         { KEY_6,        IN_BINDTYPE_EMU, SACTION_SCREENSHOT },
65         { 0, 0, 0 }
66 };
67
68 int plat_pandora_init(void)
69 {
70         int gpiokeys_id;
71
72         in_evdev_init(in_evdev_defbinds);
73         in_probe();
74         gpiokeys_id = in_name_to_id("evdev:gpio-keys");
75         in_set_config(gpiokeys_id, IN_CFG_KEY_NAMES,
76                       pandora_gpio_keys, sizeof(pandora_gpio_keys));
77         in_set_config(gpiokeys_id, IN_CFG_DEFAULT_DEV, NULL, 0);
78         in_adev[0] = in_name_to_id("evdev:nub0");
79         in_adev[1] = in_name_to_id("evdev:nub1");
80
81         return 0;
82 }
83
84 static const char pnd_script_base[] = "sudo -n /usr/pandora/scripts";
85
86 int plat_cpu_clock_get(void)
87 {
88         FILE *f;
89         int ret = 0;
90         f = fopen("/proc/pandora/cpu_mhz_max", "r");
91         if (f) {
92                 fscanf(f, "%d", &ret);
93                 fclose(f);
94         }
95         return ret;
96 }
97
98 int plat_cpu_clock_apply(int cpu_clock)
99 {
100         char buf[128];
101
102         if (cpu_clock != 0 && cpu_clock != plat_cpu_clock_get()) {
103                 snprintf(buf, sizeof(buf), "unset DISPLAY; echo y | %s/op_cpuspeed.sh %d",
104                          pnd_script_base, cpu_clock);
105                 system(buf);
106         }
107         return 0;
108 }
109
110 int plat_get_bat_capacity(void)
111 {
112         FILE *f;
113         int ret = 0;
114         f = fopen("/sys/class/power_supply/bq27500-0/capacity", "r");
115         if (f) {
116                 fscanf(f, "%d", &ret);
117                 fclose(f);
118         }
119         return ret;
120 }