7#include <linux/joystick.h>
15#include <QFileSystemWatcher>
16#include <QSignalMapper>
17#include <QSocketNotifier>
22using namespace swift::misc::input;
26 inline QString inputDevicesDir() {
return QStringLiteral(
"/dev/input/"); }
34 m_fd->setParent(
this);
36 if (ioctl(m_fd->handle(), JSIOCGNAME(
sizeof(deviceName)), deviceName) < 0)
38 strncpy(deviceName,
"Unknown",
sizeof(deviceName));
43 fcntl(m_fd->handle(), F_SETFL, O_NONBLOCK);
46 struct js_event
event;
47 while (std::cmp_equal(m_fd->read(
reinterpret_cast<char *
>(&
event),
sizeof(
event)),
sizeof(
event)))
64 void CJoystickDevice::processInput()
66 struct js_event
event;
67 while (std::cmp_equal(m_fd->
read(
reinterpret_cast<char *
>(&
event),
sizeof(
event)),
sizeof(
event)))
69 switch (
event.type & ~JS_EVENT_INIT)
87 m_inputWatcher->addPath(inputDevicesDir());
89 reloadDevices(inputDevicesDir());
98 void CJoystickLinux::cleanupJoysticks()
100 for (
auto it = m_joystickDevices.begin(); it != m_joystickDevices.end();)
103 if (!(*it)->isAttached())
106 it = m_joystickDevices.erase(it);
116 void CJoystickLinux::addJoystickDevice(
const QString &path)
118 auto fd =
new QFile(path);
121 auto joystickDevice =
new CJoystickDevice(path, fd,
this);
123 m_joystickDevices.push_back(joystickDevice);
127 swift::misc::CLogMessage(
this).error(u
"Failed to open joystick device %1: %2")
128 << fd->fileName() << fd->errorString();
134 void CJoystickLinux::joystickButtonChanged(
const QString &name,
int index,
bool isPressed)
136 swift::misc::input::CHotkeyCombination oldCombination(m_buttonCombination);
139 m_buttonCombination.addJoystickButton({ name, index });
143 m_buttonCombination.removeJoystickButton({ name, index });
146 if (oldCombination != m_buttonCombination)
152 void CJoystickLinux::reloadDevices(QString path)
157 for (
const auto &entry : dir.entryInfoList())
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())
164 addJoystickDevice(f);
Class for emitting a log message.
Derived & info(const char16_t(&format)[N])
Set the severity to info, providing a format string.
Free functions in swift::misc.
virtual void close() override
void directoryChanged(const QString &path)
QByteArray read(qint64 maxSize)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
virtual bool event(QEvent *e)
QObject * parent() const const
void activated(QSocketDescriptor socket, QSocketNotifier::Type type)