static const char *sdl_joypad_name(unsigned pad) { if (pad >= MAX_USERS) return NULL; #ifdef HAVE_SDL2 if (sdl_pads[pad].controller) return SDL_GameControllerNameForIndex(pad); return SDL_JoystickNameForIndex(pad); #else return SDL_JoystickName(pad); #endif }
void inputinit() { int i, numjoysticks; if( (numjoysticks=SDL_NumJoysticks())>0 ) { joysticks = malloc(sizeof(*joysticks)*numjoysticks); SDL_JoystickEventState(SDL_ENABLE); SJC_Write("%d controller/joystick%s detected:",numjoysticks,(numjoysticks>1?"s":"")); for( i=0; i<numjoysticks; i++ ) { joysticks[i] = SDL_JoystickOpen(i); SJC_Write(" #%i: %.20s",i,SDL_JoystickName(i)); } } }
/** * @brief Adds joysticks to the options menu */ void IN_JoystickInitMenu (void) { uiNode_t* joystickOptions = nullptr; const int total = SDL_NumJoysticks(); if (total == 0) { UI_AddOption(&joystickOptions, "", _("None"), "0"); } else { for (int i = 0; i < total; i++) UI_AddOption(&joystickOptions, "", SDL_JoystickName(i), va("%i", i)); } UI_RegisterOption(OPTION_JOYSTICKS, joystickOptions); }
bool Joystick::attachJoystick(int which) { Q_ASSERT(device_attached != true); joystick = SDL_JoystickOpen(which); if (joystick == nullptr) { qCWarning(phxInput, "Joystick: Unable to open sdl joystick: %s", SDL_GetError()); return false; } setDeviceName(SDL_JoystickName(joystick)); device_attached = true; return true; }
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 Application::initializeJoystickDevices() { closeJoysticks(); const int numJoysticks = SDL_NumJoysticks(); for (int which=0; which<numJoysticks; ++which) { SDL_Joystick *handle = SDL_JoystickOpen(which); if (handle) { TRACE(string("Joystick ") + SDL_JoystickName(which) + " Successfully opened."); } joysticks.push_back(Joystick(handle, which)); } }
static void SetJoystickButtonLabel(void) { char *name; name = "None set"; if (joystick_initted && joystick_index >= 0 && joystick_index < SDL_NumJoysticks()) { name = (char *) SDL_JoystickName(joystick_index); } TXT_SetButtonLabel(joystick_button, name); }
const char *GameJoystick::getName( long n ) { if( n < 0 ) return NULL; #ifdef HAVE_SDL_SDL_H if( n >= SDL_NumJoysticks() ) return NULL; return SDL_JoystickName( n ); #endif return NULL; }
void send_joystick_info(int joy_idx) { SDL_Joystick * joy = joysticks.find( joy_idx )->second; lo_message m1 = lo_message_new(); lo_message_add_string( m1, SDL_JoystickName(joy_idx) ); lo_message_add_int32( m1, joy_idx ); lo_message_add_int32( m1, SDL_JoystickNumAxes(joy) ); lo_message_add_int32( m1, SDL_JoystickNumButtons(joy) ); lo_message_add_int32( m1, SDL_JoystickNumHats(joy) ); lo_message_add_int32( m1, SDL_JoystickNumBalls(joy) ); lo_send_message_from( t, s, "/joystick/info", m1 ); }
// // I_GetJoystickNameFromIndex // std::string I_GetJoystickNameFromIndex (int index) { const char *joyname = NULL; std::string ret; joyname = SDL_JoystickName(index); if(!joyname) return ""; ret = joyname; return ret; }
void InputHandler_SDL::GetDevicesAndDescriptions(vector<InputDevice>& vDevicesOut, vector<CString>& vDescriptionsOut) { vDevicesOut.push_back( DEVICE_KEYBOARD ); vDescriptionsOut.push_back( "Keyboard" ); for( int i=0; i<SDL_NumJoysticks(); i++ ) { if( SDL_JoystickOpened(i) ) { vDevicesOut.push_back( InputDevice(DEVICE_JOY1+i) ); vDescriptionsOut.push_back( SDL_JoystickName(i) ); } } }
int joystickb_sdl_device_desc_get(unsigned joystick, char* desc) { #if SDL_MAJOR_VERSION == 1 const char* joy_name = SDL_JoystickName(joystick); if (!joy_name) return -1; log_std(("joystickb:sdl: desc:\"%s\"\n", joy_name)); sncpy(desc, DEVICE_NAME_MAX, joy_name); #else const char* joy_name = SDL_JoystickName(sdl_state.map[joystick]); if (!joy_name) return -1; log_std(("joystickb:sdl: desc:\"%s\"\n", joy_name)); sncpy(desc, DEVICE_NAME_MAX, joy_name); #endif return 0; }
vector<std::string> cJoystick :: Get_Names( void ) const { vector<std::string> names; // get joy count int joy_count = SDL_NumJoysticks(); // joystick names for( int i = 0; i < joy_count; i++ ) { names.push_back( SDL_JoystickName( i ) ); } return names; }
void md::init_joysticks() { int n; unsigned int i; SDL_Joystick *(*tmp)[]; deinit_joysticks(); // Initialize the joystick support // Thanks to Cameron Moore <*****@*****.**> if(SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) { fprintf(stderr, "joystick: Unable to initialize joystick system\n"); return; } n = SDL_NumJoysticks(); if (n <= 0) { fprintf(stderr, "joystick: no joystick found\n"); return; } fprintf(stderr, "joystick: %d joystick(s) found\n", n); tmp = (SDL_Joystick *(*)[])malloc(sizeof((*tmp)[0]) * n); if (tmp == NULL) { fprintf(stderr, "joystick: unable to allocate memory\n"); return; } // Open all of them. for (i = 0; (i != (unsigned int)n); ++i) { SDL_Joystick *handle = SDL_JoystickOpen(i); if (handle == NULL) fprintf(stderr, "joystick: can't open joystick %u: %s", i, SDL_GetError()); else fprintf(stderr, "joystick #%u:, %d %s, %d button(s)," " %d hat(s), name: \"%s\"\n", i, SDL_JoystickNumAxes(handle), ((SDL_JoystickNumAxes(handle) == 1) ? "axis" : "axes"), SDL_JoystickNumButtons(handle), SDL_JoystickNumHats(handle), SDL_JoystickName(i)); (*tmp)[i] = handle; } handles = tmp; handles_n = i; // Enable joystick events SDL_JoystickEventState(SDL_ENABLE); }
/* * Open controllers and joysticks */ void S2D_OpenControllers() { char guid_str[33]; // Enumerate joysticks for (int device_index = 0; device_index < SDL_NumJoysticks(); ++device_index) { // Check if joystick supports SDL's game controller interface (a mapping is available) if (SDL_IsGameController(device_index)) { SDL_GameController *controller = SDL_GameControllerOpen(device_index); SDL_JoystickID intance_id = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(controller)); SDL_JoystickGetGUIDString( SDL_JoystickGetGUID(SDL_GameControllerGetJoystick(controller)), guid_str, 33 ); if (intance_id > last_intance_id) { if (controller) { S2D_Log(S2D_INFO, "Controller #%i: %s\n GUID: %s", intance_id, SDL_GameControllerName(controller), guid_str); } else { S2D_Log(S2D_ERROR, "Could not open controller #%i: %s", intance_id, SDL_GetError()); } last_intance_id = intance_id; } // Controller interface not supported, try to open as joystick } else { SDL_Joystick *joy = SDL_JoystickOpen(device_index); SDL_JoystickID intance_id = SDL_JoystickInstanceID(joy); if (!joy) { S2D_Log(S2D_ERROR, "Could not open controller"); } else if(intance_id > last_intance_id) { SDL_JoystickGetGUIDString( SDL_JoystickGetGUID(joy), guid_str, 33 ); S2D_Log(S2D_INFO, "Controller #%i: %s\n GUID: %s\n Axes: %d\n Buttons: %d\n Balls: %d", intance_id, SDL_JoystickName(joy), guid_str, SDL_JoystickNumAxes(joy), SDL_JoystickNumButtons(joy), SDL_JoystickNumBalls(joy) ); S2D_Log(S2D_WARN, "Controller #%i does not have a mapping available", intance_id); last_intance_id = intance_id; } } } }
static SDL_bool WatchJoystick(SDL_Joystick * joystick) { SDL_Window *window = NULL; const char *name = NULL; /* Create a window to display joystick axis position */ window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, 0); if (window == NULL) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError()); return SDL_FALSE; } screen = SDL_CreateRenderer(window, -1, 0); if (screen == NULL) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError()); SDL_DestroyWindow(window); return SDL_FALSE; } SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE); SDL_RenderClear(screen); SDL_RenderPresent(screen); SDL_RaiseWindow(window); /* Print info about the joystick we are watching */ name = SDL_JoystickName(joystick); SDL_Log("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick), name ? name : "Unknown Joystick"); SDL_Log("Joystick has %d axes, %d hats, %d balls, and %d buttons\n", SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick), SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick)); /* Loop, getting joystick events! */ #ifdef __EMSCRIPTEN__ emscripten_set_main_loop_arg(loop, joystick, 0, 1); #else while (!done) { loop(joystick); } #endif SDL_DestroyRenderer(screen); SDL_DestroyWindow(window); return retval; }
lo_message get_joystick_info_msg(int joy_idx, SDL_Joystick * joy ) { // SDL_Joystick * joy = joysticks.find( joy_idx )->second; lo_message m1 = lo_message_new(); lo_message_add_string( m1, SDL_JoystickName(joy_idx) ); lo_message_add_int32( m1, joy_idx ); lo_message_add_int32( m1, SDL_JoystickNumAxes(joy) ); lo_message_add_int32( m1, SDL_JoystickNumButtons(joy) ); lo_message_add_int32( m1, SDL_JoystickNumHats(joy) ); lo_message_add_int32( m1, SDL_JoystickNumBalls(joy) ); // lo_send_message_from( t, s, "/joystick/info", m1 ); return m1; }
void InputManager::addJoystickByDeviceIndex(int id) { assert(id >= 0 && id < SDL_NumJoysticks()); // open joystick & add to our list SDL_Joystick* joy = SDL_JoystickOpen(id); assert(joy); // add it to our list so we can close it again later SDL_JoystickID joyId = SDL_JoystickInstanceID(joy); mJoysticks[joyId] = joy; char guid[65]; SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joy), guid, 65); // create the InputConfig mInputConfigs[joyId] = new InputConfig(joyId, SDL_JoystickName(joy), guid); if(!loadInputConfig(mInputConfigs[joyId])) { LOG(LogInfo) << "Added unconfigured joystick " << SDL_JoystickName(joy) << " (GUID: " << guid << ", instance ID: " << joyId << ", device index: " << id << ")."; } else { LOG(LogInfo) << "Added known joystick " << SDL_JoystickName(joy) << " (instance ID: " << joyId << ", device index: " << id << ")"; } // set up the prevAxisValues int numAxes = SDL_JoystickNumAxes(joy); mPrevAxisValues[joyId] = new int[numAxes]; std::fill(mPrevAxisValues[joyId], mPrevAxisValues[joyId] + numAxes, 0); // initialize array to 0 }
// // // ISDL12InputSubsystem::getJoystickDevices // // std::vector<IInputDeviceInfo> ISDL12InputSubsystem::getJoystickDevices() const { std::vector<IInputDeviceInfo> devices; for (int i = 0; i < SDL_NumJoysticks(); i++) { devices.push_back(IInputDeviceInfo()); IInputDeviceInfo& device_info = devices.back(); device_info.mId = i; char name[256]; sprintf(name, "SDL 1.2 joystick (%s)", SDL_JoystickName(i)); device_info.mDeviceName = name; } return devices; }
bool JoystickInfo::Init(int id) { Destroy(); _id = id; joy = SDL_JoystickOpen(id); if (joy == NULL) { PAD_LOG("failed to open joystick %d\n", id); return false; } numaxes = SDL_JoystickNumAxes(joy); numbuttons = SDL_JoystickNumButtons(joy); numhats = SDL_JoystickNumHats(joy); devname = SDL_JoystickName(id); vaxisstate.resize(numaxes); vbuttonstate.resize(numbuttons); vhatstate.resize(numhats); // Sixaxis, dualshock3 hack // Most buttons are actually axes due to analog pressure support. Only the first 4 buttons // are digital (select, start, l3, r3). To avoid conflict just forget the others. // Keep the 4 hat buttons too (usb driver). (left pressure does not work with recent kernel). Moreover the pressure // work sometime on half axis neg others time in fulll axis. So better keep them as button for the moment u32 found_hack = devname.find(string("PLAYSTATION(R)3")); if (found_hack != string::npos) { numbuttons = 4; // Enable this hack is bluetooth too. It avoid to restart the onepad gui numbuttons += 4; } #if SDL_VERSION_ATLEAST(1,3,0) if ( haptic == NULL ) { if (!SDL_JoystickIsHaptic(joy)) { PAD_LOG("Haptic devices not supported!\n"); } else { haptic = SDL_HapticOpenFromJoystick(joy); // upload some default effect InitHapticEffect(); } } #endif //PAD_LOG("There are %d buttons, %d axises, and %d hats.\n", numbuttons, numaxes, numhats); return true; }
void sdl_input_init() { joyCount = SDL_NumJoysticks(); if (joyCount > 5) joyCount = 5; printf("%d Joystick(s) Found\n", joyCount); if (joyCount > 0) { for (int i = 0; i < joyCount; i++) { printf("%s\t",SDL_JoystickName(i)); joys[i] = SDL_JoystickOpen(i); printf("Hats %d\t",SDL_JoystickNumHats(joys[i])); printf("Buttons %d\t",SDL_JoystickNumButtons(joys[i])); printf("Axis %d\n",SDL_JoystickNumAxes(joys[i])); } } }
std::vector<JoystickDevice> CApplication::GetJoystickList() { std::vector<JoystickDevice> result; int count = SDL_NumJoysticks(); for (int index = 0; index < count; ++index) { JoystickDevice device; device.index = index; device.name = SDL_JoystickName(index); result.push_back(device); } return result; }
const char *InputDeviceName(int d, int deviceIndex) { switch (d) { case INPUT_DEVICE_KEYBOARD: return "Keyboard"; case INPUT_DEVICE_MOUSE: return "Mouse"; case INPUT_DEVICE_JOYSTICK: return SDL_JoystickName(deviceIndex); case INPUT_DEVICE_NET: return "Net"; default: return ""; } }
static PyObject* _sdl_joygetname (PyObject *self, PyObject *args) { int joy; ASSERT_JOYSTICK_INIT(NULL); if (!IntFromObj (args, &joy)) return NULL; if (joy < 0 || joy >= SDL_NumJoysticks()) { PyErr_SetString (PyExc_ValueError, "invalid joystick index"); return NULL; } return Text_FromUTF8 (SDL_JoystickName (joy)); }
TGen::Engine::SDLGamepad::SDLGamepad(int id, int internalId) : TGen::Engine::InputDevice("gamepad", id) , joystick(NULL) { joystick = SDL_JoystickOpen(internalId); if (!joystick) throw TGen::RuntimeException("SDLGamepad::SDLGamepad", "failed to open joystick # ") << internalId << "!"; joyName = SDL_JoystickName(internalId); controls.numAxes = SDL_JoystickNumAxes(joystick); controls.numButtons = SDL_JoystickNumBalls(joystick); controls.numBalls = SDL_JoystickNumBalls(joystick); controls.numHats = SDL_JoystickNumHats(joystick); }
void cJoystickSDL::Open() { mJoystick = SDL_JoystickOpen( mIndex ); if ( NULL != mJoystick ) { mName = SDL_JoystickName( mIndex ); mHats = SDL_JoystickNumHats( mJoystick ); mButtons = SDL_JoystickNumButtons( mJoystick ); mAxes = SDL_JoystickNumAxes( mJoystick ); mBalls = SDL_JoystickNumBalls( mJoystick ); mButtonDown = mButtonDownLast = mButtonUp = 0; if ( mButtons > 32 ) mButtons = 32; } }
static const known_joystick_t *GetJoystickType(int index) { SDL_Joystick *joystick; const char *name; int axes, buttons, hats; int i; joystick = all_joysticks[index]; name = SDL_JoystickName(index); axes = SDL_JoystickNumAxes(joystick); buttons = SDL_JoystickNumButtons(joystick); hats = SDL_JoystickNumHats(joystick); for (i = 0; i < arrlen(known_joysticks); ++i) { // Check for a name match. If the name ends in '*', this means // ignore the rest. if (M_StringEndsWith(known_joysticks[i].name, "*")) { if (strncmp(known_joysticks[i].name, name, strlen(known_joysticks[i].name) - 1) != 0) { continue; } } else { if (strcmp(known_joysticks[i].name, name) != 0) { continue; } } if (known_joysticks[i].axes == axes && known_joysticks[i].buttons == buttons && known_joysticks[i].hats == hats) { return &known_joysticks[i]; } } printf("Unknown joystick '%s' with %i axes, %i buttons, %i hats\n", name, axes, buttons, hats); printf("Please consider sending in details about your gamepad!\n"); return NULL; }
void initJoy (void) { if (!(SDL_Init(SDL_INIT_EVENTTHREAD | SDL_INIT_JOYSTICK) < 0)) { if (SDL_NumJoysticks() > 1) { mystick = SDL_JoystickOpen(1); if(mystick != NULL) NoJoy = 0; else NoJoy = 1; } else { if (SDL_NumJoysticks() > 0) { mystick = SDL_JoystickOpen(0); if(mystick != NULL) NoJoy = 0; else NoJoy = 1; } else NoJoy = 1; } } SDL_JoystickEventState(SDL_QUERY); const char *name; int i; for ( i=0; i<SDL_NumJoysticks(); ++i ) { name = SDL_JoystickName(i); printf("Joystick %d: %s\n",i,name ? name : "Unknown Joystick"); mystick = SDL_JoystickOpen(i); if (mystick == NULL) { fprintf(stderr, "SDL_JoystickOpen(%d) failed: %s\n", i, SDL_GetError()); } else { printf(" axes: %d\n", SDL_JoystickNumAxes(mystick)); printf(" balls: %d\n", SDL_JoystickNumBalls(mystick)); printf(" hats: %d\n", SDL_JoystickNumHats(mystick)); printf(" buttons: %d\n", SDL_JoystickNumButtons(mystick)); SDL_JoystickClose(mystick); } } }
int get_joyinfo(SDL_Joystick *stick) { const char *name; name = SDL_JoystickName(0); if(!name) { return 0; } strcpy(g_context.name, name); g_context.axes = SDL_JoystickNumAxes(stick); g_context.buttons = SDL_JoystickNumButtons(stick); return 1; }
// // initinput() -- init input system // int initinput(void) { int i,j; #ifdef __APPLE__ // force OS X to operate in >1 button mouse mode so that LMB isn't adulterated if (!getenv("SDL_HAS3BUTTONMOUSE")) putenv("SDL_HAS3BUTTONMOUSE=1"); #endif if (SDL_EnableKeyRepeat(250, 30)) buildputs("Error enabling keyboard repeat.\n"); inputdevices = 1|2; // keyboard (1) and mouse (2) mouseacquired = 0; SDL_EnableUNICODE(1); // let's hope this doesn't hit us too hard memset(keynames,0,sizeof(keynames)); for (i=0; i<SDLK_LAST; i++) { if (!keytranslation[i]) continue; strncpy(keynames[ keytranslation[i] ], SDL_GetKeyName(i), sizeof(keynames[i])-1); } if (!SDL_InitSubSystem(SDL_INIT_JOYSTICK)) { i = SDL_NumJoysticks(); buildprintf("%d joystick(s) found\n",i); for (j=0;j<i;j++) buildprintf(" %d. %s\n", j+1, SDL_JoystickName(j)); joydev = SDL_JoystickOpen(0); if (joydev) { SDL_JoystickEventState(SDL_ENABLE); inputdevices |= 4; joynumaxes = SDL_JoystickNumAxes(joydev); joynumbuttons = min(32,SDL_JoystickNumButtons(joydev)); joynumhats = SDL_JoystickNumHats(joydev); buildprintf("Joystick 1 has %d axes, %d buttons, and %d hat(s).\n", joynumaxes,joynumbuttons,joynumhats); joyaxis = Bcalloc(joynumaxes, sizeof(int)); if (joynumhats > 0) { joyhat = malloc(joynumhats * sizeof(int)); memset(joyhat, -1, sizeof(int)*joynumhats); } } } return 0; }