comparison src/joystick/beos/SDL_bejoystick.cc @ 0:74212992fb08

Initial revision
author Sam Lantinga <slouken@lokigames.com>
date Thu, 26 Apr 2001 16:45:43 +0000
parents
children e8157fcb3114
comparison
equal deleted inserted replaced
-1:000000000000 0:74212992fb08
1 /*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 Sam Lantinga
20 slouken@devolution.com
21 */
22
23 #ifdef SAVE_RCSID
24 static char rcsid =
25 "@(#) $Id$";
26 #endif
27
28 /* This is the system specific header for the SDL joystick API */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include <be/support/String.h>
35 #include <be/device/Joystick.h>
36
37 extern "C" {
38
39 #include "SDL_error.h"
40 #include "SDL_joystick.h"
41 #include "SDL_sysjoystick.h"
42 #include "SDL_joystick_c.h"
43
44
45 /* The maximum number of joysticks we'll detect */
46 #define MAX_JOYSTICKS 16
47
48 /* A list of available joysticks */
49 static char *SDL_joyport[MAX_JOYSTICKS];
50 static char *SDL_joyname[MAX_JOYSTICKS];
51
52 /* The private structure used to keep track of a joystick */
53 struct joystick_hwdata {
54 BJoystick *stick;
55 uint8 *new_hats;
56 int16 *new_axes;
57 };
58
59 /* Function to scan the system for joysticks.
60 * This function should set SDL_numjoysticks to the number of available
61 * joysticks. Joystick 0 should be the system default joystick.
62 * It should return 0, or -1 on an unrecoverable fatal error.
63 */
64 int SDL_SYS_JoystickInit(void)
65 {
66 BJoystick joystick;
67 int numjoysticks;
68 int i;
69 int32 nports;
70 char name[B_OS_NAME_LENGTH];
71
72 /* Search for attached joysticks */
73 nports = joystick.CountDevices();
74 numjoysticks = 0;
75 memset(SDL_joyport, 0, (sizeof SDL_joyport));
76 memset(SDL_joyname, 0, (sizeof SDL_joyname));
77 for ( i=0; (SDL_numjoysticks < MAX_JOYSTICKS) && (i < nports); ++i ) {
78 if ( joystick.GetDeviceName(i, name) == B_OK ) {
79 if ( joystick.Open(name) != B_ERROR ) {
80 BString stick_name;
81 joystick.GetControllerName(&stick_name);
82 SDL_joyport[numjoysticks] = strdup(name);
83 SDL_joyname[numjoysticks] =
84 strdup(stick_name.String());
85 numjoysticks++;
86 joystick.Close();
87 }
88 }
89 }
90 return(numjoysticks);
91 }
92
93 /* Function to get the device-dependent name of a joystick */
94 const char *SDL_SYS_JoystickName(int index)
95 {
96 return SDL_joyname[index];
97 }
98
99 /* Function to open a joystick for use.
100 The joystick to open is specified by the index field of the joystick.
101 This should fill the nbuttons and naxes fields of the joystick structure.
102 It returns 0, or -1 if there is an error.
103 */
104 int SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
105 {
106 BJoystick *stick;
107
108 /* Create the joystick data structure */
109 joystick->hwdata = (struct joystick_hwdata *)
110 malloc(sizeof(*joystick->hwdata));
111 if ( joystick->hwdata == NULL ) {
112 SDL_OutOfMemory();
113 return(-1);
114 }
115 memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
116 stick = new BJoystick;
117 joystick->hwdata->stick = stick;
118
119 /* Open the requested joystick for use */
120 if ( stick->Open(SDL_joyport[joystick->index]) == B_ERROR ) {
121 SDL_SetError("Unable to open joystick");
122 SDL_SYS_JoystickClose(joystick);
123 return(-1);
124 }
125
126 /* Set the joystick to calibrated mode */
127 stick->EnableCalibration();
128
129 /* Get the number of buttons, hats, and axes on the joystick */
130 joystick->nbuttons = stick->CountButtons();
131 joystick->naxes = stick->CountAxes();
132 joystick->nhats = stick->CountHats();
133
134 joystick->hwdata->new_axes = (int16 *)
135 malloc(joystick->naxes*sizeof(int16));
136 joystick->hwdata->new_hats = (uint8 *)
137 malloc(joystick->nhats*sizeof(uint8));
138 if ( ! joystick->hwdata->new_hats || ! joystick->hwdata->new_axes ) {
139 SDL_OutOfMemory();
140 SDL_SYS_JoystickClose(joystick);
141 return(-1);
142 }
143
144 /* We're done! */
145 return(0);
146 }
147
148 /* Function to update the state of a joystick - called as a device poll.
149 * This function shouldn't update the joystick structure directly,
150 * but instead should call SDL_PrivateJoystick*() to deliver events
151 * and update joystick device state.
152 */
153 void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
154 {
155 static const Uint8 hat_map[9] = {
156 SDL_HAT_CENTERED,
157 SDL_HAT_UP,
158 SDL_HAT_RIGHTUP,
159 SDL_HAT_RIGHT,
160 SDL_HAT_RIGHTDOWN,
161 SDL_HAT_DOWN,
162 SDL_HAT_LEFTDOWN,
163 SDL_HAT_LEFT,
164 SDL_HAT_LEFTUP
165 };
166 const int JITTER = (32768/10); /* 10% jitter threshold (ok?) */
167
168 BJoystick *stick;
169 int i, change;
170 int16 *axes;
171 uint8 *hats;
172 uint32 buttons;
173
174 /* Set up data pointers */
175 stick = joystick->hwdata->stick;
176 axes = joystick->hwdata->new_axes;
177 hats = joystick->hwdata->new_hats;
178
179 /* Get the new joystick state */
180 stick->Update();
181 stick->GetAxisValues(axes);
182 stick->GetHatValues(hats);
183 buttons = stick->ButtonValues();
184
185 /* Generate axis motion events */
186 for ( i=0; i<joystick->naxes; ++i ) {
187 change = ((int32)axes[i] - joystick->axes[i]);
188 if ( (change > JITTER) || (change < -JITTER) ) {
189 SDL_PrivateJoystickAxis(joystick, i, axes[i]);
190 }
191 }
192
193 /* Generate hat change events */
194 for ( i=0; i<joystick->nhats; ++i ) {
195 if ( hats[i] != joystick->hats[i] ) {
196 SDL_PrivateJoystickHat(joystick, i, hat_map[hats[i]]);
197 }
198 }
199
200 /* Generate button events */
201 for ( i=0; i<joystick->nbuttons; ++i ) {
202 if ( (buttons&0x01) != joystick->buttons[i] ) {
203 SDL_PrivateJoystickButton(joystick, i, (buttons&0x01));
204 }
205 buttons >>= 1;
206 }
207 }
208
209 /* Function to close a joystick after use */
210 void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
211 {
212 if ( joystick->hwdata ) {
213 joystick->hwdata->stick->Close();
214 delete joystick->hwdata->stick;
215 if ( joystick->hwdata->new_hats ) {
216 free(joystick->hwdata->new_hats);
217 }
218 if ( joystick->hwdata->new_axes ) {
219 free(joystick->hwdata->new_axes);
220 }
221 free(joystick->hwdata);
222 joystick->hwdata = NULL;
223 }
224 }
225
226 /* Function to perform any system-specific joystick related cleanup */
227 void SDL_SYS_JoystickQuit(void)
228 {
229 int i;
230
231 for ( i=0; SDL_joyport[i]; ++i ) {
232 free(SDL_joyport[i]);
233 }
234 SDL_joyport[0] = NULL;
235
236 for ( i=0; SDL_joyname[i]; ++i ) {
237 free(SDL_joyname[i]);
238 }
239 SDL_joyname[0] = NULL;
240 }
241
242 }; // extern "C"