From this question, I can tell, from a running program, if I am in a screen (or tmux screen) by looking at the $TERM variable.
But how can I tell if the screen is actually visible? In tmux, I can have multiple screens, and at the bottom a tab bar is displayed, with a "tab" for each screen. The useful part of this is that if there is activity in another tab, the appearance of that tab changes if there is output. For the purpose of this question, I don't care if the screen is actually visible to the user (another window might be overlapping it), merely if it is the active and visible screen within tmux.
It would be useful for a program running in one of these tabs to determine whether or not its screen is currently the "active" (visible) one, and to use this to moderate its output. In this way, it can be more verbose when visible, and only output more important stuff when it is not, so as to avoid needless switching between tabs due to the activity highlighting showing up on the tab. The active screen, obviously, could be switched by the user at any time.
How can an application running in a tmux screen determine when it becomes visible and not visible?