Bug 464099

Summary: On Wayland, activation protocol support is intermittent
Product: [Applications] NeoChat Reporter: Nate Graham <nate>
Component: GeneralAssignee: Tobias Fella <fella>
Status: RESOLVED FIXED    
Severity: normal CC: aleixpol, carl, nicolas.fella
Priority: NOR Keywords: wayland
Version: unspecified   
Target Milestone: ---   
Platform: Other   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:
Bug Depends on:    
Bug Blocks: 460318    

Description Nate Graham 2023-01-10 14:14:24 UTC
Git master everything KDE on top of Fedora KDE 37. Probably an activation protocol implementation issue. I know this works with my browser (Firefox) because it just implemented activation protocol support, and opening links from Thunderbird (which also recently implemented activation protocol support) raises Firefox.
Comment 1 Nicolas Fella 2023-01-10 14:20:15 UTC
Neochat does use xdg-activation when opening URLs and it works fine for me
Comment 2 Nate Graham 2023-01-10 14:25:35 UTC
Hmm, it's not working for me. Same thing when clicking on a notification background to raise NeoChat's window, in fact.

I've submitted a bug report on this before, but later closed it because it started working. But then it broke again. So it seems like the activation support is intermittent, not 100% for me.

How would one debug this?
Comment 3 Nate Graham 2023-01-10 17:24:47 UTC
And now both are working again, after being broken earlier today when I filed the bug report.
Comment 4 Nicolas Fella 2023-01-10 23:17:16 UTC
I can confirm that activation from notifications is flaky. Opening links works perfectly though.

I think I understand what's going on with notifications:
When clicking the notification Plasma requests a token and sends that to Neochat. Then the notification window closes and the active window changes from the notification window to whatever window you have open (*). KWin reacts to the active window changing (https://invent.kde.org/plasma/kwin/-/blob/master/src/xdgactivationv1.cpp#L58) and, after performing some checks, clears the currently active token. Then Neochat uses the received token to activate itself, but at that point the token KWin expects was already cleared and KWin denies the activation. 

(*) That part seems racy and doesn't always happen. Adding a QThread::sleep(1) in https://invent.kde.org/network/neochat/-/blob/master/src/windowcontroller.cpp#L43 makes it easier to reproduce
Comment 5 Nicolas Fella 2023-01-10 23:21:16 UTC
Now why does switching focus to the open window clear the token? There are some checks that try to prevent that. One of them is comparing the input serials of the current token and newly focussed window: "if (window->lastUsageSerial() < m_currentActivationToken->serial)"

Plasma's notification code always sends 0 as input serial. This is because we call KWindowSytem::lastInputSerial() with a nullptr QWindow always: https://invent.kde.org/plasma/plasma-workspace/-/blob/master/libnotificationmanager/server.cpp#L69
Comment 6 Bug Janitor Service 2023-01-11 15:20:37 UTC
A possibly relevant merge request was started @ https://invent.kde.org/plasma/plasma-workspace/-/merge_requests/2493
Comment 7 Aleix Pol 2023-01-13 14:15:53 UTC
Git commit af0f73f13bb067b6ad28535f4ef14eb631303446 by Aleix Pol Gonzalez, on behalf of Aleix Pol.
Committed on 13/01/2023 at 11:05.
Pushed by apol into branch 'master'.

notifications: Improve activation infrastructure

The Wayland activation protocol xdg_activation_v1 requires us to send
the input serial that triggers the notification as one of the inputs. So
far we were always using 0 because it's not trivial to infer which
window is the one that triggers the action.

This change adds API to specify the calling window so this is not a
problem anymore.

M  +1    -0    applets/notifications/package/contents/ui/global/Globals.qml
M  +1    -1    dataengines/notifications/notificationaction.cpp
M  +22   -0    libnotificationmanager/abstractnotificationsmodel.cpp
M  +6    -0    libnotificationmanager/abstractnotificationsmodel.h
M  +1    -0    libnotificationmanager/abstractnotificationsmodel_p.h
M  +15   -0    libnotificationmanager/notifications.cpp
M  +12   -2    libnotificationmanager/notifications.h
M  +5    -4    libnotificationmanager/notificationsmodel.cpp
M  +5    -2    libnotificationmanager/server.cpp
M  +12   -1    libnotificationmanager/server.h

https://invent.kde.org/plasma/plasma-workspace/commit/af0f73f13bb067b6ad28535f4ef14eb631303446