1 // Ripped Snes9X joystick code and made it Genesis friendly, with a few
2 // slight modifications. [PKH]
14 // Default setup for Gravis GamePad Pro 10 button controllers.
24 // Start .... START :)
27 // Mappings are configurable via dgenrc. Mappings are done from 0-16
28 // Standard 2 button joysticks will only have buttons 0 and 1. [PKH]
30 extern int js_map_button
[2][16];
32 #ifdef LINUX_JOYSTICK_SUPPORT
33 #warning USING LINUX JOYSTICK
34 #include <linux/joystick.h>
38 int js_fd
[2] = {-1, -1};
40 char *js_device
[2] = {"/dev/js0", "/dev/js1"};
42 void md::init_joysticks() {
45 unsigned char axes
, buttons
;
47 if ((js_fd
[0] = open (js_device
[0], O_RDONLY
| O_NONBLOCK
)) < 0)
49 perror (js_device
[0]);
53 if (ioctl (js_fd
[0], JSIOCGVERSION
, &version
))
55 puts("joystick: You need at least driver version 1.0 for joystick support");
59 js_fd
[1] = open (js_device
[1], O_RDONLY
| O_NONBLOCK
);
64 if (ioctl (js_fd
[0], JSIOCGNAME(128), name
) > 0)
66 printf ("Using %s (%s) as pad1", name
, js_device
[0]);
69 ioctl (js_fd
[1], JSIOCGNAME(128), name
);
70 printf ("and %s (%s) as pad2", name
, js_device
[1]);
77 ioctl (js_fd
[0], JSIOCGAXES
, &axes
);
78 ioctl (js_fd
[0], JSIOCGBUTTONS
, &buttons
);
79 printf ("Using %d-axis %d-button joystick (%s) as pad1", axes
, buttons
, js_device
[0]);
82 ioctl (js_fd
[0], JSIOCGAXES
, &axes
);
83 ioctl (js_fd
[0], JSIOCGBUTTONS
, &buttons
);
84 printf (" and %d-axis %d-button (%s) as pad2", axes
, buttons
, js_device
[1]);
89 #endif // JSIOCGVERSION
92 void md::read_joysticks()
95 struct js_event js_ev
;
98 for (i
= 0; i
< 2 && js_fd
[i
] >= 0; i
++)
100 while (read (js_fd
[i
], &js_ev
, sizeof (struct js_event
)) == sizeof (struct js_event
) )
102 switch (js_ev
.type
& ~JS_EVENT_INIT
)
105 if (js_ev
.number
== 0)
107 if(js_ev
.value
< -16384)
109 pad
[i
] &= ~MD_LEFT_MASK
;
110 pad
[i
] |= MD_RIGHT_MASK
;
113 if (js_ev
.value
> 16384)
115 pad
[i
] |= MD_LEFT_MASK
;
116 pad
[i
] &= ~MD_RIGHT_MASK
;
119 pad
[i
] |= MD_LEFT_MASK
;
120 pad
[i
] |= MD_RIGHT_MASK
;
124 if (js_ev
.number
== 1)
126 if (js_ev
.value
< -16384)
128 pad
[i
] &= ~MD_UP_MASK
;
129 pad
[i
] |= MD_DOWN_MASK
;
132 if (js_ev
.value
> 16384)
134 pad
[i
] |= MD_UP_MASK
;
135 pad
[i
] &= ~MD_DOWN_MASK
;
138 pad
[i
] |= MD_UP_MASK
;
139 pad
[i
] |= MD_DOWN_MASK
;
145 case JS_EVENT_BUTTON
:
146 if (js_ev
.number
> 15)
150 pad
[i
] &= ~js_map_button
[i
][js_ev
.number
];
152 pad
[i
] |= js_map_button
[i
][js_ev
.number
];
158 #endif // JSIOCGVERSION
161 #elif defined(SDL_JOYSTICK_SUPPORT)
162 #warning USING SDL JOYSTICK
164 #include <SDL_joystick.h>
166 static SDL_Joystick
*js_handle
[2] = { NULL
, NULL
};
168 void md::init_joysticks() {
169 // Initialize the joystick support
170 // Thanks to Cameron Moore <cameron@unbeatenpath.net>
171 if(SDL_Init(SDL_INIT_JOYSTICK
) < 0)
173 fprintf(stderr
, "joystick: Unable to initialize joystick system\n");
177 // Open the first couple of joysticks, if possible
178 js_handle
[0] = SDL_JoystickOpen(0);
179 js_handle
[1] = SDL_JoystickOpen(1);
181 // If neither opened, quit
182 if(!(js_handle
[0] || js_handle
[1]))
184 fprintf(stderr
, "joystick: Unable to open any joysticks\n");
188 // Print the joystick names
190 if(js_handle
[0]) printf("%s (#0) as pad1 ", SDL_JoystickName(0));
191 if(js_handle
[0] && js_handle
[1]) printf("and ");
192 if(js_handle
[1]) printf("%s (#1) as pad2 ", SDL_JoystickName(1));
195 // Enable joystick events
196 SDL_JoystickEventState(SDL_ENABLE
);
199 // This does nothing; SDL joystick handling is in the main event loop in
201 void md::read_joysticks()