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..1cbbb7dea2715379b3606adb46701885d2ab3fb0 |
| --- /dev/null |
| +++ b/device/gamepad/public/interfaces/gamepad_struct_traits.cc |
| @@ -0,0 +1,178 @@ |
| +// 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::WebGamepadPose)); |
|
yzshen1
2016/11/15 19:17:33
Should it be sizeof(blink::WebGamepadQuaternion)?
ke.he
2016/11/16 09:51:41
Done.
|
| + out->notNull = false; |
| +} |
| + |
| +// static |
| +bool StructTraits<device::mojom::GamepadQuaternionDataView, |
| + blink::WebGamepadQuaternion>:: |
| + Read(device::mojom::GamepadQuaternionDataView data, |
| + blink::WebGamepadQuaternion* out) { |
| + 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) { |
| + 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) { |
| + 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) { |
| + 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 |
| +bool StructTraits<device::mojom::GamepadDataView, blink::WebGamepad>::Read( |
| + device::mojom::GamepadDataView data, |
| + blink::WebGamepad* out) { |
| + 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(); |
| + out->axesLength = data.axes_length(); |
| + 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.
|
| + return false; |
| + } |
| + |
| + CArray<double> axes = {0, blink::WebGamepad::axesLengthCap, &out->axes[0]}; |
| + if (!data.ReadAxes(&axes)) |
| + return false; |
| + |
| + out->buttonsLength = data.buttons_length(); |
| + if (out->buttonsLength > blink::WebGamepad::buttonsLengthCap) { |
| + return false; |
| + } |
| + |
| + CArray<blink::WebGamepadButton> buttons = { |
| + 0, blink::WebGamepad::buttonsLengthCap, &out->buttons[0]}; |
| + if (!data.ReadButtons(&buttons)) |
| + return false; |
| + |
| + 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 |