Bug 465530 - KWin changes focus when switching virtual desktop with an app that changes its WM_NAME and _NET_WM_NAME at runtime
Summary: KWin changes focus when switching virtual desktop with an app that changes it...
Status: REPORTED
Alias: None
Product: kwin
Classification: Plasma
Component: general (show other bugs)
Version: 5.26.90
Platform: Debian testing Linux
: NOR normal
Target Milestone: ---
Assignee: KWin default assignee
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-02-10 04:53 UTC by Oscar Fuentes
Modified: 2023-02-13 15:54 UTC (History)
1 user (show)

See Also:
Latest Commit:
Version Fixed In:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Oscar Fuentes 2023-02-10 04:53:19 UTC
When switching virtual desktops, Kwin gives the input focus to Emacs (version 29.60, entering RC stage now): let's suppose that on Desktop1 we have Emacs and Konsole, with focus on Konsole, then we switch to Desktop2 and back to Desktop1. Now the input focus is on Emacs. The z-order remains unchanged.

This was discussed on Emacs bug tracker and a developer explained:

  Window in another desktop ends up focused upon returning to that
  desktop if it changes WM_NAME and _NET_WM_NAME while in an iconic
  state.

For the record, this is the Emacs bug report with a simple recipe for reproducing the problem, in case you are interested (it requires a development version of Emacs):

https://debbugs.gnu.org/cgi/bugreport.cgi?bug=61337


SOFTWARE/OS VERSIONS
Operating System: Debian GNU/Linux 
KDE Plasma Version: 5.26.90
KDE Frameworks Version: 5.102.0
Qt Version: 5.15.8
Kernel Version: 6.1.0-3-amd64 (64-bit)
Graphics Platform: X11
Processors: 8 × Intel® Core™ i7-6700K CPU @ 4.00GHz
Memory: 62,7 GiB of RAM
Graphics Processor: Mesa Intel® HD Graphics 530
Comment 1 Nate Graham 2023-02-13 01:49:46 UTC
Why does it do that, though? That's an odd thing for a window to do.
Comment 2 Oscar Fuentes 2023-02-13 02:03:41 UTC
(In reply to Nate Graham from comment #1)
> Why does it do that, though? That's an odd thing for a window to do.

I don't know for sure, but I think that's what Emacs does to set the title bar of the iconified window (it seems some window managers support this, including KDE.) Emacs can be configured to use different strings for the iconified and the non-iconified window.
Comment 3 Nate Graham 2023-02-13 02:07:50 UTC
It doesn't need to do that; there are ways to change the window title without messing with those values. The nonstandard thing it's doing right now apparently proves confusing to KWin. :)
Comment 4 Oscar Fuentes 2023-02-13 02:30:41 UTC
(In reply to Nate Graham from comment #3)
> It doesn't need to do that; there are ways to change the window title
> without messing with those values. The nonstandard thing it's doing right
> now apparently proves confusing to KWin. :)

Would you like to give a general indication about what is the correct way of changing the window title? I'll rely your answer to the Emacs developer who implemented that feature. Thanks.
Comment 5 Nate Graham 2023-02-13 02:32:22 UTC
it depends on the UI toolkit being used, but in Qt, we typically use QWidget::setWindowTitle(). I don't know what it does under the hood, sorry.
Comment 6 Oscar Fuentes 2023-02-13 02:41:29 UTC
(In reply to Nate Graham from comment #5)
> it depends on the UI toolkit being used, but in Qt, we typically use
> QWidget::setWindowTitle(). I don't know what it does under the hood, sorry.

Emacs does not use a UI toolkit (optionally, it uses Gtk for some tasks.) I'll try looking at the sources of Qt then, thanks.
Comment 7 Oscar Fuentes 2023-02-13 03:31:30 UTC
(In reply to Nate Graham from comment #5)
> it depends on the UI toolkit being used, but in Qt, we typically use
> QWidget::setWindowTitle(). I don't know what it does under the hood, sorry.

Nate, please allow me one more question: do you *know* that the method for changing the window title described on my bug report is wrong? Because I have the impression that you know about Qt, but not about the low level X API calls.

Thanks.
Comment 8 Nate Graham 2023-02-13 03:34:49 UTC
I don't know 100%, but I do know that changing WM_NAME and _NET_WM_NAME at runtime is considered to be always wrong by KWin and Plasma developers.
Comment 9 Oscar Fuentes 2023-02-13 04:09:52 UTC
(In reply to Nate Graham from comment #8)
> I don't know 100%, but I do know that changing WM_NAME and _NET_WM_NAME at
> runtime is considered to be always wrong by KWin and Plasma developers.

Thanks.

Sorry, but what you say would amount to not changing the window's title at runtime, so I think there is a misunderstanding here.

BTW, Qt ends setting _NET_WM_NAME and, conditionally, also WM_NAME when QWidget::setWindowTitle is called:

https://code.qt.io/cgit/qt/qtbase.git/tree/src/plugins/platforms/xcb/qxcbwindow.cpp?h=dev#n2543

What the Emacs developer thinks is happening is that KWin notices that a pseudo-iconified window (I say pseudo-iconified because the window is not on the current virtual desktop) changed its title, and somehow transfers focus to that window when its virtual desktop is shown.

I hope a KWin developer will chime in for valuating this hypothesis. Until that happens, can we maintain the bug report status on a suitable state for requesting the attention of such developer?

Thanks again.
Comment 10 Nate Graham 2023-02-13 04:13:59 UTC
Oh, TIL!
Comment 11 Vlad Zahorodnii 2023-02-13 10:32:27 UTC
I'm unaware of code that could focus a window if its title changes. A shot in the dark: do you have window rules for emacs? If not, then I guess somebody would need to build a dev version of emacs and test for themselves
Comment 12 Oscar Fuentes 2023-02-13 15:16:57 UTC
(In reply to Vlad Zahorodnii from comment #11)
> I'm unaware of code that could focus a window if its title changes. A shot
> in the dark: do you have window rules for emacs? If not, then I guess
> somebody would need to build a dev version of emacs and test for themselves

Yes, I have these window rules for Emacs:

[5]
Description=Emacs Android
shortcut=Ctrl+Alt+6
shortcutrule=2
strictgeometryrule=2
title=AndroEmacs:
titlematch=2
types=1
wmclass=emacs
wmclassmatch=1

[6]
Description=Emacs
shortcut=Ctrl+Alt+1
shortcutrule=2
strictgeometryrule=2
types=1
wmclass=emacs
wmclassmatch=1

For the rule [5] I changed wmclass to "foo" (thus effectively disabling the rule) and the problem went away. So Kwin is giving focus to Emacs because of that rule. Please note that it has "title=" and "titlematch=" attributes, but the Emacs window that ends with focus do not match those criteria (in fact, in my experiment there is no window that matches that criteria.)

To recap: Emacs changes its window's title when it is iconified and de-icononified, Kwin notices the change of title and the existence of that rule [5] causes to set the input focus on Emacs' window.
Comment 13 Oscar Fuentes 2023-02-13 15:54:41 UTC
This is a simple reproducer that does not require Emacs.

Save this tcl script to foo.tcl:

# foo.tcl begins here
package require Tk

proc ct { new_title } {
    puts "Changing title to $new_title"
    wm title . $new_title
}

bind . <Unmap> { ct "Unmapped" }
bind . <Map> { ct "Mapped" }
# foo.tcl ends here

Run it from Konsole:

$ tclsh foo.tcl

Create this window rule for the window created by the script:

[aa2e10a2-9656-4729-b55f-b3a84332de67]
Description=Window settings for foo.tcl
clientmachine=localhost
strictgeometryrule=2
title=blah
titlematch=2
types=1
wmclass=tile-foo.tcl
wmclassmatch=1

It is important that wmclass matches, while  titlematch must be defined, but it is not necessary that it matches.

Set focus to Konsole. Switch to another virtual desktop and back to the original virtual desktop. Focus is now on the script's window.