Exemplo n.º 1
0
/**
 * 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;
}
Exemplo n.º 2
0
BOOL CALLBACK EnumJoystickProc (LPCDIDEVICEINSTANCE pdiDevice_, LPVOID lpv_)
{
    HRESULT hr;

    LPDIRECTINPUTDEVICE pdiJoystick;
    if (FAILED(hr = pdi->CreateDevice(pdiDevice_->guidInstance, &pdiJoystick, NULL)))
        TRACE("!!! Failed to create joystick device (%#08lx)\n", hr);
    else
    {
        DIDEVICEINSTANCE didi = { sizeof(didi) };
        strcpy(didi.tszInstanceName, "<unknown>");  // WINE fix for missing implementation

        if (FAILED(hr = pdiJoystick->GetDeviceInfo(&didi)))
            TRACE("!!! Failed to get joystick device info (%#08lx)\n", hr);

        // Overloaded use - if custom data was supplied, it's a combo box ID to add the string to
        else if (lpv_)
            SendMessage(reinterpret_cast<HWND>(lpv_), CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(didi.tszInstanceName));
        else
        {
            IDirectInputDevice2* pDevice;

            // We need an IDirectInputDevice2 interface for polling, so query for it
            if (FAILED(hr = pdiJoystick->QueryInterface(IID_IDirectInputDevice2, reinterpret_cast<void **>(&pDevice))))
                TRACE("!!! Failed to query joystick for IID_IDirectInputDevice2 (%#08lx)\n", hr);

            // If the device name matches the joystick 1 device name, save a pointer to it
            else if (!lstrcmpi(didi.tszInstanceName, GetOption(joydev1)))
                pdidJoystick1 = pDevice;

            // If the device name matches the joystick 2 device name, save a pointer to it
            else if (!lstrcmpi(didi.tszInstanceName, GetOption(joydev2)))
                pdidJoystick2 = pDevice;

            // No match
            else
                pDevice->Release();

            pdiJoystick->Release();
        }
    }

    // Continue looking for other devices, even tho we failed with the current one
    return DIENUM_CONTINUE;
}
Exemplo n.º 3
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;
}