Exemple #1
0
inline std::string to_string(controller::axis a) {
    const char* result = SDL_GameControllerGetStringForAxis(static_cast<SDL_GameControllerAxis>(a));
    if(result == nullptr) {
        return "";
    }
    return { result };
}
Exemple #2
0
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;
}
Exemple #3
0
// 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;
}
Exemple #4
0
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);
}
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 "???";
}
Exemple #6
0
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());
}