C_RESULT open_dx_gamepad(void) { HRESULT hr; HWND hDlg = GetConsoleHwnd(); // Register with the DirectInput subsystem and get a pointer // to a IDirectInput interface we can use. // Create a DInput object if (g_pDI==NULL) if( VP_FAILED( hr = DirectInput8Create( GetModuleHandle( NULL ), DIRECTINPUT_VERSION, IID_IDirectInput8, ( VOID** )&g_pDI, NULL ) ) ) return hr; if( g_bFilterOutXinputDevices ) SetupForIsXInputDevice(); DIJOYCONFIG PreferredJoyCfg = {0}; DI_ENUM_CONTEXT enumContext; enumContext.pPreferredJoyCfg = &PreferredJoyCfg; enumContext.bPreferredJoyCfgValid = false; IDirectInputJoyConfig8* pJoyConfig = NULL; if( VP_FAILED( hr = g_pDI->QueryInterface( IID_IDirectInputJoyConfig8, ( void** )&pJoyConfig ) ) ) return hr; PreferredJoyCfg.dwSize = sizeof( PreferredJoyCfg ); if( SUCCEEDED( pJoyConfig->GetConfig( 0, &PreferredJoyCfg, DIJC_GUIDINSTANCE ) ) ) // This function is expected to fail if no g_pJoystick is attached enumContext.bPreferredJoyCfgValid = true; SAFE_RELEASE( pJoyConfig ); // Look for a simple g_pJoystick we can use for this sample program. if( VP_FAILED( hr = g_pDI->EnumDevices( DI8DEVCLASS_GAMECTRL, Enumg_pJoysticksCallback, &enumContext, DIEDFL_ATTACHEDONLY ) ) ) return hr; if( g_bFilterOutXinputDevices ) CleanupForIsXInputDevice(); // Make sure we got a g_pJoystick if( g_pJoystick == NULL ) { //MessageBox( NULL, TEXT( "Joystick not found." ), // TEXT( "A.R. Drone"), // MB_ICONERROR | MB_OK ); // EndDialog( hDlg, 0 ); return C_FAIL; } // Set the data format to "simple g_pJoystick" - a predefined data format // // A data format specifies which controls on a device we are interested in, // and how they should be reported. This tells DInput that we will be // passing a DIJOYSTATE2 structure to IDirectInputDevice::GetDeviceState(). if( VP_FAILED( hr = g_pJoystick->SetDataFormat( &c_dfDIJoystick2 ) ) ) return C_FAIL; // Set the cooperative level to let DInput know how this device should // interact with the system and with other DInput applications. if( VP_FAILED( hr = g_pJoystick->SetCooperativeLevel( hDlg , DISCL_EXCLUSIVE | DISCL_FOREGROUND ) ) ) return C_FAIL; // Enumerate the g_pJoystick objects. The callback function enabled user // interface elements for objects that are found, and sets the min/max // values property for discovered axes. if( VP_FAILED( hr = g_pJoystick->EnumObjects( EnumObjectsCallback, ( VOID* )hDlg, DIDFT_ALL ) ) ) return C_FAIL; return C_OK; }
HRESULT InitDirectInput(unsigned int joystick_index) { HRESULT hr; state.is_initialized = false; // Register with the DirectInput subsystem and get a pointer // to a IDirectInput interface we can use. // Create a DInput object if (FAILED(hr = DirectInput8Create(GetModuleHandle(nullptr), DIRECTINPUT_VERSION, IID_IDirectInput8, (VOID**)&g_pDI, nullptr))) return hr; if (g_bFilterOutXinputDevices) SetupForIsXInputDevice(); enumContext.pPreferredJoyCfg = &PreferredJoyCfg; enumContext.bPreferredJoyCfgValid = false; IDirectInputJoyConfig8* pJoyConfig = nullptr; if (FAILED(hr = g_pDI->QueryInterface(IID_IDirectInputJoyConfig8, (void**)&pJoyConfig))) { state.message = "QueryInterface on IID_IDirectInputJoyConfig8 failed"; return hr; } PreferredJoyCfg.dwSize = sizeof(PreferredJoyCfg); if (SUCCEEDED(pJoyConfig->GetConfig(joystick_index, &PreferredJoyCfg, DIJC_GUIDINSTANCE))) { // This function is expected to fail if no joystick is attached enumContext.bPreferredJoyCfgValid = true; joystick_info.is_valid = true; joystick_info.instance_guide = toDIGUID(PreferredJoyCfg.guidInstance); joystick_info.pid_vid = toString(PreferredJoyCfg.wszType); } DIJT_SAFE_RELEASE(pJoyConfig); // Look for a simple joystick we can use for this sample program. if (FAILED(hr = g_pDI->EnumDevices(DI8DEVCLASS_GAMECTRL, DirectInputJoyStick::impl::EnumJoysticksCallback, this, DIEDFL_ATTACHEDONLY))) { state.message = "EnumDevices failed"; return hr; } if (g_bFilterOutXinputDevices) CleanupForIsXInputDevice(); // Make sure we got a joystick if (!g_pJoystick) { state.message = "Joystick at index " + std::to_string(joystick_index) + " is not available"; return S_FALSE; } // Set the data format to "simple joystick" - a predefined data format // // A data format specifies which controls on a device we are interested in, // and how they should be reported. This tells DInput that we will be // passing a DIJOYSTATE2 structure to IDirectInputDevice::GetDeviceState(). if (FAILED(hr = g_pJoystick->SetDataFormat(&c_dfDIJoystick2))) { state.message = "Device does not support DIJOYSTATE2"; return hr; } // Set the cooperative level to let DInput know how this device should // interact with the system and with other DInput applications. if (FAILED(hr = g_pJoystick->SetCooperativeLevel(NULL, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND))) { state.message = "SetCooperativeLevel failed"; return hr; } // Enumerate the joystick objects. The callback function enabled user // interface elements for objects that are found, and sets the min/max // values property for discovered axes. if (FAILED(hr = g_pJoystick->EnumObjects(DirectInputJoyStick::impl::EnumObjectsCallback, (VOID*) this, DIDFT_ALL))) { state.message = "EnumObjects failed"; return hr; } InitForceFeedback(); state.is_initialized = true; return S_OK; }
void InputSystem::initialize() { #ifndef PLATFORM_WP8 m_windowHandle = graphics.getWidnowHandle(); DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&m_directInput, NULL); // // keyboard // m_directInput->CreateDevice(GUID_SysKeyboard, &m_keyboard, NULL); m_keyboard->SetDataFormat(&c_dfDIKeyboard); if(!m_inputExclusive) { m_keyboard->SetCooperativeLevel(m_windowHandle, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); } else { m_keyboard->SetCooperativeLevel(m_windowHandle, DISCL_EXCLUSIVE); } m_keyboard->Acquire(); // // mouse // m_directInput->CreateDevice(GUID_SysMouse, &m_mouse, NULL); m_mouse->SetDataFormat(&c_dfDIMouse); if(!m_inputExclusive) { m_mouse->SetCooperativeLevel(m_windowHandle, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); } else { m_mouse->SetCooperativeLevel(m_windowHandle, DISCL_EXCLUSIVE); } m_mouse->Acquire(); for(int i = 0; i < 256; ++i) { m_oldKeys[i] = 0; m_keyState[i] = 0; m_keyHoldDuration[i] = 0; m_keyHoldStart[i] = 0; m_keyTapCount[i] = 0; m_keyTapFrequency[i] = 0; m_keyTotalPressCount[i] = 0; m_keySequence[i] = -1; } for(int i = 0; i < 4; ++i) { m_oldButtons[i] = 0; m_buttonHoldDuration[i] = 0; m_buttonHoldStart[i] = 0; m_buttonTapCount[i] = 0; m_buttonTapFrequency[i] = 0; m_buttonTotalPressCount[i] = 0; } m_tapTimer.reset(); // // joystick // m_joystickValidity = 0; DIJOYCONFIG conf = {0}; DI_ENUM_CONTEXT enumContext; enumContext.pPreferredJoyConfig = &conf; enumContext.bPreferredJoyConfigValid = 0; IDirectInputJoyConfig8 *joyConfig = 0; if(FAILED(m_directInput->QueryInterface(IID_IDirectInputJoyConfig8, (void **)&joyConfig))) { m_joystickValidity = 0; return; } conf.dwSize = sizeof(conf); if(SUCCEEDED(joyConfig->GetConfig(0, &conf, DIJC_GUIDINSTANCE))) { enumContext.bPreferredJoyConfigValid = 1; } SAFE_RELEASE(joyConfig); if(FAILED(m_directInput->CreateDevice(GUID_Joystick, &m_joystick, 0))) { m_joystickValidity = 0; return; } if(FAILED(m_joystick->SetDataFormat(&c_dfDIJoystick2))) { m_joystickValidity = 0; return; } if(!m_inputExclusive) { m_joystick->SetCooperativeLevel(m_windowHandle, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); } else { m_joystick->SetCooperativeLevel(m_windowHandle, DISCL_EXCLUSIVE); } m_joystick->Acquire(); for(int i = 0; i < 128; ++i) { m_oldJoystickButtons[i] = 0; } m_joystickValidity = 1; #else for(int i = 0; i < INPUT_SUPPORTED_TOUCH_COUNT; ++i) { m_touchList[i] = 0; m_oldTouchList[i] = 0; m_touchHoldDuration[i] = 0; m_touchHoldStart[i] = 0; m_touchTapCount[i] = 0; m_touchTapFrequency[i] = 0; m_touchTotalPressCount[i] = 0; } m_tapTimer.reset(); m_backButtonPressed = 0; #endif }
//=============================================== //ジョイスティックの初期化 //=============================================== //[input] // なし //[return] // hr //=============================================== HRESULT CInput::InitJoystick() { HRESULT hr; if(m_IsFilterXInputDevice) { SetupXInputDevice(); } DIJOYCONFIG PreferJoyCfg = {0}; DI_ENUM_CONTEXT eContext; eContext.pPrefrredJoyCfg = &PreferJoyCfg; eContext.IsPreferredJoyCfg = false; IDirectInputJoyConfig8 *pJoyConfig = NULL; hr = m_pDirectInput->QueryInterface(IID_IDirectInputJoyConfig8, (void **)&pJoyConfig); if(FAILED(hr) ) { return hr; } PreferJoyCfg.dwSize = sizeof(PreferJoyCfg); hr = pJoyConfig->GetConfig(0, &PreferJoyCfg, DIJC_GUIDINSTANCE); if(SUCCEEDED(hr) ) { eContext.IsPreferredJoyCfg = true; } RELEASE(pJoyConfig); //------------------------------------ //使用可能なジョイスティック数 //------------------------------------ m_JoystickMax = joyGetNumDevs(); if(m_JoystickMax >= JOYSTICK_MAX) { m_JoystickMax = JOYSTICK_MAX; } //------------------------------------ //ジョイスティックの列挙 //------------------------------------ m_JoystickCount = 0; hr = m_pDirectInput->EnumDevices(DI8DEVCLASS_GAMECTRL, EnumJoysticksCallback, &eContext, DIEDFL_ATTACHEDONLY); if(FAILED(hr) ) { for(int i = 0;i < JOYSTICK_MAX;i++) { F_RELEASE(m_pJoystickData[i].pDevice); return hr; } } for(Uint32 i = 0;i < m_JoystickCount;i++) { if(m_IsFilterXInputDevice) { ReleaseXInputDevice(); } if(m_pJoystickData[i].pDevice == NULL) { MESSAGE("ジョイスティックが見つかりません"); return S_OK; } //------------------------------------ //データフォーマットの設定 //------------------------------------ hr = m_pJoystickData[i].pDevice->SetDataFormat(&c_dfDIJoystick2); if(FAILED(hr) ) { return hr; } //------------------------------------ //協調レベルの設定 //------------------------------------ hr = m_pJoystickData[i].pDevice->SetCooperativeLevel(Joker::GetHWnd(), DISCL_EXCLUSIVE | DISCL_FOREGROUND); if(FAILED(hr) ) { return hr; } //----------------------------------- //軸モードの設定 //----------------------------------- hr = m_pJoystickData[i].pDevice->EnumObjects(EnumObjectsCallback, (VOID*)m_pJoystickData[i].pDevice, DIDFT_ALL); if(FAILED(hr) ) { return hr; } //----------------------------------- //デバイス情報取得 //----------------------------------- DIDEVCAPS caps = {sizeof(DIDEVCAPS) }; m_pJoystickData[i].pDevice->GetCapabilities(&caps); //ポーリングが必要か調べる if(caps.dwFlags & (DIDC_POLLEDDATAFORMAT & DIDC_POLLEDDEVICE) ) { m_pJoystickData[i].IsPolling = true; } else { m_pJoystickData[i].IsPolling = false; } /*入力制御開始*/ m_pJoystickData[i].pDevice->Acquire(); } return S_OK; }
bool VistaDirectXGamepad::InitDirectXInput() { // get window handle from touch sequence GamepadAttach *pAttach = dynamic_cast<GamepadAttach*>(m_pWindowAspect->GetTouchSequence()); if(pAttach && pAttach->m_nHwnd == 0L) return false; DIJOYCONFIG PreferredJoyCfg = {0}; DI_ENUM_CONTEXT enumContext; memset(&enumContext, 0, sizeof(DI_ENUM_CONTEXT)); enumContext.pPreferredJoyCfg = &PreferredJoyCfg; enumContext.bPreferredJoyCfgValid = false; enumContext.m_pDI = m_pDI; enumContext.m_pJoystick = m_pJoystick; IDirectInputJoyConfig8* pJoyConfig = NULL; HRESULT hr; // claim a joystick config structure hr = m_pDI->QueryInterface( IID_IDirectInputJoyConfig8, (void **) &pJoyConfig ); // fill the config structure with information from the // windows system database PreferredJoyCfg.dwSize = sizeof(PreferredJoyCfg); // simply try to claim config for joystick 0 // This function is expected to fail if no joystick is attached // as index 0 is not present if( SUCCEEDED( pJoyConfig->GetConfig(0, &PreferredJoyCfg, DIJC_GUIDINSTANCE ) ) ) enumContext.bPreferredJoyCfgValid = true; // give away this structure if(pJoyConfig) pJoyConfig->Release(); // Look for a joystick if(m_strJoyName.empty()) { hr = m_pDI->EnumDevices( DI8DEVCLASS_GAMECTRL, EnumJoysticksCallback, &enumContext, DIEDFL_ATTACHEDONLY ); } else { enumContext.m_strJoyName = m_strJoyName; enumContext.bPreferredJoyCfgValid = false; hr = m_pDI->EnumDevices( DI8DEVCLASS_GAMECTRL, FindJoysticksCallback, &enumContext, DIEDFL_ATTACHEDONLY ); } if( hr == S_OK && enumContext.m_pJoystick ) { // copy the joystick allocation routine to // our member pointer m_pJoystick = enumContext.m_pJoystick; DIDEVICEINSTANCE DeviceInfo; DeviceInfo.dwSize=sizeof(DIDEVICEINSTANCE); if( FAILED( hr = m_pJoystick->GetDeviceInfo(&DeviceInfo))){ std::cerr<<"GetDeviceInfo failed!\n"; return false; } std::cout << "VistaDirectXGamepad::InitDirextX() -- found [" << DeviceInfo.tszInstanceName << "] controller\n"; // set the desired data-format // c_dfDIJoystick2 is a global enum from directX hr = m_pJoystick->SetDataFormat( &c_dfDIJoystick2 ); if(hr != S_OK ) { std::cerr << "Could not set DataFormat for Joystick.\n"; } // Set the cooperative level to let DInput know how this device should // interact with the system and with other DInput applications. hr = m_pJoystick->SetCooperativeLevel( pAttach->m_nHwnd, DISCL_EXCLUSIVE | DISCL_BACKGROUND ); if( hr != S_OK ) { std::cerr << "Could not set cooperative level\n"; return false; } // normalize axes and determine number of buttons _sEnumJoystickContext s(m_pJoystick); hr = m_pJoystick->EnumObjects( EnumObjectsCallback, (VOID*)&s , DIDFT_AXIS | DIDFT_BUTTON ); if( hr != S_OK ) std::cout<<"\nEnumObjects failed (no axis or buttons?)!"; m_nNumberOfButtons = s.m_nBtNum; std::cout << "This controller has [" << s.m_nBtNum << "] buttons.\n"; DIPROPDWORD dipdw; dipdw.diph.dwSize = sizeof(DIPROPDWORD); dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER); dipdw.diph.dwObj = 0; dipdw.diph.dwHow = DIPH_DEVICE; dipdw.dwData = DIPROPAXISMODE_ABS; //DIPROPAXISMODE_ABS or DIPROPAXISMODE_REL hr = m_pJoystick->SetProperty(DIPROP_AXISMODE , &dipdw.diph); if(hr != S_OK) std::cerr << "Could not set axis mode\n"; dipdw.dwData = 100; // = 1% hr = m_pJoystick->SetProperty(DIPROP_DEADZONE , &dipdw.diph); if(hr != S_OK) std::cerr << "Could not set DEADZONE\n"; dipdw.dwData = 10000; // = 100% hr = m_pJoystick->SetProperty(DIPROP_SATURATION , &dipdw.diph); if(hr != S_OK) std::cerr << "Could not set SATURATION\n"; hr = m_pJoystick->Acquire(); if( hr != S_OK ) std::cerr << "Could not acquire initially!\n"; m_eDriver = TP_DIRECTX; // try to figure out whether this driver has a force feedback support or not if(s.m_nForceFeedbackAxes != 0) { // yes! VistaDirectXForceFeedbackAspect *pForce = new VistaDirectXForceFeedbackAspect(this); // configure force aspect // assign and register as aspect m_pForceAspect = pForce; RegisterAspect( m_pForceAspect ); } return true; } else { std::cerr << "VistaDirectXGamepad::InitDirectXInput() " << "-- FAILED to enumerate a joystick" << std::endl; if(enumContext.m_nCount == 0) std::cerr << "VistaDirectXGamepad::InitDirectXInput() -- " << "Maybe no joystick attached?\n"; } return false; }