OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 "device/vr/android/gvr/gvr_gamepad_data_fetcher.h" | |
6 | |
7 #include "base/strings/utf_string_conversions.h" | |
8 | |
9 #include "device/vr/android/gvr/gvr_delegate.h" | |
10 #include "third_party/WebKit/public/platform/WebGamepads.h" | |
11 | |
12 namespace device { | |
13 | |
14 namespace { | |
15 | |
16 void CopyToWebUString(unsigned short* dest, | |
17 size_t dest_length, | |
18 base::string16 src) { | |
19 const size_t str_to_copy = std::min(src.size(), dest_length - 1); | |
20 memcpy(dest, src.data(), str_to_copy * sizeof(base::string16::value_type)); | |
bshe
2016/09/20 15:35:03
If dest_length - 1 < src.size and sizeof(base::str
| |
21 dest[str_to_copy] = 0; | |
22 } | |
23 | |
24 } // namespace | |
25 | |
26 using namespace blink; | |
27 | |
28 GvrGamepadDataFetcher::Factory::Factory(GvrDelegate* delegate) | |
29 : delegate_(delegate) {} | |
30 | |
31 GvrGamepadDataFetcher::Factory::~Factory() {} | |
32 | |
33 std::unique_ptr<GamepadDataFetcher> | |
34 GvrGamepadDataFetcher::Factory::CreateDataFetcher() { | |
35 return std::unique_ptr<GamepadDataFetcher>( | |
36 new GvrGamepadDataFetcher(delegate_)); | |
37 } | |
38 | |
39 GamepadSource GvrGamepadDataFetcher::Factory::source() { | |
40 return GAMEPAD_SOURCE_GVR; | |
41 } | |
42 | |
43 GvrGamepadDataFetcher::GvrGamepadDataFetcher(GvrDelegate* delegate) { | |
44 gvr::GvrApi* gvr_api = delegate->gvr_api(); | |
45 controller_api_.reset(new gvr::ControllerApi()); | |
46 int32_t options = gvr::ControllerApi::DefaultOptions(); | |
47 options |= GVR_CONTROLLER_ENABLE_GYRO; | |
48 bool success = controller_api_->Init(options, gvr_api->GetContext()); | |
49 if (!success) | |
50 controller_api_.reset(nullptr); | |
51 } | |
52 | |
53 GvrGamepadDataFetcher::~GvrGamepadDataFetcher() {} | |
54 | |
55 GamepadSource GvrGamepadDataFetcher::source() { | |
56 return GAMEPAD_SOURCE_GVR; | |
57 } | |
58 | |
59 void GvrGamepadDataFetcher::OnAddedToProvider() { | |
60 PauseHint(false); | |
61 } | |
62 | |
63 void GvrGamepadDataFetcher::GetGamepadData(bool devices_changed_hint) { | |
64 if (!controller_api_) | |
65 return; | |
66 | |
67 PadState* state = GetPadState(0); | |
68 if (!state) | |
69 return; | |
70 | |
71 WebGamepad& pad = state->data; | |
72 if (state->active_state == GAMEPAD_NEWLY_ACTIVE) { | |
73 // This is the first time we've seen this device, so do some one-time | |
74 // initialization | |
75 pad.connected = true; | |
76 CopyToWebUString(pad.id, WebGamepad::idLengthCap - 1, | |
mthiesse
2016/09/20 16:02:06
I don't think this is the right fix, I think this
| |
77 base::UTF8ToUTF16("Daydream Controller")); | |
78 CopyToWebUString(pad.mapping, WebGamepad::mappingLengthCap - 1, | |
79 base::UTF8ToUTF16("")); | |
80 pad.buttonsLength = 1; | |
81 pad.axesLength = 2; | |
82 } | |
83 | |
84 controller_state_.Update(*controller_api_); | |
85 | |
86 pad.timestamp = controller_state_.GetLastOrientationTimestamp(); | |
87 | |
88 // TODO: Query from API if avaialable. | |
89 pad.hand = GamepadHandRight; | |
90 | |
91 if (controller_state_.IsTouching()) { | |
92 gvr_vec2f touch_position = controller_state_.GetTouchPos(); | |
93 pad.axes[0] = (touch_position.x * 2.0f) - 1.0f; | |
94 pad.axes[1] = (touch_position.y * 2.0f) - 1.0f; | |
95 } else { | |
96 pad.axes[0] = 0.0f; | |
97 pad.axes[1] = 0.0f; | |
98 } | |
99 | |
100 pad.buttons[0].touched = controller_state_.IsTouching(); | |
101 pad.buttons[0].pressed = | |
102 controller_state_.GetButtonState(GVR_CONTROLLER_BUTTON_CLICK); | |
103 pad.buttons[0].value = pad.buttons[0].pressed ? 1.0f : 0.0f; | |
104 | |
105 pad.pose.notNull = true; | |
106 pad.pose.hasOrientation = true; | |
107 pad.pose.hasPosition = false; | |
108 | |
109 gvr_quatf orientation = controller_state_.GetOrientation(); | |
110 pad.pose.orientation.notNull = true; | |
111 pad.pose.orientation.x = orientation.qx; | |
112 pad.pose.orientation.y = orientation.qy; | |
113 pad.pose.orientation.z = orientation.qz; | |
114 pad.pose.orientation.w = orientation.qw; | |
115 | |
116 gvr_vec3f accel = controller_state_.GetAccel(); | |
117 pad.pose.linearAcceleration.notNull = true; | |
118 pad.pose.linearAcceleration.x = accel.x; | |
119 pad.pose.linearAcceleration.y = accel.y; | |
120 pad.pose.linearAcceleration.z = accel.z; | |
121 | |
122 gvr_vec3f gyro = controller_state_.GetGyro(); | |
123 pad.pose.angularVelocity.notNull = true; | |
124 pad.pose.angularVelocity.x = gyro.x; | |
125 pad.pose.angularVelocity.y = gyro.y; | |
126 pad.pose.angularVelocity.z = gyro.z; | |
127 } | |
128 | |
129 void GvrGamepadDataFetcher::PauseHint(bool paused) { | |
130 if (!controller_api_) | |
131 return; | |
132 | |
133 if (paused) { | |
134 controller_api_->Pause(); | |
135 } else { | |
136 controller_api_->Resume(); | |
137 } | |
138 } | |
139 | |
140 } // namespace device | |
OLD | NEW |