OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "device/gamepad/gamepad_platform_data_fetcher_win.h" | 5 #include "device/gamepad/gamepad_platform_data_fetcher_win.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
14 #include "base/trace_event/trace_event.h" | 14 #include "base/trace_event/trace_event.h" |
15 #include "base/win/windows_version.h" | 15 #include "base/win/windows_version.h" |
16 | 16 |
17 namespace device { | 17 namespace device { |
18 | 18 |
19 using namespace blink; | |
20 | |
21 namespace { | 19 namespace { |
22 | 20 |
23 // See http://goo.gl/5VSJR. These are not available in all versions of the | 21 // See http://goo.gl/5VSJR. These are not available in all versions of the |
24 // header, but they can be returned from the driver, so we define our own | 22 // header, but they can be returned from the driver, so we define our own |
25 // versions here. | 23 // versions here. |
26 static const BYTE kDeviceSubTypeGamepad = 1; | 24 static const BYTE kDeviceSubTypeGamepad = 1; |
27 static const BYTE kDeviceSubTypeWheel = 2; | 25 static const BYTE kDeviceSubTypeWheel = 2; |
28 static const BYTE kDeviceSubTypeArcadeStick = 3; | 26 static const BYTE kDeviceSubTypeArcadeStick = 3; |
29 static const BYTE kDeviceSubTypeFlightStick = 4; | 27 static const BYTE kDeviceSubTypeFlightStick = 4; |
30 static const BYTE kDeviceSubTypeDancePad = 5; | 28 static const BYTE kDeviceSubTypeDancePad = 5; |
31 static const BYTE kDeviceSubTypeGuitar = 6; | 29 static const BYTE kDeviceSubTypeGuitar = 6; |
32 static const BYTE kDeviceSubTypeGuitarAlternate = 7; | 30 static const BYTE kDeviceSubTypeGuitarAlternate = 7; |
33 static const BYTE kDeviceSubTypeDrumKit = 8; | 31 static const BYTE kDeviceSubTypeDrumKit = 8; |
34 static const BYTE kDeviceSubTypeGuitarBass = 11; | 32 static const BYTE kDeviceSubTypeGuitarBass = 11; |
35 static const BYTE kDeviceSubTypeArcadePad = 19; | 33 static const BYTE kDeviceSubTypeArcadePad = 19; |
36 | 34 |
37 float NormalizeXInputAxis(SHORT value) { | 35 float NormalizeXInputAxis(SHORT value) { |
38 return ((value + 32768.f) / 32767.5f) - 1.f; | 36 return ((value + 32768.f) / 32767.5f) - 1.f; |
39 } | 37 } |
40 | 38 |
41 const WebUChar* GamepadSubTypeName(BYTE sub_type) { | 39 const UChar* GamepadSubTypeName(BYTE sub_type) { |
42 switch (sub_type) { | 40 switch (sub_type) { |
43 case kDeviceSubTypeGamepad: | 41 case kDeviceSubTypeGamepad: |
44 return L"GAMEPAD"; | 42 return L"GAMEPAD"; |
45 case kDeviceSubTypeWheel: | 43 case kDeviceSubTypeWheel: |
46 return L"WHEEL"; | 44 return L"WHEEL"; |
47 case kDeviceSubTypeArcadeStick: | 45 case kDeviceSubTypeArcadeStick: |
48 return L"ARCADE_STICK"; | 46 return L"ARCADE_STICK"; |
49 case kDeviceSubTypeFlightStick: | 47 case kDeviceSubTypeFlightStick: |
50 return L"FLIGHT_STICK"; | 48 return L"FLIGHT_STICK"; |
51 case kDeviceSubTypeDancePad: | 49 case kDeviceSubTypeDancePad: |
52 return L"DANCE_PAD"; | 50 return L"DANCE_PAD"; |
53 case kDeviceSubTypeGuitar: | 51 case kDeviceSubTypeGuitar: |
54 return L"GUITAR"; | 52 return L"GUITAR"; |
55 case kDeviceSubTypeGuitarAlternate: | 53 case kDeviceSubTypeGuitarAlternate: |
56 return L"GUITAR_ALTERNATE"; | 54 return L"GUITAR_ALTERNATE"; |
57 case kDeviceSubTypeDrumKit: | 55 case kDeviceSubTypeDrumKit: |
58 return L"DRUM_KIT"; | 56 return L"DRUM_KIT"; |
59 case kDeviceSubTypeGuitarBass: | 57 case kDeviceSubTypeGuitarBass: |
60 return L"GUITAR_BASS"; | 58 return L"GUITAR_BASS"; |
61 case kDeviceSubTypeArcadePad: | 59 case kDeviceSubTypeArcadePad: |
62 return L"ARCADE_PAD"; | 60 return L"ARCADE_PAD"; |
63 default: | 61 default: |
64 return L"<UNKNOWN>"; | 62 return L"<UNKNOWN>"; |
65 } | 63 } |
66 } | 64 } |
67 | 65 |
68 const WebUChar* XInputDllFileName() { | 66 const UChar* XInputDllFileName() { |
69 // Xinput.h defines filename (XINPUT_DLL) on different Windows versions, but | 67 // Xinput.h defines filename (XINPUT_DLL) on different Windows versions, but |
70 // Xinput.h specifies it in build time. Approach here uses the same values | 68 // Xinput.h specifies it in build time. Approach here uses the same values |
71 // and it is resolving dll filename based on Windows version it is running on. | 69 // and it is resolving dll filename based on Windows version it is running on. |
72 if (base::win::GetVersion() >= base::win::VERSION_WIN8) { | 70 if (base::win::GetVersion() >= base::win::VERSION_WIN8) { |
73 // For Windows 8 and 10, XINPUT_DLL is xinput1_4.dll. | 71 // For Windows 8 and 10, XINPUT_DLL is xinput1_4.dll. |
74 return FILE_PATH_LITERAL("xinput1_4.dll"); | 72 return FILE_PATH_LITERAL("xinput1_4.dll"); |
75 } else if (base::win::GetVersion() >= base::win::VERSION_WIN7) { | 73 } else if (base::win::GetVersion() >= base::win::VERSION_WIN7) { |
76 return FILE_PATH_LITERAL("xinput9_1_0.dll"); | 74 return FILE_PATH_LITERAL("xinput9_1_0.dll"); |
77 } else { | 75 } else { |
78 NOTREACHED(); | 76 NOTREACHED(); |
(...skipping 28 matching lines...) Expand all Loading... |
107 XINPUT_CAPABILITIES caps; | 105 XINPUT_CAPABILITIES caps; |
108 DWORD res = xinput_get_capabilities_(i, XINPUT_FLAG_GAMEPAD, &caps); | 106 DWORD res = xinput_get_capabilities_(i, XINPUT_FLAG_GAMEPAD, &caps); |
109 xinuput_connected_[i] = (res == ERROR_SUCCESS); | 107 xinuput_connected_[i] = (res == ERROR_SUCCESS); |
110 if (!xinuput_connected_[i]) | 108 if (!xinuput_connected_[i]) |
111 continue; | 109 continue; |
112 | 110 |
113 PadState* state = GetPadState(i); | 111 PadState* state = GetPadState(i); |
114 if (!state) | 112 if (!state) |
115 continue; // No slot available for this gamepad. | 113 continue; // No slot available for this gamepad. |
116 | 114 |
117 WebGamepad& pad = state->data; | 115 Gamepad& pad = state->data; |
118 | 116 |
119 if (state->active_state == GAMEPAD_NEWLY_ACTIVE) { | 117 if (state->active_state == GAMEPAD_NEWLY_ACTIVE) { |
120 // This is the first time we've seen this device, so do some one-time | 118 // This is the first time we've seen this device, so do some one-time |
121 // initialization | 119 // initialization |
122 pad.connected = true; | 120 pad.connected = true; |
123 swprintf(pad.id, WebGamepad::kIdLengthCap, | 121 swprintf(pad.id, Gamepad::kIdLengthCap, |
124 L"Xbox 360 Controller (XInput STANDARD %ls)", | 122 L"Xbox 360 Controller (XInput STANDARD %ls)", |
125 GamepadSubTypeName(caps.SubType)); | 123 GamepadSubTypeName(caps.SubType)); |
126 swprintf(pad.mapping, WebGamepad::kMappingLengthCap, L"standard"); | 124 swprintf(pad.mapping, Gamepad::kMappingLengthCap, L"standard"); |
127 } | 125 } |
128 } | 126 } |
129 } | 127 } |
130 } | 128 } |
131 | 129 |
132 void GamepadPlatformDataFetcherWin::GetGamepadData(bool devices_changed_hint) { | 130 void GamepadPlatformDataFetcherWin::GetGamepadData(bool devices_changed_hint) { |
133 TRACE_EVENT0("GAMEPAD", "GetGamepadData"); | 131 TRACE_EVENT0("GAMEPAD", "GetGamepadData"); |
134 | 132 |
135 if (!xinput_available_) | 133 if (!xinput_available_) |
136 return; | 134 return; |
(...skipping 12 matching lines...) Expand all Loading... |
149 if (xinuput_connected_[i]) | 147 if (xinuput_connected_[i]) |
150 GetXInputPadData(i); | 148 GetXInputPadData(i); |
151 } | 149 } |
152 } | 150 } |
153 | 151 |
154 void GamepadPlatformDataFetcherWin::GetXInputPadData(int i) { | 152 void GamepadPlatformDataFetcherWin::GetXInputPadData(int i) { |
155 PadState* pad_state = provider()->GetPadState(GAMEPAD_SOURCE_WIN_XINPUT, i); | 153 PadState* pad_state = provider()->GetPadState(GAMEPAD_SOURCE_WIN_XINPUT, i); |
156 if (!pad_state) | 154 if (!pad_state) |
157 return; | 155 return; |
158 | 156 |
159 WebGamepad& pad = pad_state->data; | 157 Gamepad& pad = pad_state->data; |
160 | 158 |
161 XINPUT_STATE state; | 159 XINPUT_STATE state; |
162 memset(&state, 0, sizeof(XINPUT_STATE)); | 160 memset(&state, 0, sizeof(XINPUT_STATE)); |
163 TRACE_EVENT_BEGIN1("GAMEPAD", "XInputGetState", "id", i); | 161 TRACE_EVENT_BEGIN1("GAMEPAD", "XInputGetState", "id", i); |
164 DWORD dwResult = xinput_get_state_(i, &state); | 162 DWORD dwResult = xinput_get_state_(i, &state); |
165 TRACE_EVENT_END1("GAMEPAD", "XInputGetState", "id", i); | 163 TRACE_EVENT_END1("GAMEPAD", "XInputGetState", "id", i); |
166 | 164 |
167 if (dwResult == ERROR_SUCCESS) { | 165 if (dwResult == ERROR_SUCCESS) { |
168 pad.timestamp = state.dwPacketNumber; | 166 pad.timestamp = state.dwPacketNumber; |
169 pad.buttons_length = 0; | 167 pad.buttons_length = 0; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 if (!xinput_get_state_) | 225 if (!xinput_get_state_) |
228 return false; | 226 return false; |
229 if (xinput_enable) { | 227 if (xinput_enable) { |
230 // XInputEnable is unavailable before Win8 and deprecated in Win10. | 228 // XInputEnable is unavailable before Win8 and deprecated in Win10. |
231 xinput_enable(true); | 229 xinput_enable(true); |
232 } | 230 } |
233 return true; | 231 return true; |
234 } | 232 } |
235 | 233 |
236 } // namespace device | 234 } // namespace device |
OLD | NEW |