src/haptic/windows/SDL_syshaptic.c
changeset 7319 8bdc5aac93d7
parent 7191 75360622e65f
child 7320 314c488ab516
     1.1 --- a/src/haptic/windows/SDL_syshaptic.c	Tue Jun 25 20:21:31 2013 -0700
     1.2 +++ b/src/haptic/windows/SDL_syshaptic.c	Thu Jun 27 10:59:30 2013 -0700
     1.3 @@ -53,7 +53,7 @@
     1.4  {
     1.5      LPDIRECTINPUTDEVICE8 device;
     1.6      DWORD axes[3];              /* Axes to use. */
     1.7 -    int is_joystick;            /* Device is loaded as joystick. */
     1.8 +    SDL_bool is_joystick;       /* Device is loaded as joystick. */
     1.9      Uint8 bXInputHaptic; /* Supports force feedback via XInput. */
    1.10      Uint8 userid; /* XInput userid index for this joystick */
    1.11  };
    1.12 @@ -92,7 +92,8 @@
    1.13  static int SDL_SYS_HapticOpenFromInstance(SDL_Haptic * haptic,
    1.14                                            DIDEVICEINSTANCE instance);
    1.15  static int SDL_SYS_HapticOpenFromDevice8(SDL_Haptic * haptic,
    1.16 -                                         LPDIRECTINPUTDEVICE8 device8);
    1.17 +                                         LPDIRECTINPUTDEVICE8 device8,
    1.18 +                                         SDL_bool is_joystick);
    1.19  static int SDL_SYS_HapticOpenFromXInput(SDL_Haptic * haptic, Uint8 userid);
    1.20  static DWORD DIGetTriggerButton(Uint16 button);
    1.21  static int SDL_SYS_SetDirection(DIEFFECT * effect, SDL_HapticDirection * dir,
    1.22 @@ -342,51 +343,34 @@
    1.23      HRESULT ret;
    1.24      int ret2;
    1.25      LPDIRECTINPUTDEVICE8 device;
    1.26 -
    1.27 -    /* Allocate the hwdata */
    1.28 -    haptic->hwdata = (struct haptic_hwdata *)
    1.29 -        SDL_malloc(sizeof(*haptic->hwdata));
    1.30 -    if (haptic->hwdata == NULL) {
    1.31 -        SDL_OutOfMemory();
    1.32 -        goto creat_err;
    1.33 -    }
    1.34 -    SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
    1.35 +    LPDIRECTINPUTDEVICE8 device8;
    1.36  
    1.37      /* Open the device */
    1.38      ret = IDirectInput8_CreateDevice(dinput, &instance.guidInstance,
    1.39                                      &device, NULL);
    1.40      if (FAILED(ret)) {
    1.41          DI_SetError("Creating DirectInput device", ret);
    1.42 -        goto creat_err;
    1.43 +        return -1;
    1.44      }
    1.45  
    1.46      /* Now get the IDirectInputDevice8 interface, instead. */
    1.47      ret = IDirectInputDevice8_QueryInterface(device,
    1.48                                              &IID_IDirectInputDevice8,
    1.49 -                                            (LPVOID *) & haptic->hwdata->
    1.50 -                                            device);
    1.51 +                                            (LPVOID *) &device8);
    1.52      /* Done with the temporary one now. */
    1.53      IDirectInputDevice8_Release(device);
    1.54      if (FAILED(ret)) {
    1.55          DI_SetError("Querying DirectInput interface", ret);
    1.56 -        goto creat_err;
    1.57 +        return -1;
    1.58      }
    1.59  
    1.60 -    ret2 = SDL_SYS_HapticOpenFromDevice8(haptic, haptic->hwdata->device);
    1.61 +    ret2 = SDL_SYS_HapticOpenFromDevice8(haptic, device8, SDL_FALSE);
    1.62      if (ret2 < 0) {
    1.63 -        goto query_err;
    1.64 +        IDirectInputDevice8_Release(device8);
    1.65 +        return -1;
    1.66      }
    1.67  
    1.68      return 0;
    1.69 -
    1.70 -  query_err:
    1.71 -    IDirectInputDevice8_Release(haptic->hwdata->device);
    1.72 -  creat_err:
    1.73 -    if (haptic->hwdata != NULL) {
    1.74 -        SDL_free(haptic->hwdata);
    1.75 -        haptic->hwdata = NULL;
    1.76 -    }
    1.77 -    return -1;
    1.78  }
    1.79  
    1.80  static int
    1.81 @@ -437,13 +421,21 @@
    1.82   */
    1.83  static int
    1.84  SDL_SYS_HapticOpenFromDevice8(SDL_Haptic * haptic,
    1.85 -                              LPDIRECTINPUTDEVICE8 device8)
    1.86 +                              LPDIRECTINPUTDEVICE8 device8, SDL_bool is_joystick)
    1.87  {
    1.88      HRESULT ret;
    1.89      DIPROPDWORD dipdw;
    1.90  
    1.91 +    /* Allocate the hwdata */
    1.92 +    haptic->hwdata = (struct haptic_hwdata *)SDL_malloc(sizeof(*haptic->hwdata));
    1.93 +    if (haptic->hwdata == NULL) {
    1.94 +        return SDL_OutOfMemory();
    1.95 +    }
    1.96 +    SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
    1.97 +
    1.98      /* We'll use the device8 from now on. */
    1.99      haptic->hwdata->device = device8;
   1.100 +    haptic->hwdata->is_joystick = is_joystick;
   1.101  
   1.102      /* Grab it exclusively to use force feedback stuff. */
   1.103      ret = IDirectInputDevice8_SetCooperativeLevel(haptic->hwdata->device,
   1.104 @@ -657,49 +649,24 @@
   1.105              if ((SDL_hapticlist[i].bXInputHaptic) && (SDL_hapticlist[i].userid == userid)) {
   1.106                  SDL_assert(joystick->hwdata->bXInputHaptic);
   1.107                  haptic->index = i;
   1.108 -                break;
   1.109 +                return SDL_SYS_HapticOpenFromXInput(haptic, SDL_hapticlist[haptic->index].userid);
   1.110              }
   1.111          }
   1.112      } else {
   1.113          for (i=0; i<SDL_numhaptics; i++) {
   1.114 -            idret = IDirectInputDevice8_GetDeviceInfo(joystick->hwdata->InputDevice,
   1.115 -                  &joy_instance);
   1.116 +            idret = IDirectInputDevice8_GetDeviceInfo(joystick->hwdata->InputDevice, &joy_instance);
   1.117              if (FAILED(idret)) {
   1.118                  return -1;
   1.119              }
   1.120              if (DI_GUIDIsSame(&SDL_hapticlist[i].instance.guidInstance,
   1.121                                &joy_instance.guidInstance)) {
   1.122                  haptic->index = i;
   1.123 -                break;
   1.124 +                return SDL_SYS_HapticOpenFromDevice8(haptic, joystick->hwdata->InputDevice, SDL_TRUE);
   1.125              }
   1.126          }
   1.127      }
   1.128 -    if (i >= SDL_numhaptics) {
   1.129 -        return -1;
   1.130 -    }
   1.131 -
   1.132 -    /* Allocate the hwdata */
   1.133 -    haptic->hwdata = (struct haptic_hwdata *)
   1.134 -        SDL_malloc(sizeof(*haptic->hwdata));
   1.135 -    if (haptic->hwdata == NULL) {
   1.136 -        return SDL_OutOfMemory();
   1.137 -    }
   1.138 -    SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
   1.139 -
   1.140 -    /* Now open the device. */
   1.141 -    if (!joystick->hwdata->bXInputHaptic) {
   1.142 -        ret = SDL_SYS_HapticOpenFromDevice8(haptic, joystick->hwdata->InputDevice);
   1.143 -        if (ret < 0) {
   1.144 -            return -1;
   1.145 -        }
   1.146 -    }
   1.147 -
   1.148 -    /* It's using the joystick device. */
   1.149 -    haptic->hwdata->is_joystick = 1;
   1.150 -    haptic->hwdata->bXInputHaptic = joystick->hwdata->bXInputHaptic;
   1.151 -    haptic->hwdata->userid = joystick->hwdata->userid;
   1.152 -
   1.153 -    return 0;
   1.154 +    /* No match to our haptic list */
   1.155 +    return -1;
   1.156  }
   1.157  
   1.158