Debian Codegraph @

src/joystick.c

raw symbols SCIP
1 /*
2 * OpenTyrian: A modern cross-platform port of Tyrian
3 * Copyright (C) 2007-2009 The OpenTyrian Development Team
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 #include "joystick.h"
20
21 #include "config.h"
22 #include "config_file.h"
23 #include "file.h"
24 #include "keyboard.h"
25 #include "nortsong.h"
26 #include "opentyr.h"
27 #include "params.h"
28 #include "varz.h"
29 #include "video.h"
30
31 #include <assert.h>
32 #include <ctype.h>
33 #include <string.h>
34
35 int joystick_axis_threshold(int j, int value);
36 int check_assigned(SDL_Joystick *joystick_handle, const Joystick_assignment assignment[2]);
37
38 const char *assignment_to_code(const Joystick_assignment *assignment);
39 void code_to_assignment(Joystick_assignment *assignment, const char *buffer);
40
41 int joystick_repeat_delay = 300; // milliseconds, repeat delay for buttons
42 bool joydown = false; // any joystick buttons down, updated by poll_joysticks()
43 bool ignore_joystick = false;
44
45 int joysticks = 0;
46 Joystick *joystick = NULL;
47
48 static const int joystick_analog_max = 32767;
49
50 // eliminates axis movement below the threshold
51 int joystick_axis_threshold(int j, int value)
52 {
53 assert(j < joysticks);
54
55 bool negative = value < 0;
56 if (negative)
57 value = -value;
58
59 if (value <= joystick[j].threshold * 1000)
60 return 0;
61
62 value -= joystick[j].threshold * 1000;
63
64 return negative ? -value : value;
65 }
66
67 // converts joystick axis to sane Tyrian-usable value (based on sensitivity)
68 int joystick_axis_reduce(int j, int value)
69 {
70 assert(j < joysticks);
71
72 value = joystick_axis_threshold(j, value);
73
74 if (value == 0)
75 return 0;
76
77 return value / (3000 - 200 * joystick[j].sensitivity);
78 }
79
80 // converts analog joystick axes to an angle
81 // returns false if axes are centered (there is no angle)
82 bool joystick_analog_angle(int j, float *angle)
83 {
84 assert(j < joysticks);
85
86 float x = joystick_axis_threshold(j, joystick[j].x), y = joystick_axis_threshold(j, joystick[j].y);
87
88 if (x != 0)
89 {
90 *angle += atanf(-y / x);
91 *angle += (x < 0) ? -M_PI_2 : M_PI_2;
92 return true;
93 }
94 else if (y != 0)
95 {
96 *angle += y < 0 ? M_PI : 0;
97 return true;
98 }
99
100 return false;
101 }
102
103 /* gives back value 0..joystick_analog_max indicating that one of the assigned
104 * buttons has been pressed or that one of the assigned axes/hats has been moved
105 * in the assigned direction
106 */
107 int check_assigned(SDL_Joystick *joystick_handle, const Joystick_assignment assignment[2])
108 {
109 int result = 0;
110
111 for (int i = 0; i < 2; i++)
112 {
113 int temp = 0;
114
115 switch (assignment[i].type)
116 {
117 case NONE:
118 continue;
119
120 case AXIS:
121 temp = SDL_JoystickGetAxis(joystick_handle, assignment[i].num);
122
123 if (assignment[i].negative_axis)
124 temp = -temp;
125 break;
126
127 case BUTTON:
128 temp = SDL_JoystickGetButton(joystick_handle, assignment[i].num) == 1 ? joystick_analog_max : 0;
129 break;
130
131 case HAT:
132 temp = SDL_JoystickGetHat(joystick_handle, assignment[i].num);
133
134 if (assignment[i].x_axis)
135 temp &= SDL_HAT_LEFT | SDL_HAT_RIGHT;
136 else
137 temp &= SDL_HAT_UP | SDL_HAT_DOWN;
138
139 if (assignment[i].negative_axis)
140 temp &= SDL_HAT_LEFT | SDL_HAT_UP;
141 else
142 temp &= SDL_HAT_RIGHT | SDL_HAT_DOWN;
143
144 temp = temp ? joystick_analog_max : 0;
145 break;
146 }
147
148 if (temp > result)
149 result = temp;
150 }
151
152 return result;
153 }
154
155 // updates joystick state
156 void poll_joystick(int j)
157 {
158 assert(j < joysticks);
159
160 if (joystick[j].handle == NULL)
161 return;
162
163 SDL_JoystickUpdate();
164
165 // indicates that a direction/action was pressed since last poll
166 joystick[j].input_pressed = false;
167
168 // indicates that an direction/action has been held long enough to fake a repeat press
169 bool repeat = joystick[j].joystick_delay < SDL_GetTicks();
170
171 // update direction state
172 for (uint d = 0; d < COUNTOF(joystick[j].direction); d++)
173 {
174 bool old = joystick[j].direction[d];
175
176 joystick[j].analog_direction[d] = check_assigned(joystick[j].handle, joystick[j].assignment[d]);
177 joystick[j].direction[d] = joystick[j].analog_direction[d] > (joystick_analog_max / 2);
178 joydown |= joystick[j].direction[d];
179
180 joystick[j].direction_pressed[d] = joystick[j].direction[d] && (!old || repeat);
181 joystick[j].input_pressed |= joystick[j].direction_pressed[d];
182 }
183
184 joystick[j].x = -joystick[j].analog_direction[3] + joystick[j].analog_direction[1];
185 joystick[j].y = -joystick[j].analog_direction[0] + joystick[j].analog_direction[2];
186
187 // update action state
188 for (uint d = 0; d < COUNTOF(joystick[j].action); d++)
189 {
190 bool old = joystick[j].action[d];
191
192 joystick[j].action[d] = check_assigned(joystick[j].handle, joystick[j].assignment[d + COUNTOF(joystick[j].direction)]) > (joystick_analog_max / 2);
193 joydown |= joystick[j].action[d];
194
195 joystick[j].action_pressed[d] = joystick[j].action[d] && (!old || repeat);
196 joystick[j].input_pressed |= joystick[j].action_pressed[d];
197 }
198
199 joystick[j].confirm = joystick[j].action[0] || joystick[j].action[4];
200 joystick[j].cancel = joystick[j].action[1] || joystick[j].action[5];
201
202 // if new input, reset press-repeat delay
203 if (joystick[j].input_pressed)
204 joystick[j].joystick_delay = SDL_GetTicks() + joystick_repeat_delay;
205 }
206
207 // updates all joystick states
208 void poll_joysticks(void)
209 {
210 joydown = false;
211
212 for (int j = 0; j < joysticks; j++)
213 poll_joystick(j);
214 }
215
216 // sends SDL KEYDOWN and KEYUP events for a key
217 void push_key(SDL_Scancode key)
218 {
219 SDL_Event e;
220
221 memset(&e.key.keysym, 0, sizeof(e.key.keysym));
222
223 e.key.keysym.scancode = key;
224 e.key.state = SDL_RELEASED;
225
226 e.type = SDL_KEYDOWN;
227 SDL_PushEvent(&e);
228
229 e.type = SDL_KEYUP;
230 SDL_PushEvent(&e);
231 }
232
233 // helps us be lazy by pretending joysticks are a keyboard (useful for menus)
234 void push_joysticks_as_keyboard(void)
235 {
236 const SDL_Scancode confirm = SDL_SCANCODE_RETURN, cancel = SDL_SCANCODE_ESCAPE;
237 const SDL_Scancode direction[4] = { SDL_SCANCODE_UP, SDL_SCANCODE_RIGHT, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT };
238
239 poll_joysticks();
240
241 for (int j = 0; j < joysticks; j++)
242 {
243 if (!joystick[j].input_pressed)
244 continue;
245
246 if (joystick[j].confirm)
247 push_key(confirm);
248 if (joystick[j].cancel)
249 push_key(cancel);
250
251 for (uint d = 0; d < COUNTOF(joystick[j].direction_pressed); d++)
252 {
253 if (joystick[j].direction_pressed[d])
254 push_key(direction[d]);
255 }
256 }
257 }
258
259 // initializes SDL joystick system and loads assignments for joysticks found
260 void init_joysticks(void)
261 {
262 if (ignore_joystick)
263 return;
264
265 if (SDL_InitSubSystem(SDL_INIT_JOYSTICK))
266 {
267 fprintf(stderr, "warning: failed to initialize joystick system: %s\n", SDL_GetError());
268 ignore_joystick = true;
269 return;
270 }
271
272 SDL_JoystickEventState(SDL_IGNORE);
273
274 joysticks = SDL_NumJoysticks();
275 joystick = malloc(joysticks * sizeof(*joystick));
276
277 for (int j = 0; j < joysticks; j++)
278 {
279 memset(&joystick[j], 0, sizeof(*joystick));
280
281 joystick[j].handle = SDL_JoystickOpen(j);
282 if (joystick[j].handle != NULL)
283 {
284 printf("joystick detected: %s ", SDL_JoystickName(joystick[j].handle));
285 printf("(%d axes, %d buttons, %d hats)\n",
286 SDL_JoystickNumAxes(joystick[j].handle),
287 SDL_JoystickNumButtons(joystick[j].handle),
288 SDL_JoystickNumHats(joystick[j].handle));
289
290 if (!load_joystick_assignments(&opentyrian_config, j))
291 reset_joystick_assignments(j);
292 }
293 }
294
295 if (joysticks == 0)
296 printf("no joysticks detected\n");
297 }
298
299 // deinitializes SDL joystick system and saves joystick assignments
300 void deinit_joysticks(void)
301 {
302 if (ignore_joystick)
303 return;
304
305 for (int j = 0; j < joysticks; j++)
306 {
307 if (joystick[j].handle != NULL)
308 {
309 save_joystick_assignments(&opentyrian_config, j);
310 SDL_JoystickClose(joystick[j].handle);
311 }
312 }
313
314 free(joystick);
315
316 SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
317 }
318
319 void reset_joystick_assignments(int j)
320 {
321 assert(j < joysticks);
322
323 // defaults: first 2 axes, first hat, first 6 buttons
324 for (uint a = 0; a < COUNTOF(joystick[j].assignment); a++)
325 {
326 // clear assignments
327 for (uint i = 0; i < COUNTOF(joystick[j].assignment[a]); i++)
328 joystick[j].assignment[a][i].type = NONE;
329
330 if (a < 4)
331 {
332 if (SDL_JoystickNumAxes(joystick[j].handle) >= 2)
333 {
334 joystick[j].assignment[a][0].type = AXIS;
335 joystick[j].assignment[a][0].num = (a + 1) % 2;
336 joystick[j].assignment[a][0].negative_axis = (a == 0 || a == 3);
337 }
338
339 if (SDL_JoystickNumHats(joystick[j].handle) >= 1)
340 {
341 joystick[j].assignment[a][1].type = HAT;
342 joystick[j].assignment[a][1].num = 0;
343 joystick[j].assignment[a][1].x_axis = (a == 1 || a == 3);
344 joystick[j].assignment[a][1].negative_axis = (a == 0 || a == 3);
345 }
346 }
347 else
348 {
349 if (a - 4 < (unsigned)SDL_JoystickNumButtons(joystick[j].handle))
350 {
351 joystick[j].assignment[a][0].type = BUTTON;
352 joystick[j].assignment[a][0].num = a - 4;
353 }
354 }
355 }
356
357 joystick[j].analog = false;
358 joystick[j].sensitivity = 5;
359 joystick[j].threshold = 5;
360 }
361
362 static const char* const assignment_names[] =
363 {
364 "up",
365 "right",
366 "down",
367 "left",
368 "fire",
369 "change fire",
370 "left sidekick",
371 "right sidekick",
372 "menu",
373 "pause",
374 };
375
376 bool load_joystick_assignments(Config *config, int j)
377 {
378 ConfigSection *section = config_find_section(config, "joystick", SDL_JoystickName(joystick[j].handle));
379 if (section == NULL)
380 return false;
381
382 if (!config_get_bool_option(section, "analog", &joystick[j].analog))
383 joystick[j].analog = false;
384
385 joystick[j].sensitivity = config_get_or_set_int_option(section, "sensitivity", 5);
386
387 joystick[j].threshold = config_get_or_set_int_option(section, "threshold", 5);
388
389 for (size_t a = 0; a < COUNTOF(assignment_names); ++a)
390 {
391 for (unsigned int i = 0; i < COUNTOF(joystick[j].assignment[a]); ++i)
392 joystick[j].assignment[a][i].type = NONE;
393
394 ConfigOption *option = config_get_option(section, assignment_names[a]);
395 if (option == NULL)
396 continue;
397
398 foreach_option_i_value(i, value, option)
399 {
400 if (i >= COUNTOF(joystick[j].assignment[a]))
401 break;
402
403 code_to_assignment(&joystick[j].assignment[a][i], value);
404 }
405 }
406
407 return true;
408 }
409
410 bool save_joystick_assignments(Config *config, int j)
411 {
412 ConfigSection *section = config_find_or_add_section(config, "joystick", SDL_JoystickName(joystick[j].handle));
413 if (section == NULL)
414 exit(EXIT_FAILURE); // out of memory
415
416 config_set_bool_option(section, "analog", joystick[j].analog, NO_YES);
417
418 config_set_int_option(section, "sensitivity", joystick[j].sensitivity);
419
420 config_set_int_option(section, "threshold", joystick[j].threshold);
421
422 for (size_t a = 0; a < COUNTOF(assignment_names); ++a)
423 {
424 ConfigOption *option = config_set_option(section, assignment_names[a], NULL);
425 if (option == NULL)
426 exit(EXIT_FAILURE); // out of memory
427
428 option = config_set_value(option, NULL);
429 if (option == NULL)
430 exit(EXIT_FAILURE); // out of memory
431
432 for (size_t i = 0; i < COUNTOF(joystick[j].assignment[a]); ++i)
433 {
434 if (joystick[j].assignment[a][i].type == NONE)
435 continue;
436
437 option = config_add_value(option, assignment_to_code(&joystick[j].assignment[a][i]));
438 if (option == NULL)
439 exit(EXIT_FAILURE); // out of memory
440 }
441 }
442
443 return true;
444 }
445
446 // fills buffer with comma separated list of assigned joystick functions
447 void joystick_assignments_to_string(char *buffer, size_t buffer_len, const Joystick_assignment *assignments)
448 {
449 strncpy(buffer, "", buffer_len);
450
451 bool comma = false;
452 for (uint i = 0; i < COUNTOF(*joystick->assignment); ++i)
453 {
454 if (assignments[i].type == NONE)
455 continue;
456
457 size_t len = snprintf(buffer, buffer_len, "%s%s",
458 comma ? ", " : "",
459 assignment_to_code(&assignments[i]));
460 buffer += len;
461 buffer_len -= len;
462
463 comma = true;
464 }
465 }
466
467 // reverse of assignment_to_code()
468 void code_to_assignment(Joystick_assignment *assignment, const char *buffer)
469 {
470 memset(assignment, 0, sizeof(*assignment));
471
472 char axis = 0, direction = 0;
473
474 if (sscanf(buffer, " AX %d%c", &assignment->num, &direction) == 2)
475 assignment->type = AXIS;
476 else if (sscanf(buffer, " BTN %d", &assignment->num) == 1)
477 assignment->type = BUTTON;
478 else if (sscanf(buffer, " H %d%c%c", &assignment->num, &axis, &direction) == 3)
479 assignment->type = HAT;
480
481 if (assignment->num == 0)
482 assignment->type = NONE;
483 else
484 --assignment->num;
485
486 assignment->x_axis = (toupper(axis) == 'X');
487 assignment->negative_axis = (toupper(direction) == '-');
488 }
489
490 /* gives the short (6 or less characters) identifier for a joystick assignment
491 *
492 * two of these per direction/action is all that can fit on the joystick config screen,
493 * assuming two digits for the axis/button/hat number
494 */
495 const char *assignment_to_code(const Joystick_assignment *assignment)
496 {
497 static char name[7];
498
499 switch (assignment->type)
500 {
501 case NONE:
502 strcpy(name, "");
503 break;
504
505 case AXIS:
506 snprintf(name, sizeof(name), "AX %d%c",
507 assignment->num + 1,
508 assignment->negative_axis ? '-' : '+');
509 break;
510
511 case BUTTON:
512 snprintf(name, sizeof(name), "BTN %d",
513 assignment->num + 1);
514 break;
515
516 case HAT:
517 snprintf(name, sizeof(name), "H %d%c%c",
518 assignment->num + 1,
519 assignment->x_axis ? 'X' : 'Y',
520 assignment->negative_axis ? '-' : '+');
521 break;
522 }
523
524 return name;
525 }
526
527 // captures joystick input for configuring assignments
528 // returns false if non-joystick input was detected
529 // TODO: input from joystick other than the one being configured probably should not be ignored
530 bool detect_joystick_assignment(int j, Joystick_assignment *assignment)
531 {
532 // get initial joystick state to compare against to see if anything was pressed
533
534 const int axes = SDL_JoystickNumAxes(joystick[j].handle);
535 Sint16 *axis = malloc(axes * sizeof(*axis));
536 for (int i = 0; i < axes; i++)
537 axis[i] = SDL_JoystickGetAxis(joystick[j].handle, i);
538
539 const int buttons = SDL_JoystickNumButtons(joystick[j].handle);
540 Uint8 *button = malloc(buttons * sizeof(*button));
541 for (int i = 0; i < buttons; i++)
542 button[i] = SDL_JoystickGetButton(joystick[j].handle, i);
543
544 const int hats = SDL_JoystickNumHats(joystick[j].handle);
545 Uint8 *hat = malloc(hats * sizeof(*hat));
546 for (int i = 0; i < hats; i++)
547 hat[i] = SDL_JoystickGetHat(joystick[j].handle, i);
548
549 bool detected = false;
550
551 do
552 {
553 setDelay(1);
554
555 SDL_JoystickUpdate();
556
557 for (int i = 0; i < axes; ++i)
558 {
559 Sint16 temp = SDL_JoystickGetAxis(joystick[j].handle, i);
560
561 if (abs(temp - axis[i]) > joystick_analog_max * 2 / 3)
562 {
563 assignment->type = AXIS;
564 assignment->num = i;
565 assignment->negative_axis = temp < axis[i];
566 detected = true;
567 break;
568 }
569 }
570
571 for (int i = 0; i < buttons; ++i)
572 {
573 Uint8 new_button = SDL_JoystickGetButton(joystick[j].handle, i),
574 changed = button[i] ^ new_button;
575
576 if (!changed)
577 continue;
578
579 if (new_button == 0) // button was released
580 {
581 button[i] = new_button;
582 }
583 else // button was pressed
584 {
585 assignment->type = BUTTON;
586 assignment->num = i;
587 detected = true;
588 break;
589 }
590 }
591
592 for (int i = 0; i < hats; ++i)
593 {
594 Uint8 new_hat = SDL_JoystickGetHat(joystick[j].handle, i),
595 changed = hat[i] ^ new_hat;
596
597 if (!changed)
598 continue;
599
600 if ((new_hat & changed) == SDL_HAT_CENTERED) // hat was centered
601 {
602 hat[i] = new_hat;
603 }
604 else
605 {
606 assignment->type = HAT;
607 assignment->num = i;
608 assignment->x_axis = changed & (SDL_HAT_LEFT | SDL_HAT_RIGHT);
609 assignment->negative_axis = changed & (SDL_HAT_LEFT | SDL_HAT_UP);
610 detected = true;
611 }
612 }
613
614 service_SDL_events(true);
615 JE_showVGA();
616
617 wait_delay();
618 } while (!detected && !newkey && !newmouse);
619
620 free(axis);
621 free(button);
622 free(hat);
623
624 return detected;
625 }
626
627 // compares relevant parts of joystick assignments for equality
628 bool joystick_assignment_cmp(const Joystick_assignment *a, const Joystick_assignment *b)
629 {
630 if (a->type == b->type)
631 {
632 switch (a->type)
633 {
634 case NONE:
635 return true;
636 case AXIS:
637 return (a->num == b->num) &&
638 (a->negative_axis == b->negative_axis);
639 case BUTTON:
640 return (a->num == b->num);
641 case HAT:
642 return (a->num == b->num) &&
643 (a->x_axis == b->x_axis) &&
644 (a->negative_axis == b->negative_axis);
645 }
646 }
647
648 return false;
649 }