comparison render_sdl.c @ 1862:e07fc3d473b2

Basic UI navigation with controller
author Michael Pavone <pavone@retrodev.com>
date Wed, 08 May 2019 23:44:40 -0700
parents fc05f49075c2
children 4c322abd9fa5
comparison
equal deleted inserted replaced
1861:fc05f49075c2 1862:e07fc3d473b2
919 return guid_string; 919 return guid_string;
920 } 920 }
921 921
922 SDL_GameController *render_get_controller(int index) 922 SDL_GameController *render_get_controller(int index)
923 { 923 {
924 if (index >= MAX_JOYSTICKS) { 924 if (index >= MAX_JOYSTICKS || !joysticks[index]) {
925 return NULL; 925 return NULL;
926 } 926 }
927 return SDL_GameControllerOpen(joystick_sdl_index[index]); 927 return SDL_GameControllerOpen(joystick_sdl_index[index]);
928 }
929
930 static uint8_t gc_events_enabled;
931 static SDL_GameController *controllers[MAX_JOYSTICKS];
932 void render_enable_gamepad_events(uint8_t enabled)
933 {
934 if (enabled != gc_events_enabled) {
935 gc_events_enabled = enabled;
936 for (int i = 0; i < MAX_JOYSTICKS; i++) {
937 if (enabled) {
938 controllers[i] = render_get_controller(i);
939 } else if (controllers[i]) {
940 SDL_GameControllerClose(controllers[i]);
941 controllers[i] = NULL;
942 }
943 }
944 }
928 } 945 }
929 946
930 static uint32_t overscan_top[NUM_VID_STD] = {2, 21}; 947 static uint32_t overscan_top[NUM_VID_STD] = {2, 21};
931 static uint32_t overscan_bot[NUM_VID_STD] = {1, 17}; 948 static uint32_t overscan_bot[NUM_VID_STD] = {1, 17};
932 static uint32_t overscan_left[NUM_VID_STD] = {13, 13}; 949 static uint32_t overscan_left[NUM_VID_STD] = {13, 13};
987 int index = lowest_unused_joystick_index(); 1004 int index = lowest_unused_joystick_index();
988 if (index >= 0) { 1005 if (index >= 0) {
989 SDL_Joystick * joy = joysticks[index] = SDL_JoystickOpen(event->jdevice.which); 1006 SDL_Joystick * joy = joysticks[index] = SDL_JoystickOpen(event->jdevice.which);
990 joystick_sdl_index[index] = event->jdevice.which; 1007 joystick_sdl_index[index] = event->jdevice.which;
991 joystick_index_locked[index] = 0; 1008 joystick_index_locked[index] = 0;
1009 if (gc_events_enabled) {
1010 controllers[index] = SDL_GameControllerOpen(event->jdevice.which);
1011 }
992 if (joy) { 1012 if (joy) {
993 debug_message("Joystick %d added: %s\n", index, SDL_JoystickName(joy)); 1013 debug_message("Joystick %d added: %s\n", index, SDL_JoystickName(joy));
994 debug_message("\tNum Axes: %d\n\tNum Buttons: %d\n\tNum Hats: %d\n", SDL_JoystickNumAxes(joy), SDL_JoystickNumButtons(joy), SDL_JoystickNumHats(joy)); 1014 debug_message("\tNum Axes: %d\n\tNum Buttons: %d\n\tNum Hats: %d\n", SDL_JoystickNumAxes(joy), SDL_JoystickNumButtons(joy), SDL_JoystickNumHats(joy));
995 handle_joy_added(index); 1015 handle_joy_added(index);
996 } 1016 }
1000 case SDL_JOYDEVICEREMOVED: { 1020 case SDL_JOYDEVICEREMOVED: {
1001 int index = find_joystick_index(event->jdevice.which); 1021 int index = find_joystick_index(event->jdevice.which);
1002 if (index >= 0) { 1022 if (index >= 0) {
1003 SDL_JoystickClose(joysticks[index]); 1023 SDL_JoystickClose(joysticks[index]);
1004 joysticks[index] = NULL; 1024 joysticks[index] = NULL;
1025 if (controllers[index]) {
1026 SDL_GameControllerClose(controllers[index]);
1027 controllers[index] = NULL;
1028 }
1005 debug_message("Joystick %d removed\n", index); 1029 debug_message("Joystick %d removed\n", index);
1006 } else { 1030 } else {
1007 debug_message("Failed to find removed joystick with instance ID: %d\n", index); 1031 debug_message("Failed to find removed joystick with instance ID: %d\n", index);
1008 } 1032 }
1009 break; 1033 break;