1/*
2 * Copyright (C) 2016 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "GamepadData.h"
28
29#if ENABLE(GAMEPAD)
30
31#include "ArgumentCoders.h"
32#include <wtf/text/StringBuilder.h>
33
34namespace WebKit {
35
36GamepadData::GamepadData(unsigned index, const Vector<double>& axisValues, const Vector<double>& buttonValues, MonotonicTime lastUpdateTime)
37 : m_index(index)
38 , m_axisValues(axisValues)
39 , m_buttonValues(buttonValues)
40 , m_lastUpdateTime(lastUpdateTime)
41{
42}
43
44GamepadData::GamepadData(unsigned index, const String& id, const Vector<double>& axisValues, const Vector<double>& buttonValues, MonotonicTime lastUpdateTime)
45 : m_index(index)
46 , m_id(id)
47 , m_axisValues(axisValues)
48 , m_buttonValues(buttonValues)
49 , m_lastUpdateTime(lastUpdateTime)
50{
51}
52
53void GamepadData::encode(IPC::Encoder& encoder) const
54{
55 encoder << m_isNull;
56 if (m_isNull)
57 return;
58
59 encoder << m_index << m_id << m_axisValues << m_buttonValues << m_lastUpdateTime;
60}
61
62Optional<GamepadData> GamepadData::decode(IPC::Decoder& decoder)
63{
64 GamepadData data;
65 if (!decoder.decode(data.m_isNull))
66 return WTF::nullopt;
67
68 if (data.m_isNull)
69 return data;
70
71 if (!decoder.decode(data.m_index))
72 return WTF::nullopt;
73
74 if (!decoder.decode(data.m_id))
75 return WTF::nullopt;
76
77 if (!decoder.decode(data.m_axisValues))
78 return WTF::nullopt;
79
80 if (!decoder.decode(data.m_buttonValues))
81 return WTF::nullopt;
82
83 if (!decoder.decode(data.m_lastUpdateTime))
84 return WTF::nullopt;
85
86 return WTFMove(data);
87}
88
89#if !LOG_DISABLED
90String GamepadData::loggingString() const
91{
92 StringBuilder builder;
93
94 builder.appendNumber(m_axisValues.size());
95 builder.appendLiteral(" axes, ");
96 builder.appendNumber(m_buttonValues.size());
97 builder.appendLiteral(" buttons\n");
98
99 for (size_t i = 0; i < m_axisValues.size(); ++i) {
100 builder.appendLiteral(" Axis ");
101 builder.appendNumber(i);
102 builder.appendLiteral(": ");
103 builder.appendFixedPrecisionNumber(m_axisValues[i]);
104 }
105
106 builder.append('\n');
107 for (size_t i = 0; i < m_buttonValues.size(); ++i) {
108 builder.appendLiteral(" Button ");
109 builder.appendNumber(i);
110 builder.appendLiteral(": ");
111 builder.appendFixedPrecisionNumber(m_buttonValues[i]);
112 }
113
114 return builder.toString();
115}
116#endif
117
118} // namespace WebKit
119
120#endif // ENABLE(GAMEPAD)
121