1 // Copyright 2014 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.
5 #include "content/common/gamepad_param_traits.h"
7 #include "base/pickle.h"
8 #include "base/strings/string16.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "ipc/ipc_message_utils.h"
11 #include "third_party/WebKit/public/platform/WebGamepad.h"
13 using blink::WebGamepad
;
17 void LogWebUCharString(
18 const blink::WebUChar web_string
[],
19 const size_t array_size
,
22 utf16
.reserve(array_size
);
23 for (size_t i
= 0; i
< array_size
&& web_string
[i
]; ++i
) {
24 utf16
[i
] = web_string
[i
];
26 log
->append(base::UTF16ToUTF8(utf16
));
33 void ParamTraits
<WebGamepad
>::Write(
35 const WebGamepad
& p
) {
36 m
->WriteData(reinterpret_cast<const char*>(&p
), sizeof(WebGamepad
));
39 bool ParamTraits
<WebGamepad
>::Read(
45 if (!m
->ReadData(iter
, &data
, &length
) || length
!= sizeof(WebGamepad
))
47 memcpy(p
, data
, sizeof(WebGamepad
));
52 void ParamTraits
<WebGamepad
>::Log(
55 l
->append("WebGamepad(");
56 LogParam(p
.connected
, l
);
57 LogWebUCharString(p
.id
, WebGamepad::idLengthCap
, l
);
59 LogWebUCharString(p
.mapping
, WebGamepad::mappingLengthCap
, l
);
61 LogParam(p
.timestamp
, l
);
63 LogParam(p
.axesLength
, l
);
65 for (size_t i
= 0; i
< arraysize(p
.axes
); ++i
) {
66 l
->append(base::StringPrintf("%f%s", p
.axes
[i
],
67 i
< (arraysize(p
.axes
) - 1) ? ", " : "], "));
69 LogParam(p
.buttonsLength
, l
);
71 for (size_t i
= 0; i
< arraysize(p
.buttons
); ++i
) {
72 l
->append(base::StringPrintf("(%u, %f)%s",
73 p
.buttons
[i
].pressed
, p
.buttons
[i
].value
,
74 i
< (arraysize(p
.buttons
) - 1) ? ", " : "], "));