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