[email protected] | 00c0d04 | 2012-09-10 07:06:39 | [diff] [blame] | 1 | // Copyright (c) 2012 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 "ppapi/shared_impl/ppb_gamepad_shared.h" |
| 6 | |
avi | 84f37e1 | 2015-12-25 09:31:42 | [diff] [blame] | 7 | #include <string.h> |
arajp | bc40fa31 | 2015-05-11 14:18:32 | [diff] [blame] | 8 | |
avi | 84f37e1 | 2015-12-25 09:31:42 | [diff] [blame] | 9 | #include <algorithm> |
[email protected] | 00c0d04 | 2012-09-10 07:06:39 | [diff] [blame] | 10 | |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame^] | 11 | #include "device/gamepad/public/cpp/gamepads.h" |
| 12 | |
[email protected] | 00c0d04 | 2012-09-10 07:06:39 | [diff] [blame] | 13 | namespace ppapi { |
| 14 | |
arajp | bc40fa31 | 2015-05-11 14:18:32 | [diff] [blame] | 15 | const size_t WebKitGamepads::kItemsLengthCap; |
| 16 | |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame^] | 17 | // TODO: Remove this function and use ConvertDeviceGamepadData() instead. |
[email protected] | 00c0d04 | 2012-09-10 07:06:39 | [diff] [blame] | 18 | void ConvertWebKitGamepadData(const WebKitGamepads& webkit_data, |
| 19 | PP_GamepadsSampleData* output_data) { |
aelias | 5367010 | 2017-01-04 23:13:36 | [diff] [blame] | 20 | output_data->length = WebKitGamepads::kItemsLengthCap; |
| 21 | for (unsigned i = 0; i < WebKitGamepads::kItemsLengthCap; ++i) { |
[email protected] | 00c0d04 | 2012-09-10 07:06:39 | [diff] [blame] | 22 | PP_GamepadSampleData& output_pad = output_data->items[i]; |
| 23 | const WebKitGamepad& webkit_pad = webkit_data.items[i]; |
| 24 | output_pad.connected = webkit_pad.connected ? PP_TRUE : PP_FALSE; |
| 25 | if (webkit_pad.connected) { |
mostynb | d349f6d6 | 2015-01-06 00:42:03 | [diff] [blame] | 26 | static_assert(sizeof(output_pad.id) == sizeof(webkit_pad.id), |
| 27 | "id size does not match"); |
[email protected] | 00c0d04 | 2012-09-10 07:06:39 | [diff] [blame] | 28 | memcpy(output_pad.id, webkit_pad.id, sizeof(output_pad.id)); |
pkasting | 0b6e1a3a | 2014-11-26 02:13:48 | [diff] [blame] | 29 | output_pad.timestamp = static_cast<double>(webkit_pad.timestamp); |
[email protected] | 00c0d04 | 2012-09-10 07:06:39 | [diff] [blame] | 30 | output_pad.axes_length = webkit_pad.axes_length; |
[email protected] | 7106f33 | 2014-05-20 22:04:06 | [diff] [blame] | 31 | for (unsigned j = 0; j < webkit_pad.axes_length; ++j) |
| 32 | output_pad.axes[j] = static_cast<float>(webkit_pad.axes[j]); |
[email protected] | 00c0d04 | 2012-09-10 07:06:39 | [diff] [blame] | 33 | output_pad.buttons_length = webkit_pad.buttons_length; |
[email protected] | 723db33 | 2014-02-26 23:28:28 | [diff] [blame] | 34 | for (unsigned j = 0; j < webkit_pad.buttons_length; ++j) |
[email protected] | 7106f33 | 2014-05-20 22:04:06 | [diff] [blame] | 35 | output_pad.buttons[j] = static_cast<float>(webkit_pad.buttons[j].value); |
[email protected] | 00c0d04 | 2012-09-10 07:06:39 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame^] | 40 | void ConvertDeviceGamepadData(const device::Gamepads& device_data, |
| 41 | PP_GamepadsSampleData* output_data) { |
| 42 | output_data->length = device::Gamepads::kItemsLengthCap; |
| 43 | for (unsigned i = 0; i < device::Gamepads::kItemsLengthCap; ++i) { |
| 44 | PP_GamepadSampleData& output_pad = output_data->items[i]; |
| 45 | const device::Gamepad& device_pad = device_data.items[i]; |
| 46 | output_pad.connected = device_pad.connected ? PP_TRUE : PP_FALSE; |
| 47 | if (device_pad.connected) { |
| 48 | static_assert(sizeof(output_pad.id) == sizeof(device_pad.id), |
| 49 | "id size does not match"); |
| 50 | memcpy(output_pad.id, device_pad.id, sizeof(output_pad.id)); |
| 51 | output_pad.timestamp = static_cast<double>(device_pad.timestamp); |
| 52 | output_pad.axes_length = device_pad.axes_length; |
| 53 | for (unsigned j = 0; j < device_pad.axes_length; ++j) |
| 54 | output_pad.axes[j] = static_cast<float>(device_pad.axes[j]); |
| 55 | output_pad.buttons_length = device_pad.buttons_length; |
| 56 | for (unsigned j = 0; j < device_pad.buttons_length; ++j) |
| 57 | output_pad.buttons[j] = static_cast<float>(device_pad.buttons[j].value); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
[email protected] | 00c0d04 | 2012-09-10 07:06:39 | [diff] [blame] | 62 | } // namespace ppapi |