1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
// license:BSD-3-Clause
// copyright-holders:Aaron Giles, Brad Hughes, Vas Crabb
//============================================================
//
// input_dinput.h - Windows DirectInput support
//
//============================================================
#ifndef MAME_OSD_INPUT_INPUT_DINPUT_H
#define MAME_OSD_INPUT_INPUT_DINPUT_H
#pragma once
#include "assignmenthelper.h"
#include "input_wincommon.h"
#include "modules/lib/osdlib.h"
#include "modules/lib/osdobj_common.h"
#include "window.h"
#include "strconv.h"
#include <memory>
#include <mutex>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <windows.h>
#include <dinput.h>
#include <wrl/client.h>
namespace osd {
//============================================================
// dinput_device - base directinput device
//============================================================
enum class dinput_cooperative_level
{
FOREGROUND,
BACKGROUND
};
class dinput_api_helper
{
public:
dinput_api_helper();
~dinput_api_helper();
int initialize();
template <typename TDevice, typename TCallback>
std::unique_ptr<TDevice> create_device(
input_module_base &module,
LPCDIDEVICEINSTANCE instance,
LPCDIDATAFORMAT format1,
LPCDIDATAFORMAT format2,
dinput_cooperative_level cooperative_level,
TCallback &&callback)
{
auto [device, format] = open_device(instance, format1, format2, cooperative_level);
if (!device)
return nullptr;
// get the capabilities
DIDEVCAPS caps;
caps.dwSize = sizeof(caps);
HRESULT const result = device->GetCapabilities(&caps);
if (result != DI_OK)
return nullptr;
if (!callback(device, format))
return nullptr;
// allocate memory for the device object
return std::make_unique<TDevice>(
text::from_tstring(instance->tszInstanceName),
make_id(instance),
module,
std::move(device),
caps,
format);
}
template <typename T>
HRESULT enum_attached_devices(int devclass, T &&callback) const
{
return m_dinput->EnumDevices(
devclass,
&di_emun_devices_cb<std::remove_reference_t<T> >,
LPVOID(&callback),
DIEDFL_ATTACHEDONLY);
}
template <typename T>
static HRESULT set_dword_property(
T &&device,
REFGUID property_guid,
DWORD object,
DWORD how,
DWORD value)
{
DIPROPDWORD dipdw;
dipdw.diph.dwSize = sizeof(dipdw);
dipdw.diph.dwHeaderSize = sizeof(dipdw.diph);
dipdw.diph.dwObj = object;
dipdw.diph.dwHow = how;
dipdw.dwData = value;
return device->SetProperty(property_guid, &dipdw.diph);
}
private:
std::pair<Microsoft::WRL::ComPtr<IDirectInputDevice8>, LPCDIDATAFORMAT> open_device(
LPCDIDEVICEINSTANCE instance,
LPCDIDATAFORMAT format1,
LPCDIDATAFORMAT format2,
dinput_cooperative_level cooperative_level);
static std::string make_id(LPCDIDEVICEINSTANCE instance);
template <typename T>
static BOOL CALLBACK di_emun_devices_cb(LPCDIDEVICEINSTANCE instance, LPVOID ref)
{
return (*reinterpret_cast<T *>(ref))(instance);
}
Microsoft::WRL::ComPtr<IDirectInput8> m_dinput;
dynamic_module::ptr m_dinput_dll;
};
class dinput_device : public device_info
{
protected:
dinput_device(
std::string &&name,
std::string &&id,
input_module &module,
Microsoft::WRL::ComPtr<IDirectInputDevice8> &&device,
DIDEVCAPS const &caps,
LPCDIDATAFORMAT format);
HRESULT poll_dinput(LPVOID pState) const;
std::string item_name(int offset, std::string_view defstr, char const *suffix) const;
// DirectInput-specific information about a device
Microsoft::WRL::ComPtr<IDirectInputDevice8> const m_device;
DIDEVCAPS m_caps;
LPCDIDATAFORMAT const m_format;
};
class dinput_joystick_device : public dinput_device, protected joystick_assignment_helper
{
public:
dinput_joystick_device(
std::string &&name,
std::string &&id,
input_module &module,
Microsoft::WRL::ComPtr<IDirectInputDevice8> &&device,
DIDEVCAPS const &caps,
LPCDIDATAFORMAT format);
void reset() override;
void poll(bool relative_reset) override;
void configure(input_device &device) override;
private:
// state information for a joystick; DirectInput state must be first element
struct dinput_joystick_state
{
DIJOYSTATE state;
LONG rangemin[8];
LONG rangemax[8];
};
static int32_t pov_get_state(void *device_internal, void *item_internal);
dinput_joystick_state m_joystick;
};
} // namespace osd
#endif // MAME_OSD_INPUT_INPUT_DINPUT_H
|