Chromium Code Reviews| Index: device/gamepad/public/interfaces/gamepad_struct_traits.h |
| diff --git a/device/gamepad/public/interfaces/gamepad_struct_traits.h b/device/gamepad/public/interfaces/gamepad_struct_traits.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5721e877e130f8eac82834bd7247035f684e9fe9 |
| --- /dev/null |
| +++ b/device/gamepad/public/interfaces/gamepad_struct_traits.h |
| @@ -0,0 +1,135 @@ |
| +// 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. |
| + |
| +#ifndef DEVICE_GAMEPAD_PUBLIC_INTERFACES_GAMEPAD_STRUCT_TRAITS_H_ |
| +#define DEVICE_GAMEPAD_PUBLIC_INTERFACES_GAMEPAD_STRUCT_TRAITS_H_ |
| + |
| +#include <stddef.h> |
| + |
| +#include "device/gamepad/public/interfaces/gamepad.mojom.h" |
| +#include "mojo/public/cpp/bindings/array_traits_carray.h" |
| +#include "mojo/public/cpp/bindings/struct_traits.h" |
| +#include "third_party/WebKit/public/platform/WebGamepad.h" |
| + |
| +namespace mojo { |
| + |
| +template <> |
| +struct StructTraits<device::mojom::GamepadQuaternionDataView, |
| + blink::WebGamepadQuaternion> { |
| + static bool IsNull(const blink::WebGamepadQuaternion& r) { |
| + return !r.notNull; |
| + } |
| + static void SetToNull(blink::WebGamepadQuaternion* out); |
| + static float x(const blink::WebGamepadQuaternion& r) { return r.x; } |
| + static float y(const blink::WebGamepadQuaternion& r) { return r.y; } |
| + static float z(const blink::WebGamepadQuaternion& r) { return r.z; } |
| + static float w(const blink::WebGamepadQuaternion& r) { return r.w; } |
| + static bool Read(device::mojom::GamepadQuaternionDataView data, |
| + blink::WebGamepadQuaternion* out); |
| +}; |
| + |
| +template <> |
| +struct StructTraits<device::mojom::GamepadVectorDataView, |
| + blink::WebGamepadVector> { |
| + static bool IsNull(const blink::WebGamepadVector& r) { return !r.notNull; } |
| + static void SetToNull(blink::WebGamepadVector* out); |
| + static float x(const blink::WebGamepadVector& r) { return r.x; } |
| + static float y(const blink::WebGamepadVector& r) { return r.y; } |
| + static float z(const blink::WebGamepadVector& r) { return r.z; } |
| + static bool Read(device::mojom::GamepadVectorDataView data, |
| + blink::WebGamepadVector* out); |
| +}; |
| + |
| +template <> |
| +struct StructTraits<device::mojom::GamepadButtonDataView, |
| + blink::WebGamepadButton> { |
| + static bool pressed(const blink::WebGamepadButton& r) { return r.pressed; } |
| + static bool touched(const blink::WebGamepadButton& r) { return r.touched; } |
| + static double value(const blink::WebGamepadButton& r) { return r.value; } |
| + static bool Read(device::mojom::GamepadButtonDataView data, |
| + blink::WebGamepadButton* out); |
| +}; |
| + |
| +template <> |
| +struct StructTraits<device::mojom::GamepadPoseDataView, blink::WebGamepadPose> { |
| + static bool IsNull(const blink::WebGamepadPose& r) { return !r.notNull; } |
| + static void SetToNull(blink::WebGamepadPose* out); |
| + static const blink::WebGamepadQuaternion& orientation( |
| + const blink::WebGamepadPose& r) { |
| + return r.orientation; |
| + } |
| + static const blink::WebGamepadVector& position( |
| + const blink::WebGamepadPose& r) { |
| + return r.position; |
| + } |
| + static const blink::WebGamepadVector& angular_velocity( |
| + const blink::WebGamepadPose& r) { |
| + return r.angularVelocity; |
| + } |
| + static const blink::WebGamepadVector& linear_velocity( |
| + const blink::WebGamepadPose& r) { |
| + return r.linearVelocity; |
| + } |
| + static const blink::WebGamepadVector& angular_acceleration( |
| + const blink::WebGamepadPose& r) { |
| + return r.angularAcceleration; |
| + } |
| + static const blink::WebGamepadVector& linear_acceleration( |
| + const blink::WebGamepadPose& r) { |
| + return r.linearAcceleration; |
| + } |
| + static bool Read(device::mojom::GamepadPoseDataView data, |
| + blink::WebGamepadPose* out); |
| +}; |
| + |
| +template <> |
| +struct EnumTraits<device::mojom::GamepadHand, blink::WebGamepadHand> { |
| + static device::mojom::GamepadHand ToMojom(blink::WebGamepadHand input); |
| + static bool FromMojom(device::mojom::GamepadHand input, |
| + blink::WebGamepadHand* output); |
| +}; |
| + |
| +template <> |
| +struct StructTraits<device::mojom::GamepadDataView, blink::WebGamepad> { |
| + static bool connected(const blink::WebGamepad& r) { return r.connected; } |
| + static CArray<uint16_t> id(const blink::WebGamepad& r) { |
| + return { |
| + blink::WebGamepad::idLengthCap, blink::WebGamepad::idLengthCap, |
| + reinterpret_cast<uint16_t*>(const_cast<blink::WebUChar*>(&r.id[0]))}; |
|
yzshen1
2016/11/15 19:17:33
You could return a ConstCArray, that way you don't
ke.he
2016/11/16 09:51:41
Done.
|
| + } |
| + static uint64_t timestamp(const blink::WebGamepad& r) { return r.timestamp; } |
| + static uint32_t axes_length(const blink::WebGamepad& r) { |
| + return r.axesLength; |
| + } |
| + static CArray<double> axes(const blink::WebGamepad& r) { |
| + return {blink::WebGamepad::axesLengthCap, blink::WebGamepad::axesLengthCap, |
| + const_cast<double*>(&r.axes[0])}; |
| + } |
| + static uint32_t buttons_length(const blink::WebGamepad& r) { |
| + return r.buttonsLength; |
| + } |
| + static CArray<blink::WebGamepadButton> buttons(const blink::WebGamepad& r) { |
| + return {blink::WebGamepad::buttonsLengthCap, |
| + blink::WebGamepad::buttonsLengthCap, |
| + const_cast<blink::WebGamepadButton*>(&r.buttons[0])}; |
| + } |
| + static CArray<uint16_t> mapping(const blink::WebGamepad& r) { |
| + return {blink::WebGamepad::mappingLengthCap, |
| + blink::WebGamepad::mappingLengthCap, |
| + reinterpret_cast<uint16_t*>( |
| + const_cast<blink::WebUChar*>(&r.mapping[0]))}; |
| + } |
| + static const blink::WebGamepadPose& pose(const blink::WebGamepad& r) { |
| + return r.pose; |
| + } |
| + static const blink::WebGamepadHand& hand(const blink::WebGamepad& r) { |
| + return r.hand; |
| + } |
| + static uint32_t display_id(const blink::WebGamepad& r) { return r.displayId; } |
| + static bool Read(device::mojom::GamepadDataView data, blink::WebGamepad* out); |
| +}; |
| + |
| +} // namespace mojo |
| + |
| +#endif // DEVICE_GAMEPAD_PUBLIC_INTERFACES_GAMEPAD_STRUCT_TRAITS_H_ |