| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef UI_EVENTS_DEVICES_MOJO_INPUT_DEVICE_STRUCT_TRAITS_H_ | 5 #ifndef UI_EVENTS_DEVICES_MOJO_INPUT_DEVICE_STRUCT_TRAITS_H_ |
| 6 #define UI_EVENTS_DEVICES_MOJO_INPUT_DEVICE_STRUCT_TRAITS_H_ | 6 #define UI_EVENTS_DEVICES_MOJO_INPUT_DEVICE_STRUCT_TRAITS_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
| 11 #include "ui/events/devices/input_device.h" | 11 #include "ui/events/devices/input_device.h" |
| 12 #include "ui/events/devices/mojo/input_devices.mojom.h" | 12 #include "ui/events/devices/mojo/input_devices.mojom.h" |
| 13 #include "ui/events/devices/touchscreen_device.h" | 13 #include "ui/events/devices/touchscreen_device.h" |
| 14 #include "ui/gfx/geometry/size.h" | 14 #include "ui/gfx/geometry/size.h" |
| 15 | 15 |
| 16 namespace mojo { | 16 namespace mojo { |
| 17 | 17 |
| 18 template <> | 18 template <> |
| 19 struct EnumTraits<ui::mojom::InputDeviceType, ui::InputDeviceType> { |
| 20 static ui::mojom::InputDeviceType ToMojom(ui::InputDeviceType type); |
| 21 static bool FromMojom(ui::mojom::InputDeviceType type, |
| 22 ui::InputDeviceType* output); |
| 23 }; |
| 24 |
| 25 template <> |
| 19 struct StructTraits<ui::mojom::InputDevice, ui::InputDevice> { | 26 struct StructTraits<ui::mojom::InputDevice, ui::InputDevice> { |
| 20 static int32_t id(const ui::InputDevice& device) { return device.id; } | 27 static int32_t id(const ui::InputDevice& device) { return device.id; } |
| 21 | 28 |
| 22 static ui::mojom::InputDeviceType type(const ui::InputDevice& device) { | 29 static ui::InputDeviceType type(const ui::InputDevice& device) { |
| 23 switch (device.type) { | 30 return device.type; |
| 24 case ui::INPUT_DEVICE_INTERNAL: | |
| 25 return ui::mojom::InputDeviceType::INPUT_DEVICE_INTERNAL; | |
| 26 case ui::INPUT_DEVICE_EXTERNAL: | |
| 27 return ui::mojom::InputDeviceType::INPUT_DEVICE_EXTERNAL; | |
| 28 case ui::INPUT_DEVICE_UNKNOWN: | |
| 29 return ui::mojom::InputDeviceType::INPUT_DEVICE_UNKNOWN; | |
| 30 } | |
| 31 } | 31 } |
| 32 | 32 |
| 33 static const std::string& name(const ui::InputDevice& device) { | 33 static const std::string& name(const ui::InputDevice& device) { |
| 34 return device.name; | 34 return device.name; |
| 35 } | 35 } |
| 36 | 36 |
| 37 static base::StringPiece sys_path(const ui::InputDevice& device) { | 37 static base::StringPiece sys_path(const ui::InputDevice& device) { |
| 38 return device.sys_path.AsUTF8Unsafe(); | 38 return device.sys_path.AsUTF8Unsafe(); |
| 39 } | 39 } |
| 40 | 40 |
| 41 static uint32_t vendor_id(const ui::InputDevice& device) { | 41 static uint32_t vendor_id(const ui::InputDevice& device) { |
| 42 return device.vendor_id; | 42 return device.vendor_id; |
| 43 } | 43 } |
| 44 | 44 |
| 45 static uint32_t product_id(const ui::InputDevice& device) { | 45 static uint32_t product_id(const ui::InputDevice& device) { |
| 46 return device.product_id; | 46 return device.product_id; |
| 47 } | 47 } |
| 48 | 48 |
| 49 static bool Read(ui::mojom::InputDeviceDataView data, ui::InputDevice* out) { | 49 static bool Read(ui::mojom::InputDeviceDataView data, ui::InputDevice* out); |
| 50 out->id = data.id(); | |
| 51 | |
| 52 switch (data.type()) { | |
| 53 case ui::mojom::InputDeviceType::INPUT_DEVICE_INTERNAL: | |
| 54 out->type = ui::INPUT_DEVICE_INTERNAL; | |
| 55 break; | |
| 56 case ui::mojom::InputDeviceType::INPUT_DEVICE_EXTERNAL: | |
| 57 out->type = ui::INPUT_DEVICE_EXTERNAL; | |
| 58 break; | |
| 59 case ui::mojom::InputDeviceType::INPUT_DEVICE_UNKNOWN: | |
| 60 out->type = ui::INPUT_DEVICE_UNKNOWN; | |
| 61 break; | |
| 62 default: | |
| 63 // Who knows what values might come over the wire, fail if invalid. | |
| 64 return false; | |
| 65 } | |
| 66 | |
| 67 if (!data.ReadName(&out->name)) | |
| 68 return false; | |
| 69 | |
| 70 base::StringPiece sys_path_string; | |
| 71 if (!data.ReadSysPath(&sys_path_string)) | |
| 72 return false; | |
| 73 out->sys_path = base::FilePath::FromUTF8Unsafe(sys_path_string); | |
| 74 | |
| 75 out->vendor_id = data.vendor_id(); | |
| 76 out->product_id = data.product_id(); | |
| 77 | |
| 78 return true; | |
| 79 } | |
| 80 }; | 50 }; |
| 81 | 51 |
| 82 template <> | 52 template <> |
| 83 struct StructTraits<ui::mojom::TouchscreenDevice, ui::TouchscreenDevice> { | 53 struct StructTraits<ui::mojom::TouchscreenDevice, ui::TouchscreenDevice> { |
| 84 static const ui::InputDevice& input_device( | 54 static const ui::InputDevice& input_device( |
| 85 const ui::TouchscreenDevice& device) { | 55 const ui::TouchscreenDevice& device) { |
| 86 return static_cast<const ui::InputDevice&>(device); | 56 return static_cast<const ui::InputDevice&>(device); |
| 87 } | 57 } |
| 88 | 58 |
| 89 static const gfx::Size& size(const ui::TouchscreenDevice& device) { | 59 static const gfx::Size& size(const ui::TouchscreenDevice& device) { |
| 90 return device.size; | 60 return device.size; |
| 91 } | 61 } |
| 92 | 62 |
| 93 static int32_t touch_points(const ui::TouchscreenDevice& device) { | 63 static int32_t touch_points(const ui::TouchscreenDevice& device) { |
| 94 return device.touch_points; | 64 return device.touch_points; |
| 95 } | 65 } |
| 96 | 66 |
| 97 static bool Read(ui::mojom::TouchscreenDeviceDataView data, | 67 static bool Read(ui::mojom::TouchscreenDeviceDataView data, |
| 98 ui::TouchscreenDevice* out) { | 68 ui::TouchscreenDevice* out); |
| 99 if (!data.ReadInputDevice(static_cast<ui::InputDevice*>(out))) | |
| 100 return false; | |
| 101 | |
| 102 if (!data.ReadSize(&out->size)) | |
| 103 return false; | |
| 104 | |
| 105 out->touch_points = data.touch_points(); | |
| 106 | |
| 107 return true; | |
| 108 } | |
| 109 }; | 69 }; |
| 110 | 70 |
| 111 } // namespace mojo | 71 } // namespace mojo |
| 112 | 72 |
| 113 #endif // UI_EVENTS_DEVICES_MOJO_INPUT_DEVICE_STRUCT_TRAITS_H_ | 73 #endif // UI_EVENTS_DEVICES_MOJO_INPUT_DEVICE_STRUCT_TRAITS_H_ |
| OLD | NEW |