comparison io.c @ 1187:6a4503fad67e

Initial support for using SDL2 game controller mapping functionality
author Michael Pavone <pavone@retrodev.com>
date Sun, 22 Jan 2017 16:23:59 -0800
parents 3e24de8d8073
children 1ad0ec7e3939
comparison
equal deleted inserted replaced
1186:110251ea369e 1187:6a4503fad67e
918 } 918 }
919 tern_node *buttons = tern_get_node(tern_find_path(mousedef, "buttons\0\0")); 919 tern_node *buttons = tern_get_node(tern_find_path(mousedef, "buttons\0\0"));
920 if (buttons) { 920 if (buttons) {
921 pmb_state state = {padbuttons, mousebuttons, mouseidx}; 921 pmb_state state = {padbuttons, mousebuttons, mouseidx};
922 tern_foreach(buttons, process_mouse_button, &state); 922 tern_foreach(buttons, process_mouse_button, &state);
923 }
924 }
925
926 typedef struct {
927 int padnum;
928 tern_node *padbuttons;
929 tern_node *mousebuttons;
930 } pad_button_state;
931
932 void process_pad_button(char *key, tern_val val, void *data)
933 {
934 pad_button_state *state = data;
935 int hostpadnum = state->padnum;
936 int ui_func, padnum, button;
937 int bindtype = parse_binding_target(val.ptrval, state->padbuttons, state->mousebuttons, &ui_func, &padnum, &button);
938 char *end;
939 long hostbutton = strtol(key, &end, 10);
940 if (*end) {
941 //key is not a valid base 10 integer
942 hostbutton = render_translate_input_name(hostpadnum, key);
943 if (hostbutton < 0) {
944 if (hostbutton == RENDER_INVALID_NAME) {
945 warning("%s is not a valid gamepad input name\n", key);
946 } else if (hostbutton == RENDER_NOT_MAPPED) {
947 warning("No mapping exists for input %s on gamepad %d\n", key, hostpadnum);
948 }
949 return;
950 }
951 if (hostbutton & RENDER_DPAD_BIT) {
952 if (bindtype == BIND_GAMEPAD1) {
953 bind_dpad_gamepad(hostpadnum, render_dpad_part(hostbutton), render_direction_part(hostbutton), padnum, button);
954 } else {
955 bind_dpad_ui(hostpadnum, render_dpad_part(hostbutton), render_direction_part(hostbutton), ui_func, button);
956 }
957 } else if (hostbutton & RENDER_AXIS_BIT) {
958 //TODO: support analog axes
959 return;
960 }
961 }
962 if (bindtype == BIND_GAMEPAD1) {
963 bind_button_gamepad(hostpadnum, hostbutton, padnum, button);
964 } else if (bindtype == BIND_UI) {
965 bind_button_ui(hostpadnum, hostbutton, ui_func, button);
923 } 966 }
924 } 967 }
925 968
926 void set_keybindings(sega_io *io) 969 void set_keybindings(sega_io *io)
927 { 970 {
986 mousebuttons = tern_insert_int(mousebuttons, ".middle", MOUSE_MIDDLE); 1029 mousebuttons = tern_insert_int(mousebuttons, ".middle", MOUSE_MIDDLE);
987 mousebuttons = tern_insert_int(mousebuttons, ".right", MOUSE_RIGHT); 1030 mousebuttons = tern_insert_int(mousebuttons, ".right", MOUSE_RIGHT);
988 mousebuttons = tern_insert_int(mousebuttons, ".start", MOUSE_START); 1031 mousebuttons = tern_insert_int(mousebuttons, ".start", MOUSE_START);
989 mousebuttons = tern_insert_int(mousebuttons, ".motion", PSEUDO_BUTTON_MOTION); 1032 mousebuttons = tern_insert_int(mousebuttons, ".motion", PSEUDO_BUTTON_MOTION);
990 1033
1034 //pump event loop, so initial joystick insertion events are processed
1035 process_events();
1036
991 tern_node * keys = tern_get_node(tern_find_path(config, "bindings\0keys\0")); 1037 tern_node * keys = tern_get_node(tern_find_path(config, "bindings\0keys\0"));
992 process_keys(keys, special, padbuttons, mousebuttons, NULL); 1038 process_keys(keys, special, padbuttons, mousebuttons, NULL);
993 char numstr[] = "00"; 1039 char numstr[] = "00";
994 tern_node * pads = tern_get_node(tern_find_path(config, "bindings\0pads\0")); 1040 tern_node * pads = tern_get_node(tern_find_path(config, "bindings\0pads\0"));
995 if (pads) { 1041 if (pads) {
1028 } 1074 }
1029 } 1075 }
1030 } 1076 }
1031 tern_node *button_node = tern_find_ptr(pad, "buttons"); 1077 tern_node *button_node = tern_find_ptr(pad, "buttons");
1032 if (button_node) { 1078 if (button_node) {
1033 for (int but = 0; but < 30; but++) 1079 pad_button_state state = {
1034 { 1080 .padnum = i,
1035 if (but < 10) { 1081 .padbuttons = padbuttons,
1036 numstr[0] = but + '0'; 1082 .mousebuttons = mousebuttons
1037 numstr[1] = 0; 1083 };
1038 } else { 1084 tern_foreach(button_node, process_pad_button, &state);
1039 numstr[0] = but/10 + '0'; 1085
1040 numstr[1] = but%10 + '0';
1041 }
1042 char * target = tern_find_ptr(button_node, numstr);
1043 if (target) {
1044 int ui_func, padnum, button;
1045 int bindtype = parse_binding_target(target, padbuttons, mousebuttons, &ui_func, &padnum, &button);
1046 if (bindtype == BIND_GAMEPAD1) {
1047 bind_button_gamepad(i, but, padnum, button);
1048 } else if (bindtype == BIND_UI) {
1049 bind_button_ui(i, but, ui_func, button);
1050 }
1051 }
1052 }
1053 } 1086 }
1054 } 1087 }
1055 } 1088 }
1056 } 1089 }
1057 memset(mice, 0, sizeof(mice)); 1090 memset(mice, 0, sizeof(mice));