SDL  2.0
SDL_joystick_c.h File Reference
#include "../SDL_internal.h"
#include "SDL_joystick.h"
+ Include dependency graph for SDL_joystick_c.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int SDL_JoystickInit (void)
 
void SDL_JoystickQuit (void)
 
int SDL_GameControllerInit (void)
 
void SDL_GameControllerQuit (void)
 
int SDL_PrivateJoystickAxis (SDL_Joystick *joystick, Uint8 axis, Sint16 value)
 
int SDL_PrivateJoystickBall (SDL_Joystick *joystick, Uint8 ball, Sint16 xrel, Sint16 yrel)
 
int SDL_PrivateJoystickHat (SDL_Joystick *joystick, Uint8 hat, Uint8 value)
 
int SDL_PrivateJoystickButton (SDL_Joystick *joystick, Uint8 button, Uint8 state)
 
void SDL_PrivateJoystickBatteryLevel (SDL_Joystick *joystick, SDL_JoystickPowerLevel ePowerLevel)
 
int SDL_PrivateJoystickValid (SDL_Joystick *joystick)
 

Function Documentation

int SDL_GameControllerInit ( void  )

Definition at line 860 of file SDL_gamecontroller.c.

References SDL_Event::cdevice, i, NULL, s_ControllerMappings, SDL_AddEventWatch, SDL_CONTROLLERDEVICEADDED, SDL_GameControllerAddMapping(), SDL_GameControllerEventWatcher(), SDL_GameControllerLoadHints(), SDL_IsGameController(), SDL_NumJoysticks, SDL_PushEvent, SDL_Event::type, and SDL_ControllerDeviceEvent::which.

Referenced by SDL_InitSubSystem().

861 {
862  int i = 0;
863  const char *pMappingString = NULL;
865  pMappingString = s_ControllerMappings[i];
866  while (pMappingString) {
867  SDL_GameControllerAddMapping(pMappingString);
868 
869  i++;
870  pMappingString = s_ControllerMappings[i];
871  }
872 
873  /* load in any user supplied config */
875 
876  /* watch for joy events and fire controller ones if needed */
878 
879  /* Send added events for controllers currently attached */
880  for (i = 0; i < SDL_NumJoysticks(); ++i) {
881  if (SDL_IsGameController(i)) {
882  SDL_Event deviceevent;
883  deviceevent.type = SDL_CONTROLLERDEVICEADDED;
884  deviceevent.cdevice.which = i;
885  SDL_PushEvent(&deviceevent);
886  }
887  }
888 
889  return (0);
890 }
SDL_ControllerDeviceEvent cdevice
Definition: SDL_events.h:539
#define SDL_NumJoysticks
static void SDL_GameControllerLoadHints()
static ControllerMapping_t * s_pSupportedControllers
int SDL_GameControllerAddMapping(const char *mappingString)
#define SDL_PushEvent
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:42
SDL_bool SDL_IsGameController(int device_index)
#define NULL
Definition: begin_code.h:143
static const char * s_ControllerMappings[]
#define SDL_AddEventWatch
General event structure.
Definition: SDL_events.h:521
int SDL_GameControllerEventWatcher(void *userdata, SDL_Event *event)
Uint32 type
Definition: SDL_events.h:523
void SDL_GameControllerQuit ( void  )

Definition at line 1200 of file SDL_gamecontroller.c.

References ControllerMapping_t::mapping, ControllerMapping_t::name, ControllerMapping_t::next, NULL, s_pSupportedControllers, SDL_DelEventWatch, SDL_free(), SDL_GameControllerClose(), SDL_GameControllerEventWatcher(), and SDL_gamecontrollers.

Referenced by SDL_QuitSubSystem().

1201 {
1202  ControllerMapping_t *pControllerMap;
1203  while (SDL_gamecontrollers) {
1204  SDL_gamecontrollers->ref_count = 1;
1206  }
1207 
1208  while (s_pSupportedControllers) {
1209  pControllerMap = s_pSupportedControllers;
1211  SDL_free(pControllerMap->name);
1212  SDL_free(pControllerMap->mapping);
1213  SDL_free(pControllerMap);
1214  }
1215 
1217 
1218 }
#define SDL_DelEventWatch
struct _ControllerMapping_t * next
void SDL_GameControllerClose(SDL_GameController *gamecontroller)
static SDL_GameController * SDL_gamecontrollers
static ControllerMapping_t * s_pSupportedControllers
void SDL_free(void *mem)
#define NULL
Definition: begin_code.h:143
int SDL_GameControllerEventWatcher(void *userdata, SDL_Event *event)
int SDL_JoystickInit ( void  )

Definition at line 50 of file SDL_joystick.c.

References NULL, SDL_AddHintCallback, SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, SDL_INIT_EVENTS, SDL_InitSubSystem, SDL_JoystickAllowBackgroundEventsChanged(), and SDL_SYS_JoystickInit().

Referenced by SDL_InitSubSystem().

51 {
52  int status;
53 
54  /* See if we should allow joystick events while in the background */
57 
58 #if !SDL_EVENTS_DISABLED
60  return -1;
61  }
62 #endif /* !SDL_EVENTS_DISABLED */
63 
64  status = SDL_SYS_JoystickInit();
65  if (status >= 0) {
66  status = 0;
67  }
68  return (status);
69 }
#define SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS
A variable that lets you enable joystick (and gamecontroller) events even when your app is in the bac...
Definition: SDL_hints.h:334
#define SDL_INIT_EVENTS
Definition: SDL.h:81
#define SDL_InitSubSystem
int SDL_SYS_JoystickInit(void)
#define NULL
Definition: begin_code.h:143
#define SDL_AddHintCallback
static void SDL_JoystickAllowBackgroundEventsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
Definition: SDL_joystick.c:40
void SDL_JoystickQuit ( void  )

Definition at line 458 of file SDL_joystick.c.

References SDL_assert, SDL_INIT_EVENTS, SDL_JoystickClose(), SDL_joysticks, SDL_QuitSubSystem, SDL_SYS_JoystickQuit(), and SDL_updating_joystick.

Referenced by SDL_QuitSubSystem().

459 {
460  /* Make sure we're not getting called in the middle of updating joysticks */
462 
463  /* Stop the event polling */
464  while (SDL_joysticks) {
465  SDL_joysticks->ref_count = 1;
467  }
468 
469  /* Quit the joystick setup */
471 
472 #if !SDL_EVENTS_DISABLED
474 #endif
475 }
#define SDL_INIT_EVENTS
Definition: SDL.h:81
static SDL_Joystick * SDL_updating_joystick
Definition: SDL_joystick.c:37
void SDL_JoystickClose(SDL_Joystick *joystick)
Definition: SDL_joystick.c:410
#define SDL_QuitSubSystem
void SDL_SYS_JoystickQuit(void)
#define SDL_assert(condition)
Definition: SDL_assert.h:167
static SDL_Joystick * SDL_joysticks
Definition: SDL_joystick.c:36
int SDL_PrivateJoystickAxis ( SDL_Joystick *  joystick,
Uint8  axis,
Sint16  value 
)

Definition at line 501 of file SDL_joystick.c.

References axis, SDL_ENABLE, SDL_GetEventState, SDL_JOYAXISMOTION, SDL_PrivateJoystickShouldIgnoreEvent(), and SDL_PushEvent.

Referenced by SDL_JoystickUpdate(), SDL_SYS_AccelerometerUpdate(), and SDL_SYS_MFIJoystickUpdate().

502 {
503  int posted;
504 
505  /* Make sure we're not getting garbage or duplicate events */
506  if (axis >= joystick->naxes) {
507  return 0;
508  }
509  if (value == joystick->axes[axis]) {
510  return 0;
511  }
512 
513  /* We ignore events if we don't have keyboard focus, except for centering
514  * events.
515  */
517  if ((value > 0 && value >= joystick->axes[axis]) ||
518  (value < 0 && value <= joystick->axes[axis])) {
519  return 0;
520  }
521  }
522 
523  /* Update internal joystick state */
524  joystick->axes[axis] = value;
525 
526  /* Post the event, if desired */
527  posted = 0;
528 #if !SDL_EVENTS_DISABLED
531  event.type = SDL_JOYAXISMOTION;
532  event.jaxis.which = joystick->instance_id;
533  event.jaxis.axis = axis;
534  event.jaxis.value = value;
535  posted = SDL_PushEvent(&event) == 1;
536  }
537 #endif /* !SDL_EVENTS_DISABLED */
538  return (posted);
539 }
#define SDL_ENABLE
Definition: SDL_events.h:718
SDL_Texture * axis
#define SDL_GetEventState(type)
Definition: SDL_events.h:731
GLsizei const GLfloat * value
struct _cl_event * event
#define SDL_PushEvent
static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent()
Definition: SDL_joystick.c:479
General event structure.
Definition: SDL_events.h:521
int SDL_PrivateJoystickBall ( SDL_Joystick *  joystick,
Uint8  ball,
Sint16  xrel,
Sint16  yrel 
)

Definition at line 582 of file SDL_joystick.c.

References SDL_ENABLE, SDL_GetEventState, SDL_JOYBALLMOTION, SDL_PrivateJoystickShouldIgnoreEvent(), and SDL_PushEvent.

584 {
585  int posted;
586 
587  /* Make sure we're not getting garbage events */
588  if (ball >= joystick->nballs) {
589  return 0;
590  }
591 
592  /* We ignore events if we don't have keyboard focus. */
594  return 0;
595  }
596 
597  /* Update internal mouse state */
598  joystick->balls[ball].dx += xrel;
599  joystick->balls[ball].dy += yrel;
600 
601  /* Post the event, if desired */
602  posted = 0;
603 #if !SDL_EVENTS_DISABLED
606  event.jball.type = SDL_JOYBALLMOTION;
607  event.jball.which = joystick->instance_id;
608  event.jball.ball = ball;
609  event.jball.xrel = xrel;
610  event.jball.yrel = yrel;
611  posted = SDL_PushEvent(&event) == 1;
612  }
613 #endif /* !SDL_EVENTS_DISABLED */
614  return (posted);
615 }
#define SDL_ENABLE
Definition: SDL_events.h:718
#define SDL_GetEventState(type)
Definition: SDL_events.h:731
struct _cl_event * event
#define SDL_PushEvent
static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent()
Definition: SDL_joystick.c:479
General event structure.
Definition: SDL_events.h:521
void SDL_PrivateJoystickBatteryLevel ( SDL_Joystick *  joystick,
SDL_JoystickPowerLevel  ePowerLevel 
)

Definition at line 847 of file SDL_joystick.c.

848 {
849  joystick->epowerlevel = ePowerLevel;
850 }
int SDL_PrivateJoystickButton ( SDL_Joystick *  joystick,
Uint8  button,
Uint8  state 
)

Definition at line 618 of file SDL_joystick.c.

References button, SDL_ENABLE, SDL_GetEventState, SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP, SDL_PRESSED, SDL_PrivateJoystickShouldIgnoreEvent(), SDL_PushEvent, SDL_RELEASED, state, and SDL_Event::type.

Referenced by SDL_JoystickUpdate(), and SDL_SYS_MFIJoystickUpdate().

619 {
620  int posted;
621 #if !SDL_EVENTS_DISABLED
623 
624  switch (state) {
625  case SDL_PRESSED:
626  event.type = SDL_JOYBUTTONDOWN;
627  break;
628  case SDL_RELEASED:
629  event.type = SDL_JOYBUTTONUP;
630  break;
631  default:
632  /* Invalid state -- bail */
633  return (0);
634  }
635 #endif /* !SDL_EVENTS_DISABLED */
636 
637  /* Make sure we're not getting garbage or duplicate events */
638  if (button >= joystick->nbuttons) {
639  return 0;
640  }
641  if (state == joystick->buttons[button]) {
642  return 0;
643  }
644 
645  /* We ignore events if we don't have keyboard focus, except for button
646  * release. */
648  if (state == SDL_PRESSED) {
649  return 0;
650  }
651  }
652 
653  /* Update internal joystick state */
654  joystick->buttons[button] = state;
655 
656  /* Post the event, if desired */
657  posted = 0;
658 #if !SDL_EVENTS_DISABLED
659  if (SDL_GetEventState(event.type) == SDL_ENABLE) {
660  event.jbutton.which = joystick->instance_id;
661  event.jbutton.button = button;
662  event.jbutton.state = state;
663  posted = SDL_PushEvent(&event) == 1;
664  }
665 #endif /* !SDL_EVENTS_DISABLED */
666  return (posted);
667 }
SDL_Texture * button
struct xkb_state * state
#define SDL_ENABLE
Definition: SDL_events.h:718
#define SDL_GetEventState(type)
Definition: SDL_events.h:731
struct _cl_event * event
#define SDL_PushEvent
static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent()
Definition: SDL_joystick.c:479
General event structure.
Definition: SDL_events.h:521
#define SDL_PRESSED
Definition: SDL_events.h:50
#define SDL_RELEASED
Definition: SDL_events.h:49
Uint32 type
Definition: SDL_events.h:523
int SDL_PrivateJoystickHat ( SDL_Joystick *  joystick,
Uint8  hat,
Uint8  value 
)

Definition at line 542 of file SDL_joystick.c.

References SDL_ENABLE, SDL_GetEventState, SDL_HAT_CENTERED, SDL_JOYHATMOTION, SDL_PrivateJoystickShouldIgnoreEvent(), and SDL_PushEvent.

Referenced by SDL_JoystickUpdate(), and SDL_SYS_MFIJoystickUpdate().

543 {
544  int posted;
545 
546  /* Make sure we're not getting garbage or duplicate events */
547  if (hat >= joystick->nhats) {
548  return 0;
549  }
550  if (value == joystick->hats[hat]) {
551  return 0;
552  }
553 
554  /* We ignore events if we don't have keyboard focus, except for centering
555  * events.
556  */
558  if (value != SDL_HAT_CENTERED) {
559  return 0;
560  }
561  }
562 
563  /* Update internal joystick state */
564  joystick->hats[hat] = value;
565 
566  /* Post the event, if desired */
567  posted = 0;
568 #if !SDL_EVENTS_DISABLED
571  event.jhat.type = SDL_JOYHATMOTION;
572  event.jhat.which = joystick->instance_id;
573  event.jhat.hat = hat;
574  event.jhat.value = value;
575  posted = SDL_PushEvent(&event) == 1;
576  }
577 #endif /* !SDL_EVENTS_DISABLED */
578  return (posted);
579 }
#define SDL_ENABLE
Definition: SDL_events.h:718
#define SDL_GetEventState(type)
Definition: SDL_events.h:731
GLsizei const GLfloat * value
struct _cl_event * event
#define SDL_PushEvent
static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent()
Definition: SDL_joystick.c:479
General event structure.
Definition: SDL_events.h:521
#define SDL_HAT_CENTERED
Definition: SDL_joystick.h:207
int SDL_PrivateJoystickValid ( SDL_Joystick *  joystick)

Definition at line 199 of file SDL_joystick.c.

References NULL, and SDL_SetError.

Referenced by SDL_HapticOpenFromJoystick(), SDL_JoystickCurrentPowerLevel(), SDL_JoystickGetAttached(), SDL_JoystickGetAxis(), SDL_JoystickGetBall(), SDL_JoystickGetButton(), SDL_JoystickGetGUID(), SDL_JoystickGetHat(), SDL_JoystickInstanceID(), SDL_JoystickIsHaptic(), SDL_JoystickName(), SDL_JoystickNumAxes(), SDL_JoystickNumBalls(), SDL_JoystickNumButtons(), and SDL_JoystickNumHats().

200 {
201  int valid;
202 
203  if (joystick == NULL) {
204  SDL_SetError("Joystick hasn't been opened yet");
205  valid = 0;
206  } else {
207  valid = 1;
208  }
209 
210  return valid;
211 }
#define NULL
Definition: begin_code.h:143
#define SDL_SetError