Tk Source Code

View Ticket
Login
Ticket UUID: 0c3bb74e3777ea38ddee91ff7423a20b2e7b25c
Title: Without a window manager, an application keeps the implicit focus after another application takes the focus
Type: Bug Version: 8.6.18, 9.0.5, 9.1.0
Submitter: serhiy.storchaka Created on: 2026-10-09 08:35:43
Subsystem: 55. [focus] Assigned To: aku
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2026-10-22 17:22:33
Resolution: Fixed Closed By: serhiy.storchaka
    Closed on: 2026-10-13 17:41:49
Description:

Without a window manager, an application whose toplevel gets the focus implicitly, because it is under the pointer while the X focus follows the pointer (PointerRoot), keeps believing that it has the focus after another application takes it with focus -force, as long as the pointer stays there. [focus] still returns the toplevel, and key events generated for its windows go to it.

Tk takes the focus on the FocusIn event with detail NotifyPointer and releases it on a Leave event, but ignores the matching FocusOut event with detail NotifyPointer, assuming that other focus events will update the state. This is only true if the focus moves within the same application.

Tk itself sets the X focus to PointerRoot when the pointer leaves a window which got the focus implicitly, so this can happen after moving the pointer. It made unixEmbed-7.1 fail depending on which tests ran before.

Proposed fix in branch x11-implicit-focus-release, with test focus-9.1.

User Comments: serhiy.storchaka added on 2026-10-13 17:41:08:

You're right, thanks. testxfocus already kept the test off aqua, but x11 says what is meant and also excludes Wayland. Changed in main and core-9-0-branch. I also backported the fix to core-8-6-branch, with movePointerAwayFromTestArea.


erikleunissen added on 2026-10-12 21:12:33:
Oh, regarding the regression test:

Shouldn't the test constraint "unix" be "x11" ?

This to prevent that the regression test is run under Wayland or aqua.

serhiy.storchaka added on 2026-10-12 21:11:29:

Thank you for testing and merging, Erik. movePointerAwayFromTestArea is a good idea. I've already used it in branch panedwindow-sash-cursor, and will use it in new tests. It could also replace similar code in tests/unixEmbed.test and tests/focus.test.


erikleunissen added on 2026-10-12 20:53:12:
I ran the test without window manager under Xnest.

* without the fix in [3f468f0591], the test FAILS with test result: {. .}
* with the fix, the test PASSES.

On a side note:
I've seen you using the following code to move the screen pointer away from the
test area:

    event generate . <Motion> -warp 1 -x 2000 -y 2000
    controlPointerWarpTiming

To that end, I've created this utility proc:

    proc movePointerAwayFromTestArea {} {
	event generate {} <Motion> -warp 1 -x [winfo screenwidth .] \
		-y [expr {[winfo screenheight .]/2}]
	controlPointerWarpTiming
    }

The utility proc is made available to test authors through the testutils
mechanism in the domain "generic" (which is imported by default for all test 
files).

I've taken the liberty to adapt the test in branch "x11-implicit-focus-release"
to use this proc. If you agree to its usefulness, please use it for 
other/future tests also.

Thanks very much for the analysis and fix.

I've now merged the bug-fix branch into the main development branches.