1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
/* capslock_unix.cpp - Helper to check whether Caps Lock is on
* Copyright (C) 2021 g10 Code GmbH
*
* Software engineering by Ingo Klöcker <dev@ingo-kloecker.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>.
* SPDX-License-Identifier: GPL-2.0+
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "capslock.h"
#include "capslock_p.h"
#if PINENTRY_QT5_WAYLAND
# include <KWayland/Client/connection_thread.h>
# include <KWayland/Client/keyboard.h>
# include <KWayland/Client/registry.h>
# include <KWayland/Client/seat.h>
#endif
#include <QGuiApplication>
#if PINENTRY_QT5_X11
# include <QX11Info>
# include <X11/XKBlib.h>
# undef Status
#endif
#include <QDebug>
#if PINENTRY_QT5_WAYLAND
using namespace KWayland::Client;
#endif
#if PINENTRY_QT5_WAYLAND
static bool watchingWayland = false;
#endif
LockState capsLockState()
{
static bool reportUnsupportedPlatform = true;
#if PINENTRY_QT5_X11
if (qApp->platformName() == QLatin1String("xcb")) {
unsigned int state;
XkbGetIndicatorState(QX11Info::display(), XkbUseCoreKbd, &state);
return (state & 0x01) == 1 ? LockState::On : LockState::Off;
}
#endif
#if PINENTRY_QT5_WAYLAND
if (qApp->platformName() == QLatin1String("wayland")) {
if (!watchingWayland && reportUnsupportedPlatform) {
qDebug() << "Use CapsLockWatcher for checking for Caps Lock on Wayland";
}
} else
#endif
if (reportUnsupportedPlatform) {
qWarning() << "Checking for Caps Lock not possible on unsupported platform:" << qApp->platformName();
}
reportUnsupportedPlatform = false;
return LockState::Unknown;
}
#if PINENTRY_QT5_WAYLAND
void CapsLockWatcher::Private::watchWayland()
{
watchingWayland = true;
auto connection = ConnectionThread::fromApplication(q);
if (!connection) {
qWarning() << "Failed to get connection to Wayland server from QPA";
return;
}
registry = new Registry{q};
registry->create(connection);
if (!registry->isValid()) {
qWarning() << "Failed to create valid KWayland registry";
return;
}
registry->setup();
connect(registry, &Registry::seatAnnounced,
q, [this] (quint32 name, quint32 version) { registry_seatAnnounced(name, version); });
}
void CapsLockWatcher::Private::registry_seatAnnounced(quint32 name, quint32 version)
{
Q_ASSERT(registry);
seat = registry->createSeat(name, version, q);
if (!seat->isValid()) {
qWarning() << "Failed to create valid KWayland seat";
return;
}
connect(seat, &Seat::hasKeyboardChanged,
q, [this] (bool hasKeyboard) { seat_hasKeyboardChanged(hasKeyboard); });
}
void CapsLockWatcher::Private::seat_hasKeyboardChanged(bool hasKeyboard)
{
Q_ASSERT(seat);
if (!hasKeyboard) {
qDebug() << "Seat has no keyboard";
return;
}
auto keyboard = seat->createKeyboard(q);
if (!keyboard->isValid()) {
qWarning() << "Failed to create valid KWayland keyboard";
return;
}
connect(keyboard, &Keyboard::modifiersChanged,
q, [this] (quint32, quint32, quint32 locked, quint32) { keyboard_modifiersChanged(locked); });
}
void CapsLockWatcher::Private::keyboard_modifiersChanged(quint32 locked)
{
const bool capsLockIsLocked = (locked & 2u) != 0;
qDebug() << "Caps Lock is locked:" << capsLockIsLocked;
Q_EMIT q->stateChanged(capsLockIsLocked);
}
#endif
|