1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * New joystick API.
12 *
13 * By Peter Wang.
14 *
15 * See readme.txt for copyright information.
16 */
17
18 /* Title: Joystick routines
19 */
20
21
22 #define ALLEGRO_NO_COMPATIBILITY
23
24 #include "allegro5/allegro.h"
25 #include "allegro5/internal/aintern.h"
26 #include "allegro5/internal/aintern_events.h"
27 #include "allegro5/internal/aintern_exitfunc.h"
28 #include "allegro5/internal/aintern_joystick.h"
29 #include "allegro5/internal/aintern_system.h"
30
31
32
33 /* the active joystick driver */
34 static ALLEGRO_JOYSTICK_DRIVER *new_joystick_driver = NULL;
35 static ALLEGRO_EVENT_SOURCE es;
36
37
38 /* Function: al_install_joystick
39 */
al_install_joystick(void)40 bool al_install_joystick(void)
41 {
42 ALLEGRO_SYSTEM *sysdrv;
43 ALLEGRO_JOYSTICK_DRIVER *joydrv;
44
45 if (new_joystick_driver)
46 return true;
47
48 sysdrv = al_get_system_driver();
49 ASSERT(sysdrv);
50
51 /* Currently every platform only has at most one joystick driver. */
52 if (sysdrv->vt->get_joystick_driver) {
53 joydrv = sysdrv->vt->get_joystick_driver();
54 /* Avoid race condition in case the joystick driver generates an
55 * event right after ->init_joystick.
56 */
57 _al_event_source_init(&es);
58 if (joydrv && joydrv->init_joystick()) {
59 new_joystick_driver = joydrv;
60 _al_add_exit_func(al_uninstall_joystick, "al_uninstall_joystick");
61 return true;
62 }
63 _al_event_source_free(&es);
64 }
65
66 return false;
67 }
68
69
70
71 /* Function: al_uninstall_joystick
72 */
al_uninstall_joystick(void)73 void al_uninstall_joystick(void)
74 {
75 if (new_joystick_driver) {
76 /* perform driver clean up */
77 new_joystick_driver->exit_joystick();
78 _al_event_source_free(&es);
79 new_joystick_driver = NULL;
80 }
81 }
82
83
84
85 /* Function: al_is_joystick_installed
86 */
al_is_joystick_installed(void)87 bool al_is_joystick_installed(void)
88 {
89 return (new_joystick_driver) ? true : false;
90 }
91
92
93
94 /* Function: al_reconfigure_joysticks
95 */
al_reconfigure_joysticks(void)96 bool al_reconfigure_joysticks(void)
97 {
98 if (!new_joystick_driver)
99 return false;
100
101 /* XXX only until Windows and Mac joystick drivers are updated */
102 if (!new_joystick_driver->reconfigure_joysticks) {
103 new_joystick_driver->num_joysticks();
104 return true;
105 }
106
107 return new_joystick_driver->reconfigure_joysticks();
108 }
109
110
111
112 /* Function: al_get_joystick_event_source
113 */
al_get_joystick_event_source(void)114 ALLEGRO_EVENT_SOURCE *al_get_joystick_event_source(void)
115 {
116 if (!new_joystick_driver)
117 return NULL;
118 return &es;
119 }
120
121
122
_al_generate_joystick_event(ALLEGRO_EVENT * event)123 void _al_generate_joystick_event(ALLEGRO_EVENT *event)
124 {
125 ASSERT(new_joystick_driver);
126
127 _al_event_source_lock(&es);
128 if (_al_event_source_needs_to_generate_event(&es)) {
129 _al_event_source_emit_event(&es, event);
130 }
131 _al_event_source_unlock(&es);
132 }
133
134
135
136 /* Function: al_get_num_joysticks
137 */
al_get_num_joysticks(void)138 int al_get_num_joysticks(void)
139 {
140 if (new_joystick_driver)
141 return new_joystick_driver->num_joysticks();
142
143 return 0;
144 }
145
146
147
148 /* Function: al_get_joystick
149 */
al_get_joystick(int num)150 ALLEGRO_JOYSTICK * al_get_joystick(int num)
151 {
152 ASSERT(new_joystick_driver);
153 ASSERT(num >= 0);
154
155 return new_joystick_driver->get_joystick(num);
156 }
157
158
159
160 /* Function: al_release_joystick
161 */
al_release_joystick(ALLEGRO_JOYSTICK * joy)162 void al_release_joystick(ALLEGRO_JOYSTICK *joy)
163 {
164 ASSERT(new_joystick_driver);
165 ASSERT(joy);
166
167 new_joystick_driver->release_joystick(joy);
168 }
169
170
171
172 /* Function: al_get_joystick_active
173 */
al_get_joystick_active(ALLEGRO_JOYSTICK * joy)174 bool al_get_joystick_active(ALLEGRO_JOYSTICK *joy)
175 {
176 ASSERT(joy);
177
178 return new_joystick_driver->get_active(joy);
179 }
180
181
182
183 /* Function: al_get_joystick_name
184 */
al_get_joystick_name(ALLEGRO_JOYSTICK * joy)185 const char *al_get_joystick_name(ALLEGRO_JOYSTICK *joy)
186 {
187 ASSERT(joy);
188
189 return new_joystick_driver->get_name(joy);
190 }
191
192
193
194 /* Function: al_get_joystick_num_sticks
195 */
al_get_joystick_num_sticks(ALLEGRO_JOYSTICK * joy)196 int al_get_joystick_num_sticks(ALLEGRO_JOYSTICK *joy)
197 {
198 ASSERT(joy);
199
200 return joy->info.num_sticks;
201 }
202
203
204
205 /* Function: al_get_joystick_stick_flags
206 */
al_get_joystick_stick_flags(ALLEGRO_JOYSTICK * joy,int stick)207 int al_get_joystick_stick_flags(ALLEGRO_JOYSTICK *joy, int stick)
208 {
209 ASSERT(joy);
210 ASSERT(stick >= 0);
211
212 if (stick < joy->info.num_sticks)
213 return joy->info.stick[stick].flags;
214
215 return 0;
216 }
217
218
219
220 /* Function: al_get_joystick_stick_name
221 */
al_get_joystick_stick_name(ALLEGRO_JOYSTICK * joy,int stick)222 const char *al_get_joystick_stick_name(ALLEGRO_JOYSTICK *joy, int stick)
223 {
224 ASSERT(joy);
225 ASSERT(stick >= 0);
226
227 if (stick < joy->info.num_sticks)
228 return joy->info.stick[stick].name;
229
230 return NULL;
231 }
232
233
234
235 /* Function: al_get_joystick_num_axes
236 */
al_get_joystick_num_axes(ALLEGRO_JOYSTICK * joy,int stick)237 int al_get_joystick_num_axes(ALLEGRO_JOYSTICK *joy, int stick)
238 {
239 ASSERT(joy);
240
241 if (stick < joy->info.num_sticks)
242 return joy->info.stick[stick].num_axes;
243
244 return 0;
245 }
246
247
248
249 /* Function: al_get_joystick_axis_name
250 */
al_get_joystick_axis_name(ALLEGRO_JOYSTICK * joy,int stick,int axis)251 const char *al_get_joystick_axis_name(ALLEGRO_JOYSTICK *joy, int stick, int axis)
252 {
253 ASSERT(joy);
254 ASSERT(stick >= 0);
255 ASSERT(axis >= 0);
256
257 if (stick < joy->info.num_sticks)
258 if (axis < joy->info.stick[stick].num_axes)
259 return joy->info.stick[stick].axis[axis].name;
260
261 return NULL;
262 }
263
264
265
266 /* Function: al_get_joystick_num_buttons
267 */
al_get_joystick_num_buttons(ALLEGRO_JOYSTICK * joy)268 int al_get_joystick_num_buttons(ALLEGRO_JOYSTICK *joy)
269 {
270 ASSERT(joy);
271
272 return joy->info.num_buttons;
273 }
274
275
276
277 /* Function: al_get_joystick_button_name
278 */
al_get_joystick_button_name(ALLEGRO_JOYSTICK * joy,int button)279 const char *al_get_joystick_button_name(ALLEGRO_JOYSTICK *joy, int button)
280 {
281 ASSERT(joy);
282 ASSERT(button >= 0);
283
284 if (button < joy->info.num_buttons)
285 return joy->info.button[button].name;
286
287 return NULL;
288 }
289
290
291
292 /* Function: al_get_joystick_state
293 */
al_get_joystick_state(ALLEGRO_JOYSTICK * joy,ALLEGRO_JOYSTICK_STATE * ret_state)294 void al_get_joystick_state(ALLEGRO_JOYSTICK *joy, ALLEGRO_JOYSTICK_STATE *ret_state)
295 {
296 ASSERT(new_joystick_driver);
297 ASSERT(joy);
298 ASSERT(ret_state);
299
300 new_joystick_driver->get_joystick_state(joy, ret_state);
301 }
302
303 /*
304 * Local Variables:
305 * c-basic-offset: 3
306 * indent-tabs-mode: nil
307 * End:
308 */
309 /* vim: set sts=3 sw=3 et: */
310