From: wijkstra@fwi.uva.nl (Marcel Wijkstra (AIO))
Subject: X client that sits & waits SUMMARY (switching mwm<->twm)
Organization: FWI, University of Amsterdam
Date: Mon, 14 Dec 1992 13:16:54 GMT


SUMMARY
-------
Recently, I posted a request for an X client that just sits and waits
(until it gets killed). The reason for this, was that I wanted this client
to be the last one executed in my .xinitrc, so that I would be able to
kill my window manager (which normally is the last line in .xinitrc) without
terminating X.  This would allow me to start a different window manager.
Finally, killing the small X client would end my X session.

I got several replies, most of them pointing to small programs that pop up
a little one-button menu, which exits when it is pushed. Those are:

NAME	FTP-SITE		DIR		POINTED BY
killme	larry.mcrcim.mcgill.edu	/X/		mouse@larry.mcrcim.mcgill.edu
xlogo						janzen@mprgate.mpr.ca
xlogout						shmdgljd@rchland.vnet.ibm.com
source for some unnamed program			cwikla@wri.com
xquitbutton ftp.dcs.ed.ac.uk	/pub/X11R5/local/xquitbutton/ gdmr@dcs.ed.ac.uk
xsleep						aad@lovecraft.siemens.com

klai@jman.dco.dec.com Suggested I'd use xtools as the 1st X client, and put
all subsequent client in .xtoolsrc

eillihca@drizzle.StanFord.EDU Handed me a small shell script - JAXU - to
replace the 'mwm' (or 'twm' or whatever) call in .xinitrc. This JAXU
would exit the window manager exits normally, but would start a different
window manager if it receives a signal USR1.

casper@fwi.uva.nl Provided a solution that uses the 'f.function' property
from twm and therefore is not generally applicable.

tl@etek.chalmers.se Uses xconsole. 


COMMENTS
--------
I don't want a seperate "Exit-X-button" on my desktop, I just want to exit
X  as I uses to (i.e. from within the twm root menu); therefore, these
little pop-up X clients are no good for me (perhaps YOU like them).
The JAXU script seems to me as the best solution, but unfortunately I know
to little about handling signals in sh to use it while knowing what I'm doing.
Furthermore, I want to go through as little trouble as possible, i.e. I don't
want ANYTHING to change in the looks and feels of my X session (except for
the additional possibility of switching between two window managers).
Therefore, I created


MY OWN SOLUTION
---------------
With the help of all above mentioned advises, and vdlinden@fwi.uva.nl, I
created some solution myself:

I use a script 'wm' to start and switch the different window managers:

	#!/bin/csh -f
	
	 switch ($1)
	  case mwm:
		goto MWM
	  case twm:
		goto TWM
	  default:
		exit
	 endsw
	
	MWM:
	 mwm
	TWM:
	 twm
	 goto MWM

This script simple calls it first argument - which must be the desired
initial window manager (e.g. 'wm mwm'). When this one exits, the next
one is started.
If you need more than 2 window managers, you may need some other mechanism
than this simple loop. But I'm sure you Xperts can write a fancy menu-driven
window manager selector if you need one...


The discussed 'X client that just sits and waits' is called 'xwait':

	#include <stdio.h>
	#include <X11/Xlib.h>
	
	void main(argc,argv)
	  int  argc;
	  char *argv[];
	{
	  char    *displayname;
	  int     arg,pid;
	  Display *dpy;
	  XEvent  *evt;
						/* GET DISPLAYNAME */
	  displayname=NULL;
	  for (arg=1;arg<argc;arg++)
	    if (! strncmp(argv[arg],"-display",strlen(argv[arg])))
	      displayname=argv[++arg];
						/* OPEN DISPLAY */
	  dpy = XOpenDisplay(displayname);
	  if (!dpy) {
	    fprintf(stderr, "%s: can't open display %s\n", *argv,displayname);
	    return; }
						/* PASS PID TO STDOUT */
	  pid=getpid();
	  fprintf(stdout,"%d\n",pid);
						/* FLUSH */
	  fclose(stdout);
	
						/* WAIT FOREVER */
	  XNextEvent(dpy,evt);
	}

xwait Can be called without any arguments, or with '-display <displayname>'.
It simply connects to the X server, and waits for any events. I know little
about X, but it works, i.e. xwait does not receive any events and thus
waits "forever".


In my .mwmrc, the following lines occur:

Menu RootMenu
{
...
	"TWM"		f.quit_mwm
	"Exit X"	!"kill `cat ${XWAIT}`"
}

In my .twmrc, similar lines occur.
As soon as 'f.quit_mwm' is executed, control is gained by 'wm' (see above)
again, which then calls 'twm'.
${XWAIT} Is the filename of a temporary file that holds the PID of 'xwait'.
I know of no other simple way of passing the PID of a program that runs
on the foreground to its parent. The danger of this mechanism - apart from
its uglyness - is that this file may be deleted, in which case you don't
know xwait's PID... A different solution would be to use 'ps' (e.g.
ps eauxww | grep xwait | grep $DISPLAY | grep -v grep) to query the
PID of xwait, but I don't like that approach either.


Finally, in .xinitrc, I do:

	XWAIT=/tmp/xwait.${DISPLAY}
	wm mwm &
	xwait > ${XWAIT}
	rm -f ${XWAIT}

${XWAIT} Is a unique filename, so no other window manager started in a similar
way should be able to access it (unless it *wants* to do evil).
'xwait' Writes to ${XWAIT} and closes this file before it terminates (see
source of 'xwait').


DISCLAIMER
----------
If you have any comments, or know a better way for some things I suggested
(e.g. how to pass the PIX of xwait to mwm), please let me know.
Also, if you think what I do is absolutely wrong, drop me a note too.

Remember however, that the above contruction takes into account, that it has
to run on a Sun4 (on which an X session is usually terminated with some
root menu option (like 'Exit TWM')) as well as on an RS/6000 (which uses
the Ctrl-Alt-BackSpace key-combination to exit the X session). Especially
the latter is a bit hard to handle.

Marcel.
-- 
 X	   Marcel Wijkstra   AIO   (wijkstra@fwi.uva.nl)
|X|	     Faculty of Mathematics and Computer Science	
 X	       University of Amsterdam   The Netherlands
======Life stinks. Fortunately, I've got a cold.========