static int Open(void *hwnd, int bForceFeedback) { SYS_DXTRACE(g_lpDI->EnumDevices(DI8DEVCLASS_GAMECTRL, EnumFFJoysticksCallback, NULL, DIEDFL_ATTACHEDONLY | (bForceFeedback ? DIEDFL_FORCEFEEDBACK : 0))); if (g_pJoystick == NULL) { return FALSE; } if (SYS_DXTRACE(g_pJoystick->SetDataFormat(&c_dfDIJoystick2))) { return FALSE; } if (SYS_DXTRACE(g_pJoystick->SetCooperativeLevel((HWND)hwnd, DISCL_EXCLUSIVE | DISCL_BACKGROUND))) { return FALSE; } if (bForceFeedback) { DIPROPDWORD dipdw; dipdw.diph.dwSize = sizeof(DIPROPDWORD); dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER); dipdw.diph.dwObj = 0; dipdw.diph.dwHow = DIPH_DEVICE; dipdw.dwData = FALSE; return !SYS_DXTRACE(g_pJoystick->SetProperty(DIPROP_AUTOCENTER, &dipdw.diph)); } DIDEVCAPS caps; caps.dwSize = sizeof(caps); g_pJoystick->GetCapabilities(&caps); if (!SYS_DXTRACE(g_pJoystick->GetCapabilities(&caps))) { sJOY->numButtons = caps.dwButtons; sJOY->numAxes = caps.dwAxes; sJOY->numPOVs = caps.dwPOVs; } sJOY->numControllers = 1; SYS_DXTRACE(g_pJoystick->Acquire()); return TRUE; }
bool CG27::InitializeG27() { HRESULT hr; // Register with the DirectInput subsystem and get a pointer to a IDirectInput interface we can use. // Create a DInput object //initialize directinput library if( FAILED( hr = DirectInput8Create( GetModuleHandle( NULL ), DIRECTINPUT_VERSION, IID_IDirectInput8, ( VOID** )&g_pDI, NULL ) ) ) return false; LPDIRECTINPUTDEVICE8 joystick; // Look for the first simple joystick we can find. if (FAILED(hr = g_pDI->EnumDevices(DI8DEVCLASS_GAMECTRL, enumCallback, NULL, DIEDFL_ATTACHEDONLY))) { return false; } if (g_pJoystick == NULL) { // ShowMessageBox("g27 not found, please check the connection, exiting........"); qDebug() << "fucklrl"; return false; } if (FAILED(hr = g_pJoystick->SetDataFormat(&c_dfDIJoystick2))) { // ShowMessageBox(" set g27 data format error, exiting......."); qDebug() << "fuckld"; return false; } g_pJoystick->SetCooperativeLevel(NULL, DISCL_EXCLUSIVE|DISCL_FOREGROUND); DIDEVCAPS capabilities; capabilities.dwSize = sizeof(DIDEVCAPS); g_pJoystick->GetCapabilities(&capabilities); if (FAILED(hr=g_pJoystick->EnumObjects(enumAxesCallback, NULL, DIDFT_AXIS))) { } qDebug() << "initializing succeeded"; return true; }
BOOL CALLBACK CJoystick::EnumJoysticksCallback( const DIDEVICEINSTANCE* pdidInstance, VOID* pContext ) { HRESULT hr; CJoystick* p_this = (CJoystick*) pContext; LPDIRECTINPUTDEVICE8 pJoystick = NULL; // Obtain an interface to the enumerated joystick. hr = p_this->m_pDI->CreateDevice( pdidInstance->guidInstance, &pJoystick, NULL ); if( SUCCEEDED( hr ) ) { // 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( SUCCEEDED( hr = pJoystick->SetDataFormat( &c_dfDIJoystick2 ) ) ) { // Set the cooperative level to let DInput know how this device should // interact with the system and with other DInput applications. if( SUCCEEDED( hr = pJoystick->SetCooperativeLevel( g_hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND ) ) ) { DIDEVCAPS diDevCaps; diDevCaps.dwSize = sizeof(DIDEVCAPS); if (SUCCEEDED(hr = pJoystick->GetCapabilities(&diDevCaps))) { CLog::Log(LOGNOTICE, __FUNCTION__" : Enabled Joystick: %s", pdidInstance->tszProductName); CLog::Log(LOGNOTICE, __FUNCTION__" : Total Axis: %d Total Hats: %d Total Buttons: %d", diDevCaps.dwAxes, diDevCaps.dwPOVs, diDevCaps.dwButtons); p_this->m_pJoysticks.push_back(pJoystick); p_this->m_JoystickNames.push_back(pdidInstance->tszProductName); p_this->m_devCaps.push_back(diDevCaps); } else CLog::Log(LOGDEBUG, __FUNCTION__" : Failed to GetCapabilities for: %s", pdidInstance->tszProductName); } else CLog::Log(LOGDEBUG, __FUNCTION__" : Failed to SetCooperativeLevel on: %s", pdidInstance->tszProductName); } else CLog::Log(LOGDEBUG, __FUNCTION__" : Failed to SetDataFormat on: %s", pdidInstance->tszProductName); } else CLog::Log(LOGDEBUG, __FUNCTION__" : Failed to CreateDevice: %s", pdidInstance->tszProductName); return DIENUM_CONTINUE; }
// Callbacks from hooking on device BOOL JoystickHandler::DIEnumDevicesCallback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef) { if(nEntry < maxEntry) { Entry e = { 0 }; memcpy(&e.diDeviceInstance, lpddi, sizeof(e.diDeviceInstance)); e.diDevCaps.dwSize = sizeof(e.diDevCaps); LPDIRECTINPUTDEVICE8 did = 0; if(SUCCEEDED(di->CreateDevice(lpddi->guidInstance, (LPDIRECTINPUTDEVICE*) &did, 0))) { if(SUCCEEDED(did->SetDataFormat(lpdf))) { if(SUCCEEDED(did->GetCapabilities(&e.diDevCaps))) { e.diDevice = did; entry[nEntry++] = e; } } } } return DIENUM_CONTINUE; }
//----------------------------------------------------------------------------------------- // static int CALLBACK enum_joysticks_callback(const DIDEVICEINSTANCE* lpddi, VOID* lpContext) { // 列挙されたジョイスティックへのインターフェイスを取得する。 HRESULT hr; PImpl* joy = reinterpret_cast<PImpl*>(lpContext); LPDIRECTINPUTDEVICE8 joyDevice; hr = joy->joystick_->CreateDevice(lpddi->guidInstance, &joyDevice, NULL ); if( FAILED(hr)) { return DIENUM_CONTINUE; } //ジョイスティックの能力を調べる joy->joyCaps_.dwSize = sizeof(DIDEVCAPS); hr = joyDevice->GetCapabilities( &joy->joyCaps_); if (FAILED(hr)) {//デバイスの能力を取れなかった return DIENUM_CONTINUE; } // このデバイスを使用 joy->joystickDevice_ = DirectInputDevicePtr(joyDevice, false); return DIENUM_STOP; }
int main() { // Create a DirectInput device if (FAILED(hr = DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (VOID**)&di, NULL))) { return hr; } // Look for the first simple joystick we can find. if (FAILED(hr = di->EnumDevices(DI8DEVCLASS_GAMECTRL, enumCallback, NULL, DIEDFL_ATTACHEDONLY))) { return hr; } // Make sure we got a joystick if (joystick == NULL) { printf("Joystick not found.\n"); return E_FAIL; } if (FAILED(hr = joystick->SetDataFormat(&c_dfDIJoystick2))) { return hr; } if (FAILED(hr = joystick->SetCooperativeLevel(NULL, DISCL_EXCLUSIVE | DISCL_FOREGROUND))) { return hr; } capabilities.dwSize = sizeof(DIDEVCAPS); if (FAILED(hr = joystick->GetCapabilities(&capabilities))) { return hr; } system("pause"); return 0; }
BOOL CALLBACK QuaJoystickManager::EnumJoysticks(const DIDEVICEINSTANCE*pdidInstance) { if (directInput != nullptr) { LPDIRECTINPUTDEVICE8 joystick; HRESULT err = directInput->CreateDevice(pdidInstance->guidInstance, &joystick, nullptr); if (err == DI_OK) { DIDEVCAPS caps; caps.dwSize = sizeof(caps); err = joystick->GetCapabilities(&caps); if (err == DI_OK) { } else { fprintf(stderr, "DI Enum get cap error: %s\n", direct_error_string(err)); } joystick->Unacquire(); joystick->Release(); diJoystix.push_back(joy_cap(pdidInstance->guidInstance, nullptr, true)); } else { fprintf(stderr, "DI Enum error: %s\n", direct_error_string(err)); } } return DIENUM_CONTINUE; }
/*** IDirectInputDevice8W methods ***/ HRESULT _stdcall GetCapabilities(LPDIDEVCAPS a) { return RealDevice->GetCapabilities(a); }