/* * 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; } } } }
const char* Joystick::GetDeviceGUID (int id) { char* guid = new char[64]; SDL_JoystickGetGUIDString (SDL_JoystickGetGUID (joysticks[id]), guid, 64); return guid; }
static mrb_value mrb_sdl2_joystick_joystick_get_guid(mrb_state *mrb, mrb_value self) { SDL_Joystick * joystick_p = mrb_sdl2_joystick_joystick_get_ptr(mrb, self); SDL_JoystickGUID result = SDL_JoystickGetGUID(joystick_p); return mrb_fixnum_value(*result.data); }
int main(int, char**){ SDL_Init(SDL_INIT_JOYSTICK); SDL_JoystickEventState(SDL_ENABLE); atexit(SDL_Quit); int num_joysticks = SDL_NumJoysticks(); if(num_joysticks == 0) { std::cout << "No joysticks found! please attach a device and re-run this program" << std::endl; return 0; } js = SDL_JoystickOpen(0); //ok we've found our joystick! if (js) { SDL_JoystickGUID guid = SDL_JoystickGetGUID(js); char guid_str[1024]; SDL_JoystickGetGUIDString(guid, guid_str, sizeof(guid_str)); const char* name = SDL_JoystickName(js); int num_axes = SDL_JoystickNumAxes(js); int num_buttons = SDL_JoystickNumButtons(js); int num_hats = SDL_JoystickNumHats(js); int num_balls = SDL_JoystickNumBalls(js); std:: cout << guid_str << " \\ " << name << " \\ axes: " << num_axes << " buttons: " << num_buttons << " hats: " << num_hats << " balls: " << num_balls << std::endl; } //start webserver and bind to socket. handler handler_; http_server::options options(handler_); http_server server( options.address("0.0.0.0") .port("1111")); std::cout << "found joystick! starting server!" << std::endl; server.run(); //function BLOCKS! see handler struct at top of page! return 0; }
bool Joystick::open(int deviceindex) { close(); joyhandle = SDL_JoystickOpen(deviceindex); if (joyhandle) { instanceid = SDL_JoystickInstanceID(joyhandle); // SDL_JoystickGetGUIDString uses 32 bytes plus the null terminator. char cstr[33]; SDL_JoystickGUID sdlguid = SDL_JoystickGetGUID(joyhandle); SDL_JoystickGetGUIDString(sdlguid, cstr, (int) sizeof(cstr)); pguid = std::string(cstr); // See if SDL thinks this is a Game Controller. openGamepad(deviceindex); // Prefer the Joystick name for consistency. const char *joyname = SDL_JoystickName(joyhandle); if (!joyname && controller) joyname = SDL_GameControllerName(controller); if (joyname) name = joyname; } return isConnected(); }
void print_joystick_info(int joy_idx, SDL_Joystick* joy, SDL_GameController* gamepad) { SDL_JoystickGUID guid = SDL_JoystickGetGUID(joy); char guid_str[1024]; SDL_JoystickGetGUIDString(guid, guid_str, sizeof(guid_str)); printf("Joystick Name: '%s'\n", SDL_JoystickName(joy)); printf("Joystick GUID: %s\n", guid_str); printf("Joystick Number: %2d\n", joy_idx); printf("Number of Axes: %2d\n", SDL_JoystickNumAxes(joy)); printf("Number of Buttons: %2d\n", SDL_JoystickNumButtons(joy)); printf("Number of Hats: %2d\n", SDL_JoystickNumHats(joy)); printf("Number of Balls: %2d\n", SDL_JoystickNumBalls(joy)); printf("GameController:\n"); if (!gamepad) { printf(" not a gamepad\n"); } else { printf(" Name: '%s'\n", SDL_GameControllerName(gamepad)); printf(" Mapping: '%s'\n", SDL_GameControllerMappingForGUID(guid)); } printf("\n"); }
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 }
Uint16 SDL_JoystickGetVendor(SDL_Joystick * joystick) { Uint16 vendor; SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick); SDL_GetJoystickGUIDInfo(guid, &vendor, NULL, NULL); return vendor; }
Uint16 SDL_JoystickGetProduct(SDL_Joystick * joystick) { Uint16 product; SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick); SDL_GetJoystickGUIDInfo(guid, NULL, &product, NULL); return product; }
Uint16 SDL_JoystickGetProductVersion(SDL_Joystick * joystick) { Uint16 version; SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick); SDL_GetJoystickGUIDInfo(guid, NULL, NULL, &version); return version; }
void InputDaemon::addInputDevice(int index) { SDL_Joystick *joystick = SDL_JoystickOpen(index); if (joystick) { SDL_JoystickID tempJoystickID = SDL_JoystickInstanceID(joystick); if (!joysticks->contains(tempJoystickID)) { QSettings *settings = new QSettings(PadderCommon::configFilePath, QSettings::IniFormat); settings->beginGroup("Mappings"); QString temp; SDL_JoystickGUID tempGUID = SDL_JoystickGetGUID(joystick); char guidString[65] = {'0'}; SDL_JoystickGetGUIDString(tempGUID, guidString, sizeof(guidString)); temp = QString(guidString); bool disableGameController = settings->value(QString("%1Disable").arg(temp), false).toBool(); if (SDL_IsGameController(index) && !disableGameController) { SDL_GameController *controller = SDL_GameControllerOpen(index); if (controller) { SDL_Joystick *sdlStick = SDL_GameControllerGetJoystick(controller); SDL_JoystickID tempJoystickID = SDL_JoystickInstanceID(sdlStick); if (!joysticks->contains(tempJoystickID)) { GameController *damncontroller = new GameController(controller, index, this); joysticks->insert(tempJoystickID, damncontroller); trackcontrollers.insert(tempJoystickID, damncontroller); // Force close of settings file. settings->endGroup(); delete settings; settings = 0; emit deviceAdded(damncontroller); } } } else { Joystick *curJoystick = new Joystick(joystick, index, this); joysticks->insert(tempJoystickID, curJoystick); trackjoysticks.insert(tempJoystickID, curJoystick); // Force close of settings file. settings->endGroup(); delete settings; settings = 0; emit deviceAdded(curJoystick); } } } }
static mrb_value mrb_sdl2_joystick_joystick_get_guid_as_string(mrb_state *mrb, mrb_value self) { char result[33]; SDL_Joystick * joystick_p = mrb_sdl2_joystick_joystick_get_ptr(mrb, self); SDL_JoystickGUID joystick_guid = SDL_JoystickGetGUID(joystick_p); SDL_JoystickGetGUIDString(joystick_guid, result, 33); return mrb_str_new_cstr(mrb, result); }
SDL_JoystickType SDL_JoystickGetType(SDL_Joystick * joystick) { SDL_JoystickType type; SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick); type = SDL_GetJoystickGUIDType(guid); if (type == SDL_JOYSTICK_TYPE_UNKNOWN) { if (joystick && joystick->is_game_controller) { type = SDL_JOYSTICK_TYPE_GAMECONTROLLER; } } return type; }
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); }
const char* Gamepad::GetDeviceGUID (int id) { SDL_Joystick* joystick = SDL_GameControllerGetJoystick (gameControllers[id]); if (joystick) { char* guid = new char[64]; SDL_JoystickGetGUIDString (SDL_JoystickGetGUID (joystick), guid, 64); return guid; } return 0; }
std::string InputManager::getDeviceGUIDString(int deviceId) { if(deviceId == DEVICE_KEYBOARD) return KEYBOARD_GUID_STRING; auto it = mJoysticks.find(deviceId); if(it == mJoysticks.end()) { LOG(LogError) << "getDeviceGUIDString - deviceId " << deviceId << " not found!"; return "something went horribly wrong"; } char guid[65]; SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(it->second), guid, 65); return std::string(guid); }
QString GameController::getGUIDString() { QString temp; if (controller) { SDL_Joystick *joyhandle = SDL_GameControllerGetJoystick(controller); if (joyhandle) { SDL_JoystickGUID tempGUID = SDL_JoystickGetGUID(joyhandle); char guidString[65] = {'0'}; SDL_JoystickGetGUIDString(tempGUID, guidString, sizeof(guidString)); temp = QString(guidString); } } return temp; }
int SDLJBN_GetButtonNameAndColor(SDL_Joystick *joystick, SDL_GameControllerButton button, char *s, Uint8 *r, Uint8 *g, Uint8 *b) { SDLJBN_Init(); if (joystick == NULL) { err = "joystick is NULL"; return -1; } if (button < SDL_CONTROLLER_BUTTON_A || button >= SDL_CONTROLLER_BUTTON_MAX) { err = "button is invalid"; return -1; } // Use defaults first if (s) strcpy(s, jDefault.buttonNames[(int)button]); if (r) *r = jDefault.buttonColors[(int)button].r; if (g) *g = jDefault.buttonColors[(int)button].g; if (b) *b = jDefault.buttonColors[(int)button].b; const SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick); const char *joystickName = SDL_JoystickName(joystick); // Search for a matching GUID + joystickName in the db for (int i = 0; i < nJBN; i++) { const JoystickButtonNames *j = jbn + i; if (memcmp(&j->guid, &guid, sizeof guid) == 0 && strcmp(j->joystickName, joystickName) == 0) { // GUID and joystick name match; read name and colors if (s && strlen(j->buttonNames[(int)button]) > 0) strcpy(s, j->buttonNames[(int)button]); if (r) *r = j->buttonColors[(int)button].r; if (g) *g = j->buttonColors[(int)button].g; if (b) *b = j->buttonColors[(int)button].b; break; } } return 0; }
static void sdl_pad_connect(unsigned id) { sdl_joypad_t *pad = (sdl_joypad_t*)&sdl_pads[id]; bool success = false; int32_t product = 0; int32_t vendor = 0; settings_t *settings = config_get_ptr(); autoconfig_params_t params = {{0}}; #ifdef HAVE_SDL2 SDL_JoystickGUID guid; uint16_t *guid_ptr; if (SDL_IsGameController(id)) { pad->controller = SDL_GameControllerOpen(id); pad->joypad = SDL_GameControllerGetJoystick(pad->controller); success = pad->joypad != NULL && pad->controller != NULL; } else #endif { pad->joypad = SDL_JoystickOpen(id); success = pad->joypad != NULL; } if (!success) { RARCH_ERR("[SDL]: Couldn't open joystick #%u: %s.\n", id, SDL_GetError()); if (pad->joypad) SDL_JoystickClose(pad->joypad); pad->joypad = NULL; return; } strlcpy(settings->input.device_names[id], sdl_pad_name(id), sizeof(settings->input.device_names[id])); #ifdef HAVE_SDL2 guid = SDL_JoystickGetGUID(pad->joypad); guid_ptr = (uint16_t*)guid.data; #ifdef __linux vendor = guid_ptr[2]; product = guid_ptr[4]; #elif _WIN32 vendor = guid_ptr[0]; product = guid_ptr[1]; #endif #endif params.idx = id; strlcpy(params.name, sdl_pad_name(id), sizeof(params.name)); params.vid = vendor; params.pid = product; strlcpy(params.driver, sdl_joypad.ident, sizeof(params.driver)); input_config_autoconfigure_joypad(¶ms); RARCH_LOG("[SDL]: Device #%u (%04x:%04x) connected: %s.\n", id, vendor, product, sdl_pad_name(id)); #ifdef HAVE_SDL2 if (pad->controller) RARCH_LOG("[SDL]: Device #%u supports game controller api.\n", id); pad->haptic = g_has_haptic ? SDL_HapticOpenFromJoystick(pad->joypad) : NULL; if (g_has_haptic && !pad->haptic) RARCH_WARN("[SDL]: Couldn't open haptic device of the joypad #%u: %s\n", id, SDL_GetError()); pad->rumble_effect = -1; if (pad->haptic) { SDL_HapticEffect efx; efx.type = SDL_HAPTIC_LEFTRIGHT; efx.leftright.type = SDL_HAPTIC_LEFTRIGHT; efx.leftright.large_magnitude = efx.leftright.small_magnitude = 0x4000; efx.leftright.length = 5000; if (SDL_HapticEffectSupported(pad->haptic, &efx) == SDL_FALSE) { pad->rumble_effect = -2; RARCH_WARN("[SDL]: Device #%u does not support rumble.\n", id); } } #endif pad->num_axes = SDL_JoystickNumAxes(pad->joypad); pad->num_buttons = SDL_JoystickNumButtons(pad->joypad); pad->num_hats = SDL_JoystickNumHats(pad->joypad); #ifdef HAVE_SDL2 pad->num_balls = SDL_JoystickNumBalls(pad->joypad); RARCH_LOG("[SDL]: Device #%u has: %u axes, %u buttons, %u hats and %u trackballs.\n", id, pad->num_axes, pad->num_buttons, pad->num_hats, pad->num_balls); #else RARCH_LOG("[SDL]: Device #%u has: %u axes, %u buttons, %u hats.\n", id, pad->num_axes, pad->num_buttons, pad->num_hats); #endif }
static SDL_bool WatchJoystick(SDL_Joystick * joystick) { SDL_Window *window = NULL; SDL_Renderer *screen = NULL; SDL_Texture *background, *button, *axis, *marker; const char *name = NULL; SDL_bool retval = SDL_FALSE; SDL_bool done = SDL_FALSE, next=SDL_FALSE; SDL_Event event; SDL_Rect dst; int s, _s; Uint8 alpha=200, alpha_step = -1; Uint32 alpha_ticks; char mapping[4096], temp[4096]; MappingStep *step; MappingStep steps[] = { {342, 132, 0.0, MARKER_BUTTON, "x", -1, -1, -1, -1, ""}, {387, 167, 0.0, MARKER_BUTTON, "a", -1, -1, -1, -1, ""}, {431, 132, 0.0, MARKER_BUTTON, "b", -1, -1, -1, -1, ""}, {389, 101, 0.0, MARKER_BUTTON, "y", -1, -1, -1, -1, ""}, {174, 132, 0.0, MARKER_BUTTON, "back", -1, -1, -1, -1, ""}, {233, 132, 0.0, MARKER_BUTTON, "guide", -1, -1, -1, -1, ""}, {289, 132, 0.0, MARKER_BUTTON, "start", -1, -1, -1, -1, ""}, {116, 217, 0.0, MARKER_BUTTON, "dpleft", -1, -1, -1, -1, ""}, {154, 249, 0.0, MARKER_BUTTON, "dpdown", -1, -1, -1, -1, ""}, {186, 217, 0.0, MARKER_BUTTON, "dpright", -1, -1, -1, -1, ""}, {154, 188, 0.0, MARKER_BUTTON, "dpup", -1, -1, -1, -1, ""}, {77, 40, 0.0, MARKER_BUTTON, "leftshoulder", -1, -1, -1, -1, ""}, {91, 0, 0.0, MARKER_BUTTON, "lefttrigger", -1, -1, -1, -1, ""}, {396, 36, 0.0, MARKER_BUTTON, "rightshoulder", -1, -1, -1, -1, ""}, {375, 0, 0.0, MARKER_BUTTON, "righttrigger", -1, -1, -1, -1, ""}, {75, 154, 0.0, MARKER_BUTTON, "leftstick", -1, -1, -1, -1, ""}, {305, 230, 0.0, MARKER_BUTTON, "rightstick", -1, -1, -1, -1, ""}, {75, 154, 0.0, MARKER_AXIS, "leftx", -1, -1, -1, -1, ""}, {75, 154, 90.0, MARKER_AXIS, "lefty", -1, -1, -1, -1, ""}, {305, 230, 0.0, MARKER_AXIS, "rightx", -1, -1, -1, -1, ""}, {305, 230, 90.0, MARKER_AXIS, "righty", -1, -1, -1, -1, ""}, }; /* Create a window to display joystick axis position */ window = SDL_CreateWindow("Game Controller Map", 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; } background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE); button = LoadTexture(screen, "button.bmp", SDL_TRUE); axis = LoadTexture(screen, "axis.bmp", SDL_TRUE); SDL_RaiseWindow(window); /* scale for platforms that don't give you the window size you asked for. */ SDL_RenderSetLogicalSize(screen, SCREEN_WIDTH, SCREEN_HEIGHT); /* 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)); SDL_Log("\n\n\ ====================================================================================\n\ Press the buttons on your controller when indicated\n\ (Your controller may look different than the picture)\n\ If you want to correct a mistake, press backspace or the back button on your device\n\ To skip a button, press SPACE or click/touch the screen\n\ To exit, press ESC\n\ ====================================================================================\n"); /* Initialize mapping with GUID and name */ SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), temp, SDL_arraysize(temp)); SDL_snprintf(mapping, SDL_arraysize(mapping), "%s,%s,platform:%s,", temp, name ? name : "Unknown Joystick", SDL_GetPlatform()); /* Loop, getting joystick events! */ for(s=0; s<SDL_arraysize(steps) && !done;) { /* blank screen, set up for drawing this frame. */ step = &steps[s]; SDL_strlcpy(step->mapping, mapping, SDL_arraysize(step->mapping)); step->axis = -1; step->button = -1; step->hat = -1; step->hat_value = -1; SDL_SetClipboardText("TESTING TESTING 123"); switch(step->marker) { case MARKER_AXIS: marker = axis; break; case MARKER_BUTTON: marker = button; break; default: break; } dst.x = step->x; dst.y = step->y; SDL_QueryTexture(marker, NULL, NULL, &dst.w, &dst.h); next=SDL_FALSE; SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE); while (!done && !next) { if (SDL_GetTicks() - alpha_ticks > 5) { alpha_ticks = SDL_GetTicks(); alpha += alpha_step; if (alpha == 255) { alpha_step = -1; } if (alpha < 128) { alpha_step = 1; } } SDL_RenderClear(screen); SDL_RenderCopy(screen, background, NULL, NULL); SDL_SetTextureAlphaMod(marker, alpha); SDL_SetTextureColorMod(marker, 10, 255, 21); SDL_RenderCopyEx(screen, marker, NULL, &dst, step->angle, NULL, 0); SDL_RenderPresent(screen); if (SDL_PollEvent(&event)) { switch (event.type) { case SDL_JOYAXISMOTION: if (event.jaxis.value > 20000 || event.jaxis.value < -20000) { for (_s = 0; _s < s; _s++) { if (steps[_s].axis == event.jaxis.axis) { break; } } if (_s == s) { step->axis = event.jaxis.axis; SDL_strlcat(mapping, step->field, SDL_arraysize(mapping)); SDL_snprintf(temp, SDL_arraysize(temp), ":a%u,", event.jaxis.axis); SDL_strlcat(mapping, temp, SDL_arraysize(mapping)); s++; next=SDL_TRUE; } } break; case SDL_JOYHATMOTION: if (event.jhat.value == SDL_HAT_CENTERED) { break; /* ignore centering, we're probably just coming back to the center from the previous item we set. */ } for (_s = 0; _s < s; _s++) { if (steps[_s].hat == event.jhat.hat && steps[_s].hat_value == event.jhat.value) { break; } } if (_s == s) { step->hat = event.jhat.hat; step->hat_value = event.jhat.value; SDL_strlcat(mapping, step->field, SDL_arraysize(mapping)); SDL_snprintf(temp, SDL_arraysize(temp), ":h%u.%u,", event.jhat.hat, event.jhat.value ); SDL_strlcat(mapping, temp, SDL_arraysize(mapping)); s++; next=SDL_TRUE; } break; case SDL_JOYBALLMOTION: break; case SDL_JOYBUTTONUP: for (_s = 0; _s < s; _s++) { if (steps[_s].button == event.jbutton.button) { break; } } if (_s == s) { step->button = event.jbutton.button; SDL_strlcat(mapping, step->field, SDL_arraysize(mapping)); SDL_snprintf(temp, SDL_arraysize(temp), ":b%u,", event.jbutton.button); SDL_strlcat(mapping, temp, SDL_arraysize(mapping)); s++; next=SDL_TRUE; } break; case SDL_FINGERDOWN: case SDL_MOUSEBUTTONDOWN: /* Skip this step */ s++; next=SDL_TRUE; break; case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_BACKSPACE || event.key.keysym.sym == SDLK_AC_BACK) { /* Undo! */ if (s > 0) { SDL_strlcpy(mapping, step->mapping, SDL_arraysize(step->mapping)); s--; next = SDL_TRUE; } break; } if (event.key.keysym.sym == SDLK_SPACE) { /* Skip this step */ s++; next=SDL_TRUE; break; } if ((event.key.keysym.sym != SDLK_ESCAPE)) { break; } /* Fall through to signal quit */ case SDL_QUIT: done = SDL_TRUE; break; default: break; } } } } if (s == SDL_arraysize(steps) ) { SDL_Log("Mapping:\n\n%s\n\n", mapping); /* Print to stdout as well so the user can cat the output somewhere */ printf("%s\n", mapping); } while(SDL_PollEvent(&event)) {}; SDL_DestroyRenderer(screen); SDL_DestroyWindow(window); return retval; }
int main(int argc, char *argv[]) { const char *name; int i; SDL_Joystick *joystick; /* Enable standard application logging */ SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); /* Initialize SDL (Note: video is required to start event loop) */ if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError()); exit(1); } /* Print information about the joysticks */ SDL_Log("There are %d joysticks attached\n", SDL_NumJoysticks()); for (i = 0; i < SDL_NumJoysticks(); ++i) { name = SDL_JoystickNameForIndex(i); SDL_Log("Joystick %d: %s\n", i, name ? name : "Unknown Joystick"); joystick = SDL_JoystickOpen(i); if (joystick == NULL) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_JoystickOpen(%d) failed: %s\n", i, SDL_GetError()); } else { char guid[64]; SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), guid, sizeof (guid)); SDL_Log(" axes: %d\n", SDL_JoystickNumAxes(joystick)); SDL_Log(" balls: %d\n", SDL_JoystickNumBalls(joystick)); SDL_Log(" hats: %d\n", SDL_JoystickNumHats(joystick)); SDL_Log(" buttons: %d\n", SDL_JoystickNumButtons(joystick)); SDL_Log("instance id: %d\n", SDL_JoystickInstanceID(joystick)); SDL_Log(" guid: %s\n", guid); SDL_JoystickClose(joystick); } } #ifdef ANDROID if (SDL_NumJoysticks() > 0) { #else if (argv[1]) { #endif SDL_bool reportederror = SDL_FALSE; SDL_bool keepGoing = SDL_TRUE; SDL_Event event; int device; #ifdef ANDROID device = 0; #else device = atoi(argv[1]); #endif joystick = SDL_JoystickOpen(device); while ( keepGoing ) { if (joystick == NULL) { if ( !reportederror ) { SDL_Log("Couldn't open joystick %d: %s\n", device, SDL_GetError()); keepGoing = SDL_FALSE; reportederror = SDL_TRUE; } } else { reportederror = SDL_FALSE; keepGoing = WatchJoystick(joystick); SDL_JoystickClose(joystick); } joystick = NULL; if (keepGoing) { SDL_Log("Waiting for attach\n"); } while (keepGoing) { SDL_WaitEvent(&event); if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN) || (event.type == SDL_MOUSEBUTTONDOWN)) { keepGoing = SDL_FALSE; } else if (event.type == SDL_JOYDEVICEADDED) { joystick = SDL_JoystickOpen(device); break; } } } } else { SDL_Log("\n\nUsage: ./controllermap number\nFor example: ./controllermap 0\nOr: ./controllermap 0 >> gamecontrollerdb.txt"); } SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK); return 0; } #else int main(int argc, char *argv[]) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n"); exit(1); }
int main(int argc, char *argv[]) { // Get path char *ip = NULL; unsigned short connect_port = 0; unsigned short listen_port = 0; int net_mode = NET_MODE_NONE; int ret = 0; // Path manager if(pm_init() != 0) { err_msgbox(pm_get_errormsg()); return 1; } // Check arguments if(argc >= 2) { if(strcmp(argv[1], "-v") == 0) { printf("OpenOMF v%d.%d.%d\n", V_MAJOR, V_MINOR, V_PATCH); printf("Source available at https://github.com/omf2097/ under MIT License\n"); printf("(C) 2097 Tuomas Virtanen, Andrew Thompson, Hunter and others\n"); goto exit_0; } else if(strcmp(argv[1], "-h") == 0) { printf("Arguments:\n"); printf("-h Prints this help\n"); printf("-c [ip] [port] Connect to server\n"); printf("-l [port] Start server\n"); goto exit_0; } else if(strcmp(argv[1], "-c") == 0) { if(argc >= 3) { ip = strcpy(malloc(strlen(argv[2])+1), argv[2]); } if(argc >= 4) { connect_port = atoi(argv[3]); } net_mode = NET_MODE_CLIENT; } else if(strcmp(argv[1], "-l") == 0) { if(argc >= 3) { listen_port = atoi(argv[2]); } net_mode = NET_MODE_SERVER; } } // Init log #if defined(DEBUGMODE) || defined(STANDALONE_SERVER) if(log_init(0)) { err_msgbox("Error while initializing log!"); goto exit_0; } #else if(log_init(pm_get_local_path(LOG_PATH))) { err_msgbox("Error while initializing log '%s'!", pm_get_local_path(LOG_PATH)); goto exit_0; } #endif // Simple header INFO("Starting OpenOMF v%d.%d.%d", V_MAJOR, V_MINOR, V_PATCH); // Dump pathmanager log pm_log(); // Random seed rand_seed(time(NULL)); // Init stringparser sd_stringparser_lib_init(); // Init config if(settings_init(pm_get_local_path(CONFIG_PATH))) { err_msgbox("Failed to initialize settings file"); goto exit_1; } settings_load(); // Find plugins and make sure they are valid plugins_init(); // Network game override stuff if(ip) { DEBUG("Connect IP overridden to %s", ip); settings_get()->net.net_connect_ip = ip; } if(connect_port > 0 && connect_port < 0xFFFF) { DEBUG("Connect Port overridden to %u", connect_port&0xFFFF); settings_get()->net.net_connect_port = connect_port; } if(listen_port > 0 && listen_port < 0xFFFF) { DEBUG("Listen Port overridden to %u", listen_port&0xFFFF); settings_get()->net.net_listen_port = listen_port; } // Init SDL2 unsigned int sdl_flags = SDL_INIT_TIMER; #ifndef STANDALONE_SERVER sdl_flags |= SDL_INIT_VIDEO; #endif if(SDL_Init(sdl_flags)) { err_msgbox("SDL2 Initialization failed: %s", SDL_GetError()); goto exit_2; } SDL_version sdl_linked; SDL_GetVersion(&sdl_linked); INFO("Found SDL v%d.%d.%d", sdl_linked.major, sdl_linked.minor, sdl_linked.patch); INFO("Running on platform: %s", SDL_GetPlatform()); #ifndef STANDALONE_SERVER if(SDL_InitSubSystem(SDL_INIT_JOYSTICK|SDL_INIT_GAMECONTROLLER|SDL_INIT_HAPTIC)) { err_msgbox("SDL2 Initialization failed: %s", SDL_GetError()); goto exit_2; } // Attempt to find gamecontrollerdb.txt, either from resources or from // built-in header SDL_RWops *rw = SDL_RWFromConstMem(gamecontrollerdb, strlen(gamecontrollerdb)); SDL_GameControllerAddMappingsFromRW(rw, 1); char *gamecontrollerdbpath = malloc(128); snprintf(gamecontrollerdbpath, 128, "%s/gamecontrollerdb.txt", pm_get_local_path(RESOURCE_PATH)); int mappings_loaded = SDL_GameControllerAddMappingsFromFile(gamecontrollerdbpath); if (mappings_loaded > 0) { DEBUG("loaded %d mappings from %s", mappings_loaded, gamecontrollerdbpath); } free(gamecontrollerdbpath); // Load up joysticks INFO("Found %d joysticks attached", SDL_NumJoysticks()); SDL_Joystick *joy; char guidstr[33]; for (int i = 0; i < SDL_NumJoysticks(); i++) { joy = SDL_JoystickOpen(i); if (joy) { SDL_JoystickGUID guid = SDL_JoystickGetGUID(joy); SDL_JoystickGetGUIDString(guid, guidstr, 33); INFO("Opened Joystick %d", i); INFO(" * Name: %s", SDL_JoystickNameForIndex(i)); INFO(" * Number of Axes: %d", SDL_JoystickNumAxes(joy)); INFO(" * Number of Buttons: %d", SDL_JoystickNumButtons(joy)); INFO(" * Number of Balls: %d", SDL_JoystickNumBalls(joy)); INFO(" * Number of Hats: %d", SDL_JoystickNumHats(joy)); INFO(" * GUID : %s", guidstr); } else { INFO("Joystick %d is unsupported", i); } if (SDL_JoystickGetAttached(joy)) { SDL_JoystickClose(joy); } } // Init libDumb dumb_register_stdfiles(); #endif // Init enet if(enet_initialize() != 0) { err_msgbox("Failed to initialize enet"); goto exit_3; } // Initialize engine if(engine_init()) { err_msgbox("Failed to initialize game engine."); goto exit_4; } // Run engine_run(net_mode); // Close everything engine_close(); exit_4: enet_deinitialize(); exit_3: SDL_Quit(); exit_2: dumb_exit(); settings_save(); settings_free(); exit_1: sd_stringparser_lib_deinit(); INFO("Exit."); log_close(); exit_0: if (ip) { free(ip); } plugins_close(); pm_free(); return ret; }
void InputDaemon::refreshJoysticks() { #ifdef USE_SDL_2 QHashIterator<SDL_JoystickID, InputDevice*> iter(*joysticks); #else QHashIterator<int, InputDevice*> iter(*joysticks); #endif while (iter.hasNext()) { InputDevice *joystick = iter.next().value(); if (joystick) { delete joystick; joystick = 0; } } joysticks->clear(); #ifdef USE_SDL_2 trackjoysticks.clear(); trackcontrollers.clear(); #endif #ifdef USE_SDL_2 QSettings settings(PadderCommon::configFilePath, QSettings::IniFormat); settings.beginGroup("Mappings"); #endif for (int i=0; i < SDL_NumJoysticks(); i++) { #ifdef USE_SDL_2 SDL_Joystick *joystick = SDL_JoystickOpen(i); QString temp; SDL_JoystickGUID tempGUID = SDL_JoystickGetGUID(joystick); char guidString[65] = {'0'}; SDL_JoystickGetGUIDString(tempGUID, guidString, sizeof(guidString)); temp = QString(guidString); bool disableGameController = settings.value(QString("%1Disable").arg(temp), false).toBool(); if (SDL_IsGameController(i) && !disableGameController) { SDL_GameController *controller = SDL_GameControllerOpen(i); GameController *damncontroller = new GameController(controller, i, this); SDL_Joystick *sdlStick = SDL_GameControllerGetJoystick(controller); SDL_JoystickID joystickID = SDL_JoystickInstanceID(sdlStick); joysticks->insert(joystickID, damncontroller); trackcontrollers.insert(joystickID, damncontroller); } else { Joystick *curJoystick = new Joystick(joystick, i, this); SDL_JoystickID joystickID = SDL_JoystickInstanceID(joystick); joysticks->insert(joystickID, curJoystick); trackjoysticks.insert(joystickID, curJoystick); } #else SDL_Joystick *joystick = SDL_JoystickOpen(i); Joystick *curJoystick = new Joystick(joystick, i, this); joysticks->insert(i, curJoystick); #endif } #ifdef USE_SDL_2 settings.endGroup(); #endif emit joysticksRefreshed(joysticks); }
joystick_guid guid() const noexcept { return SDL_JoystickGetGUID(ptr.get()); }
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"); } }
void InputDaemon::refreshJoysticks() { QMapIterator<SDL_JoystickID, InputDevice*> iter(*joysticks); while (iter.hasNext()) { InputDevice *joystick = iter.next().value(); if (joystick) { delete joystick; joystick = 0; } } joysticks->clear(); #ifdef USE_SDL_2 trackjoysticks.clear(); trackcontrollers.clear(); #endif #ifdef USE_SDL_2 settings->getLock()->lock(); settings->beginGroup("Mappings"); #endif for (int i=0; i < SDL_NumJoysticks(); i++) { #ifdef USE_SDL_2 SDL_Joystick *joystick = SDL_JoystickOpen(i); QString temp; SDL_JoystickGUID tempGUID = SDL_JoystickGetGUID(joystick); char guidString[65] = {'0'}; SDL_JoystickGetGUIDString(tempGUID, guidString, sizeof(guidString)); temp = QString(guidString); bool disableGameController = settings->value(QString("%1Disable").arg(temp), false).toBool(); if (SDL_IsGameController(i) && !disableGameController) { SDL_GameController *controller = SDL_GameControllerOpen(i); GameController *damncontroller = new GameController(controller, i, settings, this); connect(damncontroller, SIGNAL(requestWait()), eventWorker, SLOT(haltServices())); SDL_Joystick *sdlStick = SDL_GameControllerGetJoystick(controller); SDL_JoystickID joystickID = SDL_JoystickInstanceID(sdlStick); joysticks->insert(joystickID, damncontroller); trackcontrollers.insert(joystickID, damncontroller); } else { Joystick *curJoystick = new Joystick(joystick, i, settings, this); connect(curJoystick, SIGNAL(requestWait()), eventWorker, SLOT(haltServices())); SDL_JoystickID joystickID = SDL_JoystickInstanceID(joystick); joysticks->insert(joystickID, curJoystick); trackjoysticks.insert(joystickID, curJoystick); } #else SDL_Joystick *joystick = SDL_JoystickOpen(i); Joystick *curJoystick = new Joystick(joystick, i, settings, this); connect(curJoystick, SIGNAL(requestWait()), eventWorker, SLOT(haltServices())); joysticks->insert(i, curJoystick); #endif } #ifdef USE_SDL_2 settings->endGroup(); settings->getLock()->unlock(); #endif emit joysticksRefreshed(joysticks); }
void InputDaemon::addInputDevice(int index) { SDL_Joystick *joystick = SDL_JoystickOpen(index); if (joystick) { SDL_JoystickID tempJoystickID = SDL_JoystickInstanceID(joystick); if (!joysticks->contains(tempJoystickID)) { settings->getLock()->lock(); settings->beginGroup("Mappings"); QString temp; SDL_JoystickGUID tempGUID = SDL_JoystickGetGUID(joystick); char guidString[65] = {'0'}; SDL_JoystickGetGUIDString(tempGUID, guidString, sizeof(guidString)); temp = QString(guidString); bool disableGameController = settings->value(QString("%1Disable").arg(temp), false).toBool(); if (SDL_IsGameController(index) && !disableGameController) { // Make sure to decrement reference count SDL_JoystickClose(joystick); SDL_GameController *controller = SDL_GameControllerOpen(index); if (controller) { SDL_Joystick *sdlStick = SDL_GameControllerGetJoystick(controller); SDL_JoystickID tempJoystickID = SDL_JoystickInstanceID(sdlStick); if (!joysticks->contains(tempJoystickID)) { GameController *damncontroller = new GameController(controller, index, settings, this); connect(damncontroller, SIGNAL(requestWait()), eventWorker, SLOT(haltServices())); joysticks->insert(tempJoystickID, damncontroller); trackcontrollers.insert(tempJoystickID, damncontroller); settings->endGroup(); settings->getLock()->unlock(); emit deviceAdded(damncontroller); } } else { settings->endGroup(); settings->getLock()->unlock(); } } else { Joystick *curJoystick = new Joystick(joystick, index, settings, this); joysticks->insert(tempJoystickID, curJoystick); trackjoysticks.insert(tempJoystickID, curJoystick); settings->endGroup(); settings->getLock()->unlock(); emit deviceAdded(curJoystick); } } else { // Make sure to decrement reference count SDL_JoystickClose(joystick); } } }
int main(int argc, char *argv[]) { const char *name; int i; SDL_Joystick *joystick; /* Initialize SDL (Note: video is required to start event loop) */ if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) { fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); exit(1); } /* Print information about the joysticks */ printf("There are %d joysticks attached\n", SDL_NumJoysticks()); for (i = 0; i < SDL_NumJoysticks(); ++i) { name = SDL_JoystickNameForIndex(i); printf("Joystick %d: %s\n", i, name ? name : "Unknown Joystick"); joystick = SDL_JoystickOpen(i); if (joystick == NULL) { fprintf(stderr, "SDL_JoystickOpen(%d) failed: %s\n", i, SDL_GetError()); } else { char guid[64]; SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), guid, sizeof (guid)); printf(" axes: %d\n", SDL_JoystickNumAxes(joystick)); printf(" balls: %d\n", SDL_JoystickNumBalls(joystick)); printf(" hats: %d\n", SDL_JoystickNumHats(joystick)); printf(" buttons: %d\n", SDL_JoystickNumButtons(joystick)); printf("instance id: %d\n", SDL_JoystickInstanceID(joystick)); printf(" guid: %s\n", guid); SDL_JoystickClose(joystick); } } #ifdef ANDROID if (SDL_NumJoysticks() > 0) { #else if (argv[1]) { #endif SDL_bool reportederror = SDL_FALSE; SDL_bool keepGoing = SDL_TRUE; SDL_Event event; #ifdef ANDROID joystick = SDL_JoystickOpen(0); #else joystick = SDL_JoystickOpen(atoi(argv[1])); #endif while ( keepGoing ) { if (joystick == NULL) { if ( !reportederror ) { printf("Couldn't open joystick %d: %s\n", atoi(argv[1]), SDL_GetError()); keepGoing = SDL_FALSE; reportederror = SDL_TRUE; } } else { reportederror = SDL_FALSE; keepGoing = WatchJoystick(joystick); SDL_JoystickClose(joystick); } joystick = NULL; if (keepGoing) { printf("Waiting for attach\n"); } while (keepGoing) { SDL_WaitEvent(&event); if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN) || (event.type == SDL_MOUSEBUTTONDOWN)) { keepGoing = SDL_FALSE; } else if (event.type == SDL_JOYDEVICEADDED) { joystick = SDL_JoystickOpen(atoi(argv[1])); break; } } } } SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK); #ifdef ANDROID exit(0); #else return 0; #endif } #else int main(int argc, char *argv[]) { fprintf(stderr, "SDL compiled without Joystick support.\n"); exit(1); }
void InputDriver::update() { SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_JOYDEVICEADDED: { auto joystick = SDL_JoystickOpen(event.jdevice.which); auto guid = SDL_JoystickGetGUID(joystick); auto instanceId = SDL_JoystickInstanceID(joystick); { std::unique_lock lock { mConfigurationMutex }; auto connected = ConnectedJoystick { }; connected.joystick = joystick; connected.guid = guid; connected.instanceId = instanceId; connected.duplicateId = 0; for (auto &others : mJoysticks) { if (others.guid == connected.guid) { connected.duplicateId = std::max(connected.duplicateId, others.duplicateId + 1); } } for (auto &controller : mConfiguration.controllers) { for (auto &input : controller.inputs) { if (input.joystickGuid == guid && input.joystickDuplicateId == connected.duplicateId) { input.joystick = joystick; input.joystickInstanceId = instanceId; } } } mJoysticks.push_back(connected); } emit joystickConnected(instanceId, guid, SDL_JoystickName(joystick)); break; } case SDL_JOYDEVICEREMOVED: { auto joystick = SDL_JoystickFromInstanceID(event.jdevice.which); if (joystick) { joystickDisconnected(SDL_JoystickInstanceID(joystick), SDL_JoystickGetGUID(joystick)); { std::unique_lock lock { mConfigurationMutex }; for (auto itr = mJoysticks.begin(); itr != mJoysticks.end(); ++itr) { if (itr->joystick == joystick) { itr = mJoysticks.erase(itr); } } for (auto &controller : mConfiguration.controllers) { for (auto &input : controller.inputs) { if (input.joystick == joystick) { input.joystick = nullptr; } } } } SDL_JoystickClose(joystick); } break; } case SDL_JOYBUTTONDOWN: { if (mButtonEventsEnabled) { auto joystick = SDL_JoystickFromInstanceID(event.jdevice.which); if (joystick) { joystickButtonDown(SDL_JoystickInstanceID(joystick), SDL_JoystickGetGUID(joystick), event.jbutton.button); } } break; } case SDL_JOYAXISMOTION: { if (mButtonEventsEnabled) { auto joystick = SDL_JoystickFromInstanceID(event.jdevice.which); if (joystick) { joystickAxisMotion(SDL_JoystickInstanceID(joystick), SDL_JoystickGetGUID(joystick), event.jaxis.axis, translateAxisValue(event.jaxis.value)); } } break; } case SDL_JOYHATMOTION: { if (mButtonEventsEnabled) { auto joystick = SDL_JoystickFromInstanceID(event.jdevice.which); if (joystick) { joystickHatMotion(SDL_JoystickInstanceID(joystick), SDL_JoystickGetGUID(joystick), event.jhat.hat, event.jhat.value); } } break; } // I don't think we actually care about game controllers? case SDL_CONTROLLERDEVICEADDED: qDebug("SDL_CONTROLLERDEVICEADDED"); break; case SDL_CONTROLLERDEVICEREMAPPED: qDebug("SDL_CONTROLLERDEVICEREMAPPED"); break; case SDL_CONTROLLERDEVICEREMOVED: qDebug("SDL_CONTROLLERDEVICEREMOVED"); break; case SDL_CONTROLLERBUTTONDOWN: qDebug("SDL_CONTROLLERBUTTONDOWN"); break; } } QTimer::singleShot(10, Qt::PreciseTimer, this, SLOT(update())); }
static void sdl_pad_connect(unsigned id) { sdl_joypad_t *pad = (sdl_joypad_t*)&sdl_pads[id]; bool success = false; int32_t product = 0; int32_t vendor = 0; #ifdef HAVE_SDL2 SDL_JoystickGUID guid; uint16_t *guid_ptr; if (SDL_IsGameController(id)) { pad->controller = SDL_GameControllerOpen(id); pad->joypad = SDL_GameControllerGetJoystick(pad->controller); success = pad->joypad != NULL && pad->controller != NULL; } else #endif { pad->joypad = SDL_JoystickOpen(id); success = pad->joypad != NULL; } if (!success) { RARCH_ERR("[SDL]: Couldn't open joystick #%u: %s.\n", id, SDL_GetError()); if (pad->joypad) SDL_JoystickClose(pad->joypad); pad->joypad = NULL; return; } #ifdef HAVE_SDL2 guid = SDL_JoystickGetGUID(pad->joypad); guid_ptr = (uint16_t*)guid.data; #ifdef __linux vendor = guid_ptr[2]; product = guid_ptr[4]; #elif _WIN32 vendor = guid_ptr[0]; product = guid_ptr[1]; #endif #endif if (!input_autoconfigure_connect( sdl_joypad_name(id), NULL, sdl_joypad.ident, id, vendor, product)) input_config_set_device_name(id, sdl_joypad_name(id)); RARCH_LOG("[SDL]: Device #%u (%04x:%04x) connected: %s.\n", id, vendor, product, sdl_joypad_name(id)); #ifdef HAVE_SDL2 if (pad->controller) { /* SDL_GameController internally supports all axis/button IDs, even if * the controller's mapping does not have a binding for it. * * So, we can claim to support all axes/buttons, and when we try to poll * an unbound ID, SDL simply returns the correct unpressed value. * * Note that, in addition to 0 trackballs, we also have 0 hats. This is * because the d-pad is in the button list, as the last 4 enum entries. * * -flibit */ pad->num_axes = SDL_CONTROLLER_AXIS_MAX; pad->num_buttons = SDL_CONTROLLER_BUTTON_MAX; pad->num_hats = 0; pad->num_balls = 0; RARCH_LOG("[SDL]: Device #%u supports game controller api.\n", id); } else { pad->num_axes = SDL_JoystickNumAxes(pad->joypad); pad->num_buttons = SDL_JoystickNumButtons(pad->joypad); pad->num_hats = SDL_JoystickNumHats(pad->joypad); pad->num_balls = SDL_JoystickNumBalls(pad->joypad); RARCH_LOG("[SDL]: Device #%u has: %u axes, %u buttons, %u hats and %u trackballs.\n", id, pad->num_axes, pad->num_buttons, pad->num_hats, pad->num_balls); } pad->haptic = g_has_haptic ? SDL_HapticOpenFromJoystick(pad->joypad) : NULL; if (g_has_haptic && !pad->haptic) RARCH_WARN("[SDL]: Couldn't open haptic device of the joypad #%u: %s\n", id, SDL_GetError()); pad->rumble_effect = -1; if (pad->haptic) { SDL_HapticEffect efx; efx.type = SDL_HAPTIC_LEFTRIGHT; efx.leftright.type = SDL_HAPTIC_LEFTRIGHT; efx.leftright.large_magnitude = efx.leftright.small_magnitude = 0x4000; efx.leftright.length = 5000; if (SDL_HapticEffectSupported(pad->haptic, &efx) == SDL_FALSE) { pad->rumble_effect = -2; RARCH_WARN("[SDL]: Device #%u does not support rumble.\n", id); } } #else pad->num_axes = SDL_JoystickNumAxes(pad->joypad); pad->num_buttons = SDL_JoystickNumButtons(pad->joypad); pad->num_hats = SDL_JoystickNumHats(pad->joypad); RARCH_LOG("[SDL]: Device #%u has: %u axes, %u buttons, %u hats.\n", id, pad->num_axes, pad->num_buttons, pad->num_hats); #endif }