blob: 12b732e55602b9ae87cc805c6152f7785ced665d [file] [log] [blame]
[email protected]00c0d042012-09-10 07:06:391// 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
avi84f37e12015-12-25 09:31:427#include <string.h>
arajpbc40fa312015-05-11 14:18:328
avi84f37e12015-12-25 09:31:429#include <algorithm>
[email protected]00c0d042012-09-10 07:06:3910
juncai2f298a82017-04-18 03:51:3911#include "device/gamepad/public/cpp/gamepads.h"
12
[email protected]00c0d042012-09-10 07:06:3913namespace ppapi {
14
arajpbc40fa312015-05-11 14:18:3215const size_t WebKitGamepads::kItemsLengthCap;
16
juncai2f298a82017-04-18 03:51:3917// TODO: Remove this function and use ConvertDeviceGamepadData() instead.
[email protected]00c0d042012-09-10 07:06:3918void ConvertWebKitGamepadData(const WebKitGamepads& webkit_data,
19 PP_GamepadsSampleData* output_data) {
aelias53670102017-01-04 23:13:3620 output_data->length = WebKitGamepads::kItemsLengthCap;
21 for (unsigned i = 0; i < WebKitGamepads::kItemsLengthCap; ++i) {
[email protected]00c0d042012-09-10 07:06:3922 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) {
mostynbd349f6d62015-01-06 00:42:0326 static_assert(sizeof(output_pad.id) == sizeof(webkit_pad.id),
27 "id size does not match");
[email protected]00c0d042012-09-10 07:06:3928 memcpy(output_pad.id, webkit_pad.id, sizeof(output_pad.id));
pkasting0b6e1a3a2014-11-26 02:13:4829 output_pad.timestamp = static_cast<double>(webkit_pad.timestamp);
[email protected]00c0d042012-09-10 07:06:3930 output_pad.axes_length = webkit_pad.axes_length;
[email protected]7106f332014-05-20 22:04:0631 for (unsigned j = 0; j < webkit_pad.axes_length; ++j)
32 output_pad.axes[j] = static_cast<float>(webkit_pad.axes[j]);
[email protected]00c0d042012-09-10 07:06:3933 output_pad.buttons_length = webkit_pad.buttons_length;
[email protected]723db332014-02-26 23:28:2834 for (unsigned j = 0; j < webkit_pad.buttons_length; ++j)
[email protected]7106f332014-05-20 22:04:0635 output_pad.buttons[j] = static_cast<float>(webkit_pad.buttons[j].value);
[email protected]00c0d042012-09-10 07:06:3936 }
37 }
38}
39
juncai2f298a82017-04-18 03:51:3940void 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]00c0d042012-09-10 07:06:3962} // namespace ppapi