/* * Open a joystick for use - the index passed as an argument refers to * the N'th joystick on the system. This index is the value which will * identify this joystick in future joystick events. * * This function returns a joystick identifier, or NULL if an error occurred. */ SDL_Joystick * SDL_JoystickOpen(int device_index) { SDL_Joystick *joystick; SDL_Joystick *joysticklist; const char *joystickname = NULL; if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) { SDL_SetError("There are %d joysticks available", SDL_NumJoysticks()); return (NULL); } joysticklist = SDL_joysticks; /* If the joystick is already open, return it * it is important that we have a single joystick * for each instance id */ while (joysticklist) { if (SDL_SYS_GetInstanceIdOfDeviceIndex(device_index) == joysticklist->instance_id) { joystick = joysticklist; ++joystick->ref_count; return (joystick); } joysticklist = joysticklist->next; } /* Create and initialize the joystick */ joystick = (SDL_Joystick *) SDL_malloc((sizeof *joystick)); if (joystick == NULL) { SDL_OutOfMemory(); return NULL; } SDL_memset(joystick, 0, (sizeof *joystick)); if (SDL_SYS_JoystickOpen(joystick, device_index) < 0) { SDL_free(joystick); return NULL; } joystickname = SDL_SYS_JoystickNameForDeviceIndex(device_index); if (joystickname) joystick->name = SDL_strdup(joystickname); else joystick->name = NULL; if (joystick->naxes > 0) { joystick->axes = (Sint16 *) SDL_malloc (joystick->naxes * sizeof(Sint16)); } if (joystick->nhats > 0) { joystick->hats = (Uint8 *) SDL_malloc (joystick->nhats * sizeof(Uint8)); } if (joystick->nballs > 0) { joystick->balls = (struct balldelta *) SDL_malloc (joystick->nballs * sizeof(*joystick->balls)); } if (joystick->nbuttons > 0) { joystick->buttons = (Uint8 *) SDL_malloc (joystick->nbuttons * sizeof(Uint8)); } if (((joystick->naxes > 0) && !joystick->axes) || ((joystick->nhats > 0) && !joystick->hats) || ((joystick->nballs > 0) && !joystick->balls) || ((joystick->nbuttons > 0) && !joystick->buttons)) { SDL_OutOfMemory(); SDL_JoystickClose(joystick); return NULL; } if (joystick->axes) { SDL_memset(joystick->axes, 0, joystick->naxes * sizeof(Sint16)); } if (joystick->hats) { SDL_memset(joystick->hats, 0, joystick->nhats * sizeof(Uint8)); } if (joystick->balls) { SDL_memset(joystick->balls, 0, joystick->nballs * sizeof(*joystick->balls)); } if (joystick->buttons) { SDL_memset(joystick->buttons, 0, joystick->nbuttons * sizeof(Uint8)); } joystick->epowerlevel = SDL_JOYSTICK_POWER_UNKNOWN; /* Add joystick to list */ ++joystick->ref_count; /* Link the joystick in the list */ joystick->next = SDL_joysticks; SDL_joysticks = joystick; SDL_SYS_JoystickUpdate(joystick); return (joystick); }
static void IN_InitJoystick(void) { int i = 0; int total = 0; char buf[16384] = ""; if (stick != NULL) { SDL_JoystickClose(stick); } stick = NULL; memset(&stick_state, '\0', sizeof(stick_state)); if (!SDL_WasInit(SDL_INIT_JOYSTICK)) { Com_Printf("Initializing joystick devices\n"); if (SDL_Init(SDL_INIT_JOYSTICK) < 0) { Com_Printf("SDL_Init(SDL_INIT_JOYSTICK) failed: %s\n", SDL_GetError()); return; } Com_Printf("...joysticks initialized\n"); } total = SDL_NumJoysticks(); Com_Printf("...available joysticks: %d\n", total); // Print list and build cvar to allow ui to select joystick. for (i = 0; i < total; i++) { Q_strcat(buf, sizeof(buf), SDL_JoystickNameForIndex(i)); Q_strcat(buf, sizeof(buf), "\n"); } Cvar_Get("in_availableJoysticks", buf, CVAR_ROM); if (!in_joystick->integer) { Com_Printf("...no active joystick set\n"); SDL_QuitSubSystem(SDL_INIT_JOYSTICK); return; } in_joystickNo = Cvar_Get("in_joystickNo", "0", CVAR_ARCHIVE); if (in_joystickNo->integer < 0 || in_joystickNo->integer >= total) { Cvar_Set("in_joystickNo", "0"); } in_joystickUseAnalog = Cvar_Get("in_joystickUseAnalog", "0", CVAR_ARCHIVE); stick = SDL_JoystickOpen(in_joystickNo->integer); if (stick == NULL) { Com_Printf("No joystick opened.\n"); return; } Com_DPrintf("Joystick %d opened\n", in_joystickNo->integer); Com_DPrintf("Name: %s\n", SDL_JoystickNameForIndex(in_joystickNo->integer)); Com_DPrintf("Axes: %d\n", SDL_JoystickNumAxes(stick)); Com_DPrintf("Hats: %d\n", SDL_JoystickNumHats(stick)); Com_DPrintf("Buttons: %d\n", SDL_JoystickNumButtons(stick)); Com_DPrintf("Balls: %d\n", SDL_JoystickNumBalls(stick)); Com_DPrintf("Use Analog: %s\n", in_joystickUseAnalog->integer ? "Yes" : "No"); SDL_JoystickEventState(SDL_QUERY); }
void Joystick::unLoadJoystick () { if (SDL_NumJoysticks() >= 1) { SDL_JoystickClose(joystick); } }
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 }
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 }
/** * Initializes SDL and the joystick. * * Function exits with -1 if fails * * @param device_index string integer of desired joystick device * * @returns 0 on success */ int nps_radio_control_joystick_init(const char* device) { nps_joystick.throttle = 0.5; nps_joystick.roll = 0.; nps_joystick.pitch = 0.; nps_joystick.yaw = 0.; nps_joystick.mode = MODE_SWITCH_AUTO2; //Convert device index string to integer int device_index = atoi(device); //Initialize SDL with joystick support and event support (through video) if (SDL_Init(SDL_INIT_JOYSTICK|SDL_INIT_VIDEO) < 0) { printf("Could not initialize SDL: %s.\n", SDL_GetError()); exit(-1); } //Quit SDL at exit atexit(SDL_Quit); //Start the event handler, disable all but joystick button events and quit handler SDL_EventState(SDL_ACTIVEEVENT,SDL_IGNORE); SDL_EventState(SDL_KEYDOWN,SDL_IGNORE); SDL_EventState(SDL_KEYUP,SDL_IGNORE); SDL_EventState(SDL_MOUSEMOTION,SDL_IGNORE); SDL_EventState(SDL_MOUSEBUTTONDOWN,SDL_IGNORE); SDL_EventState(SDL_MOUSEBUTTONUP,SDL_IGNORE); SDL_EventState(SDL_JOYAXISMOTION,SDL_IGNORE); SDL_EventState(SDL_JOYBALLMOTION,SDL_IGNORE); SDL_EventState(SDL_JOYHATMOTION,SDL_IGNORE); //SDL_EventState(SDL_JOYBUTTONDOWN,SDL_IGNORE); SDL_EventState(SDL_JOYBUTTONUP,SDL_IGNORE); SDL_EventState(SDL_VIDEORESIZE,SDL_IGNORE); SDL_EventState(SDL_VIDEOEXPOSE,SDL_IGNORE); //SDL_EventState(SDL_QUIT,SDL_IGNORE); SDL_EventState(SDL_USEREVENT,SDL_IGNORE); SDL_EventState(SDL_SYSWMEVENT,SDL_IGNORE); //Check there are actually joysticks attached if (!SDL_NumJoysticks()) { printf("No joysticks attached! Quitting.\n"); exit(-1); } //Open the desired joystick and make sure it will work sdl_joystick = SDL_JoystickOpen(device_index); if (!sdl_joystick) { printf("Joystick corresponding to SDL Index %d failed to open! Exiting.\n", device_index); exit(-1); } else if (SDL_JoystickNumAxes(sdl_joystick) < JS_NB_AXIS) { printf("Selected joystick does not support enough axes!\n"); printf("Number of axes required: %i\n", JS_NB_AXIS); printf("Number of axes available: %i\n",SDL_JoystickNumAxes(sdl_joystick)); SDL_JoystickClose(sdl_joystick); exit(-1); } else if (SDL_JoystickNumButtons(sdl_joystick) < JS_NB_BUTTONS) { printf("Selected joystick does not support enough buttons!\n"); printf("Buttons supported: %d needed: %d\n", SDL_JoystickNumButtons(sdl_joystick), JS_NB_BUTTONS); SDL_JoystickClose(sdl_joystick); exit(-1); } else { printf("Using joystick named: %s\n",SDL_JoystickName(device_index)); } return 0; }
void CWinsys::CloseJoystick () { if (joystick_active) SDL_JoystickClose (joystick); }
SDLInputState::~SDLInputState() { if (joy) SDL_JoystickClose(joy); }
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[]) { const char *name, *type; int i; SDL_Joystick *joystick; SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0"); /* 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_assert(SDL_JoystickFromInstanceID(SDL_JoystickInstanceID(joystick)) == joystick); SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), guid, sizeof (guid)); switch (SDL_JoystickGetType(joystick)) { case SDL_JOYSTICK_TYPE_GAMECONTROLLER: type = "Game Controller"; break; case SDL_JOYSTICK_TYPE_WHEEL: type = "Wheel"; break; case SDL_JOYSTICK_TYPE_ARCADE_STICK: type = "Arcade Stick"; break; case SDL_JOYSTICK_TYPE_FLIGHT_STICK: type = "Flight Stick"; break; case SDL_JOYSTICK_TYPE_DANCE_PAD: type = "Dance Pad"; break; case SDL_JOYSTICK_TYPE_GUITAR: type = "Guitar"; break; case SDL_JOYSTICK_TYPE_DRUM_KIT: type = "Drum Kit"; break; case SDL_JOYSTICK_TYPE_ARCADE_PAD: type = "Arcade Pad"; break; case SDL_JOYSTICK_TYPE_THROTTLE: type = "Throttle"; break; default: type = "Unknown"; break; } SDL_Log(" type: %s\n", type); 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_Log(" VID/PID: 0x%.4x/0x%.4x\n", SDL_JoystickGetVendor(joystick), SDL_JoystickGetProduct(joystick)); SDL_JoystickClose(joystick); } } #if defined(__ANDROID__) || defined(__IPHONEOS__) if (SDL_NumJoysticks() > 0) { #else if (argv[1]) { #endif SDL_bool reportederror = SDL_FALSE; SDL_bool keepGoing = SDL_TRUE; SDL_Event event; int device; #if defined(__ANDROID__) || defined(__IPHONEOS__) device = 0; #else device = atoi(argv[1]); #endif joystick = SDL_JoystickOpen(device); if (joystick != NULL) { SDL_assert(SDL_JoystickFromInstanceID(SDL_JoystickInstanceID(joystick)) == joystick); } 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) { device = event.jdevice.which; joystick = SDL_JoystickOpen(device); if (joystick != NULL) { SDL_assert(SDL_JoystickFromInstanceID(SDL_JoystickInstanceID(joystick)) == joystick); } break; } } } } 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); }
/* * Open a joystick for use - the index passed as an argument refers to * the N'th joystick on the system. This index is the value which will * identify this joystick in future joystick events. * * This function returns a joystick identifier, or NULL if an error occurred. */ SDL_Joystick * SDL_JoystickOpen(int device_index) { int i; SDL_Joystick *joystick; if ((device_index < 0) || (device_index >= SDL_numjoysticks)) { SDL_SetError("There are %d joysticks available", SDL_numjoysticks); return (NULL); } /* If the joystick is already open, return it */ for (i = 0; SDL_joysticks[i]; ++i) { if (device_index == SDL_joysticks[i]->index) { joystick = SDL_joysticks[i]; ++joystick->ref_count; return (joystick); } } /* Create and initialize the joystick */ joystick = (SDL_Joystick *) SDL_malloc((sizeof *joystick)); if (joystick == NULL) { SDL_OutOfMemory(); return NULL; } SDL_memset(joystick, 0, (sizeof *joystick)); joystick->index = device_index; if (SDL_SYS_JoystickOpen(joystick) < 0) { SDL_free(joystick); return NULL; } if (joystick->naxes > 0) { joystick->axes = (Sint16 *) SDL_malloc (joystick->naxes * sizeof(Sint16)); } if (joystick->nhats > 0) { joystick->hats = (Uint8 *) SDL_malloc (joystick->nhats * sizeof(Uint8)); } if (joystick->nballs > 0) { joystick->balls = (struct balldelta *) SDL_malloc (joystick->nballs * sizeof(*joystick->balls)); } if (joystick->nbuttons > 0) { joystick->buttons = (Uint8 *) SDL_malloc (joystick->nbuttons * sizeof(Uint8)); } if (((joystick->naxes > 0) && !joystick->axes) || ((joystick->nhats > 0) && !joystick->hats) || ((joystick->nballs > 0) && !joystick->balls) || ((joystick->nbuttons > 0) && !joystick->buttons)) { SDL_OutOfMemory(); SDL_JoystickClose(joystick); return NULL; } if (joystick->axes) { SDL_memset(joystick->axes, 0, joystick->naxes * sizeof(Sint16)); } if (joystick->hats) { SDL_memset(joystick->hats, 0, joystick->nhats * sizeof(Uint8)); } if (joystick->balls) { SDL_memset(joystick->balls, 0, joystick->nballs * sizeof(*joystick->balls)); } if (joystick->buttons) { SDL_memset(joystick->buttons, 0, joystick->nbuttons * sizeof(Uint8)); } /* Add joystick to list */ ++joystick->ref_count; for (i = 0; SDL_joysticks[i]; ++i) /* Skip to next joystick */ ; SDL_joysticks[i] = joystick; return (joystick); }
static VOID PAL_JoystickEventFilter( const SDL_Event *lpEvent ) /*++ Purpose: Handle joystick events. Parameters: [IN] lpEvent - pointer to the event. Return value: None. --*/ { #ifdef PAL_HAS_JOYSTICKS switch (lpEvent->type) { #if defined (GEKKO) case SDL_JOYHATMOTION: switch (lpEvent->jhat.value) { case SDL_HAT_LEFT: g_InputState.dir = kDirWest; g_InputState.dwKeyPress = kKeyLeft; break; case SDL_HAT_RIGHT: g_InputState.dir = kDirEast; g_InputState.dwKeyPress = kKeyRight; break; case SDL_HAT_UP: g_InputState.dir = kDirNorth; g_InputState.dwKeyPress = kKeyUp; break; case SDL_HAT_DOWN: g_InputState.dir = kDirSouth; g_InputState.dwKeyPress = kKeyDown; break; } break; #else case SDL_JOYAXISMOTION: // // Moved an axis on joystick // switch (lpEvent->jaxis.axis) { case 0: // // X axis // #if defined(GPH) if (lpEvent->jaxis.value > MAX_DEADZONE) { g_InputState.dir = kDirEast; g_InputState.dwKeyPress = kKeyRight; } else if (lpEvent->jaxis.value < MIN_DEADZONE) { g_InputState.dir = kDirWest; g_InputState.dwKeyPress = kKeyLeft; } else { g_InputState.dir = kDirUnknown; } #else if (lpEvent->jaxis.value > 20000) { if (g_InputState.dir != kDirEast) { g_InputState.dwKeyPress |= kKeyRight; } g_InputState.dir = kDirEast; } else if (lpEvent->jaxis.value < -20000) { if (g_InputState.dir != kDirWest) { g_InputState.dwKeyPress |= kKeyLeft; } g_InputState.dir = kDirWest; } else { g_InputState.dir = kDirUnknown; } #endif break; case 1: // // Y axis // #if defined(GPH) if (lpEvent->jaxis.value > MAX_DEADZONE) { g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir); g_InputState.dir = kDirSouth; g_InputState.dwKeyPress = kKeyDown; } else if (lpEvent->jaxis.value < MIN_DEADZONE) { g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir); g_InputState.dir = kDirNorth; g_InputState.dwKeyPress = kKeyUp; } else { g_InputState.dir = kDirUnknown; } #else if (lpEvent->jaxis.value > 20000) { if (g_InputState.dir != kDirSouth) { g_InputState.dwKeyPress |= kKeyDown; } g_InputState.dir = kDirSouth; } else if (lpEvent->jaxis.value < -20000) { if (g_InputState.dir != kDirNorth) { g_InputState.dwKeyPress |= kKeyUp; } g_InputState.dir = kDirNorth; } else { g_InputState.dir = kDirUnknown; } #endif break; } break; #endif case SDL_JOYBUTTONDOWN: // // Pressed the joystick button // #if defined(GPH) switch (lpEvent->jbutton.button) { #if defined(GP2XWIZ) case 14: #elif defined(CAANOO) case 3: #endif g_InputState.dwKeyPress = kKeyMenu; break; #if defined(GP2XWIZ) case 13: #elif defined(CAANOO) case 2: #endif g_InputState.dwKeyPress = kKeySearch; break; #else #if defined(GEKKO) switch (lpEvent->jbutton.button) { case 2: g_InputState.dwKeyPress |= kKeyMenu; break; case 3: g_InputState.dwKeyPress |= kKeySearch; break; #else switch (lpEvent->jbutton.button & 1) { case 0: g_InputState.dwKeyPress |= kKeyMenu; break; case 1: g_InputState.dwKeyPress |= kKeySearch; break; #endif #endif } break; } #endif } #if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION <= 2 static int SDLCALL PAL_EventFilter( const SDL_Event *lpEvent ) #else int PAL_EventFilter( void *userdata, const SDL_Event *lpEvent ) #endif /*++ Purpose: SDL event filter function. A filter to process all events. Parameters: [IN] lpEvent - pointer to the event. Return value: 1 = the event will be added to the internal queue. 0 = the event will be dropped from the queue. --*/ { switch (lpEvent->type) { case SDL_VIDEORESIZE: break; case SDL_QUIT: // // clicked on the close button of the window. Quit immediately. // PAL_Shutdown(); exit(0); break; case SDL_WINDOWEVENT: return 0; break; } PAL_KeyboardEventFilter(lpEvent); PAL_MouseEventFilter(lpEvent); PAL_JoystickEventFilter(lpEvent); // // All events are handled here; don't put anything to the internal queue // return 0; } VOID PAL_ClearKeyState( VOID ) /*++ Purpose: Clear the record of pressed keys. Parameters: None. Return value: None. --*/ { #if 0 if (g_InputState.touchEventType != TOUCH_NONE) { printf("clear touch: %d\n", g_InputState.touchEventType); if (g_InputState.touchEventType == TOUCH_UP) { int x = 0; int y = 0; y = x + 1; } } #endif g_InputState.dwKeyPress = 0; g_InputState.touchEventType = TOUCH_NONE; } VOID PAL_InitInput( VOID ) /*++ Purpose: Initialize the input subsystem. Parameters: None. Return value: None. --*/ { memset(&g_InputState, 0, sizeof(g_InputState)); g_InputState.dir = kDirUnknown; #if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION <= 2 SDL_SetEventFilter(PAL_EventFilter); #else SDL_SetEventFilter(PAL_EventFilter, NULL); #endif // // Check for joystick // #ifdef PAL_HAS_JOYSTICKS if (SDL_NumJoysticks() > 0 && g_fUseJoystick) { g_pJoy = SDL_JoystickOpen(0); if (g_pJoy != NULL) { SDL_JoystickEventState(SDL_ENABLE); } } #endif } VOID PAL_ShutdownInput( VOID ) /*++ Purpose: Shutdown the input subsystem. Parameters: None. Return value: None. --*/ { #ifdef PAL_HAS_JOYSTICKS if (SDL_JoystickOpened(0)) { assert(g_pJoy != NULL); SDL_JoystickClose(g_pJoy); g_pJoy = NULL; } #endif } VOID PAL_ProcessEvent( VOID ) /*++ Purpose: Process all events. Parameters: None. Return value: None. --*/ { #ifdef PAL_HAS_NATIVEMIDI MIDI_CheckLoop(); #endif while (SDL_PollEvent(NULL)); } BOOL PAL_IsTouch(int x, int y, int w, int h) { if (g_InputState.touchEventType == TOUCH_NONE) { return FALSE; } if (g_InputState.touchX >= x && g_InputState.touchX <= x + w && g_InputState.touchY >= y && g_InputState.touchY <= y + h) { return TRUE; } return FALSE; } BOOL PAL_IsTouchDown(int x, int y, int w, int h) { if (g_InputState.touchEventType != TOUCH_DOWN) { return FALSE; } if (g_InputState.touchX >= x && g_InputState.touchX <= x + w && g_InputState.touchY >= y && g_InputState.touchY <= y + h) { return TRUE; } return FALSE; } BOOL PAL_IsTouchUp(int x, int y, int w, int h) { if (g_InputState.touchEventType != TOUCH_UP) { return FALSE; } if (g_InputState.touchX >= x && g_InputState.touchX <= x + w && g_InputState.touchY >= y && g_InputState.touchY <= y + h) { return TRUE; } return FALSE; }
void GameStateConfigDesktop::logicInput() { if (mouse_move_cb->checkClick()) { if (mouse_move_cb->isChecked()) { MOUSE_MOVE=true; no_mouse_cb->unCheck(); NO_MOUSE=false; } else MOUSE_MOVE=false; } else if (mouse_aim_cb->checkClick()) { if (mouse_aim_cb->isChecked()) { MOUSE_AIM=true; no_mouse_cb->unCheck(); NO_MOUSE=false; } else MOUSE_AIM=false; } else if (no_mouse_cb->checkClick()) { if (no_mouse_cb->isChecked()) { NO_MOUSE=true; mouse_aim_cb->unCheck(); MOUSE_AIM=false; mouse_move_cb->unCheck(); MOUSE_MOVE=false; } else NO_MOUSE=false; } else if (enable_joystick_cb->checkClick()) { if (enable_joystick_cb->isChecked()) { ENABLE_JOYSTICK=true; if (SDL_NumJoysticks() > 0) { JOYSTICK_DEVICE = 0; SDL_JoystickClose(joy); joy = SDL_JoystickOpen(JOYSTICK_DEVICE); joystick_device_lstb->selected[JOYSTICK_DEVICE] = true; } } else { ENABLE_JOYSTICK=false; for (int i=0; i<joystick_device_lstb->getSize(); i++) joystick_device_lstb->selected[i] = false; } if (SDL_NumJoysticks() > 0) joystick_device_lstb->refresh(); } else if (joystick_deadzone_sl->checkClick()) { JOY_DEADZONE = joystick_deadzone_sl->getValue(); } else if (joystick_device_lstb->checkClick()) { JOYSTICK_DEVICE = joystick_device_lstb->getSelected(); if (JOYSTICK_DEVICE != -1) { enable_joystick_cb->Check(); ENABLE_JOYSTICK=true; if (SDL_NumJoysticks() > 0) { SDL_JoystickClose(joy); joy = SDL_JoystickOpen(JOYSTICK_DEVICE); } } else { enable_joystick_cb->unCheck(); ENABLE_JOYSTICK = false; } } }