Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
authorRyan C. Gordon
Wed, 06 Jan 2010 06:40:16 +0000
changeset 3608a5a37f850d83
parent 3607 8b4c0320638e
child 3609 72a1c4dda07c
Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
src/joystick/SDL_joystick.c
     1.1 --- a/src/joystick/SDL_joystick.c	Wed Jan 06 06:12:01 2010 +0000
     1.2 +++ b/src/joystick/SDL_joystick.c	Wed Jan 06 06:40:16 2010 +0000
     1.3 @@ -114,64 +114,63 @@
     1.4  
     1.5      /* Create and initialize the joystick */
     1.6      joystick = (SDL_Joystick *) SDL_malloc((sizeof *joystick));
     1.7 -    if (joystick != NULL) {
     1.8 -        SDL_memset(joystick, 0, (sizeof *joystick));
     1.9 -        joystick->index = device_index;
    1.10 -        if (SDL_SYS_JoystickOpen(joystick) < 0) {
    1.11 -            SDL_free(joystick);
    1.12 -            joystick = NULL;
    1.13 -        } else {
    1.14 -            if (joystick->naxes > 0) {
    1.15 -                joystick->axes = (Sint16 *) SDL_malloc
    1.16 -                    (joystick->naxes * sizeof(Sint16));
    1.17 -            }
    1.18 -            if (joystick->nhats > 0) {
    1.19 -                joystick->hats = (Uint8 *) SDL_malloc
    1.20 -                    (joystick->nhats * sizeof(Uint8));
    1.21 -            }
    1.22 -            if (joystick->nballs > 0) {
    1.23 -                joystick->balls = (struct balldelta *) SDL_malloc
    1.24 -                    (joystick->nballs * sizeof(*joystick->balls));
    1.25 -            }
    1.26 -            if (joystick->nbuttons > 0) {
    1.27 -                joystick->buttons = (Uint8 *) SDL_malloc
    1.28 -                    (joystick->nbuttons * sizeof(Uint8));
    1.29 -            }
    1.30 -            if (((joystick->naxes > 0) && !joystick->axes)
    1.31 -                || ((joystick->nhats > 0) && !joystick->hats)
    1.32 -                || ((joystick->nballs > 0) && !joystick->balls)
    1.33 -                || ((joystick->nbuttons > 0) && !joystick->buttons)) {
    1.34 -                SDL_OutOfMemory();
    1.35 -                SDL_JoystickClose(joystick);
    1.36 -                joystick = NULL;
    1.37 -            }
    1.38 -            if (joystick->axes) {
    1.39 -                SDL_memset(joystick->axes, 0,
    1.40 -                           joystick->naxes * sizeof(Sint16));
    1.41 -            }
    1.42 -            if (joystick->hats) {
    1.43 -                SDL_memset(joystick->hats, 0,
    1.44 -                           joystick->nhats * sizeof(Uint8));
    1.45 -            }
    1.46 -            if (joystick->balls) {
    1.47 -                SDL_memset(joystick->balls, 0,
    1.48 -                           joystick->nballs * sizeof(*joystick->balls));
    1.49 -            }
    1.50 -            if (joystick->buttons) {
    1.51 -                SDL_memset(joystick->buttons, 0,
    1.52 -                           joystick->nbuttons * sizeof(Uint8));
    1.53 -            }
    1.54 -        }
    1.55 +    if (joystick == NULL) {
    1.56 +        SDL_OutOfMemory();
    1.57 +        return NULL;
    1.58 +    }
    1.59 +
    1.60 +    SDL_memset(joystick, 0, (sizeof *joystick));
    1.61 +    joystick->index = device_index;
    1.62 +    if (SDL_SYS_JoystickOpen(joystick) < 0) {
    1.63 +        SDL_free(joystick);
    1.64 +        return NULL;
    1.65 +    }
    1.66 +    if (joystick->naxes > 0) {
    1.67 +        joystick->axes = (Sint16 *) SDL_malloc
    1.68 +            (joystick->naxes * sizeof(Sint16));
    1.69 +    }
    1.70 +    if (joystick->nhats > 0) {
    1.71 +        joystick->hats = (Uint8 *) SDL_malloc
    1.72 +            (joystick->nhats * sizeof(Uint8));
    1.73 +    }
    1.74 +    if (joystick->nballs > 0) {
    1.75 +        joystick->balls = (struct balldelta *) SDL_malloc
    1.76 +            (joystick->nballs * sizeof(*joystick->balls));
    1.77 +    }
    1.78 +    if (joystick->nbuttons > 0) {
    1.79 +        joystick->buttons = (Uint8 *) SDL_malloc
    1.80 +            (joystick->nbuttons * sizeof(Uint8));
    1.81      }
    1.82 -    if (joystick) {
    1.83 -        /* Add joystick to list */
    1.84 -        ++joystick->ref_count;
    1.85 -        SDL_Lock_EventThread();
    1.86 -        for (i = 0; SDL_joysticks[i]; ++i)
    1.87 -            /* Skip to next joystick */ ;
    1.88 -        SDL_joysticks[i] = joystick;
    1.89 -        SDL_Unlock_EventThread();
    1.90 +    if (((joystick->naxes > 0) && !joystick->axes)
    1.91 +        || ((joystick->nhats > 0) && !joystick->hats)
    1.92 +        || ((joystick->nballs > 0) && !joystick->balls)
    1.93 +        || ((joystick->nbuttons > 0) && !joystick->buttons)) {
    1.94 +        SDL_OutOfMemory();
    1.95 +        SDL_JoystickClose(joystick);
    1.96 +        return NULL;
    1.97 +    }
    1.98 +    if (joystick->axes) {
    1.99 +        SDL_memset(joystick->axes, 0, joystick->naxes * sizeof(Sint16));
   1.100 +    }
   1.101 +    if (joystick->hats) {
   1.102 +        SDL_memset(joystick->hats, 0, joystick->nhats * sizeof(Uint8));
   1.103      }
   1.104 +    if (joystick->balls) {
   1.105 +        SDL_memset(joystick->balls, 0,
   1.106 +            joystick->nballs * sizeof(*joystick->balls));
   1.107 +    }
   1.108 +    if (joystick->buttons) {
   1.109 +        SDL_memset(joystick->buttons, 0, joystick->nbuttons * sizeof(Uint8));
   1.110 +    }
   1.111 +
   1.112 +    /* Add joystick to list */
   1.113 +    ++joystick->ref_count;
   1.114 +    SDL_Lock_EventThread();
   1.115 +    for (i = 0; SDL_joysticks[i]; ++i)
   1.116 +        /* Skip to next joystick */ ;
   1.117 +    SDL_joysticks[i] = joystick;
   1.118 +    SDL_Unlock_EventThread();
   1.119 +
   1.120      return (joystick);
   1.121  }
   1.122