1 /* xscreensaver, Copyright (c) 1991-1998 Jamie Zawinski <jwz@netscape.com>
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
16 #include <X11/Intrinsic.h>
18 #include "xscreensaver.h"
19 #include "resources.h"
22 #define MAX(a,b) ((a)>(b)?(a):(b))
25 draw_shaded_rectangle (Display *dpy, Window window,
27 int width, int height,
29 unsigned long top_color,
30 unsigned long bottom_color)
35 if (thickness == 0) return;
37 gcv.foreground = top_color;
38 gc1 = XCreateGC (dpy, window, GCForeground, &gcv);
39 gcv.foreground = bottom_color;
40 gc2 = XCreateGC (dpy, window, GCForeground, &gcv);
44 points [1].x = x + width;
46 points [2].x = x + width - thickness;
47 points [2].y = y + thickness;
49 points [3].y = y + thickness;
50 XFillPolygon (dpy, window, gc1, points, 4, Convex, CoordModeOrigin);
53 points [0].y = y + thickness;
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);
62 points [0].x = x + width;
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);
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);
88 string_width (XFontStruct *font, char *s)
90 int direction, ascent, descent;
92 XTextExtents (font, s, strlen(s), &direction, &ascent, &descent, &overall);
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);
102 static void do_demo (saver_info *si);
103 static void do_prefs (saver_info *si);
104 static void do_help (saver_info *si);
107 struct splash_dialog_data {
120 XFontStruct *heading_font;
121 XFontStruct *body_font;
122 XFontStruct *button_font;
126 Pixel button_foreground;
127 Pixel button_background;
128 Pixel logo_foreground;
129 Pixel logo_background;
133 Dimension logo_width;
134 Dimension logo_height;
135 Dimension internal_border;
136 Dimension shadow_width;
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;
148 make_splash_dialog (saver_info *si)
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);
160 if (si->prefs.splash_duration <= 0)
163 sp = (splash_dialog_data *) calloc (1, sizeof(*sp));
165 sp->heading_label = get_string_resource ("splash.heading.label",
166 "Dialog.Label.Label");
167 sp->body_label = get_string_resource ("splash.body.label",
168 "Dialog.Label.Label");
169 sp->body2_label = get_string_resource ("splash.body2.label",
170 "Dialog.Label.Label");
171 sp->demo_label = get_string_resource ("splash.demo.label",
172 "Dialog.Button.Label");
173 sp->prefs_label = get_string_resource ("splash.prefs.label",
174 "Dialog.Button.Label");
175 sp->help_label = get_string_resource ("splash.help.label",
176 "Dialog.Button.Label");
178 if (!sp->heading_label)
179 sp->heading_label = strdup("ERROR: REESOURCES NOT INSTALLED CORRECTLY");
181 sp->body_label = strdup("ERROR: REESOURCES NOT INSTALLED CORRECTLY");
182 if (!sp->body2_label)
183 sp->body2_label = strdup("ERROR: REESOURCES NOT INSTALLED CORRECTLY");
184 if (!sp->demo_label) sp->demo_label = strdup("ERROR");
185 if (!sp->prefs_label) sp->prefs_label = strdup("ERROR");
186 if (!sp->help_label) sp->help_label = strdup("ERROR");
188 /* Put the version number in the label. */
190 char *s = (char *) malloc (strlen(sp->heading_label) + 20);
191 sprintf(s, sp->heading_label, si->version);
192 free (sp->heading_label);
193 sp->heading_label = s;
196 f = get_string_resource ("splash.headingFont", "Dialog.Font");
197 sp->heading_font = XLoadQueryFont (si->dpy, (f ? f : "fixed"));
198 if (!sp->heading_font) sp->heading_font = XLoadQueryFont (si->dpy, "fixed");
201 f = get_string_resource("splash.bodyFont", "Dialog.Font");
202 sp->body_font = XLoadQueryFont (si->dpy, (f ? f : "fixed"));
203 if (!sp->body_font) sp->body_font = XLoadQueryFont (si->dpy, "fixed");
206 f = get_string_resource("splash.buttonFont", "Dialog.Font");
207 sp->button_font = XLoadQueryFont (si->dpy, (f ? f : "fixed"));
208 if (!sp->button_font) sp->button_font = XLoadQueryFont (si->dpy, "fixed");
211 sp->foreground = get_pixel_resource ("splash.foreground",
214 sp->background = get_pixel_resource ("splash.background",
218 if (sp->foreground == sp->background)
220 /* Make sure the error messages show up. */
221 sp->foreground = BlackPixelOfScreen (screen);
222 sp->background = WhitePixelOfScreen (screen);
225 sp->button_foreground = get_pixel_resource ("splash.Button.foreground",
226 "Dialog.Button.Foreground",
228 sp->button_background = get_pixel_resource ("splash.Button.background",
229 "Dialog.Button.Background",
231 sp->logo_foreground = get_pixel_resource ("splash.logo.foreground",
232 "Dialog.Logo.Foreground",
234 sp->logo_background = get_pixel_resource ("splash.logo.background",
235 "Dialog.Logo.Background",
237 sp->shadow_top = get_pixel_resource ("splash.topShadowColor",
240 sp->shadow_bottom = get_pixel_resource ("splash.bottomShadowColor",
244 sp->logo_width = get_integer_resource ("splash.logo.width",
245 "Dialog.Logo.Width");
246 sp->logo_height = get_integer_resource ("splash.logo.height",
247 "Dialog.Logo.Height");
248 sp->internal_border = get_integer_resource ("splash.internalBorderWidth",
249 "Dialog.InternalBorderWidth");
250 sp->shadow_width = get_integer_resource ("splash.shadowThickness",
251 "Dialog.ShadowThickness");
253 if (sp->logo_width == 0) sp->logo_width = 150;
254 if (sp->logo_height == 0) sp->logo_height = 150;
255 if (sp->internal_border == 0) sp->internal_border = 15;
256 if (sp->shadow_width == 0) sp->shadow_width = 4;
259 int direction, ascent, descent;
265 /* Measure the heading_label. */
266 XTextExtents (sp->heading_font,
267 sp->heading_label, strlen(sp->heading_label),
268 &direction, &ascent, &descent, &overall);
269 if (overall.width > sp->width) sp->width = overall.width;
270 sp->height += ascent + descent;
272 /* Measure the body_label. */
273 XTextExtents (sp->body_font,
274 sp->body_label, strlen(sp->body_label),
275 &direction, &ascent, &descent, &overall);
276 if (overall.width > sp->width) sp->width = overall.width;
277 sp->height += ascent + descent;
279 /* Measure the body2_label. */
280 XTextExtents (sp->body_font,
281 sp->body2_label, strlen(sp->body2_label),
282 &direction, &ascent, &descent, &overall);
283 if (overall.width > sp->width) sp->width = overall.width;
284 sp->height += ascent + descent;
287 Dimension w2 = 0, w3 = 0, w4 = 0;
288 Dimension h2 = 0, h3 = 0, h4 = 0;
290 /* Measure the Demo button. */
291 XTextExtents (sp->button_font,
292 sp->demo_label, strlen(sp->demo_label),
293 &direction, &ascent, &descent, &overall);
295 h2 = ascent + descent;
297 /* Measure the Prefs button. */
298 XTextExtents (sp->button_font,
299 sp->prefs_label, strlen(sp->prefs_label),
300 &direction, &ascent, &descent, &overall);
302 h3 = ascent + descent;
304 /* Measure the Help button. */
305 XTextExtents (sp->button_font,
306 sp->help_label, strlen(sp->help_label),
307 &direction, &ascent, &descent, &overall);
309 h4 = ascent + descent;
311 w2 = MAX(w2, w3); w2 = MAX(w2, w4);
312 h2 = MAX(h2, h3); h2 = MAX(h2, h4);
314 w2 += ((ascent + descent) / 2) + (sp->shadow_width * 2);
315 h2 += ((ascent + descent) / 2) + (sp->shadow_width * 2);
317 sp->button_width = w2;
318 sp->button_height = h2;
322 w2 += ((ascent + descent) * 2); /* for space between buttons */
324 if (w2 > sp->width) sp->width = w2;
328 sp->width += (sp->internal_border * 2);
329 sp->height += (sp->internal_border * 3);
331 if (sp->logo_height > sp->height)
332 sp->height = sp->logo_height;
333 else if (sp->height > sp->logo_height)
334 sp->logo_height = sp->height;
336 sp->logo_width = sp->logo_height;
338 sp->width += sp->logo_width;
341 attrmask |= CWOverrideRedirect; attrs.override_redirect = True;
342 attrmask |= CWEventMask;
343 attrs.event_mask = (ExposureMask | ButtonPressMask | ButtonReleaseMask);
346 Dimension w = WidthOfScreen(screen);
347 Dimension h = HeightOfScreen(screen);
348 if (si->prefs.debug_p) w /= 2;
349 x = ((w + sp->width) / 2) - sp->width;
350 y = ((h + sp->height) / 2) - sp->height;
355 bw = get_integer_resource ("splash.borderWidth", "Dialog.BorderWidth");
358 XCreateWindow (si->dpy,
359 RootWindowOfScreen(screen),
360 x, y, sp->width, sp->height, bw,
361 DefaultDepthOfScreen (screen), InputOutput,
362 DefaultVisualOfScreen(screen),
364 XSetWindowBackground (si->dpy, si->splash_dialog, sp->background);
366 XMapRaised (si->dpy, si->splash_dialog);
367 XSync (si->dpy, False);
371 sp->timer = XtAppAddTimeOut (si->app, si->prefs.splash_duration,
372 unsplash_timer, (XtPointer) si);
374 draw_splash_window (si);
375 XSync (si->dpy, False);
379 draw_splash_window (saver_info *si)
381 splash_dialog_data *sp = si->sp_data;
384 int hspacing, vspacing, height;
385 int x1, x2, x3, y1, y2;
388 height = (sp->heading_font->ascent + sp->heading_font->descent +
389 sp->body_font->ascent + sp->body_font->descent +
390 sp->body_font->ascent + sp->body_font->descent +
391 sp->button_font->ascent + sp->button_font->descent);
392 vspacing = ((sp->height
393 - (4 * sp->shadow_width)
394 - (2 * sp->internal_border)
396 if (vspacing < 0) vspacing = 0;
397 if (vspacing > (sp->heading_font->ascent * 2))
398 vspacing = (sp->heading_font->ascent * 2);
400 gcv.foreground = sp->foreground;
401 gc1 = XCreateGC (si->dpy, si->splash_dialog, GCForeground, &gcv);
402 gc2 = XCreateGC (si->dpy, si->splash_dialog, GCForeground, &gcv);
404 x3 = sp->width - (sp->shadow_width * 2);
405 y1 = sp->internal_border;
409 XSetFont (si->dpy, gc1, sp->heading_font->fid);
410 sw = string_width (sp->heading_font, sp->heading_label);
411 x2 = (x1 + ((x3 - x1 - sw) / 2));
412 y1 += sp->heading_font->ascent;
413 XDrawString (si->dpy, si->splash_dialog, gc1, x2, y1,
414 sp->heading_label, strlen(sp->heading_label));
415 y1 += sp->heading_font->descent;
417 /* text below top heading
419 XSetFont (si->dpy, gc1, sp->body_font->fid);
420 y1 += vspacing + sp->body_font->ascent;
421 sw = string_width (sp->body_font, sp->body_label);
422 x2 = (x1 + ((x3 - x1 - sw) / 2));
423 XDrawString (si->dpy, si->splash_dialog, gc1, x2, y1,
424 sp->body_label, strlen(sp->body_label));
425 y1 += sp->body_font->descent;
427 y1 += sp->body_font->ascent;
428 sw = string_width (sp->body_font, sp->body2_label);
429 x2 = (x1 + ((x3 - x1 - sw) / 2));
430 XDrawString (si->dpy, si->splash_dialog, gc1, x2, y1,
431 sp->body2_label, strlen(sp->body2_label));
432 y1 += sp->body_font->descent;
436 XSetForeground (si->dpy, gc1, sp->button_foreground);
437 XSetForeground (si->dpy, gc2, sp->button_background);
439 /* y1 += (vspacing * 2);*/
440 y1 = sp->height - sp->internal_border - sp->button_height;
442 x1 += sp->internal_border;
443 y2 = (y1 + ((sp->button_height -
444 (sp->button_font->ascent + sp->button_font->descent))
446 + sp->button_font->ascent);
447 hspacing = ((sp->width - x1 - (sp->shadow_width * 2) -
448 sp->internal_border - (sp->button_width * 3))
451 x2 = x1 + ((sp->button_width - string_width(sp->button_font, sp->demo_label))
453 XFillRectangle (si->dpy, si->splash_dialog, gc2, x1, y1,
454 sp->button_width, sp->button_height);
455 XDrawString (si->dpy, si->splash_dialog, gc1, x2, y2,
456 sp->demo_label, strlen(sp->demo_label));
457 sp->demo_button_x = x1;
458 sp->demo_button_y = y1;
460 x1 += hspacing + sp->button_width;
461 x2 = x1 + ((sp->button_width - string_width(sp->button_font,sp->prefs_label))
463 XFillRectangle (si->dpy, si->splash_dialog, gc2, x1, y1,
464 sp->button_width, sp->button_height);
465 XDrawString (si->dpy, si->splash_dialog, gc1, x2, y2,
466 sp->prefs_label, strlen(sp->prefs_label));
467 sp->prefs_button_x = x1;
468 sp->prefs_button_y = y1;
470 x1 += hspacing + sp->button_width;
471 x2 = x1 + ((sp->button_width - string_width(sp->button_font,sp->help_label))
473 XFillRectangle (si->dpy, si->splash_dialog, gc2, x1, y1,
474 sp->button_width, sp->button_height);
475 XDrawString (si->dpy, si->splash_dialog, gc1, x2, y2,
476 sp->help_label, strlen(sp->help_label));
477 sp->help_button_x = x1;
478 sp->help_button_y = y1;
483 XSetForeground (si->dpy, gc1, sp->logo_foreground);
484 XSetForeground (si->dpy, gc2, sp->logo_background);
486 x1 = sp->shadow_width * 3;
487 y1 = sp->shadow_width * 3;
488 x2 = sp->logo_width - (sp->shadow_width * 6);
489 y2 = sp->logo_height - (sp->shadow_width * 6);
491 XFillRectangle (si->dpy, si->splash_dialog, gc2, x1, y1, x2, y2);
492 skull (si->dpy, si->splash_dialog, gc1, gc2,
493 x1 + sp->shadow_width, y1 + sp->shadow_width,
494 x2 - (sp->shadow_width * 2), y2 - (sp->shadow_width * 2));
497 /* The shadow around the logo
499 draw_shaded_rectangle (si->dpy, si->splash_dialog,
500 sp->shadow_width * 2,
501 sp->shadow_width * 2,
502 sp->logo_width - (sp->shadow_width * 4),
503 sp->logo_height - (sp->shadow_width * 4),
505 sp->shadow_bottom, sp->shadow_top);
507 /* The shadow around the whole window
509 draw_shaded_rectangle (si->dpy, si->splash_dialog,
510 0, 0, sp->width, sp->height, sp->shadow_width,
511 sp->shadow_top, sp->shadow_bottom);
513 XFreeGC (si->dpy, gc1);
514 XFreeGC (si->dpy, gc2);
516 update_splash_window (si);
521 update_splash_window (saver_info *si)
523 splash_dialog_data *sp = si->sp_data;
526 pressed = sp->pressed;
528 /* The shadows around the buttons
530 draw_shaded_rectangle (si->dpy, si->splash_dialog,
531 sp->demo_button_x, sp->demo_button_y,
532 sp->button_width, sp->button_height, sp->shadow_width,
533 (pressed == 1 ? sp->shadow_bottom : sp->shadow_top),
534 (pressed == 1 ? sp->shadow_top : sp->shadow_bottom));
535 draw_shaded_rectangle (si->dpy, si->splash_dialog,
536 sp->prefs_button_x, sp->prefs_button_y,
537 sp->button_width, sp->button_height, sp->shadow_width,
538 (pressed == 2 ? sp->shadow_bottom : sp->shadow_top),
539 (pressed == 2 ? sp->shadow_top : sp->shadow_bottom));
540 draw_shaded_rectangle (si->dpy, si->splash_dialog,
541 sp->help_button_x, sp->help_button_y,
542 sp->button_width, sp->button_height, sp->shadow_width,
543 (pressed == 3 ? sp->shadow_bottom : sp->shadow_top),
544 (pressed == 3 ? sp->shadow_top : sp->shadow_bottom));
548 destroy_splash_window (saver_info *si)
550 splash_dialog_data *sp = si->sp_data;
551 Screen *screen = si->default_screen->screen;
552 Colormap cmap = DefaultColormapOfScreen (screen);
553 Pixel black = BlackPixelOfScreen (screen);
554 Pixel white = WhitePixelOfScreen (screen);
557 XtRemoveTimeOut (sp->timer);
559 if (si->splash_dialog)
561 XDestroyWindow (si->dpy, si->splash_dialog);
562 si->splash_dialog = 0;
565 if (sp->heading_label) free (sp->heading_label);
566 if (sp->body_label) free (sp->body_label);
567 if (sp->demo_label) free (sp->demo_label);
568 if (sp->prefs_label) free (sp->prefs_label);
569 if (sp->help_label) free (sp->help_label);
571 if (sp->heading_font) XFreeFont (si->dpy, sp->heading_font);
572 if (sp->body_font) XFreeFont (si->dpy, sp->body_font);
573 if (sp->button_font) XFreeFont (si->dpy, sp->button_font);
575 if (sp->foreground != black && sp->foreground != white)
576 XFreeColors (si->dpy, cmap, &sp->foreground, 1, 0L);
577 if (sp->background != black && sp->background != white)
578 XFreeColors (si->dpy, cmap, &sp->background, 1, 0L);
579 if (sp->button_foreground != black && sp->button_foreground != white)
580 XFreeColors (si->dpy, cmap, &sp->button_foreground, 1, 0L);
581 if (sp->button_background != black && sp->button_background != white)
582 XFreeColors (si->dpy, cmap, &sp->button_background, 1, 0L);
583 if (sp->logo_foreground != black && sp->logo_foreground != white)
584 XFreeColors (si->dpy, cmap, &sp->logo_foreground, 1, 0L);
585 if (sp->logo_background != black && sp->logo_background != white)
586 XFreeColors (si->dpy, cmap, &sp->logo_background, 1, 0L);
587 if (sp->shadow_top != black && sp->shadow_top != white)
588 XFreeColors (si->dpy, cmap, &sp->shadow_top, 1, 0L);
589 if (sp->shadow_bottom != black && sp->shadow_bottom != white)
590 XFreeColors (si->dpy, cmap, &sp->shadow_bottom, 1, 0L);
592 memset (sp, 0, sizeof(*sp));
599 handle_splash_event (saver_info *si, XEvent *event)
601 splash_dialog_data *sp = si->sp_data;
605 switch (event->xany.type)
608 draw_splash_window (si);
611 case ButtonPress: case ButtonRelease:
613 if (event->xbutton.x >= sp->demo_button_x &&
614 event->xbutton.x < sp->demo_button_x + sp->button_width &&
615 event->xbutton.y >= sp->demo_button_y &&
616 event->xbutton.y < sp->demo_button_y + sp->button_height)
619 else if (event->xbutton.x >= sp->prefs_button_x &&
620 event->xbutton.x < sp->prefs_button_x + sp->button_width &&
621 event->xbutton.y >= sp->prefs_button_y &&
622 event->xbutton.y < sp->prefs_button_y + sp->button_height)
625 else if (event->xbutton.x >= sp->help_button_x &&
626 event->xbutton.x < sp->help_button_x + sp->button_width &&
627 event->xbutton.y >= sp->help_button_y &&
628 event->xbutton.y < sp->help_button_y + sp->button_height)
631 if (event->xany.type == ButtonPress)
634 update_splash_window (si);
636 XBell (si->dpy, False);
638 else if (event->xany.type == ButtonRelease)
640 if (which && sp->pressed == which)
642 destroy_splash_window (si);
645 case 1: do_demo (si); break;
646 case 2: do_prefs (si); break;
647 case 3: do_help (si); break;
652 update_splash_window (si);
662 unsplash_timer (XtPointer closure, XtIntervalId *id)
664 saver_info *si = (saver_info *) closure;
665 if (si && si->sp_data)
666 destroy_splash_window (si);
670 /* Button callbacks */
678 fork_and_exec (saver_info *si, const char *command, const char *desc)
680 saver_preferences *p = &si->prefs;
686 if (!command || !*command)
688 fprintf (stderr, "%s: no %s command has been specified.\n",
693 switch ((int) (forked = fork ()))
696 sprintf (buf, "%s: couldn't fork", blurb());
701 close (ConnectionNumber (si->dpy)); /* close display fd */
702 hack_subproc_environment (si->default_screen); /* set $DISPLAY */
704 av [ac++] = (char *) p->shell;
705 av [ac++] = (char *) "-c";
706 av [ac++] = (char *) command;
708 execvp (av[0], av); /* shouldn't return. */
710 sprintf (buf, "%s: execvp(\"%s\", \"%s\", \"%s\") failed",
711 blurb(), av[0], av[1], av[2]);
715 exit (1); /* Note that this only exits a child fork. */
726 do_demo (saver_info *si)
728 saver_preferences *p = &si->prefs;
729 fork_and_exec (si, p->demo_command, "demo-mode");
733 do_prefs (saver_info *si)
735 saver_preferences *p = &si->prefs;
736 fork_and_exec (si, p->prefs_command, "preferences");
740 do_help (saver_info *si)
742 saver_preferences *p = &si->prefs;
745 if (!p->help_url || !*p->help_url)
747 fprintf (stderr, "%s: no Help URL has been specified.\n", blurb());
751 help_command = (char *) malloc (strlen (p->load_url_command) +
752 (strlen (p->help_url) * 2) + 10);
753 sprintf (help_command, p->load_url_command, p->help_url, p->help_url);
754 fork_and_exec (si, help_command, "URL-loading");