naev 0.12.5
joystick.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
10#include "SDL.h"
11#include "SDL_haptic.h"
12#include "SDL_joystick.h"
14
15#include "joystick.h"
16
17#include "log.h"
18
19static SDL_Joystick *joystick = NULL;
20static int has_haptic = 0;
21SDL_Haptic *haptic = NULL;
22unsigned int haptic_query = 0;
23
24/*
25 * Prototypes.
26 */
27static void joystick_initHaptic( void );
28
35int joystick_get( const char *namjoystick )
36{
37 for ( int i = 0; i < SDL_NumJoysticks(); i++ ) {
38 const char *jname = SDL_JoystickNameForIndex( i );
39 if ( strstr( jname, namjoystick ) )
40 return i;
41 }
42
43 WARN( _( "Joystick '%s' not found, using default joystick '%s'" ),
44 namjoystick, SDL_JoystickName( 0 ) );
45 return 0;
46}
47
54int joystick_use( int indjoystick )
55{
56 const char *jname;
57
58 /* Check to see if it exists. */
59 if ( ( indjoystick < 0 ) || ( indjoystick >= SDL_NumJoysticks() ) ) {
60 WARN( _( "Joystick of index number %d does not exist, switching to "
61 "default 0" ),
62 indjoystick );
63 indjoystick = 0;
64 }
65
66 /* Close if already open. */
67 if ( joystick != NULL ) {
68 SDL_JoystickClose( joystick );
69 joystick = NULL;
70 }
71
72 /* Start using joystick. */
73 joystick = SDL_JoystickOpen( indjoystick );
74 jname = SDL_JoystickNameForIndex( indjoystick );
75 if ( joystick == NULL ) {
76 WARN( _( "Error opening joystick %d [%s]" ), indjoystick, jname );
77 return -1;
78 }
79 LOG( _( "Using joystick %d - %s" ), indjoystick, jname );
80 DEBUG( _( " with %d axes, %d buttons, %d balls and %d hats" ),
81 SDL_JoystickNumAxes( joystick ), SDL_JoystickNumButtons( joystick ),
82 SDL_JoystickNumBalls( joystick ), SDL_JoystickNumHats( joystick ) );
83
84 /* Initialize the haptic if possible. */
86
87 /* For style purposes. */
88 DEBUG_BLANK();
89
90 return 0;
91}
92
96static void joystick_initHaptic( void )
97{
98 if ( !has_haptic || !SDL_JoystickIsHaptic( joystick ) )
99 return;
100
101 /* Close haptic if already open. */
102 if ( haptic != NULL ) {
103 SDL_HapticClose( haptic );
104 haptic = NULL;
105 }
106
107 /* Try to create haptic device. */
108 haptic = SDL_HapticOpenFromJoystick( joystick );
109 if ( haptic == NULL ) {
110 WARN( _( "Unable to initialize force feedback: %s" ), SDL_GetError() );
111 return;
112 }
113
114 /* Check to see what it supports. */
115 haptic_query = SDL_HapticQuery( haptic );
116 if ( !( haptic_query & SDL_HAPTIC_SINE ) ) {
117 SDL_HapticClose( haptic );
118 haptic = NULL;
119 return;
120 }
121
122 DEBUG( _( " force feedback enabled" ) );
123}
124
125#if DEBUGGING
129static void joystick_debug( void )
130{
131 /* figure out how many joysticks there are */
132 int numjoysticks = SDL_NumJoysticks();
133 DEBUG( n_( "%d joystick detected", "%d joysticks detected", numjoysticks ),
134 numjoysticks );
135 for ( int i = 0; i < numjoysticks; i++ ) {
136 const char *jname = SDL_JoystickNameForIndex( i );
137 DEBUG( " %d. %s", i, jname );
138 }
139}
140#endif /* DEBUGGING */
141
147int joystick_init( void )
148{
149 /* initialize the sdl subsystem */
150 if ( SDL_InitSubSystem( SDL_INIT_JOYSTICK ) ) {
151 WARN( _( "Unable to initialize the joystick subsystem" ) );
152 return -1;
153 }
154
155 if ( SDL_InitSubSystem( SDL_INIT_HAPTIC ) == 0 )
156 has_haptic = 1;
157
158#if DEBUGGING
159 joystick_debug();
160#endif /* DEBUGGING */
161
162 /* enables joystick events */
163 SDL_JoystickEventState( SDL_ENABLE );
164
165 return 0;
166}
167
171void joystick_exit( void )
172{
173 if ( haptic != NULL ) {
174 SDL_HapticClose( haptic );
175 haptic = NULL;
176 }
177
178 if ( joystick != NULL ) {
179 SDL_JoystickClose( joystick );
180 joystick = NULL;
181 }
182}
int joystick_get(const char *namjoystick)
Gets the joystick index by name.
Definition joystick.c:35
int joystick_init(void)
Initializes the joystick subsystem.
Definition joystick.c:147
static SDL_Joystick * joystick
Definition joystick.c:19
unsigned int haptic_query
Definition joystick.c:22
int joystick_use(int indjoystick)
Makes the game use a joystick by index.
Definition joystick.c:54
static void joystick_initHaptic(void)
Initializes force feedback for the loaded device.
Definition joystick.c:96
void joystick_exit(void)
Exits the joystick subsystem.
Definition joystick.c:171
SDL_Haptic * haptic
Definition joystick.c:21
static int has_haptic
Definition joystick.c:20