swift
Loading...
Searching...
No Matches
joysticklinux.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2014 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
4#include "joysticklinux.h"
5
6#include <fcntl.h>
7#include <linux/joystick.h>
8#include <unistd.h>
9
10#include <algorithm>
11#include <utility>
12
13#include <QDir>
14#include <QFile>
15#include <QFileSystemWatcher>
16#include <QSignalMapper>
17#include <QSocketNotifier>
18
19#include "misc/logmessage.h"
20
21using namespace swift::misc;
22using namespace swift::misc::input;
23
24namespace
25{
26 inline QString inputDevicesDir() { return QStringLiteral("/dev/input/"); }
27} // namespace
28
29namespace swift::input
30{
32 : QObject(parent), m_path(path), m_fd(fd)
33 {
34 m_fd->setParent(this);
35 char deviceName[256];
36 if (ioctl(m_fd->handle(), JSIOCGNAME(sizeof(deviceName)), deviceName) < 0)
37 {
38 strncpy(deviceName, "Unknown", sizeof(deviceName));
39 }
40
41 CLogMessage(this).info(u"Found joystick: %1") << deviceName;
42
43 fcntl(m_fd->handle(), F_SETFL, O_NONBLOCK);
44
45 /* Forward */
46 struct js_event event;
47 while (std::cmp_equal(m_fd->read(reinterpret_cast<char *>(&event), sizeof(event)), sizeof(event)))
48 {
49 }
50 auto notifier = new QSocketNotifier(m_fd->handle(), QSocketNotifier::Read, m_fd);
51 connect(notifier, &QSocketNotifier::activated, this, &CJoystickDevice::processInput);
52 m_name = QString(deviceName);
53 }
54
56 {
57 if (m_fd)
58 {
59 m_fd->close();
60 m_fd->deleteLater();
61 }
62 }
63
64 void CJoystickDevice::processInput()
65 {
66 struct js_event event;
67 while (std::cmp_equal(m_fd->read(reinterpret_cast<char *>(&event), sizeof(event)), sizeof(event)))
68 {
69 switch (event.type & ~JS_EVENT_INIT)
70 {
71 case JS_EVENT_BUTTON:
72 if (event.value)
73 {
74 emit buttonChanged(m_name, event.number, true);
75 }
76 else
77 {
78 emit buttonChanged(m_name, event.number, false);
79 }
80 break;
81 }
82 }
83 }
84
85 CJoystickLinux::CJoystickLinux(QObject *parent) : IJoystick(parent), m_inputWatcher(new QFileSystemWatcher(this))
86 {
87 m_inputWatcher->addPath(inputDevicesDir());
88 connect(m_inputWatcher, &QFileSystemWatcher::directoryChanged, this, &CJoystickLinux::reloadDevices);
89 reloadDevices(inputDevicesDir());
90 }
91
93 {
94 // We don't know which buttons are available yet.
95 return {};
96 }
97
98 void CJoystickLinux::cleanupJoysticks()
99 {
100 for (auto it = m_joystickDevices.begin(); it != m_joystickDevices.end();)
101 {
102 // Remove all joysticks that do not exist anymore (/dev/input/js* removed).
103 if (!(*it)->isAttached())
104 {
105 CJoystickDevice *joystickDevice = *it;
106 it = m_joystickDevices.erase(it);
107 joystickDevice->deleteLater();
108 }
109 else
110 {
111 ++it;
112 }
113 }
114 }
115
116 void CJoystickLinux::addJoystickDevice(const QString &path)
117 {
118 auto fd = new QFile(path);
119 if (fd->open(QIODevice::ReadOnly))
120 {
121 auto joystickDevice = new CJoystickDevice(path, fd, this);
122 connect(joystickDevice, &CJoystickDevice::buttonChanged, this, &CJoystickLinux::joystickButtonChanged);
123 m_joystickDevices.push_back(joystickDevice);
124 }
125 else
126 {
127 swift::misc::CLogMessage(this).error(u"Failed to open joystick device %1: %2")
128 << fd->fileName() << fd->errorString();
129 fd->close();
130 fd->deleteLater();
131 }
132 }
133
134 void CJoystickLinux::joystickButtonChanged(const QString &name, int index, bool isPressed)
135 {
136 swift::misc::input::CHotkeyCombination oldCombination(m_buttonCombination);
137 if (isPressed)
138 {
139 m_buttonCombination.addJoystickButton({ name, index });
140 }
141 else
142 {
143 m_buttonCombination.removeJoystickButton({ name, index });
144 }
145
146 if (oldCombination != m_buttonCombination)
147 {
148 emit buttonCombinationChanged(m_buttonCombination);
149 }
150 }
151
152 void CJoystickLinux::reloadDevices(QString path)
153 {
154 cleanupJoysticks();
155
156 QDir dir(path, QLatin1String("js*"), QDir::Name, QDir::System);
157 for (const auto &entry : dir.entryInfoList())
158 {
159 QString f = entry.absoluteFilePath();
160 auto it = std::ranges::find_if(m_joystickDevices,
161 [path](const CJoystickDevice *device) { return device->getPath() == path; });
162 if (it == m_joystickDevices.end())
163 {
164 addJoystickDevice(f);
165 }
166 }
167 }
168
169} // namespace swift::input
Linux Joystick device.
virtual ~CJoystickDevice()
Destructor.
void buttonChanged(const QString &name, int index, bool isPressed)
Joystick button changed.
CJoystickDevice(const QString &path, QFile *fd, QObject *parent)
Constructor.
CJoystickLinux(CJoystickLinux const &)=delete
Copy Constructor.
swift::misc::input::CJoystickButtonList getAllAvailableJoystickButtons() const
Get all available joystick buttons.
Abstract interface for native joystick handling.
Definition joystick.h:22
void buttonCombinationChanged(const swift::misc::input::CHotkeyCombination &)
Joystick button combination has changed.
Class for emitting a log message.
Definition logmessage.h:27
Derived & info(const char16_t(&format)[N])
Set the severity to info, providing a format string.
Value object encapsulating a list of joystick buttons.
Free functions in swift::misc.
virtual void close() override
void directoryChanged(const QString &path)
QByteArray read(qint64 maxSize)
QObject(QObject *parent)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
virtual bool event(QEvent *e)
QObject * parent() const const
void activated(QSocketDescriptor socket, QSocketNotifier::Type type)