Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog/next.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## Other Changes

- `SystemTrayItem.activate()` now forwards Wayland activation tokens to applications.
- `NotificationAction.invoke()` now sends a Wayland activation token to the notifying application.
- Added `Quickshell.requestActivationToken()` to get a Wayland activation token for the latest input, e.g. to pass to a launched app.

## Bug Fixes

Expand Down
5 changes: 5 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ qs_module_pch(quickshell-core SET large)

target_link_libraries(quickshell PRIVATE quickshell-coreplugin)

if (WAYLAND)
target_compile_definitions(quickshell-core PRIVATE QS_WAYLAND)
target_link_libraries(quickshell-core PRIVATE quickshell-wayland-activation)
endif()

if (BUILD_TESTING)
add_subdirectory(test)
endif()
26 changes: 26 additions & 0 deletions src/core/qmlglobal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
#include <qguiapplication.h>
#include <qicon.h>
#include <qjsengine.h>
#include <qjsvalue.h>
#include <qlist.h>
#include <qlogging.h>
#include <qobject.h>
#include <qpointer.h>
#include <qprocess.h>
#include <qqmlcontext.h>
#include <qqmlengine.h>
Expand All @@ -34,6 +36,10 @@
#include "rootwrapper.hpp"
#include "scanenv.hpp"

#ifdef QS_WAYLAND
#include "../wayland/activation.hpp"
#endif

QuickshellSettings::QuickshellSettings() {
QObject::connect(
static_cast<QGuiApplication*>(QGuiApplication::instance()), // NOLINT
Expand Down Expand Up @@ -330,6 +336,26 @@ void QuickshellGlobal::execDetached(const qs::io::process::ProcessContext& conte
process.startDetached();
}

void QuickshellGlobal::requestActivationToken(const QJSValue& callback) {
if (!callback.isCallable()) {
qCritical() << "requestActivationToken called with a non-function callback:"
<< callback.toString();
return;
}

// The engine may be gone by the time the token arrives, e.g. after a reload.
const auto call = [self = QPointer(this), callback](const QString& token) {
if (!self) return;
const auto result = callback.call({token});
if (result.isError()) qCritical() << "Error in activation token callback:" << result.toString();
};

#ifdef QS_WAYLAND
if (qs::wayland::activation::requestActivationToken(call)) return;
#endif
call({});
}

QString QuickshellGlobal::iconPath(const QString& icon) {
return IconImageProvider::requestString(icon);
}
Expand Down
16 changes: 16 additions & 0 deletions src/core/qmlglobal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <qcontainerfwd.h>
#include <qhash.h>
#include <qjsengine.h>
#include <qjsvalue.h>
#include <qlist.h>
#include <qobject.h>
#include <qqmlengine.h>
Expand Down Expand Up @@ -208,6 +209,21 @@ class QuickshellGlobal: public QObject {
/// This function is equivalent to @@Quickshell.Io.Process.startDetached().
Q_INVOKABLE static void execDetached(const qs::io::process::ProcessContext& context);

/// Requests an xdg-activation token for the most recent input and calls `callback` with it,
/// or with an empty string if one can't be requested. The callback is called exactly once,
/// and may be called before this function returns.
///
/// Pass the token to a launched program as `XDG_ACTIVATION_TOKEN` so the program can raise its
/// window for the input that launched it, e.g. an app that is already running.
///
/// ```qml
/// Quickshell.requestActivationToken(token => {
/// const env = token ? ["env", `XDG_ACTIVATION_TOKEN=${token}`] : [];
/// Quickshell.execDetached([...env, "myapp"]);
/// });
/// ```
Q_INVOKABLE void requestActivationToken(const QJSValue& callback);

/// Returns a string usable for a @@QtQuick.Image.source for a given system icon.
///
/// > [!INFO] By default, icons are loaded from the theme selected by the qt platform theme,
Expand Down
7 changes: 7 additions & 0 deletions src/services/notifications/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@ target_link_libraries(quickshell-service-notifications PRIVATE Qt::Quick Qt::DBu
target_link_libraries(quickshell PRIVATE quickshell-service-notificationsplugin)

qs_module_pch(quickshell-service-notifications SET dbus)

if (WAYLAND)
target_compile_definitions(quickshell-service-notifications PRIVATE QS_WAYLAND)
target_link_libraries(quickshell-service-notifications PRIVATE
quickshell-wayland-activation
)
endif()
43 changes: 42 additions & 1 deletion src/services/notifications/notification.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#include "notification.hpp"
#include <utility>

#include <qcontainerfwd.h>
#include <qdbusargument.h>
#include <qlist.h>
#include <qlogging.h>
#include <qloggingcategory.h>
#include <qobject.h>
#include <qpointer.h>
#include <qproperty.h>
#include <qstring.h>
#include <qtmetamacros.h>
#include <qtypes.h>

Expand All @@ -16,6 +19,10 @@
#include "dbusimage.hpp"
#include "server.hpp"

#ifdef QS_WAYLAND
#include "../../wayland/activation.hpp"
#endif

namespace qs::service::notifications {

// NOLINTNEXTLINE(misc-use-internal-linkage)
Expand Down Expand Up @@ -48,11 +55,31 @@ void NotificationAction::invoke() {
return;
}

NotificationServer::instance()->ActionInvoked(this->notification->id(), this->mIdentifier);
this->notification->beginInvoke();

#ifdef QS_WAYLAND
// The token has to go out before ActionInvoked, so hold the action until the compositor sends it.
const auto callback = [action = QPointer(this)](const QString& token) {
if (action) action->sendInvoked(token);
};

if (qs::wayland::activation::requestActivationToken(callback)) return;
#endif
this->sendInvoked({});
}

void NotificationAction::sendInvoked(const QString& token) {
if (this->notification->isRetained()) return;

auto* server = NotificationServer::instance();
if (!token.isEmpty()) server->ActivationToken(this->notification->id(), token);
server->ActionInvoked(this->notification->id(), this->mIdentifier);

if (!this->notification->bindableResident().value()) {
this->notification->close(NotificationCloseReason::Dismissed);
}

this->notification->finishInvoke();
}

void NotificationAction::setText(const QString& text) {
Expand All @@ -71,13 +98,27 @@ void Notification::close(NotificationCloseReason::Enum reason) {
return;
}

if (reason != 0 && this->mPendingInvokes > 0) {
this->mDeferredCloseReason = reason;
return;
}

this->mCloseReason = reason;

if (reason != 0) {
NotificationServer::instance()->deleteNotification(this, reason);
}
}

void Notification::beginInvoke() { this->mPendingInvokes++; }

void Notification::finishInvoke() {
if (--this->mPendingInvokes > 0 || this->mDeferredCloseReason == 0) return;
this->close(
std::exchange(this->mDeferredCloseReason, static_cast<NotificationCloseReason::Enum>(0))
);
}

void Notification::sendInlineReply(const QString& replyText) {
if (!NotificationServer::instance()->support.inlineReply) {
qCritical() << "Inline reply support disabled on server";
Expand Down
10 changes: 10 additions & 0 deletions src/services/notifications/notification.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ class Notification

void close(NotificationCloseReason::Enum reason);

// Closes requested between these calls wait for finishInvoke, so the client
// gets ActionInvoked before NotificationClosed.
void beginInvoke();
void finishInvoke();

[[nodiscard]] quint32 id() const;
[[nodiscard]] bool isTracked() const;

Expand Down Expand Up @@ -207,6 +212,9 @@ class Notification
private:
quint32 mId;
NotificationCloseReason::Enum mCloseReason = NotificationCloseReason::Dismissed;
qsizetype mPendingInvokes = 0;
NotificationCloseReason::Enum mDeferredCloseReason =
static_cast<NotificationCloseReason::Enum>(0);
bool mLastGeneration = false;
NotificationImage mImagePixmap;
QList<NotificationAction*> mActions;
Expand Down Expand Up @@ -268,6 +276,8 @@ class NotificationAction: public QObject {
void textChanged();

private:
void sendInvoked(const QString& token);

Notification* notification;
QString mIdentifier;
QString mText;
Expand Down
1 change: 1 addition & 0 deletions src/services/notifications/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class NotificationServer: public QObject {
// NOLINTBEGIN
void NotificationClosed(quint32 id, quint32 reason);
void ActionInvoked(quint32 id, QString action);
void ActivationToken(quint32 id, QString activationToken);
void NotificationReplied(quint32 id, QString replyText);
// NOLINTEND

Expand Down
61 changes: 31 additions & 30 deletions src/wayland/activation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <private/qwaylandintegration_p.h>
#include <private/qwaylandwindow_p.h>
#include <qguiapplication.h>
#include <qnamespace.h>
#include <qobject.h>
#include <qpointer.h>
#include <qstring.h>
Expand All @@ -29,61 +28,63 @@ class ActivationManager
ActivationManager(): QWaylandClientExtensionTemplate(1) { this->initialize(); }
};

// One token request with its own callback. It isn't owned by the window, so the token still
// arrives if the window is closed or destroyed before the compositor answers.
class ActivationToken
: public QObject
, public QtWayland::xdg_activation_token_v1 {
public:
ActivationToken(::xdg_activation_token_v1* token, QtWaylandClient::QWaylandWindow* window)
: QObject(window)
, QtWayland::xdg_activation_token_v1(token)
, window(window) {}
ActivationToken(::xdg_activation_token_v1* token, std::function<void(const QString&)> callback)
: QtWayland::xdg_activation_token_v1(token)
, callback(std::move(callback)) {}

~ActivationToken() override { this->destroy(); }
Q_DISABLE_COPY_MOVE(ActivationToken);

private:
void xdg_activation_token_v1_done(const QString& token) override {
this->deleteLater();
if (this->window) emit this->window->xdgActivationTokenCreated(token);
this->callback(token);
}

QPointer<QtWaylandClient::QWaylandWindow> window;
std::function<void(const QString&)> callback;
};

bool sendRequest(
QtWaylandClient::QWaylandWindow* window,
quint32 serial,
std::function<void(const QString&)> callback
) {
auto* seat = window->display()->lastInputDevice();

static auto* manager = new ActivationManager(); // NOLINT
if (!manager->isActive() || !window->surface() || !seat) return false;

auto* token = new ActivationToken(manager->get_activation_token(), std::move(callback));
token->set_serial(serial, seat->object());
token->set_surface(window->surface());
token->commit();
return true;
}

} // namespace

bool requestActivationToken(std::function<void(const QString&)> callback) {
if (QGuiApplication::platformName() != "wayland") return false;

auto* display = QtWaylandClient::QWaylandIntegration::instance()->display();
auto* window = display->lastInputWindow();
if (!window || !window->surface() || !display->lastInputDevice()) return false;

QObject::connect(
window,
&QtWaylandClient::QWaylandWindow::xdgActivationTokenCreated,
window,
std::move(callback),
Qt::SingleShotConnection
);
window->requestXdgActivationToken(display->lastInputSerial());
return true;
if (!window) return false;

return sendRequest(window, display->lastInputSerial(), std::move(callback));
}

void requestXdgActivationToken(QtWaylandClient::QWaylandWindow* window, quint32 serial) {
auto* display = window->display();
auto* seat = display->lastInputDevice();

static auto* manager = new ActivationManager(); // NOLINT
if (!manager->isActive() || !window->surface() || !seat) {
emit window->xdgActivationTokenCreated({});
return;
}
const auto callback = [window = QPointer(window)](const QString& token) {
if (window) emit window->xdgActivationTokenCreated(token);
};

auto* token = new ActivationToken(manager->get_activation_token(), window);
token->set_serial(serial, seat->object());
token->set_surface(window->surface());
token->commit();
if (!sendRequest(window, serial, callback)) emit window->xdgActivationTokenCreated({});
}

} // namespace qs::wayland::activation
Loading