Mercurial > sdl-ios-xcode
annotate src/joystick/beos/SDL_bejoystick.cc @ 1479:993ef9ff4688
Updated MacOS X support
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 07 Mar 2006 04:58:30 +0000 |
parents | 376665398b25 |
children | 92947e3a18db |
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 */ |
1403
376665398b25
Catch the C++ and Objective C sources too...
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
22 #include "SDL_config.h" |
0 | 23 |
24 /* This is the system specific header for the SDL joystick API */ | |
25 | |
26 #include <be/support/String.h> | |
27 #include <be/device/Joystick.h> | |
28 | |
29 extern "C" { | |
30 | |
31 #include "SDL_joystick.h" | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
32 #include "../SDL_sysjoystick.h" |
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
33 #include "../SDL_joystick_c.h" |
0 | 34 |
35 | |
36 /* The maximum number of joysticks we'll detect */ | |
37 #define MAX_JOYSTICKS 16 | |
38 | |
39 /* A list of available joysticks */ | |
40 static char *SDL_joyport[MAX_JOYSTICKS]; | |
41 static char *SDL_joyname[MAX_JOYSTICKS]; | |
42 | |
43 /* The private structure used to keep track of a joystick */ | |
44 struct joystick_hwdata { | |
45 BJoystick *stick; | |
46 uint8 *new_hats; | |
47 int16 *new_axes; | |
48 }; | |
49 | |
50 /* Function to scan the system for joysticks. | |
51 * This function should set SDL_numjoysticks to the number of available | |
52 * joysticks. Joystick 0 should be the system default joystick. | |
53 * It should return 0, or -1 on an unrecoverable fatal error. | |
54 */ | |
55 int SDL_SYS_JoystickInit(void) | |
56 { | |
57 BJoystick joystick; | |
58 int numjoysticks; | |
59 int i; | |
60 int32 nports; | |
61 char name[B_OS_NAME_LENGTH]; | |
62 | |
63 /* Search for attached joysticks */ | |
64 nports = joystick.CountDevices(); | |
65 numjoysticks = 0; | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
66 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
|
67 SDL_memset(SDL_joyname, 0, (sizeof SDL_joyname)); |
0 | 68 for ( i=0; (SDL_numjoysticks < MAX_JOYSTICKS) && (i < nports); ++i ) { |
69 if ( joystick.GetDeviceName(i, name) == B_OK ) { | |
70 if ( joystick.Open(name) != B_ERROR ) { | |
71 BString stick_name; | |
72 joystick.GetControllerName(&stick_name); | |
73 SDL_joyport[numjoysticks] = strdup(name); | |
74 SDL_joyname[numjoysticks] = | |
75 strdup(stick_name.String()); | |
76 numjoysticks++; | |
77 joystick.Close(); | |
78 } | |
79 } | |
80 } | |
81 return(numjoysticks); | |
82 } | |
83 | |
84 /* Function to get the device-dependent name of a joystick */ | |
85 const char *SDL_SYS_JoystickName(int index) | |
86 { | |
87 return SDL_joyname[index]; | |
88 } | |
89 | |
90 /* Function to open a joystick for use. | |
91 The joystick to open is specified by the index field of the joystick. | |
92 This should fill the nbuttons and naxes fields of the joystick structure. | |
93 It returns 0, or -1 if there is an error. | |
94 */ | |
95 int SDL_SYS_JoystickOpen(SDL_Joystick *joystick) | |
96 { | |
97 BJoystick *stick; | |
98 | |
99 /* Create the joystick data structure */ | |
100 joystick->hwdata = (struct joystick_hwdata *) | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
101 SDL_malloc(sizeof(*joystick->hwdata)); |
0 | 102 if ( joystick->hwdata == NULL ) { |
103 SDL_OutOfMemory(); | |
104 return(-1); | |
105 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
106 SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata)); |
0 | 107 stick = new BJoystick; |
108 joystick->hwdata->stick = stick; | |
109 | |
110 /* Open the requested joystick for use */ | |
111 if ( stick->Open(SDL_joyport[joystick->index]) == B_ERROR ) { | |
112 SDL_SetError("Unable to open joystick"); | |
113 SDL_SYS_JoystickClose(joystick); | |
114 return(-1); | |
115 } | |
116 | |
117 /* Set the joystick to calibrated mode */ | |
118 stick->EnableCalibration(); | |
119 | |
120 /* Get the number of buttons, hats, and axes on the joystick */ | |
121 joystick->nbuttons = stick->CountButtons(); | |
122 joystick->naxes = stick->CountAxes(); | |
123 joystick->nhats = stick->CountHats(); | |
124 | |
125 joystick->hwdata->new_axes = (int16 *) | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
126 SDL_malloc(joystick->naxes*sizeof(int16)); |
0 | 127 joystick->hwdata->new_hats = (uint8 *) |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
128 SDL_malloc(joystick->nhats*sizeof(uint8)); |
0 | 129 if ( ! joystick->hwdata->new_hats || ! joystick->hwdata->new_axes ) { |
130 SDL_OutOfMemory(); | |
131 SDL_SYS_JoystickClose(joystick); | |
132 return(-1); | |
133 } | |
134 | |
135 /* We're done! */ | |
136 return(0); | |
137 } | |
138 | |
139 /* Function to update the state of a joystick - called as a device poll. | |
140 * This function shouldn't update the joystick structure directly, | |
141 * but instead should call SDL_PrivateJoystick*() to deliver events | |
142 * and update joystick device state. | |
143 */ | |
144 void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick) | |
145 { | |
146 static const Uint8 hat_map[9] = { | |
147 SDL_HAT_CENTERED, | |
148 SDL_HAT_UP, | |
149 SDL_HAT_RIGHTUP, | |
150 SDL_HAT_RIGHT, | |
151 SDL_HAT_RIGHTDOWN, | |
152 SDL_HAT_DOWN, | |
153 SDL_HAT_LEFTDOWN, | |
154 SDL_HAT_LEFT, | |
155 SDL_HAT_LEFTUP | |
156 }; | |
157 const int JITTER = (32768/10); /* 10% jitter threshold (ok?) */ | |
158 | |
159 BJoystick *stick; | |
160 int i, change; | |
161 int16 *axes; | |
162 uint8 *hats; | |
163 uint32 buttons; | |
164 | |
165 /* Set up data pointers */ | |
166 stick = joystick->hwdata->stick; | |
167 axes = joystick->hwdata->new_axes; | |
168 hats = joystick->hwdata->new_hats; | |
169 | |
170 /* Get the new joystick state */ | |
171 stick->Update(); | |
172 stick->GetAxisValues(axes); | |
173 stick->GetHatValues(hats); | |
174 buttons = stick->ButtonValues(); | |
175 | |
176 /* Generate axis motion events */ | |
177 for ( i=0; i<joystick->naxes; ++i ) { | |
178 change = ((int32)axes[i] - joystick->axes[i]); | |
179 if ( (change > JITTER) || (change < -JITTER) ) { | |
180 SDL_PrivateJoystickAxis(joystick, i, axes[i]); | |
181 } | |
182 } | |
183 | |
184 /* Generate hat change events */ | |
185 for ( i=0; i<joystick->nhats; ++i ) { | |
186 if ( hats[i] != joystick->hats[i] ) { | |
187 SDL_PrivateJoystickHat(joystick, i, hat_map[hats[i]]); | |
188 } | |
189 } | |
190 | |
191 /* Generate button events */ | |
192 for ( i=0; i<joystick->nbuttons; ++i ) { | |
193 if ( (buttons&0x01) != joystick->buttons[i] ) { | |
194 SDL_PrivateJoystickButton(joystick, i, (buttons&0x01)); | |
195 } | |
196 buttons >>= 1; | |
197 } | |
198 } | |
199 | |
200 /* Function to close a joystick after use */ | |
201 void SDL_SYS_JoystickClose(SDL_Joystick *joystick) | |
202 { | |
203 if ( joystick->hwdata ) { | |
204 joystick->hwdata->stick->Close(); | |
205 delete joystick->hwdata->stick; | |
206 if ( joystick->hwdata->new_hats ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
207 SDL_free(joystick->hwdata->new_hats); |
0 | 208 } |
209 if ( joystick->hwdata->new_axes ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
210 SDL_free(joystick->hwdata->new_axes); |
0 | 211 } |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
212 SDL_free(joystick->hwdata); |
0 | 213 joystick->hwdata = NULL; |
214 } | |
215 } | |
216 | |
217 /* Function to perform any system-specific joystick related cleanup */ | |
218 void SDL_SYS_JoystickQuit(void) | |
219 { | |
220 int i; | |
221 | |
222 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
|
223 SDL_free(SDL_joyport[i]); |
0 | 224 } |
225 SDL_joyport[0] = NULL; | |
226 | |
227 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
|
228 SDL_free(SDL_joyname[i]); |
0 | 229 } |
230 SDL_joyname[0] = NULL; | |
231 } | |
232 | |
233 }; // extern "C" |