OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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/cardboard_gamepad_data_fetcher.h" |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "device/gamepad/public/cpp/gamepads.h" |
| 10 #include "device/vr/android/gvr/cardboard_gamepad_data_provider.h" |
| 11 #include "device/vr/vr_types.h" |
| 12 |
| 13 namespace device { |
| 14 |
| 15 namespace { |
| 16 |
| 17 void CopyToUString(UChar* dest, size_t dest_length, base::string16 src) { |
| 18 static_assert(sizeof(base::string16::value_type) == sizeof(UChar), |
| 19 "Mismatched string16/WebUChar size."); |
| 20 |
| 21 const size_t str_to_copy = std::min(src.size(), dest_length - 1); |
| 22 memcpy(dest, src.data(), str_to_copy * sizeof(base::string16::value_type)); |
| 23 dest[str_to_copy] = 0; |
| 24 } |
| 25 |
| 26 } // namespace |
| 27 |
| 28 CardboardGamepadDataFetcher::Factory::Factory( |
| 29 CardboardGamepadDataProvider* data_provider, |
| 30 unsigned int display_id) |
| 31 : data_provider_(data_provider), display_id_(display_id) { |
| 32 DVLOG(1) << __FUNCTION__ << "=" << this; |
| 33 } |
| 34 |
| 35 CardboardGamepadDataFetcher::Factory::~Factory() { |
| 36 DVLOG(1) << __FUNCTION__ << "=" << this; |
| 37 } |
| 38 |
| 39 std::unique_ptr<GamepadDataFetcher> |
| 40 CardboardGamepadDataFetcher::Factory::CreateDataFetcher() { |
| 41 return base::MakeUnique<CardboardGamepadDataFetcher>(data_provider_, |
| 42 display_id_); |
| 43 } |
| 44 |
| 45 GamepadSource CardboardGamepadDataFetcher::Factory::source() { |
| 46 return GAMEPAD_SOURCE_CARDBOARD; |
| 47 } |
| 48 |
| 49 CardboardGamepadDataFetcher::CardboardGamepadDataFetcher( |
| 50 CardboardGamepadDataProvider* data_provider, |
| 51 unsigned int display_id) |
| 52 : display_id_(display_id) { |
| 53 // Called on UI thread. |
| 54 DVLOG(1) << __FUNCTION__ << "=" << this; |
| 55 data_provider->RegisterCardboardGamepadDataFetcher(this); |
| 56 } |
| 57 |
| 58 CardboardGamepadDataFetcher::~CardboardGamepadDataFetcher() { |
| 59 DVLOG(1) << __FUNCTION__ << "=" << this; |
| 60 } |
| 61 |
| 62 GamepadSource CardboardGamepadDataFetcher::source() { |
| 63 return GAMEPAD_SOURCE_CARDBOARD; |
| 64 } |
| 65 |
| 66 void CardboardGamepadDataFetcher::OnAddedToProvider() { |
| 67 PauseHint(false); |
| 68 } |
| 69 |
| 70 void CardboardGamepadDataFetcher::SetGamepadData(CardboardGamepadData data) { |
| 71 // Called from UI thread. |
| 72 gamepad_data_ = data; |
| 73 } |
| 74 |
| 75 void CardboardGamepadDataFetcher::GetGamepadData(bool devices_changed_hint) { |
| 76 // Called from gamepad polling thread. |
| 77 PadState* state = GetPadState(0); |
| 78 if (!state) |
| 79 return; |
| 80 |
| 81 CardboardGamepadData provided_data = gamepad_data_; |
| 82 |
| 83 Gamepad& pad = state->data; |
| 84 if (state->active_state == GAMEPAD_NEWLY_ACTIVE) { |
| 85 // This is the first time we've seen this device, so do some one-time |
| 86 // initialization |
| 87 pad.connected = true; |
| 88 CopyToUString(pad.id, Gamepad::kIdLengthCap, |
| 89 base::UTF8ToUTF16("Cardboard Button")); |
| 90 CopyToUString(pad.mapping, Gamepad::kMappingLengthCap, |
| 91 base::UTF8ToUTF16("")); |
| 92 pad.buttons_length = 1; |
| 93 pad.axes_length = 0; |
| 94 |
| 95 pad.display_id = display_id_; |
| 96 |
| 97 pad.hand = GamepadHand::kNone; |
| 98 } |
| 99 |
| 100 pad.timestamp = provided_data.timestamp; |
| 101 |
| 102 bool pressed = provided_data.is_screen_touching; |
| 103 pad.buttons[0].touched = pressed; |
| 104 pad.buttons[0].pressed = pressed; |
| 105 pad.buttons[0].value = pressed ? 1.0f : 0.0f; |
| 106 pad.pose.not_null = false; |
| 107 } |
| 108 |
| 109 void CardboardGamepadDataFetcher::PauseHint(bool paused) {} |
| 110 |
| 111 } // namespace device |
OLD | NEW |