comparison src/haptic/SDL_haptic.c @ 2490:be9b206d44af gsoc2008_force_feedback

Some more error checking. Implemented SDL_HapticOpenFromJoystick().
author Edgar Simo <bobbens@gmail.com>
date Wed, 02 Jul 2008 09:52:44 +0000
parents 96adc8025331
children 10bc7aaf5114
comparison
equal deleted inserted replaced
2489:96adc8025331 2490:be9b206d44af
121 return NULL; 121 return NULL;
122 } 122 }
123 123
124 /* Add haptic to list */ 124 /* Add haptic to list */
125 ++haptic->ref_count; 125 ++haptic->ref_count;
126 for (i = 0; SDL_haptics[i]; ++i) 126 for (i=0; SDL_haptics[i]; i++)
127 /* Skip to next haptic */ ; 127 /* Skip to next haptic */ ;
128 SDL_haptics[i] = haptic; 128 SDL_haptics[i] = haptic;
129 129
130 return haptic; 130 return haptic;
131 } 131 }
137 int 137 int
138 SDL_JoystickIsHaptic(SDL_Joystick * joystick) 138 SDL_JoystickIsHaptic(SDL_Joystick * joystick)
139 { 139 {
140 int ret; 140 int ret;
141 141
142 /* Must be a valid joystick */
142 if (!SDL_PrivateJoystickValid(&joystick)) { 143 if (!SDL_PrivateJoystickValid(&joystick)) {
143 return -1; 144 return -1;
144 } 145 }
145 146
146 ret = SDL_SYS_JoystickIsHaptic(joystick); 147 ret = SDL_SYS_JoystickIsHaptic(joystick);
155 * Opens a haptic device from a joystick. 156 * Opens a haptic device from a joystick.
156 */ 157 */
157 SDL_Haptic * 158 SDL_Haptic *
158 SDL_HapticOpenFromJoystick(SDL_Joystick * joystick) 159 SDL_HapticOpenFromJoystick(SDL_Joystick * joystick)
159 { 160 {
161 int i;
162 SDL_Haptic *haptic;
163
164 /* Must be a valid joystick */
160 if (!SDL_PrivateJoystickValid(&joystick)) { 165 if (!SDL_PrivateJoystickValid(&joystick)) {
161 return -1; 166 return NULL;
162 } 167 }
163 return -1; 168
169 /* Joystick must be haptic */
170 if (SDL_SYS_JoystickIsHaptic(joystick) <= 0) {
171 return NULL;
172 }
173
174 /* Check to see if joystick's haptic is already open */
175 for (i=0; SDL_haptics[i]; i++) {
176 if (SDL_SYS_JoystickSameHaptic(&SDL_haptics[i],joystick)) {
177 haptic = SDL_haptics[i];
178 ++haptic->ref_count;
179 return haptic;
180 }
181 }
182
183 /* Create the haptic device */
184 haptic = (SDL_Haptic *) SDL_malloc((sizeof *haptic));
185 if (haptic == NULL) {
186 SDL_OutOfMemory();
187 return NULL;
188 }
189
190 /* Initialize the haptic device */
191 SDL_memset(haptic, 0, (sizeof *haptic));
192 if (SDL_SYS_HapticOpenFromJoystick(haptic,joystick) < 0) {
193 SDL_free(haptic);
194 return NULL;
195 }
196
197 /* Add haptic to list */
198 ++haptic->ref_count;
199 for (i=0; SDL_haptics[i]; i++)
200 /* Skip to next haptic */ ;
201 SDL_haptics[i] = haptic;
202
203 return haptic;
164 } 204 }
165 205
166 206
167 /* 207 /*
168 * Checks to see if the haptic device is valid 208 * Checks to see if the haptic device is valid