From 6bce4c40f26bf7baa5c428ac0066182bb71944ba Mon Sep 17 00:00:00 2001 From: Martchus Date: Mon, 11 Sep 2023 21:27:41 +0200 Subject: [PATCH] Improve configuration and code of capslock detection * Define `X_AVAILABLE` if that's the case so the X11-specific code can actually ever be effective * Make the default for `CAPSLOCK_DETECTION` simply depending on whether it can be configured instead of making it platform dependent * Simplify code and avoid warnings in X11-specific code --- CMakeLists.txt | 29 ++++++++------------- enterpassworddialog/enterpassworddialog.cpp | 24 +++++------------ 2 files changed, 17 insertions(+), 36 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2a7c0a4..2ddaf4e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -117,27 +117,20 @@ set(SCRIPT_FILES scripts/required_icons.sh) set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules" "${CMAKE_MODULE_PATH}") # configure platform specific capslock detection for enterpassworddialog.cpp -if (WIN32 - OR (UNIX - AND NOT APPLE - AND NOT ANDROID)) - set(ENABLE_CAPSLOCK_DETECTION_BY_DEFAULT ON) +if (WIN32) + set(HAVE_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION ON) else () - set(ENABLE_CAPSLOCK_DETECTION_BY_DEFAULT OFF) -endif () -option(CAPSLOCK_DETECTION "enables capslock detection" ${ENABLE_CAPSLOCK_DETECTION_BY_DEFAULT}) -if (CAPSLOCK_DETECTION) - if (WIN32) - # WinAPI provides functions to provide capslock detection + use_package(TARGET_NAME X11::X11 PACKAGE_NAME X11) + if (TARGET X11::X11) + set_property( + SOURCE enterpassworddialog/enterpassworddialog.cpp + APPEND + PROPERTY COMPILE_DEFINITIONS X_AVAILABLE) set(HAVE_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION ON) - else () - # X11 can provide functions for capslock detection under non-Windows environments - find_package(X11) - if (X11_FOUND) - list(APPEND LIBRARIES ${X11_LIBRARIES}) - set(HAVE_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION ON) - endif () endif () +endif () +option(CAPSLOCK_DETECTION "enables capslock detection" ${HAVE_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION}) +if (CAPSLOCK_DETECTION) if (NOT HAVE_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION) message(FATAL_ERROR "No backend for capslock detection found (WinAPI or X11 must be provided)") endif () diff --git a/enterpassworddialog/enterpassworddialog.cpp b/enterpassworddialog/enterpassworddialog.cpp index a4593ab..d44579a 100644 --- a/enterpassworddialog/enterpassworddialog.cpp +++ b/enterpassworddialog/enterpassworddialog.cpp @@ -12,8 +12,7 @@ #include #include -#ifdef QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION -#if defined(Q_OS_WIN32) +#if defined(QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION) && defined(Q_OS_WIN32) #include #elif defined(X_AVAILABLE) #include @@ -22,7 +21,6 @@ #undef FocusIn #undef FocusOut #endif -#endif namespace QtUtilities { @@ -52,12 +50,7 @@ EnterPasswordDialog::EnterPasswordDialog(QWidget *parent) m_ui->userNameLineEdit->installEventFilter(this); m_ui->password1LineEdit->installEventFilter(this); m_ui->password2LineEdit->installEventFilter(this); -// capslock key detection -#ifdef QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION m_capslockPressed = isCapslockPressed(); -#else - m_capslockPressed = false; -#endif m_ui->capslockWarningWidget->setVisible(m_capslockPressed); // draw icon to capslock warning graphics view QIcon icon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning, nullptr, this); @@ -330,22 +323,17 @@ void EnterPasswordDialog::confirm() */ bool EnterPasswordDialog::isCapslockPressed() { -#ifdef QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION -// platform dependent method of determining if CAPS LOCK is pressed -#if defined(Q_OS_WIN32) +#if defined(QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION) && defined(Q_OS_WIN32) return GetKeyState(VK_CAPITAL) == 1; #elif defined(X_AVAILABLE) - Display *d = XOpenDisplay((char *)0); - bool caps_state = false; + auto *const d = XOpenDisplay(nullptr); + auto capsState = false; if (d) { unsigned n; XkbGetIndicatorState(d, XkbUseCoreKbd, &n); - caps_state = (n & 0x01) == 1; + capsState = (n & 0x01) == 1; } - return caps_state; -#else - return false; -#endif + return capsState; #else return false; #endif