Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(773)

Side by Side Diff: content/browser/gamepad/data_fetcher_win.cc

Issue 8568029: Add gamepad hardware data fetcher, and higher level thread container (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: add some silly osx init code Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2011 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.
4
5 #include "content/browser/gamepad/data_fetcher_win.h"
6
7 #include "base/debug/trace_event.h"
8 #include "content/common/gamepad_messages.h"
9 #include "content/common/gamepad_hardware_buffer.h"
10
11 #pragma comment(lib, "xinput.lib")
12
13 namespace gamepad {
14
15 using namespace WebKit;
16
17 namespace {
18
19 // See http://goo.gl/5VSJR. These are not available in all versions of the
20 // header, but they can be returned from the driver, so we define our own
21 // versions here.
22 static const BYTE kDeviceSubTypeGamepad = 1;
23 static const BYTE kDeviceSubTypeWheel = 2;
24 static const BYTE kDeviceSubTypeArcadeStick = 3;
25 static const BYTE kDeviceSubTypeFlightStick = 4;
26 static const BYTE kDeviceSubTypeDancePad = 5;
27 static const BYTE kDeviceSubTypeGuitar = 6;
28 static const BYTE kDeviceSubTypeGuitarAlternate = 7;
29 static const BYTE kDeviceSubTypeDrumKit = 8;
30 static const BYTE kDeviceSubTypeGuitarBass = 11;
31 static const BYTE kDeviceSubTypeArcadePad = 19;
32
33 const WebUChar* const GamepadSubTypeName(BYTE sub_type) {
34 switch (sub_type) {
35 case kDeviceSubTypeGamepad: return L"GAMEPAD";
36 case kDeviceSubTypeWheel: return L"WHEEL";
37 case kDeviceSubTypeArcadeStick: return L"ARCADE_STICK";
38 case kDeviceSubTypeFlightStick: return L"FLIGHT_STICK";
39 case kDeviceSubTypeDancePad: return L"DANCE_PAD";
40 case kDeviceSubTypeGuitar: return L"GUITAR";
41 case kDeviceSubTypeGuitarAlternate: return L"GUITAR_ALTERNATE";
42 case kDeviceSubTypeDrumKit: return L"DRUM_KIT";
43 case kDeviceSubTypeGuitarBass: return L"GUITAR_BASS";
44 case kDeviceSubTypeArcadePad: return L"ARCADE_PAD";
45 default: return L"<UNKNOWN>";
46 }
47 }
48
49 }
50
51 void DataFetcherWindows::GetGamepadData(WebGamepads* pads,
52 bool devices_changed_hint) {
53 TRACE_EVENT0("GAMEPAD", "DataFetcherWindows::GetGamepadData");
54 pads->length = WebGamepads::itemsLengthCap;
55
56 // If we got notification that system devices have been updated, then
57 // run GetCapabilities to update the connected status and the device
58 // identifier. It can be slow to do to both GetCapabilities and
59 // GetState on unconnected devices, so we want to avoid a 2-5ms pause
60 // here by only doing this when the devices are updated (despite
61 // documentation claiming it's OK to call it any time).
62 if (devices_changed_hint) {
63 for (unsigned i = 0; i < WebGamepads::itemsLengthCap; ++i) {
64 WebGamepad& pad = pads->items[i];
65 TRACE_EVENT1("GAMEPAD", "DataFetcherWindows::GetCapabilities", "id", i);
66 XINPUT_CAPABILITIES caps;
67 DWORD res = XInputGetCapabilities(i, XINPUT_FLAG_GAMEPAD, &caps);
68 if (res == ERROR_DEVICE_NOT_CONNECTED) {
69 pad.connected = false;
70 } else {
71 pad.connected = true;
72 base::swprintf(pad.id,
73 WebGamepad::idLengthCap,
74 L"Xbox 360 Controller (XInput %ls)",
75 GamepadSubTypeName(caps.SubType));
76 }
77 }
78 }
79
80 // We've updated the connection state if necessary, now update the actual
81 // data for the devices that are connected.
82 for (unsigned i = 0; i < WebGamepads::itemsLengthCap; ++i) {
83 WebGamepad& pad = pads->items[i];
84
85 // We rely on device_changed and GetCapabilities to tell us that
86 // something's been connected, but we will mark as disconnected if
87 // GetState returns that we've lost the pad.
88 if (!pad.connected)
89 continue;
90
91 XINPUT_STATE state;
92 memset(&state, 0, sizeof(XINPUT_STATE));
93 TRACE_EVENT_BEGIN1("GAMEPAD", "XInputGetState", "id", i);
94 DWORD dwResult = XInputGetState(i, &state);
95 TRACE_EVENT_END1("GAMEPAD", "XInputGetState", "id", i);
96
97 if (dwResult == ERROR_SUCCESS) {
98 pad.timestamp = state.dwPacketNumber;
99 pad.buttonsLength = 0;
100 #define ADD(b) pad.buttons[pad.buttonsLength++] = \
101 ((state.Gamepad.wButtons & (b)) ? 1.0 : 0.0);
102 ADD(XINPUT_GAMEPAD_A);
103 ADD(XINPUT_GAMEPAD_B);
104 ADD(XINPUT_GAMEPAD_X);
105 ADD(XINPUT_GAMEPAD_Y);
106 ADD(XINPUT_GAMEPAD_LEFT_SHOULDER);
107 ADD(XINPUT_GAMEPAD_RIGHT_SHOULDER);
108 pad.buttons[pad.buttonsLength++] = state.Gamepad.bLeftTrigger / 255.0;
109 pad.buttons[pad.buttonsLength++] = state.Gamepad.bRightTrigger / 255.0;
110 ADD(XINPUT_GAMEPAD_BACK);
111 ADD(XINPUT_GAMEPAD_START);
112 ADD(XINPUT_GAMEPAD_LEFT_THUMB);
113 ADD(XINPUT_GAMEPAD_RIGHT_THUMB);
114 ADD(XINPUT_GAMEPAD_DPAD_UP);
115 ADD(XINPUT_GAMEPAD_DPAD_DOWN);
116 ADD(XINPUT_GAMEPAD_DPAD_LEFT);
117 ADD(XINPUT_GAMEPAD_DPAD_RIGHT);
118 #undef ADD
119 pad.axesLength = 0;
120 // XInput are +up/+right, -down/-left, we want -up/-left.
121 pad.axes[pad.axesLength++] = state.Gamepad.sThumbLX / 32767.0;
122 pad.axes[pad.axesLength++] = -state.Gamepad.sThumbLY / 32767.0;
123 pad.axes[pad.axesLength++] = state.Gamepad.sThumbRX / 32767.0;
124 pad.axes[pad.axesLength++] = -state.Gamepad.sThumbRY / 32767.0;
125 } else {
126 pad.connected = false;
127 }
128 }
129 }
130
131 } // namespace gamepad
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698