7
7
#include "config.h"
8
8
#include "input.h"
9
9
#include "render.h"
10
-
11
- #define MAX_CONTROLLERS 4
12
-
13
- SDL_GameController * game_controllers [MAX_CONTROLLERS ];
14
-
15
- // Bits for M8 input messages
16
- enum keycodes {
17
- key_left = 1 << 7 ,
18
- key_up = 1 << 6 ,
19
- key_down = 1 << 5 ,
20
- key_select = 1 << 4 ,
21
- key_start = 1 << 3 ,
22
- key_right = 1 << 2 ,
23
- key_opt = 1 << 1 ,
24
- key_edit = 1
25
- };
10
+ #include "gamecontrollers.h"
26
11
27
12
uint8_t keyjazz_enabled = 0 ;
28
13
uint8_t keyjazz_base_octave = 2 ;
29
14
uint8_t keyjazz_velocity = 0x64 ;
30
15
31
16
static uint8_t keycode = 0 ; // value of the pressed key
32
- static int num_joysticks = 0 ;
33
17
34
18
static input_msg_s key = {normal , 0 };
35
19
@@ -39,60 +23,6 @@ uint8_t toggle_input_keyjazz() {
39
23
return keyjazz_enabled ;
40
24
}
41
25
42
- // Opens available game controllers and returns the amount of opened controllers
43
- int initialize_game_controllers () {
44
-
45
- num_joysticks = SDL_NumJoysticks ();
46
- int controller_index = 0 ;
47
-
48
- SDL_Log ("Looking for game controllers\n" );
49
- SDL_Delay (10 ); // Some controllers like XBone wired need a little while to get ready
50
-
51
- // Try to load the game controller database file
52
- char db_filename [1024 ] = {0 };
53
- snprintf (db_filename , sizeof (db_filename ), "%sgamecontrollerdb.txt" , SDL_GetPrefPath ("" , "m8c" ));
54
- SDL_Log ("Trying to open game controller database from %s" , db_filename );
55
- SDL_RWops * db_rw = SDL_RWFromFile (db_filename , "rb" );
56
- if (db_rw == NULL ) {
57
- snprintf (db_filename , sizeof (db_filename ), "%sgamecontrollerdb.txt" , SDL_GetBasePath ());
58
- SDL_Log ("Trying to open game controller database from %s" , db_filename );
59
- db_rw = SDL_RWFromFile (db_filename , "rb" );
60
- }
61
-
62
- if (db_rw != NULL ) {
63
- int mappings = SDL_GameControllerAddMappingsFromRW (db_rw , 1 );
64
- if (mappings != -1 )
65
- SDL_Log ("Found %d game controller mappings" , mappings );
66
- else
67
- SDL_LogError (SDL_LOG_CATEGORY_INPUT , "Error loading game controller mappings." );
68
- } else {
69
- SDL_LogError (SDL_LOG_CATEGORY_INPUT , "Unable to open game controller database file." );
70
- }
71
-
72
- // Open all available game controllers
73
- for (int i = 0 ; i < num_joysticks ; i ++ ) {
74
- if (!SDL_IsGameController (i ))
75
- continue ;
76
- if (controller_index >= MAX_CONTROLLERS )
77
- break ;
78
- game_controllers [controller_index ] = SDL_GameControllerOpen (i );
79
- SDL_Log ("Controller %d: %s" , controller_index + 1 ,
80
- SDL_GameControllerName (game_controllers [controller_index ]));
81
- controller_index ++ ;
82
- }
83
-
84
- return controller_index ;
85
- }
86
-
87
- // Closes all open game controllers
88
- void close_game_controllers () {
89
-
90
- for (int i = 0 ; i < MAX_CONTROLLERS ; i ++ ) {
91
- if (game_controllers [i ])
92
- SDL_GameControllerClose (game_controllers [i ]);
93
- }
94
- }
95
-
96
26
static input_msg_s handle_keyjazz (SDL_Event * event , uint8_t keyvalue , config_params_s * conf ) {
97
27
input_msg_s key = {keyjazz , keyvalue , keyjazz_velocity , event -> type };
98
28
switch (event -> key .keysym .scancode ) {
@@ -259,75 +189,6 @@ static input_msg_s handle_normal_keys(SDL_Event *event, config_params_s *conf, u
259
189
return key ;
260
190
}
261
191
262
- // Check whether a button is pressed on a gamepad and return 1 if pressed.
263
- static int get_game_controller_button (config_params_s * conf , SDL_GameController * controller ,
264
- int button ) {
265
-
266
- const int button_mappings [8 ] = {conf -> gamepad_up , conf -> gamepad_down , conf -> gamepad_left ,
267
- conf -> gamepad_right , conf -> gamepad_opt , conf -> gamepad_edit ,
268
- conf -> gamepad_select , conf -> gamepad_start };
269
-
270
- // Check digital buttons
271
- if (SDL_GameControllerGetButton (controller , button_mappings [button ])) {
272
- return 1 ;
273
- }
274
-
275
- // If digital button isn't pressed, check the corresponding analog control
276
- switch (button ) {
277
- case INPUT_UP :
278
- return SDL_GameControllerGetAxis (controller , conf -> gamepad_analog_axis_updown ) <
279
- - conf -> gamepad_analog_threshold ;
280
- case INPUT_DOWN :
281
- return SDL_GameControllerGetAxis (controller , conf -> gamepad_analog_axis_updown ) >
282
- conf -> gamepad_analog_threshold ;
283
- case INPUT_LEFT :
284
- return SDL_GameControllerGetAxis (controller , conf -> gamepad_analog_axis_leftright ) <
285
- - conf -> gamepad_analog_threshold ;
286
- case INPUT_RIGHT :
287
- return SDL_GameControllerGetAxis (controller , conf -> gamepad_analog_axis_leftright ) >
288
- conf -> gamepad_analog_threshold ;
289
- case INPUT_OPT :
290
- return SDL_GameControllerGetAxis (controller , conf -> gamepad_analog_axis_opt ) >
291
- conf -> gamepad_analog_threshold ;
292
- case INPUT_EDIT :
293
- return SDL_GameControllerGetAxis (controller , conf -> gamepad_analog_axis_edit ) >
294
- conf -> gamepad_analog_threshold ;
295
- case INPUT_SELECT :
296
- return SDL_GameControllerGetAxis (controller , conf -> gamepad_analog_axis_select ) >
297
- conf -> gamepad_analog_threshold ;
298
- case INPUT_START :
299
- return SDL_GameControllerGetAxis (controller , conf -> gamepad_analog_axis_start ) >
300
- conf -> gamepad_analog_threshold ;
301
- default :
302
- return 0 ;
303
- }
304
- return 0 ;
305
- }
306
-
307
- // Handle game controllers, simply check all buttons and analog axis on every
308
- // cycle
309
- static int handle_game_controller_buttons (config_params_s * conf ) {
310
-
311
- const int keycodes [8 ] = {key_up , key_down , key_left , key_right ,
312
- key_opt , key_edit , key_select , key_start };
313
-
314
- int key = 0 ;
315
-
316
- // Cycle through every active game controller
317
- for (int gc = 0 ; gc < num_joysticks ; gc ++ ) {
318
- // Cycle through all M8 buttons
319
- for (int button = 0 ; button < INPUT_MAX ; button ++ ) {
320
- // If the button is active, add the keycode to the variable containing
321
- // active keys
322
- if (get_game_controller_button (conf , game_controllers [gc ], button )) {
323
- key |= keycodes [button ];
324
- }
325
- }
326
- }
327
-
328
- return key ;
329
- }
330
-
331
192
// Handles SDL input events
332
193
void handle_sdl_events (config_params_s * conf ) {
333
194
@@ -336,23 +197,14 @@ void handle_sdl_events(config_params_s *conf) {
336
197
SDL_Event event ;
337
198
338
199
// Read joysticks
339
- int key_analog = handle_game_controller_buttons (conf );
200
+ int key_analog = gamecontrollers_handle_buttons (conf );
340
201
if (prev_key_analog != key_analog ) {
341
202
keycode = key_analog ;
342
203
prev_key_analog = key_analog ;
343
204
}
344
205
345
- // Read special case game controller buttons quit and reset
346
- for (int gc = 0 ; gc < num_joysticks ; gc ++ ) {
347
- if (SDL_GameControllerGetButton (game_controllers [gc ], conf -> gamepad_quit ) &&
348
- (SDL_GameControllerGetButton (game_controllers [gc ], conf -> gamepad_select ) ||
349
- SDL_GameControllerGetAxis (game_controllers [gc ], conf -> gamepad_analog_axis_select )))
350
- key = (input_msg_s ){special , msg_quit };
351
- else if (SDL_GameControllerGetButton (game_controllers [gc ], conf -> gamepad_reset ) &&
352
- (SDL_GameControllerGetButton (game_controllers [gc ], conf -> gamepad_select ) ||
353
- SDL_GameControllerGetAxis (game_controllers [gc ], conf -> gamepad_analog_axis_select )))
354
- key = (input_msg_s ){special , msg_reset_display };
355
- }
206
+ input_msg_s gcmsg = gamecontrollers_handle_special_messages (conf );
207
+ if (gcmsg .type == special ) { key = gcmsg ; }
356
208
357
209
while (SDL_PollEvent (& event )) {
358
210
@@ -361,7 +213,7 @@ void handle_sdl_events(config_params_s *conf) {
361
213
// Reinitialize game controllers on controller add/remove/remap
362
214
case SDL_CONTROLLERDEVICEADDED :
363
215
case SDL_CONTROLLERDEVICEREMOVED :
364
- initialize_game_controllers ();
216
+ gamecontrollers_initialize ();
365
217
break ;
366
218
367
219
// Handle SDL quit events (for example, window close)
0 commit comments