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