comparison src/joystick/SDL_joystick.c @ 4147:6bdec986f2f6 SDL-1.2

Fix bug 545, by returning early whenever an error occurs when initializing joystick structure
author Patrice Mandin <patmandin@gmail.com>
date Sat, 19 Jul 2008 11:54:55 +0000
parents 97d0966f4bf7
children a1b03ba2fcd0
comparison
equal deleted inserted replaced
4146:dc726b233f5f 4147:6bdec986f2f6
110 } 110 }
111 } 111 }
112 112
113 /* Create and initialize the joystick */ 113 /* Create and initialize the joystick */
114 joystick = (SDL_Joystick *)SDL_malloc((sizeof *joystick)); 114 joystick = (SDL_Joystick *)SDL_malloc((sizeof *joystick));
115 if ( joystick != NULL ) { 115 if ( !joystick ) {
116 SDL_memset(joystick, 0, (sizeof *joystick)); 116 return(NULL);
117 joystick->index = device_index; 117 }
118 if ( SDL_SYS_JoystickOpen(joystick) < 0 ) { 118
119 SDL_free(joystick); 119 SDL_memset(joystick, 0, (sizeof *joystick));
120 joystick = NULL; 120 joystick->index = device_index;
121 } else { 121 if ( SDL_SYS_JoystickOpen(joystick) < 0 ) {
122 if ( joystick->naxes > 0 ) { 122 SDL_free(joystick);
123 joystick->axes = (Sint16 *)SDL_malloc 123 return(NULL);
124 (joystick->naxes*sizeof(Sint16)); 124 }
125 } 125
126 if ( joystick->nhats > 0 ) { 126 if ( joystick->naxes > 0 ) {
127 joystick->hats = (Uint8 *)SDL_malloc 127 joystick->axes = (Sint16 *)SDL_malloc
128 (joystick->nhats*sizeof(Uint8)); 128 (joystick->naxes*sizeof(Sint16));
129 } 129 }
130 if ( joystick->nballs > 0 ) { 130 if ( joystick->nhats > 0 ) {
131 joystick->balls = (struct balldelta *)SDL_malloc 131 joystick->hats = (Uint8 *)SDL_malloc
132 (joystick->nballs*sizeof(*joystick->balls)); 132 (joystick->nhats*sizeof(Uint8));
133 } 133 }
134 if ( joystick->nbuttons > 0 ) { 134 if ( joystick->nballs > 0 ) {
135 joystick->buttons = (Uint8 *)SDL_malloc 135 joystick->balls = (struct balldelta *)SDL_malloc
136 (joystick->nbuttons*sizeof(Uint8)); 136 (joystick->nballs*sizeof(*joystick->balls));
137 } 137 }
138 if ( ((joystick->naxes > 0) && !joystick->axes) 138 if ( joystick->nbuttons > 0 ) {
139 || ((joystick->nhats > 0) && !joystick->hats) 139 joystick->buttons = (Uint8 *)SDL_malloc
140 || ((joystick->nballs > 0) && !joystick->balls) 140 (joystick->nbuttons*sizeof(Uint8));
141 || ((joystick->nbuttons > 0) && !joystick->buttons)) { 141 }
142 SDL_OutOfMemory(); 142 if ( ((joystick->naxes > 0) && !joystick->axes)
143 SDL_JoystickClose(joystick); 143 || ((joystick->nhats > 0) && !joystick->hats)
144 joystick = NULL; 144 || ((joystick->nballs > 0) && !joystick->balls)
145 } 145 || ((joystick->nbuttons > 0) && !joystick->buttons)) {
146 if ( joystick->axes ) { 146 SDL_OutOfMemory();
147 SDL_memset(joystick->axes, 0, 147 SDL_JoystickClose(joystick);
148 joystick->naxes*sizeof(Sint16)); 148 return(NULL);
149 } 149 }
150 if ( joystick->hats ) { 150
151 SDL_memset(joystick->hats, 0, 151 if ( joystick->axes ) {
152 joystick->nhats*sizeof(Uint8)); 152 SDL_memset(joystick->axes, 0,
153 } 153 joystick->naxes*sizeof(Sint16));
154 if ( joystick->balls ) { 154 }
155 SDL_memset(joystick->balls, 0, 155 if ( joystick->hats ) {
156 joystick->nballs*sizeof(*joystick->balls)); 156 SDL_memset(joystick->hats, 0,
157 } 157 joystick->nhats*sizeof(Uint8));
158 if ( joystick->buttons ) { 158 }
159 SDL_memset(joystick->buttons, 0, 159 if ( joystick->balls ) {
160 joystick->nbuttons*sizeof(Uint8)); 160 SDL_memset(joystick->balls, 0,
161 } 161 joystick->nballs*sizeof(*joystick->balls));
162 } 162 }
163 } 163 if ( joystick->buttons ) {
164 if ( joystick ) { 164 SDL_memset(joystick->buttons, 0,
165 /* Add joystick to list */ 165 joystick->nbuttons*sizeof(Uint8));
166 ++joystick->ref_count; 166 }
167 SDL_Lock_EventThread(); 167
168 for ( i=0; SDL_joysticks[i]; ++i ) 168 /* Add joystick to list */
169 /* Skip to next joystick */; 169 ++joystick->ref_count;
170 SDL_joysticks[i] = joystick; 170 SDL_Lock_EventThread();
171 SDL_Unlock_EventThread(); 171 for ( i=0; SDL_joysticks[i]; ++i )
172 } 172 /* Skip to next joystick */ ;
173 SDL_joysticks[i] = joystick;
174 SDL_Unlock_EventThread();
175
173 return(joystick); 176 return(joystick);
174 } 177 }
175 178
176 /* 179 /*
177 * Returns 1 if the joystick has been opened, or 0 if it has not. 180 * Returns 1 if the joystick has been opened, or 0 if it has not.