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

Side by Side Diff: device/gamepad/public/interfaces/gamepad_struct_traits.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/public/interfaces/gamepad_struct_traits.h" 5 #include "device/gamepad/public/interfaces/gamepad_struct_traits.h"
6 6
7 namespace mojo { 7 namespace mojo {
8 8
9 // static 9 // static
10 void StructTraits< 10 void StructTraits<
11 device::mojom::GamepadQuaternionDataView, 11 device::mojom::GamepadQuaternionDataView,
12 blink::WebGamepadQuaternion>::SetToNull(blink::WebGamepadQuaternion* out) { 12 device::GamepadQuaternion>::SetToNull(device::GamepadQuaternion* out) {
13 memset(out, 0, sizeof(blink::WebGamepadQuaternion)); 13 memset(out, 0, sizeof(device::GamepadQuaternion));
14 out->not_null = false; 14 out->not_null = false;
15 } 15 }
16 16
17 // static 17 // static
18 bool StructTraits<device::mojom::GamepadQuaternionDataView, 18 bool StructTraits<device::mojom::GamepadQuaternionDataView,
19 blink::WebGamepadQuaternion>:: 19 device::GamepadQuaternion>::
20 Read(device::mojom::GamepadQuaternionDataView data, 20 Read(device::mojom::GamepadQuaternionDataView data,
21 blink::WebGamepadQuaternion* out) { 21 device::GamepadQuaternion* out) {
22 out->not_null = true; 22 out->not_null = true;
23 out->x = data.x(); 23 out->x = data.x();
24 out->y = data.y(); 24 out->y = data.y();
25 out->z = data.z(); 25 out->z = data.z();
26 out->w = data.w(); 26 out->w = data.w();
27 return true; 27 return true;
28 } 28 }
29 29
30 // static 30 // static
31 void StructTraits<device::mojom::GamepadVectorDataView, 31 void StructTraits<device::mojom::GamepadVectorDataView,
32 blink::WebGamepadVector>::SetToNull(blink::WebGamepadVector* 32 device::GamepadVector>::SetToNull(device::GamepadVector*
33 out) { 33 out) {
34 memset(out, 0, sizeof(blink::WebGamepadVector)); 34 memset(out, 0, sizeof(device::GamepadVector));
35 out->not_null = false; 35 out->not_null = false;
36 } 36 }
37 37
38 // static 38 // static
39 bool StructTraits< 39 bool StructTraits<device::mojom::GamepadVectorDataView, device::GamepadVector>::
40 device::mojom::GamepadVectorDataView, 40 Read(device::mojom::GamepadVectorDataView data,
41 blink::WebGamepadVector>::Read(device::mojom::GamepadVectorDataView data, 41 device::GamepadVector* out) {
42 blink::WebGamepadVector* out) {
43 out->not_null = true; 42 out->not_null = true;
44 out->x = data.x(); 43 out->x = data.x();
45 out->y = data.y(); 44 out->y = data.y();
46 out->z = data.z(); 45 out->z = data.z();
47 return true; 46 return true;
48 } 47 }
49 48
50 // static 49 // static
51 bool StructTraits< 50 bool StructTraits<device::mojom::GamepadButtonDataView, device::GamepadButton>::
52 device::mojom::GamepadButtonDataView, 51 Read(device::mojom::GamepadButtonDataView data,
53 blink::WebGamepadButton>::Read(device::mojom::GamepadButtonDataView data, 52 device::GamepadButton* out) {
54 blink::WebGamepadButton* out) {
55 out->pressed = data.pressed(); 53 out->pressed = data.pressed();
56 out->touched = data.touched(); 54 out->touched = data.touched();
57 out->value = data.value(); 55 out->value = data.value();
58 return true; 56 return true;
59 } 57 }
60 58
61 // static 59 // static
62 void StructTraits<device::mojom::GamepadPoseDataView, 60 void StructTraits<device::mojom::GamepadPoseDataView,
63 blink::WebGamepadPose>::SetToNull(blink::WebGamepadPose* 61 device::GamepadPose>::SetToNull(device::GamepadPose* out) {
64 out) { 62 memset(out, 0, sizeof(device::GamepadPose));
65 memset(out, 0, sizeof(blink::WebGamepadPose));
66 out->not_null = false; 63 out->not_null = false;
67 } 64 }
68 65
69 // static 66 // static
70 bool StructTraits<device::mojom::GamepadPoseDataView, blink::WebGamepadPose>:: 67 bool StructTraits<device::mojom::GamepadPoseDataView, device::GamepadPose>::
71 Read(device::mojom::GamepadPoseDataView data, blink::WebGamepadPose* out) { 68 Read(device::mojom::GamepadPoseDataView data, device::GamepadPose* out) {
72 out->not_null = true; 69 out->not_null = true;
73 if (!data.ReadOrientation(&out->orientation)) { 70 if (!data.ReadOrientation(&out->orientation)) {
74 return false; 71 return false;
75 } 72 }
76 out->has_orientation = out->orientation.not_null; 73 out->has_orientation = out->orientation.not_null;
77 74
78 if (!data.ReadPosition(&out->position)) { 75 if (!data.ReadPosition(&out->position)) {
79 return false; 76 return false;
80 } 77 }
81 out->has_position = out->position.not_null; 78 out->has_position = out->position.not_null;
82 79
83 if (!data.ReadAngularVelocity(&out->angular_velocity)) { 80 if (!data.ReadAngularVelocity(&out->angular_velocity)) {
84 return false; 81 return false;
85 } 82 }
86 if (!data.ReadLinearVelocity(&out->linear_velocity)) { 83 if (!data.ReadLinearVelocity(&out->linear_velocity)) {
87 return false; 84 return false;
88 } 85 }
89 if (!data.ReadAngularAcceleration(&out->angular_acceleration)) { 86 if (!data.ReadAngularAcceleration(&out->angular_acceleration)) {
90 return false; 87 return false;
91 } 88 }
92 if (!data.ReadLinearAcceleration(&out->linear_acceleration)) { 89 if (!data.ReadLinearAcceleration(&out->linear_acceleration)) {
93 return false; 90 return false;
94 } 91 }
95 return true; 92 return true;
96 } 93 }
97 94
98 // static 95 // static
99 device::mojom::GamepadHand 96 device::mojom::GamepadHand
100 EnumTraits<device::mojom::GamepadHand, blink::WebGamepadHand>::ToMojom( 97 EnumTraits<device::mojom::GamepadHand, device::GamepadHand>::ToMojom(
101 blink::WebGamepadHand input) { 98 device::GamepadHand input) {
102 switch (input) { 99 switch (input) {
103 case blink::WebGamepadHand::kGamepadHandNone: 100 case device::GamepadHand::kNone:
104 return device::mojom::GamepadHand::GamepadHandNone; 101 return device::mojom::GamepadHand::GamepadHandNone;
105 case blink::WebGamepadHand::kGamepadHandLeft: 102 case device::GamepadHand::kLeft:
106 return device::mojom::GamepadHand::GamepadHandLeft; 103 return device::mojom::GamepadHand::GamepadHandLeft;
107 case blink::WebGamepadHand::kGamepadHandRight: 104 case device::GamepadHand::kRight:
108 return device::mojom::GamepadHand::GamepadHandRight; 105 return device::mojom::GamepadHand::GamepadHandRight;
109 } 106 }
110 107
111 NOTREACHED(); 108 NOTREACHED();
112 return device::mojom::GamepadHand::GamepadHandNone; 109 return device::mojom::GamepadHand::GamepadHandNone;
113 } 110 }
114 111
115 // static 112 // static
116 bool EnumTraits<device::mojom::GamepadHand, blink::WebGamepadHand>::FromMojom( 113 bool EnumTraits<device::mojom::GamepadHand, device::GamepadHand>::FromMojom(
117 device::mojom::GamepadHand input, 114 device::mojom::GamepadHand input,
118 blink::WebGamepadHand* output) { 115 device::GamepadHand* output) {
119 switch (input) { 116 switch (input) {
120 case device::mojom::GamepadHand::GamepadHandNone: 117 case device::mojom::GamepadHand::GamepadHandNone:
121 *output = blink::WebGamepadHand::kGamepadHandNone; 118 *output = device::GamepadHand::kNone;
122 return true; 119 return true;
123 case device::mojom::GamepadHand::GamepadHandLeft: 120 case device::mojom::GamepadHand::GamepadHandLeft:
124 *output = blink::WebGamepadHand::kGamepadHandLeft; 121 *output = device::GamepadHand::kLeft;
125 return true; 122 return true;
126 case device::mojom::GamepadHand::GamepadHandRight: 123 case device::mojom::GamepadHand::GamepadHandRight:
127 *output = blink::WebGamepadHand::kGamepadHandRight; 124 *output = device::GamepadHand::kRight;
128 return true; 125 return true;
129 } 126 }
130 127
131 NOTREACHED(); 128 NOTREACHED();
132 return false; 129 return false;
133 } 130 }
134 131
135 // static 132 // static
136 ConstCArray<uint16_t> 133 ConstCArray<uint16_t>
137 StructTraits<device::mojom::GamepadDataView, blink::WebGamepad>::id( 134 StructTraits<device::mojom::GamepadDataView, device::Gamepad>::id(
138 const blink::WebGamepad& r) { 135 const device::Gamepad& r) {
139 size_t id_length = 0; 136 size_t id_length = 0;
140 while (id_length < blink::WebGamepad::kIdLengthCap && r.id[id_length] != 0) { 137 while (id_length < device::Gamepad::kIdLengthCap && r.id[id_length] != 0) {
141 id_length++; 138 id_length++;
142 } 139 }
143 return {id_length, reinterpret_cast<const uint16_t*>(&r.id[0])}; 140 return {id_length, reinterpret_cast<const uint16_t*>(&r.id[0])};
144 } 141 }
145 142
146 // static 143 // static
147 ConstCArray<uint16_t> 144 ConstCArray<uint16_t>
148 StructTraits<device::mojom::GamepadDataView, blink::WebGamepad>::mapping( 145 StructTraits<device::mojom::GamepadDataView, device::Gamepad>::mapping(
149 const blink::WebGamepad& r) { 146 const device::Gamepad& r) {
150 size_t mapping_length = 0; 147 size_t mapping_length = 0;
151 while (mapping_length < blink::WebGamepad::kMappingLengthCap && 148 while (mapping_length < device::Gamepad::kMappingLengthCap &&
152 r.mapping[mapping_length] != 0) { 149 r.mapping[mapping_length] != 0) {
153 mapping_length++; 150 mapping_length++;
154 } 151 }
155 return {mapping_length, reinterpret_cast<const uint16_t*>(&r.mapping[0])}; 152 return {mapping_length, reinterpret_cast<const uint16_t*>(&r.mapping[0])};
156 } 153 }
157 154
158 // static 155 // static
159 bool StructTraits<device::mojom::GamepadDataView, blink::WebGamepad>::Read( 156 bool StructTraits<device::mojom::GamepadDataView, device::Gamepad>::Read(
160 device::mojom::GamepadDataView data, 157 device::mojom::GamepadDataView data,
161 blink::WebGamepad* out) { 158 device::Gamepad* out) {
162 out->connected = data.connected(); 159 out->connected = data.connected();
163 160
164 memset(&out->id[0], 0, 161 memset(&out->id[0], 0, device::Gamepad::kIdLengthCap * sizeof(device::UChar));
165 blink::WebGamepad::kIdLengthCap * sizeof(blink::WebUChar)); 162 CArray<uint16_t> id = {0, device::Gamepad::kIdLengthCap,
166 CArray<uint16_t> id = {0, blink::WebGamepad::kIdLengthCap,
167 reinterpret_cast<uint16_t*>(&out->id[0])}; 163 reinterpret_cast<uint16_t*>(&out->id[0])};
168 if (!data.ReadId(&id)) { 164 if (!data.ReadId(&id)) {
169 return false; 165 return false;
170 } 166 }
171 167
172 out->timestamp = data.timestamp(); 168 out->timestamp = data.timestamp();
173 169
174 CArray<double> axes = {0, blink::WebGamepad::kAxesLengthCap, &out->axes[0]}; 170 CArray<double> axes = {0, device::Gamepad::kAxesLengthCap, &out->axes[0]};
175 if (!data.ReadAxes(&axes)) { 171 if (!data.ReadAxes(&axes)) {
176 return false; 172 return false;
177 } 173 }
178 // static_cast is safe when "data.ReadAxes(&axes)" above returns true. 174 // static_cast is safe when "data.ReadAxes(&axes)" above returns true.
179 out->axes_length = static_cast<unsigned>(axes.size); 175 out->axes_length = static_cast<unsigned>(axes.size);
180 176
181 CArray<blink::WebGamepadButton> buttons = { 177 CArray<device::GamepadButton> buttons = {
182 0, blink::WebGamepad::kButtonsLengthCap, &out->buttons[0]}; 178 0, device::Gamepad::kButtonsLengthCap, &out->buttons[0]};
183 if (!data.ReadButtons(&buttons)) { 179 if (!data.ReadButtons(&buttons)) {
184 return false; 180 return false;
185 } 181 }
186 // static_cast is safe when "data.ReadButtons(&buttons)" above returns true. 182 // static_cast is safe when "data.ReadButtons(&buttons)" above returns true.
187 out->buttons_length = static_cast<unsigned>(buttons.size); 183 out->buttons_length = static_cast<unsigned>(buttons.size);
188 184
189 memset(&out->mapping[0], 0, 185 memset(&out->mapping[0], 0,
190 blink::WebGamepad::kMappingLengthCap * sizeof(blink::WebUChar)); 186 device::Gamepad::kMappingLengthCap * sizeof(device::UChar));
191 CArray<uint16_t> mapping = {0, blink::WebGamepad::kMappingLengthCap, 187 CArray<uint16_t> mapping = {0, device::Gamepad::kMappingLengthCap,
192 reinterpret_cast<uint16_t*>(&out->mapping[0])}; 188 reinterpret_cast<uint16_t*>(&out->mapping[0])};
193 if (!data.ReadMapping(&mapping)) { 189 if (!data.ReadMapping(&mapping)) {
194 return false; 190 return false;
195 } 191 }
196 192
197 if (!data.ReadPose(&out->pose)) { 193 if (!data.ReadPose(&out->pose)) {
198 return false; 194 return false;
199 } 195 }
200 196
201 blink::WebGamepadHand hand; 197 device::GamepadHand hand;
202 if (!data.ReadHand(&hand)) { 198 if (!data.ReadHand(&hand)) {
203 return false; 199 return false;
204 } 200 }
205 out->hand = hand; 201 out->hand = hand;
206 202
207 out->display_id = data.display_id(); 203 out->display_id = data.display_id();
208 204
209 return true; 205 return true;
210 } 206 }
211 207
212 } // namespace mojo 208 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698