void Joystick_Cleanup(void) { #if 0 if (joy[0]) IDirectInputDevice2_Release(joy[0]); if (joy[1]) IDirectInputDevice2_Release(joy[1]); if (dinput) IDirectInput_Release(dinput); #endif }
static void DX5_DInputQuit(_THIS) { int i; if ( dinput != NULL ) { for ( i=0; i<MAX_INPUTS; ++i ) { if ( SDL_DIdev[i] != NULL ) { IDirectInputDevice2_Unacquire(SDL_DIdev[i]); IDirectInputDevice2_SetEventNotification( SDL_DIdev[i], NULL); if ( SDL_DIevt[i] != NULL ) { CloseHandle(SDL_DIevt[i]); SDL_DIevt[i] = NULL; } IDirectInputDevice2_Release(SDL_DIdev[i]); SDL_DIdev[i] = NULL; } } SDL_DIndev = 0; IDirectInput_Release(dinput); dinput = NULL; } }
/* joystick_dinput_exit: [primary thread] * Shuts down the DirectInput joystick devices. */ static void joystick_dinput_exit(void) { int i, j; /* unacquire the devices */ wnd_call_proc(joystick_dinput_unacquire); /* destroy the devices */ for (i=0; i<dinput_joy_num; i++) { IDirectInputDevice2_Release(dinput_joystick[i].device); for (j=0; j<dinput_joystick[i].num_axes; j++) { if (dinput_joystick[i].axis_name[j]) _AL_FREE(dinput_joystick[i].axis_name[j]); } if (dinput_joystick[i].caps & JOYCAPS_HASPOV) _AL_FREE(dinput_joystick[i].hat_name); for (j=0; j<dinput_joystick[i].num_buttons; j++) _AL_FREE(dinput_joystick[i].button_name[j]); } /* destroy the DirectInput interface */ IDirectInput_Release(joystick_dinput); win_remove_all_joysticks(); dinput_joy_num = 0; }
/* Function to close a joystick after use */ void SDL_SYS_JoystickClose(SDL_Joystick * joystick) { IDirectInputDevice2_Unacquire(joystick->hwdata->InputDevice); IDirectInputDevice2_Release(joystick->hwdata->InputDevice); if (joystick->hwdata != NULL) { /* free system specific hardware data */ SDL_free(joystick->hwdata); } }
/* * 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; }
/* * Closes the haptic device. */ void SDL_SYS_HapticClose(SDL_Haptic * haptic) { if (haptic->hwdata) { /* Free effects. */ SDL_free(haptic->effects); haptic->effects = NULL; haptic->neffects = 0; /* Clean up */ IDirectInputDevice2_Unacquire(haptic->hwdata->device); /* Only release if isn't grabbed by a joystick. */ if (haptic->hwdata->is_joystick == 0) { IDirectInputDevice2_Release(haptic->hwdata->device); } /* Free */ SDL_free(haptic->hwdata); haptic->hwdata = NULL; } }
bool joysticks_cleanup( void ) { int i; for (i = 0; i < Num_Joysticks; i++) { if (lpdiJoystick[i]) { IDirectInputDevice2_Unacquire(lpdiJoystick[i]); IDirectInputDevice2_Release(lpdiJoystick[i]); lpdiJoystick[i] = NULL; } } if (lpdi) { IDirectInputDevice_Release(lpdi); lpdi = NULL; } ReleaseJoysticks(); return true; }
/* joystick_enum_callback: * Helper function to find out how many joysticks we have and set them up. */ static BOOL CALLBACK joystick_enum_callback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef) { LPDIRECTINPUTDEVICE _dinput_device1; LPDIRECTINPUTDEVICE2 dinput_device = NULL; HRESULT hr; LPVOID temp; HWND allegro_wnd = win_get_window(); DIPROPRANGE property_range = { /* the header */ { sizeof(DIPROPRANGE), // diph.dwSize sizeof(DIPROPHEADER), // diph.dwHeaderSize 0, // diph.dwObj DIPH_DEVICE, // diph.dwHow }, /* the data */ 0, // lMin 256 // lMax }; DIPROPDWORD property_deadzone = { /* the header */ { sizeof(DIPROPDWORD), // diph.dwSize sizeof(DIPROPHEADER), // diph.dwHeaderSize 0, // diph.dwObj DIPH_DEVICE, // diph.dwHow }, /* the data */ 2000, // dwData }; if (dinput_joy_num == MAX_JOYSTICKS-1) { _TRACE(PREFIX_W "The system supports more than %d joysticks\n", MAX_JOYSTICKS); return DIENUM_STOP; } /* create the DirectInput joystick device */ hr = IDirectInput_CreateDevice(joystick_dinput, &lpddi->guidInstance, &_dinput_device1, NULL); if (FAILED(hr)) goto Error; /* query the DirectInputDevice2 interface needed for the poll() method */ hr = IDirectInputDevice_QueryInterface(_dinput_device1, &IID_IDirectInputDevice2, &temp); IDirectInputDevice_Release(_dinput_device1); if (FAILED(hr)) goto Error; dinput_device = temp; /* set cooperative level */ hr = IDirectInputDevice2_SetCooperativeLevel(dinput_device, allegro_wnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); if (FAILED(hr)) goto Error; /* enumerate objects available on the device */ memset(&dinput_joystick[dinput_joy_num], 0, sizeof(struct DINPUT_JOYSTICK_INFO)); hr = IDirectInputDevice2_EnumObjects(dinput_device, object_enum_callback, &dinput_joystick[dinput_joy_num], DIDFT_PSHBUTTON | DIDFT_AXIS | DIDFT_POV); if (FAILED(hr)) goto Error; /* set data format */ hr = IDirectInputDevice2_SetDataFormat(dinput_device, &c_dfDIJoystick); if (FAILED(hr)) goto Error; /* set the range of axes */ hr = IDirectInputDevice2_SetProperty(dinput_device, DIPROP_RANGE, &property_range.diph); if (FAILED(hr)) goto Error; /* set the dead zone of axes */ hr = IDirectInputDevice2_SetProperty(dinput_device, DIPROP_DEADZONE, &property_deadzone.diph); if (FAILED(hr)) goto Error; /* register this joystick */ dinput_joystick[dinput_joy_num].device = dinput_device; if (win_add_joystick((WINDOWS_JOYSTICK_INFO *)&dinput_joystick[dinput_joy_num]) != 0) return DIENUM_STOP; dinput_joy_num++; return DIENUM_CONTINUE; Error: if (dinput_device) IDirectInputDevice2_Release(dinput_device); return DIENUM_CONTINUE; }
bool joysticks_init(void) { HRESULT err; DIPROPDWORD dipdw = { { sizeof(DIPROPDWORD), // diph.dwSize sizeof(DIPROPHEADER), // diph.dwHeaderSize 0, // diph.dwObj DIPH_DEVICE, // diph.dwHow }, DINPUT_BUFFERSIZE, // dwData }; LPDIRECTINPUTDEVICE tempJoystick = NULL; LPVOID joysticknumptr; int i, j, k; bool failjoystick; err = DirectInputCreate(GetModuleHandle(NULL), DIRECTINPUT_VERSION, &lpdi, NULL); if (FAILED(err))//DirectInput8Create(hInstApp, DIRECTINPUT_VERSION, &IID_IDirectInput8, (void**)&lpdi, NULL))) { return false; } // try to create Joystick devices for ( i = 0; i < MAX_JOYSTICKS; i++ ) lpdiJoystick[i] = NULL; Num_Joysticks = 0; lpdi->lpVtbl->EnumDevices(lpdi, DIDEVTYPE_JOYSTICK, //DI8DEVCLASS_GAMECTRL, InitJoystickInput, lpdi, DIEDFL_ATTACHEDONLY); failjoystick = false; for (i = 0; i < Num_Joysticks; i++) { JoystickInfo[i].assigned = false; JoystickInfo[i].connected = true; JoystickInfo[i].NumAxis = 0; JoystickInfo[i].NumButtons = 0; JoystickInfo[i].NumPOVs = 0; joysticknumptr = (LPVOID)&i; for (j = AXIS_Start; j <= AXIS_End; j++) { JoystickInfo[i].Axis[j].exists = false; } lpdiJoystick[i]->lpVtbl->EnumObjects(lpdiJoystick[i], DIEnumDeviceObjectsProc, joysticknumptr, DIDFT_ALL); for (j = AXIS_Start; j <= AXIS_End; j++) { JoystickInfo[i].Axis[j].action = SHIPACTION_Nothing; JoystickInfo[i].Axis[j].inverted = false; JoystickInfo[i].Axis[j].deadzone = 20; JoystickInfo[i].Axis[j].fine = true; } for (j = 0; j < JoystickInfo[i].NumButtons; j++) JoystickInfo[i].Button[j].action = SHIPACTION_Nothing; for (j = 0; j < JoystickInfo[i].NumPOVs; j++) { for (k = 0; k < MAX_POV_DIRECTIONS; k++) JoystickInfo[i].POV[j].action[k] = SHIPACTION_Nothing; } // Tell DirectInput that we want to receive data in joystick format if (IDirectInputDevice2_SetDataFormat(lpdiJoystick[i], &c_dfDIJoystick2) == DI_OK) { // set cooperative level if(IDirectInputDevice2_SetCooperativeLevel(lpdiJoystick[i], GetActiveWindow(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND) == DI_OK) { // try to acquire the Joystick err = IDirectInputDevice2_Acquire(lpdiJoystick[i]); if (err != DI_OK) { failjoystick = true; } }else { failjoystick = true; } } else { failjoystick = true; } if (failjoystick) { failjoystick = false; IDirectInputDevice2_Release(lpdiJoystick[i]); lpdiJoystick[i] = NULL; } } DebugPrintf( "joysticks_init: %d joysticks connected\n", Num_Joysticks ); // if we get here, all DirectInput objects were created ok return true; }