Skip to content

Commit

Permalink
Move global shortcut handling into OSUtils (#5566)
Browse files Browse the repository at this point in the history
Move global shortcut handling into OSUtils
  • Loading branch information
droidmonkey authored Dec 14, 2020
1 parent a6f0134 commit 404fd94
Show file tree
Hide file tree
Showing 35 changed files with 1,124 additions and 1,136 deletions.
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,11 @@ if(WITH_COVERAGE AND CMAKE_COMPILER_IS_CLANGXX)
# `find src -iname '*.h' -or -iname '*.cpp'`
endif()

if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
if(UNIX AND NOT APPLE)
check_add_gcc_compiler_flag("-Qunused-arguments")
add_gcc_compiler_flags("-pie -fPIE")
check_add_gcc_compiler_flag("-fPIC")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--no-add-needed -Wl,--as-needed -Wl,--no-undefined")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-z,relro,-z,now")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-z,relro,-z,now -pie")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--no-add-needed -Wl,--as-needed")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,-z,relro,-z,now")
endif()
Expand Down Expand Up @@ -399,7 +399,7 @@ include(CLangFormat)

set(QT_COMPONENTS Core Network Concurrent Gui Svg Widgets Test LinguistTools)
if(UNIX AND NOT APPLE)
find_package(Qt5 COMPONENTS ${QT_COMPONENTS} DBus REQUIRED)
find_package(Qt5 COMPONENTS ${QT_COMPONENTS} DBus X11Extras REQUIRED)
elseif(APPLE)
find_package(Qt5 COMPONENTS ${QT_COMPONENTS} REQUIRED HINTS /usr/local/opt/qt/lib/cmake /usr/local/Cellar/qt/*/lib/cmake ENV PATH)
find_package(Qt5 COMPONENTS MacExtras HINTS /usr/local/opt/qt/lib/cmake /usr/local/Cellar/qt/*/lib/cmake ENV PATH)
Expand Down
12 changes: 4 additions & 8 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -206,19 +206,15 @@ if(UNIX AND NOT APPLE)
${keepassx_SOURCES}
gui/MainWindowAdaptor.cpp
gui/osutils/nixutils/ScreenLockListenerDBus.cpp
gui/osutils/nixutils/NixUtils.cpp)
gui/osutils/nixutils/NixUtils.cpp
gui/osutils/nixutils/X11Funcs.cpp)
endif()
if(MINGW)
set(keepassx_SOURCES
${keepassx_SOURCES}
gui/osutils/winutils/ScreenLockListenerWin.cpp
gui/osutils/winutils/WinUtils.cpp)
endif()
if(MINGW OR (UNIX AND NOT APPLE))
set(keepassx_SOURCES
${keepassx_SOURCES}
gui/osutils/OSEventFilter.cpp)
endif()

set(keepassx_SOURCES ${keepassx_SOURCES}
../share/icons/icons.qrc
Expand Down Expand Up @@ -343,7 +339,7 @@ if(WITH_XC_KEESHARE)
endif()

if(APPLE)
target_link_libraries(keepassx_core "-framework Foundation -framework AppKit")
target_link_libraries(keepassx_core "-framework Foundation -framework AppKit -framework Carbon")
if(Qt5MacExtras_FOUND)
target_link_libraries(keepassx_core Qt5::MacExtras)
endif()
Expand All @@ -356,7 +352,7 @@ if(HAIKU)
target_link_libraries(keepassx_core network)
endif()
if(UNIX AND NOT APPLE)
target_link_libraries(keepassx_core Qt5::DBus X11)
target_link_libraries(keepassx_core Qt5::DBus Qt5::X11Extras X11)
include_directories(${Qt5Gui_PRIVATE_INCLUDE_DIRS})
endif()
if(MINGW)
Expand Down
62 changes: 26 additions & 36 deletions src/autotype/AutoType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,13 @@
#include "core/Tools.h"
#include "gui/MainWindow.h"
#include "gui/MessageBox.h"

#ifdef Q_OS_MAC
#include "gui/osutils/macutils/MacUtils.h"
#endif
#include "gui/osutils/OSUtils.h"

AutoType* AutoType::m_instance = nullptr;

AutoType::AutoType(QObject* parent, bool test)
: QObject(parent)
, m_autoTypeDelay(0)
, m_currentGlobalKey(static_cast<Qt::Key>(0))
, m_currentGlobalModifiers(nullptr)
, m_pluginLoader(new QPluginLoader(this))
, m_plugin(nullptr)
, m_executor(nullptr)
Expand Down Expand Up @@ -96,7 +91,11 @@ void AutoType::loadPlugin(const QString& pluginPath)
if (m_plugin) {
if (m_plugin->isAvailable()) {
m_executor = m_plugin->createExecutor();
connect(pluginInstance, SIGNAL(globalShortcutTriggered()), SLOT(startGlobalAutoType()));
connect(osUtils, &OSUtilsBase::globalShortcutTriggered, this, [this](QString name) {
if (name == "autotype") {
startGlobalAutoType();
}
});
} else {
unloadPlugin();
}
Expand Down Expand Up @@ -153,44 +152,18 @@ void AutoType::raiseWindow()
#endif
}

bool AutoType::registerGlobalShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers)
bool AutoType::registerGlobalShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers, QString* error)
{
Q_ASSERT(key);
Q_ASSERT(modifiers);

if (!m_plugin) {
return false;
}

if (key != m_currentGlobalKey || modifiers != m_currentGlobalModifiers) {
if (m_currentGlobalKey && m_currentGlobalModifiers) {
m_plugin->unregisterGlobalShortcut(m_currentGlobalKey, m_currentGlobalModifiers);
}

if (m_plugin->registerGlobalShortcut(key, modifiers)) {
m_currentGlobalKey = key;
m_currentGlobalModifiers = modifiers;
return true;
}
return false;
}
return true;
return osUtils->registerGlobalShortcut("autotype", key, modifiers, error);
}

void AutoType::unregisterGlobalShortcut()
{
if (m_plugin && m_currentGlobalKey && m_currentGlobalModifiers) {
m_plugin->unregisterGlobalShortcut(m_currentGlobalKey, m_currentGlobalModifiers);
}
}

int AutoType::callEventFilter(void* event)
{
if (!m_plugin) {
return -1;
}

return m_plugin->platformEventFilter(event);
osUtils->unregisterGlobalShortcut("autotype");
}

/**
Expand Down Expand Up @@ -303,6 +276,23 @@ void AutoType::startGlobalAutoType()
m_windowForGlobal = m_plugin->activeWindow();
m_windowTitleForGlobal = m_plugin->activeWindowTitle();
#ifdef Q_OS_MACOS
// Determine if the user has given proper permissions to KeePassXC to perform Auto-Type
static bool accessibilityChecked = false;
if (!accessibilityChecked) {
if (macUtils()->enableAccessibility() && macUtils()->enableScreenRecording()) {
accessibilityChecked = true;
} else if (getMainWindow()) {
// Does not have required permissions to Auto-Type, ignore the event
MessageBox::information(
nullptr,
tr("Permission Required"),
tr("KeePassXC requires the Accessibility and Screen Recorder permission in order to perform global "
"Auto-Type. Screen Recording is necessary to use the window title to find entries. If you "
"already granted permission, you may have to restart KeePassXC."));
return;
}
}

m_windowState = WindowState::Normal;
if (getMainWindow()) {
if (getMainWindow()->isMinimized()) {
Expand Down
5 changes: 1 addition & 4 deletions src/autotype/AutoType.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ class AutoType : public QObject

public:
QStringList windowTitles();
bool registerGlobalShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers);
bool registerGlobalShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers, QString* error = nullptr);
void unregisterGlobalShortcut();
int callEventFilter(void* event);
static bool checkSyntax(const QString& string);
static bool checkHighRepetition(const QString& string);
static bool checkSlowKeypress(const QString& string);
Expand Down Expand Up @@ -99,8 +98,6 @@ private slots:
QMutex m_inAutoType;
QMutex m_inGlobalAutoTypeDialog;
int m_autoTypeDelay;
Qt::Key m_currentGlobalKey;
Qt::KeyboardModifiers m_currentGlobalModifiers;
QPluginLoader* m_pluginLoader;
AutoTypePlatformInterface* m_plugin;
AutoTypeExecutor* m_executor;
Expand Down
3 changes: 0 additions & 3 deletions src/autotype/AutoTypePlatformPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ class AutoTypePlatformInterface
virtual QStringList windowTitles() = 0;
virtual WId activeWindow() = 0;
virtual QString activeWindowTitle() = 0;
virtual bool registerGlobalShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers) = 0;
virtual void unregisterGlobalShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers) = 0;
virtual int platformEventFilter(void* event) = 0;
virtual bool raiseWindow(WId window) = 0;
virtual void unload()
{
Expand Down
5 changes: 4 additions & 1 deletion src/autotype/ShortcutWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "ShortcutWidget.h"

#include <QKeyEvent>
#include <QToolTip>

#include "autotype/AutoType.h"

Expand Down Expand Up @@ -48,9 +49,11 @@ void ShortcutWidget::setShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers)

displayShortcut(m_key, m_modifiers);

if (autoType()->registerGlobalShortcut(m_key, m_modifiers)) {
QString error;
if (autoType()->registerGlobalShortcut(m_key, m_modifiers, &error)) {
setStyleSheet("");
} else {
QToolTip::showText(mapToGlobal(rect().bottomLeft()), error);
setStyleSheet("background-color: #FF9696;");
}
}
Expand Down
Loading

0 comments on commit 404fd94

Please sign in to comment.