4 * Copyright (C) 2013 Giuseppe Bilotta
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <linux/input.h>
24 #include <linux/uinput.h>
26 #include <sys/select.h>
39 /* These definitions might be missing from older linux/input.h headers */
41 #define BTN_DPAD_LEFT 0x222
43 #ifndef BTN_DPAD_RIGHT
44 #define BTN_DPAD_RIGHT 0x223
47 #define BTN_DPAD_UP 0x220
50 #define BTN_DPAD_DOWN 0x221
53 #define WIIMOTE_PADMODE_VENDOR_ID 0x6181 /* GIuseppe BIlotta */
54 #define WIIMOTE_PADMODE_DEVICE_ID 0x3169 /* WIimote GamePad */
56 /* Check if an error \code is negative during \str action */
57 void err_check(int code, char const *str) {
60 fprintf(stderr, "could not %s (%d): %s\n",
61 str, err, strerror(err));
66 #define BUTTONS do { \
67 _BUTTON(XWII_KEY_LEFT, BTN_DPAD_LEFT); \
68 _BUTTON(XWII_KEY_RIGHT, BTN_DPAD_RIGHT); \
69 _BUTTON(XWII_KEY_UP, BTN_DPAD_UP); \
70 _BUTTON(XWII_KEY_DOWN, BTN_DPAD_DOWN); \
71 _BUTTON(XWII_KEY_A, BTN_A); \
72 _BUTTON(XWII_KEY_B, BTN_B); \
73 _BUTTON(XWII_KEY_PLUS, BTN_TL); \
74 _BUTTON(XWII_KEY_MINUS, BTN_TR); \
75 _BUTTON(XWII_KEY_HOME, BTN_MODE); \
76 _BUTTON(XWII_KEY_ONE, BTN_1); \
77 _BUTTON(XWII_KEY_TWO, BTN_2); \
81 struct uinput_user_dev padmode;
82 /* Room for controller keys and two axes, plus SYN */
83 struct input_event iev[XWII_KEY_TWO+2+1+1];
85 /* macros to set evbits and keybits */
86 #define set_ev(key) do { \
87 ret = ioctl(fd, UI_SET_EVBIT, key); \
88 err_check(ret, "set " #key); \
91 #define set_key(key) do { \
92 ret = ioctl(fd, UI_SET_KEYBIT, key); \
93 err_check(ret, "set " #key); \
96 #define set_abs(key, min, max, fuzz, flat) do { \
97 ret = ioctl(fd, UI_SET_ABSBIT, key); \
98 err_check(ret, "set " #key); \
99 padmode.absmin[key] = min; \
100 padmode.absmax[key] = max; \
101 padmode.absfuzz[key] = fuzz; \
102 padmode.absflat[key] = flat; \
115 struct xwii_iface *iface;
120 #define MAX_WIIMOTES 1
121 struct wiimote_dev dev[MAX_WIIMOTES];
123 void dev_init(struct wiimote_dev const *dev) {
125 int fd = dev->uinput;
127 #define _BUTTON(n, bt) do { \
129 iev[n].type = EV_KEY; \
138 set_abs(ABS_X, -AXIS_MAX, AXIS_MAX, 2, 4);
139 iev[11].type = EV_ABS;
140 iev[11].code = ABS_X;
141 set_abs(ABS_Y, -AXIS_MAX, AXIS_MAX, 2, 4);
142 iev[12].type = EV_ABS;
143 iev[12].code = ABS_Y;
145 iev[13].type = EV_SYN;
146 iev[13].code = iev[13].value = 0;
148 snprintf(padmode.name, UINPUT_MAX_NAME_SIZE, XWII_NAME_CORE " in gamepad mode");
149 padmode.id.bustype = BUS_VIRTUAL;
151 padmode.id.vendor = 0;
152 padmode.id.product = 0;
153 padmode.id.version = 0;
156 ret = write(fd, &padmode, sizeof(padmode));
157 err_check(ret, "set dev properties");
158 ret = ioctl(fd, UI_DEV_CREATE);
159 err_check(ret, "create device");
163 static int wiimote_refresh(struct wiimote_dev *dev)
165 puts("Refreshing\n");
166 return xwii_iface_open(dev->iface, dev->ifs);
169 static void wiimote_key(struct wiimote_dev *dev, struct xwii_event const *ev)
174 code = ev->v.key.code;
175 state = ev->v.key.state;
177 if (code > XWII_KEY_TWO)
181 iev[code].value = state;
183 if (dev->uinput > 0) {
184 int ret = write(dev->uinput, iev + code, sizeof(*iev));
185 err_check(ret, "report button");
186 ret = write(dev->uinput, iev + 13, sizeof(*iev));
187 err_check(ret, "report btn SYN");
189 fputs("nowhere to report butto presses to\n", stderr);
193 #define CLIP_AXIS(val) do { \
194 if (val < -AXIS_MAX) \
196 if (val > AXIS_MAX) \
201 static void wiimote_accel(struct wiimote_dev *dev, struct xwii_event const *ev)
203 iev[11].value = -(ev->v.abs[0].y);
204 iev[12].value = -(ev->v.abs[0].x);
206 CLIP_AXIS(iev[11].value);
207 CLIP_AXIS(iev[12].value);
209 if (dev->uinput > 0) {
210 int ret = write(dev->uinput, iev + 11, sizeof(*iev));
211 err_check(ret, "report accel X");
212 ret = write(dev->uinput, iev + 12, sizeof(*iev));
213 err_check(ret, "report accel Y");
214 ret = write(dev->uinput, iev + 13, sizeof(*iev));
215 err_check(ret, "report accel SYN");
217 printf("reported J (%d, %d) from ev (%d, %d)\n",
218 iev[11].value, iev[12].value,
219 -(ev->v.abs[0].y), ev->v.abs[0].x);
222 fputs("nowhere to report accel to\n", stderr);
226 static int wiimote_poll(struct wiimote_dev *dev)
228 struct xwii_event ev;
232 memset(&ev, 0, sizeof(ev));
233 ret = xwii_iface_dispatch(dev->iface, &ev, sizeof(ev));
239 case XWII_EVENT_WATCH:
240 ret = wiimote_refresh(dev);
243 wiimote_key(dev, &ev);
245 case XWII_EVENT_ACCEL:
246 wiimote_accel(dev, &ev);
249 printf("Unhandled Wiimote event type %d\n", ev.type);
253 if (ret == -EAGAIN) {
260 int dev_create(struct wiimote_dev *dev) {
263 struct udev_device *d, *p;
265 const char *root, *snum, *driver, *subs;
273 if (stat(dev->device, &st)) {
280 fputs("could not connect to udev\n", stderr);
285 d = udev_device_new_from_devnum(udev, 'c', st.st_rdev);
287 fputs("could not find udev device\n", stderr);
292 p = udev_device_get_parent_with_subsystem_devtype(d, "hid", NULL);
294 fputs("could not find parent HID device\n", stderr);
299 driver = udev_device_get_driver(p);
300 subs = udev_device_get_subsystem(p);
301 if (!driver || strcmp(driver, "wiimote") || !subs || strcmp(subs, "hid")) {
302 fputs("parent is not a HID Wiimote\n", stderr);
307 root = udev_device_get_syspath(p);
308 snum = udev_device_get_sysname(p);
309 snum = snum ? strchr(snum, '.') : NULL;
310 if (!root || !snum) {
311 fputs("Could not get root path\n", stderr);
316 num = strtol(&snum[1], NULL, 16);
318 fputs("Negative device number!\n", stderr);
324 dev->root = strdup(root);
326 fputs("Could not set device root\n", stderr);
331 printf("using device %d from root %s for %s\n",
332 dev->dev_id, dev->root, dev->device);
334 dev->ifs = XWII_IFACE_CORE | XWII_IFACE_ACCEL;
335 ret = xwii_iface_new(&dev->iface, dev->root);
337 fputs("Could not create xwiimote interface\n", stderr);
342 ret = xwii_iface_open(dev->iface, dev->ifs);
344 fputs("Could not open xwiimote interface\n", stderr);
348 if (xwii_iface_opened(dev->iface) != dev->ifs) {
349 fputs("Some interfaces failed to open\n", stderr);
354 dev->fd = xwii_iface_get_fd(dev->iface);
363 udev_device_unref(d);
370 static void dev_destroy(struct wiimote_dev *dev) {
372 xwii_iface_unref(dev->iface);
376 if (dev->uinput > 0) {
377 ioctl(dev->uinput, UI_DEV_DESTROY);
380 printf("deassociated from device %s\n", dev->device);
385 static void destroy_all_devs(void) {
387 #error "destroy_all_devs only works for a single device"
393 static int last_signal;
395 static void sig_exit(int _signal) {
396 last_signal = _signal;
397 printf("Interrupted by signal %d\n", last_signal);
400 struct timeval no_wait;
402 const char uinput_path[] = "/dev/uinput";
404 const char js_glob[] = "/dev/input/js*";
406 int main(int argc, char *argv[]) {
411 atexit(destroy_all_devs);
412 signal(SIGINT, sig_exit);
415 dev->device = argv[1];
416 ret = dev_create(dev);
419 fprintf(stderr, "could not %s (%d): %s\n",
420 "associate", ret, strerror(ret));
424 /* No device specified. Since the Linux kernel exposes the
425 * controller also as a joystick (without axes), we peek at
426 * all available joysticks looking for one which is a Wiimote
429 switch (glob(js_glob, GLOB_NOSORT, NULL, &js_devs)) {
433 fputs("no joysticks found\n", stderr);
437 for (j = 0; j < js_devs.gl_pathc; ++j) {
438 dev->device = js_devs.gl_pathv[j];
439 ret = dev_create(dev);
442 printf("skipping %s (%d): %s\n",
443 dev->device, ret, strerror(ret));
447 fputs("no wiimote found\n", stderr);
452 dev->uinput = open(uinput_path, O_WRONLY | O_NONBLOCK);
453 err_check(dev->uinput, "open uinput");
458 memset(&no_wait, 0, sizeof(no_wait));
460 FD_SET(dev->fd, fds);
464 ret = select(dev->fd + 1, fds, NULL, NULL, NULL);
465 err_check(ret, "poll wiimote fd");
467 ret = wiimote_poll(dev);
468 err_check(ret, "process wiimote data");