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