int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pCmdLine, int nShowCmd) { HRESULT result = CoInitialize(0); LPDIRECTINPUT directinput; if (FAILED(result)) { std::cerr << "CL_DisplayWindow_Win32: Damn murphy must hate you. CoInitialize failed!" << std::endl; } result = CoCreateInstance(CLSID_DirectInput, 0, CLSCTX_INPROC_SERVER, IID_IDirectInput, (LPVOID *) &directinput); if (FAILED(result)) { std::cerr << "FAILURE" << std::endl; } else { std::cerr << "SUCCESS" << std::endl; result = directinput->Initialize(GetModuleHandle(0), DIRECTINPUT_VERSION); } if (!InitWindowsApp(hInstance, nShowCmd)) { ::MessageBox(0, "Init - Failed", "Error", MB_OK); return 0; } return Run(); }
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; }
void di_cleanup() { /* * Destroy any lingering IDirectInputDevice object. */ if (Di_keyboard) { /* * Cleanliness is next to godliness. Unacquire the device * one last time just in case we got really confused and tried * to exit while the device is still acquired. */ Di_keyboard->Unacquire(); Di_keyboard->Release(); Di_keyboard = NULL; } /* * Destroy any lingering IDirectInput object. */ if (Di_object) { Di_object->Release(); Di_object = NULL; } if ( Di_event ) { CloseHandle(Di_event); Di_event = NULL; } }
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; }
int DirectInput_Init(void) { HRESULT hr; if(dInput || dInput3) return true; // Create the DirectInput interface instance. Try version 8 first. if(FAILED(hr = CoCreateInstance(CLSID_DirectInput8, NULL, CLSCTX_INPROC_SERVER, IID_IDirectInput8, (LPVOID*)&dInput)) || FAILED(hr = dInput->Initialize(app.hInstance, DIRECTINPUT_VERSION))) { Con_Message("DirectInput 8 init failed (0x%x).", hr); // Try the older version 3 interface instead. // I'm not sure if this works correctly. if(FAILED(hr = CoCreateInstance(CLSID_DirectInput, NULL, CLSCTX_INPROC_SERVER, IID_IDirectInput2W, (LPVOID*)&dInput3)) || FAILED(hr = dInput3->Initialize(app.hInstance, 0x0300))) { Con_Message("Failed to create DirectInput 3 object (0x%x).", hr); return false; } Con_Message("Using DirectInput 3."); } if(!dInput && !dInput3) { Con_Message(" DirectInput init failed."); return false; } return true; }
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; }
void ReleaseDirectInput(void) { if (lpdi!= NULL) { lpdi->Release(); lpdi = NULL; } }
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; }
int HookDirectInput(HMODULE module, int version) { HINSTANCE hinst; void *tmp; LPDIRECTINPUT lpdi; const GUID di7 = {0x9A4CB684,0x236D,0x11D3,0x8E,0x9D,0x00,0xC0,0x4F,0x68,0x44,0xAE}; const GUID di8 = {0xBF798030,0x483A,0x4DA2,0xAA,0x99,0x5D,0x64,0xED,0x36,0x97,0x00}; tmp = HookAPI(module, "dinput.dll", NULL, "DirectInputCreateA", extDirectInputCreate); if(tmp) pDirectInputCreate = (DirectInputCreate_Type)tmp; tmp = HookAPI(module, "dinput.dll", NULL, "DirectInputCreateW", extDirectInputCreate); if(tmp) pDirectInputCreate = (DirectInputCreate_Type)tmp; tmp = HookAPI(module, "dinput.dll", NULL, "DirectInputCreateEx", extDirectInputCreateEx); if(tmp) pDirectInputCreateEx = (DirectInputCreateEx_Type)tmp; tmp = HookAPI(module, "dinput8.dll", NULL, "DirectInput8Create", extDirectInput8Create); if(tmp) pDirectInputCreateEx = (DirectInputCreateEx_Type)tmp; if(!pDirectInputCreate && !pDirectInputCreateEx){ if(version < 8){ hinst = LoadLibrary("dinput.dll"); pDirectInputCreate = (DirectInputCreate_Type)GetProcAddress(hinst, "DirectInputCreateA"); if(pDirectInputCreate) if(!extDirectInputCreate(GetModuleHandle(0), DIRECTINPUT_VERSION, &lpdi, 0)) lpdi->Release(); pDirectInputCreateEx = (DirectInputCreateEx_Type)GetProcAddress(hinst, "DirectInputCreateEx"); if(pDirectInputCreateEx) if(!extDirectInputCreateEx(GetModuleHandle(0), DIRECTINPUT_VERSION, di7, (void **)&lpdi, 0)) lpdi->Release(); } else{ hinst = LoadLibrary("dinput8.dll"); pDirectInputCreateEx = (DirectInputCreateEx_Type)GetProcAddress(hinst, "DirectInput8Create"); if(pDirectInputCreateEx) if(!extDirectInputCreateEx(GetModuleHandle(0), DIRECTINPUT_VERSION, di8, (void **)&lpdi, 0)) lpdi->Release(); } } if(pDirectInputCreate || pDirectInputCreateEx) return 1; return 0; }
int CPPJoyJoyDlg::ScanJoystickDevices (void) { int JoyCount; int Count; int Index; for (JoyCount=0;JoyCount<(sizeof(did)/sizeof(*did));JoyCount++) { if (did[JoyCount]) { // do we want to unacquire here too?? did[JoyCount]->Release(); } did[JoyCount]= NULL; } memset (JoystickName,0,sizeof(JoystickName)); memset (JoystickGUID,0,sizeof(JoystickGUID)); JoystickCount= 0; for (Count=0;Count<8;Count++) { m_AxisJoy[Count]->ResetContent(); Index= m_AxisJoy[Count]->AddString("<clear>"); m_AxisJoy[Count]->SetItemData(Index,-1); } for (Count=0;Count<16;Count++) { m_ButJoy[Count]->ResetContent(); Index= m_ButJoy[Count]->AddString("<clear>"); m_ButJoy[Count]->SetItemData(Index,-1); } if (di->EnumDevices(DIDEVTYPE_JOYSTICK,DIEnumDevicesProc,NULL,DIEDFL_ALLDEVICES)!=DI_OK) return 0; for (JoyCount=0;JoyCount<JoystickCount;JoyCount++) { for (Count=0;Count<8;Count++) { Index= m_AxisJoy[Count]->AddString(JoystickName[JoyCount]); m_AxisJoy[Count]->SetItemData(Index,JoyCount); } for (Count=0;Count<16;Count++) { Index= m_ButJoy[Count]->AddString(JoystickName[JoyCount]); m_ButJoy[Count]->SetItemData(Index,JoyCount); } } return 1; }
void Input::Exit (bool fReInit_/*=false*/) { TRACE("Input::Exit(%d)\n", fReInit_); if (pdiKeyboard) { pdiKeyboard->Unacquire(); pdiKeyboard->Release(); pdiKeyboard = NULL; } if (pdidJoystick1) { pdidJoystick1->Unacquire(); pdidJoystick1->Release(); pdidJoystick1 = NULL; } if (pdidJoystick2) { pdidJoystick2->Unacquire(); pdidJoystick2->Release(); pdidJoystick2 = NULL; } if (pdi) { pdi->Release(); pdi = NULL; } Keyboard::Exit(fReInit_); }
// Hook on joystick void JoystickHandler::enumerate(LPDIRECTINPUT di, DWORD dwDevType = DI8DEVTYPE_JOYSTICK, LPCDIDATAFORMAT lpdf = &c_dfDIJoystick2, DWORD dwFlags = DIEDFL_ATTACHEDONLY, int maxEntry = 16) { clear(); entry = new Entry [maxEntry]; this->di = di; this->maxEntry = maxEntry; nEntry = 0; this->lpdf = lpdf; // Callbacks to DIEnumDevicesCallback_static di->EnumDevices(dwDevType, DIEnumDevicesCallback_static, this, dwFlags); this->di = 0; }
/** * input_dinput_add_joysticks_to_listbox(): Enumerate joysticks and add them to a listbox. * @param lstBox Listbox to add the joystick information to. */ void input_dinput_add_joysticks_to_listbox(HWND lstBox) { if (!lpDI || input_dinput_joystick_error) return; // Initialize the "Add Joysticks" counter. input_dinput_add_joysticks_count = 0; // Enumerate the joysticks to get their names. // TODO: Somehow add this to the first EnumDevices(). lpDI->EnumDevices(DIDEVTYPE_JOYSTICK, &input_dinput_callback_add_joysticks_to_listbox, lstBox, DIEDFL_ATTACHEDONLY); }
bool InitJoysticks () { HRESULT hr; // Enumerate the joystick devices if (FAILED(hr = pdi->EnumDevices(DIDEVTYPE_JOYSTICK, EnumJoystickProc, NULL, DIEDFL_ATTACHEDONLY))) TRACE("!!! Failed to enumerate joystick devices (%#08lx)\n", hr); // Initialise matched joysticks if (pdidJoystick1) InitJoystick(pdidJoystick1); if (pdidJoystick2) InitJoystick(pdidJoystick2); return true; }
void DXGame::release(){ if(lpDirectDrawObject != NULL){ //if DD object exists if(lpPrimary != NULL) //if primary surface exists lpPrimary->Release(); //release primary surface lpDirectDrawObject->Release(); //release DD object } if(lpDirectInput != NULL){ if(lpKeyboard){ lpKeyboard->Unacquire(); lpKeyboard->Release(); } lpDirectInput->Release(); } }
void InitJoysticks(void) { HRESULT hr; GotJoystick = No; g_pJoystick = NULL; hr = lpdi->EnumDevices(DIDEVTYPE_JOYSTICK, EnumJoysticksCallback, NULL, DIEDFL_ATTACHEDONLY); if ( FAILED(hr) ) return; hr = g_pJoystick->SetDataFormat( &c_dfDIJoystick ); if ( FAILED(hr) ) return; hr = g_pJoystick->SetCooperativeLevel( hWndMain, DISCL_EXCLUSIVE | DISCL_FOREGROUND); if ( FAILED(hr) ) return; GotJoystick = Yes; }
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; }
void di_cleanup() { // Destroy any lingering IDirectInputDevice object. if (Di_mouse) { // Unacquire the device one last time just in case we got really confused // and tried to exit while the device is still acquired. Di_mouse->Unacquire(); Di_mouse->Release(); Di_mouse = NULL; } // Destroy any lingering IDirectInput object. if (Di_mouse_obj) { Di_mouse_obj->Release(); Di_mouse_obj = NULL; } Di_mouse_inited = 0; }
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; }
/** * input_dinput_end(): Shut down the DirectInput subsystem. * @return 0 on success; non-zero on error. */ int input_dinput_end(void) { // If any joysticks were opened, close them. if (!lpDI) return 0; if (lpDIDMouse) { lpDIDMouse->Release(); lpDIDMouse = NULL; } if (lpDIDKeyboard) { lpDIDKeyboard->Release(); lpDIDKeyboard = NULL; } for (int i = 0; i < MAX_JOYS; i++) { if (input_dinput_joy_id[i]) { input_dinput_joy_id[i]->Unacquire(); input_dinput_joy_id[i]->Release(); input_dinput_joy_id[i] = NULL; } } input_dinput_num_joysticks = 0; lpDI->Release(); lpDI = NULL; // Clear the DirectInput version variable. input_dinput_version = 0; // DirectInput shut down. return 0; }
int PisteInput_Lopeta() { if (!PI_unload) { for (int i=0;i<PI_MAX_PELIOHJAIMIA;i++) if (PI_joysticks[i].lpdijoy) { PI_joysticks[i].lpdijoy->Unacquire(); PI_joysticks[i].lpdijoy->Release(); PI_joysticks[i].lpdijoy = NULL; } if (PI_lpdimouse) { PI_lpdimouse->Unacquire(); PI_lpdimouse->Release(); PI_lpdimouse = NULL; } if (PI_lpdikey) { PI_lpdikey->Unacquire(); PI_lpdikey->Release(); PI_lpdikey = NULL; } if (PI_lpdi) { PI_lpdi->Release(); PI_lpdi = NULL; } PI_unload = true; } return 0; }
//----------------------------------------------------------------------------- // 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; }
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; } }
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; }
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; }
bool PisteInput_Alusta_Ohjaimet() { //LPDIRECTINPUTDEVICE temp; if (FAILED(PI_lpdi->EnumDevices(DIDEVTYPE_JOYSTICK, PisteInput_Enum_Ohjaimet, /*&PI_joystickGUID*/NULL, DIEDFL_ATTACHEDONLY))) { PisteLog_Kirjoita("[Error] Piste Input: Gamepads - Enumerating failed! \n"); return false; } for (int i=0; i < PI_MAX_PELIOHJAIMIA; i++) { if (PI_joysticks[i].nimi != NULL) { if (PisteInput_Alusta_Ohjain(i)) { PI_joysticks[i].available = true; } else { PI_joysticks[i].available = false; } } else PI_joysticks[i].available = false; } /* if (FAILED(PI_lpdi->CreateDevice(PI_joystickGUID, &temp, NULL))) return false; if (FAILED(temp->QueryInterface(IID_IDirectInputDevice2,(void**) &PI_lpdijoy))) return false; if (FAILED(temp->Release())) return false; if (FAILED(PI_lpdijoy->SetCooperativeLevel(PI_main_window_handle, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE ))) return false; if (FAILED(PI_lpdijoy->SetDataFormat(&c_dfDIJoystick))) 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_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_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_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_lpdijoy->SetProperty(DIPROP_DEADZONE, &dead_angle.diph); if (FAILED(PI_lpdijoy->Acquire())) return false; */ 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()
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; }
// Fill the supplied combo-box with the list of connected joysticks void Input::FillJoystickCombo (HWND hwndCombo_) { HRESULT hr; if (FAILED(hr = pdi->EnumDevices(DIDEVTYPE_JOYSTICK, EnumJoystickProc, reinterpret_cast<LPVOID>(hwndCombo_), DIEDFL_ATTACHEDONLY))) TRACE("!!! Failed to enumerate joystick devices (%#08lx)\n", hr); }