1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_GAMEPAD_RAW_INPUT_DATA_FETCHER_WIN_H_
6 #define CONTENT_BROWSER_GAMEPAD_RAW_INPUT_DATA_FETCHER_WIN_H_
8 #include "build/build_config.h"
19 #include "base/memory/scoped_ptr.h"
20 #include "base/memory/weak_ptr.h"
21 #include "base/message_loop/message_loop.h"
22 #include "base/scoped_native_library.h"
23 #include "base/win/message_window.h"
24 #include "content/browser/gamepad/gamepad_data_fetcher.h"
25 #include "content/browser/gamepad/gamepad_standard_mappings.h"
26 #include "third_party/WebKit/public/platform/WebGamepads.h"
30 struct RawGamepadAxis
{
34 unsigned long bitmask
;
37 struct RawGamepadInfo
{
42 scoped_ptr
<uint8
[]> ppd_buffer
;
43 PHIDP_PREPARSED_DATA preparsed_data
;
49 wchar_t id
[blink::WebGamepad::idLengthCap
];
51 uint32_t buttons_length
;
52 bool buttons
[blink::WebGamepad::buttonsLengthCap
];
55 RawGamepadAxis axes
[blink::WebGamepad::axesLengthCap
];
58 class RawInputDataFetcher
59 : public base::SupportsWeakPtr
<RawInputDataFetcher
>,
60 public base::MessageLoop::DestructionObserver
{
62 explicit RawInputDataFetcher();
63 ~RawInputDataFetcher() override
;
65 // DestructionObserver overrides.
66 void WillDestroyCurrentMessageLoop() override
;
68 bool Available() { return rawinput_available_
; }
72 std::vector
<RawGamepadInfo
*> EnumerateDevices();
73 RawGamepadInfo
* GetGamepadInfo(HANDLE handle
);
76 RawGamepadInfo
* ParseGamepadInfo(HANDLE hDevice
);
77 void UpdateGamepad(RAWINPUT
* input
, RawGamepadInfo
* gamepad_info
);
78 // Handles WM_INPUT messages.
79 LRESULT
OnInput(HRAWINPUT input_handle
);
80 // Handles messages received by |window_|.
81 bool HandleMessage(UINT message
,
85 RAWINPUTDEVICE
* GetRawInputDevices(DWORD flags
);
86 void ClearControllers();
88 // Function types we use from hid.dll.
89 typedef NTSTATUS (__stdcall
*HidPGetCapsFunc
)(
90 PHIDP_PREPARSED_DATA PreparsedData
, PHIDP_CAPS Capabilities
);
91 typedef NTSTATUS (__stdcall
*HidPGetButtonCapsFunc
)(
92 HIDP_REPORT_TYPE ReportType
, PHIDP_BUTTON_CAPS ButtonCaps
,
93 PUSHORT ButtonCapsLength
, PHIDP_PREPARSED_DATA PreparsedData
);
94 typedef NTSTATUS (__stdcall
*HidPGetValueCapsFunc
)(
95 HIDP_REPORT_TYPE ReportType
, PHIDP_VALUE_CAPS ValueCaps
,
96 PUSHORT ValueCapsLength
, PHIDP_PREPARSED_DATA PreparsedData
);
97 typedef NTSTATUS(__stdcall
* HidPGetUsagesExFunc
)(
98 HIDP_REPORT_TYPE ReportType
,
99 USHORT LinkCollection
,
100 PUSAGE_AND_PAGE ButtonList
,
102 PHIDP_PREPARSED_DATA PreparsedData
,
105 typedef NTSTATUS (__stdcall
*HidPGetUsageValueFunc
)(
106 HIDP_REPORT_TYPE ReportType
, USAGE UsagePage
, USHORT LinkCollection
,
107 USAGE Usage
, PULONG UsageValue
, PHIDP_PREPARSED_DATA PreparsedData
,
108 PCHAR Report
, ULONG ReportLength
);
109 typedef NTSTATUS (__stdcall
*HidPGetScaledUsageValueFunc
)(
110 HIDP_REPORT_TYPE ReportType
, USAGE UsagePage
, USHORT LinkCollection
,
111 USAGE Usage
, PLONG UsageValue
, PHIDP_PREPARSED_DATA PreparsedData
,
112 PCHAR Report
, ULONG ReportLength
);
113 typedef BOOLEAN (__stdcall
*HidDGetStringFunc
)(
114 HANDLE HidDeviceObject
, PVOID Buffer
, ULONG BufferLength
);
116 // Get functions from dynamically loaded hid.dll. Returns true if loading was
118 bool GetHidDllFunctions();
120 base::ScopedNativeLibrary hid_dll_
;
121 scoped_ptr
<base::win::MessageWindow
> window_
;
122 bool rawinput_available_
;
124 bool events_monitored_
;
126 std::map
<HANDLE
, RawGamepadInfo
*> controllers_
;
128 // Function pointers to HID functionality, retrieved in
129 // |GetHidDllFunctions|.
130 HidPGetCapsFunc hidp_get_caps_
;
131 HidPGetButtonCapsFunc hidp_get_button_caps_
;
132 HidPGetValueCapsFunc hidp_get_value_caps_
;
133 HidPGetUsagesExFunc hidp_get_usages_ex_
;
134 HidPGetUsageValueFunc hidp_get_usage_value_
;
135 HidPGetScaledUsageValueFunc hidp_get_scaled_usage_value_
;
136 HidDGetStringFunc hidd_get_product_string_
;
138 DISALLOW_COPY_AND_ASSIGN(RawInputDataFetcher
);
141 } // namespace content
143 #endif // CONTENT_BROWSER_GAMEPAD_RAW_INPUT_DATA_FETCHER_WIN_H_