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

Side by Side Diff: device/gamepad/public/interfaces/gamepad_struct_traits.cc

Issue 2492183002: Add struct_traits and typemap for blink::WebGamepad (Closed)
Patch Set: use nullable feature Created 4 years, 1 month 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
(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/gamepad/public/interfaces/gamepad_struct_traits.h"
6
7 namespace mojo {
8
9 // static
10 void StructTraits<
11 device::mojom::GamepadQuaternionDataView,
12 blink::WebGamepadQuaternion>::SetToNull(blink::WebGamepadQuaternion* out) {
13 memset(out, 0, sizeof(blink::WebGamepadPose));
yzshen1 2016/11/15 19:17:33 Should it be sizeof(blink::WebGamepadQuaternion)?
ke.he 2016/11/16 09:51:41 Done.
14 out->notNull = false;
15 }
16
17 // static
18 bool StructTraits<device::mojom::GamepadQuaternionDataView,
19 blink::WebGamepadQuaternion>::
20 Read(device::mojom::GamepadQuaternionDataView data,
21 blink::WebGamepadQuaternion* out) {
22 out->notNull = true;
23 out->x = data.x();
24 out->y = data.y();
25 out->z = data.z();
26 out->w = data.w();
27 return true;
28 }
29
30 // static
31 void StructTraits<device::mojom::GamepadVectorDataView,
32 blink::WebGamepadVector>::SetToNull(blink::WebGamepadVector*
33 out) {
34 memset(out, 0, sizeof(blink::WebGamepadVector));
35 out->notNull = false;
36 }
37
38 // static
39 bool StructTraits<
40 device::mojom::GamepadVectorDataView,
41 blink::WebGamepadVector>::Read(device::mojom::GamepadVectorDataView data,
42 blink::WebGamepadVector* out) {
43 out->notNull = true;
44 out->x = data.x();
45 out->y = data.y();
46 out->z = data.z();
47 return true;
48 }
49
50 // static
51 bool StructTraits<
52 device::mojom::GamepadButtonDataView,
53 blink::WebGamepadButton>::Read(device::mojom::GamepadButtonDataView data,
54 blink::WebGamepadButton* out) {
55 out->pressed = data.pressed();
56 out->touched = data.touched();
57 out->value = data.value();
58 return true;
59 }
60
61 // static
62 void StructTraits<device::mojom::GamepadPoseDataView,
63 blink::WebGamepadPose>::SetToNull(blink::WebGamepadPose*
64 out) {
65 memset(out, 0, sizeof(blink::WebGamepadPose));
66 out->notNull = false;
67 }
68
69 // static
70 bool StructTraits<device::mojom::GamepadPoseDataView, blink::WebGamepadPose>::
71 Read(device::mojom::GamepadPoseDataView data, blink::WebGamepadPose* out) {
72 out->notNull = true;
73 if (!data.ReadOrientation(&out->orientation))
74 return false;
75 out->hasOrientation = out->orientation.notNull;
76
77 if (!data.ReadPosition(&out->position))
78 return false;
79 out->hasPosition = out->position.notNull;
80
81 if (!data.ReadAngularVelocity(&out->angularVelocity))
82 return false;
83 if (!data.ReadLinearVelocity(&out->linearVelocity))
84 return false;
85 if (!data.ReadAngularAcceleration(&out->angularAcceleration))
86 return false;
87 if (!data.ReadLinearAcceleration(&out->linearAcceleration))
88 return false;
89
90 return true;
91 }
92
93 // static
94 device::mojom::GamepadHand
95 EnumTraits<device::mojom::GamepadHand, blink::WebGamepadHand>::ToMojom(
96 blink::WebGamepadHand input) {
97 switch (input) {
98 case blink::WebGamepadHand::GamepadHandNone:
99 return device::mojom::GamepadHand::GamepadHandNone;
100 case blink::WebGamepadHand::GamepadHandLeft:
101 return device::mojom::GamepadHand::GamepadHandLeft;
102 case blink::WebGamepadHand::GamepadHandRight:
103 return device::mojom::GamepadHand::GamepadHandRight;
104 }
105
106 NOTREACHED();
107 return device::mojom::GamepadHand::GamepadHandNone;
108 }
109
110 // static
111 bool EnumTraits<device::mojom::GamepadHand, blink::WebGamepadHand>::FromMojom(
112 device::mojom::GamepadHand input,
113 blink::WebGamepadHand* output) {
114 switch (input) {
115 case device::mojom::GamepadHand::GamepadHandNone:
116 *output = blink::WebGamepadHand::GamepadHandNone;
117 return true;
118 case device::mojom::GamepadHand::GamepadHandLeft:
119 *output = blink::WebGamepadHand::GamepadHandLeft;
120 return true;
121 case device::mojom::GamepadHand::GamepadHandRight:
122 *output = blink::WebGamepadHand::GamepadHandRight;
123 return true;
124 }
125
126 NOTREACHED();
127 return false;
128 }
129
130 // static
131 bool StructTraits<device::mojom::GamepadDataView, blink::WebGamepad>::Read(
132 device::mojom::GamepadDataView data,
133 blink::WebGamepad* out) {
134 out->connected = data.connected();
135
136 CArray<uint16_t> id = {0, blink::WebGamepad::idLengthCap,
137 reinterpret_cast<uint16_t*>(&out->id[0])};
138 if (!data.ReadId(&id))
139 return false;
140
141 out->timestamp = data.timestamp();
142 out->axesLength = data.axes_length();
143 if (out->axesLength > blink::WebGamepad::axesLengthCap) {
yzshen1 2016/11/15 19:17:33 It is okay to have {} for one-line body, or omit i
ke.he 2016/11/16 09:51:41 Done.
144 return false;
145 }
146
147 CArray<double> axes = {0, blink::WebGamepad::axesLengthCap, &out->axes[0]};
148 if (!data.ReadAxes(&axes))
149 return false;
150
151 out->buttonsLength = data.buttons_length();
152 if (out->buttonsLength > blink::WebGamepad::buttonsLengthCap) {
153 return false;
154 }
155
156 CArray<blink::WebGamepadButton> buttons = {
157 0, blink::WebGamepad::buttonsLengthCap, &out->buttons[0]};
158 if (!data.ReadButtons(&buttons))
159 return false;
160
161 CArray<uint16_t> mapping = {0, blink::WebGamepad::mappingLengthCap,
162 reinterpret_cast<uint16_t*>(&out->mapping[0])};
163 if (!data.ReadMapping(&mapping))
164 return false;
165 if (!data.ReadPose(&out->pose))
166 return false;
167
168 blink::WebGamepadHand hand;
169 if (!data.ReadHand(&hand))
170 return false;
171 out->hand = hand;
172
173 out->displayId = data.display_id();
174
175 return true;
176 }
177
178 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698