Michael C. Toren on Tue, 9 Sep 2003 05:39:36 -0400

[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] Propagating DISPLAY into screen sessions

> A typical session looks something like this:
>   % ssh -X example.com
>   % screen -x
>   % emacs foo.pl
>   [nothing happens, much swearing]
>   C-c
>   % echo $DISPLAY
>   localhost:11.0
>   % C-a d
>   % echo $DISPLAY
>   localhost:13.0
>   % screen -x
>   % DISPLAY=localhost:13.0
>   % emacs foo.pl

To automate this process, I use the following in my ~/.profile:

	if test -n "$WINDOW"
	then test -r ~/.display && source ~/.display
	else export | egrep -- '-x (DISPLAY|XAUTHORITY)=' > ~/.display

Testing for $WINDOW gives an indication of weather or not I'm running
inside of a screen session -- if I'm not, the current values of $DISPLAY
and $XAUTHORITY are dumped to a file ~/.display, and if I am, the
~/.display file is sourced.  I also have the following in my ~/.screenrc,
which causes screen to execute bash as a login shell for new windows,
which in turn causes bash to read my ~/.profile on startup:

	shell -$SHELL

This way, even if the master screen process has an outdated copy of the
$DISPLAY and $XAUTHORITY variables, it doesn't matter, as they'll be reset
to the correct values when a new window is created.  If I detach and later
reattach from a different terminal, and discover that for an existing
window $DISPLAY is outdated, to fix it all I need to do is type:

	. ~/.profile

It's not a perfect system, but it's a huge improvement over having to
reset your $DISPLAY by hand.

> While this (eventually) works, I'm getting annoyed that I have to
> manually reset DISPLAY all the time.  Is there any easy way to
> reattach to a screen session and get it to automatically propagate
> DISPLAY to all the sessions?

That's difficult to do for the same reason it's difficult for a child
process to update it's parent's current working directory.  In order for
information to propagate to already running shells, some method of IPC is
required.  One such method may be to place the lines I mentioned earlier
in your ~/.profile, along with:

	PROMPT_COMMAND="test -r ~/.display && source ~/.display"

bash executes the contents of $PROMPT_COMMAND before displaying the
prompt, and in this case it will constantly update it's $DISPLAY variable.
This still isn't perfect -- if you leave bash waiting at a prompt, detach,
and later reattach from somewhere new and type "emacs", $PROMPT_COMMAND
hasn't yet been run since you attached from the new location, and $DISPLAY
will be out of sync.  However, if the command fails, $PROMPT_COMMAND will
be run again and a new prompt displayed, setting things up properly for a
second try.  All in all, I think I'd rather re-source my ~/.profile by
hand when I need to.

Philadelphia Linux Users Group        --       http://www.phillylinux.org
Announcements - http://lists.netisland.net/mailman/listinfo/plug-announce
General Discussion  --   http://lists.netisland.net/mailman/listinfo/plug