inline std::string to_string(controller::button b) { const char* result = SDL_GameControllerGetStringForButton(static_cast<SDL_GameControllerButton>(b)); if(result == nullptr) { return ""; } return { result }; }
QString keyToMappingString( Remapper::Key key ) { QString keyString; if( key.first == Remapper::BUTTON ) { keyString.append( SDL_GameControllerGetStringForButton( static_cast<SDL_GameControllerButton>( key.second ) ) ); } else if( key.first == Remapper::AXIS ) { keyString.append( SDL_GameControllerGetStringForAxis( static_cast<SDL_GameControllerAxis>( key.second ) ) ); } return keyString; }
// FIXME: Kinda overlaps with keyToMappingString() QString gameControllerIDToMappingString( int gameControllerID ) { QString result = QString( SDL_GameControllerGetStringForButton( static_cast<SDL_GameControllerButton>( gameControllerID ) ) ); if( !result.isEmpty() ) { return result; } result = QString( SDL_GameControllerGetStringForAxis( static_cast<SDL_GameControllerAxis>( gameControllerID ) ) ); // Will return either a valid label or empty string return result; }
static int _SaveMappings() { char bind[2056]; const char* name; if (_this->controller != NULL) { name = SDL_GameControllerName(_this->controller); } else { name = SDL_JoystickName(_this->joystick); } SDL_JoystickGUID guid = SDL_JoystickGetGUID(_this->joystick); char guid_string[33]; SDL_JoystickGetGUIDString(guid, guid_string, 33); guid_string[32] = 0; snprintf(bind, 2056, "%s,%s,platform:%s,", guid_string, name, SDL_GetPlatform()); int index = strlen(bind); for (int i = 0; i < NUM_MAPPINGS; ++i) { const char* to = NULL; if (_this->mappings[i].type == SDL_CONTROLLER_BINDTYPE_AXIS) { to = SDL_GameControllerGetStringForAxis(_this->mappings[i].value.axis); } else if (_this->mappings[i].type != SDL_CONTROLLER_BINDTYPE_BUTTON){ to = SDL_GameControllerGetStringForButton(_this->mappings[i].value.button); } if (to == NULL) { continue; } char from[10]; if (_this->mappings[i].bind.bindType == SDL_CONTROLLER_BINDTYPE_AXIS) { snprintf(from, 10, "a%d", _this->mappings[i].bind.value.axis); } else if (_this->mappings[i].bind.bindType == SDL_CONTROLLER_BINDTYPE_BUTTON) { snprintf(from, 10, "b%d", _this->mappings[i].bind.value.button); } if (_this->mappings[i].bind.bindType == SDL_CONTROLLER_BINDTYPE_HAT) { snprintf(from, 10, "h%d.%d", _this->mappings[i].bind.value.hat.hat, _this->mappings[i].bind.value.hat.hat_mask); } else { continue; } snprintf(bind, 2056 - index, "%s:%s,", to, from); index += strlen(to) + strlen(from); } SDL_GameControllerAddMapping(bind); // TODO Save to file SDL_Log("Mapping: %s\n", bind); }
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); } }
PIN_String GetKeyString(PIN_GameControllerKeyInfo key) { if(key.KeyType == PIN_GKT_BUTTON) { if(key.Button == SDL_CONTROLLER_BUTTON_INVALID) { return "INVALID"; } return SDL_GameControllerGetStringForButton(key.Button); } else if(key.KeyType == PIN_GKT_AXIS) { if(key.Axis == SDL_CONTROLLER_AXIS_INVALID) { return "INVALID"; } return SDL_GameControllerGetStringForAxis(key.Axis); } return "???"; }
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); } }
void test_gamecontroller_events(SDL_GameController* gamepad) { assert(gamepad); printf("Entering gamecontroller test loop, press Ctrl-c to exit\n"); int quit = 0; SDL_Event event; while(!quit && SDL_WaitEvent(&event)) { switch(event.type) { case SDL_JOYAXISMOTION: //printf("SDL_JOYAXISMOTION: joystick: %d axis: %d value: %d\n", // event.jaxis.which, event.jaxis.axis, event.jaxis.value); break; case SDL_JOYBUTTONDOWN: //printf("SDL_JOYBUTTONDOWN: joystick: %d button: %d state: %d\n", // event.jbutton.which, event.jbutton.button, event.jbutton.state); break; case SDL_JOYBUTTONUP: //printf("SDL_JOYBUTTONUP: joystick: %d button: %d state: %d\n", // event.jbutton.which, event.jbutton.button, event.jbutton.state); break; case SDL_JOYHATMOTION: //printf("SDL_JOYHATMOTION: joystick: %d hat: %d value: %d\n", // event.jhat.which, event.jhat.hat, event.jhat.value); break; case SDL_JOYBALLMOTION: //printf("SDL_JOYBALLMOTION: joystick: %d ball: %d x: %d y: %d\n", // event.jball.which, event.jball.ball, event.jball.xrel, event.jball.yrel); break; case SDL_JOYDEVICEADDED: //printf("SDL_JOYDEVICEADDED which:%d\n", event.jdevice.which); break; case SDL_JOYDEVICEREMOVED: //printf("SDL_JOYDEVICEREMOVED which:%d\n", event.jdevice.which); break; case SDL_CONTROLLERAXISMOTION: printf("SDL_CONTROLLERAXISMOTION controller: %d axis: %-12s value: %d\n", event.caxis.which, SDL_GameControllerGetStringForAxis(event.caxis.axis), event.caxis.value); break; case SDL_CONTROLLERBUTTONDOWN: printf("SDL_CONTROLLERBUTTONDOWN controller: %d button: %s state: %d\n", event.cbutton.which, SDL_GameControllerGetStringForButton(event.cbutton.button), event.cbutton.state); break; case SDL_CONTROLLERBUTTONUP: printf("SDL_CONTROLLERBUTTONUP controller: %d button: %s state: %d\n", event.cbutton.which, SDL_GameControllerGetStringForButton(event.cbutton.button), event.cbutton.state); break; case SDL_CONTROLLERDEVICEADDED: printf("SDL_CONTROLLERDEVICEADDED which:%d\n", event.cdevice.which); break; case SDL_CONTROLLERDEVICEREMOVED: printf("SDL_CONTROLLERDEVICEREMOVED which:%d\n", event.cdevice.which); break; case SDL_CONTROLLERDEVICEREMAPPED: printf("SDL_CONTROLLERDEVICEREMAPPED which:%d\n", event.cdevice.which); break; case SDL_QUIT: quit = 1; printf("Recieved interrupt, exiting\n"); break; default: fprintf(stderr, "Error: Unhandled event type: %#x\n", event.type); break; } } }
PIN_GameControllerManager::PIN_GameControllerManager(PIN_ControlType* controlType, PIN_MappingList* mappingList, PIN_ControllerBaseList* masterControllerList ) : PIN_ManagerBase(controlType, mappingList, masterControllerList) { InitEvents(); std::string strKeyList(/*"\\b(?:"*/""); std::string strAxisList(""); bool firstItem = true; for(int x = SDL_CONTROLLER_BUTTON_A; x < SDL_CONTROLLER_BUTTON_MAX; x++) { PIN_String strButton = SDL_GameControllerGetStringForButton((SDL_GameControllerButton)x); if(!boost::equals(strButton,"")) { if(!firstItem) { strKeyList += "|"; } else { firstItem = false; } std::string strCode(strButton); strCode = PAUX_Extensors::regex_escape(strCode); strKeyList+= "(?:\\b" + strCode + "\\b)"; } } firstItem = true; for(int x = SDL_CONTROLLER_AXIS_LEFTX; x < SDL_CONTROLLER_AXIS_MAX; x++) { PIN_String strAxis = SDL_GameControllerGetStringForAxis((SDL_GameControllerAxis)x); if(!boost::equals(strAxis,"")) { strKeyList += "|"; if(!firstItem) { strAxisList += "|"; } else { firstItem = false; } std::string strCode(strAxis); strCode = PAUX_Extensors::regex_escape(strCode); strCode = "(?:(?<func>POS|NEG)\\()?(?<axis>\\b"+strCode+"\\b)\\)?";// //strCode = "(?:(?:(<func>POS|NEG)\\()?(<axis>" + strCode + ")(?'func'\\))?)"; //printf("Axis code: %s\n",strCode.c_str()); strAxisList += strCode; strKeyList+= strCode; } } //strKeyList+=")\\b"; _keyListRegex = boost::regex(strKeyList,boost::regex::perl|boost::regex::icase); _regexAxisString = boost::regex(strAxisList,boost::regex::perl|boost::regex::icase); //printf("Main regex: %s\n",_keyListRegex.str().c_str()); //printf("Axis regex: %s\n",_regexAxisString.str().c_str()); }
void PIN_GameControllerManager::AddGameController(int joy_index) { printf("Checking joystick #%d...\n",joy_index); printf("Name: %s\n",SDL_JoystickNameForIndex(joy_index)); if(SDL_IsGameController(joy_index)) { printf("This joystick is a gamecontroller. Adding to controller pool...\n"); printf("Game controller name: %s\n",SDL_GameControllerNameForIndex(joy_index)); PIN_GameControllerEntry* newEntry = CreateGameControllerEntry(); SDL_GameController* ctrl = SDL_GameControllerOpen(joy_index); SDL_Joystick* joy = SDL_GameControllerGetJoystick(ctrl); SDL_JoystickGUID joyGUID = SDL_JoystickGetGUID(joy); char strGUID[PIN_BUFFER_SIZE]; SDL_JoystickGetGUIDString(joyGUID,strGUID, PIN_BUFFER_SIZE); printf("Controller GUID: %s\n",strGUID); SDL_JoystickID joyID = SDL_JoystickInstanceID(joy); if(joyID < 0) { printf("Error: %s\n",SDL_GetError()); delete newEntry; return; } else { newEntry->ControllerID = joyID + PIN_ID_JOYSTICK; } newEntry->Controller = ctrl; newEntry->Joystick = joy; newEntry->AxisDeadZone = 8000; newEntry->GlobalKeys[PIN_GK_UP] = BuildComboSet(newEntry,SDL_GameControllerGetStringForButton(SDL_CONTROLLER_BUTTON_DPAD_UP)); newEntry->GlobalKeys[PIN_GK_DOWN] = BuildComboSet(newEntry,SDL_GameControllerGetStringForButton(SDL_CONTROLLER_BUTTON_DPAD_DOWN)); newEntry->GlobalKeys[PIN_GK_LEFT] = BuildComboSet(newEntry,SDL_GameControllerGetStringForButton(SDL_CONTROLLER_BUTTON_DPAD_LEFT)); newEntry->GlobalKeys[PIN_GK_RIGHT] = BuildComboSet(newEntry,SDL_GameControllerGetStringForButton(SDL_CONTROLLER_BUTTON_DPAD_RIGHT)); newEntry->GlobalKeys[PIN_GK_ENTER] = BuildComboSet(newEntry,SDL_GameControllerGetStringForButton(SDL_CONTROLLER_BUTTON_A)); newEntry->GlobalKeys[PIN_GK_BACK] = BuildComboSet(newEntry,SDL_GameControllerGetStringForButton(SDL_CONTROLLER_BUTTON_B)); PIN_KeyList lstDef = _controlType->DEFAULT_KEYS; printf("Setting control type definitions...\n"); AssignKey(newEntry, &(newEntry->GlobalKeys),PIN_GK_UP,lstDef,PIN_KEY_UP); AssignKey(newEntry, &(newEntry->GlobalKeys),PIN_GK_DOWN,lstDef,PIN_KEY_DOWN); AssignKey(newEntry, &(newEntry->GlobalKeys),PIN_GK_LEFT,lstDef,PIN_KEY_LEFT); AssignKey(newEntry, &(newEntry->GlobalKeys),PIN_GK_RIGHT,lstDef,PIN_KEY_RIGHT); AssignKey(newEntry, &(newEntry->GlobalKeys),PIN_GK_ENTER,lstDef,PIN_KEY_ENTER); AssignKey(newEntry, &(newEntry->GlobalKeys),PIN_GK_BACK,lstDef,PIN_KEY_BACK); for(PIN_MappingList::iterator it = _mappingList->begin(); it!= _mappingList->end(); ++it) { PIN_Mapping* m = *it; if(boost::equals(m->GUID,strGUID)) { printf("Setting mapping-specific definitions...\n"); printf("Mapping name: '%s'\n",m->NAME); PIN_KeyList m_keys = m->DEFAULT_KEYS; newEntry->SourceMapping = m; newEntry->AxisDeadZone = m->DEADZONE; AssignKey(newEntry, &(newEntry->GlobalKeys),PIN_GK_UP,m_keys,PIN_KEY_UP); AssignKey(newEntry, &(newEntry->GlobalKeys),PIN_GK_DOWN,m_keys,PIN_KEY_DOWN); AssignKey(newEntry, &(newEntry->GlobalKeys),PIN_GK_LEFT,m_keys,PIN_KEY_LEFT); AssignKey(newEntry, &(newEntry->GlobalKeys),PIN_GK_RIGHT,m_keys,PIN_KEY_RIGHT); AssignKey(newEntry, &(newEntry->GlobalKeys),PIN_GK_ENTER,m_keys,PIN_KEY_ENTER); AssignKey(newEntry, &(newEntry->GlobalKeys),PIN_GK_BACK,m_keys,PIN_KEY_BACK); break; } } if(_currentGameMode != NULL) { UpdateControllerGameMode(newEntry); } //_controllerList.push_back(newEntry); AddController(newEntry); } else { printf("This joystick is NOT a gamecontroller. It can't be used.\n"); } }
inline std::string EventHandler::getControllerButtonName(uint8_t button) const { const char *name = SDL_GameControllerGetStringForButton(static_cast<SDL_GameControllerButton>(button)); if (name == nullptr) return "unknown"; return name; }