-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Wayland autotype implementation (using xdg-desktop-portal) #10905
Open
TheConfuZzledDude
wants to merge
1
commit into
keepassxreboot:develop
Choose a base branch
from
TheConfuZzledDude:autotype-wayland-xdg-portal
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+855
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
find_package(PkgConfig) | ||
pkg_check_modules(Xkbcommon xkbcommon) | ||
|
||
include(FindPackageHandleStandardArgs) | ||
find_package_handle_standard_args(Xkbcommon DEFAULT_MSG Xkbcommon_LIBRARIES Xkbcommon_INCLUDE_DIRS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,246 @@ | ||
/* | ||
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de> | ||
* Copyright (C) 2000-2008 Tom Sato <VEF00200@nifty.ne.jp> | ||
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org> | ||
* | ||
* 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 or (at your option) | ||
* version 3 of the License. | ||
* | ||
* 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 <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "AutoTypeWayland.h" | ||
#include "autotype/AutoTypeAction.h" | ||
#include "core/Tools.h" | ||
#include "gui/osutils/nixutils/X11Funcs.h" | ||
#include <qcbormap.h> | ||
#include <qcoreapplication.h> | ||
#include <qdbusconnection.h> | ||
#include <qdbusinterface.h> | ||
#include <qdbusmessage.h> | ||
#include <qdebug.h> | ||
#include <qglobal.h> | ||
#include <qguiapplication.h> | ||
#include <qlist.h> | ||
#include <qmetaobject.h> | ||
#include <qnamespace.h> | ||
#include <qrandom.h> | ||
#include <qscopedpointer.h> | ||
#include <qsocketnotifier.h> | ||
#include <qvariant.h> | ||
#include <qvector.h> | ||
#include <xkbcommon/xkbcommon.h> | ||
|
||
QString generateToken() | ||
{ | ||
static uint next = 0; | ||
return QString("keepassxc_%1_%2").arg(next++).arg(QRandomGenerator::system()->generate()); | ||
} | ||
|
||
AutoTypePlatformWayland::AutoTypePlatformWayland() | ||
: m_bus(QDBusConnection::sessionBus()) | ||
, m_remote_desktop("org.freedesktop.portal.Desktop", | ||
"/org/freedesktop/portal/desktop", | ||
"org.freedesktop.portal.RemoteDesktop", | ||
m_bus, | ||
this) | ||
{ | ||
m_bus.connect("org.freedesktop.portal.Desktop", | ||
"", | ||
"org.freedesktop.portal.Request", | ||
"Response", | ||
this, | ||
SLOT(portalResponse(uint, QVariantMap, QDBusMessage))); | ||
|
||
createSession(); | ||
} | ||
|
||
void AutoTypePlatformWayland::createSession() | ||
{ | ||
QString requestHandle = generateToken(); | ||
|
||
m_handlers.insert(requestHandle, | ||
[this](uint _response, QVariantMap _result) { handleCreateSession(_response, _result); }); | ||
|
||
m_remote_desktop.call("CreateSession", | ||
QVariantMap{{"handle_token", requestHandle}, {"session_handle_token", generateToken()}}); | ||
} | ||
|
||
void AutoTypePlatformWayland::handleCreateSession(uint response, QVariantMap result) | ||
{ | ||
qDebug() << "Got response and result" << response << result; | ||
if (response == 0) { | ||
m_session_handle = QDBusObjectPath(result["session_handle"].toString()); | ||
|
||
QString selectDevicesRequestHandle = generateToken(); | ||
m_handlers.insert(selectDevicesRequestHandle, | ||
[this](uint _response, QVariantMap _result) { handleSelectDevices(_response, _result); }); | ||
|
||
QVariantMap selectDevicesOptions{ | ||
{"handle_token", selectDevicesRequestHandle}, | ||
{"types", uint(1)}, | ||
{"persist_mode", uint(2)}, | ||
}; | ||
|
||
// TODO: Store restore token in database/some other persistent data so the dialog doesn't appear every launch | ||
if (!m_restore_token.isEmpty()) { | ||
selectDevicesOptions.insert("restore_token", m_restore_token); | ||
} | ||
|
||
m_remote_desktop.call("SelectDevices", m_session_handle, selectDevicesOptions); | ||
|
||
QString startRequestHandle = generateToken(); | ||
m_handlers.insert(startRequestHandle, | ||
[this](uint _response, QVariantMap _result) { handleStart(_response, _result); }); | ||
|
||
QVariantMap startOptions{ | ||
{"handle_token", startRequestHandle}, | ||
}; | ||
|
||
// TODO: Pass window identifier here instead of empty string if we want the dialog to appear on top of the | ||
// application window, need to be able to get active window and handle from Wayland | ||
m_remote_desktop.call("Start", m_session_handle, "", startOptions); | ||
} | ||
} | ||
|
||
void AutoTypePlatformWayland::handleSelectDevices(uint response, QVariantMap result) | ||
{ | ||
Q_UNUSED(result); | ||
qDebug() << "Select Devices: " << response << result; | ||
|
||
if (response == 0) { | ||
} | ||
Comment on lines
+119
to
+120
Check notice Code scanning / CodeQL Futile conditional Note
If-statement with an empty then-branch and no else-branch.
|
||
} | ||
|
||
void AutoTypePlatformWayland::handleStart(uint response, QVariantMap result) | ||
{ | ||
qDebug() << "Start: " << response << result; | ||
if (response == 0) { | ||
m_session_started = true; | ||
m_restore_token = result["restore_token"].toString(); | ||
} | ||
} | ||
|
||
void AutoTypePlatformWayland::portalResponse(uint response, QVariantMap results, QDBusMessage message) | ||
{ | ||
Q_UNUSED(response); | ||
Q_UNUSED(results); | ||
qDebug() << "Recieved message: " << message; | ||
auto index = message.path().lastIndexOf("/"); | ||
auto handle = message.path().right(message.path().length() - index - 1); | ||
if (m_handlers.contains(handle)) { | ||
m_handlers.take(handle)(response, results); | ||
} | ||
} | ||
|
||
AutoTypeAction::Result AutoTypePlatformWayland::sendKey(xkb_keysym_t keysym, QVector<xkb_keysym_t> modifiers) | ||
{ | ||
for (auto modifier : modifiers) { | ||
m_remote_desktop.call("NotifyKeyboardKeysym", m_session_handle, QVariantMap(), int(modifier), uint(1)); | ||
} | ||
|
||
m_remote_desktop.call("NotifyKeyboardKeysym", m_session_handle, QVariantMap(), int(keysym), uint(1)); | ||
|
||
m_remote_desktop.call("NotifyKeyboardKeysym", m_session_handle, QVariantMap(), int(keysym), uint(0)); | ||
|
||
for (auto modifier : modifiers) { | ||
m_remote_desktop.call("NotifyKeyboardKeysym", m_session_handle, QVariantMap(), int(modifier), uint(0)); | ||
} | ||
return AutoTypeAction::Result::Ok(); | ||
}; | ||
|
||
bool AutoTypePlatformWayland::isAvailable() | ||
{ | ||
return true; | ||
} | ||
|
||
void AutoTypePlatformWayland::unload() | ||
{ | ||
} | ||
|
||
QString AutoTypePlatformWayland::activeWindowTitle() | ||
{ | ||
return QString(""); | ||
} | ||
|
||
WId AutoTypePlatformWayland::activeWindow() | ||
{ | ||
return 0; | ||
}; | ||
|
||
AutoTypeExecutor* AutoTypePlatformWayland::createExecutor() | ||
{ | ||
return new AutoTypeExecutorWayland(this); | ||
} | ||
|
||
bool AutoTypePlatformWayland::raiseWindow(WId window) | ||
{ | ||
Q_UNUSED(window); | ||
return false; | ||
} | ||
|
||
QStringList AutoTypePlatformWayland::windowTitles() | ||
{ | ||
return QStringList{}; | ||
} | ||
|
||
AutoTypeExecutorWayland::AutoTypeExecutorWayland(AutoTypePlatformWayland* platform) | ||
: m_platform(platform) | ||
{ | ||
} | ||
|
||
AutoTypeAction::Result AutoTypeExecutorWayland::execBegin(const AutoTypeBegin* action) | ||
{ | ||
Q_UNUSED(action); | ||
return AutoTypeAction::Result::Ok(); | ||
} | ||
|
||
AutoTypeAction::Result AutoTypeExecutorWayland::execType(const AutoTypeKey* action) | ||
{ | ||
Q_UNUSED(action); | ||
|
||
QVector<xkb_keysym_t> modifiers{}; | ||
|
||
if (action->modifiers.testFlag(Qt::ShiftModifier)) { | ||
modifiers.append(XKB_KEY_Shift_L); | ||
} | ||
if (action->modifiers.testFlag(Qt::ControlModifier)) { | ||
modifiers.append(XKB_KEY_Control_L); | ||
} | ||
if (action->modifiers.testFlag(Qt::AltModifier)) { | ||
modifiers.append(XKB_KEY_Alt_L); | ||
} | ||
if (action->modifiers.testFlag(Qt::MetaModifier)) { | ||
modifiers.append(XKB_KEY_Meta_L); | ||
} | ||
|
||
// TODO: Replace these with proper lookups to xkbcommon keysyms instead of just reusing the X11 ones | ||
// They're mostly the same for most things, but strictly speaking differ slightly | ||
if (action->key != Qt::Key_unknown) { | ||
m_platform->sendKey(qtToNativeKeyCode(action->key), modifiers); | ||
} else { | ||
m_platform->sendKey(qcharToNativeKeyCode(action->character), modifiers); | ||
} | ||
|
||
Tools::sleep(execDelayMs); | ||
|
||
return AutoTypeAction::Result::Ok(); | ||
} | ||
|
||
AutoTypeAction::Result AutoTypeExecutorWayland::execClearField(const AutoTypeClearField* action) | ||
{ | ||
Q_UNUSED(action); | ||
execType(new AutoTypeKey(Qt::Key_Home)); | ||
execType(new AutoTypeKey(Qt::Key_End, Qt::ShiftModifier)); | ||
execType(new AutoTypeKey(Qt::Key_Backspace)); | ||
|
||
return AutoTypeAction::Result::Ok(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de> | ||
* Copyright (C) 2000-2008 Tom Sato <VEF00200@nifty.ne.jp> | ||
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org> | ||
* | ||
* 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 or (at your option) | ||
* version 3 of the License. | ||
* | ||
* 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 <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef KEEPASSX_AUTOTYPEWAYLAND_H | ||
#define KEEPASSX_AUTOTYPEWAYLAND_H | ||
|
||
#include <QApplication> | ||
#include <QSet> | ||
#include <QWidget> | ||
#include <QtPlugin> | ||
#include <qdbusconnection.h> | ||
#include <qdbusinterface.h> | ||
#include <qdbusmessage.h> | ||
#include <qnamespace.h> | ||
#include <qpointer.h> | ||
#include <qscopedpointer.h> | ||
#include <qsocketnotifier.h> | ||
#include <xkbcommon/xkbcommon.h> | ||
|
||
#include "autotype/AutoTypePlatformPlugin.h" | ||
|
||
class AutoTypePlatformWayland : public QObject, public AutoTypePlatformInterface | ||
{ | ||
Q_OBJECT | ||
Q_PLUGIN_METADATA(IID "org.keepassx.AutoTypePlatformWaylnd") | ||
Q_INTERFACES(AutoTypePlatformInterface) | ||
|
||
public: | ||
AutoTypePlatformWayland(); | ||
bool isAvailable() override; | ||
void unload() override; | ||
QStringList windowTitles() override; | ||
WId activeWindow() override; | ||
QString activeWindowTitle() override; | ||
bool raiseWindow(WId window) override; | ||
AutoTypeExecutor* createExecutor() override; | ||
|
||
AutoTypeAction::Result sendKey(xkb_keysym_t keysym, QVector<xkb_keysym_t> modifiers = {}); | ||
void createSession(); | ||
|
||
private: | ||
bool m_loaded; | ||
QDBusConnection m_bus; | ||
QMap<QString, std::function<void(uint, QVariantMap)>> m_handlers; | ||
QDBusInterface m_remote_desktop; | ||
QDBusObjectPath m_session_handle; | ||
QString m_restore_token; | ||
bool m_session_started = false; | ||
|
||
void handleCreateSession(uint response, QVariantMap results); | ||
void handleSelectDevices(uint response, QVariantMap results); | ||
void handleStart(uint response, QVariantMap results); | ||
private slots: | ||
void portalResponse(uint response, QVariantMap results, QDBusMessage message); | ||
}; | ||
|
||
class AutoTypeExecutorWayland : public AutoTypeExecutor | ||
{ | ||
public: | ||
explicit AutoTypeExecutorWayland(AutoTypePlatformWayland* platform); | ||
|
||
AutoTypeAction::Result execBegin(const AutoTypeBegin* action) override; | ||
AutoTypeAction::Result execType(const AutoTypeKey* action) override; | ||
AutoTypeAction::Result execClearField(const AutoTypeClearField* action) override; | ||
|
||
private: | ||
AutoTypePlatformWayland* const m_platform; | ||
}; | ||
|
||
#endif // KEEPASSX_AUTOTYPEWAYLAND_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
set(autotype_WAYLAND_SOURCES AutoTypeWayland.cpp) | ||
|
||
add_library(keepassxc-autotype-wayland MODULE ${autotype_WAYLAND_SOURCES}) | ||
target_link_libraries(keepassxc-autotype-wayland keepassx_core Qt5::Core Qt5::Widgets Qt5::DBus ${Xkbcommon_LIBRARIES}) | ||
install(TARGETS keepassxc-autotype-wayland | ||
BUNDLE DESTINATION . COMPONENT Runtime | ||
LIBRARY DESTINATION ${PLUGIN_INSTALL_DIR} COMPONENT Runtime) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Empty branch of conditional Note