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)
 
void SDL_PrivateJoystickAdded (int device_index)
 
void SDL_PrivateJoystickRemoved (SDL_JoystickID device_instance)
 
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

◆ SDL_GameControllerInit()

int SDL_GameControllerInit ( void  )

Definition at line 901 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().

902 {
903  int i = 0;
904  const char *pMappingString = NULL;
905  pMappingString = s_ControllerMappings[i];
906  while (pMappingString) {
907  SDL_GameControllerAddMapping(pMappingString);
908 
909  i++;
910  pMappingString = s_ControllerMappings[i];
911  }
912 
913  /* load in any user supplied config */
915 
916  /* watch for joy events and fire controller ones if needed */
918 
919  /* Send added events for controllers currently attached */
920  for (i = 0; i < SDL_NumJoysticks(); ++i) {
921  if (SDL_IsGameController(i)) {
922  SDL_Event deviceevent;
923  deviceevent.type = SDL_CONTROLLERDEVICEADDED;
924  deviceevent.cdevice.which = i;
925  SDL_PushEvent(&deviceevent);
926  }
927  }
928 
929  return (0);
930 }
SDL_ControllerDeviceEvent cdevice
Definition: SDL_events.h:543
#define SDL_NumJoysticks
static void SDL_GameControllerLoadHints()
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:50
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:525
int SDL_GameControllerEventWatcher(void *userdata, SDL_Event *event)
Uint32 type
Definition: SDL_events.h:527

◆ SDL_GameControllerQuit()

void SDL_GameControllerQuit ( void  )

Definition at line 1254 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().

1255 {
1256  ControllerMapping_t *pControllerMap;
1257  while (SDL_gamecontrollers) {
1258  SDL_gamecontrollers->ref_count = 1;
1260  }
1261 
1262  while (s_pSupportedControllers) {
1263  pControllerMap = s_pSupportedControllers;
1265  SDL_free(pControllerMap->name);
1266  SDL_free(pControllerMap->mapping);
1267  SDL_free(pControllerMap);
1268  }
1269 
1271 
1272 }
#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)

◆ SDL_JoystickInit()

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:373
#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

◆ SDL_JoystickQuit()

void SDL_JoystickQuit ( void  )

Definition at line 459 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().

460 {
461  /* Make sure we're not getting called in the middle of updating joysticks */
463 
464  /* Stop the event polling */
465  while (SDL_joysticks) {
466  SDL_joysticks->ref_count = 1;
468  }
469 
470  /* Quit the joystick setup */
472 
473 #if !SDL_EVENTS_DISABLED
475 #endif
476 }
#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:411
#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

◆ SDL_PrivateJoystickAdded()

void SDL_PrivateJoystickAdded ( int  device_index)

Definition at line 501 of file SDL_joystick.c.

References NULL, SDL_ENABLE, SDL_EventOK, SDL_EventOKParam, SDL_GetEventState, SDL_JOYDEVICEADDED, SDL_PushEvent, and SDL_Event::type.

Referenced by SDL_SYS_AddJoystickDevice().

502 {
503 #if !SDL_EVENTS_DISABLED
505 
506  event.type = SDL_JOYDEVICEADDED;
507 
508  if (SDL_GetEventState(event.type) == SDL_ENABLE) {
509  event.jdevice.which = device_index;
510  if ( (SDL_EventOK == NULL) ||
511  (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
512  SDL_PushEvent(&event);
513  }
514  }
515 #endif /* !SDL_EVENTS_DISABLED */
516 }
#define SDL_ENABLE
Definition: SDL_events.h:722
#define SDL_GetEventState(type)
Definition: SDL_events.h:735
struct _cl_event * event
SDL_EventFilter SDL_EventOK
Definition: SDL_events.c:40
#define SDL_PushEvent
void * SDL_EventOKParam
Definition: SDL_events.c:41
#define NULL
Definition: begin_code.h:143
General event structure.
Definition: SDL_events.h:525
Uint32 type
Definition: SDL_events.h:527

◆ SDL_PrivateJoystickAxis()

int SDL_PrivateJoystickAxis ( SDL_Joystick *  joystick,
Uint8  axis,
Sint16  value 
)

Definition at line 567 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().

568 {
569  int posted;
570 
571  /* Make sure we're not getting garbage or duplicate events */
572  if (axis >= joystick->naxes) {
573  return 0;
574  }
575  if (value == joystick->axes[axis]) {
576  return 0;
577  }
578 
579  /* We ignore events if we don't have keyboard focus, except for centering
580  * events.
581  */
583  if ((value > joystick->axes_zero[axis] && value >= joystick->axes[axis]) ||
584  (value < joystick->axes_zero[axis] && value <= joystick->axes[axis])) {
585  return 0;
586  }
587  }
588 
589  /* Update internal joystick state */
590  joystick->axes[axis] = value;
591 
592  /* Post the event, if desired */
593  posted = 0;
594 #if !SDL_EVENTS_DISABLED
597  event.type = SDL_JOYAXISMOTION;
598  event.jaxis.which = joystick->instance_id;
599  event.jaxis.axis = axis;
600  event.jaxis.value = value;
601  posted = SDL_PushEvent(&event) == 1;
602  }
603 #endif /* !SDL_EVENTS_DISABLED */
604  return (posted);
605 }
#define SDL_ENABLE
Definition: SDL_events.h:722
SDL_Texture * axis
#define SDL_GetEventState(type)
Definition: SDL_events.h:735
GLsizei const GLfloat * value
struct _cl_event * event
#define SDL_PushEvent
static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent()
Definition: SDL_joystick.c:480
General event structure.
Definition: SDL_events.h:525

◆ SDL_PrivateJoystickBall()

int SDL_PrivateJoystickBall ( SDL_Joystick *  joystick,
Uint8  ball,
Sint16  xrel,
Sint16  yrel 
)

Definition at line 648 of file SDL_joystick.c.

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

650 {
651  int posted;
652 
653  /* Make sure we're not getting garbage events */
654  if (ball >= joystick->nballs) {
655  return 0;
656  }
657 
658  /* We ignore events if we don't have keyboard focus. */
660  return 0;
661  }
662 
663  /* Update internal mouse state */
664  joystick->balls[ball].dx += xrel;
665  joystick->balls[ball].dy += yrel;
666 
667  /* Post the event, if desired */
668  posted = 0;
669 #if !SDL_EVENTS_DISABLED
672  event.jball.type = SDL_JOYBALLMOTION;
673  event.jball.which = joystick->instance_id;
674  event.jball.ball = ball;
675  event.jball.xrel = xrel;
676  event.jball.yrel = yrel;
677  posted = SDL_PushEvent(&event) == 1;
678  }
679 #endif /* !SDL_EVENTS_DISABLED */
680  return (posted);
681 }
#define SDL_ENABLE
Definition: SDL_events.h:722
#define SDL_GetEventState(type)
Definition: SDL_events.h:735
struct _cl_event * event
#define SDL_PushEvent
static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent()
Definition: SDL_joystick.c:480
General event structure.
Definition: SDL_events.h:525

◆ SDL_PrivateJoystickBatteryLevel()

void SDL_PrivateJoystickBatteryLevel ( SDL_Joystick *  joystick,
SDL_JoystickPowerLevel  ePowerLevel 
)

Definition at line 913 of file SDL_joystick.c.

914 {
915  joystick->epowerlevel = ePowerLevel;
916 }

◆ SDL_PrivateJoystickButton()

int SDL_PrivateJoystickButton ( SDL_Joystick *  joystick,
Uint8  button,
Uint8  state 
)

Definition at line 684 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().

685 {
686  int posted;
687 #if !SDL_EVENTS_DISABLED
689 
690  switch (state) {
691  case SDL_PRESSED:
692  event.type = SDL_JOYBUTTONDOWN;
693  break;
694  case SDL_RELEASED:
695  event.type = SDL_JOYBUTTONUP;
696  break;
697  default:
698  /* Invalid state -- bail */
699  return (0);
700  }
701 #endif /* !SDL_EVENTS_DISABLED */
702 
703  /* Make sure we're not getting garbage or duplicate events */
704  if (button >= joystick->nbuttons) {
705  return 0;
706  }
707  if (state == joystick->buttons[button]) {
708  return 0;
709  }
710 
711  /* We ignore events if we don't have keyboard focus, except for button
712  * release. */
714  if (state == SDL_PRESSED) {
715  return 0;
716  }
717  }
718 
719  /* Update internal joystick state */
720  joystick->buttons[button] = state;
721 
722  /* Post the event, if desired */
723  posted = 0;
724 #if !SDL_EVENTS_DISABLED
725  if (SDL_GetEventState(event.type) == SDL_ENABLE) {
726  event.jbutton.which = joystick->instance_id;
727  event.jbutton.button = button;
728  event.jbutton.state = state;
729  posted = SDL_PushEvent(&event) == 1;
730  }
731 #endif /* !SDL_EVENTS_DISABLED */
732  return (posted);
733 }
SDL_Texture * button
struct xkb_state * state
#define SDL_ENABLE
Definition: SDL_events.h:722
#define SDL_GetEventState(type)
Definition: SDL_events.h:735
struct _cl_event * event
#define SDL_PushEvent
static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent()
Definition: SDL_joystick.c:480
General event structure.
Definition: SDL_events.h:525
#define SDL_PRESSED
Definition: SDL_events.h:50
#define SDL_RELEASED
Definition: SDL_events.h:49
Uint32 type
Definition: SDL_events.h:527

◆ SDL_PrivateJoystickHat()

int SDL_PrivateJoystickHat ( SDL_Joystick *  joystick,
Uint8  hat,
Uint8  value 
)

Definition at line 608 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().

609 {
610  int posted;
611 
612  /* Make sure we're not getting garbage or duplicate events */
613  if (hat >= joystick->nhats) {
614  return 0;
615  }
616  if (value == joystick->hats[hat]) {
617  return 0;
618  }
619 
620  /* We ignore events if we don't have keyboard focus, except for centering
621  * events.
622  */
624  if (value != SDL_HAT_CENTERED) {
625  return 0;
626  }
627  }
628 
629  /* Update internal joystick state */
630  joystick->hats[hat] = value;
631 
632  /* Post the event, if desired */
633  posted = 0;
634 #if !SDL_EVENTS_DISABLED
637  event.jhat.type = SDL_JOYHATMOTION;
638  event.jhat.which = joystick->instance_id;
639  event.jhat.hat = hat;
640  event.jhat.value = value;
641  posted = SDL_PushEvent(&event) == 1;
642  }
643 #endif /* !SDL_EVENTS_DISABLED */
644  return (posted);
645 }
#define SDL_ENABLE
Definition: SDL_events.h:722
#define SDL_GetEventState(type)
Definition: SDL_events.h:735
GLsizei const GLfloat * value
struct _cl_event * event
#define SDL_PushEvent
static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent()
Definition: SDL_joystick.c:480
General event structure.
Definition: SDL_events.h:525
#define SDL_HAT_CENTERED
Definition: SDL_joystick.h:207

◆ SDL_PrivateJoystickRemoved()

void SDL_PrivateJoystickRemoved ( SDL_JoystickID  device_instance)

Definition at line 547 of file SDL_joystick.c.

References NULL, SDL_ENABLE, SDL_EventOK, SDL_EventOKParam, SDL_GetEventState, SDL_JOYDEVICEREMOVED, SDL_PushEvent, SDL_Event::type, and UpdateEventsForDeviceRemoval().

Referenced by SDL_SYS_RemoveJoystickDevice().

548 {
549 #if !SDL_EVENTS_DISABLED
551 
552  event.type = SDL_JOYDEVICEREMOVED;
553 
554  if (SDL_GetEventState(event.type) == SDL_ENABLE) {
555  event.jdevice.which = device_instance;
556  if ( (SDL_EventOK == NULL) ||
557  (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
558  SDL_PushEvent(&event);
559  }
560  }
561 
563 #endif /* !SDL_EVENTS_DISABLED */
564 }
#define SDL_ENABLE
Definition: SDL_events.h:722
static void UpdateEventsForDeviceRemoval()
Definition: SDL_joystick.c:523
#define SDL_GetEventState(type)
Definition: SDL_events.h:735
struct _cl_event * event
SDL_EventFilter SDL_EventOK
Definition: SDL_events.c:40
#define SDL_PushEvent
void * SDL_EventOKParam
Definition: SDL_events.c:41
#define NULL
Definition: begin_code.h:143
General event structure.
Definition: SDL_events.h:525
Uint32 type
Definition: SDL_events.h:527

◆ SDL_PrivateJoystickValid()

int SDL_PrivateJoystickValid ( SDL_Joystick *  joystick)

Definition at line 200 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().

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