Remapper::Key mappingStringToKey( QString keyString, bool *ok ) { Remapper::Type keyType; int keyValue; bool okay = false; // Try to parse the key as a button keyValue = SDL_GameControllerGetButtonFromString( keyString.toLocal8Bit().constData() ); okay = true; keyType = Remapper::BUTTON; // If it's not a button then it's an axis if( keyValue == SDL_CONTROLLER_BUTTON_INVALID ) { keyValue = SDL_GameControllerGetAxisFromString( keyString.toLocal8Bit().constData() ); keyType = Remapper::AXIS; } // If it's not that then what the heck is it? if( keyValue == SDL_CONTROLLER_AXIS_INVALID ) { okay = false; } if( ok ) { *ok = okay; } return Remapper::Key( keyType, keyValue ); }
/* * given a controller button name and a joystick name update our mapping structure with it */ void SDL_PrivateGameControllerParseButton(const char *szGameButton, const char *szJoystickButton, struct _SDL_ControllerMapping *pMapping) { int iSDLButton = 0; SDL_GameControllerButton button; SDL_GameControllerAxis axis; button = SDL_GameControllerGetButtonFromString(szGameButton); axis = SDL_GameControllerGetAxisFromString(szGameButton); iSDLButton = SDL_atoi(&szJoystickButton[1]); if (szJoystickButton[0] == 'a') { if (iSDLButton >= k_nMaxReverseEntries) { SDL_SetError("Axis index too large: %d", iSDLButton); return; } if (axis != SDL_CONTROLLER_AXIS_INVALID) { pMapping->axes[ axis ] = iSDLButton; pMapping->raxes[ iSDLButton ] = axis; } else if (button != SDL_CONTROLLER_BUTTON_INVALID) { pMapping->axesasbutton[ button ] = iSDLButton; pMapping->raxesasbutton[ iSDLButton ] = button; } else { SDL_assert(!"How did we get here?"); } } else if (szJoystickButton[0] == 'b') { if (iSDLButton >= k_nMaxReverseEntries) { SDL_SetError("Button index too large: %d", iSDLButton); return; } if (button != SDL_CONTROLLER_BUTTON_INVALID) { pMapping->buttons[ button ] = iSDLButton; pMapping->rbuttons[ iSDLButton ] = button; } else if (axis != SDL_CONTROLLER_AXIS_INVALID) { pMapping->buttonasaxis[ axis ] = iSDLButton; pMapping->rbuttonasaxis[ iSDLButton ] = axis; } else { SDL_assert(!"How did we get here?"); } } else if (szJoystickButton[0] == 'h') { int hat = SDL_atoi(&szJoystickButton[1]); int mask = SDL_atoi(&szJoystickButton[3]); if (hat >= 4) { SDL_SetError("Hat index too large: %d", iSDLButton); } if (button != SDL_CONTROLLER_BUTTON_INVALID) { int ridx; pMapping->hatasbutton[ button ].hat = hat; pMapping->hatasbutton[ button ].mask = mask; ridx = (hat << 4) | mask; pMapping->rhatasbutton[ ridx ] = button; } else if (axis != SDL_CONTROLLER_AXIS_INVALID) { SDL_assert(!"Support hat as axis"); } else { SDL_assert(!"How did we get here?"); } } }
int mappingStringToGameControllerID( QString gameControllerString ) { int result; result = SDL_GameControllerGetButtonFromString( gameControllerString.toUtf8().constData() ); if( result != SDL_CONTROLLER_BUTTON_INVALID ) { return result; } result = SDL_GameControllerGetAxisFromString( gameControllerString.toUtf8().constData() ); // Will return either a valid axis or SDL_CONTROLLER_AXIS_INVALID return result; }
PIN_GameControllerKeyInfo PIN_GameControllerManager::GenerateKeyInfo(PIN_String strKey) { printf("\t\tGenerateKeyInfo '%s'...\n",strKey); SDL_GameControllerButton btn = SDL_GameControllerGetButtonFromString(strKey); if(btn != SDL_CONTROLLER_BUTTON_INVALID) { printf("\t\t\tType BUTTON\n"); return CreateKeyInfo(PIN_GKT_BUTTON,btn,SDL_CONTROLLER_AXIS_INVALID); } printf("\t\t\tNot a button. Checking if it's an axis...\n"); boost::cmatch what; if(boost::regex_match(strKey, what, _regexAxisString)) { printf("\t\t\tType AXIS\n"); std::string strfAxis = what["func"].str(); PIN_String fAxis = strfAxis.c_str(); std::string strAxis = what["axis"].str(); PIN_String axis = strAxis.c_str(); SDL_GameControllerAxis ax = SDL_GameControllerGetAxisFromString(axis); PIN_GameControllerAxisDirection direction = PIN_GAD_BOTH; printf("\t\t\tAxis function for '%s' is '%s'\n",strKey, fAxis); if(boost::equals(fAxis, "POS")) { direction = PIN_GAD_POSITIVE; printf("\t\t\tDirection POSITIVE\n"); } else if(boost::equals(fAxis, "NEG")) { direction = PIN_GAD_NEGATIVE; printf("\t\t\tDirection NEGATIVE\n"); } else { printf("\t\t\tDirection BOTH\n"); } return CreateKeyInfo(PIN_GKT_AXIS,SDL_CONTROLLER_BUTTON_INVALID,ax,direction); } else { printf("\t\t\tKey '%s' is INVALID!\n",strKey); return CreateKeyInfo(PIN_GKT_BUTTON,SDL_CONTROLLER_BUTTON_INVALID,SDL_CONTROLLER_AXIS_INVALID); } }
void ConfigManager::getBindingMap (LUA& lua, std::map<std::string, std::string>* map, const char *key, BindingType type) { lua.getGlobalKeyValue(key); while (lua.getNextKeyValue()) { const std::string id = lua.getKey(); if (id.empty()) { lua.pop(); continue; } lua_pushnil(lua.getState()); std::map<std::string, std::string> strMap; while (lua_next(lua.getState(), -2) != 0) { const char *_key = lua_tostring(lua.getState(), -2); assert(_key); std::string _value; if (lua_isstring(lua.getState(), -1)) { _value = lua_tostring(lua.getState(), -1); } else if (lua_isnumber(lua.getState(), -1)) { _value = string::toString(lua_tonumber(lua.getState(), -1)); } else if (lua_isboolean(lua.getState(), -1)) { _value = lua_toboolean(lua.getState(), -1) ? "true" : "false"; } strMap[_key] = _value; lua_pop(lua.getState(), 1); } BindingSpace bindingSpace = BINDINGS_UI; if (id == "map") bindingSpace = BINDINGS_MAP; for (std::map<std::string, std::string>::const_iterator i = strMap.begin(); i != strMap.end(); ++i) { if (type == KEYBOARD) { map[bindingSpace][i->first] = i->second; } else if (type == JOYSTICK) { const std::string index = i->first.substr(3); map[bindingSpace][index] = i->second; } else if (type == CONTROLLER) { const SDL_GameControllerButton button = SDL_GameControllerGetButtonFromString(i->first.c_str()); const char *buttonStr = SDL_GameControllerGetStringForButton(button); map[bindingSpace][buttonStr] = i->second; } } lua_pop(lua.getState(), 1); } }
static int lua_SDL_GameControllerGetButtonFromString(State & state){ Stack * stack = state.stack; if (stack->is<LUA_TSTRING>(1)){ const std::string name = stack->to<const std::string>(1); SDL_GameControllerButton button = SDL_GameControllerGetButtonFromString(name.c_str()); if (button == SDL_CONTROLLER_BUTTON_INVALID){ stack->push<bool>(false); } else{ stack->push<int>(button); } return 1; } else{ return 0; } }
void ConfigManager::getBindingMap (LUA& lua, std::map<std::string, std::string>* map, const char *key, BindingType type) { if (!lua.getGlobalKeyValue(key)) return; while (lua.getNextKeyValue()) { const std::string id = lua.getKey(); if (id.empty()) { lua.pop(); continue; } lua_pushnil(lua.getState()); std::map<std::string, std::string> strMap; while (lua.getNextKeyValue()) { const std::string& _key = lua.getLuaValue(-2); const std::string& _value = lua.getLuaValue(-1); strMap[_key] = _value; lua.pop(); } BindingSpace bindingSpace = BINDINGS_UI; if (id == "map") bindingSpace = BINDINGS_MAP; for (std::map<std::string, std::string>::const_iterator i = strMap.begin(); i != strMap.end(); ++i) { if (type == KEYBOARD) { map[bindingSpace][i->first] = i->second; } else if (type == CONTROLLER) { const SDL_GameControllerButton button = SDL_GameControllerGetButtonFromString(i->first.c_str()); const char *buttonStr = SDL_GameControllerGetStringForButton(button); map[bindingSpace][buttonStr] = i->second; } } lua_pop(lua.getState(), 1); } }
static int ReadMappingsString(const char *s) { #define READ_TOKEN(buf, p, end)\ if (end == NULL)\ {\ strcpy(buf, p);\ p = NULL;\ }\ else\ {\ strncpy(buf, p, end - p);\ buf[end - p] = '\0';\ p = end + 1;\ } // Read compiled string button names into memory // Search for a matching GUID + joystickName in the db int read = 0; for (const char *cur = s; cur;) { const char *nl = strchr(cur, '\n'); char line[2048]; READ_TOKEN(line, cur, nl); char buf[256]; // Check for the platform string sprintf(buf, "platform:%s", SDL_GetPlatform()); if (strstr(line, buf) == NULL) continue; #define STR_NOT_EQ(expected, actualP, actualEnd)\ strlen(expected) != (actualEnd) - (actualP) || \ strncmp(expected, actualP, (actualEnd) - (actualP)) != 0 const char *curL = line; // Ignore hash comments if (*curL == '#') continue; const char *nextComma; JoystickButtonNames j = jDefault; // Read GUID nextComma = strchr(curL, ','); if (nextComma == NULL || cur == nextComma) continue; READ_TOKEN(buf, curL, nextComma); j.guid = SDL_JoystickGetGUIDFromString(buf); // Read joystick name nextComma = strchr(curL, ','); if (nextComma == NULL || curL == nextComma) continue; READ_TOKEN(j.joystickName, curL, nextComma); // Check if GUID+joystick name already exists bool exists = false; for (int i = 0; i < nJBN; i++) { const JoystickButtonNames *jp = jbn + i; if (memcmp(&jp->guid, &j.guid, sizeof j.guid) == 0 && strcmp(jp->joystickName, j.joystickName) == 0) { exists = true; break; } } if (exists) continue; // Read name and colors for (;; curL = nextComma + 1) { nextComma = strchr(curL, ','); if (nextComma == NULL) break; const char *nextColon; nextColon = strchr(curL, ':'); if (nextColon == NULL || curL == nextColon) continue; READ_TOKEN(buf, curL, nextColon); const SDL_GameControllerButton button = SDL_GameControllerGetButtonFromString(buf); const SDL_GameControllerAxis axis = SDL_GameControllerGetAxisFromString(buf); char *name; SDL_Color *color; if (button != SDL_CONTROLLER_BUTTON_INVALID) { name = j.buttonNames[(int)button]; color = &j.buttonColors[(int)button]; } else if (axis != SDL_CONTROLLER_AXIS_INVALID) { name = j.axisNames[(int)axis]; color = &j.axisColors[(int)axis]; } else { continue; } // Read the real button/axis name nextColon = strchr(curL, ':'); if (nextColon == NULL) continue; READ_TOKEN(name, curL, nextColon); // R nextColon = strchr(curL, ':'); if (nextColon == NULL) continue; READ_TOKEN(buf, curL, nextColon); color->r = (Uint8)atoi(buf); // G nextColon = strchr(curL, ':'); if (nextColon == NULL) continue; READ_TOKEN(buf, curL, nextColon); color->g = (Uint8)atoi(buf); // B READ_TOKEN(buf, curL, nextComma); color->b = (Uint8)atoi(buf); color->a = 255; } nJBN++; read++; jbn = SDL_realloc(jbn, nJBN * sizeof *jbn); memcpy(jbn + nJBN - 1, &j, sizeof j); } return read; }
inline controller::button to_button(const std::string& str) noexcept { return static_cast<controller::button>(SDL_GameControllerGetButtonFromString(str.c_str())); }