forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raw_input_gamepad_device_win.h
147 lines (112 loc) · 4.96 KB
/
raw_input_gamepad_device_win.h
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
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef DEVICE_GAMEPAD_RAW_INPUT_GAMEPAD_DEVICE_WIN_
#define DEVICE_GAMEPAD_RAW_INPUT_GAMEPAD_DEVICE_WIN_
#include <Unknwn.h>
#include <WinDef.h>
#include <hidsdi.h>
#include <stdint.h>
#include <stdlib.h>
#include <windows.h>
#include <memory>
#include <vector>
#include "device/gamepad/abstract_haptic_gamepad.h"
#include "device/gamepad/dualshock4_controller_win.h"
#include "device/gamepad/hid_dll_functions_win.h"
#include "device/gamepad/hid_haptic_gamepad_win.h"
#include "device/gamepad/public/cpp/gamepad.h"
namespace device {
class RawInputGamepadDeviceWin : public AbstractHapticGamepad {
public:
// Relevant usage IDs within the Generic Desktop usage page. RawInput gamepads
// must have one of these usage IDs.
static const uint16_t kGenericDesktopJoystick = 0x04;
static const uint16_t kGenericDesktopGamePad = 0x05;
static const uint16_t kGenericDesktopMultiAxisController = 0x08;
RawInputGamepadDeviceWin(HANDLE device_handle,
int source_id,
HidDllFunctionsWin* hid_functions);
~RawInputGamepadDeviceWin() override;
static bool IsGamepadUsageId(uint16_t usage);
int GetSourceId() const { return source_id_; }
uint16_t GetVendorId() const { return vendor_id_; }
uint16_t GetVersionNumber() const { return version_number_; }
uint16_t GetProductId() const { return product_id_; }
std::wstring GetDeviceName() const { return name_; }
std::wstring GetProductString() const { return product_string_; }
// Return true if this device is a gamepad.
bool IsValid() const { return is_valid_; }
// Return true if this device supports vibration effects.
bool SupportsVibration() const;
// Updates the current gamepad state with data from a RAWINPUT event.
void UpdateGamepad(RAWINPUT* input);
// Read the current gamepad state into |pad|.
void ReadPadState(Gamepad* pad) const;
// Set the vibration magnitude for the strong and weak vibration actuators.
void SetVibration(double strong_magnitude, double weak_magnitude) override;
private:
// Axis state and capabilities for a single RawInput axis.
struct RawGamepadAxis {
HIDP_VALUE_CAPS caps;
float value;
bool active;
unsigned long bitmask;
};
// Stop vibration and release held resources.
void DoShutdown() override;
// Fetch information about this device. Returns true if the device appears to
// be a valid gamepad.
bool QueryDeviceInfo();
// Fetch HID properties (RID_DEVICE_INFO_HID). Returns false on failure.
bool QueryHidInfo();
// Fetch the device name (RIDI_DEVICENAME). Returns false on failure.
bool QueryDeviceName();
// Fetch the product string. Returns false if none is available.
bool QueryProductString();
// These methods fetch information about the capabilities of buttons and axes
// on the device.
bool QueryDeviceCapabilities();
void QueryButtonCapabilities(uint16_t button_count);
void QueryNormalButtonCapabilities(HIDP_BUTTON_CAPS button_caps[],
uint16_t button_count,
std::vector<bool>* button_indices_used);
void QuerySpecialButtonCapabilities(HIDP_BUTTON_CAPS button_caps[],
uint16_t button_count,
std::vector<bool>* button_indices_used);
void QueryAxisCapabilities(uint16_t axis_count);
// True if the device described by this object is a valid RawInput gamepad.
bool is_valid_ = false;
// The device handle.
HANDLE handle_ = nullptr;
// The index assigned to this gamepad by the data fetcher.
int source_id_ = 0;
// The last time the pad state was updated.
int64_t last_update_timestamp_;
// Functions loaded from hid.dll. Not owned.
HidDllFunctionsWin* hid_functions_ = nullptr;
uint16_t vendor_id_ = 0;
uint16_t product_id_ = 0;
uint16_t version_number_ = 0;
uint16_t usage_ = 0;
std::wstring name_;
std::wstring product_string_;
size_t buttons_length_ = 0;
bool buttons_[Gamepad::kButtonsLengthCap];
// Mapping from "Special" usage index (defined by the kSpecialUsages table)
// to an index within the |buttons_| array, or -1 if the special usage is not
// mapped for this device.
std::vector<int> special_button_map_;
size_t axes_length_ = 0;
RawGamepadAxis axes_[Gamepad::kAxesLengthCap];
// Buffer used for querying device capabilities. |ppd_buffer_| owns the
// memory pointed to by |preparsed_data_|.
std::unique_ptr<uint8_t[]> ppd_buffer_;
PHIDP_PREPARSED_DATA preparsed_data_ = nullptr;
// Dualshock4-specific functionality (e.g., haptics), if available.
std::unique_ptr<Dualshock4ControllerWin> dualshock4_;
// A controller that uses a HID output report for vibration effects.
std::unique_ptr<HidHapticGamepadWin> hid_haptics_;
};
} // namespace device
#endif // DEVICE_GAMEPAD_RAW_INPUT_GAMEPAD_DEVICE_WIN_