If your terminal shell has easy access to its own X window ID, you're probably doing something wrong! They have nothing to do with each other - e.g. you could (and should) be running long jobs inside screen
which could in theory be outputting to any number of terminals anywhere in the world.
That being said, the way I solve this problem is by using the prompt's ability to update a terminal's "status"/"title" to report the shell's PID, as with the following prompt:
PS1=\u@\H:\w\$\ \[\e]2;\u@\H:\w [$$]\a\]
Any pseudo-terminal showing the shell with this prompt and PID 6399 have a title like user@host:~ [6399]
. Then, using a tool like wmctrl, you can write a bash script such as this:
win_from_pid() {
type wmctrl &>/dev/null || return 1
wmctrl -l | awk '/^.*\['"$1"'\]$/ { print $1 }'
}
This searches the window list and gives you the X Window ID(s) of any ending with that title. Thus, the function win_from_pid $$
can tell you your window ID(s) on the same host running the script, if any. You can figure out how to determine focus from there. :)
Do you have any idea if the focus messages you're talking about are the same as the 'focus events' described in the Tmux manual?
...focus events are requested from the terminal if supported and passed through to applications running in tmux.
Tmux isn't windowed and neither are the terminals inside it, so if not by X-window focus messages, then what kind of focus event could it be listening to? Thanks either way. – John P – 2018-10-02T11:33:02.737