comparison src/joystick/SDL_joystick.c @ 3608:a5a37f850d83

Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
author Ryan C. Gordon <icculus@icculus.org>
date Wed, 06 Jan 2010 06:40:16 +0000
parents 71bb88152e3f
children f7b03b6838cb
comparison
equal deleted inserted replaced
3607:8b4c0320638e 3608:a5a37f850d83
112 } 112 }
113 } 113 }
114 114
115 /* Create and initialize the joystick */ 115 /* Create and initialize the joystick */
116 joystick = (SDL_Joystick *) SDL_malloc((sizeof *joystick)); 116 joystick = (SDL_Joystick *) SDL_malloc((sizeof *joystick));
117 if (joystick != NULL) { 117 if (joystick == NULL) {
118 SDL_memset(joystick, 0, (sizeof *joystick)); 118 SDL_OutOfMemory();
119 joystick->index = device_index; 119 return NULL;
120 if (SDL_SYS_JoystickOpen(joystick) < 0) { 120 }
121 SDL_free(joystick); 121
122 joystick = NULL; 122 SDL_memset(joystick, 0, (sizeof *joystick));
123 } else { 123 joystick->index = device_index;
124 if (joystick->naxes > 0) { 124 if (SDL_SYS_JoystickOpen(joystick) < 0) {
125 joystick->axes = (Sint16 *) SDL_malloc 125 SDL_free(joystick);
126 (joystick->naxes * sizeof(Sint16)); 126 return NULL;
127 } 127 }
128 if (joystick->nhats > 0) { 128 if (joystick->naxes > 0) {
129 joystick->hats = (Uint8 *) SDL_malloc 129 joystick->axes = (Sint16 *) SDL_malloc
130 (joystick->nhats * sizeof(Uint8)); 130 (joystick->naxes * sizeof(Sint16));
131 } 131 }
132 if (joystick->nballs > 0) { 132 if (joystick->nhats > 0) {
133 joystick->balls = (struct balldelta *) SDL_malloc 133 joystick->hats = (Uint8 *) SDL_malloc
134 (joystick->nballs * sizeof(*joystick->balls)); 134 (joystick->nhats * sizeof(Uint8));
135 } 135 }
136 if (joystick->nbuttons > 0) { 136 if (joystick->nballs > 0) {
137 joystick->buttons = (Uint8 *) SDL_malloc 137 joystick->balls = (struct balldelta *) SDL_malloc
138 (joystick->nbuttons * sizeof(Uint8)); 138 (joystick->nballs * sizeof(*joystick->balls));
139 } 139 }
140 if (((joystick->naxes > 0) && !joystick->axes) 140 if (joystick->nbuttons > 0) {
141 || ((joystick->nhats > 0) && !joystick->hats) 141 joystick->buttons = (Uint8 *) SDL_malloc
142 || ((joystick->nballs > 0) && !joystick->balls) 142 (joystick->nbuttons * sizeof(Uint8));
143 || ((joystick->nbuttons > 0) && !joystick->buttons)) { 143 }
144 SDL_OutOfMemory(); 144 if (((joystick->naxes > 0) && !joystick->axes)
145 SDL_JoystickClose(joystick); 145 || ((joystick->nhats > 0) && !joystick->hats)
146 joystick = NULL; 146 || ((joystick->nballs > 0) && !joystick->balls)
147 } 147 || ((joystick->nbuttons > 0) && !joystick->buttons)) {
148 if (joystick->axes) { 148 SDL_OutOfMemory();
149 SDL_memset(joystick->axes, 0, 149 SDL_JoystickClose(joystick);
150 joystick->naxes * sizeof(Sint16)); 150 return NULL;
151 } 151 }
152 if (joystick->hats) { 152 if (joystick->axes) {
153 SDL_memset(joystick->hats, 0, 153 SDL_memset(joystick->axes, 0, joystick->naxes * sizeof(Sint16));
154 joystick->nhats * sizeof(Uint8)); 154 }
155 } 155 if (joystick->hats) {
156 if (joystick->balls) { 156 SDL_memset(joystick->hats, 0, joystick->nhats * sizeof(Uint8));
157 SDL_memset(joystick->balls, 0, 157 }
158 joystick->nballs * sizeof(*joystick->balls)); 158 if (joystick->balls) {
159 } 159 SDL_memset(joystick->balls, 0,
160 if (joystick->buttons) { 160 joystick->nballs * sizeof(*joystick->balls));
161 SDL_memset(joystick->buttons, 0, 161 }
162 joystick->nbuttons * sizeof(Uint8)); 162 if (joystick->buttons) {
163 } 163 SDL_memset(joystick->buttons, 0, joystick->nbuttons * sizeof(Uint8));
164 } 164 }
165 } 165
166 if (joystick) { 166 /* Add joystick to list */
167 /* Add joystick to list */ 167 ++joystick->ref_count;
168 ++joystick->ref_count; 168 SDL_Lock_EventThread();
169 SDL_Lock_EventThread(); 169 for (i = 0; SDL_joysticks[i]; ++i)
170 for (i = 0; SDL_joysticks[i]; ++i) 170 /* Skip to next joystick */ ;
171 /* Skip to next joystick */ ; 171 SDL_joysticks[i] = joystick;
172 SDL_joysticks[i] = joystick; 172 SDL_Unlock_EventThread();
173 SDL_Unlock_EventThread(); 173
174 }
175 return (joystick); 174 return (joystick);
176 } 175 }
177 176
178 /* 177 /*
179 * Returns 1 if the joystick has been opened, or 0 if it has not. 178 * Returns 1 if the joystick has been opened, or 0 if it has not.