Repository: qtile/qtile

Issue:
PyCharm popup loses focus on keypress
I've been using qtile as my daily driver for a few weeks without major issues, but yesterday I noticed that the New file popup in PyCharm isn't working properly. When it is open and focused and I press any key, the main window regains focus while the popup is still shown. The following GIF shows the problem:

![qtile-pycharm](https://user-images.githubusercontent.com/446867/106395256-fd180a00-63df-11eb-9418-ab6684419374.gif)

FWIW, it works just fine in i3, which makes me think that it might be an issue in qtile.

# Qtile version

0.16.2.dev200+ge6daea51

# Stack traces

The xprop output of the popup window is as follows:

```
_NET_WM_DESKTOP(CARDINAL) = 0
WM_STATE(WM_STATE):
		window state: Normal
		icon window: 0x0
_NET_WM_USER_TIME(CARDINAL) = 148265765
_NET_WM_WINDOW_TYPE(ATOM) = _NET_WM_WINDOW_TYPE_DIALOG
_MOTIF_DRAG_RECEIVER_INFO(_MOTIF_DRAG_RECEIVER_INFO) = 0x6c, 0x0, 0x5, 0x0, 0x3a, 0xa, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0
XdndAware(ATOM) = BITMAP
_NET_WM_STATE(ATOM) = _NET_WM_STATE_SKIP_TASKBAR
WM_HINTS(WM_HINTS):
		Client accepts input or input focus: False
		window id # of group leader: 0x100004c
WM_TRANSIENT_FOR(WINDOW): window id # 0x100004c
_NET_WM_PID(CARDINAL) = 192251
WM_PROTOCOLS(ATOM): protocols  
_MOTIF_WM_HINTS(_MOTIF_WM_HINTS) = 0x3, 0x0, 0x0, 0x0, 0x0
WM_NORMAL_HINTS(WM_SIZE_HINTS):
		program specified location: 1722, 797
		program specified size: 392 by 54
		window gravity: NorthWest
WM_CLASS(STRING) = "jetbrains-pycharm", "jetbrains-pycharm"
WM_CLIENT_LEADER(WINDOW): window id # 0x1000008
_NET_WM_NAME(UTF8_STRING) = "win90"
WM_NAME(STRING) = "win90"
```

# Configuration

https://github.com/mbenford/dotfiles/blob/master/.config/qtile/config.py



Discussion:
In case anyone else is experiencing this issue, there is a workaround:

- Go to _Help > Find Actions_, or press Ctrl+Shift+A;
- Search for _Registry_;
- Locate the _show.create.new.element.in.pop_ key and uncheck it.

Now PyCharm (or any other JetBrains IDE, for that matter) will use a modal dialog when you invoke the _New file_ action and focus won't be lost anymore when pressing keys.

![image](https://user-images.githubusercontent.com/446867/106408018-31112080-641c-11eb-8115-0c29ccac7bc6.png)

For the sake of completeness, xprop outputs the following about that modal dialog:

```
_NET_WM_DESKTOP(CARDINAL) = 0
WM_STATE(WM_STATE):
		window state: Normal
		icon window: 0x0
_NET_WM_USER_TIME(CARDINAL) = 164423046
_NET_WM_WINDOW_TYPE(ATOM) = _NET_WM_WINDOW_TYPE_DIALOG
_MOTIF_WM_HINTS(_MOTIF_WM_HINTS) = 0x3, 0x26, 0x1e, 0x0, 0x0
_MOTIF_DRAG_RECEIVER_INFO(_MOTIF_DRAG_RECEIVER_INFO) = 0x6c, 0x0, 0x5, 0x0, 0x13, 0x4, 0xc0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0
XdndAware(ATOM) = BITMAP
_NET_WM_ICON(CARDINAL) = 	Icon (80 x 80):
	(not shown)
_NET_WM_STATE(ATOM) = _NET_WM_STATE_SKIP_TASKBAR
WM_HINTS(WM_HINTS):
		Client accepts input or input focus: False
		window id # of group leader: 0xc0004c
WM_TRANSIENT_FOR(WINDOW): window id # 0xc0004c
_NET_WM_PID(CARDINAL) = 203632
WM_PROTOCOLS(ATOM): protocols  WM_DELETE_WINDOW, WM_TAKE_FOCUS
WM_CLASS(STRING) = "jetbrains-pycharm", "jetbrains-pycharm"
WM_CLIENT_LEADER(WINDOW): window id # 0xc00008
_NET_WM_ICON_NAME(UTF8_STRING) = "New File"
WM_ICON_NAME(STRING) = "New File"
_NET_WM_NAME(UTF8_STRING) = "New File"
WM_NAME(STRING) = "New File"
WM_NORMAL_HINTS(WM_SIZE_HINTS):
		user specified location: 1708, 235
		program specified location: 1708, 235
		program specified size: 420 by 107
		program specified minimum size: 195 by 107
		window gravity: NorthWest
```
I just installed pycharm from the Arch repos (as `pycharm-community-edition` version 2020.3.3-1) and do not see this bug. I notice that my xprop output is a bit different though:
```
_NET_WM_DESKTOP(CARDINAL) = 5
WM_STATE(WM_STATE):
                window state: Normal
                icon window: 0x0
_NET_WM_WINDOW_TYPE(ATOM) = _NET_WM_WINDOW_TYPE_DIALOG
_MOTIF_DRAG_RECEIVER_INFO(_MOTIF_DRAG_RECEIVER_INFO) = 0x6c, 0x0, 0x5, 0x0, 0xb, 0x2, 0x60, 0x3, 0x0, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0
XdndAware(ATOM) = BITMAP
_NET_WM_STATE(ATOM) = _NET_WM_STATE_SKIP_TASKBAR
WM_HINTS(WM_HINTS):
                Client accepts input or input focus: False
                window id # of group leader: 0x36000c9
WM_TRANSIENT_FOR(WINDOW): window id # 0x36000c9
_NET_WM_PID(CARDINAL) = 26218
WM_CLIENT_MACHINE(STRING) = "zenbook"
WM_PROTOCOLS(ATOM): protocols  
_MOTIF_WM_HINTS(_MOTIF_WM_HINTS) = 0x3, 0x0, 0x0, 0x0, 0x0
WM_NORMAL_HINTS(WM_SIZE_HINTS):
                program specified location: 2714, 547
                program specified size: 332 by 54
                window gravity: NorthWest
WM_CLASS(STRING) = "jetbrains-pycharm-ce", "jetbrains-pycharm-ce"
WM_CLIENT_LEADER(WINDOW): window id # 0x3600008
_NET_WM_NAME(UTF8_STRING) = "win7"
WM_NAME(STRING) = "win7"
```

What version are you using?
Here's my version info: `PyCharm 2020.3.3 (Professional Edition) Build #PY-203.7148.72, built on January 27, 2021`. But I guess it happens in all JetBrains IDE since I also have seen it in GoLand and WebStorm.

I can provide more information and perform additional tests as needed. Just let me know.
For me, this issue exists when I set follow_mouse_focus to False in the Qtile configuration. When it is True everything is ok.
> For me, this issue exists when I set follow_mouse_focus to False in the Qtile configuration. When it is True everything is ok.

Nice find, I have it set to True.
Just tested with follow_mouse_focus set to False and it still works fine for me.

@mbenford would it be possible for you to test with the community edition of pycharm, to see if that works fine?
@m-col I found out that when I am using my local Java installation (OpenJDK 15) as runtime it works fine. When I switch to the runtime built by Jetbrains (https://aur.archlinux.org/packages/intellij-idea-ultimate-edition-jre/) it loses focus.
- works fine:
![2021-02-23_07-19-31_screenshot](https://user-images.githubusercontent.com/50179033/108808858-dfd1f800-75a7-11eb-9af6-b0f2e9118a32.png)
- loses focus:
![2021-02-23_07-21-59_screenshot](https://user-images.githubusercontent.com/50179033/108808883-ee201400-75a7-11eb-98b4-b128eb923f42.png)

Here's how you can change your runtime https://www.jetbrains.com/help/idea/switching-boot-jdk.html
@m-col I just tested it with the community edition and the issue still happened. I also tested changing the Java runtime, as @dawids21 suggested, and it solved the problem with both editions (community and professional).

I'm not sure qtile is to blame anymore, although the problem doesn't happen in i3 regardless of the Java runtime used.
Can you send the xprops for the window in both JVMs?
Jetbrains JVM:

```
_NET_WM_DESKTOP(CARDINAL) = 1
WM_STATE(WM_STATE):
		window state: Normal
		icon window: 0x0
_NET_WM_USER_TIME(CARDINAL) = 26535309
_NET_WM_WINDOW_TYPE(ATOM) = _NET_WM_WINDOW_TYPE_DIALOG
_MOTIF_DRAG_RECEIVER_INFO(_MOTIF_DRAG_RECEIVER_INFO) = 0x6c, 0x0, 0x5, 0x0, 0x39, 0x4, 0xe0, 0x4, 0x0, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0
XdndAware(ATOM) = BITMAP
_NET_WM_STATE(ATOM) = _NET_WM_STATE_SKIP_TASKBAR
WM_HINTS(WM_HINTS):
		Client accepts input or input focus: False
		window id # of group leader: 0x4e0015d
WM_TRANSIENT_FOR(WINDOW): window id # 0x4e0015d
_NET_WM_PID(CARDINAL) = 25630
WM_PROTOCOLS(ATOM): protocols  
_MOTIF_WM_HINTS(_MOTIF_WM_HINTS) = 0x3, 0x0, 0x0, 0x0, 0x0
WM_NORMAL_HINTS(WM_SIZE_HINTS):
		program specified location: 444, 537
		program specified size: 392 by 54
		window gravity: NorthWest
WM_CLASS(STRING) = "jetbrains-pycharm", "jetbrains-pycharm"
WM_CLIENT_LEADER(WINDOW): window id # 0x4e00008
_NET_WM_NAME(UTF8_STRING) = "win12"
WM_NAME(STRING) = "win12"
```
OpenJDK 11:

```
_NET_WM_DESKTOP(CARDINAL) = 1
WM_STATE(WM_STATE):
		window state: Normal
		icon window: 0x0
_NET_WM_WINDOW_TYPE(ATOM) = _NET_WM_WINDOW_TYPE_DIALOG
_MOTIF_DRAG_RECEIVER_INFO(_MOTIF_DRAG_RECEIVER_INFO) = 0x6c, 0x0, 0x5, 0x0, 0x45, 0x1, 0xe0, 0x4, 0x0, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0
XdndAware(ATOM) = BITMAP
_NET_WM_STATE(ATOM) = _NET_WM_STATE_SKIP_TASKBAR
WM_HINTS(WM_HINTS):
		Client accepts input or input focus: False
		window id # of group leader: 0x4e0004c
WM_TRANSIENT_FOR(WINDOW): window id # 0x4e0004c
_NET_WM_PID(CARDINAL) = 26311
WM_PROTOCOLS(ATOM): protocols  
_MOTIF_WM_HINTS(_MOTIF_WM_HINTS) = 0x3, 0x0, 0x0, 0x0, 0x0
WM_NORMAL_HINTS(WM_SIZE_HINTS):
		program specified location: 1752, 537
		program specified size: 332 by 54
		window gravity: NorthWest
WM_CLASS(STRING) = "jetbrains-pycharm", "jetbrains-pycharm"
WM_CLIENT_LEADER(WINDOW): window id # 0x4e00008
_NET_WM_NAME(UTF8_STRING) = "win3"
WM_NAME(STRING) = "win3"
```

Here's a diff for the sake of convenience (OpenJDK on the left):
![image](https://user-images.githubusercontent.com/446867/109197121-63900e00-777b-11eb-8943-47363b7a7028.png)

Does the one on the left set `_NET_WM_USER_TIME` at any point (i.e. after you interact with it)?

Also -- pls could you post debug logs around when the focus is lost for both? I'm wondering if there is a `PropertyNotify` event fired, possibly by the parent window, that is pulling focus to itself or something.
also which way round were the JVMs? OpenJDK was the good one?
A bit more info on `_NET_WM_USER_TIME` in case it is involved: https://mail.gnome.org/archives/desktop-devel-list/2004-August/msg00158.html
If you set `follow_mouse_focus` to True, does it still happen? If not, then AFAICT it is a bug with the java thing setting `_NET_WM_USER_TIME` at the wrong window or time. i3 does not support `_NET_WM_USER_TIME` completely, so the bug doesn't appear. They probably don't support it because problems like this happen.
Why does our code do a focus operation when _NET_WM_USER_TIME is changed? That seems wrong according to what I'm reading here: https://specifications.freedesktop.org/wm-spec/latest/ar01s05.html which doesn't mention anything about focus...
@m-col 

> Does the one on the left set _NET_WM_USER_TIME at any point (i.e. after you interact with it)?

No. `_NET_WM_USER_TIME` is never set even after interacting with the window when OpenJDK is used.

> Also -- pls could you post debug logs around when the focus is lost for both?

Do you mean qtile logs?

> also which way round were the JVMs? OpenJDK was the good one?

Yes, OpenJDK 11 was the one that did the trick. I couldn't test with version 15 because PyCharm does not support it (it seems that JetBrains' JVM is based on OpenJDK 11, as far as I can tell).

> If you set follow_mouse_focus to True, does it still happen?

It does not! Focus isn't lost anymore when typing inside the dialog!

Resolving patch:
diff --git a/libqtile/window.py b/libqtile/window.py
--- a/libqtile/window.py
+++ b/libqtile/window.py
@@ -1230,10 +1230,6 @@ def handle_PropertyNotify(self, e):  # noqa: N802
             # are set when the property is emitted
             # self.update_state()
             self.update_state()
-        elif name == "_NET_WM_USER_TIME":
-            if not self.qtile.config.follow_mouse_focus and \
-                    self.group.current_window != self:
-                self.group.focus(self, False)
         else:
             logger.info("Unknown window property: %s", name)
         return False


Test patch: