static BOOL CALLBACK enum_joypad_cb(const DIDEVICEINSTANCE *inst, void *p) { (void)p; if (g_joypad_cnt == MAX_PLAYERS) return DIENUM_STOP; LPDIRECTINPUTDEVICE8 *pad = &g_pads[g_joypad_cnt].joypad; #ifdef __cplusplus if (FAILED(IDirectInput8_CreateDevice(g_ctx, inst->guidInstance, pad, NULL))) #else if (FAILED(IDirectInput8_CreateDevice(g_ctx, &inst->guidInstance, pad, NULL))) #endif return DIENUM_CONTINUE; IDirectInputDevice8_SetDataFormat(*pad, &c_dfDIJoystick2); IDirectInputDevice8_SetCooperativeLevel(*pad, (HWND)driver.video_window, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND); IDirectInputDevice8_EnumObjects(*pad, enum_axes_cb, *pad, DIDFT_ABSAXIS); g_joypad_cnt++; return DIENUM_CONTINUE; }
static void *dinput_init(void) { struct dinput_input *di = NULL; if (!dinput_init_context()) { RARCH_ERR("Failed to start DirectInput driver.\n"); return NULL; } di = (struct dinput_input*)calloc(1, sizeof(*di)); if (!di) return NULL; #ifdef __cplusplus if (FAILED(IDirectInput8_CreateDevice(g_ctx, GUID_SysKeyboard, &di->keyboard, NULL))) { RARCH_ERR("Failed to create keyboard device.\n"); di->keyboard = NULL; } if (FAILED(IDirectInput8_CreateDevice(g_ctx, GUID_SysMouse, &di->mouse, NULL))) { RARCH_ERR("Failed to create mouse device.\n"); di->mouse = NULL; } #else if (FAILED(IDirectInput8_CreateDevice(g_ctx, &GUID_SysKeyboard, &di->keyboard, NULL))) { RARCH_ERR("Failed to create keyboard device.\n"); di->keyboard = NULL; } if (FAILED(IDirectInput8_CreateDevice(g_ctx, &GUID_SysMouse, &di->mouse, NULL))) { RARCH_ERR("Failed to create mouse device.\n"); di->mouse = NULL; } #endif if (di->keyboard) { IDirectInputDevice8_SetDataFormat(di->keyboard, &c_dfDIKeyboard); IDirectInputDevice8_SetCooperativeLevel(di->keyboard, (HWND)driver.video_window, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND); IDirectInputDevice8_Acquire(di->keyboard); } if (di->mouse) { IDirectInputDevice8_SetDataFormat(di->mouse, &c_dfDIMouse2); IDirectInputDevice8_SetCooperativeLevel(di->mouse, (HWND)driver.video_window, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND); IDirectInputDevice8_Acquire(di->mouse); } input_keymaps_init_keyboard_lut(rarch_key_map_dinput); di->joypad = input_joypad_init_driver(g_settings.input.joypad_driver); return di; }
static BOOL CALLBACK enum_joypad_cb(const DIDEVICEINSTANCE *inst, void *p) { (void)p; if (g_joypad_cnt == MAX_PLAYERS) return DIENUM_STOP; LPDIRECTINPUTDEVICE8 *pad = &g_pads[g_joypad_cnt].joypad; #ifdef __cplusplus if (FAILED(IDirectInput8_CreateDevice(g_ctx, inst->guidInstance, pad, NULL))) #else if (FAILED(IDirectInput8_CreateDevice(g_ctx, &inst->guidInstance, pad, NULL))) #endif return DIENUM_CONTINUE; g_pads[g_joypad_cnt].joy_name = strdup(inst->tszProductName); #ifdef HAVE_WINXINPUT int last_xbox_pad_index = 0; bool is_360_pad = name_is_360_pad(inst->tszProductName); if (is_360_pad) { if (last_xbox_pad_index < 4) g_xbox_pad_indexes[g_joypad_cnt] = last_xbox_pad_index; ++last_xbox_pad_index; goto enum_iteration_done; } #endif IDirectInputDevice8_SetDataFormat(*pad, &c_dfDIJoystick2); IDirectInputDevice8_SetCooperativeLevel(*pad, (HWND)driver.video_window, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND); IDirectInputDevice8_EnumObjects(*pad, enum_axes_cb, *pad, DIDFT_ABSAXIS); #ifdef HAVE_WINXINPUT if (!is_360_pad) #endif { strlcpy(g_settings.input.device_names[g_joypad_cnt], dinput_joypad_name(g_joypad_cnt), sizeof(g_settings.input.device_names[g_joypad_cnt])); input_config_autoconfigure_joypad(g_joypad_cnt, dinput_joypad_name(g_joypad_cnt), dinput_joypad.ident); } enum_iteration_done: g_joypad_cnt++; return DIENUM_CONTINUE; }
/* * Opens the haptic device from the file descriptor. * * Steps: * - Open temporary DirectInputDevice interface. * - Create DirectInputDevice8 interface. * - Release DirectInputDevice interface. * - Call SDL_SYS_HapticOpenFromDevice8 */ static int SDL_SYS_HapticOpenFromInstance(SDL_Haptic * haptic, DIDEVICEINSTANCE instance) { HRESULT ret; int ret2; LPDIRECTINPUTDEVICE8 device; LPDIRECTINPUTDEVICE8 device8; /* Open the device */ ret = IDirectInput8_CreateDevice(dinput, &instance.guidInstance, &device, NULL); if (FAILED(ret)) { DI_SetError("Creating DirectInput device", ret); return -1; } /* Now get the IDirectInputDevice8 interface, instead. */ ret = IDirectInputDevice8_QueryInterface(device, &IID_IDirectInputDevice8, (LPVOID *) &device8); /* Done with the temporary one now. */ IDirectInputDevice8_Release(device); if (FAILED(ret)) { DI_SetError("Querying DirectInput interface", ret); return -1; } ret2 = SDL_SYS_HapticOpenFromDevice8(haptic, device8, SDL_FALSE); if (ret2 < 0) { IDirectInputDevice8_Release(device8); return -1; } return 0; }
BOOL CALLBACK EnumPeripheralsCallback (LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef) { if (GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_GAMEPAD || GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_JOYSTICK) { #ifdef HAVE_XINPUT if (IsXInputDevice(&lpddi->guidProduct)) { dev_list[num_devices].lpDIDevice = NULL; dev_list[num_devices].is_xinput_device = TRUE; dev_list[num_devices].user_index=((int *)pvRef)[0]; ((int *)pvRef)[0]++; num_devices++; } else #endif { dev_list[num_devices].is_xinput_device = FALSE; if (SUCCEEDED(IDirectInput8_CreateDevice(lpDI8, &lpddi->guidInstance, &dev_list[num_devices].lpDIDevice, NULL) )) num_devices++; } } return DIENUM_CONTINUE; }
int SDL_DINPUT_HapticOpen(SDL_Haptic * haptic, SDL_hapticlist_item *item) { HRESULT ret; LPDIRECTINPUTDEVICE8 device; LPDIRECTINPUTDEVICE8 device8; /* Open the device */ ret = IDirectInput8_CreateDevice(dinput, &item->instance.guidInstance, &device, NULL); if (FAILED(ret)) { DI_SetError("Creating DirectInput device", ret); return -1; } /* Now get the IDirectInputDevice8 interface, instead. */ ret = IDirectInputDevice8_QueryInterface(device, &IID_IDirectInputDevice8, (LPVOID *)&device8); /* Done with the temporary one now. */ IDirectInputDevice8_Release(device); if (FAILED(ret)) { DI_SetError("Querying DirectInput interface", ret); return -1; } if (SDL_DINPUT_HapticOpenFromDevice(haptic, device8, SDL_FALSE) < 0) { IDirectInputDevice8_Release(device8); return -1; } return 0; }
int SDL_DINPUT_MaybeAddDevice(const DIDEVICEINSTANCE * pdidInstance) { HRESULT ret; LPDIRECTINPUTDEVICE8 device; const DWORD needflags = DIDC_ATTACHED | DIDC_FORCEFEEDBACK; DIDEVCAPS capabilities; SDL_hapticlist_item *item = NULL; if (dinput == NULL) { return -1; /* not initialized. We'll pick these up on enumeration if we init later. */ } /* Make sure we don't already have it */ for (item = SDL_hapticlist; item; item = item->next) { if ((!item->bXInputHaptic) && (SDL_memcmp(&item->instance, pdidInstance, sizeof(*pdidInstance)) == 0)) { return -1; /* Already added */ } } /* Open the device */ ret = IDirectInput8_CreateDevice(dinput, &pdidInstance->guidInstance, &device, NULL); if (FAILED(ret)) { /* DI_SetError("Creating DirectInput device",ret); */ return -1; } /* Get capabilities. */ SDL_zero(capabilities); capabilities.dwSize = sizeof(DIDEVCAPS); ret = IDirectInputDevice8_GetCapabilities(device, &capabilities); IDirectInputDevice8_Release(device); if (FAILED(ret)) { /* DI_SetError("Getting device capabilities",ret); */ return -1; } if ((capabilities.dwFlags & needflags) != needflags) { return -1; /* not a device we can use. */ } item = (SDL_hapticlist_item *)SDL_calloc(1, sizeof(SDL_hapticlist_item)); if (item == NULL) { return SDL_OutOfMemory(); } item->name = WIN_StringToUTF8(pdidInstance->tszProductName); if (!item->name) { SDL_free(item); return -1; } /* Copy the instance over, useful for creating devices. */ SDL_memcpy(&item->instance, pdidInstance, sizeof(DIDEVICEINSTANCE)); SDL_memcpy(&item->capabilities, &capabilities, sizeof(capabilities)); return SDL_SYS_AddHapticDevice(item); }
HRESULT WINAPI HookDirectInput8Create( PVOID ModuleBase, ULONG Version, REFIID IID, PVOID* Out, LPUNKNOWN UnknownOuter ) { HRESULT Result; PVOID HookGetDeviceData, HookGetDeviceState; Result = StubDirectInput8Create((HINSTANCE)ModuleBase, Version, IID, Out, UnknownOuter); if (FAILED(Result)) return Result; if (IID == IID_IDirectInput8A) { HookGetDeviceData = HookGetDeviceDataA; HookGetDeviceState = HookGetDeviceStateA; } else if (IID == IID_IDirectInput8W) { HookGetDeviceData = HookGetDeviceDataW; HookGetDeviceState = HookGetDeviceStateW; } else { return Result; } SCOPE_EXIT { Nt_RestoreMemory(&StubDirectInput8Create); } SCOPE_EXIT_END; LPDIRECTINPUT8W DInput8W; LPDIRECTINPUTDEVICE8W KeyboardW; DInput8W = (LPDIRECTINPUT8W)*Out; if (FAILED(IDirectInput8_CreateDevice(DInput8W, GUID_SysKeyboard, &KeyboardW, NULL))) return Result; MEMORY_FUNCTION_PATCH f[] = { INLINE_HOOK_JUMP(KeyboardW->lpVtbl->GetDeviceState, HookGetDeviceState, StubGetDeviceState), INLINE_HOOK_JUMP(KeyboardW->lpVtbl->GetDeviceData, HookGetDeviceData, StubGetDeviceData), }; Nt_PatchMemory(NULL, 0, f, countof(f), NULL); IDirectInputDevice8_Release(KeyboardW); return Result; }
static void *dinput_init(void) { if (!dinput_init_context()) return NULL; struct dinput_input *di = (struct dinput_input*)calloc(1, sizeof(*di)); if (!di) return NULL; #ifdef __cplusplus if (FAILED(IDirectInput8_CreateDevice(g_ctx, GUID_SysKeyboard, &di->keyboard, NULL))) goto error; if (FAILED(IDirectInput8_CreateDevice(g_ctx, GUID_SysMouse, &di->mouse, NULL))) goto error; #else if (FAILED(IDirectInput8_CreateDevice(g_ctx, &GUID_SysKeyboard, &di->keyboard, NULL))) goto error; if (FAILED(IDirectInput8_CreateDevice(g_ctx, &GUID_SysMouse, &di->mouse, NULL))) goto error; #endif IDirectInputDevice8_SetDataFormat(di->keyboard, &c_dfDIKeyboard); IDirectInputDevice8_SetCooperativeLevel(di->keyboard, (HWND)driver.video_window, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND); IDirectInputDevice8_Acquire(di->keyboard); IDirectInputDevice8_SetDataFormat(di->mouse, &c_dfDIMouse2); IDirectInputDevice8_SetCooperativeLevel(di->mouse, (HWND)driver.video_window, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND); IDirectInputDevice8_Acquire(di->mouse); input_init_keyboard_lut(rarch_key_map_dinput); di->joypad = input_joypad_init_driver(g_settings.input.joypad_driver); return di; error: dinput_destroy_context(); free(di); return NULL; }
BOOL CALLBACK EnumPeripheralsCallback (LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef) { if (GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_GAMEPAD || GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_JOYSTICK || GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_KEYBOARD) { if (SUCCEEDED(IDirectInput8_CreateDevice(lpDI8, &lpddi->guidInstance, &lpDIDevice[numdevices], NULL) )) numdevices++; } return DIENUM_CONTINUE; }
/*********************************************************************** * enum_callback [internal] * Enumerates, creates and sets the common data format for all the joystick devices. * First time it checks if space for the joysticks was already reserved * and if not, just counts how many there are. */ static BOOL CALLBACK enum_callback(const DIDEVICEINSTANCEW *instance, void *context) { struct JoystickData *data = context; struct Joystick *joystick; DIPROPRANGE proprange; DIDEVCAPS caps; if (data->joysticks == NULL) { data->num_joysticks += 1; return DIENUM_CONTINUE; } joystick = &data->joysticks[data->cur_joystick]; data->cur_joystick += 1; IDirectInput8_CreateDevice(data->di, &instance->guidInstance, &joystick->device, NULL); IDirectInputDevice8_SetDataFormat(joystick->device, &c_dfDIJoystick); joystick->instance = *instance; caps.dwSize = sizeof(caps); IDirectInputDevice8_GetCapabilities(joystick->device, &caps); joystick->num_buttons = caps.dwButtons; joystick->num_axes = caps.dwAxes; joystick->forcefeedback = caps.dwFlags & DIDC_FORCEFEEDBACK; joystick->num_effects = 0; if (joystick->forcefeedback) data->num_ff++; /* Set axis range to ease the GUI visualization */ proprange.diph.dwSize = sizeof(DIPROPRANGE); proprange.diph.dwHeaderSize = sizeof(DIPROPHEADER); proprange.diph.dwHow = DIPH_DEVICE; proprange.diph.dwObj = 0; proprange.lMin = TEST_AXIS_MIN; proprange.lMax = TEST_AXIS_MAX; IDirectInputDevice_SetProperty(joystick->device, DIPROP_RANGE, &proprange.diph); return DIENUM_CONTINUE; }
int open_joystick(char *joystick_device, GdkWindow *window) { HINSTANCE hi = 0; HRESULT hr = 0; HWND hwin = 0; struct DEVINFO devinfo = {0}; /* create interface */ hi = GetModuleHandle(NULL); hr = DirectInput8Create(hi, DIRECTINPUT_VERSION, &IID_IDirectInput8, (VOID**)&dinput, NULL); if (FAILED(hr)) return -1; /* look for a joystick */ hr = IDirectInput8_EnumDevices(dinput, DI8DEVCLASS_GAMECTRL, EnumDevCallback, &devinfo, DIEDFL_ATTACHEDONLY); if(FAILED(hr)) return -1; /* obtain joystick interface */ hr = IDirectInput8_CreateDevice(dinput, &devinfo.deviceID, &joystick, NULL); if(FAILED(hr)) return -1; /* set data format to "simple joystick" */ hr = IDirectInputDevice2_SetDataFormat(joystick, &c_dfDIJoystick2); if(FAILED(hr)) return -1; /* set the cooperative level */ #ifdef __WIN32__ hwin = GDK_WINDOW_HWND(window); #endif hr = IDirectInputDevice2_SetCooperativeLevel(joystick, hwin, DISCL_EXCLUSIVE | DISCL_FOREGROUND); if(FAILED(hr)) return -1; /* enumerate axes, buttons, povs */ hr = IDirectInputDevice2_EnumObjects(joystick, EnumObjectsCallback, NULL, DIDFT_ALL); if(FAILED(hr)) return -1; return 0; }
/* * Callback to find the haptic devices. */ static BOOL CALLBACK EnumHapticsCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext) { HRESULT ret; LPDIRECTINPUTDEVICE8 device; /* Copy the instance over, useful for creating devices. */ SDL_memcpy(&SDL_hapticlist[SDL_numhaptics].instance, pdidInstance, sizeof(DIDEVICEINSTANCE)); /* Open the device */ ret = IDirectInput8_CreateDevice(dinput, &pdidInstance->guidInstance, &device, NULL); if (FAILED(ret)) { /* DI_SetError("Creating DirectInput device",ret); */ return DIENUM_CONTINUE; } /* Get capabilities. */ SDL_hapticlist[SDL_numhaptics].capabilities.dwSize = sizeof(DIDEVCAPS); ret = IDirectInputDevice8_GetCapabilities(device, &SDL_hapticlist[SDL_numhaptics]. capabilities); if (FAILED(ret)) { /* DI_SetError("Getting device capabilities",ret); */ IDirectInputDevice8_Release(device); return DIENUM_CONTINUE; } /* Copy the name */ SDL_hapticlist[SDL_numhaptics].name = WIN_StringToUTF8(SDL_hapticlist[SDL_numhaptics].instance.tszProductName); /* Close up device and count it. */ IDirectInputDevice8_Release(device); SDL_numhaptics++; /* Watch out for hard limit. */ if (SDL_numhaptics >= MAX_HAPTICS) return DIENUM_STOP; return DIENUM_CONTINUE; }
static BOOL CALLBACK EnumCallback(const DIDEVICEINSTANCEA *instance, void *context) { struct JoystickData *data = context; struct Joystick *joystick; if (data->joysticks == NULL) { data->num_joysticks += 1; return DIENUM_CONTINUE; } joystick = &data->joysticks[data->cur_joystick]; data->cur_joystick += 1; IDirectInput8_CreateDevice(data->di, &instance->guidInstance, &joystick->device, NULL); IDirectInputDevice8_SetDataFormat(joystick->device, &c_dfDIJoystick); joystick->instance = *instance; joystick->num_effects = 0; return DIENUM_CONTINUE; }
static void test_CreateDevice(void) { IDirectInput8A *pDI; HRESULT hr; IDirectInputDevice8A *pDID; hr = DirectInput8Create(hInstance, DIRECTINPUT_VERSION, &IID_IDirectInput8A, (void **)&pDI, NULL); if (FAILED(hr)) { win_skip("Failed to instantiate a IDirectInputA instance: 0x%08x\n", hr); return; } hr = IDirectInput8_CreateDevice(pDI, NULL, NULL, NULL); ok(hr == E_POINTER, "IDirectInput8_CreateDevice returned 0x%08x\n", hr); pDID = (void *)0xdeadbeef; hr = IDirectInput8_CreateDevice(pDI, NULL, &pDID, NULL); ok(hr == E_POINTER, "IDirectInput8_CreateDevice returned 0x%08x\n", hr); ok(pDID == NULL, "Output interface pointer is %p\n", pDID); hr = IDirectInput8_CreateDevice(pDI, &GUID_Unknown, NULL, NULL); ok(hr == E_POINTER, "IDirectInput8_CreateDevice returned 0x%08x\n", hr); pDID = (void *)0xdeadbeef; hr = IDirectInput8_CreateDevice(pDI, &GUID_Unknown, &pDID, NULL); ok(hr == DIERR_DEVICENOTREG, "IDirectInput8_CreateDevice returned 0x%08x\n", hr); ok(pDID == NULL, "Output interface pointer is %p\n", pDID); hr = IDirectInput8_CreateDevice(pDI, &GUID_SysMouse, NULL, NULL); ok(hr == E_POINTER, "IDirectInput8_CreateDevice returned 0x%08x\n", hr); hr = IDirectInput8_CreateDevice(pDI, &GUID_SysMouse, &pDID, NULL); ok(hr == DI_OK, "IDirectInput8_CreateDevice returned 0x%08x\n", hr); IDirectInputDevice_Release(pDID); IDirectInput8_Release(pDI); }
/* joystick_enum_callback: [primary thread] * Helper function to find out how many joysticks we have and set them up. * At the end joydx_num_joysticks and joydx_joystick[] will be initialised. */ static BOOL CALLBACK joystick_enum_callback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef) { DIPROPRANGE property_range = { /* the header */ { sizeof(DIPROPRANGE), // diph.dwSize sizeof(DIPROPHEADER), // diph.dwHeaderSize 0, // diph.dwObj DIPH_DEVICE, // diph.dwHow }, /* the data */ -32767, // lMin +32767 // 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 }; DIPROPDWORD property_buffersize = { /* the header */ { sizeof(DIPROPDWORD), // diph.dwSize sizeof(DIPROPHEADER), // diph.dwHeaderSize 0, // diph.dwObj DIPH_DEVICE, // diph.dwHow }, /* the data */ DEVICE_BUFFER_SIZE // number of data items }; LPDIRECTINPUTDEVICE _dinput_device1; LPDIRECTINPUTDEVICE2 dinput_device = NULL; HRESULT hr; LPVOID temp; CAPS_AND_NAMES caps_and_names; ALLEGRO_JOYSTICK_DIRECTX *joy; int num; (void)pvRef; /* check if the joystick already existed before */ joy = joydx_by_guid(lpddi->guidInstance); if (joy) { ALLEGRO_DEBUG("Device %s still exists\n", joydx_guid_string(joy)); joy->marked = true; return DIENUM_CONTINUE; } /* create the DirectInput joystick device */ hr = IDirectInput8_CreateDevice(joystick_dinput, &lpddi->guidInstance, &_dinput_device1, NULL); if (FAILED(hr)) goto Error; /* query the DirectInputDevice2 interface needed for the poll() method */ hr = IDirectInputDevice8_QueryInterface(_dinput_device1, &__al_IID_IDirectInputDevice8A, &temp); IDirectInputDevice8_Release(_dinput_device1); if (FAILED(hr)) goto Error; dinput_device = temp; /* enumerate objects available on the device */ memset(&caps_and_names, 0, sizeof(caps_and_names)); hr = IDirectInputDevice8_EnumObjects(dinput_device, object_enum_callback, &caps_and_names, DIDFT_PSHBUTTON | DIDFT_AXIS | DIDFT_POV); if (FAILED(hr)) goto Error; /* set data format */ hr = IDirectInputDevice8_SetDataFormat(dinput_device, &__al_c_dfDIJoystick); if (FAILED(hr)) goto Error; /* set the range of axes */ hr = IDirectInputDevice8_SetProperty(dinput_device, DIPROP_RANGE, &property_range.diph); if (FAILED(hr)) goto Error; /* set the dead zone of axes */ hr = IDirectInputDevice8_SetProperty(dinput_device, DIPROP_DEADZONE, &property_deadzone.diph); if (FAILED(hr)) goto Error; /* set the buffer size */ hr = IDirectInputDevice8_SetProperty(dinput_device, DIPROP_BUFFERSIZE, &property_buffersize.diph); if (FAILED(hr)) goto Error; /* set up the joystick structure */ joy = joydx_allocate_structure(&num); if (!joy) { ALLEGRO_ERROR("Joystick array full\n"); goto Error; } joy->config_state = STATE_BORN; joy->marked = true; joy->device = dinput_device; memcpy(&joy->guid, &lpddi->guidInstance, sizeof(GUID)); _al_sane_strncpy(joy->name, lpddi->tszInstanceName, sizeof(joy->name)); /* fill in the joystick structure */ fill_joystick_info_using_caps_and_names(joy, &caps_and_names); /* create a thread event for this joystick, unless it was already created */ joy->waker_event = CreateEvent(NULL, false, false, NULL); /* tell the joystick background thread to wake up when this joystick * device's state changes */ hr = IDirectInputDevice8_SetEventNotification(joy->device, joy->waker_event); if (FAILED(hr)) { ALLEGRO_ERROR("SetEventNotification failed for joystick %d: %s\n", num, dinput_err_str(hr)); goto Error; } if (hr == DI_POLLEDDEVICE) { /* This joystick device must be polled -- replace the Event with * a Waitable Timer object. * * Theoretically all polled devices could share a single * waitable timer object. But, really, how many such devices * are there going to be on a system? */ CloseHandle(joy->waker_event); joy->waker_event = CreateWaitableTimer(NULL, false, NULL); if (joy->waker_event == NULL) { ALLEGRO_ERROR("CreateWaitableTimer failed for polled device.\n"); goto Error; } { LARGE_INTEGER due_time; due_time.HighPart = 0; due_time.LowPart = 150; /* 15 ms (arbitrary) */ SetWaitableTimer(joy->waker_event, &due_time, true, /* periodic */ NULL, NULL, false); } } ALLEGRO_INFO("Joystick %d initialized, GUID: %s\n", num, joydx_guid_string(joy)); config_needs_merging = true; return DIENUM_CONTINUE; Error: if (dinput_device) IDirectInputDevice8_Release(dinput_device); if (joy) { joy->device = NULL; joydx_inactivate_joy(joy); } return DIENUM_CONTINUE; }
static void *dinput_init(void) { struct dinput_input *di = NULL; settings_t *settings = config_get_ptr(); if (!dinput_init_context()) { RARCH_ERR("Failed to start DirectInput driver.\n"); return NULL; } di = (struct dinput_input*)calloc(1, sizeof(*di)); if (!di) return NULL; #ifdef __cplusplus if (FAILED(IDirectInput8_CreateDevice(g_dinput_ctx, GUID_SysKeyboard, &di->keyboard, NULL))) { RARCH_ERR("Failed to create keyboard device.\n"); di->keyboard = NULL; } if (FAILED(IDirectInput8_CreateDevice(g_dinput_ctx, GUID_SysMouse, &di->mouse, NULL))) { RARCH_ERR("Failed to create mouse device.\n"); di->mouse = NULL; } #else if (FAILED(IDirectInput8_CreateDevice(g_dinput_ctx, &GUID_SysKeyboard, &di->keyboard, NULL))) { RARCH_ERR("Failed to create keyboard device.\n"); di->keyboard = NULL; } if (FAILED(IDirectInput8_CreateDevice(g_dinput_ctx, &GUID_SysMouse, &di->mouse, NULL))) { RARCH_ERR("Failed to create mouse device.\n"); di->mouse = NULL; } #endif if (di->keyboard) { IDirectInputDevice8_SetDataFormat(di->keyboard, &c_dfDIKeyboard); IDirectInputDevice8_SetCooperativeLevel(di->keyboard, (HWND)video_driver_window_get(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND); IDirectInputDevice8_Acquire(di->keyboard); } if (di->mouse) { DIDATAFORMAT c_dfDIMouse2_custom = c_dfDIMouse2; c_dfDIMouse2_custom.dwFlags = DIDF_ABSAXIS; IDirectInputDevice8_SetDataFormat(di->mouse, &c_dfDIMouse2_custom); IDirectInputDevice8_SetCooperativeLevel(di->mouse, (HWND)video_driver_window_get(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND); IDirectInputDevice8_Acquire(di->mouse); } input_keymaps_init_keyboard_lut(rarch_key_map_dinput); di->joypad = input_joypad_init_driver(settings->input.joypad_driver, di); return di; }
int SDL_DINPUT_JoystickOpen(SDL_Joystick * joystick, JoyStick_DeviceData *joystickdevice) { HRESULT result; LPDIRECTINPUTDEVICE8 device; DIPROPDWORD dipdw; joystick->hwdata->buffered = SDL_TRUE; joystick->hwdata->Capabilities.dwSize = sizeof(DIDEVCAPS); SDL_zero(dipdw); dipdw.diph.dwSize = sizeof(DIPROPDWORD); dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER); result = IDirectInput8_CreateDevice(dinput, &(joystickdevice->dxdevice.guidInstance), &device, NULL); if (FAILED(result)) { return SetDIerror("IDirectInput::CreateDevice", result); } /* Now get the IDirectInputDevice8 interface, instead. */ result = IDirectInputDevice8_QueryInterface(device, &IID_IDirectInputDevice8, (LPVOID *)& joystick-> hwdata->InputDevice); /* We are done with this object. Use the stored one from now on. */ IDirectInputDevice8_Release(device); if (FAILED(result)) { return SetDIerror("IDirectInputDevice8::QueryInterface", result); } /* Acquire shared access. Exclusive access is required for forces, * though. */ result = IDirectInputDevice8_SetCooperativeLevel(joystick->hwdata-> InputDevice, SDL_HelperWindow, DISCL_EXCLUSIVE | DISCL_BACKGROUND); if (FAILED(result)) { return SetDIerror("IDirectInputDevice8::SetCooperativeLevel", result); } /* Use the extended data structure: DIJOYSTATE2. */ result = IDirectInputDevice8_SetDataFormat(joystick->hwdata->InputDevice, &SDL_c_dfDIJoystick2); if (FAILED(result)) { return SetDIerror("IDirectInputDevice8::SetDataFormat", result); } /* Get device capabilities */ result = IDirectInputDevice8_GetCapabilities(joystick->hwdata->InputDevice, &joystick->hwdata->Capabilities); if (FAILED(result)) { return SetDIerror("IDirectInputDevice8::GetCapabilities", result); } /* Force capable? */ if (joystick->hwdata->Capabilities.dwFlags & DIDC_FORCEFEEDBACK) { result = IDirectInputDevice8_Acquire(joystick->hwdata->InputDevice); if (FAILED(result)) { return SetDIerror("IDirectInputDevice8::Acquire", result); } /* reset all actuators. */ result = IDirectInputDevice8_SendForceFeedbackCommand(joystick->hwdata-> InputDevice, DISFFC_RESET); /* Not necessarily supported, ignore if not supported. if (FAILED(result)) { return SetDIerror("IDirectInputDevice8::SendForceFeedbackCommand", result); } */ result = IDirectInputDevice8_Unacquire(joystick->hwdata->InputDevice); if (FAILED(result)) { return SetDIerror("IDirectInputDevice8::Unacquire", result); } /* Turn on auto-centering for a ForceFeedback device (until told * otherwise). */ dipdw.diph.dwObj = 0; dipdw.diph.dwHow = DIPH_DEVICE; dipdw.dwData = DIPROPAUTOCENTER_ON; result = IDirectInputDevice8_SetProperty(joystick->hwdata->InputDevice, DIPROP_AUTOCENTER, &dipdw.diph); /* Not necessarily supported, ignore if not supported. if (FAILED(result)) { return SetDIerror("IDirectInputDevice8::SetProperty", result); } */ } /* What buttons and axes does it have? */ IDirectInputDevice8_EnumObjects(joystick->hwdata->InputDevice, EnumDevObjectsCallback, joystick, DIDFT_BUTTON | DIDFT_AXIS | DIDFT_POV); /* Reorder the input objects. Some devices do not report the X axis as * the first axis, for example. */ SortDevObjects(joystick); dipdw.diph.dwObj = 0; dipdw.diph.dwHow = DIPH_DEVICE; dipdw.dwData = INPUT_QSIZE; /* Set the buffer size */ result = IDirectInputDevice8_SetProperty(joystick->hwdata->InputDevice, DIPROP_BUFFERSIZE, &dipdw.diph); if (result == DI_POLLEDDEVICE) { /* This device doesn't support buffering, so we're forced * to use less reliable polling. */ joystick->hwdata->buffered = SDL_FALSE; } else if (FAILED(result)) { return SetDIerror("IDirectInputDevice8::SetProperty", result); } return 0; }
void PERDXLoadDevices(char *inifilename) { char tempstr[MAX_PATH]; char string1[20]; char string2[20]; GUID guid; DIDEVCAPS didc; u32 i; int j, i2; int buttonid; DIPROPDWORD dipdw; int id; DWORD coopflags=DISCL_FOREGROUND | DISCL_NONEXCLUSIVE; BOOL loaddefault=TRUE; int numpads; HRESULT hr; if (!PERCore) return; PerPortReset(); memset(pad, 0, sizeof(pad)); // Check Connection Type if (GetPrivateProfileStringA("Input", "Port1Type", "", tempstr, MAX_PATH, inifilename) == 0) { // Check if it's using the old ini settings for peripherals if (GetPrivateProfileStringA("Peripheral1", "GUID", "", tempstr, MAX_PATH, inifilename) != 0) { // Convert to the newer type of settings for (i = 0; i < 2; i++) { sprintf(string1, "Port%dType", (int)i+1); WritePrivateProfileStringA("Input", string1, "1", inifilename); sprintf(string1, "Peripheral%d", (int)i+1); sprintf(string2, "Peripheral%dA", (int)i+1); if (GetPrivateProfileStringA(string1, "GUID", "", tempstr, MAX_PATH, inifilename)) WritePrivateProfileStringA(string2, "GUID", tempstr, inifilename); if (GetPrivateProfileStringA(string1, "EmulateType", "", tempstr, MAX_PATH, inifilename)) WritePrivateProfileStringA(string2, "EmulateType", tempstr, inifilename); for (i2 = 0; i2 < 13; i2++) { if (GetPrivateProfileStringA(string1, PerPadNames[i2], "", tempstr, MAX_PATH, inifilename)) WritePrivateProfileStringA(string2, PerPadNames[i2], tempstr, inifilename); } } // Remove old ini entries for (i = 0; i < 12; i++) { sprintf(string1, "Peripheral%d", (int)i+1); WritePrivateProfileStringA(string1, NULL, NULL, inifilename); } loaddefault = FALSE; } } else loaddefault = FALSE; if (loaddefault) { LoadDefaultPort1A(); return; } // Load new type settings for (i = 0; i < 2; i++) { sprintf(string1, "Port%dType", (int)i+1); if (GetPrivateProfileStringA("Input", string1, "", tempstr, MAX_PATH, inifilename) != 0) { porttype[i] = atoi(tempstr); switch(porttype[i]) { case 1: numpads = 1; break; case 2: numpads = 6; break; default: numpads = 0; break; } // Load new type settings for (j = 0; j < numpads; j++) { int padindex=(6*i)+j; padconf_struct *curdevice=&paddevice[padindex]; sprintf(string1, "Peripheral%d%C", (int)i+1, 'A' + j); // Let's first fetch the guid of the device if (GetPrivateProfileStringA(string1, "GUID", "", tempstr, MAX_PATH, inifilename) == 0) continue; if (GetPrivateProfileStringA(string1, "EmulateType", "0", string2, MAX_PATH, inifilename)) { curdevice->emulatetype = atoi(string2); if (curdevice->emulatetype == 0) continue; } if (curdevice->lpDIDevice) { // Free the default keyboard, etc. IDirectInputDevice8_Unacquire(curdevice->lpDIDevice); IDirectInputDevice8_Release(curdevice->lpDIDevice); } StringToGUID(tempstr, &guid); // Ok, now that we've got the GUID of the device, let's set it up if (FAILED(IDirectInput8_CreateDevice(lpDI8, &guid, &lpDIDevice[padindex], NULL) )) { curdevice->lpDIDevice = NULL; curdevice->emulatetype = 0; continue; } curdevice->lpDIDevice = lpDIDevice[padindex]; didc.dwSize = sizeof(DIDEVCAPS); if (FAILED(IDirectInputDevice8_GetCapabilities(lpDIDevice[padindex], &didc) )) continue; if (GET_DIDEVICE_TYPE(didc.dwDevType) == DI8DEVTYPE_KEYBOARD) { if (FAILED(IDirectInputDevice8_SetDataFormat(lpDIDevice[padindex], &c_dfDIKeyboard) )) continue; curdevice->type = TYPE_KEYBOARD; coopflags |= DISCL_NOWINKEY; } else if (GET_DIDEVICE_TYPE(didc.dwDevType) == DI8DEVTYPE_GAMEPAD || GET_DIDEVICE_TYPE(didc.dwDevType) == DI8DEVTYPE_JOYSTICK) { if (FAILED(IDirectInputDevice8_SetDataFormat(lpDIDevice[padindex], &c_dfDIJoystick2) )) continue; curdevice->type = TYPE_JOYSTICK; } else if (GET_DIDEVICE_TYPE(didc.dwDevType) == DI8DEVTYPE_MOUSE) { if (FAILED(IDirectInputDevice8_SetDataFormat(lpDIDevice[padindex], &c_dfDIMouse2) )) continue; curdevice->type = TYPE_MOUSE; coopflags = DISCL_FOREGROUND | DISCL_EXCLUSIVE; } hr = IDirectInputDevice8_SetCooperativeLevel(lpDIDevice[i], DXGetWindow(), coopflags); if (FAILED(hr)) continue; dipdw.diph.dwSize = sizeof(DIPROPDWORD); dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER); dipdw.diph.dwObj = 0; dipdw.diph.dwHow = DIPH_DEVICE; dipdw.dwData = 8; // should be enough // Setup Buffered input if (FAILED(IDirectInputDevice8_SetProperty(lpDIDevice[padindex], DIPROP_BUFFERSIZE, &dipdw.diph))) continue; IDirectInputDevice8_Acquire(lpDIDevice[padindex]); switch(curdevice->emulatetype) { case 1: // Standard Pad id = PERPAD; break; case 2: // Analog Pad case 3: // Stunner case 5: // Keyboard id = 0; break; case 4: // Mouse id = PERMOUSE; break; default: break; } // Make sure we're added to the smpc list if (i == 0) pad[padindex] = PerAddPeripheral(&PORTDATA1, id); else pad[padindex] = PerAddPeripheral(&PORTDATA2, id); // Now that we're all setup, let's fetch the controls from the ini if (curdevice->emulatetype != 3 && curdevice->emulatetype != 4) { for (i2 = 0; i2 < 13; i2++) { buttonid = GetPrivateProfileIntA(string1, PerPadNames[i2], 0, inifilename); PerSetKey(buttonid, i2, pad[padindex]); } } else if (curdevice->emulatetype == 4) { for (i2 = 0; i2 < 4; i2++) { buttonid = GetPrivateProfileIntA(string1, mouse_names[i2], 0, inifilename); PerSetKey(buttonid, PERMOUSE_LEFT+i2, pad[padindex]); } } } } } }
static BOOL CALLBACK enum_joypad_cb(const DIDEVICEINSTANCE *inst, void *p) { #ifdef HAVE_XINPUT bool is_xinput_pad; #endif LPDIRECTINPUTDEVICE8 *pad = NULL; settings_t *settings = config_get_ptr(); (void)p; if (g_joypad_cnt == MAX_USERS) return DIENUM_STOP; pad = &g_pads[g_joypad_cnt].joypad; #ifdef __cplusplus if (FAILED(IDirectInput8_CreateDevice( g_dinput_ctx, inst->guidInstance, pad, NULL))) #else if (FAILED(IDirectInput8_CreateDevice( g_dinput_ctx, &inst->guidInstance, pad, NULL))) #endif return DIENUM_CONTINUE; g_pads[g_joypad_cnt].joy_name = strdup(inst->tszProductName); g_pads[g_joypad_cnt].joy_friendly_name = strdup(inst->tszInstanceName); /* there may be more useful info in the GUID so leave this here for a while */ #if 0 printf("Guid = {%08lX-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX}\n", inst->guidProduct.Data1, inst->guidProduct.Data2, inst->guidProduct.Data3, inst->guidProduct.Data4[0], inst->guidProduct.Data4[1], inst->guidProduct.Data4[2], inst->guidProduct.Data4[3], inst->guidProduct.Data4[4], inst->guidProduct.Data4[5], inst->guidProduct.Data4[6], inst->guidProduct.Data4[7]); #endif g_pads[g_joypad_cnt].vid = inst->guidProduct.Data1 % 0x10000; g_pads[g_joypad_cnt].pid = inst->guidProduct.Data1 / 0x10000; RARCH_LOG("Device #%u PID: {%04lX} VID:{%04lX}\n", g_joypad_cnt, g_pads[g_joypad_cnt].pid, g_pads[g_joypad_cnt].vid); #ifdef HAVE_XINPUT is_xinput_pad = g_xinput_block_pads && guid_is_xinput_device(&inst->guidProduct); if (is_xinput_pad) { if (g_last_xinput_pad_idx < 4) g_xinput_pad_indexes[g_joypad_cnt] = g_last_xinput_pad_idx++; goto enum_iteration_done; } #endif IDirectInputDevice8_SetDataFormat(*pad, &c_dfDIJoystick2); IDirectInputDevice8_SetCooperativeLevel(*pad, (HWND)video_driver_window_get(), DISCL_NONEXCLUSIVE | DISCL_BACKGROUND); IDirectInputDevice8_EnumObjects(*pad, enum_axes_cb, *pad, DIDFT_ABSAXIS); #ifdef HAVE_XINPUT if (!is_xinput_pad) #endif { autoconfig_params_t params = {{0}}; strlcpy(settings->input.device_names[g_joypad_cnt], dinput_joypad_name(g_joypad_cnt), sizeof(settings->input.device_names[g_joypad_cnt])); params.idx = g_joypad_cnt; strlcpy(params.name, dinput_joypad_name(g_joypad_cnt), sizeof(params.name)); strlcpy(params.display_name, dinput_joypad_friendly_name(g_joypad_cnt), sizeof(params.driver)); strlcpy(params.driver, dinput_joypad.ident, sizeof(params.driver)); params.vid = dinput_joypad_vid(g_joypad_cnt); params.pid = dinput_joypad_pid(g_joypad_cnt); input_config_autoconfigure_joypad(¶ms); settings->input.pid[g_joypad_cnt] = params.pid; settings->input.vid[g_joypad_cnt] = params.vid; } #ifdef HAVE_XINPUT enum_iteration_done: #endif g_joypad_cnt++; return DIENUM_CONTINUE; }
static void test_preinitialization(void) { static const struct { REFGUID rguid; BOOL pdev; HRESULT expected_hr; } create_device_tests[] = { {NULL, FALSE, E_POINTER}, {NULL, TRUE, E_POINTER}, {&GUID_Unknown, FALSE, E_POINTER}, {&GUID_Unknown, TRUE, DIERR_NOTINITIALIZED}, {&GUID_SysMouse, FALSE, E_POINTER}, {&GUID_SysMouse, TRUE, DIERR_NOTINITIALIZED}, }; static const struct { DWORD dwDevType; LPDIENUMDEVICESCALLBACKA lpCallback; DWORD dwFlags; HRESULT expected_hr; int todo; } enum_devices_tests[] = { {0, NULL, 0, DIERR_INVALIDPARAM}, {0, NULL, ~0u, DIERR_INVALIDPARAM}, {0, dummy_callback, 0, DIERR_NOTINITIALIZED}, {0, dummy_callback, ~0u, DIERR_INVALIDPARAM}, {0xdeadbeef, NULL, 0, DIERR_INVALIDPARAM}, {0xdeadbeef, NULL, ~0u, DIERR_INVALIDPARAM}, {0xdeadbeef, dummy_callback, 0, DIERR_INVALIDPARAM}, {0xdeadbeef, dummy_callback, ~0u, DIERR_INVALIDPARAM}, }; IDirectInput8A *pDI; HRESULT hr; int i; IDirectInputDevice8A *pDID; hr = CoCreateInstance(&CLSID_DirectInput8, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectInput8A, (void **)&pDI); if (FAILED(hr)) { skip("Failed to instantiate a IDirectInputA instance: 0x%08x\n", hr); return; } for (i = 0; i < ARRAY_SIZE(create_device_tests); i++) { if (create_device_tests[i].pdev) pDID = (void *)0xdeadbeef; hr = IDirectInput8_CreateDevice(pDI, create_device_tests[i].rguid, create_device_tests[i].pdev ? &pDID : NULL, NULL); ok(hr == create_device_tests[i].expected_hr, "[%d] IDirectInput8_CreateDevice returned 0x%08x\n", i, hr); if (create_device_tests[i].pdev) ok(pDID == NULL, "[%d] Output interface pointer is %p\n", i, pDID); } for (i = 0; i < ARRAY_SIZE(enum_devices_tests); i++) { hr = IDirectInput8_EnumDevices(pDI, enum_devices_tests[i].dwDevType, enum_devices_tests[i].lpCallback, NULL, enum_devices_tests[i].dwFlags); todo_wine_if(enum_devices_tests[i].todo) ok(hr == enum_devices_tests[i].expected_hr, "[%d] IDirectInput8_EnumDevice returned 0x%08x\n", i, hr); } hr = IDirectInput8_GetDeviceStatus(pDI, NULL); ok(hr == E_POINTER, "IDirectInput8_GetDeviceStatus returned 0x%08x\n", hr); hr = IDirectInput8_GetDeviceStatus(pDI, &GUID_Unknown); ok(hr == DIERR_NOTINITIALIZED, "IDirectInput8_GetDeviceStatus returned 0x%08x\n", hr); hr = IDirectInput8_GetDeviceStatus(pDI, &GUID_SysMouse); ok(hr == DIERR_NOTINITIALIZED, "IDirectInput8_GetDeviceStatus returned 0x%08x\n", hr); hr = IDirectInput8_RunControlPanel(pDI, NULL, 0); ok(hr == DIERR_NOTINITIALIZED, "IDirectInput8_RunControlPanel returned 0x%08x\n", hr); hr = IDirectInput8_RunControlPanel(pDI, NULL, ~0u); ok(hr == DIERR_INVALIDPARAM, "IDirectInput8_RunControlPanel returned 0x%08x\n", hr); hr = IDirectInput8_RunControlPanel(pDI, (HWND)0xdeadbeef, 0); ok(hr == E_HANDLE, "IDirectInput8_RunControlPanel returned 0x%08x\n", hr); hr = IDirectInput8_RunControlPanel(pDI, (HWND)0xdeadbeef, ~0u); ok(hr == E_HANDLE, "IDirectInput8_RunControlPanel returned 0x%08x\n", hr); IDirectInput8_Release(pDI); }
int PERDXFetchNextPress(HWND hWnd, u32 guidnum, char *buttonname) { LPDIRECTINPUT8 lpDI8temp = NULL; LPDIRECTINPUTDEVICE8 lpDIDevicetemp; DIDEVCAPS didc; int buttonid=-1; if (FAILED(DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, &IID_IDirectInput8, (LPVOID *)&lpDI8temp, NULL))) return -1; if (FAILED(IDirectInput8_CreateDevice(lpDI8temp, &GUIDDevice[guidnum], &lpDIDevicetemp, NULL))) { IDirectInput8_Release(lpDI8temp); return -1; } didc.dwSize = sizeof(DIDEVCAPS); if (FAILED(IDirectInputDevice8_GetCapabilities(lpDIDevicetemp, &didc))) { IDirectInputDevice8_Release(lpDIDevicetemp); IDirectInput8_Release(lpDI8temp); return -1; } if (GET_DIDEVICE_TYPE(didc.dwDevType) == DI8DEVTYPE_KEYBOARD) { if (FAILED(IDirectInputDevice8_SetDataFormat(lpDIDevicetemp, &c_dfDIKeyboard))) { IDirectInputDevice8_Release(lpDIDevicetemp); IDirectInput8_Release(lpDI8temp); return -1; } } else if (GET_DIDEVICE_TYPE(didc.dwDevType) == DI8DEVTYPE_GAMEPAD || GET_DIDEVICE_TYPE(didc.dwDevType) == DI8DEVTYPE_JOYSTICK) { if (FAILED(IDirectInputDevice8_SetDataFormat(lpDIDevicetemp, &c_dfDIJoystick))) { IDirectInputDevice8_Release(lpDIDevicetemp); IDirectInput8_Release(lpDI8temp); return -1; } } else if (GET_DIDEVICE_TYPE(didc.dwDevType) == DI8DEVTYPE_MOUSE) { if (FAILED(IDirectInputDevice8_SetDataFormat(lpDIDevicetemp, &c_dfDIMouse2))) { IDirectInputDevice8_Release(lpDIDevicetemp); IDirectInput8_Release(lpDI8temp); return -1; } } if (DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_BUTTONCONFIG), hWnd, (DLGPROC)ButtonConfigDlgProc, (LPARAM)lpDIDevicetemp) == TRUE) { // Figure out what kind of code to generate if (GET_DIDEVICE_TYPE(didc.dwDevType) == DI8DEVTYPE_KEYBOARD) { memset(buttonname, 0, MAX_PATH); buttonid = nextpress.dwOfs; // This fixes some strange inconsistencies if (buttonid == DIK_PAUSE) buttonid = DIK_NUMLOCK; else if (buttonid == DIK_NUMLOCK) buttonid = DIK_PAUSE; if (buttonid & 0x80) buttonid += 0x80; GetKeyNameTextA(buttonid << 16, buttonname, MAX_PATH); buttonid = nextpress.dwOfs; } else if (GET_DIDEVICE_TYPE(didc.dwDevType) == DI8DEVTYPE_GAMEPAD || GET_DIDEVICE_TYPE(didc.dwDevType) == DI8DEVTYPE_JOYSTICK) { if (nextpress.dwOfs == DIJOFS_X) { if (nextpress.dwData <= 0x8000) { sprintf(buttonname, "Axis Left"); buttonid = 0x00; } else { sprintf(buttonname, "Axis Right"); buttonid = 0x01; } } else if (nextpress.dwOfs == DIJOFS_Y) { if (nextpress.dwData <= 0x8000) { sprintf(buttonname, "Axis Up"); buttonid = 0x02; } else { sprintf(buttonname, "Axis Down"); buttonid = 0x03; } } else if (nextpress.dwOfs == DIJOFS_POV(0)) { if (nextpress.dwData < 9000) { sprintf(buttonname, "POV Up"); buttonid = 0x04; } else if (nextpress.dwData < 18000) { sprintf(buttonname, "POV Right"); buttonid = 0x05; } else if (nextpress.dwData < 27000) { sprintf(buttonname, "POV Down"); buttonid = 0x06; } else { sprintf(buttonname, "POV Left"); buttonid = 0x07; } } else if (nextpress.dwOfs >= DIJOFS_BUTTON(0) && nextpress.dwOfs <= DIJOFS_BUTTON(127)) { sprintf(buttonname, "Button %d", (int)(nextpress.dwOfs - 0x2F)); buttonid = nextpress.dwOfs; } } else if (GET_DIDEVICE_TYPE(didc.dwDevType) == DI8DEVTYPE_MOUSE) { buttonid = nextpress.dwOfs-DIMOFS_BUTTON0; sprintf(buttonname, "Button %d", buttonid+1); } } IDirectInputDevice8_Unacquire(lpDIDevicetemp); IDirectInputDevice8_Release(lpDIDevicetemp); IDirectInput8_Release(lpDI8temp); return buttonid; }
int PERDXInitControlConfig(HWND hWnd, u8 padnum, int *controlmap, const char *inifilename) { char tempstr[MAX_PATH]; char string1[20]; GUID guid; u32 i; int idlist[] = { IDC_UPTEXT, IDC_RIGHTTEXT, IDC_DOWNTEXT, IDC_LEFTTEXT, IDC_RTEXT, IDC_LTEXT, IDC_STARTTEXT, IDC_ATEXT, IDC_BTEXT, IDC_CTEXT, IDC_XTEXT, IDC_YTEXT, IDC_ZTEXT }; sprintf(string1, "Peripheral%d%C", ((padnum/6)+1), 'A'+(padnum%6)); // Let's first fetch the guid of the device and see if we can get a match if (GetPrivateProfileStringA(string1, "GUID", "", tempstr, MAX_PATH, inifilename) == 0) { if (padnum == 0) { // Let's use default values SendDlgItemMessage(hWnd, IDC_DXDEVICECB, CB_SETCURSEL, 1, 0); controlmap[0] = DIK_UP; controlmap[1] = DIK_RIGHT; controlmap[2] = DIK_DOWN; controlmap[3] = DIK_LEFT; controlmap[4] = DIK_Z; controlmap[5] = DIK_X; controlmap[6] = DIK_J; controlmap[7] = DIK_K; controlmap[8] = DIK_L; controlmap[9] = DIK_M; controlmap[10] = DIK_U; controlmap[11] = DIK_I; controlmap[12] = DIK_O; for (i = 0; i < 13; i++) { ConvertKBIDToName(controlmap[i], tempstr); SetDlgItemText(hWnd, idlist[i], _16(tempstr)); } } else { SendDlgItemMessage(hWnd, IDC_DXDEVICECB, CB_SETCURSEL, 0, 0); return -1; } } else { LPDIRECTINPUT8 lpDI8temp = NULL; LPDIRECTINPUTDEVICE8 lpDIDevicetemp; DIDEVCAPS didc; int buttonid; StringToGUID(tempstr, &guid); // Let's find a match for (i = 0; i < numguids; i++) { if (memcmp(&guid, &GUIDDevice[i], sizeof(GUID)) == 0) { SendDlgItemMessage(hWnd, IDC_DXDEVICECB, CB_SETCURSEL, i+1, 0); break; } } if (FAILED(DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, &IID_IDirectInput8, (LPVOID *)&lpDI8temp, NULL))) return -1; if (FAILED(IDirectInput8_CreateDevice(lpDI8temp, &GUIDDevice[i], &lpDIDevicetemp, NULL))) { IDirectInput8_Release(lpDI8temp); return -1; } didc.dwSize = sizeof(DIDEVCAPS); if (FAILED(IDirectInputDevice8_GetCapabilities(lpDIDevicetemp, &didc))) { IDirectInputDevice8_Release(lpDIDevicetemp); IDirectInput8_Release(lpDI8temp); return -1; } if (GET_DIDEVICE_TYPE(didc.dwDevType) == DI8DEVTYPE_KEYBOARD) { sprintf(string1, "Peripheral%d%C", ((padnum/6)+1), 'A'+(padnum%6)); for (i = 0; i < 13; i++) { buttonid = GetPrivateProfileIntA(string1, PerPadNames[i], 0, inifilename); printf("%2d: %d\n", i, buttonid); controlmap[i] = buttonid; ConvertKBIDToName(buttonid, tempstr); SetDlgItemText(hWnd, idlist[i], _16(tempstr)); } } else if (GET_DIDEVICE_TYPE(didc.dwDevType) == DI8DEVTYPE_GAMEPAD || GET_DIDEVICE_TYPE(didc.dwDevType) == DI8DEVTYPE_JOYSTICK) { sprintf(string1, "Peripheral%d%C", ((padnum/6)+1), 'A'+(padnum%6)); for (i = 0; i < 13; i++) { buttonid = GetPrivateProfileIntA(string1, PerPadNames[i], 0, inifilename); controlmap[i] = buttonid; ConvertJoyIDToName(buttonid, tempstr); SetDlgItemText(hWnd, idlist[i], _16(tempstr)); } } else if (GET_DIDEVICE_TYPE(didc.dwDevType) == DI8DEVTYPE_MOUSE) { for (i = 0; i < 13; i++) { buttonid = GetPrivateProfileIntA(string1, PerPadNames[i], 0, inifilename); controlmap[i] = buttonid; ConvertMouseIDToName(buttonid, tempstr); SetDlgItemText(hWnd, idlist[i], _16(tempstr)); } } IDirectInputDevice8_Release(lpDIDevicetemp); IDirectInput8_Release(lpDI8temp); } return 0; }
static BOOL CALLBACK enum_joypad_cb(const DIDEVICEINSTANCE *inst, void *p) { bool is_xinput_pad; LPDIRECTINPUTDEVICE8 *pad = NULL; driver_t *driver = driver_get_ptr(); settings_t *settings = config_get_ptr(); (void)p; if (g_joypad_cnt == MAX_USERS) return DIENUM_STOP; pad = &g_pads[g_joypad_cnt].joypad; #ifdef __cplusplus if (FAILED(IDirectInput8_CreateDevice( g_dinput_ctx, inst->guidInstance, pad, NULL))) #else if (FAILED(IDirectInput8_CreateDevice( g_dinput_ctx, &inst->guidInstance, pad, NULL))) #endif return DIENUM_CONTINUE; g_pads[g_joypad_cnt].joy_name = strdup(inst->tszProductName); #ifdef HAVE_XINPUT #if 0 is_xinput_pad = g_xinput_block_pads && name_is_xinput_pad(inst->tszProductName); #endif is_xinput_pad = g_xinput_block_pads && guid_is_xinput_device(&inst->guidProduct); if (is_xinput_pad) { if (g_last_xinput_pad_idx < 4) g_xinput_pad_indexes[g_joypad_cnt] = g_last_xinput_pad_idx++; goto enum_iteration_done; } #endif IDirectInputDevice8_SetDataFormat(*pad, &c_dfDIJoystick2); IDirectInputDevice8_SetCooperativeLevel(*pad, (HWND)driver->video_window, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND); IDirectInputDevice8_EnumObjects(*pad, enum_axes_cb, *pad, DIDFT_ABSAXIS); #ifdef HAVE_XINPUT if (!is_xinput_pad) #endif { autoconfig_params_t params = {{0}}; strlcpy(settings->input.device_names[g_joypad_cnt], dinput_joypad_name(g_joypad_cnt), sizeof(settings->input.device_names[g_joypad_cnt])); /* TODO - implement VID/PID? */ params.idx = g_joypad_cnt; strlcpy(params.name, dinput_joypad_name(g_joypad_cnt), sizeof(params.name)); strlcpy(params.driver, dinput_joypad.ident, sizeof(params.driver)); input_config_autoconfigure_joypad(¶ms); } enum_iteration_done: g_joypad_cnt++; return DIENUM_CONTINUE; }
boolean I_InitJoystick(void) { DIDEVICEINSTANCE ddi; int i, joyProp[] = { DIJOFS_X, DIJOFS_Y, DIJOFS_Z, DIJOFS_RX, DIJOFS_RY, DIJOFS_RZ, DIJOFS_SLIDER(0), DIJOFS_SLIDER(1) }; const char *axisName[] = { "X", "Y", "Z", "RX", "RY", "RZ", "Slider 1", "Slider 2" }; HWND hWnd; HRESULT hr; if(ArgCheck("-nojoy")) return false; hWnd = Sys_GetWindowHandle(mainWindowIdx); if(!hWnd) { Con_Error("I_InitJoystick: Main window not available, cannot init joystick."); return false; } // ddi will contain info for the joystick device. memset(&firstJoystick, 0, sizeof(firstJoystick)); memset(&ddi, 0, sizeof(ddi)); counter = 0; // Find the joystick we want by doing an enumeration. IDirectInput_EnumDevices(dInput, DI8DEVCLASS_GAMECTRL, I_JoyEnum, &ddi, DIEDFL_ALLDEVICES); // Was the joystick we want found? if(!ddi.dwSize) { // Use the default joystick. if(!firstJoystick.dwSize) return false; // Not found. Con_Message("I_InitJoystick: joydevice = %i, out of range.\n", joydevice); // Use the first joystick that was found. memcpy(&ddi, &firstJoystick, sizeof(ddi)); } // Show some info. Con_Message("I_InitJoystick: %s\n", ddi.tszProductName); // Create the joystick device. hr = IDirectInput8_CreateDevice(dInput, &ddi.guidInstance, &didJoy, 0); if(FAILED(hr)) { Con_Message("I_InitJoystick: Failed to create device (0x%x).\n", hr); return false; } // Set data format. if(FAILED(hr = IDirectInputDevice_SetDataFormat(didJoy, &c_dfDIJoystick))) { Con_Message("I_InitJoystick: Failed to set data format (0x%x).\n", hr); goto kill_joy; } // Set behaviour. if(FAILED (hr = IDirectInputDevice_SetCooperativeLevel(didJoy, hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND))) { Con_Message("I_InitJoystick: Failed to set co-op level (0x%x: %s).\n", hr, I_ErrorMsg(hr)); goto kill_joy; } // Set properties. for(i = 0; i < sizeof(joyProp) / sizeof(joyProp[0]); i++) { if(FAILED (hr = I_SetRangeProperty(didJoy, DIPROP_RANGE, DIPH_BYOFFSET, joyProp[i], IJOY_AXISMIN, IJOY_AXISMAX))) { if(verbose) Con_Message("I_InitJoystick: Failed to set %s " "range (0x%x: %s).\n", axisName[i], hr, I_ErrorMsg(hr)); } } // Set no dead zone. if(FAILED(hr = I_SetProperty(didJoy, DIPROP_DEADZONE, DIPH_DEVICE, 0, 0))) { Con_Message("I_InitJoystick: Failed to set dead zone (0x%x: %s).\n", hr, I_ErrorMsg(hr)); } // Set absolute mode. if(FAILED (hr = I_SetProperty(didJoy, DIPROP_AXISMODE, DIPH_DEVICE, 0, DIPROPAXISMODE_ABS))) { Con_Message ("I_InitJoystick: Failed to set absolute axis mode (0x%x: %s).\n", hr, I_ErrorMsg(hr)); } // Acquire it. IDirectInputDevice_Acquire(didJoy); // Initialization was successful. return true; kill_joy: I_SAFE_RELEASE(didJoy); return false; }
int PERDXInit(void) { DIPROPDWORD dipdw; char tempstr[512]; HRESULT ret; memset(pad, 0, sizeof(pad)); memset(paddevice, 0, sizeof(paddevice)); if (FAILED((ret = DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, &IID_IDirectInput8, (LPVOID *)&lpDI8, NULL)) )) { sprintf(tempstr, "DirectInput8Create error: %s - %s", DXGetErrorString8(ret), DXGetErrorDescription8(ret)); MessageBox (NULL, _16(tempstr), _16("Error"), MB_OK | MB_ICONINFORMATION); return -1; } IDirectInput8_EnumDevices(lpDI8, DI8DEVCLASS_ALL, EnumPeripheralsCallback, NULL, DIEDFL_ATTACHEDONLY); if (FAILED((ret = IDirectInput8_CreateDevice(lpDI8, &GUID_SysKeyboard, &lpDIDevice[0], NULL)) )) { sprintf(tempstr, "IDirectInput8_CreateDevice error: %s - %s", DXGetErrorString8(ret), DXGetErrorDescription8(ret)); MessageBox (NULL, _16(tempstr), _16("Error"), MB_OK | MB_ICONINFORMATION); return -1; } if (FAILED((ret = IDirectInputDevice8_SetDataFormat(lpDIDevice[0], &c_dfDIKeyboard)) )) { sprintf(tempstr, "IDirectInputDevice8_SetDataFormat error: %s - %s", DXGetErrorString8(ret), DXGetErrorDescription8(ret)); MessageBox (NULL, _16(tempstr), _16("Error"), MB_OK | MB_ICONINFORMATION); return -1; } if (FAILED((ret = IDirectInputDevice8_SetCooperativeLevel(lpDIDevice[0], DXGetWindow(), DISCL_FOREGROUND | DISCL_NONEXCLUSIVE | DISCL_NOWINKEY)) )) { sprintf(tempstr, "IDirectInputDevice8_SetCooperativeLevel error: %s - %s", DXGetErrorString8(ret), DXGetErrorDescription8(ret)); MessageBox (NULL, _16(tempstr), _16("Error"), MB_OK | MB_ICONINFORMATION); return -1; } dipdw.diph.dwSize = sizeof(DIPROPDWORD); dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER); dipdw.diph.dwObj = 0; dipdw.diph.dwHow = DIPH_DEVICE; dipdw.dwData = 8; // should be enough // Setup Buffered input if (FAILED((ret = IDirectInputDevice8_SetProperty(lpDIDevice[0], DIPROP_BUFFERSIZE, &dipdw.diph)) )) { sprintf(tempstr, "IDirectInputDevice8_SetProperty error: %s - %s", DXGetErrorString8(ret), DXGetErrorDescription8(ret)); MessageBox (NULL, _16(tempstr), _16("Error"), MB_OK | MB_ICONINFORMATION); return -1; } // Make sure Keyboard is acquired already IDirectInputDevice8_Acquire(lpDIDevice[0]); paddevice[0].lpDIDevice = lpDIDevice[0]; paddevice[0].type = TYPE_KEYBOARD; paddevice[0].emulatetype = 1; PerPortReset(); LoadDefaultPort1A(); return 0; }