Debian Codegraph @

Source/Core/InputCommon/ControllerInterface/DInput/DInput.cpp

raw symbols SCIP
1 // Copyright 2010 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "InputCommon/ControllerInterface/DInput/DInput.h"
5
6 #include "Common/HRWrap.h"
7 #include "Common/Logging/Log.h"
8 #include "Common/StringUtil.h"
9
10 #include "InputCommon/ControllerInterface/ControllerInterface.h"
11 #include "InputCommon/ControllerInterface/DInput/DInputJoystick.h"
12 #include "InputCommon/ControllerInterface/DInput/DInputKeyboardMouse.h"
13
14 #pragma comment(lib, "Dinput8.lib")
15 #pragma comment(lib, "dxguid.lib")
16
17 namespace ciface::DInput
18 {
19 static IDirectInput8* s_idi8 = nullptr;
20
21 BOOL CALLBACK DIEnumDeviceObjectsCallback(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef)
22 {
23 ((std::list<DIDEVICEOBJECTINSTANCE>*)pvRef)->push_back(*lpddoi);
24 return DIENUM_CONTINUE;
25 }
26
27 BOOL CALLBACK DIEnumDevicesCallback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef)
28 {
29 ((std::list<DIDEVICEINSTANCE>*)pvRef)->push_back(*lpddi);
30 return DIENUM_CONTINUE;
31 }
32
33 std::string GetDeviceName(const LPDIRECTINPUTDEVICE8 device)
34 {
35 DIPROPSTRING str = {};
36 str.diph.dwSize = sizeof(str);
37 str.diph.dwHeaderSize = sizeof(str.diph);
38 str.diph.dwHow = DIPH_DEVICE;
39
40 std::string result;
41 HRESULT hr = device->GetProperty(DIPROP_PRODUCTNAME, &str.diph);
42 if (SUCCEEDED(hr))
43 {
44 result = StripWhitespace(WStringToUTF8(str.wsz));
45 }
46 else
47 {
48 ERROR_LOG_FMT(CONTROLLERINTERFACE, "GetProperty(DIPROP_PRODUCTNAME) failed: {}",
49 Common::HRWrap(hr));
50 }
51
52 return result;
53 }
54
55 // Assumes hwnd had not changed from the previous call
56 void PopulateDevices(HWND hwnd)
57 {
58 if (!s_idi8)
59 {
60 HRESULT hr = DirectInput8Create(GetModuleHandle(nullptr), DIRECTINPUT_VERSION,
61 IID_IDirectInput8, (LPVOID*)&s_idi8, nullptr);
62 if (FAILED(hr))
63 {
64 ERROR_LOG_FMT(CONTROLLERINTERFACE, "DirectInput8Create failed: {}", Common::HRWrap(hr));
65 return;
66 }
67 }
68
69 // Remove old (invalid) devices. No need to ever remove the KeyboardMouse device.
70 // Note that if we have 2+ DInput controllers, not fully repopulating devices
71 // will mean that a device with index "2" could persist while there is no device with index "0".
72 // This is slightly inconsistent as when we refresh all devices, they will instead reset, and
73 // that happens a lot (for uncontrolled reasons, like starting/stopping the emulation).
74 g_controller_interface.RemoveDevice(
75 [](const auto* dev) { return dev->GetSource() == DINPUT_SOURCE_NAME && !dev->IsValid(); });
76
77 InitKeyboardMouse(s_idi8, hwnd);
78 InitJoystick(s_idi8, hwnd);
79 }
80
81 void ChangeWindow(HWND hwnd)
82 {
83 if (s_idi8) // Has init? Ignore if called before the first PopulateDevices()
84 {
85 // The KeyboardMouse device is marked as virtual device, so we avoid removing it.
86 // We need to force all the DInput joysticks to be destroyed now, or recreation would fail.
87 g_controller_interface.RemoveDevice(
88 [](const auto* dev) {
89 return dev->GetSource() == DINPUT_SOURCE_NAME && !dev->IsVirtualDevice();
90 },
91 true);
92
93 SetKeyboardMouseWindow(hwnd);
94 InitJoystick(s_idi8, hwnd);
95 }
96 }
97
98 void DeInit()
99 {
100 if (s_idi8)
101 {
102 s_idi8->Release();
103 s_idi8 = nullptr;
104 }
105 }
106 } // namespace ciface::DInput