Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / gamepad / raw_input_data_fetcher_win.h
blob3de85df7326f9284c7f40659f6dd3bdd75083955
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;
34 unsigned long bitmask;
37 struct RawGamepadInfo {
38 RawGamepadInfo();
39 ~RawGamepadInfo();
41 HANDLE handle;
42 scoped_ptr<uint8[]> ppd_buffer;
43 PHIDP_PREPARSED_DATA preparsed_data;
45 uint32_t report_id;
46 uint32_t vendor_id;
47 uint32_t product_id;
49 wchar_t id[blink::WebGamepad::idLengthCap];
51 uint32_t buttons_length;
52 bool buttons[blink::WebGamepad::buttonsLengthCap];
54 uint32_t axes_length;
55 RawGamepadAxis axes[blink::WebGamepad::axesLengthCap];
58 class RawInputDataFetcher
59 : public base::SupportsWeakPtr<RawInputDataFetcher>,
60 public base::MessageLoop::DestructionObserver {
61 public:
62 explicit RawInputDataFetcher();
63 ~RawInputDataFetcher() override;
65 // DestructionObserver overrides.
66 void WillDestroyCurrentMessageLoop() override;
68 bool Available() { return rawinput_available_; }
69 void StartMonitor();
70 void StopMonitor();
72 std::vector<RawGamepadInfo*> EnumerateDevices();
73 RawGamepadInfo* GetGamepadInfo(HANDLE handle);
75 private:
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,
82 WPARAM wparam,
83 LPARAM lparam,
84 LRESULT* result);
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,
101 ULONG* UsageLength,
102 PHIDP_PREPARSED_DATA PreparsedData,
103 PCHAR Report,
104 ULONG ReportLength);
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
117 // successful.
118 bool GetHidDllFunctions();
120 base::ScopedNativeLibrary hid_dll_;
121 scoped_ptr<base::win::MessageWindow> window_;
122 bool rawinput_available_;
123 bool filter_xinput_;
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_