/*
 * Opens the haptic device from the file descriptor.
 *
 *    Steps:
 *       - Open temporary DirectInputDevice interface.
 *       - Create DirectInputDevice2 interface.
 *       - Release DirectInputDevice interface.
 *       - Call SDL_SYS_HapticOpenFromDevice2
 */
static int
SDL_SYS_HapticOpenFromInstance(SDL_Haptic * haptic, DIDEVICEINSTANCE instance)
{
    HRESULT ret;
    int ret2;
    LPDIRECTINPUTDEVICE device;

    /* Allocate the hwdata */
    haptic->hwdata = (struct haptic_hwdata *)
        SDL_malloc(sizeof(*haptic->hwdata));
    if (haptic->hwdata == NULL) {
        SDL_OutOfMemory();
        goto creat_err;
    }
    SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));

    /* Open the device */
    ret = IDirectInput_CreateDevice(dinput, &instance.guidInstance,
                                    &device, NULL);
    if (FAILED(ret)) {
        DI_SetError("Creating DirectInput device", ret);
        goto creat_err;
    }

    /* Now get the IDirectInputDevice2 interface, instead. */
    ret = IDirectInputDevice_QueryInterface(device,
                                            &IID_IDirectInputDevice2,
                                            (LPVOID *) & haptic->hwdata->
                                            device);
    /* Done with the temporary one now. */
    IDirectInputDevice_Release(device);
    if (FAILED(ret)) {
        DI_SetError("Querying DirectInput interface", ret);
        goto creat_err;
    }

    ret2 = SDL_SYS_HapticOpenFromDevice2(haptic, haptic->hwdata->device);
    if (ret2 < 0) {
        goto query_err;
    }

    return 0;

  query_err:
    IDirectInputDevice2_Release(haptic->hwdata->device);
  creat_err:
    if (haptic->hwdata != NULL) {
        SDL_free(haptic->hwdata);
        haptic->hwdata = NULL;
    }
    return -1;
}
Example #2
0
/*
 * Opens a SDL_Haptic from a SDL_Joystick.
 */
int
SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
{
    int i, ret;
    HRESULT idret;
    DIDEVICEINSTANCE joy_instance;
    joy_instance.dwSize = sizeof(DIDEVICEINSTANCE);

    /* Since it comes from a joystick we have to try to match it with a haptic device on our haptic list. */
    for (i=0; i<SDL_numhaptics; i++) {
        idret = IDirectInputDevice2_GetDeviceInfo(joystick->hwdata->InputDevice,
              &joy_instance);
        if (FAILED(idret)) {
            return -1;
        }
        if (DI_GUIDIsSame(&SDL_hapticlist[i].instance.guidInstance,
                          &joy_instance.guidInstance)) {
            haptic->index = i;
            break;
        }
    }
    if (i >= SDL_numhaptics) {
        return -1;
    }

    /* Allocate the hwdata */
    haptic->hwdata = (struct haptic_hwdata *)
        SDL_malloc(sizeof(*haptic->hwdata));
    if (haptic->hwdata == NULL) {
        SDL_OutOfMemory();
        return -1;
    }
    SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));

    /* Now open the device. */
    ret =
        SDL_SYS_HapticOpenFromDevice2(haptic, joystick->hwdata->InputDevice);
    if (ret < 0) {
        return -1;
    }

    /* It's using the joystick device. */
    haptic->hwdata->is_joystick = 1;

    return 0;
}