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

Side by Side Diff: device/gamepad/gamepad_pad_state_provider.cc

Issue 2808093006: [Device Service] Move Gamepad Blink headers to be part of the Gamepad client library (Closed)
Patch Set: rebase and address comments Created 3 years, 8 months 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "device/gamepad/gamepad_pad_state_provider.h" 5 #include "device/gamepad/gamepad_pad_state_provider.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 8
9 #include "device/gamepad/gamepad_data_fetcher.h" 9 #include "device/gamepad/gamepad_data_fetcher.h"
10 #include "third_party/WebKit/public/platform/WebGamepads.h" 10 #include "device/gamepad/public/cpp/gamepads.h"
11
12 using blink::WebGamepad;
13 using blink::WebGamepads;
14 11
15 namespace device { 12 namespace device {
16 13
17 namespace { 14 namespace {
18 15
19 const float kMinAxisResetValue = 0.1f; 16 const float kMinAxisResetValue = 0.1f;
20 17
21 } // namespace 18 } // namespace
22 19
23 GamepadPadStateProvider::GamepadPadStateProvider() { 20 GamepadPadStateProvider::GamepadPadStateProvider() {
24 pad_states_.reset(new PadState[WebGamepads::kItemsLengthCap]); 21 pad_states_.reset(new PadState[Gamepads::kItemsLengthCap]);
25 22
26 for (unsigned i = 0; i < WebGamepads::kItemsLengthCap; ++i) 23 for (unsigned i = 0; i < Gamepads::kItemsLengthCap; ++i)
27 ClearPadState(pad_states_.get()[i]); 24 ClearPadState(pad_states_.get()[i]);
28 } 25 }
29 26
30 GamepadPadStateProvider::~GamepadPadStateProvider() {} 27 GamepadPadStateProvider::~GamepadPadStateProvider() {}
31 28
32 PadState* GamepadPadStateProvider::GetPadState(GamepadSource source, 29 PadState* GamepadPadStateProvider::GetPadState(GamepadSource source,
33 int source_id) { 30 int source_id) {
34 // Check to see if the device already has a reserved slot 31 // Check to see if the device already has a reserved slot
35 PadState* empty_slot = nullptr; 32 PadState* empty_slot = nullptr;
36 for (size_t i = 0; i < WebGamepads::kItemsLengthCap; ++i) { 33 for (size_t i = 0; i < Gamepads::kItemsLengthCap; ++i) {
37 PadState& state = pad_states_.get()[i]; 34 PadState& state = pad_states_.get()[i];
38 if (state.source == source && state.source_id == source_id) { 35 if (state.source == source && state.source_id == source_id) {
39 // Retrieving the pad state marks this gamepad as active. 36 // Retrieving the pad state marks this gamepad as active.
40 state.active_state = GAMEPAD_ACTIVE; 37 state.active_state = GAMEPAD_ACTIVE;
41 return &state; 38 return &state;
42 } 39 }
43 if (!empty_slot && state.source == GAMEPAD_SOURCE_NONE) 40 if (!empty_slot && state.source == GAMEPAD_SOURCE_NONE)
44 empty_slot = &state; 41 empty_slot = &state;
45 } 42 }
46 if (empty_slot) { 43 if (empty_slot) {
47 empty_slot->source = source; 44 empty_slot->source = source;
48 empty_slot->source_id = source_id; 45 empty_slot->source_id = source_id;
49 empty_slot->active_state = GAMEPAD_NEWLY_ACTIVE; 46 empty_slot->active_state = GAMEPAD_NEWLY_ACTIVE;
50 } 47 }
51 return empty_slot; 48 return empty_slot;
52 } 49 }
53 50
54 void GamepadPadStateProvider::ClearPadState(PadState& state) { 51 void GamepadPadStateProvider::ClearPadState(PadState& state) {
55 memset(&state, 0, sizeof(PadState)); 52 memset(&state, 0, sizeof(PadState));
56 } 53 }
57 54
58 void GamepadPadStateProvider::InitializeDataFetcher( 55 void GamepadPadStateProvider::InitializeDataFetcher(
59 GamepadDataFetcher* fetcher) { 56 GamepadDataFetcher* fetcher) {
60 fetcher->InitializeProvider(this); 57 fetcher->InitializeProvider(this);
61 } 58 }
62 59
63 void GamepadPadStateProvider::MapAndSanitizeGamepadData(PadState* pad_state, 60 void GamepadPadStateProvider::MapAndSanitizeGamepadData(PadState* pad_state,
64 WebGamepad* pad, 61 Gamepad* pad,
65 bool sanitize) { 62 bool sanitize) {
66 DCHECK(pad_state); 63 DCHECK(pad_state);
67 DCHECK(pad); 64 DCHECK(pad);
68 65
69 if (!pad_state->data.connected) { 66 if (!pad_state->data.connected) {
70 memset(pad, 0, sizeof(WebGamepad)); 67 memset(pad, 0, sizeof(Gamepad));
71 return; 68 return;
72 } 69 }
73 70
74 // Copy the current state to the output buffer, using the mapping 71 // Copy the current state to the output buffer, using the mapping
75 // function, if there is one available. 72 // function, if there is one available.
76 if (pad_state->mapper) 73 if (pad_state->mapper)
77 pad_state->mapper(pad_state->data, pad); 74 pad_state->mapper(pad_state->data, pad);
78 else 75 else
79 *pad = pad_state->data; 76 *pad = pad_state->data;
80 77
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 } else { 114 } else {
118 pad->buttons[button].pressed = false; 115 pad->buttons[button].pressed = false;
119 pad->buttons[button].value = 0.0f; 116 pad->buttons[button].value = 0.0f;
120 } 117 }
121 } 118 }
122 } 119 }
123 } 120 }
124 } 121 }
125 122
126 } // namespace device 123 } // namespace device
OLDNEW
« no previous file with comments | « device/gamepad/gamepad_pad_state_provider.h ('k') | device/gamepad/gamepad_platform_data_fetcher_android.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698