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