Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / gamepad / raw_input_data_fetcher_win.h
blob16908c116467927aca60a0793ea2df406eb4d67d
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"
10 #include <stdlib.h>
11 #include <Unknwn.h>
12 #include <WinDef.h>
13 #include <windows.h>
15 #include <hidsdi.h>
16 #include <map>
17 #include <vector>
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"
28 namespace content {
30 struct RawGamepadAxis {
31 HIDP_VALUE_CAPS caps;
32 float value;
33 bool active;
36 struct RawGamepadInfo {
37 HANDLE handle;
38 scoped_ptr<uint8[]> ppd_buffer;
39 PHIDP_PREPARSED_DATA preparsed_data;
41 uint32_t report_id;
42 uint32_t vendor_id;
43 uint32_t product_id;
45 wchar_t id[blink::WebGamepad::idLengthCap];
47 uint32_t buttons_length;
48 bool buttons[blink::WebGamepad::buttonsLengthCap];
50 uint32_t axes_length;
51 RawGamepadAxis axes[blink::WebGamepad::axesLengthCap];
54 class RawInputDataFetcher
55 : public base::SupportsWeakPtr<RawInputDataFetcher>,
56 public base::MessageLoop::DestructionObserver {
57 public:
58 explicit RawInputDataFetcher();
59 ~RawInputDataFetcher();
61 // DestructionObserver overrides.
62 virtual void WillDestroyCurrentMessageLoop() override;
64 bool Available() { return rawinput_available_; }
65 void StartMonitor();
66 void StopMonitor();
68 std::vector<RawGamepadInfo*> EnumerateDevices();
69 RawGamepadInfo* GetGamepadInfo(HANDLE handle);
71 private:
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,
78 WPARAM wparam,
79 LPARAM lparam,
80 LRESULT* result);
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,
97 ULONG* UsageLength,
98 PHIDP_PREPARSED_DATA PreparsedData,
99 PCHAR Report,
100 ULONG ReportLength);
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
113 // successful.
114 bool GetHidDllFunctions();
116 base::ScopedNativeLibrary hid_dll_;
117 scoped_ptr<base::win::MessageWindow> window_;
118 bool rawinput_available_;
119 bool filter_xinput_;
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_