/** * input_dinput_set_cooperative_level_joysticks(): Sets the cooperative level on joysticks. * @param hWnd Window to set the cooperative level on. * @return 0 on success; non-zero on error. */ int input_dinput_set_cooperative_level_joysticks(HWND hWnd) { // If no hWnd was specified, use the Gens window. if (!hWnd) hWnd = gens_window; HRESULT rval; for (int i = 0; i < MAX_JOYS; i++) { IDirectInputDevice2 *pJoyDevice = input_dinput_joy_id[i]; if (!pJoyDevice) continue; rval = pJoyDevice->SetCooperativeLevel(hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND); if (rval != DI_OK) { LOG_MSG(input, LOG_MSG_LEVEL_WARNING, "SetCooperativeLevel() failed on joystick %d.", i); pJoyDevice->Release(); input_dinput_joy_id[i] = NULL; } else { LOG_MSG(input, LOG_MSG_LEVEL_INFO, "SetCooperativeLevel() succeeded on joystick %d.", i); } } return 0; }
/** * input_dinput_callback_init_joysticks_enum(): EnumDevices callback for initializing joysticks. * @param lpDIIJoy Joystick information. * @param pvRef hWnd of the Gens window. * @return DIENUM_CONTINUE to continue the enumeration; DIENUM_STOP to stop the enumeration. */ static BOOL CALLBACK input_dinput_callback_init_joysticks_enum(LPCDIDEVICEINSTANCE lpDIIJoy, LPVOID pvRef) { HRESULT rval; LPDIRECTINPUTDEVICE lpDIJoy; if (!lpDIIJoy || input_dinput_num_joysticks >= MAX_JOYS || input_dinput_callback_init_joysticks_enum_counter >= MAX_JOYS) return DIENUM_STOP; // Number of times this function has been called. int cur_joystick = input_dinput_callback_init_joysticks_enum_counter; input_dinput_callback_init_joysticks_enum_counter++; rval = lpDI->CreateDevice(lpDIIJoy->guidInstance, &lpDIJoy, NULL); if (rval != DI_OK) { LOG_MSG(input, LOG_MSG_LEVEL_CRITICAL, "lpDI->CreateDevice() failed. (Joystick %d)", cur_joystick); return DIENUM_CONTINUE; } IDirectInputDevice2 *pJoyDevice = NULL; input_dinput_joy_id[input_dinput_num_joysticks] = NULL; rval = lpDIJoy->QueryInterface(IID_IDirectInputDevice2, (void**)&pJoyDevice); lpDIJoy->Release(); if (rval != DI_OK || !pJoyDevice) { LOG_MSG(input, LOG_MSG_LEVEL_CRITICAL, "lpDIJoy->QueryInterface() failed. (Joystick %d)", cur_joystick); return DIENUM_CONTINUE; } rval = pJoyDevice->SetDataFormat(&c_dfDIJoystick); if (rval != DI_OK) { LOG_MSG(input, LOG_MSG_LEVEL_CRITICAL, "pJoyDevice->SetDatFormat(&c_dfDIJoystick) failed. (Joystick %d)", cur_joystick); pJoyDevice->Release(); return DIENUM_CONTINUE; } // TODO: Add an option to specify DISCL_FOREGROUND so the joysticks only work when the Gens window is active. rval = pJoyDevice->SetCooperativeLevel((HWND)pvRef, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND); if (rval != DI_OK) { LOG_MSG(input, LOG_MSG_LEVEL_WARNING, "pJoyDevice->SetCooperativeLevel() failed. (Joystick %d)", cur_joystick); pJoyDevice->Release(); return DIENUM_CONTINUE; } // Attempt to acquire the joystick device. for (unsigned int i = 10; i != 0; i--) { rval = pJoyDevice->Acquire(); if (rval == DI_OK) break; GensUI::sleep(10); } input_dinput_joy_id[input_dinput_num_joysticks] = pJoyDevice; input_dinput_num_joysticks++; return DIENUM_CONTINUE; }