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