static void menuKeybinds_genList( unsigned int wid ) { int i, j; char **str; SDLKey key; KeybindType type; SDLMod mod; int w, h; int lw, lh; /* Get dimensions. */ menuKeybinds_getDim( wid, &w, &h, &lw, &lh, NULL, NULL ); /* Create the list. */ for (i=0; strcmp(keybindNames[i],"end"); i++); str = malloc(sizeof(char*) * i); for (j=0; j < i; j++) { str[j] = malloc(sizeof(char) * 64); key = input_getKeybind( keybindNames[j], &type, &mod ); switch (type) { case KEYBIND_KEYBOARD: /* SDL_GetKeyName returns lowercase which is ugly. */ if (nstd_isalpha(key)) snprintf(str[j], 64, "%s <%c>", keybindNames[j], nstd_toupper(key) ); else snprintf(str[j], 64, "%s <%s>", keybindNames[j], SDL_GetKeyName(key) ); break; case KEYBIND_JAXISPOS: snprintf(str[j], 64, "%s <ja+%d>", keybindNames[j], key); break; case KEYBIND_JAXISNEG: snprintf(str[j], 64, "%s <ja-%d>", keybindNames[j], key); break; case KEYBIND_JBUTTON: snprintf(str[j], 64, "%s <jb%d>", keybindNames[j], key); break; default: snprintf(str[j], 64, "%s", keybindNames[j]); break; } } window_addList( wid, 20, -40, lw, lh, "lstKeybinds", str, i, 0, menuKeybinds_update ); /* Update the list. */ menuKeybinds_update( wid, NULL ); }
/** * @brief Unsets the key. */ static void opt_unsetKey( unsigned int wid, char *str ) { (void) str; unsigned int parent; /* Unsets the keybind. */ input_setKeybind( opt_selectedKeybind, KEYBIND_NULL, 0, 0 ); /* Close window. */ window_close( wid, NULL ); /* Update parent window. */ parent = window_get("Keybindings"); window_destroyWidget( parent, "lstKeybinds" ); menuKeybinds_genList( parent ); menuKeybinds_update( parent, NULL ); }
/** * @brief Tries to set the key from an event. */ static int opt_setKeyEvent( unsigned int wid, SDL_Event *event ) { unsigned int parent; KeybindType type; int key; SDLMod mod; const char *str; /* See how to handle it. */ switch (event->type) { case SDL_KEYDOWN: key = event->key.keysym.sym; /* If control key make player hit twice. */ if (((key == SDLK_NUMLOCK) || (key == SDLK_CAPSLOCK) || (key == SDLK_SCROLLOCK) || (key == SDLK_RSHIFT) || (key == SDLK_LSHIFT) || (key == SDLK_RCTRL) || (key == SDLK_LCTRL) || (key == SDLK_RALT) || (key == SDLK_LALT) || (key == SDLK_RMETA) || (key == SDLK_LMETA) || (key == SDLK_LSUPER) || (key == SDLK_RSUPER)) && (opt_lastKeyPress != key)) { opt_lastKeyPress = key; return 0; } type = KEYBIND_KEYBOARD; if (window_checkboxState( wid, "chkAny" )) mod = KMOD_ALL; else mod = event->key.keysym.mod & ~(KMOD_CAPS | KMOD_NUM | KMOD_MODE); /* Set key. */ opt_lastKeyPress = key; break; case SDL_JOYAXISMOTION: if (event->jaxis.value > 0) type = KEYBIND_JAXISPOS; else if (event->jaxis.value < 0) type = KEYBIND_JAXISNEG; else return 0; /* Not handled. */ key = event->jaxis.axis; mod = KMOD_ALL; break; case SDL_JOYBUTTONDOWN: type = KEYBIND_JBUTTON; key = event->jbutton.button; mod = KMOD_ALL; break; /* Not handled. */ default: return 0; } /* Warn if already bound. */ str = input_keyAlreadyBound( type, key, mod ); if ((str != NULL) && strcmp(str, opt_selectedKeybind)) dialogue_alert( "Key '%s' overlaps with key '%s' that was just set. " "You may want to correct this.", str, opt_selectedKeybind ); /* Set keybinding. */ input_setKeybind( opt_selectedKeybind, type, key, mod ); /* Close window. */ window_close( wid, NULL ); /* Update parent window. */ parent = window_get("Keybindings"); window_destroyWidget( parent, "lstKeybinds" ); menuKeybinds_genList( parent ); menuKeybinds_update( parent, NULL ); return 0; }
/** * @brief Generates the keybindings list. */ static void menuKeybinds_genList( unsigned int wid ) { int i, j, l, p; char **str, mod_text[64]; SDLKey key; KeybindType type; SDLMod mod; int w, h; int lw, lh; /* Get dimensions. */ menuKeybinds_getDim( wid, &w, &h, &lw, &lh, NULL, NULL ); /* Create the list. */ for (i=0; strcmp(keybindNames[i],"end"); i++); str = malloc(sizeof(char*) * i); for (j=0; j < i; j++) { l = 64; str[j] = malloc(sizeof(char) * l); key = input_getKeybind( keybindNames[j], &type, &mod ); switch (type) { case KEYBIND_KEYBOARD: /* Generate mod text. */ if (mod == NMOD_ALL) snprintf( mod_text, sizeof(mod_text), "any+" ); else { p = 0; mod_text[0] = '\0'; if (mod & NMOD_SHIFT) p += snprintf( &mod_text[p], sizeof(mod_text)-p, "shift+" ); if (mod & NMOD_CTRL) p += snprintf( &mod_text[p], sizeof(mod_text)-p, "ctrl+" ); if (mod & NMOD_ALT) p += snprintf( &mod_text[p], sizeof(mod_text)-p, "alt+" ); if (mod & NMOD_META) p += snprintf( &mod_text[p], sizeof(mod_text)-p, "meta+" ); } /* SDL_GetKeyName returns lowercase which is ugly. */ if (nstd_isalpha(key)) snprintf(str[j], l, "%s <%s%c>", keybindNames[j], mod_text, nstd_toupper(key) ); else snprintf(str[j], l, "%s <%s%s>", keybindNames[j], mod_text, SDL_GetKeyName(key) ); break; case KEYBIND_JAXISPOS: snprintf(str[j], l, "%s <ja+%d>", keybindNames[j], key); break; case KEYBIND_JAXISNEG: snprintf(str[j], l, "%s <ja-%d>", keybindNames[j], key); break; case KEYBIND_JBUTTON: snprintf(str[j], l, "%s <jb%d>", keybindNames[j], key); break; default: snprintf(str[j], l, "%s", keybindNames[j]); break; } } window_addList( wid, 20, -40, lw, lh, "lstKeybinds", str, i, 0, menuKeybinds_update ); /* Update the list. */ menuKeybinds_update( wid, NULL ); }