Debian Codegraph @

src/linux/ljoynu.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * Linux joystick driver.
12 *
13 * By George Foot and Peter Wang.
14 *
15 * Updated for new joystick API by Peter Wang.
16 *
17 * See readme.txt for copyright information.
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <sys/ioctl.h>
26 #include <sys/stat.h>
27
28 #define ALLEGRO_NO_KEY_DEFINES
29 #define ALLEGRO_NO_COMPATIBILITY
30
31 #include "allegro5/allegro.h"
32 #include "allegro5/internal/aintern.h"
33 #include "allegro5/internal/aintern_events.h"
34 #include "allegro5/internal/aintern_joystick.h"
35 #include "allegro5/platform/aintunix.h"
36
37 #ifdef ALLEGRO_HAVE_LINUX_INPUT_H
38
39 /* To be safe, include sys/types.h before linux/joystick.h to avoid conflicting
40 * definitions of fd_set.
41 */
42 #include <sys/types.h>
43 #include <linux/input.h>
44
45 #if defined(ALLEGRO_HAVE_SYS_INOTIFY_H)
46 #define SUPPORT_HOTPLUG
47 #include <sys/inotify.h>
48 #endif
49
50 #ifndef KEY_CNT
51 #define KEY_CNT (KEY_MAX+1)
52 #endif
53
54 #ifndef ABS_CNT
55 #define ABS_CNT (ABS_MAX+1)
56 #endif
57
58 ALLEGRO_DEBUG_CHANNEL("ljoy");
59
60 #include "allegro5/internal/aintern_ljoynu.h"
61
62
63
64 #define LONG_BITS (sizeof(long) * 8)
65 #define NLONGS(x) (((x) + LONG_BITS - 1) / LONG_BITS)
66 /* Tests if a bit in an array of longs is set. */
67 #define TEST_BIT(nr, addr) \
68 ((1UL << ((nr) % LONG_BITS)) & (addr)[(nr) / LONG_BITS])
69
70
71 /* We only accept the axes ABS_X to ABS_HAT3Y as real joystick axes.
72 * The axes ABS_PRESSURE, ABS_DISTANCE, ABS_TILT_X, ABS_TILT_Y,
73 * ABS_TOOL_WIDTH are for use by a drawing tablet.
74 * ABS_VOLUME is for volume sliders or key pairs on some keyboards.
75 * Other axes up to ABS_MISC may exist, such as on the PS3, but they are
76 * most likely not useful.
77 */
78 #define LJOY_AXIS_RANGE_START (ABS_X)
79 #define LJOY_AXIS_RANGE_END (ABS_HAT3Y + 1)
80
81
82 /* The range of potential joystick button codes in the Linux input API
83 * is from BTN_MISC to BTN_GEAR_UP.
84 */
85 #define LJOY_BTN_RANGE_START (BTN_MISC)
86 #if !defined(ALLEGRO_ANDROID) && defined(BTN_TRIGGER_HAPPY)
87 #define LJOY_BTN_RANGE_END (BTN_TRIGGER_HAPPY40 + 1)
88 #else
89 #define LJOY_BTN_RANGE_END (BTN_GEAR_UP + 1)
90 #endif
91
92
93
94 /* forward declarations */
95 static bool ljoy_init_joystick(void);
96 static void ljoy_exit_joystick(void);
97 static bool ljoy_reconfigure_joysticks(void);
98 static int ljoy_num_joysticks(void);
99 static ALLEGRO_JOYSTICK *ljoy_get_joystick(int num);
100 static void ljoy_release_joystick(ALLEGRO_JOYSTICK *joy_);
101 static void ljoy_get_joystick_state(ALLEGRO_JOYSTICK *joy_, ALLEGRO_JOYSTICK_STATE *ret_state);
102 static const char *ljoy_get_name(ALLEGRO_JOYSTICK *joy_);
103 static bool ljoy_get_active(ALLEGRO_JOYSTICK *joy_);
104
105 static void ljoy_process_new_data(void *data);
106
107
108
109 /* the driver vtable */
110 ALLEGRO_JOYSTICK_DRIVER _al_joydrv_linux =
111 {
112 _ALLEGRO_JOYDRV_LINUX,
113 "",
114 "",
115 "Linux joystick(s)",
116 ljoy_init_joystick,
117 ljoy_exit_joystick,
118 ljoy_reconfigure_joysticks,
119 ljoy_num_joysticks,
120 ljoy_get_joystick,
121 ljoy_release_joystick,
122 ljoy_get_joystick_state,
123 ljoy_get_name,
124 ljoy_get_active
125 };
126
127
128 static unsigned num_joysticks; /* number of joysticks known to the user */
129 static _AL_VECTOR joysticks; /* of ALLEGRO_JOYSTICK_LINUX pointers */
130 static volatile bool config_needs_merging;
131 static ALLEGRO_MUTEX *config_mutex;
132 #ifdef SUPPORT_HOTPLUG
133 static int inotify_fd = -1;
134 static ALLEGRO_THREAD *hotplug_thread;
135 static ALLEGRO_MUTEX *hotplug_mutex;
136 static ALLEGRO_COND *hotplug_cond;
137 static bool hotplug_ended = false;
138 #endif
139
140
141
142 /* Return true if a joystick-related button or key:
143 *
144 * BTN_MISC to BTN_9 for miscellaneous input,
145 * BTN_JOYSTICK to BTN_DEAD for joysticks,
146 * BTN_GAMEPAD to BTN_THUMBR for game pads,
147 * BTN_WHEEL, BTN_GEAR_DOWN, BTN_GEAR_UP for steering wheels,
148 * BTN_TRIGGER_HAPPY_n buttons just in case.
149 */
150 static bool is_joystick_button(int i)
151 {
152 ASSERT(i >= LJOY_BTN_RANGE_START && i < LJOY_BTN_RANGE_END);
153
154 return (i >= BTN_MISC && i <= BTN_9)
155 || (i >= BTN_JOYSTICK && i <= BTN_DEAD)
156 || (i >= BTN_GAMEPAD && i <= BTN_THUMBR)
157 || (i >= BTN_WHEEL && i <= BTN_GEAR_UP)
158 #if !defined(ALLEGRO_ANDROID) && defined(BTN_TRIGGER_HAPPY)
159 || (i >= BTN_TRIGGER_HAPPY && i <= BTN_TRIGGER_HAPPY40)
160 #endif
161 ;
162 }
163
164
165
166 static bool is_single_axis_throttle(int i)
167 {
168 ASSERT(i >= LJOY_AXIS_RANGE_START && i < LJOY_AXIS_RANGE_END);
169
170 /* Consider all these types of axis as throttle axis. */
171 return i == ABS_THROTTLE
172 || i == ABS_RUDDER
173 || i == ABS_WHEEL
174 || i == ABS_GAS
175 || i == ABS_BRAKE
176 || i == ABS_PRESSURE
177 || i == ABS_DISTANCE
178 || i == ABS_TOOL_WIDTH;
179 }
180
181
182
183 static bool is_hat_axis(int i)
184 {
185 return (i >= ABS_HAT0X && i <= ABS_HAT3Y);
186 }
187
188
189
190 /* Check if the device has at least once joystick-related button. */
191 static bool have_joystick_button(int fd)
192 {
193 unsigned long key_bits[NLONGS(KEY_CNT)] = {0};
194 int i;
195
196 if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)), key_bits) < 0)
197 return false;
198
199 for (i = LJOY_BTN_RANGE_START; i < LJOY_BTN_RANGE_END; i++) {
200 if (TEST_BIT(i, key_bits) && is_joystick_button(i))
201 return true;
202 }
203
204 return false;
205 }
206
207
208
209 /* Check if the device has at least one joystick-related absolute axis.
210 * Some devices, like (wireless) keyboards, may actually have absolute axes
211 * such as a volume axis for the volume up/down buttons.
212 * Also, some devices like a PS3 controller report many unusual axes, probably
213 * used in nonstandard ways by the PS3 console. All such axes are ignored by
214 * this function and by this driver.
215 */
216 static bool have_joystick_axis(int fd)
217 {
218 unsigned long abs_bits[NLONGS(ABS_CNT)] = {0};
219 int i;
220
221 if (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)), abs_bits) < 0)
222 return false;
223
224 for (i = LJOY_AXIS_RANGE_START; i < LJOY_AXIS_RANGE_END; i++) {
225 if (TEST_BIT(i, abs_bits))
226 return true;
227 }
228
229 return false;
230 }
231
232
233
234 static bool ljoy_detect_device_name(int num, ALLEGRO_USTR *device_name)
235 {
236 char key[80];
237 const char *value;
238 struct stat stbuf;
239
240 al_ustr_truncate(device_name, 0);
241
242 snprintf(key, sizeof(key), "device%d", num);
243 value = al_get_config_value(al_get_system_config(), "joystick", key);
244 if (value)
245 al_ustr_assign_cstr(device_name, value);
246
247 return (stat(al_cstr(device_name), &stbuf) == 0);
248 }
249
250
251
252 static ALLEGRO_JOYSTICK_LINUX *ljoy_by_device_name(
253 const ALLEGRO_USTR *device_name)
254 {
255 unsigned i;
256
257 for (i = 0; i < _al_vector_size(&joysticks); i++) {
258 ALLEGRO_JOYSTICK_LINUX **slot = _al_vector_ref(&joysticks, i);
259 ALLEGRO_JOYSTICK_LINUX *joy = *slot;
260
261 if (joy && al_ustr_equal(device_name, joy->device_name))
262 return joy;
263 }
264
265 return NULL;
266 }
267
268
269
270 static void ljoy_generate_configure_event(void)
271 {
272 ALLEGRO_EVENT event;
273 event.joystick.type = ALLEGRO_EVENT_JOYSTICK_CONFIGURATION;
274 event.joystick.timestamp = al_get_time();
275
276 _al_generate_joystick_event(&event);
277 }
278
279
280
281 static ALLEGRO_JOYSTICK_LINUX *ljoy_allocate_structure(void)
282 {
283 ALLEGRO_JOYSTICK_LINUX **slot;
284 ALLEGRO_JOYSTICK_LINUX *joy;
285 unsigned i;
286
287 for (i = 0; i < _al_vector_size(&joysticks); i++) {
288 slot = _al_vector_ref(&joysticks, i);
289 joy = *slot;
290
291 if (joy->config_state == LJOY_STATE_UNUSED)
292 return joy;
293 }
294
295 joy = al_calloc(1, sizeof *joy);
296 slot = _al_vector_alloc_back(&joysticks);
297 *slot = joy;
298 return joy;
299 }
300
301
302
303 static void inactivate_joy(ALLEGRO_JOYSTICK_LINUX *joy)
304 {
305 if (joy->config_state == LJOY_STATE_UNUSED)
306 return;
307 joy->config_state = LJOY_STATE_UNUSED;
308
309 _al_unix_stop_watching_fd(joy->fd);
310 close(joy->fd);
311 joy->fd = -1;
312
313 _al_destroy_joystick_info(&joy->parent.info);
314 memset(&joy->parent.info, 0, sizeof(joy->parent.info));
315 memset(&joy->joystate, 0, sizeof(joy->joystate));
316
317 al_ustr_free(joy->device_name);
318 joy->device_name = NULL;
319 }
320
321
322
323 static void set_axis_mapping(AXIS_MAPPING *map, _AL_JOYSTICK_OUTPUT output,
324 const struct input_absinfo *absinfo)
325 {
326 map->output = output;
327 map->min = absinfo->minimum;
328 map->max = absinfo->maximum;
329 map->value = absinfo->value;
330 map->fuzz = absinfo->fuzz;
331 map->flat = absinfo->flat;
332 }
333
334
335
336 static bool fill_joystick_axes(ALLEGRO_JOYSTICK_LINUX *joy, int fd)
337 {
338 unsigned long abs_bits[NLONGS(ABS_CNT)] = {0};
339 int stick;
340 int axis;
341 int name_sticks;
342 int name_throttles;
343 int res, i;
344
345 /* Scan the axes to get their properties. */
346 res = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)), abs_bits);
347 if (res < 0)
348 return false;
349
350 stick = 0;
351 axis = 0;
352 name_sticks = 0;
353 name_throttles = 0;
354
355 for (i = LJOY_AXIS_RANGE_START; i < LJOY_AXIS_RANGE_END; i++) {
356 struct input_absinfo absinfo;
357
358 if (!TEST_BIT(i, abs_bits))
359 continue;
360
361 if (ioctl(fd, EVIOCGABS(i), &absinfo) < 0)
362 continue;
363
364 if (is_single_axis_throttle(i)) {
365 /* One axis throttle. */
366 name_throttles++;
367 joy->parent.info.stick[stick].flags = ALLEGRO_JOYFLAG_ANALOGUE;
368 joy->parent.info.stick[stick].num_axes = 1;
369 joy->parent.info.stick[stick].axis[0].name = _al_strdup("X");
370 joy->parent.info.stick[stick].name = al_malloc(32);
371 snprintf((char *)joy->parent.info.stick[stick].name, 32,
372 "Throttle %d", name_throttles);
373 set_axis_mapping(&joy->axis_mapping[i], _al_new_joystick_stick_output(stick, 0), &absinfo);
374 stick++;
375 }
376 else {
377 _AL_JOYSTICK_OUTPUT output = _al_new_joystick_stick_output(stick, axis);
378 /* Regular axis, two axis stick. */
379 if (axis == 0) {
380 /* First axis of new joystick. */
381 name_sticks++;
382 if (is_hat_axis(i)) {
383 joy->parent.info.stick[stick].flags = ALLEGRO_JOYFLAG_DIGITAL;
384 } else {
385 joy->parent.info.stick[stick].flags = ALLEGRO_JOYFLAG_ANALOGUE;
386 }
387 joy->parent.info.stick[stick].num_axes = 2;
388 joy->parent.info.stick[stick].axis[0].name = _al_strdup("X");
389 joy->parent.info.stick[stick].axis[1].name = _al_strdup("Y");
390 joy->parent.info.stick[stick].name = al_malloc(32);
391 snprintf((char *)joy->parent.info.stick[stick].name, 32,
392 "Stick %d", name_sticks);
393 set_axis_mapping(&joy->axis_mapping[i], output, &absinfo);
394 axis++;
395 }
396 else {
397 /* Second axis. */
398 ASSERT(axis == 1);
399 set_axis_mapping(&joy->axis_mapping[i], output, &absinfo);
400 stick++;
401 axis = 0;
402 }
403 }
404 }
405
406 joy->parent.info.num_sticks = stick;
407
408 return true;
409 }
410
411
412
413 static bool fill_joystick_axes_with_map(ALLEGRO_JOYSTICK_LINUX *joy, int fd, const _AL_JOYSTICK_MAPPING *mapping)
414 {
415 unsigned long abs_bits[NLONGS(ABS_CNT)] = {0};
416 int i;
417 int hat = 0;
418 int axis = 0;
419 _AL_JOYSTICK_OUTPUT *output;
420 int num_hats_mapped = 0;
421 int num_axes_mapped = 0;
422
423 /* Scan the axes to get their properties. */
424 if (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)), abs_bits) < 0)
425 return false;
426
427 for (i = LJOY_AXIS_RANGE_START; i < LJOY_AXIS_RANGE_END; i++) {
428 struct input_absinfo absinfo;
429
430 if (!TEST_BIT(i, abs_bits))
431 continue;
432
433 if (ioctl(fd, EVIOCGABS(i), &absinfo) < 0)
434 continue;
435
436 if (is_hat_axis(i)) {
437 output = _al_get_joystick_output(&mapping->hat_map, hat);
438 if (output) {
439 set_axis_mapping(&joy->axis_mapping[i], *output, &absinfo);
440 num_hats_mapped++;
441 }
442 hat++;
443 }
444 else {
445 output = _al_get_joystick_output(&mapping->axis_map, axis);
446 if (output) {
447 set_axis_mapping(&joy->axis_mapping[i], *output, &absinfo);
448 num_axes_mapped++;
449 }
450 axis++;
451 }
452 }
453 if (num_hats_mapped != (int)_al_vector_size(&mapping->hat_map)) {
454 ALLEGRO_ERROR("Could not use mapping, some hats are not mapped.\n");
455 return false;
456 }
457 if (num_axes_mapped != (int)_al_vector_size(&mapping->axis_map)) {
458 ALLEGRO_ERROR("Could not use mapping, some axes are not mapped.\n");
459 return false;
460 }
461 return true;
462 }
463
464
465 static bool fill_joystick_buttons(ALLEGRO_JOYSTICK_LINUX *joy, int fd)
466 {
467 unsigned long key_bits[NLONGS(KEY_CNT)] = {0};
468 int b;
469 int i;
470
471 if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)), key_bits) < 0)
472 return false;
473
474 b = 0;
475
476 for (i = LJOY_BTN_RANGE_START; i < LJOY_BTN_RANGE_END; i++) {
477 if (TEST_BIT(i, key_bits) && is_joystick_button(i)) {
478 joy->button_mapping[b].ev_code = i;
479 joy->button_mapping[b].output = _al_new_joystick_button_output(b);
480 ALLEGRO_DEBUG("Input event code %d maps to button %d\n", i, b);
481
482 joy->parent.info.button[b].name = al_malloc(32);
483 snprintf((char *)joy->parent.info.button[b].name, 32, "B%d", b+1);
484
485 b++;
486 if (b == _AL_MAX_JOYSTICK_BUTTONS)
487 break;
488 }
489 }
490
491 joy->parent.info.num_buttons = b;
492
493 /* Clear the rest. */
494 for (; b < _AL_MAX_JOYSTICK_BUTTONS; b++) {
495 joy->button_mapping[b].ev_code = -1;
496 }
497
498 return true;
499 }
500
501
502
503 static bool fill_joystick_buttons_with_map(ALLEGRO_JOYSTICK_LINUX *joy, int fd, const _AL_JOYSTICK_MAPPING *mapping)
504 {
505 unsigned long key_bits[NLONGS(KEY_CNT)] = {0};
506 int button;
507 int i;
508 int num_buttons_mapped = 0;
509
510 if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)), key_bits) < 0)
511 return false;
512
513 button = 0;
514
515 for (i = LJOY_BTN_RANGE_START; i < LJOY_BTN_RANGE_END; i++) {
516 if (TEST_BIT(i, key_bits)) {
517 joy->button_mapping[button].ev_code = i;
518 _AL_JOYSTICK_OUTPUT *output = _al_get_joystick_output(&mapping->button_map, button);
519 if (output) {
520 joy->button_mapping[button].output = *output;
521 num_buttons_mapped++;
522 }
523 button++;
524 }
525 }
526
527 /* Clear the rest. */
528 for (; button < _AL_MAX_JOYSTICK_BUTTONS; button++) {
529 joy->button_mapping[button].ev_code = -1;
530 }
531
532 if (num_buttons_mapped != (int)_al_vector_size(&mapping->button_map)) {
533 ALLEGRO_ERROR("Could not use mapping, some buttons are not mapped.\n");
534 return false;
535 }
536
537 return true;
538 }
539
540
541
542 static bool fill_gamepad(ALLEGRO_JOYSTICK_LINUX *joy, int fd) {
543 struct input_id id;
544 if (ioctl(fd, EVIOCGID, &id) < 0)
545 return false;
546
547 ALLEGRO_JOYSTICK_GUID guid = _al_new_joystick_guid(id.bustype, id.vendor, id.product, id.version, NULL, joy->name, 0, 0);
548 joy->parent.info.guid = guid;
549 const _AL_JOYSTICK_MAPPING *mapping = _al_get_gamepad_mapping("Linux", guid);
550 if (!mapping)
551 return false;
552
553 if (!fill_joystick_buttons_with_map(joy, fd, mapping))
554 return false;
555
556 if (!fill_joystick_axes_with_map(joy, fd, mapping))
557 return false;
558
559 _al_fill_gamepad_info(&joy->parent.info);
560
561 memcpy(joy->name, mapping->name, sizeof(mapping->name));
562 return true;
563 }
564
565
566
567 static void ljoy_device(ALLEGRO_USTR *device_name) {
568 ALLEGRO_JOYSTICK_LINUX *joy = ljoy_by_device_name(device_name);
569 if (joy) {
570 ALLEGRO_DEBUG("Device %s still exists\n", al_cstr(device_name));
571 joy->marked = true;
572 return;
573 }
574
575 /* Try to open the device. The device must be opened in O_RDWR mode to
576 * allow writing of haptic effects! The haptic driver for linux
577 * reuses the joystick driver's fd.
578 */
579 int fd = open(al_cstr(device_name), O_RDWR|O_NONBLOCK);
580 if (fd == -1) {
581 ALLEGRO_WARN("Failed to open device %s\n", al_cstr(device_name));
582 return;
583 }
584
585 /* The device must have at least one joystick-related axis, and one
586 * joystick-related button. Some devices, such as mouse pads, have ABS_X
587 * and ABS_Y axes like a joystick but not joystick-related buttons. By
588 * checking for both axes and buttons, such devices can be excluded.
589 */
590 if (!have_joystick_button(fd) || !have_joystick_axis(fd)) {
591 ALLEGRO_DEBUG("Device %s not a joystick\n", al_cstr(device_name));
592 close(fd);
593 return;
594 }
595
596 ALLEGRO_DEBUG("Device %s is new\n", al_cstr(device_name));
597
598 joy = ljoy_allocate_structure();
599 joy->fd = fd;
600 joy->device_name = al_ustr_dup(device_name);
601 joy->config_state = LJOY_STATE_BORN;
602 joy->marked = true;
603 config_needs_merging = true;
604
605 if (ioctl(fd, EVIOCGNAME(sizeof(joy->name)), joy->name) < 0)
606 strcpy(joy->name, "Unknown");
607
608 /* Map Linux input API axis and button numbers to ours, and fill in
609 * information.
610 */
611 if (!fill_gamepad(joy, fd)) {
612 if (!fill_joystick_axes(joy, fd) || !fill_joystick_buttons(joy, fd)) {
613 ALLEGRO_ERROR("fill_joystick_info failed %s\n", al_cstr(device_name));
614 inactivate_joy(joy);
615 close(fd);
616 return;
617 }
618 }
619
620 /* Register the joystick with the fdwatch subsystem. */
621 _al_unix_start_watching_fd(joy->fd, ljoy_process_new_data, joy);
622 }
623
624
625 static void ljoy_scan(bool configure)
626 {
627 int num;
628 ALLEGRO_USTR *device_name;
629 const ALLEGRO_FS_INTERFACE *fs_interface;
630 unsigned i;
631
632 /* Clear mark bits. */
633 for (i = 0; i < _al_vector_size(&joysticks); i++) {
634 ALLEGRO_JOYSTICK_LINUX **joypp = _al_vector_ref(&joysticks, i);
635 (*joypp)->marked = false;
636 }
637
638 /* Make sure we're using stdio. */
639 fs_interface = al_get_fs_interface();
640 al_set_standard_fs_interface();
641
642 device_name = al_ustr_new("");
643
644 /* First try to read devices from allegro.cfg. */
645 for (num = 0; num < 32; num++) {
646 if (!ljoy_detect_device_name(num, device_name))
647 continue;
648 ljoy_device(device_name);
649 }
650
651 ALLEGRO_FS_ENTRY *dir = al_create_fs_entry("/dev/input");
652 if (al_open_directory(dir)) {
653 while (true) {
654 ALLEGRO_FS_ENTRY *dev = al_read_directory(dir);
655 if (!dev) {
656 break;
657 }
658 if (al_get_fs_entry_mode(dev) & ALLEGRO_FILEMODE_ISDIR) {
659 al_destroy_fs_entry(dev);
660 continue;
661 }
662 al_ustr_assign_cstr(device_name, al_get_fs_entry_name(dev));
663 ljoy_device(device_name);
664 al_destroy_fs_entry(dev);
665 }
666 al_close_directory(dir);
667 }
668 al_destroy_fs_entry(dir);
669
670 al_ustr_free(device_name);
671
672 /* Restore the fs interface. */
673 al_set_fs_interface(fs_interface);
674
675 /* Schedule unmarked structures to be inactivated. */
676 for (i = 0; i < _al_vector_size(&joysticks); i++) {
677 ALLEGRO_JOYSTICK_LINUX **joypp = _al_vector_ref(&joysticks, i);
678 ALLEGRO_JOYSTICK_LINUX *joy = *joypp;
679
680 if (joy->config_state == LJOY_STATE_ALIVE && !joy->marked) {
681 ALLEGRO_DEBUG("Device %s to be inactivated\n",
682 al_cstr(joy->device_name));
683 joy->config_state = LJOY_STATE_DYING;
684 config_needs_merging = true;
685 }
686 }
687
688 /* Generate a configure event if necessary.
689 * Even if we generated one before that the user hasn't responded to,
690 * we don't know if the user received it so always generate it.
691 */
692 if (config_needs_merging && configure) {
693 ljoy_generate_configure_event();
694 }
695 }
696
697
698
699 static void ljoy_merge(void)
700 {
701 unsigned i;
702
703 config_needs_merging = false;
704 num_joysticks = 0;
705
706 for (i = 0; i < _al_vector_size(&joysticks); i++) {
707 ALLEGRO_JOYSTICK_LINUX **slot = _al_vector_ref(&joysticks, i);
708 ALLEGRO_JOYSTICK_LINUX *joy = *slot;
709
710 switch (joy->config_state) {
711 case LJOY_STATE_UNUSED:
712 break;
713
714 case LJOY_STATE_BORN:
715 case LJOY_STATE_ALIVE:
716 joy->config_state = LJOY_STATE_ALIVE;
717 num_joysticks++;
718 break;
719
720 case LJOY_STATE_DYING:
721 inactivate_joy(joy);
722 break;
723 }
724 }
725
726 ALLEGRO_DEBUG("Merge done, num_joysticks=%d\n", num_joysticks);
727 }
728
729
730
731 #ifdef SUPPORT_HOTPLUG
732 /* ljoy_config_dev_changed: [fdwatch thread]
733 * Called when the /dev hierarchy changes.
734 */
735 static void ljoy_config_dev_changed(void *data)
736 {
737 char buf[128];
738 (void)data;
739
740 /* Empty the event buffer. We only care that some inotify event was sent but it
741 * doesn't matter what it is since we are going to do a full scan anyway once
742 * the timer_fd fires.
743 */
744 while (read(inotify_fd, buf, sizeof(buf)) > 0) {
745 }
746
747 al_signal_cond(hotplug_cond);
748 }
749
750 static void *hotplug_proc(ALLEGRO_THREAD *thread, void *data)
751 {
752 (void)data;
753
754 while (!al_get_thread_should_stop(thread)) {
755 if (!hotplug_ended) {
756 al_wait_cond(hotplug_cond, hotplug_mutex);
757 }
758 if (hotplug_ended) {
759 break;
760 }
761
762 al_rest(1);
763
764 al_lock_mutex(config_mutex);
765 ljoy_scan(true);
766 al_unlock_mutex(config_mutex);
767 }
768
769 hotplug_ended = false;
770
771 return NULL;
772 }
773 #endif
774
775
776
777 /* ljoy_init_joystick: [primary thread]
778 * Initialise the joystick driver.
779 */
780 static bool ljoy_init_joystick(void)
781 {
782 _al_vector_init(&joysticks, sizeof(ALLEGRO_JOYSTICK_LINUX *));
783 num_joysticks = 0;
784
785 if (!(config_mutex = al_create_mutex())) {
786 return false;
787 }
788
789 // Scan for joysticks
790 ljoy_scan(false);
791 ljoy_merge();
792
793 #ifdef SUPPORT_HOTPLUG
794 if (!(hotplug_mutex = al_create_mutex())) {
795 al_destroy_mutex(config_mutex);
796 return false;
797 }
798 if (!(hotplug_cond = al_create_cond())) {
799 al_destroy_mutex(config_mutex);
800 al_destroy_mutex(hotplug_mutex);
801 return false;
802 }
803 if (!(hotplug_thread = al_create_thread(hotplug_proc, NULL))) {
804 al_destroy_mutex(config_mutex);
805 al_destroy_mutex(hotplug_mutex);
806 al_destroy_cond(hotplug_cond);
807 return false;
808 }
809
810 al_start_thread(hotplug_thread);
811
812 inotify_fd = inotify_init();
813 if (inotify_fd != -1) {
814 fcntl(inotify_fd, F_SETFL, O_NONBLOCK);
815 /* Modern Linux probably only needs to monitor /dev/input. */
816 inotify_add_watch(inotify_fd, "/dev/input", IN_CREATE|IN_DELETE);
817 _al_unix_start_watching_fd(inotify_fd, ljoy_config_dev_changed, NULL);
818 ALLEGRO_INFO("Hotplugging enabled\n");
819 }
820 else {
821 ALLEGRO_WARN("Hotplugging not enabled\n");
822 if (inotify_fd != -1) {
823 close(inotify_fd);
824 inotify_fd = -1;
825 }
826 }
827 #endif
828
829 return true;
830 }
831
832
833
834 /* ljoy_exit_joystick: [primary thread]
835 * Shut down the joystick driver.
836 */
837 static void ljoy_exit_joystick(void)
838 {
839 int i;
840
841 #ifdef SUPPORT_HOTPLUG
842 if (inotify_fd != -1) {
843 _al_unix_stop_watching_fd(inotify_fd);
844 close(inotify_fd);
845 inotify_fd = -1;
846 }
847 hotplug_ended = true;
848 al_signal_cond(hotplug_cond);
849 al_join_thread(hotplug_thread, NULL);
850 #endif
851
852 al_destroy_mutex(config_mutex);
853 config_mutex = NULL;
854
855 for (i = 0; i < (int)_al_vector_size(&joysticks); i++) {
856 ALLEGRO_JOYSTICK_LINUX **slot = _al_vector_ref(&joysticks, i);
857 inactivate_joy(*slot);
858 al_free(*slot);
859 }
860 _al_vector_free(&joysticks);
861 num_joysticks = 0;
862 }
863
864
865
866 /* ljoy_reconfigure_joysticks: [primary thread]
867 */
868 static bool ljoy_reconfigure_joysticks(void)
869 {
870 bool ret = false;
871
872 al_lock_mutex(config_mutex);
873
874 if (config_needs_merging) {
875 ljoy_merge();
876 ret = true;
877 }
878
879 al_unlock_mutex(config_mutex);
880
881 return ret;
882 }
883
884
885
886 /* ljoy_num_joysticks: [primary thread]
887 *
888 * Return the number of joysticks available on the system.
889 */
890 static int ljoy_num_joysticks(void)
891 {
892 return num_joysticks;
893 }
894
895
896
897 /* ljoy_get_joystick: [primary thread]
898 *
899 * Returns the address of a ALLEGRO_JOYSTICK structure for the device
900 * number NUM.
901 */
902 static ALLEGRO_JOYSTICK *ljoy_get_joystick(int num)
903 {
904 ALLEGRO_JOYSTICK *ret = NULL;
905 unsigned i;
906 ASSERT(num >= 0);
907
908 al_lock_mutex(config_mutex);
909
910 for (i = 0; i < _al_vector_size(&joysticks); i++) {
911 ALLEGRO_JOYSTICK_LINUX **slot = _al_vector_ref(&joysticks, i);
912 ALLEGRO_JOYSTICK_LINUX *joy = *slot;
913
914 if (ACTIVE_STATE(joy->config_state)) {
915 if (num == 0) {
916 ret = (ALLEGRO_JOYSTICK *)joy;
917 break;
918 }
919 num--;
920 }
921 }
922
923 al_unlock_mutex(config_mutex);
924
925 return ret;
926 }
927
928
929
930 /* ljoy_release_joystick: [primary thread]
931 *
932 * Close the device for a joystick then free the joystick structure.
933 */
934 static void ljoy_release_joystick(ALLEGRO_JOYSTICK *joy_)
935 {
936 (void)joy_;
937 }
938
939
940
941 /* ljoy_get_joystick_state: [primary thread]
942 *
943 * Copy the internal joystick state to a user-provided structure.
944 */
945 static void ljoy_get_joystick_state(ALLEGRO_JOYSTICK *joy_, ALLEGRO_JOYSTICK_STATE *ret_state)
946 {
947 ALLEGRO_JOYSTICK_LINUX *joy = (ALLEGRO_JOYSTICK_LINUX *) joy_;
948 ALLEGRO_EVENT_SOURCE *es = al_get_joystick_event_source();
949
950 _al_event_source_lock(es);
951 {
952 *ret_state = joy->joystate;
953 }
954 _al_event_source_unlock(es);
955 }
956
957
958
959 static const char *ljoy_get_name(ALLEGRO_JOYSTICK *joy_)
960 {
961 ALLEGRO_JOYSTICK_LINUX *joy = (ALLEGRO_JOYSTICK_LINUX *)joy_;
962 return joy->name;
963 }
964
965
966
967 static bool ljoy_get_active(ALLEGRO_JOYSTICK *joy_)
968 {
969 ALLEGRO_JOYSTICK_LINUX *joy = (ALLEGRO_JOYSTICK_LINUX *)joy_;
970
971 return ACTIVE_STATE(joy->config_state);
972 }
973
974
975
976 static bool map_button_number(const ALLEGRO_JOYSTICK_LINUX *joy, int ev_code, _AL_JOYSTICK_OUTPUT *output)
977 {
978 int b;
979
980 for (b = 0; b < _AL_MAX_JOYSTICK_BUTTONS; b++) {
981 const BUTTON_MAPPING *m = &joy->button_mapping[b];
982 if (m->ev_code == ev_code) {
983 *output = m->output;
984 return true;
985 }
986 }
987
988 return false;
989 }
990
991
992
993 static float norm_pos(const AXIS_MAPPING *map, float value)
994 {
995 const float range = (float)map->max - (float)map->min;
996 return ((value - (float)map->min) / range) * 2.0f - 1.0f;
997 }
998
999
1000
1001 /* ljoy_process_new_data: [fdwatch thread]
1002 *
1003 * Process new data arriving in the joystick's fd.
1004 */
1005 static void ljoy_process_new_data(void *data)
1006 {
1007 ALLEGRO_JOYSTICK_LINUX *joy = data;
1008 ALLEGRO_EVENT_SOURCE *es = al_get_joystick_event_source();
1009
1010 if (!es) {
1011 // Joystick driver not fully initialized
1012 return;
1013 }
1014
1015 _al_event_source_lock(es);
1016 {
1017 struct input_event input_events[32];
1018 int bytes, nr, i;
1019
1020 while ((bytes = read(joy->fd, &input_events, sizeof input_events)) > 0) {
1021 nr = bytes / sizeof(struct input_event);
1022
1023 for (i = 0; i < nr; i++) {
1024 int type = input_events[i].type;
1025 int code = input_events[i].code;
1026 int value = input_events[i].value;
1027
1028 if (type == EV_KEY) {
1029 _AL_JOYSTICK_OUTPUT output;
1030 if (map_button_number(joy, code, &output)) {
1031 if (output.button_enabled || output.pos_enabled || output.neg_enabled)
1032 _al_joystick_generate_button_event((ALLEGRO_JOYSTICK *)joy, &joy->joystate, output, value);
1033 }
1034 }
1035 else if (type == EV_ABS) {
1036 int number = code;
1037 if (number < TOTAL_JOYSTICK_AXES) {
1038 const AXIS_MAPPING *map = &joy->axis_mapping[number];
1039 _AL_JOYSTICK_OUTPUT output = map->output;
1040 float pos = norm_pos(map, value);
1041 if (output.button_enabled || output.pos_enabled || output.neg_enabled)
1042 _al_joystick_generate_axis_event((ALLEGRO_JOYSTICK *)joy, &joy->joystate, output, pos);
1043 }
1044 }
1045 }
1046 }
1047 }
1048 _al_event_source_unlock(es);
1049 }
1050
1051
1052
1053 #endif /* ALLEGRO_HAVE_LINUX_INPUT_H */
1054
1055
1056
1057 /* vim: set sts=3 sw=3 et: */