Make it possible to tell if a tab currently has focus #22067

Closed
opened 2026-01-31 08:02:32 +00:00 by claunia · 4 comments
Owner

Originally created by @dgl on GitHub (Aug 9, 2024).

Description of the new feature/enhancement

It would be useful for a program running in a terminal to be able to tell whether it currently has focus. My use case for this is after a command finishes, I sometimes want my shell to ring the bell, which makes me notice the terminal, but this is annoying if I currently have the window focused.

The FocusIn/FocusOut reporting alone isn't enough, as that needs a focus change to happen and it's not always possible to have reporting on when running an arbitrary program.

Proposed technical implementation details (optional)

There is some discussion on https://unix.stackexchange.com/questions/480052/how-do-i-detect-whether-my-terminal-has-focus-in-the-gui-from-a-shell-script

Apparently GNOME based terminals (VTE) send an event immediately when FocusIn/FocusOut reporting is turned on. In some ways this would be the easiest way to implement this, as other terminals do already do it, so programs have to handle this. If other terminals weren't doing it, I would be against it, but given they are programs have to deal with it anyway.

Alternatively maybe wt.exe could allow querying for whether a $WT_SESSION currently has focus (although the details of that seem harder, some X11 terminals have a $WINDOWID which directly corresponds to the X11 window ID, I don't know how to go from the guid session ID to anything useful).

Originally created by @dgl on GitHub (Aug 9, 2024). <!-- 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 I ACKNOWLEDGE THE FOLLOWING BEFORE PROCEEDING: 1. If I delete this entire template and go my own path, the core team may close my issue without further explanation or engagement. 2. If I list multiple bugs/concerns in this one issue, the core team may close my issue without further explanation or engagement. 3. If I write an issue that has many duplicates, the core team may close my issue without further explanation or engagement (and without necessarily spending time to find the exact duplicate ID number). 4. If I leave the title incomplete when filing the issue, the core team may close my issue without further explanation or engagement. 5. If I file something completely blank in the body, the core team may close my issue without further explanation or engagement. All good? Then proceed! --> # Description of the new feature/enhancement It would be useful for a program running in a terminal to be able to tell whether it currently has focus. My use case for this is after a command finishes, I sometimes want my shell to ring the bell, which makes me notice the terminal, but this is annoying if I currently have the window focused. The FocusIn/FocusOut reporting alone isn't enough, as that needs a focus change to happen and it's not always possible to have reporting on when running an arbitrary program. <!-- A clear and concise description of what the problem is that the new feature would solve. Describe why and how a user would use this new functionality (if applicable). --> # Proposed technical implementation details (optional) There is some discussion on https://unix.stackexchange.com/questions/480052/how-do-i-detect-whether-my-terminal-has-focus-in-the-gui-from-a-shell-script Apparently GNOME based terminals (VTE) send an event immediately when FocusIn/FocusOut reporting is turned on. In some ways this would be the easiest way to implement this, as other terminals do already do it, so programs have to handle this. If other terminals weren't doing it, I would be against it, but given they are programs have to deal with it anyway. Alternatively maybe `wt.exe` could allow querying for whether a `$WT_SESSION` currently has focus (although the details of that seem harder, some X11 terminals have a `$WINDOWID` which directly corresponds to the X11 window ID, I don't know how to go from the guid session ID to anything useful). <!-- A clear and concise description of what you want to happen. -->
Author
Owner

@carlos-zamora commented on GitHub (Aug 14, 2024):

Thanks for filing this! The VT approach is probably best. @j4james what do you think?

@carlos-zamora commented on GitHub (Aug 14, 2024): Thanks for filing this! The VT approach is probably best. @j4james what do you think?
Author
Owner

@j4james commented on GitHub (Aug 15, 2024):

It seems like a reasonable extension, but this is xterm's protocol, so it should be up to Thomas to decide whether it's OK to change or not. If xterm decides to support the extension, then I'd be OK with us adopting it too. Otherwise I'd be hesitant to encourage what could be a breaking change so someone else's protocol.

@j4james commented on GitHub (Aug 15, 2024): It seems like a reasonable extension, but this is xterm's protocol, so it should be up to Thomas to decide whether it's OK to change or not. If xterm decides to support the extension, then I'd be OK with us adopting it too. Otherwise I'd be hesitant to encourage what could be a breaking change so someone else's protocol.
Author
Owner

@zadjii-msft commented on GitHub (Aug 20, 2024):

Taking a step back: if your goal is ultimately:

My use case for this is after a command finishes, I sometimes want my shell to ring the bell, which makes me notice the terminal, but this is annoying if I currently have the window focused

#7955 has quite a bit of related discussion. Especially in https://github.com/microsoft/terminal/issues/7955#issuecomment-1303740796. I believe the "$profile.notifyOnNextPrompt": "audible" would basically be what you're looking for/?

@zadjii-msft commented on GitHub (Aug 20, 2024): Taking a step back: if your goal is ultimately: > My use case for this is after a command finishes, I sometimes want my shell to ring the bell, which makes me notice the terminal, but this is annoying if I currently have the window focused #7955 has quite a bit of related discussion. Especially in https://github.com/microsoft/terminal/issues/7955#issuecomment-1303740796. I believe the `"$profile.notifyOnNextPrompt": "audible"` would basically be what you're looking for/?
Author
Owner

@microsoft-github-policy-service[bot] commented on GitHub (Aug 24, 2024):

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

@microsoft-github-policy-service[bot] commented on GitHub (Aug 24, 2024): This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for **4 days**. It will be closed if no further activity occurs **within 3 days of this comment**. <!-- Policy app identification https://img.shields.io/static/v1?label=PullRequestIssueManagement. -->
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#22067