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
This commit is contained in:
Martchus 2023-09-11 21:27:41 +02:00
parent 472e32bfc2
commit 6bce4c40f2
2 changed files with 17 additions and 36 deletions

View File

@ -117,27 +117,20 @@ set(SCRIPT_FILES scripts/required_icons.sh)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules" "${CMAKE_MODULE_PATH}") set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules" "${CMAKE_MODULE_PATH}")
# configure platform specific capslock detection for enterpassworddialog.cpp # configure platform specific capslock detection for enterpassworddialog.cpp
if (WIN32 if (WIN32)
OR (UNIX set(HAVE_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION ON)
AND NOT APPLE
AND NOT ANDROID))
set(ENABLE_CAPSLOCK_DETECTION_BY_DEFAULT ON)
else () else ()
set(ENABLE_CAPSLOCK_DETECTION_BY_DEFAULT OFF) use_package(TARGET_NAME X11::X11 PACKAGE_NAME X11)
endif () if (TARGET X11::X11)
option(CAPSLOCK_DETECTION "enables capslock detection" ${ENABLE_CAPSLOCK_DETECTION_BY_DEFAULT}) set_property(
if (CAPSLOCK_DETECTION) SOURCE enterpassworddialog/enterpassworddialog.cpp
if (WIN32) APPEND
# WinAPI provides functions to provide capslock detection PROPERTY COMPILE_DEFINITIONS X_AVAILABLE)
set(HAVE_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION ON) 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 ()
endif ()
option(CAPSLOCK_DETECTION "enables capslock detection" ${HAVE_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION})
if (CAPSLOCK_DETECTION)
if (NOT HAVE_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION) if (NOT HAVE_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION)
message(FATAL_ERROR "No backend for capslock detection found (WinAPI or X11 must be provided)") message(FATAL_ERROR "No backend for capslock detection found (WinAPI or X11 must be provided)")
endif () endif ()

View File

@ -12,8 +12,7 @@
#include <QMessageBox> #include <QMessageBox>
#include <QStyle> #include <QStyle>
#ifdef QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION #if defined(QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION) && defined(Q_OS_WIN32)
#if defined(Q_OS_WIN32)
#include <windows.h> #include <windows.h>
#elif defined(X_AVAILABLE) #elif defined(X_AVAILABLE)
#include <X11/XKBlib.h> #include <X11/XKBlib.h>
@ -22,7 +21,6 @@
#undef FocusIn #undef FocusIn
#undef FocusOut #undef FocusOut
#endif #endif
#endif
namespace QtUtilities { namespace QtUtilities {
@ -52,12 +50,7 @@ EnterPasswordDialog::EnterPasswordDialog(QWidget *parent)
m_ui->userNameLineEdit->installEventFilter(this); m_ui->userNameLineEdit->installEventFilter(this);
m_ui->password1LineEdit->installEventFilter(this); m_ui->password1LineEdit->installEventFilter(this);
m_ui->password2LineEdit->installEventFilter(this); m_ui->password2LineEdit->installEventFilter(this);
// capslock key detection
#ifdef QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION
m_capslockPressed = isCapslockPressed(); m_capslockPressed = isCapslockPressed();
#else
m_capslockPressed = false;
#endif
m_ui->capslockWarningWidget->setVisible(m_capslockPressed); m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
// draw icon to capslock warning graphics view // draw icon to capslock warning graphics view
QIcon icon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning, nullptr, this); QIcon icon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning, nullptr, this);
@ -330,22 +323,17 @@ void EnterPasswordDialog::confirm()
*/ */
bool EnterPasswordDialog::isCapslockPressed() bool EnterPasswordDialog::isCapslockPressed()
{ {
#ifdef QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION #if defined(QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION) && defined(Q_OS_WIN32)
// platform dependent method of determining if CAPS LOCK is pressed
#if defined(Q_OS_WIN32)
return GetKeyState(VK_CAPITAL) == 1; return GetKeyState(VK_CAPITAL) == 1;
#elif defined(X_AVAILABLE) #elif defined(X_AVAILABLE)
Display *d = XOpenDisplay((char *)0); auto *const d = XOpenDisplay(nullptr);
bool caps_state = false; auto capsState = false;
if (d) { if (d) {
unsigned n; unsigned n;
XkbGetIndicatorState(d, XkbUseCoreKbd, &n); XkbGetIndicatorState(d, XkbUseCoreKbd, &n);
caps_state = (n & 0x01) == 1; capsState = (n & 0x01) == 1;
} }
return caps_state; return capsState;
#else
return false;
#endif
#else #else
return false; return false;
#endif #endif