| 1 | /* |
| 2 | * Copyright (C) 2019 Damir Porobic <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * Inspired by Skycoder42`s QHotKey implementation https://github.com/Skycoder42/QHotkey/blob/master/QHotkey/qhotkey_x11.cpp |
| 22 | */ |
| 23 | |
| 24 | // Can't include <QtGlobal> for QT_VERSION_CHECK because it includes too much, |
| 25 | // and symbols conflict with X11. Can't include <QtVersionChecks> because it |
| 26 | // doesn't exist in Qt 5. |
| 27 | #include "BuildConfig.h" |
| 28 | #if KSNIP_QT6 |
| 29 | #include <QtGui/private/qtx11extras_p.h> |
| 30 | #else |
| 31 | #include <QX11Info> |
| 32 | #endif |
| 33 | |
| 34 | #include "X11KeyHandler.h" |
| 35 | |
| 36 | #include "src/gui/globalHotKeys/X11ErrorLogger.h" |
| 37 | |
| 38 | X11KeyHandler::X11KeyHandler() : |
| 39 | mFixedModifiers({ 0, Mod2Mask, LockMask, (Mod2Mask | LockMask)}) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | X11KeyHandler::~X11KeyHandler() |
| 44 | { |
| 45 | unregisterKey(); |
| 46 | } |
| 47 | |
| 48 | bool X11KeyHandler::registerKey(const QKeySequence &keySequence) |
| 49 | { |
| 50 | auto display = QX11Info::display(); |
| 51 | if(!display) { |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | X11ErrorLogger x11ErrorLogger; |
| 56 | mKeyCodeCombo = mKeyCodeMapper.map(keySequence); |
| 57 | for(auto fixedModifier : mFixedModifiers) { |
| 58 | GrabKey(display, fixedModifier); |
| 59 | } |
| 60 | |
| 61 | XSync(display, False); |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | bool X11KeyHandler::isKeyPressed(void *message) |
| 66 | { |
| 67 | auto genericEvent = static_cast<xcb_generic_event_t *>(message); |
| 68 | if (genericEvent->response_type == XCB_KEY_PRESS) { |
| 69 | auto keyEvent = static_cast<xcb_key_press_event_t *>(message); |
| 70 | |
| 71 | for(auto fixedModifier : mFixedModifiers) { |
| 72 | if(isMatching(keyEvent, fixedModifier)) { |
| 73 | return true; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | bool X11KeyHandler::isMatching(const xcb_key_press_event_t *keyEvent, unsigned int fixedModifier) const |
| 81 | { |
| 82 | return keyEvent->detail == mKeyCodeCombo.key && keyEvent->state == (mKeyCodeCombo.modifier | fixedModifier); |
| 83 | } |
| 84 | |
| 85 | void X11KeyHandler::unregisterKey() const |
| 86 | { |
| 87 | auto display = QX11Info::display(); |
| 88 | if(!display) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | X11ErrorLogger x11ErrorLogger; |
| 93 | for(auto fixedModifier : mFixedModifiers) { |
| 94 | UngrabKey(display, fixedModifier); |
| 95 | } |
| 96 | |
| 97 | XSync(display, False); |
| 98 | } |
| 99 | |
| 100 | void X11KeyHandler::GrabKey(void *display, unsigned int fixedModifier) const |
| 101 | { |
| 102 | XGrabKey((Display*)display, mKeyCodeCombo.key, mKeyCodeCombo.modifier | fixedModifier, DefaultRootWindow((Display*)display), true, GrabModeAsync, GrabModeAsync); |
| 103 | } |
| 104 | |
| 105 | void X11KeyHandler::UngrabKey(void *display, unsigned int fixedModifier) const |
| 106 | { |
| 107 | XUngrabKey((Display*)display, mKeyCodeCombo.key, mKeyCodeCombo.modifier | fixedModifier, DefaultRootWindow((Display*)display)); |
| 108 | } |