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
{
36 struct RawGamepadInfo
{
38 scoped_ptr
<uint8
[]> ppd_buffer
;
39 PHIDP_PREPARSED_DATA preparsed_data
;
45 wchar_t id
[blink::WebGamepad::idLengthCap
];
47 uint32_t buttons_length
;
48 bool buttons
[blink::WebGamepad::buttonsLengthCap
];
51 RawGamepadAxis axes
[blink::WebGamepad::axesLengthCap
];
54 class RawInputDataFetcher
55 : public base::SupportsWeakPtr
<RawInputDataFetcher
>,
56 public base::MessageLoop::DestructionObserver
{
58 explicit RawInputDataFetcher();
59 ~RawInputDataFetcher();
61 // DestructionObserver overrides.
62 virtual void WillDestroyCurrentMessageLoop() OVERRIDE
;
64 bool Available() { return rawinput_available_
; }
68 std::vector
<RawGamepadInfo
*> EnumerateDevices();
69 RawGamepadInfo
* GetGamepadInfo(HANDLE handle
);
72 RawGamepadInfo
* ParseGamepadInfo(HANDLE hDevice
);
73 void UpdateGamepad(RAWINPUT
* input
, RawGamepadInfo
* gamepad_info
);
74 // Handles WM_INPUT messages.
75 LRESULT
OnInput(HRAWINPUT input_handle
);
76 // Handles messages received by |window_|.
77 bool HandleMessage(UINT message
,
81 RAWINPUTDEVICE
* GetRawInputDevices(DWORD flags
);
82 void ClearControllers();
84 // Function types we use from hid.dll.
85 typedef NTSTATUS (__stdcall
*HidPGetCapsFunc
)(
86 PHIDP_PREPARSED_DATA PreparsedData
, PHIDP_CAPS Capabilities
);
87 typedef NTSTATUS (__stdcall
*HidPGetButtonCapsFunc
)(
88 HIDP_REPORT_TYPE ReportType
, PHIDP_BUTTON_CAPS ButtonCaps
,
89 PUSHORT ButtonCapsLength
, PHIDP_PREPARSED_DATA PreparsedData
);
90 typedef NTSTATUS (__stdcall
*HidPGetValueCapsFunc
)(
91 HIDP_REPORT_TYPE ReportType
, PHIDP_VALUE_CAPS ValueCaps
,
92 PUSHORT ValueCapsLength
, PHIDP_PREPARSED_DATA PreparsedData
);
93 typedef NTSTATUS(__stdcall
* HidPGetUsagesExFunc
)(
94 HIDP_REPORT_TYPE ReportType
,
95 USHORT LinkCollection
,
96 PUSAGE_AND_PAGE ButtonList
,
98 PHIDP_PREPARSED_DATA PreparsedData
,
101 typedef NTSTATUS (__stdcall
*HidPGetUsageValueFunc
)(
102 HIDP_REPORT_TYPE ReportType
, USAGE UsagePage
, USHORT LinkCollection
,
103 USAGE Usage
, PULONG UsageValue
, PHIDP_PREPARSED_DATA PreparsedData
,
104 PCHAR Report
, ULONG ReportLength
);
105 typedef NTSTATUS (__stdcall
*HidPGetScaledUsageValueFunc
)(
106 HIDP_REPORT_TYPE ReportType
, USAGE UsagePage
, USHORT LinkCollection
,
107 USAGE Usage
, PLONG UsageValue
, PHIDP_PREPARSED_DATA PreparsedData
,
108 PCHAR Report
, ULONG ReportLength
);
109 typedef BOOLEAN (__stdcall
*HidDGetStringFunc
)(
110 HANDLE HidDeviceObject
, PVOID Buffer
, ULONG BufferLength
);
112 // Get functions from dynamically loaded hid.dll. Returns true if loading was
114 bool GetHidDllFunctions();
116 base::ScopedNativeLibrary hid_dll_
;
117 scoped_ptr
<base::win::MessageWindow
> window_
;
118 bool rawinput_available_
;
120 bool events_monitored_
;
122 std::map
<HANDLE
, RawGamepadInfo
*> controllers_
;
124 // Function pointers to HID functionality, retrieved in
125 // |GetHidDllFunctions|.
126 HidPGetCapsFunc hidp_get_caps_
;
127 HidPGetButtonCapsFunc hidp_get_button_caps_
;
128 HidPGetValueCapsFunc hidp_get_value_caps_
;
129 HidPGetUsagesExFunc hidp_get_usages_ex_
;
130 HidPGetUsageValueFunc hidp_get_usage_value_
;
131 HidPGetScaledUsageValueFunc hidp_get_scaled_usage_value_
;
132 HidDGetStringFunc hidd_get_product_string_
;
134 DISALLOW_COPY_AND_ASSIGN(RawInputDataFetcher
);
137 } // namespace content
139 #endif // CONTENT_BROWSER_GAMEPAD_RAW_INPUT_DATA_FETCHER_WIN_H_