2
Simple DirectMedia Layer
3
Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
5
This software is provided 'as-is', without any express or implied
6
warranty. In no event will the authors be held liable for any damages
7
arising from the use of this software.
9
Permission is granted to anyone to use this software for any purpose,
10
including commercial applications, and to alter it and redistribute it
11
freely, subject to the following restrictions:
13
1. The origin of this software must not be misrepresented; you must not
14
claim that you wrote the original software. If you use this software
15
in a product, an acknowledgment in the product documentation would be
16
appreciated but is not required.
17
2. Altered source versions must be plainly marked as such, and must not be
18
misrepresented as being the original software.
19
3. This notice may not be removed or altered from any source distribution.
21
#include "SDL_config.h"
23
#ifdef SDL_JOYSTICK_BEOS
25
/* This is the system specific header for the SDL joystick API */
27
#include <be/support/String.h>
28
#include <be/device/Joystick.h>
33
#include "SDL_joystick.h"
34
#include "../SDL_sysjoystick.h"
35
#include "../SDL_joystick_c.h"
38
/* The maximum number of joysticks we'll detect */
39
#define MAX_JOYSTICKS 16
41
/* A list of available joysticks */
42
static char *SDL_joyport[MAX_JOYSTICKS];
43
static char *SDL_joyname[MAX_JOYSTICKS];
45
/* The private structure used to keep track of a joystick */
46
struct joystick_hwdata
53
static int SDL_SYS_numjoysticks = 0;
55
/* Function to scan the system for joysticks.
56
* This function should set SDL_numjoysticks to the number of available
57
* joysticks. Joystick 0 should be the system default joystick.
58
* It should return 0, or -1 on an unrecoverable fatal error.
60
int SDL_SYS_JoystickInit(void)
65
char name[B_OS_NAME_LENGTH];
67
/* Search for attached joysticks */
68
nports = joystick.CountDevices();
69
SDL_SYS_numjoysticks = 0;
70
SDL_memset(SDL_joyport, 0, (sizeof SDL_joyport));
71
SDL_memset(SDL_joyname, 0, (sizeof SDL_joyname));
72
for (i = 0; (SDL_SYS_numjoysticks < MAX_JOYSTICKS) && (i < nports); ++i)
74
if (joystick.GetDeviceName(i, name) == B_OK) {
75
if (joystick.Open(name) != B_ERROR) {
77
joystick.GetControllerName(&stick_name);
78
SDL_joyport[SDL_SYS_numjoysticks] = strdup(name);
79
SDL_joyname[SDL_SYS_numjoysticks] = strdup(stick_name.String());
80
SDL_SYS_numjoysticks++;
85
return (SDL_SYS_numjoysticks);
88
int SDL_SYS_NumJoysticks()
90
return SDL_SYS_numjoysticks;
93
void SDL_SYS_JoystickDetect()
97
SDL_bool SDL_SYS_JoystickNeedsPolling()
102
/* Function to get the device-dependent name of a joystick */
103
const char *SDL_SYS_JoystickNameForDeviceIndex(int device_index)
105
return SDL_joyname[device_index];
108
/* Function to perform the mapping from device index to the instance id for this index */
109
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
114
/* Function to open a joystick for use.
115
The joystick to open is specified by the index field of the joystick.
116
This should fill the nbuttons and naxes fields of the joystick structure.
117
It returns 0, or -1 if there is an error.
119
int SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
123
/* Create the joystick data structure */
124
joystick->instance_id = device_index;
125
joystick->hwdata = (struct joystick_hwdata *)
126
SDL_malloc(sizeof(*joystick->hwdata));
127
if (joystick->hwdata == NULL) {
131
SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
132
stick = new BJoystick;
133
joystick->hwdata->stick = stick;
135
/* Open the requested joystick for use */
136
if (stick->Open(SDL_joyport[device_index]) == B_ERROR) {
137
SDL_SetError("Unable to open joystick");
138
SDL_SYS_JoystickClose(joystick);
142
/* Set the joystick to calibrated mode */
143
stick->EnableCalibration();
145
/* Get the number of buttons, hats, and axes on the joystick */
146
joystick->nbuttons = stick->CountButtons();
147
joystick->naxes = stick->CountAxes();
148
joystick->nhats = stick->CountHats();
150
joystick->hwdata->new_axes = (int16 *)
151
SDL_malloc(joystick->naxes * sizeof(int16));
152
joystick->hwdata->new_hats = (uint8 *)
153
SDL_malloc(joystick->nhats * sizeof(uint8));
154
if (!joystick->hwdata->new_hats || !joystick->hwdata->new_axes) {
156
SDL_SYS_JoystickClose(joystick);
164
/* Function to determine is this joystick is attached to the system right now */
165
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
170
/* Function to update the state of a joystick - called as a device poll.
171
* This function shouldn't update the joystick structure directly,
172
* but instead should call SDL_PrivateJoystick*() to deliver events
173
* and update joystick device state.
175
void SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
177
static const Uint8 hat_map[9] = {
188
const int JITTER = (32768 / 10); /* 10% jitter threshold (ok?) */
196
/* Set up data pointers */
197
stick = joystick->hwdata->stick;
198
axes = joystick->hwdata->new_axes;
199
hats = joystick->hwdata->new_hats;
201
/* Get the new joystick state */
203
stick->GetAxisValues(axes);
204
stick->GetHatValues(hats);
205
buttons = stick->ButtonValues();
207
/* Generate axis motion events */
208
for (i = 0; i < joystick->naxes; ++i) {
209
change = ((int32) axes[i] - joystick->axes[i]);
210
if ((change > JITTER) || (change < -JITTER)) {
211
SDL_PrivateJoystickAxis(joystick, i, axes[i]);
215
/* Generate hat change events */
216
for (i = 0; i < joystick->nhats; ++i) {
217
if (hats[i] != joystick->hats[i]) {
218
SDL_PrivateJoystickHat(joystick, i, hat_map[hats[i]]);
222
/* Generate button events */
223
for (i = 0; i < joystick->nbuttons; ++i) {
224
if ((buttons & 0x01) != joystick->buttons[i]) {
225
SDL_PrivateJoystickButton(joystick, i, (buttons & 0x01));
231
/* Function to close a joystick after use */
232
void SDL_SYS_JoystickClose(SDL_Joystick * joystick)
234
if (joystick->hwdata) {
235
joystick->hwdata->stick->Close();
236
delete joystick->hwdata->stick;
237
if (joystick->hwdata->new_hats) {
238
SDL_free(joystick->hwdata->new_hats);
240
if (joystick->hwdata->new_axes) {
241
SDL_free(joystick->hwdata->new_axes);
243
SDL_free(joystick->hwdata);
244
joystick->hwdata = NULL;
248
/* Function to perform any system-specific joystick related cleanup */
249
void SDL_SYS_JoystickQuit(void)
253
for (i = 0; SDL_joyport[i]; ++i) {
254
SDL_free(SDL_joyport[i]);
256
SDL_joyport[0] = NULL;
258
for (i = 0; SDL_joyname[i]; ++i) {
259
SDL_free(SDL_joyname[i]);
261
SDL_joyname[0] = NULL;
264
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
266
SDL_JoystickGUID guid;
267
// the GUID is just the first 16 chars of the name for now
268
const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
270
SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
274
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
276
SDL_JoystickGUID guid;
277
// the GUID is just the first 16 chars of the name for now
278
const char *name = joystick->name;
280
SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
286
#endif /* SDL_JOYSTICK_BEOS */
287
/* vi: set ts=4 sw=4 expandtab: */