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)); | |
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; | |
mthiesse
2016/09/18 23:21:48
Just to check my understanding, we're only exposin
| |
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, | |
mthiesse
2016/09/18 23:21:48
Should this be WebGamepad::idLengthCap + 1 to incl
| |
77 base::UTF8ToUTF16("Daydream Controller")); | |
78 CopyToWebUString(pad.mapping, WebGamepad::mappingLengthCap, | |
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 pad.hand = GamepadHandRight; | |
89 | |
90 gvr_vec2f touch_position = controller_state_.GetTouchPos(); | |
91 pad.axes[0] = (touch_position.x * 2.0) - 1.0; | |
92 pad.axes[1] = (touch_position.y * 2.0) - 1.0; | |
93 | |
94 // TODO: use gamepad touched attribute to communicate touched state | |
95 pad.buttons[0].touched = controller_state_.IsTouching(); | |
96 pad.buttons[0].pressed = | |
97 controller_state_.GetButtonState(GVR_CONTROLLER_BUTTON_CLICK); | |
98 pad.buttons[0].value = pad.buttons[0].pressed ? 1.0f : 0.0f; | |
99 | |
100 // TODO: expose orientation/acceleration/velocity | |
mthiesse
2016/09/18 23:21:48
Should orientation still be in this TODO? Looks li
| |
101 pad.pose.notNull = true; | |
102 pad.pose.hasOrientation = true; | |
103 pad.pose.hasPosition = false; | |
104 | |
105 gvr_quatf orientation = controller_state_.GetOrientation(); | |
106 pad.pose.orientation.notNull = true; | |
107 pad.pose.orientation.x = orientation.qx; | |
108 pad.pose.orientation.y = orientation.qy; | |
109 pad.pose.orientation.z = orientation.qz; | |
110 pad.pose.orientation.w = orientation.qw; | |
111 } | |
112 | |
113 void GvrGamepadDataFetcher::PauseHint(bool paused) { | |
114 if (!controller_api_) | |
115 return; | |
116 | |
117 if (paused) { | |
118 controller_api_->Pause(); | |
119 } else { | |
120 controller_api_->Resume(); | |
121 } | |
122 } | |
123 | |
124 } // namespace device | |
OLD | NEW |