forked from laamaa/m8c
-
Notifications
You must be signed in to change notification settings - Fork 1
/
input.c
477 lines (424 loc) · 14.3 KB
/
input.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// Copyright 2021 Jonne Kokkonen
// Released under the MIT licence, https://opensource.org/licenses/MIT
#include <SDL.h>
#include <stdio.h>
#include "config.h"
#include "input.h"
#include "render.h"
#include "write.h"
#define MAX_CONTROLLERS 4
SDL_GameController *game_controllers[MAX_CONTROLLERS];
// Bits for M8 input messages
enum keycodes {
key_left = 1 << 7,
key_up = 1 << 6,
key_down = 1 << 5,
key_select = 1 << 4,
key_start = 1 << 3,
key_right = 1 << 2,
key_opt = 1 << 1,
key_edit = 1
};
uint8_t keyjazz_enabled = 0;
uint8_t keyjazz_base_octave = 2;
uint8_t keyjazz_velocity = 0x64;
static uint8_t keycode = 0; // value of the pressed key
static int num_joysticks = 0;
input_msg_s key = {normal, 0};
uint8_t toggle_input_keyjazz() {
keyjazz_enabled = !keyjazz_enabled;
return keyjazz_enabled;
}
// Opens available game controllers and returns the amount of opened controllers
int initialize_game_controllers() {
num_joysticks = SDL_NumJoysticks();
int controller_index = 0;
SDL_Log("Looking for game controllers\n");
SDL_Delay(
10); // Some controllers like XBone wired need a little while to get ready
// Try to load the game controller database file
char db_filename[1024] = {0};
snprintf(db_filename, sizeof(db_filename), "%sgamecontrollerdb.txt",
SDL_GetPrefPath("", "m8c"));
SDL_Log("Trying to open game controller database from %s", db_filename);
SDL_RWops* db_rw = SDL_RWFromFile(db_filename, "rb");
if (db_rw == NULL) {
snprintf(db_filename, sizeof(db_filename), "%sgamecontrollerdb.txt",
SDL_GetBasePath());
SDL_Log("Trying to open game controller database from %s", db_filename);
db_rw = SDL_RWFromFile(db_filename, "rb");
}
if (db_rw != NULL) {
int mappings = SDL_GameControllerAddMappingsFromRW(db_rw, 1);
if (mappings != -1)
SDL_Log("Found %d game controller mappings", mappings);
else
SDL_LogError(SDL_LOG_CATEGORY_INPUT,
"Error loading game controller mappings.");
} else {
SDL_LogError(SDL_LOG_CATEGORY_INPUT,
"Unable to open game controller database file.");
}
// Open all available game controllers
for (int i = 0; i < num_joysticks; i++) {
if (!SDL_IsGameController(i))
continue;
if (controller_index >= MAX_CONTROLLERS)
break;
game_controllers[controller_index] = SDL_GameControllerOpen(i);
SDL_Log("Controller %d: %s", controller_index + 1,
SDL_GameControllerName(game_controllers[controller_index]));
controller_index++;
}
return controller_index;
}
// Closes all open game controllers
void close_game_controllers() {
for (int i = 0; i < MAX_CONTROLLERS; i++) {
if (game_controllers[i])
SDL_GameControllerClose(game_controllers[i]);
}
}
static input_msg_s handle_keyjazz(SDL_Event *event, uint8_t keyvalue) {
input_msg_s key = {keyjazz, keyvalue, keyjazz_velocity, event->type};
switch (event->key.keysym.scancode) {
case SDL_SCANCODE_Z:
key.value = keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_S:
key.value = 1 + keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_X:
key.value = 2 + keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_D:
key.value = 3 + keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_C:
key.value = 4 + keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_V:
key.value = 5 + keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_G:
key.value = 6 + keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_B:
key.value = 7 + keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_H:
key.value = 8 + keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_N:
key.value = 9 + keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_J:
key.value = 10 + keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_M:
key.value = 11 + keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_Q:
key.value = 12 + keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_2:
key.value = 13 + keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_W:
key.value = 14 + keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_3:
key.value = 15 + keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_E:
key.value = 16 + keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_R:
key.value = 17 + keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_5:
key.value = 18 + keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_T:
key.value = 19 + keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_6:
key.value = 20 + keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_Y:
key.value = 21 + keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_7:
key.value = 22 + keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_U:
key.value = 23 + keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_I:
key.value = 24 + keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_9:
key.value = 25 + keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_O:
key.value = 26 + keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_0:
key.value = 27 + keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_P:
key.value = 28 + keyjazz_base_octave * 12;
break;
case SDL_SCANCODE_KP_DIVIDE:
key.type = normal;
if (event->type == SDL_KEYDOWN && keyjazz_base_octave > 0) {
keyjazz_base_octave--;
display_keyjazz_overlay(1, keyjazz_base_octave, keyjazz_velocity);
}
break;
case SDL_SCANCODE_KP_MULTIPLY:
key.type = normal;
if (event->type == SDL_KEYDOWN && keyjazz_base_octave < 8) {
keyjazz_base_octave++;
display_keyjazz_overlay(1, keyjazz_base_octave, keyjazz_velocity);
}
break;
case SDL_SCANCODE_KP_MINUS:
key.type = normal;
if (event->type == SDL_KEYDOWN) {
if ((event->key.keysym.mod & KMOD_ALT) > 0) {
if (keyjazz_velocity > 1)
keyjazz_velocity -= 1;
} else {
if (keyjazz_velocity > 0x10)
keyjazz_velocity -= 0x10;
}
display_keyjazz_overlay(1, keyjazz_base_octave, keyjazz_velocity);
}
break;
case SDL_SCANCODE_KP_PLUS:
key.type = normal;
if (event->type == SDL_KEYDOWN) {
if ((event->key.keysym.mod & KMOD_ALT) > 0) {
if (keyjazz_velocity < 0x7F)
keyjazz_velocity += 1;
} else {
if (keyjazz_velocity < 0x6F)
keyjazz_velocity += 0x10;
}
display_keyjazz_overlay(1, keyjazz_base_octave, keyjazz_velocity);
}
break;
default:
key.type = normal;
break;
}
return key;
}
static input_msg_s handle_normal_keys(SDL_Event *event, config_params_s *conf,
uint8_t keyvalue) {
input_msg_s key = {normal, keyvalue};
if (event->key.keysym.scancode == conf->key_up) {
key.value = key_up;
} else if (event->key.keysym.scancode == conf->key_left) {
key.value = key_left;
} else if (event->key.keysym.scancode == conf->key_down) {
key.value = key_down;
} else if (event->key.keysym.scancode == conf->key_right) {
key.value = key_right;
} else if (event->key.keysym.scancode == conf->key_select ||
event->key.keysym.scancode == conf->key_select_alt) {
key.value = key_select;
} else if (event->key.keysym.scancode == conf->key_start ||
event->key.keysym.scancode == conf->key_start_alt) {
key.value = key_start;
} else if (event->key.keysym.scancode == conf->key_opt ||
event->key.keysym.scancode == conf->key_opt_alt) {
key.value = key_opt;
} else if (event->key.keysym.scancode == conf->key_edit ||
event->key.keysym.scancode == conf->key_edit_alt) {
key.value = key_edit;
} else if (event->key.keysym.scancode == conf->key_delete) {
key.value = key_opt | key_edit;
} else if (event->key.keysym.scancode == conf->key_reset) {
key = (input_msg_s){special, msg_reset_display};
} else {
key.value = 0;
}
return key;
}
// Check whether a button is pressed on a gamepad and return 1 if pressed.
static int get_game_controller_button(config_params_s *conf,
SDL_GameController *controller,
int button) {
const int button_mappings[8] = {conf->gamepad_up, conf->gamepad_down,
conf->gamepad_left, conf->gamepad_right,
conf->gamepad_opt, conf->gamepad_edit,
conf->gamepad_select, conf->gamepad_start};
// Check digital buttons
if (SDL_GameControllerGetButton(controller, button_mappings[button])) {
return 1;
} else {
// If digital button isn't pressed, check the corresponding analog control
switch (button) {
case INPUT_UP:
return SDL_GameControllerGetAxis(controller,
conf->gamepad_analog_axis_updown) <
-conf->gamepad_analog_threshold;
case INPUT_DOWN:
return SDL_GameControllerGetAxis(controller,
conf->gamepad_analog_axis_updown) >
conf->gamepad_analog_threshold;
case INPUT_LEFT:
return SDL_GameControllerGetAxis(controller,
conf->gamepad_analog_axis_leftright) <
-conf->gamepad_analog_threshold;
case INPUT_RIGHT:
return SDL_GameControllerGetAxis(controller,
conf->gamepad_analog_axis_leftright) >
conf->gamepad_analog_threshold;
case INPUT_OPT:
return SDL_GameControllerGetAxis(controller,
conf->gamepad_analog_axis_opt) >
conf->gamepad_analog_threshold;
case INPUT_EDIT:
return SDL_GameControllerGetAxis(controller,
conf->gamepad_analog_axis_edit) >
conf->gamepad_analog_threshold;
case INPUT_SELECT:
return SDL_GameControllerGetAxis(controller,
conf->gamepad_analog_axis_select) >
conf->gamepad_analog_threshold;
case INPUT_START:
return SDL_GameControllerGetAxis(controller,
conf->gamepad_analog_axis_start) >
conf->gamepad_analog_threshold;
default:
return 0;
}
}
return 0;
}
// Handle game controllers, simply check all buttons and analog axis on every
// cycle
static int handle_game_controller_buttons(config_params_s *conf) {
const int keycodes[8] = {key_up, key_down, key_left, key_right,
key_opt, key_edit, key_select, key_start};
int key = 0;
// Cycle through every active game controller
for (int gc = 0; gc < num_joysticks; gc++) {
// Cycle through all M8 buttons
for (int button = 0; button < (input_buttons_t)INPUT_MAX; button++) {
// If the button is active, add the keycode to the variable containing
// active keys
if (get_game_controller_button(conf, game_controllers[gc], button)) {
key |= keycodes[button];
}
}
}
return key;
}
// Handles SDL input events
void handle_sdl_events(config_params_s *conf) {
static int prev_key_analog = 0;
SDL_Event event;
// Read joysticks
int key_analog = handle_game_controller_buttons(conf);
if (prev_key_analog != key_analog) {
keycode = key_analog;
prev_key_analog = key_analog;
}
// Read special case game controller buttons quit and reset
for (int gc = 0; gc < num_joysticks; gc++) {
if (SDL_GameControllerGetButton(game_controllers[gc], conf->gamepad_quit) &&
(SDL_GameControllerGetButton(game_controllers[gc], conf->gamepad_select) ||
SDL_GameControllerGetAxis(game_controllers[gc], conf->gamepad_analog_axis_select)))
key = (input_msg_s){special, msg_quit};
else if (SDL_GameControllerGetButton(game_controllers[gc], conf->gamepad_reset) &&
(SDL_GameControllerGetButton(game_controllers[gc], conf->gamepad_select) ||
SDL_GameControllerGetAxis(game_controllers[gc], conf->gamepad_analog_axis_select)))
key = (input_msg_s){special, msg_reset_display};
}
SDL_PollEvent(&event);
switch (event.type) {
// Reinitialize game controllers on controller add/remove/remap
case SDL_CONTROLLERDEVICEADDED:
case SDL_CONTROLLERDEVICEREMOVED:
initialize_game_controllers();
break;
// Handle SDL quit events (for example, window close)
case SDL_QUIT:
key = (input_msg_s){special, msg_quit};
break;
case SDL_WINDOWEVENT:
if (event.window.event == SDL_WINDOWEVENT_RESIZED)
{
SDL_Log("Resizing window...");
key = (input_msg_s){special, msg_reset_display};
}
break;
// Keyboard events. Special events are handled within SDL_KEYDOWN.
case SDL_KEYDOWN:
// ALT+ENTER toggles fullscreen
if (event.key.keysym.sym == SDLK_RETURN &&
(event.key.keysym.mod & KMOD_ALT) > 0) {
toggle_fullscreen();
break;
}
// ALT+Q quits program
if (event.key.keysym.sym == SDLK_q &&
(event.key.keysym.mod & KMOD_ALT) > 0) {
key = (input_msg_s){special, msg_quit};
break;
}
// ESC = toggle keyjazz
if (event.key.keysym.sym == SDLK_ESCAPE) {
display_keyjazz_overlay(toggle_input_keyjazz(), keyjazz_base_octave, keyjazz_velocity);
}
// Normal keyboard inputs
case SDL_KEYUP:
key = handle_normal_keys(&event, conf, 0);
if (keyjazz_enabled)
key = handle_keyjazz(&event, key.value);
break;
default:
break;
}
switch (key.type) {
case normal:
if (event.type == SDL_KEYDOWN) {
keycode |= key.value;
} else {
keycode &= ~key.value;
}
break;
case keyjazz:
// Do not allow pressing multiple keys with keyjazz
case special:
if (event.type == SDL_KEYDOWN) {
keycode = key.value;
} else {
keycode = 0;
}
break;
default:
break;
}
}
// Returns the currently pressed keys to main
input_msg_s get_input_msg(config_params_s *conf) {
key = (input_msg_s){normal, 0};
// Query for SDL events
handle_sdl_events(conf);
if (keycode == (key_start | key_select | key_opt | key_edit)) {
key = (input_msg_s){special, msg_reset_display};
}
if (key.type == normal) {
/* Normal input keys go through some event-based manipulation in
handle_sdl_events(), the value is stored in keycode variable */
return (input_msg_s){key.type, keycode};
} else {
// Special event keys already have the correct keycode baked in
return key;
}
}