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}")
# 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 ()

View File

@ -12,8 +12,7 @@
#include <QMessageBox>
#include <QStyle>
#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 <windows.h>
#elif defined(X_AVAILABLE)
#include <X11/XKBlib.h>
@ -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