comparison render_sdl.c @ 937:9364dad5561a

Added reasonable handling of joystick hotplug
author Michael Pavone <pavone@retrodev.com>
date Tue, 23 Feb 2016 21:17:56 -0800
parents 9e882eca717e
children 534f522a1162
comparison
equal deleted inserted replaced
936:f1a8124ad881 937:9364dad5561a
88 SDL_CondSignal(audio_ready); 88 SDL_CondSignal(audio_ready);
89 SDL_UnlockMutex(audio_mutex); 89 SDL_UnlockMutex(audio_mutex);
90 SDL_CloseAudio(); 90 SDL_CloseAudio();
91 } 91 }
92 92
93 SDL_Joystick * joysticks[MAX_JOYSTICKS]; 93 static SDL_Joystick * joysticks[MAX_JOYSTICKS];
94 int num_joysticks;
95
96 int render_num_joysticks()
97 {
98 return num_joysticks;
99 }
100 94
101 int render_width() 95 int render_width()
102 { 96 {
103 return main_width; 97 return main_width;
104 } 98 }
387 } 381 }
388 buffer_samples = actual.samples; 382 buffer_samples = actual.samples;
389 sample_rate = actual.freq; 383 sample_rate = actual.freq;
390 printf("Initialized audio at frequency %d with a %d sample buffer\n", actual.freq, actual.samples); 384 printf("Initialized audio at frequency %d with a %d sample buffer\n", actual.freq, actual.samples);
391 SDL_PauseAudio(0); 385 SDL_PauseAudio(0);
392 num_joysticks = SDL_NumJoysticks();
393 if (num_joysticks > MAX_JOYSTICKS) {
394 num_joysticks = MAX_JOYSTICKS;
395 }
396 for (int i = 0; i < num_joysticks; i++) {
397 SDL_Joystick * joy = joysticks[i] = SDL_JoystickOpen(i);
398 printf("Joystick %d: %s\n", i, SDL_JoystickName(joy));
399 if (joy) {
400 printf("\tNum Axes: %d\n\tNum Buttons: %d\n\tNum Hats: %d\n", SDL_JoystickNumAxes(joy), SDL_JoystickNumButtons(joy), SDL_JoystickNumHats(joy));
401 }
402 }
403 SDL_JoystickEventState(SDL_ENABLE); 386 SDL_JoystickEventState(SDL_ENABLE);
404 387
405 atexit(render_quit); 388 atexit(render_quit);
406 } 389 }
407 390
487 #endif 470 #endif
488 471
489 if (context->regs[REG_MODE_4] & BIT_INTERLACE) { 472 if (context->regs[REG_MODE_4] & BIT_INTERLACE) {
490 context->framebuf = context->framebuf == context->oddbuf ? context->evenbuf : context->oddbuf; 473 context->framebuf = context->framebuf == context->oddbuf ? context->evenbuf : context->oddbuf;
491 } 474 }
492 }
493
494 int render_joystick_num_buttons(int joystick)
495 {
496 if (joystick >= num_joysticks) {
497 return 0;
498 }
499 return SDL_JoystickNumButtons(joysticks[joystick]);
500 }
501
502 int render_joystick_num_hats(int joystick)
503 {
504 if (joystick >= num_joysticks) {
505 return 0;
506 }
507 return SDL_JoystickNumHats(joysticks[joystick]);
508 } 475 }
509 476
510 void render_wait_quit(vdp_context * context) 477 void render_wait_quit(vdp_context * context)
511 { 478 {
512 SDL_Event event; 479 SDL_Event event;
544 if (pal < 4) { 511 if (pal < 4) {
545 debug_pal = pal; 512 debug_pal = pal;
546 } 513 }
547 } 514 }
548 515
516 int find_joystick_index(SDL_JoystickID instanceID)
517 {
518 for (int i = 0; i < MAX_JOYSTICKS; i++) {
519 if (joysticks[i] && SDL_JoystickInstanceID(joysticks[i]) == instanceID) {
520 return i;
521 }
522 }
523 return -1;
524 }
525
526 int lowest_unused_joystick_index()
527 {
528 for (int i = 0; i < MAX_JOYSTICKS; i++) {
529 if (!joysticks[i]) {
530 return i;
531 }
532 }
533 return -1;
534 }
535
549 int32_t handle_event(SDL_Event *event) 536 int32_t handle_event(SDL_Event *event)
550 { 537 {
551 switch (event->type) { 538 switch (event->type) {
552 case SDL_KEYDOWN: 539 case SDL_KEYDOWN:
553 handle_keydown(event->key.keysym.sym); 540 handle_keydown(event->key.keysym.sym);
554 break; 541 break;
555 case SDL_KEYUP: 542 case SDL_KEYUP:
556 handle_keyup(event->key.keysym.sym); 543 handle_keyup(event->key.keysym.sym);
557 break; 544 break;
558 case SDL_JOYBUTTONDOWN: 545 case SDL_JOYBUTTONDOWN:
559 handle_joydown(event->jbutton.which, event->jbutton.button); 546 handle_joydown(find_joystick_index(event->jbutton.which), event->jbutton.button);
560 break; 547 break;
561 case SDL_JOYBUTTONUP: 548 case SDL_JOYBUTTONUP:
562 handle_joyup(event->jbutton.which, event->jbutton.button); 549 handle_joyup(find_joystick_index(event->jbutton.which), event->jbutton.button);
563 break; 550 break;
564 case SDL_JOYHATMOTION: 551 case SDL_JOYHATMOTION:
565 handle_joy_dpad(event->jbutton.which, event->jhat.hat, event->jhat.value); 552 handle_joy_dpad(find_joystick_index(event->jbutton.which), event->jhat.hat, event->jhat.value);
566 break; 553 break;
554 case SDL_JOYDEVICEADDED:
555 if (event->jdevice.which < MAX_JOYSTICKS) {
556 int index = lowest_unused_joystick_index();
557 if (index >= 0) {
558 SDL_Joystick * joy = joysticks[index] = SDL_JoystickOpen(event->jdevice.which);
559 if (joy) {
560 printf("Joystick %d added: %s\n", index, SDL_JoystickName(joy));
561 printf("\tNum Axes: %d\n\tNum Buttons: %d\n\tNum Hats: %d\n", SDL_JoystickNumAxes(joy), SDL_JoystickNumButtons(joy), SDL_JoystickNumHats(joy));
562 }
563 }
564 }
565 break;
566 case SDL_JOYDEVICEREMOVED: {
567 int index = find_joystick_index(event->jdevice.which);
568 if (index >= 0) {
569 SDL_JoystickClose(joysticks[index]);
570 joysticks[index] = NULL;
571 printf("Joystick %d removed\n", index);
572 } else {
573 printf("Failed to find removed joystick with instance ID: %d\n", index);
574 }
575 break;
576 }
567 case SDL_MOUSEMOTION: 577 case SDL_MOUSEMOTION:
568 handle_mouse_moved(event->motion.which, event->motion.x, event->motion.y, event->motion.xrel, event->motion.yrel); 578 handle_mouse_moved(event->motion.which, event->motion.x, event->motion.y, event->motion.xrel, event->motion.yrel);
569 break; 579 break;
570 case SDL_MOUSEBUTTONDOWN: 580 case SDL_MOUSEBUTTONDOWN:
571 handle_mousedown(event->button.which, event->button.button); 581 handle_mousedown(event->button.which, event->button.button);