Mercurial > sdl-ios-xcode
annotate src/joystick/beos/SDL_bejoystick.cc @ 1336:3692456e7b0f
Use SDL_ prefixed versions of C library functions.
FIXME:
Change #include <stdlib.h> to #include "SDL_stdlib.h"
Change #include <string.h> to #include "SDL_string.h"
Make sure nothing else broke because of this...
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 07 Feb 2006 06:59:48 +0000 |
parents | c9b51268668f |
children | 604d73db6802 |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 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 | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 /* This is the system specific header for the SDL joystick API */ | |
24 | |
25 #include <stdio.h> | |
26 #include <stdlib.h> | |
27 #include <string.h> | |
28 | |
29 #include <be/support/String.h> | |
30 #include <be/device/Joystick.h> | |
31 | |
32 extern "C" { | |
33 | |
34 #include "SDL_error.h" | |
35 #include "SDL_joystick.h" | |
36 #include "SDL_sysjoystick.h" | |
37 #include "SDL_joystick_c.h" | |
38 | |
39 | |
40 /* The maximum number of joysticks we'll detect */ | |
41 #define MAX_JOYSTICKS 16 | |
42 | |
43 /* A list of available joysticks */ | |
44 static char *SDL_joyport[MAX_JOYSTICKS]; | |
45 static char *SDL_joyname[MAX_JOYSTICKS]; | |
46 | |
47 /* The private structure used to keep track of a joystick */ | |
48 struct joystick_hwdata { | |
49 BJoystick *stick; | |
50 uint8 *new_hats; | |
51 int16 *new_axes; | |
52 }; | |
53 | |
54 /* Function to scan the system for joysticks. | |
55 * This function should set SDL_numjoysticks to the number of available | |
56 * joysticks. Joystick 0 should be the system default joystick. | |
57 * It should return 0, or -1 on an unrecoverable fatal error. | |
58 */ | |
59 int SDL_SYS_JoystickInit(void) | |
60 { | |
61 BJoystick joystick; | |
62 int numjoysticks; | |
63 int i; | |
64 int32 nports; | |
65 char name[B_OS_NAME_LENGTH]; | |
66 | |
67 /* Search for attached joysticks */ | |
68 nports = joystick.CountDevices(); | |
69 numjoysticks = 0; | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
70 SDL_memset(SDL_joyport, 0, (sizeof SDL_joyport)); |
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
71 SDL_memset(SDL_joyname, 0, (sizeof SDL_joyname)); |
0 | 72 for ( i=0; (SDL_numjoysticks < MAX_JOYSTICKS) && (i < nports); ++i ) { |
73 if ( joystick.GetDeviceName(i, name) == B_OK ) { | |
74 if ( joystick.Open(name) != B_ERROR ) { | |
75 BString stick_name; | |
76 joystick.GetControllerName(&stick_name); | |
77 SDL_joyport[numjoysticks] = strdup(name); | |
78 SDL_joyname[numjoysticks] = | |
79 strdup(stick_name.String()); | |
80 numjoysticks++; | |
81 joystick.Close(); | |
82 } | |
83 } | |
84 } | |
85 return(numjoysticks); | |
86 } | |
87 | |
88 /* Function to get the device-dependent name of a joystick */ | |
89 const char *SDL_SYS_JoystickName(int index) | |
90 { | |
91 return SDL_joyname[index]; | |
92 } | |
93 | |
94 /* Function to open a joystick for use. | |
95 The joystick to open is specified by the index field of the joystick. | |
96 This should fill the nbuttons and naxes fields of the joystick structure. | |
97 It returns 0, or -1 if there is an error. | |
98 */ | |
99 int SDL_SYS_JoystickOpen(SDL_Joystick *joystick) | |
100 { | |
101 BJoystick *stick; | |
102 | |
103 /* Create the joystick data structure */ | |
104 joystick->hwdata = (struct joystick_hwdata *) | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
105 SDL_malloc(sizeof(*joystick->hwdata)); |
0 | 106 if ( joystick->hwdata == NULL ) { |
107 SDL_OutOfMemory(); | |
108 return(-1); | |
109 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
110 SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata)); |
0 | 111 stick = new BJoystick; |
112 joystick->hwdata->stick = stick; | |
113 | |
114 /* Open the requested joystick for use */ | |
115 if ( stick->Open(SDL_joyport[joystick->index]) == B_ERROR ) { | |
116 SDL_SetError("Unable to open joystick"); | |
117 SDL_SYS_JoystickClose(joystick); | |
118 return(-1); | |
119 } | |
120 | |
121 /* Set the joystick to calibrated mode */ | |
122 stick->EnableCalibration(); | |
123 | |
124 /* Get the number of buttons, hats, and axes on the joystick */ | |
125 joystick->nbuttons = stick->CountButtons(); | |
126 joystick->naxes = stick->CountAxes(); | |
127 joystick->nhats = stick->CountHats(); | |
128 | |
129 joystick->hwdata->new_axes = (int16 *) | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
130 SDL_malloc(joystick->naxes*sizeof(int16)); |
0 | 131 joystick->hwdata->new_hats = (uint8 *) |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
132 SDL_malloc(joystick->nhats*sizeof(uint8)); |
0 | 133 if ( ! joystick->hwdata->new_hats || ! joystick->hwdata->new_axes ) { |
134 SDL_OutOfMemory(); | |
135 SDL_SYS_JoystickClose(joystick); | |
136 return(-1); | |
137 } | |
138 | |
139 /* We're done! */ | |
140 return(0); | |
141 } | |
142 | |
143 /* Function to update the state of a joystick - called as a device poll. | |
144 * This function shouldn't update the joystick structure directly, | |
145 * but instead should call SDL_PrivateJoystick*() to deliver events | |
146 * and update joystick device state. | |
147 */ | |
148 void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick) | |
149 { | |
150 static const Uint8 hat_map[9] = { | |
151 SDL_HAT_CENTERED, | |
152 SDL_HAT_UP, | |
153 SDL_HAT_RIGHTUP, | |
154 SDL_HAT_RIGHT, | |
155 SDL_HAT_RIGHTDOWN, | |
156 SDL_HAT_DOWN, | |
157 SDL_HAT_LEFTDOWN, | |
158 SDL_HAT_LEFT, | |
159 SDL_HAT_LEFTUP | |
160 }; | |
161 const int JITTER = (32768/10); /* 10% jitter threshold (ok?) */ | |
162 | |
163 BJoystick *stick; | |
164 int i, change; | |
165 int16 *axes; | |
166 uint8 *hats; | |
167 uint32 buttons; | |
168 | |
169 /* Set up data pointers */ | |
170 stick = joystick->hwdata->stick; | |
171 axes = joystick->hwdata->new_axes; | |
172 hats = joystick->hwdata->new_hats; | |
173 | |
174 /* Get the new joystick state */ | |
175 stick->Update(); | |
176 stick->GetAxisValues(axes); | |
177 stick->GetHatValues(hats); | |
178 buttons = stick->ButtonValues(); | |
179 | |
180 /* Generate axis motion events */ | |
181 for ( i=0; i<joystick->naxes; ++i ) { | |
182 change = ((int32)axes[i] - joystick->axes[i]); | |
183 if ( (change > JITTER) || (change < -JITTER) ) { | |
184 SDL_PrivateJoystickAxis(joystick, i, axes[i]); | |
185 } | |
186 } | |
187 | |
188 /* Generate hat change events */ | |
189 for ( i=0; i<joystick->nhats; ++i ) { | |
190 if ( hats[i] != joystick->hats[i] ) { | |
191 SDL_PrivateJoystickHat(joystick, i, hat_map[hats[i]]); | |
192 } | |
193 } | |
194 | |
195 /* Generate button events */ | |
196 for ( i=0; i<joystick->nbuttons; ++i ) { | |
197 if ( (buttons&0x01) != joystick->buttons[i] ) { | |
198 SDL_PrivateJoystickButton(joystick, i, (buttons&0x01)); | |
199 } | |
200 buttons >>= 1; | |
201 } | |
202 } | |
203 | |
204 /* Function to close a joystick after use */ | |
205 void SDL_SYS_JoystickClose(SDL_Joystick *joystick) | |
206 { | |
207 if ( joystick->hwdata ) { | |
208 joystick->hwdata->stick->Close(); | |
209 delete joystick->hwdata->stick; | |
210 if ( joystick->hwdata->new_hats ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
211 SDL_free(joystick->hwdata->new_hats); |
0 | 212 } |
213 if ( joystick->hwdata->new_axes ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
214 SDL_free(joystick->hwdata->new_axes); |
0 | 215 } |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
216 SDL_free(joystick->hwdata); |
0 | 217 joystick->hwdata = NULL; |
218 } | |
219 } | |
220 | |
221 /* Function to perform any system-specific joystick related cleanup */ | |
222 void SDL_SYS_JoystickQuit(void) | |
223 { | |
224 int i; | |
225 | |
226 for ( i=0; SDL_joyport[i]; ++i ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
227 SDL_free(SDL_joyport[i]); |
0 | 228 } |
229 SDL_joyport[0] = NULL; | |
230 | |
231 for ( i=0; SDL_joyname[i]; ++i ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
232 SDL_free(SDL_joyname[i]); |
0 | 233 } |
234 SDL_joyname[0] = NULL; | |
235 } | |
236 | |
237 }; // extern "C" |