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;
131 Dimension logo_width;
132 Dimension logo_height;
133 Dimension internal_border;
134 Dimension shadow_width;
136 Dimension button_width, button_height;
137 Dimension demo_button_x, demo_button_y;
138 Dimension prefs_button_x, prefs_button_y;
139 Dimension help_button_x, help_button_y;
143 unsigned long *logo_pixels;
150 make_splash_dialog (saver_info *si)
153 XSetWindowAttributes attrs;
154 unsigned long attrmask = 0;
155 splash_dialog_data *sp;
156 Screen *screen = si->default_screen->screen;
157 Colormap cmap = DefaultColormapOfScreen (screen);
162 if (!si->prefs.splash_p ||
163 si->prefs.splash_duration <= 0)
166 sp = (splash_dialog_data *) calloc (1, sizeof(*sp));
168 sp->heading_label = get_string_resource ("splash.heading.label",
169 "Dialog.Label.Label");
170 sp->body_label = get_string_resource ("splash.body.label",
171 "Dialog.Label.Label");
172 sp->body2_label = get_string_resource ("splash.body2.label",
173 "Dialog.Label.Label");
174 sp->demo_label = get_string_resource ("splash.demo.label",
175 "Dialog.Button.Label");
176 sp->prefs_label = get_string_resource ("splash.prefs.label",
177 "Dialog.Button.Label");
178 sp->help_label = get_string_resource ("splash.help.label",
179 "Dialog.Button.Label");
181 if (!sp->heading_label)
182 sp->heading_label = strdup("ERROR: REESOURCES NOT INSTALLED CORRECTLY");
184 sp->body_label = strdup("ERROR: REESOURCES NOT INSTALLED CORRECTLY");
185 if (!sp->body2_label)
186 sp->body2_label = strdup("ERROR: REESOURCES NOT INSTALLED CORRECTLY");
187 if (!sp->demo_label) sp->demo_label = strdup("ERROR");
188 if (!sp->prefs_label) sp->prefs_label = strdup("ERROR");
189 if (!sp->help_label) sp->help_label = strdup("ERROR");
191 /* Put the version number in the label. */
193 char *s = (char *) malloc (strlen(sp->heading_label) + 20);
194 sprintf(s, sp->heading_label, si->version);
195 free (sp->heading_label);
196 sp->heading_label = s;
199 f = get_string_resource ("splash.headingFont", "Dialog.Font");
200 sp->heading_font = XLoadQueryFont (si->dpy, (f ? f : "fixed"));
201 if (!sp->heading_font) sp->heading_font = XLoadQueryFont (si->dpy, "fixed");
204 f = get_string_resource("splash.bodyFont", "Dialog.Font");
205 sp->body_font = XLoadQueryFont (si->dpy, (f ? f : "fixed"));
206 if (!sp->body_font) sp->body_font = XLoadQueryFont (si->dpy, "fixed");
209 f = get_string_resource("splash.buttonFont", "Dialog.Font");
210 sp->button_font = XLoadQueryFont (si->dpy, (f ? f : "fixed"));
211 if (!sp->button_font) sp->button_font = XLoadQueryFont (si->dpy, "fixed");
214 sp->foreground = get_pixel_resource ("splash.foreground",
217 sp->background = get_pixel_resource ("splash.background",
221 if (sp->foreground == sp->background)
223 /* Make sure the error messages show up. */
224 sp->foreground = BlackPixelOfScreen (screen);
225 sp->background = WhitePixelOfScreen (screen);
228 sp->button_foreground = get_pixel_resource ("splash.Button.foreground",
229 "Dialog.Button.Foreground",
231 sp->button_background = get_pixel_resource ("splash.Button.background",
232 "Dialog.Button.Background",
234 sp->shadow_top = get_pixel_resource ("splash.topShadowColor",
237 sp->shadow_bottom = get_pixel_resource ("splash.bottomShadowColor",
241 sp->logo_width = get_integer_resource ("splash.logo.width",
242 "Dialog.Logo.Width");
243 sp->logo_height = get_integer_resource ("splash.logo.height",
244 "Dialog.Logo.Height");
245 sp->internal_border = get_integer_resource ("splash.internalBorderWidth",
246 "Dialog.InternalBorderWidth");
247 sp->shadow_width = get_integer_resource ("splash.shadowThickness",
248 "Dialog.ShadowThickness");
250 if (sp->logo_width == 0) sp->logo_width = 150;
251 if (sp->logo_height == 0) sp->logo_height = 150;
252 if (sp->internal_border == 0) sp->internal_border = 15;
253 if (sp->shadow_width == 0) sp->shadow_width = 4;
256 int direction, ascent, descent;
262 /* Measure the heading_label. */
263 XTextExtents (sp->heading_font,
264 sp->heading_label, strlen(sp->heading_label),
265 &direction, &ascent, &descent, &overall);
266 if (overall.width > sp->width) sp->width = overall.width;
267 sp->height += ascent + descent;
269 /* Measure the body_label. */
270 XTextExtents (sp->body_font,
271 sp->body_label, strlen(sp->body_label),
272 &direction, &ascent, &descent, &overall);
273 if (overall.width > sp->width) sp->width = overall.width;
274 sp->height += ascent + descent;
276 /* Measure the body2_label. */
277 XTextExtents (sp->body_font,
278 sp->body2_label, strlen(sp->body2_label),
279 &direction, &ascent, &descent, &overall);
280 if (overall.width > sp->width) sp->width = overall.width;
281 sp->height += ascent + descent;
284 Dimension w2 = 0, w3 = 0, w4 = 0;
285 Dimension h2 = 0, h3 = 0, h4 = 0;
287 /* Measure the Demo button. */
288 XTextExtents (sp->button_font,
289 sp->demo_label, strlen(sp->demo_label),
290 &direction, &ascent, &descent, &overall);
292 h2 = ascent + descent;
294 /* Measure the Prefs button. */
295 XTextExtents (sp->button_font,
296 sp->prefs_label, strlen(sp->prefs_label),
297 &direction, &ascent, &descent, &overall);
299 h3 = ascent + descent;
301 /* Measure the Help button. */
302 XTextExtents (sp->button_font,
303 sp->help_label, strlen(sp->help_label),
304 &direction, &ascent, &descent, &overall);
306 h4 = ascent + descent;
308 w2 = MAX(w2, w3); w2 = MAX(w2, w4);
309 h2 = MAX(h2, h3); h2 = MAX(h2, h4);
311 w2 += ((ascent + descent) / 2) + (sp->shadow_width * 2);
312 h2 += ((ascent + descent) / 2) + (sp->shadow_width * 2);
314 sp->button_width = w2;
315 sp->button_height = h2;
319 w2 += ((ascent + descent) * 2); /* for space between buttons */
321 if (w2 > sp->width) sp->width = w2;
325 sp->width += (sp->internal_border * 2);
326 sp->height += (sp->internal_border * 3);
328 if (sp->logo_height > sp->height)
329 sp->height = sp->logo_height;
330 else if (sp->height > sp->logo_height)
331 sp->logo_height = sp->height;
333 sp->logo_width = sp->logo_height;
335 sp->width += sp->logo_width;
338 attrmask |= CWOverrideRedirect; attrs.override_redirect = True;
339 attrmask |= CWEventMask;
340 attrs.event_mask = (ExposureMask | ButtonPressMask | ButtonReleaseMask);
344 get_screen_viewport (si->default_screen, &sx, &sy, &w, &h, False);
345 if (si->prefs.debug_p) w /= 2;
346 x = sx + (((w + sp->width) / 2) - sp->width);
347 y = sy + (((h + sp->height) / 2) - sp->height);
352 bw = get_integer_resource ("splash.borderWidth", "Dialog.BorderWidth");
355 XCreateWindow (si->dpy,
356 RootWindowOfScreen(screen),
357 x, y, sp->width, sp->height, bw,
358 DefaultDepthOfScreen (screen), InputOutput,
359 DefaultVisualOfScreen(screen),
361 XSetWindowBackground (si->dpy, si->splash_dialog, sp->background);
363 sp->logo_pixmap = xscreensaver_logo (si->dpy, si->splash_dialog, cmap,
365 &sp->logo_pixels, &sp->logo_npixels,
368 XMapRaised (si->dpy, si->splash_dialog);
369 XSync (si->dpy, False);
373 sp->timer = XtAppAddTimeOut (si->app, si->prefs.splash_duration,
374 unsplash_timer, (XtPointer) si);
376 draw_splash_window (si);
377 XSync (si->dpy, False);
382 draw_splash_window (saver_info *si)
384 splash_dialog_data *sp = si->sp_data;
387 int hspacing, vspacing, height;
388 int x1, x2, x3, y1, y2;
391 height = (sp->heading_font->ascent + sp->heading_font->descent +
392 sp->body_font->ascent + sp->body_font->descent +
393 sp->body_font->ascent + sp->body_font->descent +
394 sp->button_font->ascent + sp->button_font->descent);
395 vspacing = ((sp->height
396 - (4 * sp->shadow_width)
397 - (2 * sp->internal_border)
399 if (vspacing < 0) vspacing = 0;
400 if (vspacing > (sp->heading_font->ascent * 2))
401 vspacing = (sp->heading_font->ascent * 2);
403 gcv.foreground = sp->foreground;
404 gc1 = XCreateGC (si->dpy, si->splash_dialog, GCForeground, &gcv);
405 gc2 = XCreateGC (si->dpy, si->splash_dialog, GCForeground, &gcv);
407 x3 = sp->width - (sp->shadow_width * 2);
408 y1 = sp->internal_border;
412 XSetFont (si->dpy, gc1, sp->heading_font->fid);
413 sw = string_width (sp->heading_font, sp->heading_label);
414 x2 = (x1 + ((x3 - x1 - sw) / 2));
415 y1 += sp->heading_font->ascent;
416 XDrawString (si->dpy, si->splash_dialog, gc1, x2, y1,
417 sp->heading_label, strlen(sp->heading_label));
418 y1 += sp->heading_font->descent;
420 /* text below top heading
422 XSetFont (si->dpy, gc1, sp->body_font->fid);
423 y1 += vspacing + sp->body_font->ascent;
424 sw = string_width (sp->body_font, sp->body_label);
425 x2 = (x1 + ((x3 - x1 - sw) / 2));
426 XDrawString (si->dpy, si->splash_dialog, gc1, x2, y1,
427 sp->body_label, strlen(sp->body_label));
428 y1 += sp->body_font->descent;
430 y1 += sp->body_font->ascent;
431 sw = string_width (sp->body_font, sp->body2_label);
432 x2 = (x1 + ((x3 - x1 - sw) / 2));
433 XDrawString (si->dpy, si->splash_dialog, gc1, x2, y1,
434 sp->body2_label, strlen(sp->body2_label));
435 y1 += sp->body_font->descent;
439 XSetForeground (si->dpy, gc1, sp->button_foreground);
440 XSetForeground (si->dpy, gc2, sp->button_background);
442 /* y1 += (vspacing * 2);*/
443 y1 = sp->height - sp->internal_border - sp->button_height;
445 x1 += sp->internal_border;
446 y2 = (y1 + ((sp->button_height -
447 (sp->button_font->ascent + sp->button_font->descent))
449 + sp->button_font->ascent);
450 hspacing = ((sp->width - x1 - (sp->shadow_width * 2) -
451 sp->internal_border - (sp->button_width * 3))
454 x2 = x1 + ((sp->button_width - string_width(sp->button_font, sp->demo_label))
456 XFillRectangle (si->dpy, si->splash_dialog, gc2, x1, y1,
457 sp->button_width, sp->button_height);
458 XDrawString (si->dpy, si->splash_dialog, gc1, x2, y2,
459 sp->demo_label, strlen(sp->demo_label));
460 sp->demo_button_x = x1;
461 sp->demo_button_y = y1;
463 x1 += hspacing + sp->button_width;
464 x2 = x1 + ((sp->button_width - string_width(sp->button_font,sp->prefs_label))
466 XFillRectangle (si->dpy, si->splash_dialog, gc2, x1, y1,
467 sp->button_width, sp->button_height);
468 XDrawString (si->dpy, si->splash_dialog, gc1, x2, y2,
469 sp->prefs_label, strlen(sp->prefs_label));
470 sp->prefs_button_x = x1;
471 sp->prefs_button_y = y1;
473 x1 += hspacing + sp->button_width;
474 x2 = x1 + ((sp->button_width - string_width(sp->button_font,sp->help_label))
476 XFillRectangle (si->dpy, si->splash_dialog, gc2, x1, y1,
477 sp->button_width, sp->button_height);
478 XDrawString (si->dpy, si->splash_dialog, gc1, x2, y2,
479 sp->help_label, strlen(sp->help_label));
480 sp->help_button_x = x1;
481 sp->help_button_y = y1;
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);
495 unsigned int w, h, bw, d;
496 XGetGeometry (si->dpy, sp->logo_pixmap, &root, &x, &y, &w, &h, &bw, &d);
497 XSetForeground (si->dpy, gc1, sp->foreground);
498 XSetBackground (si->dpy, gc1, sp->background);
500 XCopyPlane (si->dpy, sp->logo_pixmap, si->splash_dialog, gc1,
502 x1 + ((x2 - (int)w) / 2),
503 y1 + ((y2 - (int)h) / 2),
506 XCopyArea (si->dpy, sp->logo_pixmap, si->splash_dialog, gc1,
508 x1 + ((x2 - (int)w) / 2),
509 y1 + ((y2 - (int)h) / 2));
512 /* Solid border inside the logo box. */
513 XSetForeground (si->dpy, gc1, sp->foreground);
514 XDrawRectangle (si->dpy, si->splash_dialog, gc1, x1, y1, x2-1, y2-1);
516 /* The shadow around the logo
518 draw_shaded_rectangle (si->dpy, si->splash_dialog,
519 sp->shadow_width * 2,
520 sp->shadow_width * 2,
521 sp->logo_width - (sp->shadow_width * 4),
522 sp->logo_height - (sp->shadow_width * 4),
524 sp->shadow_bottom, sp->shadow_top);
526 /* The shadow around the whole window
528 draw_shaded_rectangle (si->dpy, si->splash_dialog,
529 0, 0, sp->width, sp->height, sp->shadow_width,
530 sp->shadow_top, sp->shadow_bottom);
532 XFreeGC (si->dpy, gc1);
533 XFreeGC (si->dpy, gc2);
535 update_splash_window (si);
540 update_splash_window (saver_info *si)
542 splash_dialog_data *sp = si->sp_data;
545 pressed = sp->pressed;
547 /* The shadows around the buttons
549 draw_shaded_rectangle (si->dpy, si->splash_dialog,
550 sp->demo_button_x, sp->demo_button_y,
551 sp->button_width, sp->button_height, sp->shadow_width,
552 (pressed == 1 ? sp->shadow_bottom : sp->shadow_top),
553 (pressed == 1 ? sp->shadow_top : sp->shadow_bottom));
554 draw_shaded_rectangle (si->dpy, si->splash_dialog,
555 sp->prefs_button_x, sp->prefs_button_y,
556 sp->button_width, sp->button_height, sp->shadow_width,
557 (pressed == 2 ? sp->shadow_bottom : sp->shadow_top),
558 (pressed == 2 ? sp->shadow_top : sp->shadow_bottom));
559 draw_shaded_rectangle (si->dpy, si->splash_dialog,
560 sp->help_button_x, sp->help_button_y,
561 sp->button_width, sp->button_height, sp->shadow_width,
562 (pressed == 3 ? sp->shadow_bottom : sp->shadow_top),
563 (pressed == 3 ? sp->shadow_top : sp->shadow_bottom));
567 destroy_splash_window (saver_info *si)
569 splash_dialog_data *sp = si->sp_data;
570 Screen *screen = si->default_screen->screen;
571 Colormap cmap = DefaultColormapOfScreen (screen);
572 Pixel black = BlackPixelOfScreen (screen);
573 Pixel white = WhitePixelOfScreen (screen);
576 XtRemoveTimeOut (sp->timer);
578 if (si->splash_dialog)
580 XDestroyWindow (si->dpy, si->splash_dialog);
581 si->splash_dialog = 0;
584 if (sp->heading_label) free (sp->heading_label);
585 if (sp->body_label) free (sp->body_label);
586 if (sp->demo_label) free (sp->demo_label);
587 if (sp->prefs_label) free (sp->prefs_label);
588 if (sp->help_label) free (sp->help_label);
590 if (sp->heading_font) XFreeFont (si->dpy, sp->heading_font);
591 if (sp->body_font) XFreeFont (si->dpy, sp->body_font);
592 if (sp->button_font) XFreeFont (si->dpy, sp->button_font);
594 if (sp->foreground != black && sp->foreground != white)
595 XFreeColors (si->dpy, cmap, &sp->foreground, 1, 0L);
596 if (sp->background != black && sp->background != white)
597 XFreeColors (si->dpy, cmap, &sp->background, 1, 0L);
598 if (sp->button_foreground != black && sp->button_foreground != white)
599 XFreeColors (si->dpy, cmap, &sp->button_foreground, 1, 0L);
600 if (sp->button_background != black && sp->button_background != white)
601 XFreeColors (si->dpy, cmap, &sp->button_background, 1, 0L);
602 if (sp->shadow_top != black && sp->shadow_top != white)
603 XFreeColors (si->dpy, cmap, &sp->shadow_top, 1, 0L);
604 if (sp->shadow_bottom != black && sp->shadow_bottom != white)
605 XFreeColors (si->dpy, cmap, &sp->shadow_bottom, 1, 0L);
608 XFreePixmap (si->dpy, sp->logo_pixmap);
609 if (sp->logo_npixels && sp->logo_pixels)
610 XFreeColors (si->dpy, cmap, sp->logo_pixels, sp->logo_npixels, 0L);
612 free (sp->logo_pixels);
614 memset (sp, 0, sizeof(*sp));
621 handle_splash_event (saver_info *si, XEvent *event)
623 splash_dialog_data *sp = si->sp_data;
627 switch (event->xany.type)
630 draw_splash_window (si);
633 case ButtonPress: case ButtonRelease:
635 if (event->xbutton.x >= sp->demo_button_x &&
636 event->xbutton.x < sp->demo_button_x + sp->button_width &&
637 event->xbutton.y >= sp->demo_button_y &&
638 event->xbutton.y < sp->demo_button_y + sp->button_height)
641 else if (event->xbutton.x >= sp->prefs_button_x &&
642 event->xbutton.x < sp->prefs_button_x + sp->button_width &&
643 event->xbutton.y >= sp->prefs_button_y &&
644 event->xbutton.y < sp->prefs_button_y + sp->button_height)
647 else if (event->xbutton.x >= sp->help_button_x &&
648 event->xbutton.x < sp->help_button_x + sp->button_width &&
649 event->xbutton.y >= sp->help_button_y &&
650 event->xbutton.y < sp->help_button_y + sp->button_height)
653 if (event->xany.type == ButtonPress)
656 update_splash_window (si);
658 XBell (si->dpy, False);
660 else if (event->xany.type == ButtonRelease)
662 if (which && sp->pressed == which)
664 destroy_splash_window (si);
667 case 1: do_demo (si); break;
668 case 2: do_prefs (si); break;
669 case 3: do_help (si); break;
674 update_splash_window (si);
684 unsplash_timer (XtPointer closure, XtIntervalId *id)
686 saver_info *si = (saver_info *) closure;
687 if (si && si->sp_data)
688 destroy_splash_window (si);
692 /* Button callbacks */
700 fork_and_exec (saver_info *si, const char *command, const char *desc)
702 saver_preferences *p = &si->prefs;
708 if (!command || !*command)
710 fprintf (stderr, "%s: no %s command has been specified.\n",
715 switch ((int) (forked = fork ()))
718 sprintf (buf, "%s: couldn't fork", blurb());
723 close (ConnectionNumber (si->dpy)); /* close display fd */
724 hack_subproc_environment (si->default_screen); /* set $DISPLAY */
726 av [ac++] = (char *) p->shell;
727 av [ac++] = (char *) "-c";
728 av [ac++] = (char *) command;
730 execvp (av[0], av); /* shouldn't return. */
732 sprintf (buf, "%s: execvp(\"%s\", \"%s\", \"%s\") failed",
733 blurb(), av[0], av[1], av[2]);
737 exit (1); /* Note that this only exits a child fork. */
748 do_demo (saver_info *si)
750 saver_preferences *p = &si->prefs;
751 fork_and_exec (si, p->demo_command, "demo-mode");
755 do_prefs (saver_info *si)
757 saver_preferences *p = &si->prefs;
758 fork_and_exec (si, p->prefs_command, "preferences");
762 do_help (saver_info *si)
764 saver_preferences *p = &si->prefs;
767 if (!p->help_url || !*p->help_url)
769 fprintf (stderr, "%s: no Help URL has been specified.\n", blurb());
773 help_command = (char *) malloc (strlen (p->load_url_command) +
774 (strlen (p->help_url) * 2) + 10);
775 sprintf (help_command, p->load_url_command, p->help_url, p->help_url);
776 fork_and_exec (si, help_command, "URL-loading");