Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[X11] Godot (mostly) refusing to give up input focus when switching to external editor when using i3 window manager #86442

Closed
dsnopek opened this issue Dec 22, 2023 · 2 comments · Fixed by #86671

Comments

@dsnopek
Copy link
Contributor

dsnopek commented Dec 22, 2023

Tested versions

Reproducible in Godot master (9d1cbab), Godot v4.2.1 and v4.1.3

(I've had this problem on all Godot 4.x versions since switching to i3 -- I used to use notion, another tiling window manager, but I don't recall if it had the same problem)

System information

Ubuntu 22.04, X11 with i3 window manager

Issue description

I have Godot configured to use VS Code as an external editor. When Godot opens VS Code, most of the time (like 1% of the time this doesn't happen), it refuses to give up input focus, even though VS Code's title bar is highlighted in the way that usually indicates that it has input focus (and Godot's is greyed out in the way that usually indicates it doesn't have input focus).

So, if I press any keys, including shortcuts, they are received by Godot rather than VS Code. This happens when Godot and VS Code are on the same screen, or on different screens. (When on different screens, it's very easy to see that my keyboard input is really going to Godot, because I can see my actions happening there.)

In order to get VS Code to have focus, I need to first switch focus back to Godot, and then switch again to VS Code (either using the mouse, or the i3 keyboard shortcuts that I have configured). Sometimes I have to do this more than once, although, usually once will do it.

(Note: switching to single window mode makes no difference, in fact, I usually have the editor in single window mode because there's other weirdnesses with i3 without single window mode.)

(Note 2: this problem doesn't happen for me with Godot 3 -- only with Godot 4.)

This seems sort of similar to these other X11 input focus issues:

However, it's not exactly the same, and, unfortunately, the PRs that fix those issues for others don't seem to fix my issue. :-/ So, I've decided to make a new issue here!

When I try using Gnome instead (on the same computer with the same Godot, VS Code and configurations of them both), this problem doesn't happen. When the external editor is opened by Godot in Gnome, it immediately has input focus and receives my key presses.

Steps to reproduce

  1. Start a session with the i3 window manager
  2. Open a Godot project
  3. Change your editor settings to use VS Code as an external editor
  4. Click the "Open Script" icon next to a node in your scene tree
  5. After VS Code opens and it's title bar is highlighted blue and Godot's title bar is greyed out (indicating the VS Code should have focus and Godot should not), press some keys and see that Godot is receiving them rather than VS Code (this is easiest to see with shortcuts like Ctrl+N)

Minimal reproduction project (MRP)

n/a - this isn't project specific

@dsnopek
Copy link
Contributor Author

dsnopek commented Dec 31, 2023

I did a little bit more investigation and it turns out that the problem goes away if I comment out the XSetInputFocus() line in this code in DisplayServerX11::process_events():

				if (event.type == ButtonPress) {
					DEBUG_LOG_X11("[%u] ButtonPress window=%lu (%u), button_index=%u \n", frame, event.xbutton.window, window_id, mb->get_button_index());

					// Ensure window focus on click.
					// RevertToPointerRoot is used to make sure we don't lose all focus in case
					// a subwindow and its parent are both destroyed.
					if (!wd.no_focus && !wd.is_popup) {
						XSetInputFocus(x11_display, wd.x11_window, RevertToPointerRoot, CurrentTime);
					}

If I were to guess, it seems like clicking the "Open Script" icon leads to setting the input focus to Godot, but that message is somehow received or acted upon later, after VS Code has already opened, leading to input focus snapping back to Godot.

I wonder if maybe we should check if the window we are clicking is already focused, and if so, don't bother setting it to be the input focus again?

@dsnopek
Copy link
Contributor Author

dsnopek commented Dec 31, 2023

If I were to guess, it seems like clicking the "Open Script" icon leads to setting the input focus to Godot, but that message is somehow received or acted upon later, after VS Code has already opened, leading to input focus snapping back to Godot.

I wonder if maybe we should check if the window we are clicking is already focused, and if so, don't bother setting it to be the input focus again?

I just posted PR #86671 which does this, and it fixes the issue for me!

@akien-mga akien-mga modified the milestones: 4.x, 4.3 Jan 18, 2024
@dsnopek dsnopek added cherrypick:4.2 Considered for cherry-picking into a future 4.2.x release and removed cherrypick:4.2 Considered for cherry-picking into a future 4.2.x release labels Jan 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants