2 * Copyright (C) 2012 Zan Dobersek <zandobersek@gmail.com>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 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.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
27 #include "GamepadDeviceLinux.h"
32 #include <sys/ioctl.h>
34 #include <wtf/text/CString.h>
38 GamepadDeviceLinux::GamepadDeviceLinux(String deviceFile)
39 : m_fileDescriptor(-1)
43 // FIXME: Log errors when returning early.
44 m_fileDescriptor = open(deviceFile.utf8().data(), O_RDONLY | O_NONBLOCK);
45 if (m_fileDescriptor == -1)
48 char deviceName[1024];
49 if (ioctl(m_fileDescriptor, JSIOCGNAME(sizeof(deviceName)), deviceName) < 0)
51 m_deviceName = String(deviceName).simplifyWhiteSpace();
54 uint8_t numberOfButtons;
55 if (ioctl(m_fileDescriptor, JSIOCGAXES, &numberOfAxes) < 0 || ioctl(m_fileDescriptor, JSIOCGBUTTONS, &numberOfButtons) < 0)
57 m_axes.fill(0.0, numberOfAxes);
58 m_buttons.fill(0.0, numberOfButtons);
61 GamepadDeviceLinux::~GamepadDeviceLinux()
63 if (m_fileDescriptor != -1)
64 close(m_fileDescriptor);
67 void GamepadDeviceLinux::updateForEvent(struct js_event event)
69 if (!(event.type & JS_EVENT_AXIS || event.type & JS_EVENT_BUTTON))
72 // Mark the device as connected only if it is not yet connected, the event is not an initialization
73 // and the value is not 0 (indicating a genuine interaction with the device).
74 if (!m_connected && !(event.type & JS_EVENT_INIT) && event.value)
77 if (event.type & JS_EVENT_AXIS)
78 m_axes[event.number] = normalizeAxisValue(event.value);
79 else if (event.type & JS_EVENT_BUTTON)
80 m_buttons[event.number] = normalizeButtonValue(event.value);
82 m_lastTimestamp = event.time;
85 float GamepadDeviceLinux::normalizeAxisValue(short value)
87 // Normalize from range [-32767, 32767] into range [-1.0, 1.0]
88 return value / 32767.0f;
91 float GamepadDeviceLinux::normalizeButtonValue(short value)
93 // Normalize from range [0, 1] into range [0.0, 1.0]
97 } // namespace WebCore
99 #endif // ENABLE(GAMEPAD)