bool PisteInput_Alusta_Mouse() { if (FAILED(PI_lpdi->CreateDevice(GUID_SysMouse, &PI_lpdimouse, NULL))) { PisteLog_Kirjoita("[Warning] Piste Input: No mouse available! \n"); PI_mouse_available = false; } if (PI_mouse_available) { if (FAILED(PI_lpdimouse->SetCooperativeLevel(PI_main_window_handle, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE))) { PisteLog_Kirjoita("[Error] Piste Input: Mouse - Set cooperative level failed! \n"); PI_mouse_available = false; } if (FAILED(PI_lpdimouse->SetDataFormat(&c_dfDIMouse))) { PisteLog_Kirjoita("[Error] Piste Input: Mouse - Set data format failed! \n"); PI_mouse_available = false; } if (FAILED(PI_lpdimouse->Acquire())) { PisteLog_Kirjoita("[Error] Piste Input: Mouse - Acquire failed! \n"); PI_mouse_available = false; } } return PI_mouse_available; }
bool DXGame::initDirectInput(HINSTANCE hInstance){ dprintf(("Entered initDirectInput\n")); if(FAILED(DirectInputCreate(hInstance, DIRECTINPUT_VERSION, &lpDirectInput, NULL))){ dprintf(("*****COULD NOT CREATE DIRECT INPUT OBJECT*****\n")); } if(FAILED(lpDirectInput->CreateDevice(GUID_SysKeyboard, &lpKeyboard, NULL))){ dprintf(("*****COULD NOT CREATE KEYBOARD*****\n")); } // set cooperation level if (lpKeyboard->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND)!=DI_OK){ dprintf(("COULD NOT SetCooperative Level\n")); return false; } // set data format if (lpKeyboard->SetDataFormat(&c_dfDIKeyboard)!=DI_OK){ dprintf(("COULD NOT Set Data Format\n")); return false; } // acquire the keyboard if (lpKeyboard->Acquire()!=DI_OK){ dprintf(("COULD NOT Acquire the keyboard\n")); return false; } dprintf(("Leaving the DirectInput\n")); return true; }
bool PisteInput_Alusta_Keyboard() { if (FAILED(PI_lpdi->CreateDevice(GUID_SysKeyboard, &PI_lpdikey, NULL))) { PisteLog_Kirjoita("[Error] Piste Input: Keyboard - Create Device failed! \n"); return false; } if (FAILED(PI_lpdikey->SetCooperativeLevel(PI_main_window_handle, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE /* | DISCL_FOREGROUND*/))) { PisteLog_Kirjoita("[Error] Piste Input: Keyboard - Set cooperative level failed! \n"); return false; } if (FAILED(PI_lpdikey->SetDataFormat(&c_dfDIKeyboard))) { PisteLog_Kirjoita("[Error] Piste Input: Keyboard - Set data format failed! \n"); return false; } if (FAILED(PI_lpdikey->Acquire())) { PisteLog_Kirjoita("[Error] Piste Input: Keyboard - Acquire failed! \n"); return false; } return true; }
BOOL InitialiseDirectMouse() { GUID guid = GUID_SysMouse; HRESULT hres; //MouseButton = 0; // Obtain an interface to the system mouse device. hres = lpdi->CreateDevice(guid, &lpdiMouse, NULL); if (hres != DI_OK) return FALSE; // Set the data format to "mouse format". hres = lpdiMouse->SetDataFormat(&c_dfDIMouse); if (hres != DI_OK) return FALSE; // Set the cooperativity level. #if debug // This level should allow the debugger to actually work // not to mention drop 'n' drag in sub-window mode hres = lpdiMouse->SetCooperativeLevel(hWndMain, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND); #else // this level is the most likely to work across // multiple mouse drivers hres = lpdiMouse->SetCooperativeLevel(hWndMain, DISCL_EXCLUSIVE | DISCL_FOREGROUND); #endif if (hres != DI_OK) return FALSE; // Set the buffer size for reading the mouse to // DMouse_BufferSize elements // mouse-type should be relative by default, so there // is no need to change axis mode. DIPROPDWORD dipdw = { { sizeof(DIPROPDWORD), // diph.dwSize sizeof(DIPROPHEADER), // diph.dwHeaderSize 0, // diph.dwObj DIPH_DEVICE, // diph.dwHow }, DMouse_BufferSize, // dwData }; hres = lpdiMouse->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph); if (hres != DI_OK) return FALSE; // try to acquire the mouse hres = lpdiMouse->Acquire(); return TRUE; }
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; }
bool MouseMG::Initialize(const LPDIRECTINPUT pDInput, const HWND hWnd) { hr = pDInput->CreateDevice(GUID_SysMouse , &pDInputMouse, NULL); if( FAILED(hr) ) return false; hr = pDInputMouse->SetDataFormat( &c_dfDIMouse ); if( FAILED(hr) ) return false; hr = pDInputMouse->SetCooperativeLevel(hWnd, DISCL_FOREGROUND|DISCL_NONEXCLUSIVE); if( FAILED(hr) ) return false; hr = pDInputMouse->Acquire(); if( FAILED(hr) ) return false; return true; }
bool InitKeyboard () { HRESULT hr; if (FAILED(hr = pdi->CreateDevice(GUID_SysKeyboard, &pdiKeyboard, NULL))) TRACE("!!! Failed to create keyboard device (%#08lx)\n", hr); else if (FAILED(hr = pdiKeyboard->SetCooperativeLevel(g_hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND))) TRACE("!!! Failed to set cooperative level of keyboard device (%#08lx)\n", hr); else if (FAILED(hr = pdiKeyboard->SetDataFormat(&c_dfDIKeyboard))) TRACE("!!! Failed to set data format of keyboard device (%#08lx)\n", hr); else { DIPROPDWORD dipdw = { { sizeof(DIPROPDWORD), sizeof(DIPROPHEADER), 0, DIPH_DEVICE, }, EVENT_BUFFER_SIZE, }; if (FAILED(hr = pdiKeyboard->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph))) TRACE("!!! Failed to set keyboard buffer size\n", hr); return true; } return false; }
//----------------------------------------------------------------------------- // Function: EnumJoysticksCallback // // Description: // Called once for each enumerated joystick. If we find one, // create a device interface on it so we can play with it. // //----------------------------------------------------------------------------- BOOL CALLBACK EnumJoysticksCallback( LPCDIDEVICEINSTANCE pInst, LPVOID lpvContext ) { HRESULT hr; LPDIRECTINPUTDEVICE pDevice; // obtain an interface to the enumerated force feedback joystick. hr = lpdi->CreateDevice( pInst->guidInstance, &pDevice, NULL ); // if it failed, then we can't use this joystick for some // bizarre reason. (Maybe the user unplugged it while we // were in the middle of enumerating it.) So continue enumerating if ( FAILED(hr) ) return DIENUM_CONTINUE; // we successfully created an IDirectInputDevice. So stop looking // for another one. g_pJoystick = pDevice; // query for IDirectInputDevice2 - we need this to poll the joystick pDevice->QueryInterface( IID_IDirectInputDevice2, (LPVOID *)&g_pJoystickDevice2 ); return DIENUM_STOP; }
BOOL __stdcall DIEnumDevicesProc(LPCDIDEVICEINSTANCE lpddi,LPVOID pvRef) { HRESULT hr; LPDIRECTINPUTDEVICE didTemp= NULL; DIPROPRANGE dipr; // MessageBox (AfxGetMainWnd()->m_hWnd,lpddi->tszInstanceName,"Found device",MB_OK); // if (JoystickCount>=16) return DIENUM_STOP; memcpy (JoystickGUID+JoystickCount,&(lpddi->guidInstance),sizeof(GUID)); strcpy (JoystickName[JoystickCount],lpddi->tszInstanceName); #if COMPILING_DX8 hr= di->CreateDevice(JoystickGUID[JoystickCount],did+JoystickCount,NULL); #else hr= di->CreateDevice(JoystickGUID[JoystickCount],&didTemp,NULL); if (hr!=DI_OK) { MessageBox (AfxGetMainWnd()->m_hWnd,"Cannot create joystick device","Error",MB_OK); goto Exit; } // Get a "Device 2" object required for polling hr= didTemp->QueryInterface(IID_IDirectInputDevice2,(LPVOID*)did+JoystickCount); if(FAILED(hr)) { MessageBox (AfxGetMainWnd()->m_hWnd,"Cannot create joystick device2","Error",MB_OK); goto Exit; } if (didTemp) { didTemp->Release(); didTemp= NULL; } #endif hr= did[JoystickCount]->SetCooperativeLevel(AfxGetMainWnd()->m_hWnd,DISCL_BACKGROUND|DISCL_NONEXCLUSIVE); if (FAILED(hr)) { MessageBox (AfxGetMainWnd()->m_hWnd,"Cannot set Cooperation level...","Error",MB_OK); goto Exit; } hr= did[JoystickCount]->SetDataFormat(&c_dfDIJoystick); if (FAILED(hr)) { MessageBox (AfxGetMainWnd()->m_hWnd,"Cannot set joystick data format...","Error",MB_OK); goto Exit; } // Set the axis ranges for the device dipr.diph.dwSize = sizeof(DIPROPRANGE); dipr.diph.dwHeaderSize = sizeof(dipr.diph); // dipr.diph.dwHow = DIPH_BYOFFSET; // dipr.diph.dwObj = DIJOFS_X; dipr.diph.dwHow = DIPH_DEVICE; dipr.diph.dwObj = 0; dipr.lMin = PPJOY_AXIS_MIN; // negative to the left/top dipr.lMax = PPJOY_AXIS_MAX; // positive to the right/bottom hr= did[JoystickCount]->SetProperty(DIPROP_RANGE,&dipr.diph); if (FAILED(hr)) { MessageBox (AfxGetMainWnd()->m_hWnd,"Cannot set joystick axis ranges...","Error",MB_OK); goto Exit; } hr= did[JoystickCount]->Acquire(); if (FAILED(hr)) { MessageBox (AfxGetMainWnd()->m_hWnd,"Cannot acquire joystick...","Error",MB_OK); goto Exit; } JoystickCount++; return DIENUM_CONTINUE; Exit: if (didTemp) didTemp->Release(); didTemp= NULL; if (did[JoystickCount]) did[JoystickCount]->Release(); did[JoystickCount]= NULL; return DIENUM_CONTINUE; }
BOOL InitialiseDirectKeyboard() { HRESULT hRes; // try to create keyboard device if (lpdi->CreateDevice(guid, &lpdiKeyboard, NULL) !=DI_OK) { #if debug ReleaseDirect3D(); exit(0x4112); #else return FALSE; #endif } // Tell DirectInput that we want to receive data in keyboard format if (lpdiKeyboard->SetDataFormat(&c_dfDIKeyboard) != DI_OK) { #if debug ReleaseDirect3D(); exit(0x4113); #else return FALSE; #endif } // set cooperative level // this level is the most likely to work across // multiple hardware targets // (i.e. this is probably best for a production // release) #if UseForegroundKeyboard if (lpdiKeyboard->SetCooperativeLevel(hWndMain, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND) != DI_OK) #else // this level makes alt-tabbing multiple instances in // SunWindow mode possible without receiving lots // of false inputs if (lpdiKeyboard->SetCooperativeLevel(hWndMain, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND) != DI_OK) #endif { #if debug ReleaseDirect3D(); exit(0x4114); #else return FALSE; #endif } // try to acquire the keyboard hRes = lpdiKeyboard->Acquire(); if (hRes == DI_OK) { // keyboard was acquired DIKeyboardOkay = TRUE; } else { // keyboard was NOT acquired DIKeyboardOkay = FALSE; } // if we get here, all objects were created successfully return TRUE; }
//=========================================================================== // DIUtilCreateDevice2FromJoyConfig // // Helper function to create a DirectInputDevice2 object from a // DirectInputJoyConfig object. // // Parameters: // short nJoystickId - joystick id for creation // HWND hWnd - window handle // LPDIRECTINPUT pdi - ptr to base DInput object // LPDIRECTINPUTJOYCONFIG pdiJoyCfg - ptr to joyconfig object // LPDIRECTINPUTDEVICE *ppdiDevice2 - ptr to device object ptr // // Returns: HRESULT // //=========================================================================== HRESULT DIUtilCreateDevice2FromJoyConfig(short nJoystickId, HWND hWnd, LPDIRECTINPUT pdi, LPDIRECTINPUTJOYCONFIG pdiJoyCfg, LPDIRECTINPUTDEVICE2 *ppdiDevice2) { HRESULT hRes = E_NOTIMPL; LPDIRECTINPUTDEVICE pdiDevTemp = NULL; DIJOYCONFIG dijc; // validate pointers if( (IsBadReadPtr((void*)pdi, sizeof(IDirectInput))) || (IsBadWritePtr((void*)pdi, sizeof(IDirectInput))) ) { ERROUT(TEXT("DIUtilCreateDeviceFromJoyConfig() - invalid pdi\n")); return E_POINTER; } if( (IsBadReadPtr((void*)pdiJoyCfg, sizeof(IDirectInputJoyConfig))) || (IsBadWritePtr((void*)pdiJoyCfg, sizeof(IDirectInputJoyConfig))) ) { ERROUT(TEXT("DIUtilCreateDeviceFromJoyConfig() - invalid pdiJoyCfg\n")); return E_POINTER; } if( (IsBadReadPtr((void*)ppdiDevice2, sizeof(LPDIRECTINPUTDEVICE2))) || (IsBadWritePtr((void*)ppdiDevice2, sizeof(LPDIRECTINPUTDEVICE2))) ) { ERROUT(TEXT("DIUtilCreateDeviceFromJoyConfig() - invalid ppdiDevice2\n")); return E_POINTER; } // get the instance GUID for device configured as nJoystickId // // GetConfig will provide this information dijc.dwSize = sizeof(DIJOYCONFIG); hRes = pdiJoyCfg->GetConfig(nJoystickId, &dijc, DIJC_GUIDINSTANCE); if(FAILED(hRes)) { ERROUT(TEXT("DIUtilCreateDeviceFromJoyConfig() - GetConfig() failed\n")); return hRes; } // create temporary device object // // use the instance GUID returned by GetConfig() hRes = pdi->CreateDevice(dijc.guidInstance, &pdiDevTemp, NULL); if(FAILED(hRes)) { ERROUT(TEXT("DIUtilCreateDeviceFromJoyConfig() - CreateDevice() failed\n")); return hRes; } // query for a device2 object hRes = pdiDevTemp->QueryInterface(IID_IDirectInputDevice2, (LPVOID*)ppdiDevice2); // release the temporary object pdiDevTemp->Release(); if(FAILED(hRes)) { ERROUT(TEXT("DIUtilCreateDeviceFromJoyConfig() - QueryInterface(IDirectInputDevice2) failed\n")); return hRes; } // set the desired data format // // we want to be a joystick hRes = (*ppdiDevice2)->SetDataFormat(&c_dfDIJoystick); if(FAILED(hRes)) { ERROUT(TEXT("DIUtilCreateDeviceFromJoyConfig() - SetDataFormat(Joystick) failed\n")); return hRes; } // set the cooperative level for the device // // want to set EXCLUSIVE | BACKGROUND hRes = (*ppdiDevice2)->SetCooperativeLevel(hWnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND); if(FAILED(hRes)) { ERROUT(TEXT("DIUtilCreateDeviceFromJoyConfig() - SetCooperativeLevel() failed\n")); return hRes; } // all is well return hRes; } //*** end DIUtilCreateDevice2FromJoyConfig()
/** * 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; }
Joystick_DX5::Joystick_DX5(LPDIRECTINPUT dii, DIDEVICEINSTANCE *ddi) : dev(NULL), have_exclusive_access(-1) { LPDIRECTINPUTDEVICE tmp_dev = NULL; try { REQUIRE_DI_CALL( dii->CreateDevice(ddi->guidInstance, &tmp_dev, NULL) ); REQUIRE_DI_CALL( tmp_dev->QueryInterface(IID_IDirectInputDevice2, (LPVOID *)&dev) ); REQUIRE_DI_CALL( dev->SetDataFormat(&c_dfDIJoystick2) ); DevCaps.dwSize = sizeof(DevCaps); REQUIRE_DI_CALL( dev->GetCapabilities(&DevCaps) ); for(unsigned rax = 0; rax < 8; rax++) { DIPROPRANGE diprg; HRESULT hres; diprg.diph.dwSize = sizeof(diprg); diprg.diph.dwHeaderSize = sizeof(diprg.diph); diprg.diph.dwObj = rax * sizeof(LONG); diprg.diph.dwHow = DIPH_BYOFFSET; // TODO: Handle DIPROPRANGE_NOMIN and DIPROPRANGE_NOMAX hres = dev->GetProperty(DIPROP_RANGE, &diprg.diph); if(hres == DI_OK) { if(diprg.lMin < diprg.lMax) { di_axis_info dai; dai.jd_logical_offset = rax; dai.minimum = diprg.lMin; dai.maximum = diprg.lMax; DIAxisInfo.push_back(dai); } } //else if(hres != DIERR_OBJECTNOTFOUND) } num_rel_axes = 0; num_axes = DIAxisInfo.size() + DevCaps.dwPOVs * 2; num_buttons = DevCaps.dwButtons; axis_state.resize(num_axes); rel_axis_state.resize(num_rel_axes); button_state.resize(num_buttons); // id, guidinstance, etc. // // // #if 0 { DIEFFECTINFO eff_inf; eff_inf.dwSize = sizeof(eff_inf); if(dev->GetEffectInfo(&eff_inf, GUID_Square) == DI_OK || dev->GetEffectInfo(&eff_inf, GUID_Sine) == DI_OK) { } } #endif RequestExclusive(false); CalcOldStyleID(DIAxisInfo.size(), 0, DevCaps.dwPOVs, DevCaps.dwButtons); snprintf(name, sizeof(name), "%s", ddi->tszProductName); } catch(...) { if(tmp_dev != NULL) tmp_dev->Release(); if(dev != NULL) dev->Release(); throw; } }
int di_init() { HRESULT hr; return 0; /* * Register with the DirectInput subsystem and get a pointer * to a IDirectInput interface we can use. * * Parameters: * * g_hinst * * Instance handle to our application or DLL. * * DIRECTINPUT_VERSION * * The version of DirectInput we were designed for. * We take the value from the <dinput.h> header file. * * &g_pdi * * Receives pointer to the IDirectInput interface * that was created. * * NULL * * We do not use OLE aggregation, so this parameter * must be NULL. * */ hr = DirectInputCreate(GetModuleHandle(NULL), 0x300, &Di_object, NULL); if (FAILED(hr)) { mprintf(( "DirectInputCreate failed!\n" )); return FALSE; } /* * Obtain an interface to the system keyboard device. * * Parameters: * * GUID_SysKeyboard * * The instance GUID for the device we wish to access. * GUID_SysKeyboard is a predefined instance GUID that * always refers to the system keyboard device. * * &g_pKeyboard * * Receives pointer to the IDirectInputDevice interface * that was created. * * NULL * * We do not use OLE aggregation, so this parameter * must be NULL. * */ hr = Di_object->CreateDevice(GUID_SysKeyboard, &Di_keyboard, NULL); if (FAILED(hr)) { mprintf(( "CreateDevice failed!\n" )); return FALSE; } /* * Set the data format to "keyboard format". * * A data format specifies which controls on a device we * are interested in, and how they should be reported. * * This tells DirectInput that we will be passing an array * of 256 bytes to IDirectInputDevice::GetDeviceState. * * Parameters: * * c_dfDIKeyboard * * Predefined data format which describes * an array of 256 bytes, one per scancode. */ hr = Di_keyboard->SetDataFormat(&c_dfDIKeyboard); if (FAILED(hr)) { mprintf(( "SetDataFormat failed!\n" )); return FALSE; } /* * Set the cooperativity level to let DirectInput know how * this device should interact with the system and with other * DirectInput applications. * * Parameters: * * DISCL_NONEXCLUSIVE * * Retrieve keyboard data when acquired, not interfering * with any other applications which are reading keyboard * data. * * DISCL_FOREGROUND * * If the user switches away from our application, * automatically release the keyboard back to the system. * */ hr = Di_keyboard->SetCooperativeLevel((HWND)os_get_window(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND); if (FAILED(hr)) { mprintf(( "SetCooperativeLevel failed!\n" )); return FALSE; } DIPROPDWORD hdr; // Turn on buffering hdr.diph.dwSize = sizeof(DIPROPDWORD); hdr.diph.dwHeaderSize = sizeof(DIPROPHEADER); hdr.diph.dwObj = 0; hdr.diph.dwHow = DIPH_DEVICE; // Apply to entire device hdr.dwData = 16; //MAX_BUFFERED_KEYBOARD_EVENTS; hr = Di_keyboard->SetProperty( DIPROP_BUFFERSIZE, &hdr.diph ); if (FAILED(hr)) { mprintf(( "SetProperty DIPROP_BUFFERSIZE failed\n" )); return FALSE; } Di_event = CreateEvent( NULL, FALSE, FALSE, NULL ); Assert(Di_event != NULL); Di_thread = CreateThread(NULL, 1024, (LPTHREAD_START_ROUTINE)di_process, NULL, 0, &Di_thread_id); Assert( Di_thread != NULL ); SetThreadPriority(Di_thread, THREAD_PRIORITY_HIGHEST); hr = Di_keyboard->SetEventNotification(Di_event); if (FAILED(hr)) { mprintf(( "SetEventNotification failed\n" )); return FALSE; } Di_keyboard->Acquire(); return TRUE; }
/** * input_dinput_init(): Initialize the DirectInput subsystem. * @return 0 on success; non-zero on error. */ int input_dinput_init(void) { int i; HRESULT rval; // Attempt to initialize DirectInput 5. input_dinput_version = 0; rval = DirectInputCreate(ghInstance, DIRECTINPUT_VERSION_5, &lpDI, NULL); if (rval == DI_OK) { // DirectInput 5 initialized. LOG_MSG(input, LOG_MSG_LEVEL_INFO, "Initialized DirectInput 5."); input_dinput_version = DIRECTINPUT_VERSION_5; } else { // Attempt to initialize DirectInput 3. rval = DirectInputCreate(ghInstance, DIRECTINPUT_VERSION_3, &lpDI, NULL); if (rval == DI_OK) { // DirectInput 3 initialized. LOG_MSG(input, LOG_MSG_LEVEL_INFO, "Initialized DirectInput 3."); input_dinput_version = DIRECTINPUT_VERSION_3; } else { // DirectInput could not be initialized. LOG_MSG(input, LOG_MSG_LEVEL_CRITICAL, "Could not initialize DirectInput 3 or DirectInput 5.\n\nYou must have DirectX 3 or later."); return -1; } } input_dinput_joystick_initialized = false; input_dinput_joystick_error = false; input_dinput_num_joysticks = 0; memset(input_dinput_joy_id, 0x00, sizeof(input_dinput_joy_id)); //rval = lpDI->CreateDevice(GUID_SysMouse, &lpDIDMouse, NULL); lpDIDMouse = NULL; rval = lpDI->CreateDevice(GUID_SysKeyboard, &lpDIDKeyboard, NULL); if (rval != DI_OK) { LOG_MSG(input, LOG_MSG_LEVEL_CRITICAL, "lpDI->CreateDevice() failed. (Keyboard)"); // TODO: Use cross-platform error numbers, not just DirectInput return values. return -2; } // Set the cooperative level. input_dinput_set_cooperative_level(NULL); //rval = lpDIDMouse->SetDataFormat(&c_dfDIMouse); rval = lpDIDKeyboard->SetDataFormat(&c_dfDIKeyboard); if (rval != DI_OK) { LOG_MSG(input, LOG_MSG_LEVEL_CRITICAL, "lpDIDKeyboard->SetDataFormat() failed."); // TODO: Use cross-platform error numbers, not just DirectInput return values. return -1; } //rval = lpDIDMouse->Acquire(); for(i = 0; i < 10; i++) { rval = lpDIDKeyboard->Acquire(); if (rval == DI_OK) break; GensUI::sleep(10); } // Clear the DirectInput arrays. memset(input_dinput_keys, 0x00, sizeof(input_dinput_keys)); memset(input_dinput_joy_id, 0x00, sizeof(input_dinput_joy_id)); memset(input_dinput_joy_state, 0x00, sizeof(input_dinput_joy_state)); // DirectInput initialized. return 0; }
bool PisteInput_Alusta_Ohjain(int index) { LPDIRECTINPUTDEVICE temp; if (FAILED(PI_lpdi->CreateDevice(PI_joysticks[index].joystickGUID, &temp, NULL))) { PisteLog_Kirjoita("[Error] Piste Input: Gamepad - Create device failed! \n"); return false; } if (FAILED(temp->QueryInterface(IID_IDirectInputDevice2,(void**) &PI_joysticks[index].lpdijoy))) { PisteLog_Kirjoita("[Error] Piste Input: Gamepad - Create device failed! \n"); return false; } if (FAILED(temp->Release())) { PisteLog_Kirjoita("[Error] Piste Input: Gamepad - Releasing DirectInputDevice 1 failed! \n"); return false; } if (FAILED(PI_joysticks[index].lpdijoy->SetCooperativeLevel(PI_main_window_handle, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE ))) { PisteLog_Kirjoita("[Error] Piste Input: Gamepad - Releasing DirectInputDevice 1 failed! \n"); return false; } if (FAILED(PI_joysticks[index].lpdijoy->SetDataFormat(&c_dfDIJoystick))) { PisteLog_Kirjoita("[Error] Piste Input: Gamepad - Set dataformat failed! \n"); return false; } DIPROPRANGE joy_axis_range; //Määritellään x-akseli joy_axis_range.lMin = -PI_OHJAIN_XY; joy_axis_range.lMax = PI_OHJAIN_XY; joy_axis_range.diph.dwSize = sizeof(DIPROPRANGE); joy_axis_range.diph.dwHeaderSize = sizeof(DIPROPHEADER); joy_axis_range.diph.dwObj = DIJOFS_X; joy_axis_range.diph.dwHow = DIPH_BYOFFSET; PI_joysticks[index].lpdijoy->SetProperty(DIPROP_RANGE, &joy_axis_range.diph); //Määritellään y-akseli joy_axis_range.lMin = -PI_OHJAIN_XY; joy_axis_range.lMax = PI_OHJAIN_XY; joy_axis_range.diph.dwSize = sizeof(DIPROPRANGE); joy_axis_range.diph.dwHeaderSize = sizeof(DIPROPHEADER); joy_axis_range.diph.dwObj = DIJOFS_Y; joy_axis_range.diph.dwHow = DIPH_BYOFFSET; PI_joysticks[index].lpdijoy->SetProperty(DIPROP_RANGE, &joy_axis_range.diph); DIPROPDWORD dead_angle; dead_angle.diph.dwSize = sizeof(dead_angle); dead_angle.diph.dwHeaderSize = sizeof(dead_angle.diph); dead_angle.diph.dwObj = DIJOFS_X; dead_angle.diph.dwHow = DIPH_BYOFFSET; dead_angle.dwData = 5000; //= 10% PI_joysticks[index].lpdijoy->SetProperty(DIPROP_DEADZONE, &dead_angle.diph); dead_angle.diph.dwSize = sizeof(dead_angle); dead_angle.diph.dwHeaderSize = sizeof(dead_angle.diph); dead_angle.diph.dwObj = DIJOFS_Y; dead_angle.diph.dwHow = DIPH_BYOFFSET; dead_angle.dwData = 5000; //= 10% PI_joysticks[index].lpdijoy->SetProperty(DIPROP_DEADZONE, &dead_angle.diph); if (FAILED(PI_joysticks[index].lpdijoy->Acquire())) { PisteLog_Kirjoita("[Error] Piste Input: Gamepad - Acquiring device failed! \n"); return false; } return true; }
int di_init() { HRESULT hr; if (Mouse_mode == MOUSE_MODE_WIN){ return 0; } Di_mouse_inited = 0; hr = DirectInputCreate(GetModuleHandle(NULL), DIRECTINPUT_VERSION, &Di_mouse_obj, NULL); if (FAILED(hr)) { hr = DirectInputCreate(GetModuleHandle(NULL), 0x300, &Di_mouse_obj, NULL); if (FAILED(hr)) { mprintf(( "DirectInputCreate() failed!\n" )); return FALSE; } } hr = Di_mouse_obj->CreateDevice(GUID_SysMouse, &Di_mouse, NULL); if (FAILED(hr)) { mprintf(( "CreateDevice() failed!\n" )); return FALSE; } hr = Di_mouse->SetDataFormat(&c_dfDIMouse); if (FAILED(hr)) { mprintf(( "SetDataFormat() failed!\n" )); return FALSE; } hr = Di_mouse->SetCooperativeLevel((HWND)os_get_window(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND); if (FAILED(hr)) { mprintf(( "SetCooperativeLevel() failed!\n" )); return FALSE; } /* DIPROPDWORD hdr; // Turn on buffering hdr.diph.dwSize = sizeof(DIPROPDWORD); hdr.diph.dwHeaderSize = sizeof(DIPROPHEADER); hdr.diph.dwObj = 0; hdr.diph.dwHow = DIPH_DEVICE; // Apply to entire device hdr.dwData = 16; //MAX_BUFFERED_KEYBOARD_EVENTS; hr = Di_mouse->SetProperty( DIPROP_BUFFERSIZE, &hdr.diph ); if (FAILED(hr)) { mprintf(( "SetProperty DIPROP_BUFFERSIZE failed\n" )); return FALSE; } Di_event = CreateEvent( NULL, FALSE, FALSE, NULL ); Assert(Di_event != NULL); hr = Di_mouse->SetEventNotification(Di_event); if (FAILED(hr)) { mprintf(( "SetEventNotification failed\n" )); return FALSE; } */ Di_mouse->Acquire(); Di_mouse_inited = 1; return TRUE; }