1 /* xscreensaver, Copyright (c) 1991-2001 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_p ||
161 si->prefs.splash_duration <= 0)
164 sp = (splash_dialog_data *) calloc (1, sizeof(*sp));
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");
179 if (!sp->heading_label)
180 sp->heading_label = strdup("ERROR: REESOURCES NOT INSTALLED CORRECTLY");
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");
189 /* Put the version number in the label. */
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;
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");
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");
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");
212 sp->foreground = get_pixel_resource ("splash.foreground",
215 sp->background = get_pixel_resource ("splash.background",
219 if (sp->foreground == sp->background)
221 /* Make sure the error messages show up. */
222 sp->foreground = BlackPixelOfScreen (screen);
223 sp->background = WhitePixelOfScreen (screen);
226 sp->button_foreground = get_pixel_resource ("splash.Button.foreground",
227 "Dialog.Button.Foreground",
229 sp->button_background = get_pixel_resource ("splash.Button.background",
230 "Dialog.Button.Background",
232 sp->logo_foreground = get_pixel_resource ("splash.logo.foreground",
233 "Dialog.Logo.Foreground",
235 sp->logo_background = get_pixel_resource ("splash.logo.background",
236 "Dialog.Logo.Background",
238 sp->shadow_top = get_pixel_resource ("splash.topShadowColor",
241 sp->shadow_bottom = get_pixel_resource ("splash.bottomShadowColor",
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");
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;
260 int direction, ascent, descent;
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;
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;
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;
288 Dimension w2 = 0, w3 = 0, w4 = 0;
289 Dimension h2 = 0, h3 = 0, h4 = 0;
291 /* Measure the Demo button. */
292 XTextExtents (sp->button_font,
293 sp->demo_label, strlen(sp->demo_label),
294 &direction, &ascent, &descent, &overall);
296 h2 = ascent + descent;
298 /* Measure the Prefs button. */
299 XTextExtents (sp->button_font,
300 sp->prefs_label, strlen(sp->prefs_label),
301 &direction, &ascent, &descent, &overall);
303 h3 = ascent + descent;
305 /* Measure the Help button. */
306 XTextExtents (sp->button_font,
307 sp->help_label, strlen(sp->help_label),
308 &direction, &ascent, &descent, &overall);
310 h4 = ascent + descent;
312 w2 = MAX(w2, w3); w2 = MAX(w2, w4);
313 h2 = MAX(h2, h3); h2 = MAX(h2, h4);
315 w2 += ((ascent + descent) / 2) + (sp->shadow_width * 2);
316 h2 += ((ascent + descent) / 2) + (sp->shadow_width * 2);
318 sp->button_width = w2;
319 sp->button_height = h2;
323 w2 += ((ascent + descent) * 2); /* for space between buttons */
325 if (w2 > sp->width) sp->width = w2;
329 sp->width += (sp->internal_border * 2);
330 sp->height += (sp->internal_border * 3);
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;
337 sp->logo_width = sp->logo_height;
339 sp->width += sp->logo_width;
342 attrmask |= CWOverrideRedirect; attrs.override_redirect = True;
343 attrmask |= CWEventMask;
344 attrs.event_mask = (ExposureMask | ButtonPressMask | ButtonReleaseMask);
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);
356 bw = get_integer_resource ("splash.borderWidth", "Dialog.BorderWidth");
359 XCreateWindow (si->dpy,
360 RootWindowOfScreen(screen),
361 x, y, sp->width, sp->height, bw,
362 DefaultDepthOfScreen (screen), InputOutput,
363 DefaultVisualOfScreen(screen),
365 XSetWindowBackground (si->dpy, si->splash_dialog, sp->background);
367 XMapRaised (si->dpy, si->splash_dialog);
368 XSync (si->dpy, False);
372 sp->timer = XtAppAddTimeOut (si->app, si->prefs.splash_duration,
373 unsplash_timer, (XtPointer) si);
375 draw_splash_window (si);
376 XSync (si->dpy, False);
380 draw_logo (saver_info *si, Window win, int x, int y, int w, int h,
383 Colormap cmap = DefaultColormapOfScreen (si->default_screen->screen);
384 Pixmap logo_map = XCreatePixmap (si->dpy, win, w, h,
385 si->default_screen->current_depth);
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);
396 draw_splash_window (saver_info *si)
398 splash_dialog_data *sp = si->sp_data;
401 int hspacing, vspacing, height;
402 int x1, x2, x3, y1, y2;
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)
413 if (vspacing < 0) vspacing = 0;
414 if (vspacing > (sp->heading_font->ascent * 2))
415 vspacing = (sp->heading_font->ascent * 2);
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);
421 x3 = sp->width - (sp->shadow_width * 2);
422 y1 = sp->internal_border;
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;
434 /* text below top heading
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;
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;
453 XSetForeground (si->dpy, gc1, sp->button_foreground);
454 XSetForeground (si->dpy, gc2, sp->button_background);
456 /* y1 += (vspacing * 2);*/
457 y1 = sp->height - sp->internal_border - sp->button_height;
459 x1 += sp->internal_border;
460 y2 = (y1 + ((sp->button_height -
461 (sp->button_font->ascent + sp->button_font->descent))
463 + sp->button_font->ascent);
464 hspacing = ((sp->width - x1 - (sp->shadow_width * 2) -
465 sp->internal_border - (sp->button_width * 3))
468 x2 = x1 + ((sp->button_width - string_width(sp->button_font, sp->demo_label))
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;
477 x1 += hspacing + sp->button_width;
478 x2 = x1 + ((sp->button_width - string_width(sp->button_font,sp->prefs_label))
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;
487 x1 += hspacing + sp->button_width;
488 x2 = x1 + ((sp->button_width - string_width(sp->button_font,sp->help_label))
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;
500 XSetForeground (si->dpy, gc1, sp->logo_foreground);
501 XSetForeground (si->dpy, gc2, sp->logo_background);
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);
508 draw_logo (si, si->splash_dialog, x1, y1, x2, y2, True);
511 /* The shadow around the logo
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),
519 sp->shadow_bottom, sp->shadow_top);
521 /* The shadow around the whole window
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);
527 XFreeGC (si->dpy, gc1);
528 XFreeGC (si->dpy, gc2);
530 update_splash_window (si);
535 update_splash_window (saver_info *si)
537 splash_dialog_data *sp = si->sp_data;
540 pressed = sp->pressed;
542 /* The shadows around the buttons
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));
562 destroy_splash_window (saver_info *si)
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);
571 XtRemoveTimeOut (sp->timer);
573 if (si->splash_dialog)
575 XDestroyWindow (si->dpy, si->splash_dialog);
576 si->splash_dialog = 0;
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);
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);
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);
606 memset (sp, 0, sizeof(*sp));
613 handle_splash_event (saver_info *si, XEvent *event)
615 splash_dialog_data *sp = si->sp_data;
619 switch (event->xany.type)
622 draw_splash_window (si);
625 case ButtonPress: case ButtonRelease:
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)
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)
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)
645 if (event->xany.type == ButtonPress)
648 update_splash_window (si);
650 XBell (si->dpy, False);
652 else if (event->xany.type == ButtonRelease)
654 if (which && sp->pressed == which)
656 destroy_splash_window (si);
659 case 1: do_demo (si); break;
660 case 2: do_prefs (si); break;
661 case 3: do_help (si); break;
666 update_splash_window (si);
676 unsplash_timer (XtPointer closure, XtIntervalId *id)
678 saver_info *si = (saver_info *) closure;
679 if (si && si->sp_data)
680 destroy_splash_window (si);
684 /* Button callbacks */
692 fork_and_exec (saver_info *si, const char *command, const char *desc)
694 saver_preferences *p = &si->prefs;
700 if (!command || !*command)
702 fprintf (stderr, "%s: no %s command has been specified.\n",
707 switch ((int) (forked = fork ()))
710 sprintf (buf, "%s: couldn't fork", blurb());
715 close (ConnectionNumber (si->dpy)); /* close display fd */
716 hack_subproc_environment (si->default_screen); /* set $DISPLAY */
718 av [ac++] = (char *) p->shell;
719 av [ac++] = (char *) "-c";
720 av [ac++] = (char *) command;
722 execvp (av[0], av); /* shouldn't return. */
724 sprintf (buf, "%s: execvp(\"%s\", \"%s\", \"%s\") failed",
725 blurb(), av[0], av[1], av[2]);
729 exit (1); /* Note that this only exits a child fork. */
740 do_demo (saver_info *si)
742 saver_preferences *p = &si->prefs;
743 fork_and_exec (si, p->demo_command, "demo-mode");
747 do_prefs (saver_info *si)
749 saver_preferences *p = &si->prefs;
750 fork_and_exec (si, p->prefs_command, "preferences");
754 do_help (saver_info *si)
756 saver_preferences *p = &si->prefs;
759 if (!p->help_url || !*p->help_url)
761 fprintf (stderr, "%s: no Help URL has been specified.\n", blurb());
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");