-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathinput.c
314 lines (281 loc) · 8.98 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
#include "input.h"
#include "config.h"
#include "gamecontrollers.h"
#include "render.h"
#include <SDL.h>
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 input_msg_s key = {normal, 0, 0, 0};
uint8_t toggle_input_keyjazz() {
keyjazz_enabled = !keyjazz_enabled;
SDL_LogDebug(SDL_LOG_CATEGORY_SYSTEM, keyjazz_enabled ? "Keyjazz enabled" : "Keyjazz disabled");
return keyjazz_enabled;
}
static input_msg_s handle_keyjazz(SDL_Event *event, uint8_t keyvalue, config_params_s *conf) {
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;
default:
key.type = normal;
if (event->key.repeat > 0 || event->key.type == SDL_KEYUP) {
break;
}
if (event->key.keysym.scancode == conf->key_jazz_dec_octave) {
if (keyjazz_base_octave > 0) {
keyjazz_base_octave--;
display_keyjazz_overlay(1, keyjazz_base_octave, keyjazz_velocity);
}
} else if (event->key.keysym.scancode == conf->key_jazz_inc_octave) {
if (keyjazz_base_octave < 8) {
keyjazz_base_octave++;
display_keyjazz_overlay(1, keyjazz_base_octave, keyjazz_velocity);
}
} else if (event->key.keysym.scancode == conf->key_jazz_dec_velocity) {
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);
} else if (event->key.keysym.scancode == conf->key_jazz_inc_velocity) {
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;
}
return key;
}
static input_msg_s handle_normal_keys(const SDL_Event *event, const config_params_s *conf) {
input_msg_s key = {normal, 0, 0, 0};
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, 0, 0};
} else if (event->key.keysym.scancode == conf->key_toggle_audio) {
key = (input_msg_s){special, msg_toggle_audio, 0, 0};
} else {
key.value = 0;
}
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
const int key_analog = gamecontrollers_handle_buttons(conf);
if (prev_key_analog != key_analog) {
keycode = key_analog;
prev_key_analog = key_analog;
}
input_msg_s gcmsg = gamecontrollers_handle_special_messages(conf);
if (gcmsg.type == special) {
key = gcmsg;
}
while (SDL_PollEvent(&event)) {
switch (event.type) {
// Reinitialize game controllers on controller add/remove/remap
case SDL_CONTROLLERDEVICEADDED:
case SDL_CONTROLLERDEVICEREMOVED:
gamecontrollers_initialize();
break;
// Handle SDL quit events (for example, window close)
case SDL_QUIT:
key = (input_msg_s){special, msg_quit, 0, 0};
break;
case SDL_WINDOWEVENT:
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
static uint32_t ticks_window_resized = 0;
if (SDL_GetTicks() - ticks_window_resized > 500) {
SDL_Log("Resizing window...");
key = (input_msg_s){special, msg_reset_display, 0, 0};
ticks_window_resized = SDL_GetTicks();
}
}
break;
case SDL_KEYDOWN:
if (event.key.repeat > 0) {
break;
}
// ALT+ENTER toggles fullscreen
if (event.key.keysym.sym == SDLK_RETURN && (event.key.keysym.mod & KMOD_ALT) > 0) {
toggle_fullscreen();
break;
}
// ALT+F4 quits program
if (event.key.keysym.sym == SDLK_F4 && (event.key.keysym.mod & KMOD_ALT) > 0) {
key = (input_msg_s){special, msg_quit, 0, 0};
break;
}
// ESC = toggle keyjazz
if (event.key.keysym.sym == SDLK_ESCAPE) {
display_keyjazz_overlay(toggle_input_keyjazz(), keyjazz_base_octave, keyjazz_velocity);
break;
}
// Intentional fallthrough
case SDL_KEYUP:
// Normal keyboard inputs
key = handle_normal_keys(&event, conf);
if (keyjazz_enabled) {
key = handle_keyjazz(&event, key.value, conf);
}
break;
default:
break;
}
switch (key.type) {
case normal:
if (event.type == SDL_KEYDOWN) {
keycode |= key.value;
} else if (event.type == SDL_KEYUP) {
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 if (event.type == SDL_KEYUP) {
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, 0, 0};
// Query for SDL events
handle_sdl_events(conf);
if (!keyjazz_enabled && keycode == (key_start | key_select | key_opt | key_edit)) {
key = (input_msg_s){special, msg_reset_display, 0, 0};
}
if (key.type == normal) {
/* Normal input keys go through some event-based manipulation in
handle_sdl_events(), the value is stored in keycode variable */
const input_msg_s input = (input_msg_s){key.type, keycode, 0, 0};
return input;
} else {
// Special event keys already have the correct keycode baked in
return key;
}
}