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); } }
static int lua_SDL_GameControllerGetAxisFromString(State & state){ Stack * stack = state.stack; if (stack->is<LUA_TSTRING>(1)){ const std::string name = stack->to<const std::string>(1); SDL_GameControllerAxis axis = SDL_GameControllerGetAxisFromString(name.c_str()); if (axis == SDL_CONTROLLER_AXIS_INVALID){ stack->push<bool>(false); } else{ stack->push<int>(axis); } return 1; } else{ return 0; } }
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::axis to_axis(const std::string& str) noexcept { return static_cast<controller::axis>(SDL_GameControllerGetAxisFromString(str.c_str())); }