ftp://ftp.smr.ru/pub/0/FreeBSD/releases/distfiles/xscreensaver-3.16.tar.gz
[xscreensaver] / driver / splash.c
1 /* xscreensaver, Copyright (c) 1991-1998 Jamie Zawinski <jwz@netscape.com>
2  *
3  * Permission to use, copy, modify, distribute, and sell this software and its
4  * documentation for any purpose is hereby granted without fee, provided that
5  * the above copyright notice appear in all copies and that both that
6  * copyright notice and this permission notice appear in supporting
7  * documentation.  No representations are made about the suitability of this
8  * software for any purpose.  It is provided "as is" without express or 
9  * implied warranty.
10  */
11
12 #ifdef HAVE_CONFIG_H
13 # include "config.h"
14 #endif
15
16 #include <X11/Intrinsic.h>
17
18 #include "xscreensaver.h"
19 #include "resources.h"
20
21 #undef MAX
22 #define MAX(a,b) ((a)>(b)?(a):(b))
23
24 void
25 draw_shaded_rectangle (Display *dpy, Window window,
26                        int x, int y,
27                        int width, int height,
28                        int thickness,
29                        unsigned long top_color,
30                        unsigned long bottom_color)
31 {
32   XPoint points[4];
33   XGCValues gcv;
34   GC gc1, gc2;
35   if (thickness == 0) return;
36
37   gcv.foreground = top_color;
38   gc1 = XCreateGC (dpy, window, GCForeground, &gcv);
39   gcv.foreground = bottom_color;
40   gc2 = XCreateGC (dpy, window, GCForeground, &gcv);
41
42   points [0].x = x;
43   points [0].y = y;
44   points [1].x = x + width;
45   points [1].y = y;
46   points [2].x = x + width - thickness;
47   points [2].y = y + thickness;
48   points [3].x = x;
49   points [3].y = y + thickness;
50   XFillPolygon (dpy, window, gc1, points, 4, Convex, CoordModeOrigin);
51
52   points [0].x = x;
53   points [0].y = y + thickness;
54   points [1].x = x;
55   points [1].y = y + height;
56   points [2].x = x + thickness;
57   points [2].y = y + height - thickness;
58   points [3].x = x + thickness;
59   points [3].y = y + thickness;
60   XFillPolygon (dpy, window, gc1, points, 4, Convex, CoordModeOrigin);
61
62   points [0].x = x + width;
63   points [0].y = y;
64   points [1].x = x + width - thickness;
65   points [1].y = y + thickness;
66   points [2].x = x + width - thickness;
67   points [2].y = y + height - thickness;
68   points [3].x = x + width;
69   points [3].y = y + height - thickness;
70   XFillPolygon (dpy, window, gc2, points, 4, Convex, CoordModeOrigin);
71
72   points [0].x = x;
73   points [0].y = y + height;
74   points [1].x = x + width;
75   points [1].y = y + height;
76   points [2].x = x + width;
77   points [2].y = y + height - thickness;
78   points [3].x = x + thickness;
79   points [3].y = y + height - thickness;
80   XFillPolygon (dpy, window, gc2, points, 4, Convex, CoordModeOrigin);
81
82   XFreeGC (dpy, gc1);
83   XFreeGC (dpy, gc2);
84 }
85
86
87 int
88 string_width (XFontStruct *font, char *s)
89 {
90   int direction, ascent, descent;
91   XCharStruct overall;
92   XTextExtents (font, s, strlen(s), &direction, &ascent, &descent, &overall);
93   return overall.width;
94 }
95
96
97 static void update_splash_window (saver_info *si);
98 static void draw_splash_window (saver_info *si);
99 static void destroy_splash_window (saver_info *si);
100 static void unsplash_timer (XtPointer closure, XtIntervalId *id);
101
102 static void do_demo (saver_info *si);
103 static void do_prefs (saver_info *si);
104 static void do_help (saver_info *si);
105
106
107 struct splash_dialog_data {
108   XtIntervalId timer;
109
110   Dimension width;
111   Dimension height;
112
113   char *heading_label;
114   char *body_label;
115   char *body2_label;
116   char *demo_label;
117   char *prefs_label;
118   char *help_label;
119
120   XFontStruct *heading_font;
121   XFontStruct *body_font;
122   XFontStruct *button_font;
123
124   Pixel foreground;
125   Pixel background;
126   Pixel button_foreground;
127   Pixel button_background;
128   Pixel logo_foreground;
129   Pixel logo_background;
130   Pixel shadow_top;
131   Pixel shadow_bottom;
132
133   Dimension logo_width;
134   Dimension logo_height;
135   Dimension internal_border;
136   Dimension shadow_width;
137
138   Dimension button_width, button_height;
139   Dimension demo_button_x, demo_button_y;
140   Dimension prefs_button_x, prefs_button_y;
141   Dimension help_button_x, help_button_y;
142
143   int pressed;
144 };
145
146
147 void
148 make_splash_dialog (saver_info *si)
149 {
150   int x, y, bw;
151   XSetWindowAttributes attrs;
152   unsigned long attrmask = 0;
153   splash_dialog_data *sp;
154   Screen *screen = si->default_screen->screen;
155   Colormap cmap = DefaultColormapOfScreen (screen);
156   char *f;
157
158   if (si->sp_data)
159     return;
160   if (si->prefs.splash_duration <= 0)
161     return;
162
163   sp = (splash_dialog_data *) calloc (1, sizeof(*sp));
164
165   sp->heading_label = get_string_resource ("splash.heading.label",
166                                            "Dialog.Label.Label");
167   sp->body_label = get_string_resource ("splash.body.label",
168                                         "Dialog.Label.Label");
169   sp->body2_label = get_string_resource ("splash.body2.label",
170                                          "Dialog.Label.Label");
171   sp->demo_label = get_string_resource ("splash.demo.label",
172                                         "Dialog.Button.Label");
173   sp->prefs_label = get_string_resource ("splash.prefs.label",
174                                         "Dialog.Button.Label");
175   sp->help_label = get_string_resource ("splash.help.label",
176                                         "Dialog.Button.Label");
177
178   if (!sp->heading_label)
179     sp->heading_label = strdup("ERROR: REESOURCES NOT INSTALLED CORRECTLY");
180   if (!sp->body_label)
181     sp->body_label = strdup("ERROR: REESOURCES NOT INSTALLED CORRECTLY");
182   if (!sp->body2_label)
183     sp->body2_label = strdup("ERROR: REESOURCES NOT INSTALLED CORRECTLY");
184   if (!sp->demo_label) sp->demo_label = strdup("ERROR");
185   if (!sp->prefs_label) sp->prefs_label = strdup("ERROR");
186   if (!sp->help_label) sp->help_label = strdup("ERROR");
187
188   /* Put the version number in the label. */
189   {
190     char *s = (char *) malloc (strlen(sp->heading_label) + 20);
191     sprintf(s, sp->heading_label, si->version);
192     free (sp->heading_label);
193     sp->heading_label = s;
194   }
195
196   f = get_string_resource ("splash.headingFont", "Dialog.Font");
197   sp->heading_font = XLoadQueryFont (si->dpy, (f ? f : "fixed"));
198   if (!sp->heading_font) sp->heading_font = XLoadQueryFont (si->dpy, "fixed");
199   if (f) free (f);
200
201   f = get_string_resource("splash.bodyFont", "Dialog.Font");
202   sp->body_font = XLoadQueryFont (si->dpy, (f ? f : "fixed"));
203   if (!sp->body_font) sp->body_font = XLoadQueryFont (si->dpy, "fixed");
204   if (f) free (f);
205
206   f = get_string_resource("splash.buttonFont", "Dialog.Font");
207   sp->button_font = XLoadQueryFont (si->dpy, (f ? f : "fixed"));
208   if (!sp->button_font) sp->button_font = XLoadQueryFont (si->dpy, "fixed");
209   if (f) free (f);
210
211   sp->foreground = get_pixel_resource ("splash.foreground",
212                                        "Dialog.Foreground",
213                                        si->dpy, cmap);
214   sp->background = get_pixel_resource ("splash.background",
215                                        "Dialog.Background",
216                                        si->dpy, cmap);
217
218   if (sp->foreground == sp->background)
219     {
220       /* Make sure the error messages show up. */
221       sp->foreground = BlackPixelOfScreen (screen);
222       sp->background = WhitePixelOfScreen (screen);
223     }
224
225   sp->button_foreground = get_pixel_resource ("splash.Button.foreground",
226                                               "Dialog.Button.Foreground",
227                                               si->dpy, cmap);
228   sp->button_background = get_pixel_resource ("splash.Button.background",
229                                               "Dialog.Button.Background",
230                                               si->dpy, cmap);
231   sp->logo_foreground = get_pixel_resource ("splash.logo.foreground",
232                                             "Dialog.Logo.Foreground",
233                                             si->dpy, cmap);
234   sp->logo_background = get_pixel_resource ("splash.logo.background",
235                                             "Dialog.Logo.Background",
236                                             si->dpy, cmap);
237   sp->shadow_top = get_pixel_resource ("splash.topShadowColor",
238                                        "Dialog.Foreground",
239                                        si->dpy, cmap);
240   sp->shadow_bottom = get_pixel_resource ("splash.bottomShadowColor",
241                                           "Dialog.Background",
242                                           si->dpy, cmap);
243
244   sp->logo_width = get_integer_resource ("splash.logo.width",
245                                          "Dialog.Logo.Width");
246   sp->logo_height = get_integer_resource ("splash.logo.height",
247                                           "Dialog.Logo.Height");
248   sp->internal_border = get_integer_resource ("splash.internalBorderWidth",
249                                               "Dialog.InternalBorderWidth");
250   sp->shadow_width = get_integer_resource ("splash.shadowThickness",
251                                            "Dialog.ShadowThickness");
252
253   if (sp->logo_width == 0)  sp->logo_width = 150;
254   if (sp->logo_height == 0) sp->logo_height = 150;
255   if (sp->internal_border == 0) sp->internal_border = 15;
256   if (sp->shadow_width == 0) sp->shadow_width = 4;
257
258   {
259     int direction, ascent, descent;
260     XCharStruct overall;
261
262     sp->width = 0;
263     sp->height = 0;
264
265     /* Measure the heading_label. */
266     XTextExtents (sp->heading_font,
267                   sp->heading_label, strlen(sp->heading_label),
268                   &direction, &ascent, &descent, &overall);
269     if (overall.width > sp->width) sp->width = overall.width;
270     sp->height += ascent + descent;
271
272     /* Measure the body_label. */
273     XTextExtents (sp->body_font,
274                   sp->body_label, strlen(sp->body_label),
275                   &direction, &ascent, &descent, &overall);
276     if (overall.width > sp->width) sp->width = overall.width;
277     sp->height += ascent + descent;
278
279     /* Measure the body2_label. */
280     XTextExtents (sp->body_font,
281                   sp->body2_label, strlen(sp->body2_label),
282                   &direction, &ascent, &descent, &overall);
283     if (overall.width > sp->width) sp->width = overall.width;
284     sp->height += ascent + descent;
285
286     {
287       Dimension w2 = 0, w3 = 0, w4 = 0;
288       Dimension h2 = 0, h3 = 0, h4 = 0;
289
290       /* Measure the Demo button. */
291       XTextExtents (sp->button_font,
292                     sp->demo_label, strlen(sp->demo_label),
293                     &direction, &ascent, &descent, &overall);
294       w2 = overall.width;
295       h2 = ascent + descent;
296
297       /* Measure the Prefs button. */
298       XTextExtents (sp->button_font,
299                     sp->prefs_label, strlen(sp->prefs_label),
300                     &direction, &ascent, &descent, &overall);
301       w3 = overall.width;
302       h3 = ascent + descent;
303
304       /* Measure the Help button. */
305       XTextExtents (sp->button_font,
306                     sp->help_label, strlen(sp->help_label),
307                     &direction, &ascent, &descent, &overall);
308       w4 = overall.width;
309       h4 = ascent + descent;
310
311       w2 = MAX(w2, w3); w2 = MAX(w2, w4);
312       h2 = MAX(h2, h3); h2 = MAX(h2, h4);
313
314       w2 += ((ascent + descent) / 2) + (sp->shadow_width * 2);
315       h2 += ((ascent + descent) / 2) + (sp->shadow_width * 2);
316
317       sp->button_width = w2;
318       sp->button_height = h2;
319
320       w2 *= 3;
321
322       w2 += ((ascent + descent) * 2);  /* for space between buttons */
323
324       if (w2 > sp->width) sp->width = w2;
325       sp->height += h2;
326     }
327
328     sp->width  += (sp->internal_border * 2);
329     sp->height += (sp->internal_border * 3);
330
331     if (sp->logo_height > sp->height)
332       sp->height = sp->logo_height;
333     else if (sp->height > sp->logo_height)
334       sp->logo_height = sp->height;
335
336     sp->logo_width = sp->logo_height;
337
338     sp->width += sp->logo_width;
339   }
340
341   attrmask |= CWOverrideRedirect; attrs.override_redirect = True;
342   attrmask |= CWEventMask;
343   attrs.event_mask = (ExposureMask | ButtonPressMask | ButtonReleaseMask);
344
345   {
346     int sx, sy, w, h;
347     get_screen_viewport (si->default_screen, &sx, &sy, &w, &h, False);
348     if (si->prefs.debug_p) w /= 2;
349     x = sx + (((w + sp->width)  / 2) - sp->width);
350     y = sy + (((h + sp->height) / 2) - sp->height);
351     if (x < sx) x = sx;
352     if (y < sy) y = sy;
353   }
354
355   bw = get_integer_resource ("splash.borderWidth", "Dialog.BorderWidth");
356
357   si->splash_dialog =
358     XCreateWindow (si->dpy,
359                    RootWindowOfScreen(screen),
360                    x, y, sp->width, sp->height, bw,
361                    DefaultDepthOfScreen (screen), InputOutput,
362                    DefaultVisualOfScreen(screen),
363                    attrmask, &attrs);
364   XSetWindowBackground (si->dpy, si->splash_dialog, sp->background);
365
366   XMapRaised (si->dpy, si->splash_dialog);
367   XSync (si->dpy, False);
368
369   si->sp_data = sp;
370
371   sp->timer = XtAppAddTimeOut (si->app, si->prefs.splash_duration,
372                                unsplash_timer, (XtPointer) si);
373
374   draw_splash_window (si);
375   XSync (si->dpy, False);
376 }
377
378 static void
379 draw_splash_window (saver_info *si)
380 {
381   splash_dialog_data *sp = si->sp_data;
382   XGCValues gcv;
383   GC gc1, gc2;
384   int hspacing, vspacing, height;
385   int x1, x2, x3, y1, y2;
386   int sw;
387
388   height = (sp->heading_font->ascent + sp->heading_font->descent +
389             sp->body_font->ascent + sp->body_font->descent +
390             sp->body_font->ascent + sp->body_font->descent +
391             sp->button_font->ascent + sp->button_font->descent);
392   vspacing = ((sp->height
393                - (4 * sp->shadow_width)
394                - (2 * sp->internal_border)
395                - height) / 5);
396   if (vspacing < 0) vspacing = 0;
397   if (vspacing > (sp->heading_font->ascent * 2))
398     vspacing = (sp->heading_font->ascent * 2);
399             
400   gcv.foreground = sp->foreground;
401   gc1 = XCreateGC (si->dpy, si->splash_dialog, GCForeground, &gcv);
402   gc2 = XCreateGC (si->dpy, si->splash_dialog, GCForeground, &gcv);
403   x1 = sp->logo_width;
404   x3 = sp->width - (sp->shadow_width * 2);
405   y1 = sp->internal_border;
406
407   /* top heading
408    */
409   XSetFont (si->dpy, gc1, sp->heading_font->fid);
410   sw = string_width (sp->heading_font, sp->heading_label);
411   x2 = (x1 + ((x3 - x1 - sw) / 2));
412   y1 += sp->heading_font->ascent;
413   XDrawString (si->dpy, si->splash_dialog, gc1, x2, y1,
414                sp->heading_label, strlen(sp->heading_label));
415   y1 += sp->heading_font->descent;
416
417   /* text below top heading
418    */
419   XSetFont (si->dpy, gc1, sp->body_font->fid);
420   y1 += vspacing + sp->body_font->ascent;
421   sw = string_width (sp->body_font, sp->body_label);
422   x2 = (x1 + ((x3 - x1 - sw) / 2));
423   XDrawString (si->dpy, si->splash_dialog, gc1, x2, y1,
424                sp->body_label, strlen(sp->body_label));
425   y1 += sp->body_font->descent;
426
427   y1 += sp->body_font->ascent;
428   sw = string_width (sp->body_font, sp->body2_label);
429   x2 = (x1 + ((x3 - x1 - sw) / 2));
430   XDrawString (si->dpy, si->splash_dialog, gc1, x2, y1,
431                sp->body2_label, strlen(sp->body2_label));
432   y1 += sp->body_font->descent;
433
434   /* The buttons
435    */
436   XSetForeground (si->dpy, gc1, sp->button_foreground);
437   XSetForeground (si->dpy, gc2, sp->button_background);
438
439 /*  y1 += (vspacing * 2);*/
440   y1 = sp->height - sp->internal_border - sp->button_height;
441
442   x1 += sp->internal_border;
443   y2 = (y1 + ((sp->button_height -
444                (sp->button_font->ascent + sp->button_font->descent))
445               / 2)
446         + sp->button_font->ascent);
447   hspacing = ((sp->width - x1 - (sp->shadow_width * 2) -
448                sp->internal_border - (sp->button_width * 3))
449               / 2);
450
451   x2 = x1 + ((sp->button_width - string_width(sp->button_font, sp->demo_label))
452              / 2);
453   XFillRectangle (si->dpy, si->splash_dialog, gc2, x1, y1,
454                   sp->button_width, sp->button_height);
455   XDrawString (si->dpy, si->splash_dialog, gc1, x2, y2,
456                sp->demo_label, strlen(sp->demo_label));
457   sp->demo_button_x = x1;
458   sp->demo_button_y = y1;
459   
460   x1 += hspacing + sp->button_width;
461   x2 = x1 + ((sp->button_width - string_width(sp->button_font,sp->prefs_label))
462              / 2);
463   XFillRectangle (si->dpy, si->splash_dialog, gc2, x1, y1,
464                   sp->button_width, sp->button_height);
465   XDrawString (si->dpy, si->splash_dialog, gc1, x2, y2,
466                sp->prefs_label, strlen(sp->prefs_label));
467   sp->prefs_button_x = x1;
468   sp->prefs_button_y = y1;
469
470   x1 += hspacing + sp->button_width;
471   x2 = x1 + ((sp->button_width - string_width(sp->button_font,sp->help_label))
472              / 2);
473   XFillRectangle (si->dpy, si->splash_dialog, gc2, x1, y1,
474                   sp->button_width, sp->button_height);
475   XDrawString (si->dpy, si->splash_dialog, gc1, x2, y2,
476                sp->help_label, strlen(sp->help_label));
477   sp->help_button_x = x1;
478   sp->help_button_y = y1;
479
480
481   /* the logo
482    */
483   XSetForeground (si->dpy, gc1, sp->logo_foreground);
484   XSetForeground (si->dpy, gc2, sp->logo_background);
485
486   x1 = sp->shadow_width * 3;
487   y1 = sp->shadow_width * 3;
488   x2 = sp->logo_width - (sp->shadow_width * 6);
489   y2 = sp->logo_height - (sp->shadow_width * 6);
490
491   XFillRectangle (si->dpy, si->splash_dialog, gc2, x1, y1, x2, y2);
492   skull (si->dpy, si->splash_dialog, gc1, gc2,
493          x1 + sp->shadow_width, y1 + sp->shadow_width,
494          x2 - (sp->shadow_width * 2), y2 - (sp->shadow_width * 2));
495
496
497   /* The shadow around the logo
498    */
499   draw_shaded_rectangle (si->dpy, si->splash_dialog,
500                          sp->shadow_width * 2,
501                          sp->shadow_width * 2,
502                          sp->logo_width - (sp->shadow_width * 4),
503                          sp->logo_height - (sp->shadow_width * 4),
504                          sp->shadow_width,
505                          sp->shadow_bottom, sp->shadow_top);
506
507   /* The shadow around the whole window
508    */
509   draw_shaded_rectangle (si->dpy, si->splash_dialog,
510                          0, 0, sp->width, sp->height, sp->shadow_width,
511                          sp->shadow_top, sp->shadow_bottom);
512
513   XFreeGC (si->dpy, gc1);
514   XFreeGC (si->dpy, gc2);
515
516   update_splash_window (si);
517 }
518
519
520 static void
521 update_splash_window (saver_info *si)
522 {
523   splash_dialog_data *sp = si->sp_data;
524   int pressed;
525   if (!sp) return;
526   pressed = sp->pressed;
527
528   /* The shadows around the buttons
529    */
530   draw_shaded_rectangle (si->dpy, si->splash_dialog,
531                          sp->demo_button_x, sp->demo_button_y,
532                          sp->button_width, sp->button_height, sp->shadow_width,
533                          (pressed == 1 ? sp->shadow_bottom : sp->shadow_top),
534                          (pressed == 1 ? sp->shadow_top : sp->shadow_bottom));
535   draw_shaded_rectangle (si->dpy, si->splash_dialog,
536                          sp->prefs_button_x, sp->prefs_button_y,
537                          sp->button_width, sp->button_height, sp->shadow_width,
538                          (pressed == 2 ? sp->shadow_bottom : sp->shadow_top),
539                          (pressed == 2 ? sp->shadow_top : sp->shadow_bottom));
540   draw_shaded_rectangle (si->dpy, si->splash_dialog,
541                          sp->help_button_x, sp->help_button_y,
542                          sp->button_width, sp->button_height, sp->shadow_width,
543                          (pressed == 3 ? sp->shadow_bottom : sp->shadow_top),
544                          (pressed == 3 ? sp->shadow_top : sp->shadow_bottom));
545 }
546
547 static void
548 destroy_splash_window (saver_info *si)
549 {
550   splash_dialog_data *sp = si->sp_data;
551   Screen *screen = si->default_screen->screen;
552   Colormap cmap = DefaultColormapOfScreen (screen);
553   Pixel black = BlackPixelOfScreen (screen);
554   Pixel white = WhitePixelOfScreen (screen);
555
556   if (sp->timer)
557     XtRemoveTimeOut (sp->timer);
558
559   if (si->splash_dialog)
560     {
561       XDestroyWindow (si->dpy, si->splash_dialog);
562       si->splash_dialog = 0;
563     }
564   
565   if (sp->heading_label) free (sp->heading_label);
566   if (sp->body_label)    free (sp->body_label);
567   if (sp->demo_label)    free (sp->demo_label);
568   if (sp->prefs_label)   free (sp->prefs_label);
569   if (sp->help_label)    free (sp->help_label);
570
571   if (sp->heading_font) XFreeFont (si->dpy, sp->heading_font);
572   if (sp->body_font)    XFreeFont (si->dpy, sp->body_font);
573   if (sp->button_font)  XFreeFont (si->dpy, sp->button_font);
574
575   if (sp->foreground != black && sp->foreground != white)
576     XFreeColors (si->dpy, cmap, &sp->foreground, 1, 0L);
577   if (sp->background != black && sp->background != white)
578     XFreeColors (si->dpy, cmap, &sp->background, 1, 0L);
579   if (sp->button_foreground != black && sp->button_foreground != white)
580     XFreeColors (si->dpy, cmap, &sp->button_foreground, 1, 0L);
581   if (sp->button_background != black && sp->button_background != white)
582     XFreeColors (si->dpy, cmap, &sp->button_background, 1, 0L);
583   if (sp->logo_foreground != black && sp->logo_foreground != white)
584     XFreeColors (si->dpy, cmap, &sp->logo_foreground, 1, 0L);
585   if (sp->logo_background != black && sp->logo_background != white)
586     XFreeColors (si->dpy, cmap, &sp->logo_background, 1, 0L);
587   if (sp->shadow_top != black && sp->shadow_top != white)
588     XFreeColors (si->dpy, cmap, &sp->shadow_top, 1, 0L);
589   if (sp->shadow_bottom != black && sp->shadow_bottom != white)
590     XFreeColors (si->dpy, cmap, &sp->shadow_bottom, 1, 0L);
591
592   memset (sp, 0, sizeof(*sp));
593   free (sp);
594
595   si->sp_data = 0;
596 }
597
598 void
599 handle_splash_event (saver_info *si, XEvent *event)
600 {
601   splash_dialog_data *sp = si->sp_data;
602   int which = 0;
603   if (!sp) return;
604
605   switch (event->xany.type)
606     {
607     case Expose:
608       draw_splash_window (si);
609       break;
610
611     case ButtonPress: case ButtonRelease:
612
613       if (event->xbutton.x >= sp->demo_button_x &&
614           event->xbutton.x < sp->demo_button_x + sp->button_width &&
615           event->xbutton.y >= sp->demo_button_y &&
616           event->xbutton.y < sp->demo_button_y + sp->button_height)
617         which = 1;
618
619       else if (event->xbutton.x >= sp->prefs_button_x &&
620                event->xbutton.x < sp->prefs_button_x + sp->button_width &&
621                event->xbutton.y >= sp->prefs_button_y &&
622                event->xbutton.y < sp->prefs_button_y + sp->button_height)
623         which = 2;
624
625       else if (event->xbutton.x >= sp->help_button_x &&
626                event->xbutton.x < sp->help_button_x + sp->button_width &&
627                event->xbutton.y >= sp->help_button_y &&
628                event->xbutton.y < sp->help_button_y + sp->button_height)
629         which = 3;
630
631       if (event->xany.type == ButtonPress)
632         {
633           sp->pressed = which;
634           update_splash_window (si);
635           if (which == 0)
636             XBell (si->dpy, False);
637         }
638       else if (event->xany.type == ButtonRelease)
639         {
640           if (which && sp->pressed == which)
641             {
642               destroy_splash_window (si);
643               switch (which)
644                 {
645                 case 1: do_demo (si); break;
646                 case 2: do_prefs (si); break;
647                 case 3: do_help (si); break;
648                 default: abort();
649                 }
650             }
651           sp->pressed = 0;
652           update_splash_window (si);
653         }
654       break;
655
656     default:
657       break;
658     }
659 }
660
661 static void
662 unsplash_timer (XtPointer closure, XtIntervalId *id)
663 {
664   saver_info *si = (saver_info *) closure;
665   if (si && si->sp_data)
666     destroy_splash_window (si);
667 }
668
669 \f
670 /* Button callbacks */
671
672 #ifdef VMS
673 # define pid_t int
674 # define fork  vfork
675 #endif /* VMS */
676
677 static void
678 fork_and_exec (saver_info *si, const char *command, const char *desc)
679 {
680   saver_preferences *p = &si->prefs;
681   pid_t forked;
682   char buf [512];
683   char *av[5];
684   int ac;
685
686   if (!command || !*command)
687     {
688       fprintf (stderr, "%s: no %s command has been specified.\n",
689                blurb(), desc);
690       return;
691     }
692
693   switch ((int) (forked = fork ()))
694     {
695     case -1:
696       sprintf (buf, "%s: couldn't fork", blurb());
697       perror (buf);
698       break;
699
700     case 0:
701       close (ConnectionNumber (si->dpy));               /* close display fd */
702       hack_subproc_environment (si->default_screen);    /* set $DISPLAY */
703       ac = 0;
704       av [ac++] = (char *) p->shell;
705       av [ac++] = (char *) "-c";
706       av [ac++] = (char *) command;
707       av [ac]   = 0;
708       execvp (av[0], av);                               /* shouldn't return. */
709
710       sprintf (buf, "%s: execvp(\"%s\", \"%s\", \"%s\") failed",
711                blurb(), av[0], av[1], av[2]);
712       perror (buf);
713       fflush (stderr);
714       fflush (stdout);
715       exit (1);                  /* Note that this only exits a child fork.  */
716       break;
717
718     default:
719       /* parent fork. */
720       break;
721     }
722 }
723
724
725 static void
726 do_demo (saver_info *si)
727 {
728   saver_preferences *p = &si->prefs;
729   fork_and_exec (si, p->demo_command, "demo-mode");
730 }
731
732 static void
733 do_prefs (saver_info *si)
734 {
735   saver_preferences *p = &si->prefs;
736   fork_and_exec (si, p->prefs_command, "preferences");
737 }
738
739 static void
740 do_help (saver_info *si)
741 {
742   saver_preferences *p = &si->prefs;
743   char *help_command;
744
745   if (!p->help_url || !*p->help_url)
746     {
747       fprintf (stderr, "%s: no Help URL has been specified.\n", blurb());
748       return;
749     }
750
751   help_command = (char *) malloc (strlen (p->load_url_command) +
752                                   (strlen (p->help_url) * 2) + 10);
753   sprintf (help_command, p->load_url_command, p->help_url, p->help_url);
754   fork_and_exec (si, help_command, "URL-loading");
755   free (help_command);
756 }