9#include <linux/input.h>
15#include <QSocketNotifier>
20using namespace swift::misc::input;
25 static QHash<int, misc::input::KeyCode> keyMapping {
62 { XK_Shift_L, Key_ShiftLeft },
63 { XK_Shift_R, Key_ShiftRight },
64 { XK_Control_L, Key_ControlLeft },
65 { XK_Control_R, Key_ControlRight },
66 { XK_Alt_L, Key_AltLeft },
67 { XK_Alt_R, Key_AltRight },
73 { XK_KP_Divide, Key_Divide },
74 { XK_KP_Multiply, Key_Multiply },
75 { XK_KP_Subtract, Key_NumpadMinus },
76 { XK_KP_Add, Key_NumpadPlus },
77 { XK_KP_Delete, Key_NumpadDelete },
80 { XK_Escape, Key_Esc },
82 { XK_dead_grave, Key_DeadGrave },
92 { XK_F1, Key_Function1 },
93 { XK_F2, Key_Function2 },
94 { XK_F3, Key_Function3 },
95 { XK_F4, Key_Function4 },
96 { XK_F5, Key_Function5 },
97 { XK_F6, Key_Function6 },
98 { XK_F7, Key_Function7 },
99 { XK_F8, Key_Function8 },
100 { XK_F9, Key_Function9 },
101 { XK_F10, Key_Function10 },
102 { XK_F11, Key_Function11 },
103 { XK_F12, Key_Function12 },
104 { XK_F13, Key_Function13 },
105 { XK_F14, Key_Function14 },
106 { XK_F15, Key_Function15 },
107 { XK_F16, Key_Function16 },
108 { XK_F17, Key_Function17 },
109 { XK_F18, Key_Function18 },
110 { XK_F19, Key_Function19 },
111 { XK_F20, Key_Function20 },
112 { XK_F21, Key_Function21 },
113 { XK_F22, Key_Function22 },
114 { XK_F23, Key_Function23 },
115 { XK_F24, Key_Function24 },
135 if (m_display) XCloseDisplay(m_display);
143 &CKeyboardLinux::deviceDirectoryChanged);
144 deviceDirectoryChanged(dir);
149 void CKeyboardLinux::deviceDirectoryChanged(
const QString &dir)
153 foreach (
QFileInfo fileInfo, eventFiles.entryInfoList())
156 if (!m_keyboardDevices.contains(path)) addRawInputDevice(path);
160 void CKeyboardLinux::inputReadyRead(
int)
162 struct input_event eventInput;
164 auto fileInput = qobject_cast<QFile *>(sender()->parent());
165 if (!fileInput)
return;
169 while (std::cmp_equal(fileInput->read(
reinterpret_cast<char *
>(&eventInput),
sizeof(eventInput)),
173 if (eventInput.type != EV_KEY)
continue;
174 bool isPressed =
false;
175 switch (eventInput.value)
177 case 0: isPressed =
false;
break;
178 case 1: isPressed =
true;
break;
183 int keyCode = eventInput.code + 8;
189 int fd = fileInput->handle();
191 if ((ioctl(fd, EVIOCGVERSION, &version) < 0) || (((version >> 16) & 0xFF) < 1))
193 qWarning(
"CKeyboardLinux: Removing dead input device %s", qPrintable(fileInput->fileName()));
194 m_keyboardDevices.remove(fileInput->fileName());
199 void CKeyboardLinux::addRawInputDevice(
const QString &filePath)
204 int fd = inputFile->handle();
211 if (ioctl(fd, EVIOCGVERSION, &version) < 0)
216 char deviceName[255];
217 if (ioctl(fd, EVIOCGNAME(
sizeof(deviceName)), deviceName) < 0)
222 uint8_t bitmask[EV_MAX / 8 + 1];
223 memset(bitmask, 0,
sizeof(bitmask));
224 if (ioctl(fd, EVIOCGBIT(0,
sizeof(bitmask)), &bitmask) < 0)
231 if (!(bitmask[EV_SYN / 8] & (1 << (EV_SYN % 8))) && !(bitmask[EV_KEY / 8] & (1 << (EV_KEY % 8))) &&
232 (bitmask[EV_REL / 8] & (1 << (EV_REL % 8))) && (bitmask[EV_ABS / 8] & (1 << (EV_ABS % 8))))
238 if ((ioctl(fd, EVIOCGRAB, 1) < 0))
241 u
"Device exclusively grabbed by someone else (X11 using exclusive-mode evdev?)")
246 ioctl(fd, EVIOCGRAB, 0);
247 uint8_t keys[KEY_MAX / 8 + 1];
248 if ((ioctl(fd, EVIOCGBIT(EV_KEY,
sizeof(keys)), &keys) >= 0) &&
249 (keys[KEY_SPACE / 8] & (1 << (KEY_SPACE % 8))))
253 fcntl(inputFile->handle(), F_SETFL, O_NONBLOCK);
257 m_keyboardDevices.insert(filePath, inputFile);
264 << inputFile->fileName() << inputFile->errorString();
268 void CKeyboardLinux::keyEvent(
int keyCode,
bool isPressed)
270 if (isMouseButton(keyCode))
278 auto key = convertToKey(keyCode);
279 if (key == Key_Unknown)
284 m_keyCombination.addKeyboardKey(key);
288 auto key = convertToKey(keyCode);
289 if (key == Key_Unknown)
294 m_keyCombination.removeKeyboardKey(key);
297 if (oldCombination != m_keyCombination)
299 emit keyCombinationChanged(m_keyCombination);
312 auto keySym = XkbKeycodeToKeysym(m_display, keyCode, 0, 0);
313 return keyMapping.value(keySym, Key_Unknown);
316 bool CKeyboardLinux::isModifier(
int keyCode)
318 auto keySym = XkbKeycodeToKeysym(m_display, keyCode, 0, 0);
326 case XK_Alt_R:
return true;
327 default:
return false;
333 bool CKeyboardLinux::isMouseButton(
int keyCode)
339 case BTN_MIDDLE:
return true;
340 default:
return false;
Class for emitting a log message.
Derived & warning(const char16_t(&format)[N])
Set the severity to warning, providing a format string.
Derived & error(const char16_t(&format)[N])
Set the severity to error, providing a format string.
Derived & info(const char16_t(&format)[N])
Set the severity to info, providing a format string.
KeyCode
Key code http://www.kbdlayout.info/.
Free functions in swift::misc.
QString absoluteFilePath() const const
void directoryChanged(const QString &path)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void activated(QSocketDescriptor socket, QSocketNotifier::Type type)
void keyEvent(QTest::KeyAction action, QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier, int delay)
QFuture< QtFuture::ArgsType< Signal > > connect(Sender *sender, Signal signal)