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