Mercurial > sdl-ios-xcode
comparison src/joystick/beos/SDL_bejoystick.cc @ 1668:4da1ee79c9af SDL-1.3
more tweaking indent options
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 29 May 2006 04:04:35 +0000 |
parents | 782fd950bd46 |
children |
comparison
equal
deleted
inserted
replaced
1667:1fddae038bc8 | 1668:4da1ee79c9af |
---|---|
54 /* Function to scan the system for joysticks. | 54 /* Function to scan the system for joysticks. |
55 * This function should set SDL_numjoysticks to the number of available | 55 * This function should set SDL_numjoysticks to the number of available |
56 * joysticks. Joystick 0 should be the system default joystick. | 56 * joysticks. Joystick 0 should be the system default joystick. |
57 * It should return 0, or -1 on an unrecoverable fatal error. | 57 * It should return 0, or -1 on an unrecoverable fatal error. |
58 */ | 58 */ |
59 int SDL_SYS_JoystickInit (void) | 59 int SDL_SYS_JoystickInit(void) |
60 { | 60 { |
61 BJoystick joystick; | 61 BJoystick joystick; |
62 int numjoysticks; | 62 int numjoysticks; |
63 int i; | 63 int i; |
64 int32 nports; | 64 int32 nports; |
65 char name[B_OS_NAME_LENGTH]; | 65 char name[B_OS_NAME_LENGTH]; |
66 | 66 |
67 /* Search for attached joysticks */ | 67 /* Search for attached joysticks */ |
68 nports = joystick.CountDevices (); | 68 nports = joystick.CountDevices(); |
69 numjoysticks = 0; | 69 numjoysticks = 0; |
70 SDL_memset (SDL_joyport, 0, (sizeof SDL_joyport)); | 70 SDL_memset(SDL_joyport, 0, (sizeof SDL_joyport)); |
71 SDL_memset (SDL_joyname, 0, (sizeof SDL_joyname)); | 71 SDL_memset(SDL_joyname, 0, (sizeof SDL_joyname)); |
72 for (i = 0; (SDL_numjoysticks < MAX_JOYSTICKS) && (i < nports); ++i) | 72 for (i = 0; (SDL_numjoysticks < MAX_JOYSTICKS) && (i < nports); ++i) |
73 { | 73 { |
74 if (joystick.GetDeviceName (i, name) == B_OK) { | 74 if (joystick.GetDeviceName(i, name) == B_OK) { |
75 if (joystick.Open (name) != B_ERROR) { | 75 if (joystick.Open(name) != B_ERROR) { |
76 BString stick_name; | 76 BString stick_name; |
77 joystick.GetControllerName (&stick_name); | 77 joystick.GetControllerName(&stick_name); |
78 SDL_joyport[numjoysticks] = strdup (name); | 78 SDL_joyport[numjoysticks] = strdup(name); |
79 SDL_joyname[numjoysticks] = | 79 SDL_joyname[numjoysticks] = strdup(stick_name.String()); |
80 strdup (stick_name.String ()); | |
81 numjoysticks++; | 80 numjoysticks++; |
82 joystick.Close (); | 81 joystick.Close(); |
83 } | 82 } |
84 } | 83 } |
85 } | 84 } |
86 return (numjoysticks); | 85 return (numjoysticks); |
87 } | 86 } |
88 | 87 |
89 /* Function to get the device-dependent name of a joystick */ | 88 /* Function to get the device-dependent name of a joystick */ |
90 const char *SDL_SYS_JoystickName (int index) | 89 const char *SDL_SYS_JoystickName(int index) |
91 { | 90 { |
92 return SDL_joyname[index]; | 91 return SDL_joyname[index]; |
93 } | 92 } |
94 | 93 |
95 /* Function to open a joystick for use. | 94 /* Function to open a joystick for use. |
96 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. |
97 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. |
98 It returns 0, or -1 if there is an error. | 97 It returns 0, or -1 if there is an error. |
99 */ | 98 */ |
100 int SDL_SYS_JoystickOpen (SDL_Joystick * joystick) | 99 int SDL_SYS_JoystickOpen(SDL_Joystick * joystick) |
101 { | 100 { |
102 BJoystick *stick; | 101 BJoystick *stick; |
103 | 102 |
104 /* Create the joystick data structure */ | 103 /* Create the joystick data structure */ |
105 joystick->hwdata = (struct joystick_hwdata *) | 104 joystick->hwdata = (struct joystick_hwdata *) |
106 SDL_malloc (sizeof (*joystick->hwdata)); | 105 SDL_malloc(sizeof(*joystick->hwdata)); |
107 if (joystick->hwdata == NULL) { | 106 if (joystick->hwdata == NULL) { |
108 SDL_OutOfMemory (); | 107 SDL_OutOfMemory(); |
109 return (-1); | 108 return (-1); |
110 } | 109 } |
111 SDL_memset (joystick->hwdata, 0, sizeof (*joystick->hwdata)); | 110 SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata)); |
112 stick = new BJoystick; | 111 stick = new BJoystick; |
113 joystick->hwdata->stick = stick; | 112 joystick->hwdata->stick = stick; |
114 | 113 |
115 /* Open the requested joystick for use */ | 114 /* Open the requested joystick for use */ |
116 if (stick->Open (SDL_joyport[joystick->index]) == B_ERROR) { | 115 if (stick->Open(SDL_joyport[joystick->index]) == B_ERROR) { |
117 SDL_SetError ("Unable to open joystick"); | 116 SDL_SetError("Unable to open joystick"); |
118 SDL_SYS_JoystickClose (joystick); | 117 SDL_SYS_JoystickClose(joystick); |
119 return (-1); | 118 return (-1); |
120 } | 119 } |
121 | 120 |
122 /* Set the joystick to calibrated mode */ | 121 /* Set the joystick to calibrated mode */ |
123 stick->EnableCalibration (); | 122 stick->EnableCalibration(); |
124 | 123 |
125 /* Get the number of buttons, hats, and axes on the joystick */ | 124 /* Get the number of buttons, hats, and axes on the joystick */ |
126 joystick->nbuttons = stick->CountButtons (); | 125 joystick->nbuttons = stick->CountButtons(); |
127 joystick->naxes = stick->CountAxes (); | 126 joystick->naxes = stick->CountAxes(); |
128 joystick->nhats = stick->CountHats (); | 127 joystick->nhats = stick->CountHats(); |
129 | 128 |
130 joystick->hwdata->new_axes = (int16 *) | 129 joystick->hwdata->new_axes = (int16 *) |
131 SDL_malloc (joystick->naxes * sizeof (int16)); | 130 SDL_malloc(joystick->naxes * sizeof(int16)); |
132 joystick->hwdata->new_hats = (uint8 *) | 131 joystick->hwdata->new_hats = (uint8 *) |
133 SDL_malloc (joystick->nhats * sizeof (uint8)); | 132 SDL_malloc(joystick->nhats * sizeof(uint8)); |
134 if (!joystick->hwdata->new_hats || !joystick->hwdata->new_axes) { | 133 if (!joystick->hwdata->new_hats || !joystick->hwdata->new_axes) { |
135 SDL_OutOfMemory (); | 134 SDL_OutOfMemory(); |
136 SDL_SYS_JoystickClose (joystick); | 135 SDL_SYS_JoystickClose(joystick); |
137 return (-1); | 136 return (-1); |
138 } | 137 } |
139 | 138 |
140 /* We're done! */ | 139 /* We're done! */ |
141 return (0); | 140 return (0); |
144 /* 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. |
145 * This function shouldn't update the joystick structure directly, | 144 * This function shouldn't update the joystick structure directly, |
146 * but instead should call SDL_PrivateJoystick*() to deliver events | 145 * but instead should call SDL_PrivateJoystick*() to deliver events |
147 * and update joystick device state. | 146 * and update joystick device state. |
148 */ | 147 */ |
149 void SDL_SYS_JoystickUpdate (SDL_Joystick * joystick) | 148 void SDL_SYS_JoystickUpdate(SDL_Joystick * joystick) |
150 { | 149 { |
151 static const Uint8 hat_map[9] = { | 150 static const Uint8 hat_map[9] = { |
152 SDL_HAT_CENTERED, | 151 SDL_HAT_CENTERED, |
153 SDL_HAT_UP, | 152 SDL_HAT_UP, |
154 SDL_HAT_RIGHTUP, | 153 SDL_HAT_RIGHTUP, |
171 stick = joystick->hwdata->stick; | 170 stick = joystick->hwdata->stick; |
172 axes = joystick->hwdata->new_axes; | 171 axes = joystick->hwdata->new_axes; |
173 hats = joystick->hwdata->new_hats; | 172 hats = joystick->hwdata->new_hats; |
174 | 173 |
175 /* Get the new joystick state */ | 174 /* Get the new joystick state */ |
176 stick->Update (); | 175 stick->Update(); |
177 stick->GetAxisValues (axes); | 176 stick->GetAxisValues(axes); |
178 stick->GetHatValues (hats); | 177 stick->GetHatValues(hats); |
179 buttons = stick->ButtonValues (); | 178 buttons = stick->ButtonValues(); |
180 | 179 |
181 /* Generate axis motion events */ | 180 /* Generate axis motion events */ |
182 for (i = 0; i < joystick->naxes; ++i) { | 181 for (i = 0; i < joystick->naxes; ++i) { |
183 change = ((int32) axes[i] - joystick->axes[i]); | 182 change = ((int32) axes[i] - joystick->axes[i]); |
184 if ((change > JITTER) || (change < -JITTER)) { | 183 if ((change > JITTER) || (change < -JITTER)) { |
185 SDL_PrivateJoystickAxis (joystick, i, axes[i]); | 184 SDL_PrivateJoystickAxis(joystick, i, axes[i]); |
186 } | 185 } |
187 } | 186 } |
188 | 187 |
189 /* Generate hat change events */ | 188 /* Generate hat change events */ |
190 for (i = 0; i < joystick->nhats; ++i) { | 189 for (i = 0; i < joystick->nhats; ++i) { |
191 if (hats[i] != joystick->hats[i]) { | 190 if (hats[i] != joystick->hats[i]) { |
192 SDL_PrivateJoystickHat (joystick, i, hat_map[hats[i]]); | 191 SDL_PrivateJoystickHat(joystick, i, hat_map[hats[i]]); |
193 } | 192 } |
194 } | 193 } |
195 | 194 |
196 /* Generate button events */ | 195 /* Generate button events */ |
197 for (i = 0; i < joystick->nbuttons; ++i) { | 196 for (i = 0; i < joystick->nbuttons; ++i) { |
198 if ((buttons & 0x01) != joystick->buttons[i]) { | 197 if ((buttons & 0x01) != joystick->buttons[i]) { |
199 SDL_PrivateJoystickButton (joystick, i, (buttons & 0x01)); | 198 SDL_PrivateJoystickButton(joystick, i, (buttons & 0x01)); |
200 } | 199 } |
201 buttons >>= 1; | 200 buttons >>= 1; |
202 } | 201 } |
203 } | 202 } |
204 | 203 |
205 /* Function to close a joystick after use */ | 204 /* Function to close a joystick after use */ |
206 void SDL_SYS_JoystickClose (SDL_Joystick * joystick) | 205 void SDL_SYS_JoystickClose(SDL_Joystick * joystick) |
207 { | 206 { |
208 if (joystick->hwdata) { | 207 if (joystick->hwdata) { |
209 joystick->hwdata->stick->Close (); | 208 joystick->hwdata->stick->Close(); |
210 delete joystick->hwdata->stick; | 209 delete joystick->hwdata->stick; |
211 if (joystick->hwdata->new_hats) { | 210 if (joystick->hwdata->new_hats) { |
212 SDL_free (joystick->hwdata->new_hats); | 211 SDL_free(joystick->hwdata->new_hats); |
213 } | 212 } |
214 if (joystick->hwdata->new_axes) { | 213 if (joystick->hwdata->new_axes) { |
215 SDL_free (joystick->hwdata->new_axes); | 214 SDL_free(joystick->hwdata->new_axes); |
216 } | 215 } |
217 SDL_free (joystick->hwdata); | 216 SDL_free(joystick->hwdata); |
218 joystick->hwdata = NULL; | 217 joystick->hwdata = NULL; |
219 } | 218 } |
220 } | 219 } |
221 | 220 |
222 /* Function to perform any system-specific joystick related cleanup */ | 221 /* Function to perform any system-specific joystick related cleanup */ |
223 void SDL_SYS_JoystickQuit (void) | 222 void SDL_SYS_JoystickQuit(void) |
224 { | 223 { |
225 int i; | 224 int i; |
226 | 225 |
227 for (i = 0; SDL_joyport[i]; ++i) { | 226 for (i = 0; SDL_joyport[i]; ++i) { |
228 SDL_free (SDL_joyport[i]); | 227 SDL_free(SDL_joyport[i]); |
229 } | 228 } |
230 SDL_joyport[0] = NULL; | 229 SDL_joyport[0] = NULL; |
231 | 230 |
232 for (i = 0; SDL_joyname[i]; ++i) { | 231 for (i = 0; SDL_joyname[i]; ++i) { |
233 SDL_free (SDL_joyname[i]); | 232 SDL_free(SDL_joyname[i]); |
234 } | 233 } |
235 SDL_joyname[0] = NULL; | 234 SDL_joyname[0] = NULL; |
236 } | 235 } |
237 | 236 |
238 }; // extern "C" | 237 }; // extern "C" |