| OLD | NEW |
| (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::WebGamepadQuaternion)); |
| 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 } |
| 76 out->hasOrientation = out->orientation.notNull; |
| 77 |
| 78 if (!data.ReadPosition(&out->position)) { |
| 79 return false; |
| 80 } |
| 81 out->hasPosition = out->position.notNull; |
| 82 |
| 83 if (!data.ReadAngularVelocity(&out->angularVelocity)) { |
| 84 return false; |
| 85 } |
| 86 if (!data.ReadLinearVelocity(&out->linearVelocity)) { |
| 87 return false; |
| 88 } |
| 89 if (!data.ReadAngularAcceleration(&out->angularAcceleration)) { |
| 90 return false; |
| 91 } |
| 92 if (!data.ReadLinearAcceleration(&out->linearAcceleration)) { |
| 93 return false; |
| 94 } |
| 95 return true; |
| 96 } |
| 97 |
| 98 // static |
| 99 device::mojom::GamepadHand |
| 100 EnumTraits<device::mojom::GamepadHand, blink::WebGamepadHand>::ToMojom( |
| 101 blink::WebGamepadHand input) { |
| 102 switch (input) { |
| 103 case blink::WebGamepadHand::GamepadHandNone: |
| 104 return device::mojom::GamepadHand::GamepadHandNone; |
| 105 case blink::WebGamepadHand::GamepadHandLeft: |
| 106 return device::mojom::GamepadHand::GamepadHandLeft; |
| 107 case blink::WebGamepadHand::GamepadHandRight: |
| 108 return device::mojom::GamepadHand::GamepadHandRight; |
| 109 } |
| 110 |
| 111 NOTREACHED(); |
| 112 return device::mojom::GamepadHand::GamepadHandNone; |
| 113 } |
| 114 |
| 115 // static |
| 116 bool EnumTraits<device::mojom::GamepadHand, blink::WebGamepadHand>::FromMojom( |
| 117 device::mojom::GamepadHand input, |
| 118 blink::WebGamepadHand* output) { |
| 119 switch (input) { |
| 120 case device::mojom::GamepadHand::GamepadHandNone: |
| 121 *output = blink::WebGamepadHand::GamepadHandNone; |
| 122 return true; |
| 123 case device::mojom::GamepadHand::GamepadHandLeft: |
| 124 *output = blink::WebGamepadHand::GamepadHandLeft; |
| 125 return true; |
| 126 case device::mojom::GamepadHand::GamepadHandRight: |
| 127 *output = blink::WebGamepadHand::GamepadHandRight; |
| 128 return true; |
| 129 } |
| 130 |
| 131 NOTREACHED(); |
| 132 return false; |
| 133 } |
| 134 |
| 135 // static |
| 136 ConstCArray<uint16_t> |
| 137 StructTraits<device::mojom::GamepadDataView, blink::WebGamepad>::id( |
| 138 const blink::WebGamepad& r) { |
| 139 size_t id_length = 0; |
| 140 while (id_length < blink::WebGamepad::idLengthCap && r.id[id_length] != 0) { |
| 141 id_length++; |
| 142 } |
| 143 return {id_length, reinterpret_cast<const uint16_t*>(&r.id[0])}; |
| 144 } |
| 145 |
| 146 // static |
| 147 ConstCArray<uint16_t> |
| 148 StructTraits<device::mojom::GamepadDataView, blink::WebGamepad>::mapping( |
| 149 const blink::WebGamepad& r) { |
| 150 size_t mapping_length = 0; |
| 151 while (mapping_length < blink::WebGamepad::mappingLengthCap && |
| 152 r.mapping[mapping_length] != 0) { |
| 153 mapping_length++; |
| 154 } |
| 155 return {mapping_length, reinterpret_cast<const uint16_t*>(&r.mapping[0])}; |
| 156 } |
| 157 |
| 158 // static |
| 159 bool StructTraits<device::mojom::GamepadDataView, blink::WebGamepad>::Read( |
| 160 device::mojom::GamepadDataView data, |
| 161 blink::WebGamepad* out) { |
| 162 out->connected = data.connected(); |
| 163 |
| 164 memset(&out->id[0], 0, |
| 165 blink::WebGamepad::idLengthCap * sizeof(blink::WebUChar)); |
| 166 CArray<uint16_t> id = {0, blink::WebGamepad::idLengthCap, |
| 167 reinterpret_cast<uint16_t*>(&out->id[0])}; |
| 168 if (!data.ReadId(&id)) { |
| 169 return false; |
| 170 } |
| 171 |
| 172 out->timestamp = data.timestamp(); |
| 173 |
| 174 CArray<double> axes = {0, blink::WebGamepad::axesLengthCap, &out->axes[0]}; |
| 175 if (!data.ReadAxes(&axes)) { |
| 176 return false; |
| 177 } |
| 178 // static_cast is safe when "data.ReadAxes(&axes)" above returns true. |
| 179 out->axesLength = static_cast<unsigned>(axes.size); |
| 180 |
| 181 CArray<blink::WebGamepadButton> buttons = { |
| 182 0, blink::WebGamepad::buttonsLengthCap, &out->buttons[0]}; |
| 183 if (!data.ReadButtons(&buttons)) { |
| 184 return false; |
| 185 } |
| 186 // static_cast is safe when "data.ReadButtons(&buttons)" above returns true. |
| 187 out->buttonsLength = static_cast<unsigned>(buttons.size); |
| 188 |
| 189 memset(&out->mapping[0], 0, |
| 190 blink::WebGamepad::mappingLengthCap * sizeof(blink::WebUChar)); |
| 191 CArray<uint16_t> mapping = {0, blink::WebGamepad::mappingLengthCap, |
| 192 reinterpret_cast<uint16_t*>(&out->mapping[0])}; |
| 193 if (!data.ReadMapping(&mapping)) { |
| 194 return false; |
| 195 } |
| 196 |
| 197 if (!data.ReadPose(&out->pose)) { |
| 198 return false; |
| 199 } |
| 200 |
| 201 blink::WebGamepadHand hand; |
| 202 if (!data.ReadHand(&hand)) { |
| 203 return false; |
| 204 } |
| 205 out->hand = hand; |
| 206 |
| 207 out->displayId = data.display_id(); |
| 208 |
| 209 return true; |
| 210 } |
| 211 |
| 212 } // namespace mojo |
| OLD | NEW |