http://packetstormsecurity.org/UNIX/admin/xscreensaver-3.28.tar.gz
[xscreensaver] / driver / splash.c
1 /* xscreensaver, Copyright (c) 1991-2001 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_p ||
161       si->prefs.splash_duration <= 0)
162     return;
163
164   sp = (splash_dialog_data *) calloc (1, sizeof(*sp));
165
166   sp->heading_label = get_string_resource ("splash.heading.label",
167                                            "Dialog.Label.Label");
168   sp->body_label = get_string_resource ("splash.body.label",
169                                         "Dialog.Label.Label");
170   sp->body2_label = get_string_resource ("splash.body2.label",
171                                          "Dialog.Label.Label");
172   sp->demo_label = get_string_resource ("splash.demo.label",
173                                         "Dialog.Button.Label");
174   sp->prefs_label = get_string_resource ("splash.prefs.label",
175                                         "Dialog.Button.Label");
176   sp->help_label = get_string_resource ("splash.help.label",
177                                         "Dialog.Button.Label");
178
179   if (!sp->heading_label)
180     sp->heading_label = strdup("ERROR: REESOURCES NOT INSTALLED CORRECTLY");
181   if (!sp->body_label)
182     sp->body_label = strdup("ERROR: REESOURCES NOT INSTALLED CORRECTLY");
183   if (!sp->body2_label)
184     sp->body2_label = strdup("ERROR: REESOURCES NOT INSTALLED CORRECTLY");
185   if (!sp->demo_label) sp->demo_label = strdup("ERROR");
186   if (!sp->prefs_label) sp->prefs_label = strdup("ERROR");
187   if (!sp->help_label) sp->help_label = strdup("ERROR");
188
189   /* Put the version number in the label. */
190   {
191     char *s = (char *) malloc (strlen(sp->heading_label) + 20);
192     sprintf(s, sp->heading_label, si->version);
193     free (sp->heading_label);
194     sp->heading_label = s;
195   }
196
197   f = get_string_resource ("splash.headingFont", "Dialog.Font");
198   sp->heading_font = XLoadQueryFont (si->dpy, (f ? f : "fixed"));
199   if (!sp->heading_font) sp->heading_font = XLoadQueryFont (si->dpy, "fixed");
200   if (f) free (f);
201
202   f = get_string_resource("splash.bodyFont", "Dialog.Font");
203   sp->body_font = XLoadQueryFont (si->dpy, (f ? f : "fixed"));
204   if (!sp->body_font) sp->body_font = XLoadQueryFont (si->dpy, "fixed");
205   if (f) free (f);
206
207   f = get_string_resource("splash.buttonFont", "Dialog.Font");
208   sp->button_font = XLoadQueryFont (si->dpy, (f ? f : "fixed"));
209   if (!sp->button_font) sp->button_font = XLoadQueryFont (si->dpy, "fixed");
210   if (f) free (f);
211
212   sp->foreground = get_pixel_resource ("splash.foreground",
213                                        "Dialog.Foreground",
214                                        si->dpy, cmap);
215   sp->background = get_pixel_resource ("splash.background",
216                                        "Dialog.Background",
217                                        si->dpy, cmap);
218
219   if (sp->foreground == sp->background)
220     {
221       /* Make sure the error messages show up. */
222       sp->foreground = BlackPixelOfScreen (screen);
223       sp->background = WhitePixelOfScreen (screen);
224     }
225
226   sp->button_foreground = get_pixel_resource ("splash.Button.foreground",
227                                               "Dialog.Button.Foreground",
228                                               si->dpy, cmap);
229   sp->button_background = get_pixel_resource ("splash.Button.background",
230                                               "Dialog.Button.Background",
231                                               si->dpy, cmap);
232   sp->logo_foreground = get_pixel_resource ("splash.logo.foreground",
233                                             "Dialog.Logo.Foreground",
234                                             si->dpy, cmap);
235   sp->logo_background = get_pixel_resource ("splash.logo.background",
236                                             "Dialog.Logo.Background",
237                                             si->dpy, cmap);
238   sp->shadow_top = get_pixel_resource ("splash.topShadowColor",
239                                        "Dialog.Foreground",
240                                        si->dpy, cmap);
241   sp->shadow_bottom = get_pixel_resource ("splash.bottomShadowColor",
242                                           "Dialog.Background",
243                                           si->dpy, cmap);
244
245   sp->logo_width = get_integer_resource ("splash.logo.width",
246                                          "Dialog.Logo.Width");
247   sp->logo_height = get_integer_resource ("splash.logo.height",
248                                           "Dialog.Logo.Height");
249   sp->internal_border = get_integer_resource ("splash.internalBorderWidth",
250                                               "Dialog.InternalBorderWidth");
251   sp->shadow_width = get_integer_resource ("splash.shadowThickness",
252                                            "Dialog.ShadowThickness");
253
254   if (sp->logo_width == 0)  sp->logo_width = 150;
255   if (sp->logo_height == 0) sp->logo_height = 150;
256   if (sp->internal_border == 0) sp->internal_border = 15;
257   if (sp->shadow_width == 0) sp->shadow_width = 4;
258
259   {
260     int direction, ascent, descent;
261     XCharStruct overall;
262
263     sp->width = 0;
264     sp->height = 0;
265
266     /* Measure the heading_label. */
267     XTextExtents (sp->heading_font,
268                   sp->heading_label, strlen(sp->heading_label),
269                   &direction, &ascent, &descent, &overall);
270     if (overall.width > sp->width) sp->width = overall.width;
271     sp->height += ascent + descent;
272
273     /* Measure the body_label. */
274     XTextExtents (sp->body_font,
275                   sp->body_label, strlen(sp->body_label),
276                   &direction, &ascent, &descent, &overall);
277     if (overall.width > sp->width) sp->width = overall.width;
278     sp->height += ascent + descent;
279
280     /* Measure the body2_label. */
281     XTextExtents (sp->body_font,
282                   sp->body2_label, strlen(sp->body2_label),
283                   &direction, &ascent, &descent, &overall);
284     if (overall.width > sp->width) sp->width = overall.width;
285     sp->height += ascent + descent;
286
287     {
288       Dimension w2 = 0, w3 = 0, w4 = 0;
289       Dimension h2 = 0, h3 = 0, h4 = 0;
290
291       /* Measure the Demo button. */
292       XTextExtents (sp->button_font,
293                     sp->demo_label, strlen(sp->demo_label),
294                     &direction, &ascent, &descent, &overall);
295       w2 = overall.width;
296       h2 = ascent + descent;
297
298       /* Measure the Prefs button. */
299       XTextExtents (sp->button_font,
300                     sp->prefs_label, strlen(sp->prefs_label),
301                     &direction, &ascent, &descent, &overall);
302       w3 = overall.width;
303       h3 = ascent + descent;
304
305       /* Measure the Help button. */
306       XTextExtents (sp->button_font,
307                     sp->help_label, strlen(sp->help_label),
308                     &direction, &ascent, &descent, &overall);
309       w4 = overall.width;
310       h4 = ascent + descent;
311
312       w2 = MAX(w2, w3); w2 = MAX(w2, w4);
313       h2 = MAX(h2, h3); h2 = MAX(h2, h4);
314
315       w2 += ((ascent + descent) / 2) + (sp->shadow_width * 2);
316       h2 += ((ascent + descent) / 2) + (sp->shadow_width * 2);
317
318       sp->button_width = w2;
319       sp->button_height = h2;
320
321       w2 *= 3;
322
323       w2 += ((ascent + descent) * 2);  /* for space between buttons */
324
325       if (w2 > sp->width) sp->width = w2;
326       sp->height += h2;
327     }
328
329     sp->width  += (sp->internal_border * 2);
330     sp->height += (sp->internal_border * 3);
331
332     if (sp->logo_height > sp->height)
333       sp->height = sp->logo_height;
334     else if (sp->height > sp->logo_height)
335       sp->logo_height = sp->height;
336
337     sp->logo_width = sp->logo_height;
338
339     sp->width += sp->logo_width;
340   }
341
342   attrmask |= CWOverrideRedirect; attrs.override_redirect = True;
343   attrmask |= CWEventMask;
344   attrs.event_mask = (ExposureMask | ButtonPressMask | ButtonReleaseMask);
345
346   {
347     int sx, sy, w, h;
348     get_screen_viewport (si->default_screen, &sx, &sy, &w, &h, False);
349     if (si->prefs.debug_p) w /= 2;
350     x = sx + (((w + sp->width)  / 2) - sp->width);
351     y = sy + (((h + sp->height) / 2) - sp->height);
352     if (x < sx) x = sx;
353     if (y < sy) y = sy;
354   }
355
356   bw = get_integer_resource ("splash.borderWidth", "Dialog.BorderWidth");
357
358   si->splash_dialog =
359     XCreateWindow (si->dpy,
360                    RootWindowOfScreen(screen),
361                    x, y, sp->width, sp->height, bw,
362                    DefaultDepthOfScreen (screen), InputOutput,
363                    DefaultVisualOfScreen(screen),
364                    attrmask, &attrs);
365   XSetWindowBackground (si->dpy, si->splash_dialog, sp->background);
366
367   XMapRaised (si->dpy, si->splash_dialog);
368   XSync (si->dpy, False);
369
370   si->sp_data = sp;
371
372   sp->timer = XtAppAddTimeOut (si->app, si->prefs.splash_duration,
373                                unsplash_timer, (XtPointer) si);
374
375   draw_splash_window (si);
376   XSync (si->dpy, False);
377 }
378
379 void
380 draw_logo (saver_info *si, Window win, int x, int y, int w, int h,
381            Bool first_time_p)
382 {
383   Colormap cmap = DefaultColormapOfScreen (si->default_screen->screen);
384   Pixmap logo_map = XCreatePixmap (si->dpy, win, w, h, 
385                                    si->default_screen->current_depth);
386   XGCValues gcv;
387   GC gc = XCreateGC (si->dpy, win, 0, &gcv);
388   xscreensaver_logo (si->dpy, logo_map, cmap, !first_time_p);
389   XCopyArea (si->dpy, logo_map, win, gc, 0, 0, w, h, x, y);
390   XFreeGC (si->dpy, gc);
391   XFreePixmap (si->dpy, logo_map);
392 }
393
394
395 static void
396 draw_splash_window (saver_info *si)
397 {
398   splash_dialog_data *sp = si->sp_data;
399   XGCValues gcv;
400   GC gc1, gc2;
401   int hspacing, vspacing, height;
402   int x1, x2, x3, y1, y2;
403   int sw;
404
405   height = (sp->heading_font->ascent + sp->heading_font->descent +
406             sp->body_font->ascent + sp->body_font->descent +
407             sp->body_font->ascent + sp->body_font->descent +
408             sp->button_font->ascent + sp->button_font->descent);
409   vspacing = ((sp->height
410                - (4 * sp->shadow_width)
411                - (2 * sp->internal_border)
412                - height) / 5);
413   if (vspacing < 0) vspacing = 0;
414   if (vspacing > (sp->heading_font->ascent * 2))
415     vspacing = (sp->heading_font->ascent * 2);
416             
417   gcv.foreground = sp->foreground;
418   gc1 = XCreateGC (si->dpy, si->splash_dialog, GCForeground, &gcv);
419   gc2 = XCreateGC (si->dpy, si->splash_dialog, GCForeground, &gcv);
420   x1 = sp->logo_width;
421   x3 = sp->width - (sp->shadow_width * 2);
422   y1 = sp->internal_border;
423
424   /* top heading
425    */
426   XSetFont (si->dpy, gc1, sp->heading_font->fid);
427   sw = string_width (sp->heading_font, sp->heading_label);
428   x2 = (x1 + ((x3 - x1 - sw) / 2));
429   y1 += sp->heading_font->ascent;
430   XDrawString (si->dpy, si->splash_dialog, gc1, x2, y1,
431                sp->heading_label, strlen(sp->heading_label));
432   y1 += sp->heading_font->descent;
433
434   /* text below top heading
435    */
436   XSetFont (si->dpy, gc1, sp->body_font->fid);
437   y1 += vspacing + sp->body_font->ascent;
438   sw = string_width (sp->body_font, sp->body_label);
439   x2 = (x1 + ((x3 - x1 - sw) / 2));
440   XDrawString (si->dpy, si->splash_dialog, gc1, x2, y1,
441                sp->body_label, strlen(sp->body_label));
442   y1 += sp->body_font->descent;
443
444   y1 += sp->body_font->ascent;
445   sw = string_width (sp->body_font, sp->body2_label);
446   x2 = (x1 + ((x3 - x1 - sw) / 2));
447   XDrawString (si->dpy, si->splash_dialog, gc1, x2, y1,
448                sp->body2_label, strlen(sp->body2_label));
449   y1 += sp->body_font->descent;
450
451   /* The buttons
452    */
453   XSetForeground (si->dpy, gc1, sp->button_foreground);
454   XSetForeground (si->dpy, gc2, sp->button_background);
455
456 /*  y1 += (vspacing * 2);*/
457   y1 = sp->height - sp->internal_border - sp->button_height;
458
459   x1 += sp->internal_border;
460   y2 = (y1 + ((sp->button_height -
461                (sp->button_font->ascent + sp->button_font->descent))
462               / 2)
463         + sp->button_font->ascent);
464   hspacing = ((sp->width - x1 - (sp->shadow_width * 2) -
465                sp->internal_border - (sp->button_width * 3))
466               / 2);
467
468   x2 = x1 + ((sp->button_width - string_width(sp->button_font, sp->demo_label))
469              / 2);
470   XFillRectangle (si->dpy, si->splash_dialog, gc2, x1, y1,
471                   sp->button_width, sp->button_height);
472   XDrawString (si->dpy, si->splash_dialog, gc1, x2, y2,
473                sp->demo_label, strlen(sp->demo_label));
474   sp->demo_button_x = x1;
475   sp->demo_button_y = y1;
476   
477   x1 += hspacing + sp->button_width;
478   x2 = x1 + ((sp->button_width - string_width(sp->button_font,sp->prefs_label))
479              / 2);
480   XFillRectangle (si->dpy, si->splash_dialog, gc2, x1, y1,
481                   sp->button_width, sp->button_height);
482   XDrawString (si->dpy, si->splash_dialog, gc1, x2, y2,
483                sp->prefs_label, strlen(sp->prefs_label));
484   sp->prefs_button_x = x1;
485   sp->prefs_button_y = y1;
486
487   x1 += hspacing + sp->button_width;
488   x2 = x1 + ((sp->button_width - string_width(sp->button_font,sp->help_label))
489              / 2);
490   XFillRectangle (si->dpy, si->splash_dialog, gc2, x1, y1,
491                   sp->button_width, sp->button_height);
492   XDrawString (si->dpy, si->splash_dialog, gc1, x2, y2,
493                sp->help_label, strlen(sp->help_label));
494   sp->help_button_x = x1;
495   sp->help_button_y = y1;
496
497
498   /* the logo
499    */
500   XSetForeground (si->dpy, gc1, sp->logo_foreground);
501   XSetForeground (si->dpy, gc2, sp->logo_background);
502
503   x1 = sp->shadow_width * 3;
504   y1 = sp->shadow_width * 3;
505   x2 = sp->logo_width - (sp->shadow_width * 6);
506   y2 = sp->logo_height - (sp->shadow_width * 6);
507
508   draw_logo (si, si->splash_dialog, x1, y1, x2, y2, True);
509
510
511   /* The shadow around the logo
512    */
513   draw_shaded_rectangle (si->dpy, si->splash_dialog,
514                          sp->shadow_width * 2,
515                          sp->shadow_width * 2,
516                          sp->logo_width - (sp->shadow_width * 4),
517                          sp->logo_height - (sp->shadow_width * 4),
518                          sp->shadow_width,
519                          sp->shadow_bottom, sp->shadow_top);
520
521   /* The shadow around the whole window
522    */
523   draw_shaded_rectangle (si->dpy, si->splash_dialog,
524                          0, 0, sp->width, sp->height, sp->shadow_width,
525                          sp->shadow_top, sp->shadow_bottom);
526
527   XFreeGC (si->dpy, gc1);
528   XFreeGC (si->dpy, gc2);
529
530   update_splash_window (si);
531 }
532
533
534 static void
535 update_splash_window (saver_info *si)
536 {
537   splash_dialog_data *sp = si->sp_data;
538   int pressed;
539   if (!sp) return;
540   pressed = sp->pressed;
541
542   /* The shadows around the buttons
543    */
544   draw_shaded_rectangle (si->dpy, si->splash_dialog,
545                          sp->demo_button_x, sp->demo_button_y,
546                          sp->button_width, sp->button_height, sp->shadow_width,
547                          (pressed == 1 ? sp->shadow_bottom : sp->shadow_top),
548                          (pressed == 1 ? sp->shadow_top : sp->shadow_bottom));
549   draw_shaded_rectangle (si->dpy, si->splash_dialog,
550                          sp->prefs_button_x, sp->prefs_button_y,
551                          sp->button_width, sp->button_height, sp->shadow_width,
552                          (pressed == 2 ? sp->shadow_bottom : sp->shadow_top),
553                          (pressed == 2 ? sp->shadow_top : sp->shadow_bottom));
554   draw_shaded_rectangle (si->dpy, si->splash_dialog,
555                          sp->help_button_x, sp->help_button_y,
556                          sp->button_width, sp->button_height, sp->shadow_width,
557                          (pressed == 3 ? sp->shadow_bottom : sp->shadow_top),
558                          (pressed == 3 ? sp->shadow_top : sp->shadow_bottom));
559 }
560
561 static void
562 destroy_splash_window (saver_info *si)
563 {
564   splash_dialog_data *sp = si->sp_data;
565   Screen *screen = si->default_screen->screen;
566   Colormap cmap = DefaultColormapOfScreen (screen);
567   Pixel black = BlackPixelOfScreen (screen);
568   Pixel white = WhitePixelOfScreen (screen);
569
570   if (sp->timer)
571     XtRemoveTimeOut (sp->timer);
572
573   if (si->splash_dialog)
574     {
575       XDestroyWindow (si->dpy, si->splash_dialog);
576       si->splash_dialog = 0;
577     }
578   
579   if (sp->heading_label) free (sp->heading_label);
580   if (sp->body_label)    free (sp->body_label);
581   if (sp->demo_label)    free (sp->demo_label);
582   if (sp->prefs_label)   free (sp->prefs_label);
583   if (sp->help_label)    free (sp->help_label);
584
585   if (sp->heading_font) XFreeFont (si->dpy, sp->heading_font);
586   if (sp->body_font)    XFreeFont (si->dpy, sp->body_font);
587   if (sp->button_font)  XFreeFont (si->dpy, sp->button_font);
588
589   if (sp->foreground != black && sp->foreground != white)
590     XFreeColors (si->dpy, cmap, &sp->foreground, 1, 0L);
591   if (sp->background != black && sp->background != white)
592     XFreeColors (si->dpy, cmap, &sp->background, 1, 0L);
593   if (sp->button_foreground != black && sp->button_foreground != white)
594     XFreeColors (si->dpy, cmap, &sp->button_foreground, 1, 0L);
595   if (sp->button_background != black && sp->button_background != white)
596     XFreeColors (si->dpy, cmap, &sp->button_background, 1, 0L);
597   if (sp->logo_foreground != black && sp->logo_foreground != white)
598     XFreeColors (si->dpy, cmap, &sp->logo_foreground, 1, 0L);
599   if (sp->logo_background != black && sp->logo_background != white)
600     XFreeColors (si->dpy, cmap, &sp->logo_background, 1, 0L);
601   if (sp->shadow_top != black && sp->shadow_top != white)
602     XFreeColors (si->dpy, cmap, &sp->shadow_top, 1, 0L);
603   if (sp->shadow_bottom != black && sp->shadow_bottom != white)
604     XFreeColors (si->dpy, cmap, &sp->shadow_bottom, 1, 0L);
605
606   memset (sp, 0, sizeof(*sp));
607   free (sp);
608
609   si->sp_data = 0;
610 }
611
612 void
613 handle_splash_event (saver_info *si, XEvent *event)
614 {
615   splash_dialog_data *sp = si->sp_data;
616   int which = 0;
617   if (!sp) return;
618
619   switch (event->xany.type)
620     {
621     case Expose:
622       draw_splash_window (si);
623       break;
624
625     case ButtonPress: case ButtonRelease:
626
627       if (event->xbutton.x >= sp->demo_button_x &&
628           event->xbutton.x < sp->demo_button_x + sp->button_width &&
629           event->xbutton.y >= sp->demo_button_y &&
630           event->xbutton.y < sp->demo_button_y + sp->button_height)
631         which = 1;
632
633       else if (event->xbutton.x >= sp->prefs_button_x &&
634                event->xbutton.x < sp->prefs_button_x + sp->button_width &&
635                event->xbutton.y >= sp->prefs_button_y &&
636                event->xbutton.y < sp->prefs_button_y + sp->button_height)
637         which = 2;
638
639       else if (event->xbutton.x >= sp->help_button_x &&
640                event->xbutton.x < sp->help_button_x + sp->button_width &&
641                event->xbutton.y >= sp->help_button_y &&
642                event->xbutton.y < sp->help_button_y + sp->button_height)
643         which = 3;
644
645       if (event->xany.type == ButtonPress)
646         {
647           sp->pressed = which;
648           update_splash_window (si);
649           if (which == 0)
650             XBell (si->dpy, False);
651         }
652       else if (event->xany.type == ButtonRelease)
653         {
654           if (which && sp->pressed == which)
655             {
656               destroy_splash_window (si);
657               switch (which)
658                 {
659                 case 1: do_demo (si); break;
660                 case 2: do_prefs (si); break;
661                 case 3: do_help (si); break;
662                 default: abort();
663                 }
664             }
665           sp->pressed = 0;
666           update_splash_window (si);
667         }
668       break;
669
670     default:
671       break;
672     }
673 }
674
675 static void
676 unsplash_timer (XtPointer closure, XtIntervalId *id)
677 {
678   saver_info *si = (saver_info *) closure;
679   if (si && si->sp_data)
680     destroy_splash_window (si);
681 }
682
683 \f
684 /* Button callbacks */
685
686 #ifdef VMS
687 # define pid_t int
688 # define fork  vfork
689 #endif /* VMS */
690
691 static void
692 fork_and_exec (saver_info *si, const char *command, const char *desc)
693 {
694   saver_preferences *p = &si->prefs;
695   pid_t forked;
696   char buf [512];
697   char *av[5];
698   int ac;
699
700   if (!command || !*command)
701     {
702       fprintf (stderr, "%s: no %s command has been specified.\n",
703                blurb(), desc);
704       return;
705     }
706
707   switch ((int) (forked = fork ()))
708     {
709     case -1:
710       sprintf (buf, "%s: couldn't fork", blurb());
711       perror (buf);
712       break;
713
714     case 0:
715       close (ConnectionNumber (si->dpy));               /* close display fd */
716       hack_subproc_environment (si->default_screen);    /* set $DISPLAY */
717       ac = 0;
718       av [ac++] = (char *) p->shell;
719       av [ac++] = (char *) "-c";
720       av [ac++] = (char *) command;
721       av [ac]   = 0;
722       execvp (av[0], av);                               /* shouldn't return. */
723
724       sprintf (buf, "%s: execvp(\"%s\", \"%s\", \"%s\") failed",
725                blurb(), av[0], av[1], av[2]);
726       perror (buf);
727       fflush (stderr);
728       fflush (stdout);
729       exit (1);                  /* Note that this only exits a child fork.  */
730       break;
731
732     default:
733       /* parent fork. */
734       break;
735     }
736 }
737
738
739 static void
740 do_demo (saver_info *si)
741 {
742   saver_preferences *p = &si->prefs;
743   fork_and_exec (si, p->demo_command, "demo-mode");
744 }
745
746 static void
747 do_prefs (saver_info *si)
748 {
749   saver_preferences *p = &si->prefs;
750   fork_and_exec (si, p->prefs_command, "preferences");
751 }
752
753 static void
754 do_help (saver_info *si)
755 {
756   saver_preferences *p = &si->prefs;
757   char *help_command;
758
759   if (!p->help_url || !*p->help_url)
760     {
761       fprintf (stderr, "%s: no Help URL has been specified.\n", blurb());
762       return;
763     }
764
765   help_command = (char *) malloc (strlen (p->load_url_command) +
766                                   (strlen (p->help_url) * 2) + 10);
767   sprintf (help_command, p->load_url_command, p->help_url, p->help_url);
768   fork_and_exec (si, help_command, "URL-loading");
769   free (help_command);
770 }