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