| 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 | #include "main.h" |
| 21 | |
| 22 | static int fdnub[2]; |
| 23 | static int analog_init_done; |
| 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 | 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_SPACE, IN_BINDTYPE_EMU, SACTION_ENTER_MENU }, |
| 58 | { KEY_1, IN_BINDTYPE_EMU, SACTION_SAVE_STATE }, |
| 59 | { KEY_2, IN_BINDTYPE_EMU, SACTION_LOAD_STATE }, |
| 60 | { KEY_3, IN_BINDTYPE_EMU, SACTION_PREV_SSLOT }, |
| 61 | { KEY_4, IN_BINDTYPE_EMU, SACTION_NEXT_SSLOT }, |
| 62 | { KEY_5, IN_BINDTYPE_EMU, SACTION_TOGGLE_FSKIP }, |
| 63 | { 0, 0, 0 } |
| 64 | }; |
| 65 | |
| 66 | static void analog_init(void) |
| 67 | { |
| 68 | int i, nub; |
| 69 | |
| 70 | fdnub[0] = fdnub[1] = -1; |
| 71 | |
| 72 | for (i = nub = 0; nub < 2; i++) |
| 73 | { |
| 74 | long absbits[(ABS_MAX+1) / sizeof(long) / 8]; |
| 75 | int ret, fd, support = 0; |
| 76 | char name[64]; |
| 77 | |
| 78 | snprintf(name, sizeof(name), "/dev/input/event%d", i); |
| 79 | fd = open(name, O_RDONLY|O_NONBLOCK); |
| 80 | if (fd == -1) { |
| 81 | if (errno == EACCES) |
| 82 | continue; /* maybe we can access next one */ |
| 83 | break; |
| 84 | } |
| 85 | |
| 86 | /* check supported events */ |
| 87 | ret = ioctl(fd, EVIOCGBIT(0, sizeof(support)), &support); |
| 88 | if (ret == -1) { |
| 89 | printf("pandora: ioctl failed on %s\n", name); |
| 90 | goto skip; |
| 91 | } |
| 92 | |
| 93 | if (!(support & (1 << EV_ABS))) |
| 94 | goto skip; |
| 95 | |
| 96 | ret = ioctl(fd, EVIOCGNAME(sizeof(name)), name); |
| 97 | if (ret == -1 || strncmp(name, "nub", 3) != 0) |
| 98 | goto skip; |
| 99 | |
| 100 | ret = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbits)), absbits); |
| 101 | if (ret == -1) |
| 102 | goto skip; |
| 103 | if ((absbits[0] & ((1 << ABS_X)|(1 << ABS_Y))) != ((1 << ABS_X)|(1 << ABS_Y))) |
| 104 | goto skip; |
| 105 | |
| 106 | printf("pandora: found analog #%d \"%s\"\n", nub, name); |
| 107 | fdnub[nub++] = fd; |
| 108 | continue; |
| 109 | |
| 110 | skip: |
| 111 | close(fd); |
| 112 | } |
| 113 | |
| 114 | if (nub != 2) |
| 115 | printf("pandora: warning: not all nubs found: %d\n", nub); |
| 116 | |
| 117 | analog_init_done = 1; |
| 118 | } |
| 119 | |
| 120 | void in_update_analogs(void) |
| 121 | { |
| 122 | int *nubp[2] = { in_a1, in_a2 }; |
| 123 | struct input_absinfo ainfo; |
| 124 | int i, fd, v, ret; |
| 125 | |
| 126 | if (!analog_init_done) |
| 127 | analog_init(); |
| 128 | |
| 129 | for (i = 0; i < 2; i++) { |
| 130 | fd = fdnub[i]; |
| 131 | if (fd < 0) |
| 132 | continue; |
| 133 | |
| 134 | ret = ioctl(fd, EVIOCGABS(ABS_X), &ainfo); |
| 135 | if (ret == -1) { |
| 136 | perror("ioctl"); |
| 137 | continue; |
| 138 | } |
| 139 | v = ainfo.value / 2 + 127; |
| 140 | nubp[i][0] = v < 0 ? 0 : v; |
| 141 | |
| 142 | ret = ioctl(fd, EVIOCGABS(ABS_Y), &ainfo); |
| 143 | if (ret == -1) { |
| 144 | perror("ioctl"); |
| 145 | continue; |
| 146 | } |
| 147 | v = ainfo.value / 2 + 127; |
| 148 | nubp[i][1] = v < 0 ? 0 : v; |
| 149 | } |
| 150 | //printf("%4d %4d %4d %4d\n", in_a1[0], in_a1[1], in_a2[0], in_a2[1]); |
| 151 | } |
| 152 | |
| 153 | int pandora_init(void) |
| 154 | { |
| 155 | in_set_config(in_name_to_id("evdev:gpio-keys"), IN_CFG_KEY_NAMES, |
| 156 | pandora_gpio_keys, sizeof(pandora_gpio_keys)); |
| 157 | |
| 158 | return 0; |
| 159 | } |