Skip to content

Restore focus after the sole monitor round-trips through FALLBACK - #9209

Open
alexvcasillas wants to merge 1 commit into
omacom:quattrofrom
alexvcasillas:fix-focus-after-fallback-monitor
Open

alexvcasillas wants to merge 1 commit into
omacom:quattrofrom
alexvcasillas:fix-focus-after-fallback-monitor

Conversation

@alexvcasillas

Copy link
Copy Markdown

Fixes #9153.

Why

If the only monitor stays powered off long enough to drop its link, Hyprland moves everything onto a headless FALLBACK monitor and clears focus. When the real monitor comes back it doesn't focus anything again.

The result is a session that looks fine but takes no keyboard input. The window keeps its focus border, the bar still names it, hyprctl activewindow still reports it, and nothing you type reaches any client. You have to switch workspace or window by hand to get it back. On a stock idle setup you hit this every time you leave the machine for more than about 10 minutes.

Upstream considers it intended, so it isn't going to be fixed there:

I don't think we enforce focus on monitor addition to not disrupt current state. With lua I think the workaround is totally reasonable

vaxerski, in discussion #15554

Omarchy's idle lock is what powers the display down, so it seemed fair to handle it here.

What

New default/hypr/monitor-focus.lua, required from default/hypr/omarchy.lua.

It remembers the focused workspace and window when a real monitor goes away, then puts focus back once the monitor returns.

  • Only a FALLBACK round trip counts. Unplugging one of several monitors leaves things alone, since FALLBACK only appears when no real output is left.
  • Ids are stored, not objects. The objects don't survive the migration.
  • Focusing the workspace Hyprland already records as focused is a no-op, so in that case it hops through another workspace to make focus land. This runs behind the lock screen, so you don't see it.
  • If the saved window is gone by the time the monitor is back, it falls back to the workspace's most recently focused window.
  • A pending restore is invalidated by any newer monitor event, so a fast second cycle can't replay a stale snapshot.

How it was verified

On Omarchy 4.0.1-1, Hyprland 0.56.2-1, single LG UltraGear on HDMI-A-1, NVIDIA RTX 4070.

Two real occurrences, a 4h52m lock and a 22m36s one. Both restored focus straight away, no manual workspace switch. Socket trace from the second:

19:00:53 activewindow>>,          <-- focus cleared by the migration
19:00:55 monitoradded>>HDMI-A-1
19:00:55 monitorremoved>>FALLBACK
19:00:56 workspace>>2             <-- the hop
19:00:56 workspace>>1             <-- back where I was
19:01:03 (unlocked, typing worked)

Before the change, the same wake produced no activewindow event at all after the monitor returned, and input stayed dead until a manual switch.

restore_focus was also exercised directly for the branches a real wake doesn't reach in one run: hop taken when the target workspace is already current, hop skipped when it isn't, and the most recently focused window used when the saved address no longer resolves.

Left out

No retry if the restore itself doesn't take. One dispatch 500ms after the monitor is back was enough in both real runs, and retries risk yanking you off a workspace you've since moved to. Could we add one later if it turns out to be flaky on other hardware? 🤔

The 500ms delay is inherited from the workaround in the upstream discussion. I didn't tune it, it worked first time here.

@alexvcasillas
alexvcasillas force-pushed the fix-focus-after-fallback-monitor branch from 4e31978 to 669bdf6 Compare August 31, 2026 07:03
@omarchybot omarchybot added the bug Something isn't working label Sep 27, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

I ran this on an Omarchy ISO worker (Hyprland 0.56.2, one virtual output). It fixes the round trip when the session is unlocked, but not with the session locked, which is the sequence #9153 describes.

How it was driven. I forced the only output's DRM connector off and back on, and sent the hotplug uevent Aquamarine listens for. That produces the same sequence as your trace: monitorremoved for the real output, monitoradded>>FALLBACK, then monitoradded for the real output and monitorremoved>>FALLBACK. Input was measured with wtype into a foot window running cat > file.

Unlocked: fixed. On quattro as it stands, nothing typed after the round trip reaches the window (2/2). On this head, and on this head merged onto current quattro, it does (7/7, one of them with a 20-second outage). With the require line removed, it fails again (2/2).

Locked: not fixed. I locked with omarchy-system-lock before the round trip, then unlocked with the password and typed. Input is lost on quattro, and also on this head and the merge (4/4). hyprctl activewindow still names the foot window afterwards, which is the symptom in #9153. A plain lock and unlock with no round trip keeps input on both trees, so it is the round trip that breaks it.

An instrumented copy of monitor-focus.lua shows why. In the locked run, monitor.removed saved workspace 1 and the window, monitor.added saw went_headless, the timer fired with a current generation, and restore_focus ran with the workspace found. No workspace>> or activewindow>> event followed, so the dispatches don't take while the ext-session-lock is held. After unlocking, the same hop run by hand (hl.dsp.focus({ workspace = 2 }), then { workspace = 1 }) brings input back immediately. So the restore works; it just runs too early. The Lua event list in this Hyprland has no lock or unlock event, so the module can't wait for the unlock by itself. Running the restore after unlock, for example from finishUnlock in shell/plugins/lock/Service.qml, is the direction I'd expect to work. That choice is yours and the maintainer's, so I haven't pushed anything.

Your trace shows workspace>>2 and workspace>>1 firing before you unlocked, which I never saw under quattro's lock. If your machine locks some other way than omarchy-system-lock on current quattro, that would explain the difference.

Other checks: test/shell.d hyprland-default-config, config, binding-conflicts, screensaver-workspace, brightness-display and screenrecording all pass on the merge. ./test/cli stops at one vscode-theme assertion that fails the same way on quattro without this change, so it says nothing about this PR. No other open pull request touches this.

Reviewed by Claude Opus 5.5. The second opinion (Codex Medium) did not run, because the reviewer's daily budget was spent, so this rests on one model plus the worker measurements above. Waiting on the author.

@alexvcasillas

Copy link
Copy Markdown
Author

Thanks for the thorough review. I've retested on current quattro (1676904, Hyprland 0.56.2, NVIDIA RTX 4070, single HDMI-A-1 monitor), locking with omarchy-system-lock and making the only monitor drop to FALLBACK and come back while locked.

With this PR's module disabled, input survives the round trip. After unlocking, typing reaches the previously focused window with no restore at all:

14:17:48.363 session-locked=true
14:19:30.141 monitorremoved>>HDMI-A-1 / monitoradded>>FALLBACK
14:19:32.137 monitoradded>>HDMI-A-1
14:19:32.198 workspace>>3 / monitorremoved>>FALLBACK
14:19:39.802 unlocked: no focus events, typing reaches the window

I think #15655 fixed this as a side effect, since monitor-workspaces.lua reconciles and refocuses the window on monitor.added. I haven't proven that it's the responsible commit.

It also means the follow-up I'd drafted (retrying the restore from omarchy-system-wake after unlock) doesn't work on current quattro. The workspace.active that fires when the monitor returns clears the saved state before the retry can use it, so I'm not pushing it.

Could you re-run your locked repro on current quattro without this PR? If it no longer loses input there either, I'll close this as fixed by #15655.

@omarchybot

Copy link
Copy Markdown
Collaborator

@alexvcasillas I re-ran it, and I see the same as you: on current quattro the round trip no longer loses input, locked or not, and this pull request makes no measurable difference there.

How it was run. An Omarchy ISO worker, now on Hyprland 0.57.0, with one virtual output. The same harness as before: force the only connector off so everything moves to FALLBACK, bring it back, then type with wtype into a foot window running cat > file. In the locked runs I checked that the session was locked and that FALLBACK was the only output while it was off, then unlocked with the password and typed.

Tree Locked Unlocked
quattro at d367a56, without this PR input kept (4/4) input kept (2/2)
This PR merged onto quattro input kept (2/2) input kept (2/2)
This PR's own base, 943d2fc input lost (4/4) input lost (1/1)
This PR's head on that base input kept (1/1) input kept (2/2)

So the harness still catches the bug and the module still fixes it on the code it was written against, but quattro has since fixed it without the module.

Which commit. It wasn't #15655: its parent, dd07b62, already keeps input. A bisect of the unlocked round trip between 943d2fc and dd07b62, two runs per step, puts the change at 1498718 (#14482, the floating workspace mode), with its parent c09ddcf still losing input. I haven't worked out how that commit restores focus, and the bisect only covered the unlocked case, so treat it as where it changed rather than why.

One trap in the harness. On a virtual output, forcing the connector off throws away its EDID, so since #15655 the first round trip after boot comes back as a monitor monitor-workspaces.lua has never seen. It gets a new workspace block, the window lands on workspace 22 while the screen shows an empty 21, and typing goes nowhere, with or without this PR. A real monitor keeps its EDID, so the runs above were taken after one warm-up round trip had settled that. Your real-monitor result on 0.56.2 covers the case the worker can't.

The module itself, for the record. Merged onto quattro, it now conflicts in default/hypr/omarchy.lua (the require list), which is trivial to fix. Three things in default/hypr/monitor-focus.lua would also need work if it were kept: the 500ms timer at line 127 isn't cancelled when you switch workspace or window yourself in that window, so it can pull you back; the ordinary-hotplug branch at line 109 clears the saved state but leaves a pending restore live; and restore_focus returns at line 62 when the saved workspace id no longer exists, before trying the saved window, and monitor-workspaces.lua now renumbers workspaces. None of that matters if it closes.

Reviewed by Claude Opus 5.5, with a second opinion from Codex Medium and muse-wonder via pi (thinking high). Both agreed the evidence shows no benefit on current quattro (they could read this review's reasoning, so that agreement isn't fully independent). They raised the three module issues above, which I checked against the source, and pointed out that my locked script didn't assert the lock had engaged, which is why the locked runs were repeated with that check.

Waiting on you: as you offered, this can be closed as fixed on quattro. #9153 can go with it, which is for the maintainer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Keyboard input dead on the focused window after a long idle lock, workspace switch restores it

2 participants