1 /* -*- coding: utf-8 -*- */
2 #ifdef WITH_SDL2
3 #include <SDL2/SDL_hints.h>
4 #endif
5 #include "joy_data.h"
6 
7 extern int verbosity;
8 extern int list_devices;
9 extern int list_sdl_devices;
10 
detect_joysticks()11 void detect_joysticks() {
12   int i = 0, j = 0, active = 0;
13   char buf[100];
14 
15   if (list_devices == 1) { puts("List of joystick devices:"); }
16   else if (list_sdl_devices == 1) { puts("List of SDL joystick devices:"); }
17 
18   /* only maintain state of joysticks that are actually joysticks */
19   for (i = 0; i < NUM_JOYS; i++) {
20     SDL_Joystick *joy = SDL_JoystickOpen(i);
21     int num_axes = SDL_JoystickNumAxes(joy);
22     int num_hats = SDL_JoystickNumHats(joy);
23     int num_btns = SDL_JoystickNumButtons(joy);
24     active = (num_axes > 0 || num_hats > 0 || num_btns > 0) ? 1 : 0;
25 
26     if (list_sdl_devices == 1) {
27       sprintf(buf, "joy %d: %d axes, %d hats, %d buttons",
28               i, num_axes, num_hats, num_btns);
29       puts(buf); }
30 
31     else if (list_devices == 1 && active == 1) {
32       sprintf(buf, "joy %d: %d axes, %d hats, %d buttons",
33               j, num_axes, num_hats, num_btns);
34       puts(buf);
35       j++; }
36 
37     else if (active == 1 && cfg_map[j] == 1) {
38       sprintf(buf, "joy %d configured: ready", j);
39       if (verbosity >= 0) { puts(buf); }
40       joys[j] = joy; dev_map[i] = j; j++; }
41 
42     else if (active == 1 && cfg_map[j] == -1) {
43       sprintf(buf, "joy %d ignored: not configured in specified profile", j);
44       if (verbosity >= 0) { puts(buf); }
45       dev_map[i] = -1; }
46 
47     else { dev_map[i] = -1; } }
48 
49   /* use j for number of joysticks since some of the original were ignored */
50   NUM_JOYS = j;
51   /* if we are merely listing devices, then exit here... */
52   if (list_devices == 1 || list_sdl_devices == 1) { SDL_Quit(); exit(0); } }
53