Chromium Code Reviews| Index: device/gamepad/public/interfaces/gamepad_struct_traits.cc |
| diff --git a/device/gamepad/public/interfaces/gamepad_struct_traits.cc b/device/gamepad/public/interfaces/gamepad_struct_traits.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e080dd723150afaf4c60b03eed3ae263d9dc7155 |
| --- /dev/null |
| +++ b/device/gamepad/public/interfaces/gamepad_struct_traits.cc |
| @@ -0,0 +1,214 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "device/gamepad/public/interfaces/gamepad_struct_traits.h" |
| + |
| +namespace mojo { |
| + |
| +// static |
| +void StructTraits< |
| + device::mojom::GamepadQuaternionDataView, |
| + blink::WebGamepadQuaternion>::SetToNull(blink::WebGamepadQuaternion* out) { |
| + memset(out, 0, sizeof(blink::WebGamepadQuaternion)); |
| + out->notNull = false; |
| +} |
| + |
| +// static |
| +bool StructTraits<device::mojom::GamepadQuaternionDataView, |
| + blink::WebGamepadQuaternion>:: |
| + Read(device::mojom::GamepadQuaternionDataView data, |
| + blink::WebGamepadQuaternion* out) { |
| + memset(out, 0, sizeof(blink::WebGamepadQuaternion)); |
|
yzshen1
2016/11/16 19:20:18
why is this needed?
ke.he
2016/11/17 05:51:48
Done.
|
| + out->notNull = true; |
| + out->x = data.x(); |
| + out->y = data.y(); |
| + out->z = data.z(); |
| + out->w = data.w(); |
| + return true; |
| +} |
| + |
| +// static |
| +void StructTraits<device::mojom::GamepadVectorDataView, |
| + blink::WebGamepadVector>::SetToNull(blink::WebGamepadVector* |
| + out) { |
| + memset(out, 0, sizeof(blink::WebGamepadVector)); |
| + out->notNull = false; |
| +} |
| + |
| +// static |
| +bool StructTraits< |
| + device::mojom::GamepadVectorDataView, |
| + blink::WebGamepadVector>::Read(device::mojom::GamepadVectorDataView data, |
| + blink::WebGamepadVector* out) { |
| + memset(out, 0, sizeof(blink::WebGamepadVector)); |
|
yzshen1
2016/11/16 19:20:18
ditto
ke.he
2016/11/17 05:51:48
Done.
|
| + out->notNull = true; |
| + out->x = data.x(); |
| + out->y = data.y(); |
| + out->z = data.z(); |
| + return true; |
| +} |
| + |
| +// static |
| +bool StructTraits< |
| + device::mojom::GamepadButtonDataView, |
| + blink::WebGamepadButton>::Read(device::mojom::GamepadButtonDataView data, |
| + blink::WebGamepadButton* out) { |
| + memset(out, 0, sizeof(blink::WebGamepadButton)); |
|
yzshen1
2016/11/16 19:20:18
ditto
ke.he
2016/11/17 05:51:48
Done.
|
| + out->pressed = data.pressed(); |
| + out->touched = data.touched(); |
| + out->value = data.value(); |
| + return true; |
| +} |
| + |
| +// static |
| +void StructTraits<device::mojom::GamepadPoseDataView, |
| + blink::WebGamepadPose>::SetToNull(blink::WebGamepadPose* |
| + out) { |
| + memset(out, 0, sizeof(blink::WebGamepadPose)); |
| + out->notNull = false; |
| +} |
| + |
| +// static |
| +bool StructTraits<device::mojom::GamepadPoseDataView, blink::WebGamepadPose>:: |
| + Read(device::mojom::GamepadPoseDataView data, blink::WebGamepadPose* out) { |
| + memset(out, 0, sizeof(blink::WebGamepadPose)); |
|
yzshen1
2016/11/16 19:20:18
ditto
ke.he
2016/11/17 05:51:49
Done.
|
| + out->notNull = true; |
| + if (!data.ReadOrientation(&out->orientation)) { |
| + return false; |
| + } |
| + out->hasOrientation = out->orientation.notNull; |
| + |
| + if (!data.ReadPosition(&out->position)) { |
| + return false; |
| + } |
| + out->hasPosition = out->position.notNull; |
| + |
| + if (!data.ReadAngularVelocity(&out->angularVelocity)) { |
| + return false; |
| + } |
| + if (!data.ReadLinearVelocity(&out->linearVelocity)) { |
| + return false; |
| + } |
| + if (!data.ReadAngularAcceleration(&out->angularAcceleration)) { |
| + return false; |
| + } |
| + if (!data.ReadLinearAcceleration(&out->linearAcceleration)) { |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| +// static |
| +device::mojom::GamepadHand |
| +EnumTraits<device::mojom::GamepadHand, blink::WebGamepadHand>::ToMojom( |
| + blink::WebGamepadHand input) { |
| + switch (input) { |
| + case blink::WebGamepadHand::GamepadHandNone: |
| + return device::mojom::GamepadHand::GamepadHandNone; |
| + case blink::WebGamepadHand::GamepadHandLeft: |
| + return device::mojom::GamepadHand::GamepadHandLeft; |
| + case blink::WebGamepadHand::GamepadHandRight: |
| + return device::mojom::GamepadHand::GamepadHandRight; |
| + } |
| + |
| + NOTREACHED(); |
| + return device::mojom::GamepadHand::GamepadHandNone; |
| +} |
| + |
| +// static |
| +bool EnumTraits<device::mojom::GamepadHand, blink::WebGamepadHand>::FromMojom( |
| + device::mojom::GamepadHand input, |
| + blink::WebGamepadHand* output) { |
| + switch (input) { |
| + case device::mojom::GamepadHand::GamepadHandNone: |
| + *output = blink::WebGamepadHand::GamepadHandNone; |
| + return true; |
| + case device::mojom::GamepadHand::GamepadHandLeft: |
| + *output = blink::WebGamepadHand::GamepadHandLeft; |
| + return true; |
| + case device::mojom::GamepadHand::GamepadHandRight: |
| + *output = blink::WebGamepadHand::GamepadHandRight; |
| + return true; |
| + } |
| + |
| + NOTREACHED(); |
| + return false; |
| +} |
| + |
| +// static |
| +ConstCArray<uint16_t> |
| +StructTraits<device::mojom::GamepadDataView, blink::WebGamepad>::id( |
| + const blink::WebGamepad& r) { |
| + size_t id_length = 0; |
| + while (r.id[id_length] != 0 && id_length < blink::WebGamepad::idLengthCap) { |
|
yzshen1
2016/11/16 19:20:18
You need to swap the position of the two condition
ke.he
2016/11/17 05:51:48
blundell@, I think it is correct way to check the
blundell
2016/11/17 14:55:41
I'm not a domain expert on Gamepad either, but thi
|
| + id_length++; |
| + } |
| + return {id_length, reinterpret_cast<const uint16_t*>(&r.id[0])}; |
| +} |
| + |
| +// static |
| +ConstCArray<uint16_t> |
| +StructTraits<device::mojom::GamepadDataView, blink::WebGamepad>::mapping( |
| + const blink::WebGamepad& r) { |
| + size_t mapping_length = 0; |
| + while (r.mapping[mapping_length] != 0 && |
|
yzshen1
2016/11/16 19:20:18
ditto
ke.he
2016/11/17 05:51:48
Done.
|
| + mapping_length < blink::WebGamepad::mappingLengthCap) { |
| + mapping_length++; |
| + } |
| + return {mapping_length, reinterpret_cast<const uint16_t*>(&r.mapping[0])}; |
| +} |
| + |
| +// static |
| +bool StructTraits<device::mojom::GamepadDataView, blink::WebGamepad>::Read( |
| + device::mojom::GamepadDataView data, |
| + blink::WebGamepad* out) { |
| + memset(out, 0, sizeof(blink::WebGamepad)); |
|
yzshen1
2016/11/16 19:20:18
Does it make sense to only memset those necessary
ke.he
2016/11/17 05:51:48
Yes, it make sense to only memset necessary fields
|
| + out->connected = data.connected(); |
| + |
| + CArray<uint16_t> id = {0, blink::WebGamepad::idLengthCap, |
| + reinterpret_cast<uint16_t*>(&out->id[0])}; |
| + if (!data.ReadId(&id)) { |
| + return false; |
| + } |
| + |
| + out->timestamp = data.timestamp(); |
| + |
| + CArray<double> axes = {0, blink::WebGamepad::axesLengthCap, &out->axes[0]}; |
| + if (!data.ReadAxes(&axes)) { |
| + return false; |
| + } |
| + // static_cast is safe when "data.ReadAxes(&axes)" above returns true. |
| + out->axesLength = static_cast<unsigned>(axes.size); |
| + |
| + CArray<blink::WebGamepadButton> buttons = { |
| + 0, blink::WebGamepad::buttonsLengthCap, &out->buttons[0]}; |
| + if (!data.ReadButtons(&buttons)) { |
| + return false; |
| + } |
| + // static_cast is safe when "data.ReadButtons(&buttons)" above returns true. |
| + out->buttonsLength = static_cast<unsigned>(buttons.size); |
| + |
| + CArray<uint16_t> mapping = {0, blink::WebGamepad::mappingLengthCap, |
| + reinterpret_cast<uint16_t*>(&out->mapping[0])}; |
| + |
| + if (!data.ReadMapping(&mapping)) { |
| + return false; |
| + } |
| + |
| + if (!data.ReadPose(&out->pose)) { |
| + return false; |
| + } |
| + |
| + blink::WebGamepadHand hand; |
| + if (!data.ReadHand(&hand)) { |
| + return false; |
| + } |
| + out->hand = hand; |
| + |
| + out->displayId = data.display_id(); |
| + |
| + return true; |
| +} |
| + |
| +} // namespace mojo |