summaryrefslogtreecommitdiffstats
path: root/hacks/xscreensaver-sgigl.c
blob: 8d25ad14abf9b416522a5763ced6b33a9d41db9d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* xscreensaver, Copyright (c) 1997, 2005 Jamie Zawinski <jwz@jwz.org>
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or 
 * implied warranty.
 */

/* This is a kludge that lets xscreensaver work with SGI demos that expect
   to be run from `haven'.  It runs the program given on the command line,
   then waits for an X window to be created whose name is that of the 
   program.  Then, it resizes that window to fill the screen.  Run it
   like this:

       xscreensaver-sgigl /usr/demos/bin/ep -S
       xscreensaver-sgigl -n ant /usr/demos/General_Demos/ant/RUN
       xscreensaver-sgigl -n atlantis /usr/demos/General_Demos/atlantis/RUN
       xscreensaver-sgigl -n /usr/demos/General_Demos/powerflip/powerflip \
          /usr/demos/General_Demos/powerflip/RUN

   Except that doesn't really work.  You have to do this instead:

       xscreensaver-sgigl -n ant ant.sh

   where ant.sh contains

       #!/bin/sh
       cd /usr/demos/General_Demos/ant
       exec ./ant -S

   There's no way to make this work with powerflip at all, since it doesn't
   take a -S option to run in the foreground.
 */

/* #### Another way to do this would be:
   instead of exec'ing the hack, fork it; then wait for that fork to die.
   If it dies, but the window ID is still valid, then that means the 
   sub-process has forked itself (as those fuckwits at SGI are wont to do.)
   In that case, this process should go to sleep, and set up a signal handler
   that will destroy the X window when it is killed.  That way, the caller
   is given a foreground pid which, when killed, will cause the hack to die
   (by a roundabout mechanism.)

   This would all be so much simpler if those assholes would just:

   1: get out of the habit of writing programs that magically background
      themselves, and

   2: give the fucking programs arguments which control the window size
      instead of always making 100x100 windows!

   I won't even dream of having a "-root" option that understood virtual-roots;
   that would just be too outlandish to even dream about.
 */

static char *progname;

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xmu/Error.h>

#include "vroot.h"
#undef RootWindowOfScreen
#undef RootWindow
#undef DefaultRootWindow


static int
BadWindow_ehandler (Display *dpy, XErrorEvent *error)
{
  if (error->error_code == BadWindow ||
      error->error_code == BadMatch ||
      error->error_code == BadDrawable)
    return 0;
  else
    {
      fprintf (stderr, "\nX error in %s:\n", progname);
      if (XmuPrintDefaultErrorMessage (dpy, error, stderr))
	exit(1);
      else
	fprintf (stderr, " (nonfatal.)\n");
    }
  return 0;
}


void
main(int ac, char **av)
{
  char buf [512];
  pid_t parent, forked;
  Display *dpy;
  Screen *screen;
  char *s;
  char *n1 = 0;
  char *n2 = 0;
  Bool verbose = False;
  Window root, vroot;
  XSizeHints h;
  long ls;

  progname = av[0];

  s = strrchr(progname, '/');
  if (s) progname = s+1;

  if (ac < 1)
    {
      fprintf(stderr,
	      "usage: %s [ -v ] [ -n window-name ] program arguments...\n",
	      progname);
      exit(1);
    }

  if (ac > 2 && !strcmp(av[1], "-v"))
    {
      verbose = True;
      av++;
      ac--;
    }

  if (ac > 2 && !strcmp(av[1], "-n"))
    {
      n2 = av[2];
      av += 2;
      ac -= 2;
    }

  n1 = strrchr(av[1], '/');
  if (n1) n1++;
  else n1 = av[1];


  dpy = XOpenDisplay(0);
  if (!dpy)
    {
      fprintf(stderr, "%s: couldn't open display\n", progname);
      exit(1);
    }

  screen = DefaultScreenOfDisplay(dpy);
  root   = XRootWindowOfScreen (screen);
  vroot  = VirtualRootWindowOfScreen (screen);

  XSelectInput (dpy, root, SubstructureNotifyMask);
  if (root != vroot)
    XSelectInput (dpy, vroot, SubstructureNotifyMask);

  XSetErrorHandler (BadWindow_ehandler);

  if (verbose)
    fprintf(stderr, "%s: selected SubstructureNotifyMask on 0x%x / 0x%x\n",
	    progname, root, vroot);

  parent = getpid();

  if (verbose)
    fprintf(stderr, "%s: pid is %d\n", progname, parent);

  switch ((int) (forked = fork ()))
    {
    case -1:
      {
	sprintf (buf, "%s: couldn't fork", progname);
	perror (buf);
	exit (1);
	break;
      }
    case 0:	/* forked */
      {
	time_t start = time((time_t) 0);
	XEvent event;

	if (verbose)
	  fprintf(stderr, "%s: forked pid is %d\n", progname, getpid());

	while (1)
	  {
	    XNextEvent(dpy, &event);

	    if (event.xany.type == CreateNotify)
	      {
		char *name = 0;
		Window w = event.xcreatewindow.window;
		XSync(dpy, False);

		XFetchName(dpy, w, &name);
		if (!name)
		  {
		    /* Try again to see if the name has been set later... */
		    XSync(dpy, False);
		    sleep(1);
		    XFetchName(dpy, w, &name);
		  }

		if (name &&
		    ((n1 && !strcmp(name, n1)) ||
		     (n2 && !strcmp(name, n2))))
		  {
		    if (verbose)
		      fprintf(stderr, "%s: resizing 0x%x\n", progname, w);

                    /* Make sure the window allows itself to be resized. */
		    XGetWMNormalHints (dpy, w, &h, &ls);
		    h.flags |= PMaxSize;
		    h.max_width = WidthOfScreen(screen)+128;
		    h.max_height = HeightOfScreen(screen)+128;
		    XSetWMNormalHints (dpy, w, &h);

		    XMoveResizeWindow(dpy, w, 0, 0,
				      WidthOfScreen(screen),
				      HeightOfScreen(screen));

#if 0
		    if (vroot && vroot != root &&
			event.xcreatewindow.parent == root)
		      {
			if (verbose)
			  fprintf(stderr,
				  "%s: reparenting 0x%x from 0x%x to 0x%x\n",
				  progname, w, root, vroot);
			XReparentWindow(dpy, w, vroot, 0, 0);
		      }
#endif

		    XSync(dpy, False);
		    fflush(stdout);
		    fflush(stderr);
		    exit(0);	/* Note that this only exits a child fork.  */
		  }
	      }

	    if (start + 5 < time((time_t) 0))
	      {
		fprintf(stderr,
		    "%s: timed out: no window named \"%s\" has been created\n",
			progname, (n2 ? n2 : n1));
		fflush(stdout);
		fflush(stderr);
		kill(parent, SIGTERM);
		exit(1);
	      }
	  }
	break;
      }
    default:	/* foreground */
      {
	close (ConnectionNumber (dpy));		/* close display fd */
	execvp (av[1], av+1);			/* shouldn't return. */
	sprintf (buf, "%s: execvp(\"%s\") failed", progname, av[1]);
	perror (buf);
	fflush(stderr);
	fflush(stdout);
	exit (1);
	break;
      }
    }
}