Bug 442265 - Activating an app from konsole has no activation environment set
Summary: Activating an app from konsole has no activation environment set
Status: CONFIRMED
Alias: None
Product: kwin
Classification: Plasma
Component: wayland-generic (other bugs)
Version First Reported In: git master
Platform: Other Linux
: NOR normal
Target Milestone: ---
Assignee: KWin default assignee
URL:
Keywords: wayland-only
: 473237 512715 513559 (view as bug list)
Depends on:
Blocks: 460318
  Show dependency tree / graph
 
Reported: 2021-09-10 15:50 UTC by Nate Graham
Modified: 2026-08-21 23:15 UTC (History)
25 users (show)

See Also:
Latest Commit:
Version Fixed/Implemented In:
Sentry Crash Report:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Nate Graham 2021-09-10 15:50:57 UTC
STEPS TO REPRODUCE
1. Launch Kate using Kickoff or the Task Manager
2. Open Konsole
3. enter `kate [path to file]`


OBSERVED RESULT
Kate opens the file, but does not get brought forward automatically.


EXPECTED RESULT
Kate opens the file, but gets brought forward.
Comment 1 David Edmundson 2021-09-10 15:53:25 UTC
This is kate at fault.
Comment 2 Nate Graham 2021-09-10 15:55:52 UTC
It's not just Kate, happens happen for any app though.
Comment 3 David Edmundson 2021-09-10 16:04:28 UTC
We have a few options here, all with different downsides.

We could *somehow* have konsole put an XDG_ACTIVATION token into the current bash whenever konsole gets focus. I don't know if this is feasible, we're already forked from konsole at this point.

We could have it so calling request_activate with an invalid token still activates the window when "Focus stealing prevention" is set to None/Low.
Fastest and laziest solution that works well shortterm, but will definitely come back to haunt us when inevitability no-one fixes anything.

We could have kstart5 or equivalent import the window activation protocol, then it can create a token. It means people using an alias, but that's probably fine.

We could do something konsole specific https://invent.kde.org/utilities/konsole/-/commit/29440f0c2453552a050815b80a3fac3129fdf254 and requires all people to either use an alias or work it into a kstart5 equivalent.
Comment 4 Nate Graham 2021-09-10 16:13:01 UTC
> We could have it so calling request_activate with an invalid token still activates the
> window when "Focus stealing prevention" is set to None/Low.
Definitely get what you mean about fastest and laziest solution that will come back to haunt us, but I feel like it's the only general purpose way to do this short of re-engineering the entirely world around us. If we don't do this, we will still be tracking down bugs forever--just different ones, in each individual app--but the user experience will suffer in the meantime.

Asking people to use an alias or kstart is probably not going to work as it requires that people 1) know this 2) remember to do it and 3) type more characters/do more work. In practice they won't.
Comment 5 Nicolas Fella 2022-10-11 18:58:32 UTC
As-is using kstart5 doesn't help. kstart5 is a windowless application, so it doesn't have a focussed surface that we can transfer focus from
Comment 6 Nicolas Fella 2023-08-10 12:39:23 UTC
*** Bug 473237 has been marked as a duplicate of this bug. ***
Comment 7 Nick Shaforostoff 2023-08-10 20:32:42 UTC
why double clicking a text file in dolphin raises already running Kate instance correctly? Why can't Konsole do the same?
Comment 8 Nate Graham 2023-08-10 20:35:38 UTC
Because of the issue tracked by this bug report.
Comment 9 Nicolas Fella 2023-08-10 20:38:35 UTC
(In reply to Nick Shaforostoff from comment #7)
> why double clicking a text file in dolphin raises already running Kate
> instance correctly? Why can't Konsole do the same?

Because Dolphin knows that a file/window is being opened so it can request an activation token from the compositor.

Konsole doesn't know that running "kate bla.txt" opens a file but "ls bla.txt" doesn't. Konsole only hosts bash or whatever shell you are using, it doesn't know what's happening inside that
Comment 10 Nick Shaforostoff 2023-08-11 07:54:56 UTC
Is there a workaround? This is very important part of my workflow
Comment 11 Nick Shaforostoff 2023-08-14 13:26:19 UTC
Ok, so at least I have got it working locally

git clone https://invent.kde.org/utilities/konsole.git --branch release/22.12 
cd konsole
git cherry-pick 29440f0c2453552a050815b80a3fac3129fdf254
mkdir build
cd build
cmake ..
sudo mv bin/konsole /usr/bin/konsole

vim /usr/local/bin/open
to add:
 XDG_ACTIVATION_TOKEN=`qdbus $KONSOLE_DBUS_SERVICE $KONSOLE_DBUS_WINDOW org.kde.konsole.Window.activationToken` kate $@

chmod a+x /usr/local/bin/open

and BTW a related BUG: 442721
Comment 12 Nick Shaforostoff 2023-10-14 16:12:43 UTC
after apt dist-upgrade which updated kde to 23.08.1 the mentioned workaround no longer works :(
Comment 13 Christoph Cullmann 2025-07-28 18:18:15 UTC
Would that work:

* Konsole exports an env var that contains a dbus interface
* e.g. Kate checks on start if that env var is set and contacts konsole over that interface to get a activation token
* Konsole generates the token (it has the current active window) and sends it
* Kate uses it to activate the window
Comment 14 Christoph Cullmann 2025-08-02 14:50:56 UTC
I have now:

https://invent.kde.org/utilities/kate/-/merge_requests/1841

&

https://invent.kde.org/utilities/konsole/-/merge_requests/1119

Together they bring Kate back to the X11 experience.

Naturally a generic interface would be even nicer, but I think that is a minimal invasive workaround.
Comment 15 David Edmundson 2025-08-04 22:01:21 UTC
FYI at some point I know someone (David R) tried #11 with the zsh "preexec" hook, which runs before doing any command.

It's a manual user step, and it's a waste when you're doing "ls" or whatever, but I think it worked.
Comment 16 Christoph Cullmann 2025-08-05 14:47:58 UTC
Yeah, but that is an even worse hack and needs manual work for each user and shell variant. At least for Kate I prefer my hack, even for the other the Konsole patch is needed.

I would assume that patch is not that controversial?
Comment 17 Christoph Cullmann 2025-08-05 15:48:24 UTC
Updated

https://invent.kde.org/utilities/kate/-/merge_requests/1841

&

https://invent.kde.org/utilities/konsole/-/merge_requests/1119

The kate.git patch should now work for KWrite and Kate and the Konsole patch should be more save with the single shot connect and we always do send a dbus response, in worst case with to token.
Comment 18 Christoph Cullmann 2025-08-13 10:06:14 UTC
Git commit 0ae9ad22ce34d4e368e2c1dcad8cccb729ecab44 by Christoph Cullmann.
Committed on 13/08/2025 at 09:28.
Pushed by cullmann into branch 'master'.

ask Konsole for activation token

this will allow us to pass a valid token to a already existing
instance if invoked from Konsole

M  +2    -2    apps/kate/main.cpp
M  +1    -1    apps/kwrite/main.cpp
M  +32   -1    apps/lib/kateapp.cpp
M  +7    -6    apps/lib/kateapp.h

https://invent.kde.org/utilities/kate/-/commit/0ae9ad22ce34d4e368e2c1dcad8cccb729ecab44
Comment 19 Christoph Cullmann 2025-08-13 11:10:04 UTC
Git commit fc6532e97e4227feafdace88c9120a66a16cfff0 by Christoph Cullmann.
Committed on 13/08/2025 at 11:10.
Pushed by cullmann into branch 'master'.

add a DBus slot to get an XDG activation token

Will check if the passed shellSessionId is the current one for safety.
Will try to generate a token and pass it back.

Allows e.g. Kate to properly activate existing instance windows

M  +52   -0    src/session/Session.cpp
M  +18   -0    src/session/Session.h

https://invent.kde.org/utilities/konsole/-/commit/fc6532e97e4227feafdace88c9120a66a16cfff0
Comment 20 Christoph Cullmann 2025-08-13 17:27:16 UTC
At least for Kate with Konsole that is now fixed, but that is no generic solution. Perhaps one can standardize such a DBus interface for terminal emulators or there are even better solutions. If something better comes up, I will help to add it to Kate and Konsole, until then, a good old hack is IMHO better than an infinite regression.
Comment 21 Christoph Cullmann 2025-12-13 16:41:09 UTC
Konsole & Kate and Konsole & Konsole work with 25.12 with my 'hacks'.

Perhaps, if no bugs are seen with that in 25.12 we can think of moving that hack to a more central place or finding a more generic solution.
Comment 22 Nate Graham 2025-12-19 16:50:24 UTC
*** Bug 513559 has been marked as a duplicate of this bug. ***
Comment 23 Guido 2026-02-25 10:42:18 UTC
Are you sure the bug is still there? 
I ran several tests with LibreOffice or Gimp instead of Kate, and in both cases the app launched from Konsole comes to the foreground. I also tried from another terminal, Foot, and it works the same way. Even from xterm it works. I suspected it was something to do with fish, so I tried bash, with the same result.
Comment 24 Nate Graham 2026-02-25 14:22:37 UTC
The issue can be worked around as of Plasma 6.5 and newer by turning off focus stealing prevention entirely. I've done this, and it's possible you have too but forgot about it!
Comment 25 Guido 2026-02-25 14:32:59 UTC
(In reply to Nate Graham from comment #24)
> The issue can be worked around as of Plasma 6.5 and newer by turning off
> focus stealing prevention entirely. I've done this, and it's possible you
> have too but forgot about it!

I have "low" in stealing focus prevention.
Comment 26 Nate Graham 2026-03-02 21:18:56 UTC
That's weird, it doesn't work (as expected) for me with low focus stealing prevention. Make sure GIMP and LibreOffice are already running when you do it; if the apps get launched from the terminal, everything does work, but that's not what this bug report is about.
Comment 27 Guido 2026-03-02 22:53:25 UTC
(In reply to Nate Graham from comment #26)
> That's weird, it doesn't work (as expected) for me with low focus stealing
> prevention. Make sure GIMP and LibreOffice are already running when you do
> it; if the apps get launched from the terminal, everything does work, but
> that's not what this bug report is about.

Yes, I did that, I followed those instructions.
Comment 28 Nate Graham 2026-03-02 23:18:02 UTC
I'm at a loss to explain why you don't see the issue, then.
Comment 29 Guido 2026-03-03 18:26:27 UTC
(In reply to Nate Graham from comment #28)
> I'm at a loss to explain why you don't see the issue, then.

I did it again to confirm.

1. I launched Gimp from Kicker [https://userbase.kde.org/Plasma/Kicker].
2. I minimised Gimp.
3. I opened Konsole.
4. cd Images
5. gimp filename.jpg

At that point, Gimp maximise itself and appear in the foreground.
Comment 30 Oded Arbel 2026-03-05 10:52:58 UTC
(In reply to Guido from comment #29)
> (In reply to Nate Graham from comment #28)
> > I'm at a loss to explain why you don't see the issue, then.
> 
> I did it again to confirm.
> ...
> At that point, Gimp maximise itself and appear in the foreground.

On my system - Neon with Plasma 6.6.1, with default focus stealing prevention ("low") - I cannot reproduce GIMP or LibreOffice raising themselves when using a command line in Konsole to open a file in an existing instance - if there is no immediate interaction requested by the target app, it stays in the background and does not activate itself.

If the app requires immediate interaction - such as GIMP color space conversion dialog, or LibreOffice CSV import dialog - then the new dialog appears active, and when dismissed - the full application is now active.

I've tested with LibreOffice Calc installed locally and with GIMP from Flatpak.
Comment 31 Vlad Zahorodnii 2026-05-27 13:40:45 UTC
*** Bug 512715 has been marked as a duplicate of this bug. ***
Comment 32 Méven Car 2026-06-27 08:20:50 UTC
Made a MR: https://invent.kde.org/utilities/konsole/-/merge_requests/1236

Using OSC to pass in token to GUI apps only.