1 /* xscreensaver, Copyright (c) 1993 Jamie Zawinski <jwz@lucid.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
12 /* This file contains some code for intelligently picking the best visual
13 (where "best" is somewhat biased in the direction of writable cells...)
22 #include <X11/Xutil.h>
25 # define isupper(c) ((c) >= 'A' && (c) <= 'Z')
28 # define _tolower(c) ((c) - 'A' + 'a')
31 extern char *progname;
32 extern char *get_string_resource ();
35 pick_best_visual_of_class (display, visual_class)
39 XVisualInfo vi_in, *vi_out;
42 vi_in.class = visual_class;
43 vi_in.screen = DefaultScreen (display);
44 vi_out = XGetVisualInfo (display, VisualClassMask|VisualScreenMask,
47 { /* choose the 'best' one, if multiple */
50 for (i = 0, best = 0; i < out_count; i++)
51 if (vi_out [i].depth > vi_out [best].depth)
53 visual = vi_out [best].visual;
54 XFree ((char *) vi_out);
62 pick_best_visual (display)
66 if (visual = pick_best_visual_of_class (display, PseudoColor))
68 if (visual = pick_best_visual_of_class (display, DirectColor))
70 if (visual = pick_best_visual_of_class (display, GrayScale))
72 if (visual = pick_best_visual_of_class (display, StaticGray))
74 return DefaultVisual (display, DefaultScreen (display));
79 id_to_visual (dpy, id)
83 XVisualInfo vi_in, *vi_out;
85 vi_in.screen = DefaultScreen (dpy);
87 vi_out = XGetVisualInfo (dpy, VisualScreenMask|VisualIDMask,
91 Visual *v = vi_out[0].visual;
92 XFree ((char *) vi_out);
99 get_visual_depth (dpy, visual)
103 XVisualInfo vi_in, *vi_out;
105 vi_in.screen = DefaultScreen (dpy);
106 vi_in.visualid = XVisualIDFromVisual (visual);
107 vi_out = XGetVisualInfo (dpy, VisualScreenMask|VisualIDMask,
109 if (! vi_out) abort ();
110 d = vi_out [0].depth;
111 XFree ((char *) vi_out);
117 get_visual_resource (dpy, name, class)
121 char c, *s = get_string_resource (name, class);
127 for (tmp = s; *tmp; tmp++)
128 if (isupper (*tmp)) *tmp = _tolower (*tmp);
130 if (!s || !strcmp (s, "best")) vclass = -1;
131 else if (!strcmp (s, "default")) vclass = -2;
132 else if (!strcmp (s, "staticgray")) vclass = StaticGray;
133 else if (!strcmp (s, "staticcolor")) vclass = StaticColor;
134 else if (!strcmp (s, "truecolor")) vclass = TrueColor;
135 else if (!strcmp (s, "grayscale")) vclass = GrayScale;
136 else if (!strcmp (s, "pseudocolor")) vclass = PseudoColor;
137 else if (!strcmp (s, "directcolor")) vclass = DirectColor;
138 else if (1 == sscanf (s, " %ld %c", &id, &c)) vclass = -3;
139 else if (1 == sscanf (s, " 0x%lx %c", &id, &c)) vclass = -3;
142 fprintf (stderr, "%s: unrecognized visual \"%s\".\n", progname, s);
148 return pick_best_visual (dpy);
149 else if (vclass == -2)
150 return DefaultVisual (dpy, DefaultScreen (dpy));
151 else if (vclass == -3)
153 Visual *v = id_to_visual (dpy, id);
155 fprintf (stderr, "%s: no visual with id 0x%x.\n", progname, id);
156 return pick_best_visual (dpy);
159 return pick_best_visual_of_class (dpy, vclass);
163 describe_visual (f, dpy, visual)
168 XVisualInfo vi_in, *vi_out;
170 vi_in.screen = DefaultScreen (dpy);
171 vi_in.visualid = XVisualIDFromVisual (visual);
172 vi_out = XGetVisualInfo (dpy, VisualScreenMask|VisualIDMask,
174 if (! vi_out) abort ();
175 fprintf (f, "0x%02x (%s depth: %2d, cmap: %3d @ %d)\n", vi_out->visualid,
176 (vi_out->class == StaticGray ? "StaticGray, " :
177 vi_out->class == StaticColor ? "StaticColor," :
178 vi_out->class == TrueColor ? "TrueColor, " :
179 vi_out->class == GrayScale ? "GrayScale, " :
180 vi_out->class == PseudoColor ? "PseudoColor," :
181 vi_out->class == DirectColor ? "DirectColor," :
183 vi_out->depth, vi_out->colormap_size, vi_out->bits_per_rgb);
184 XFree ((char *) vi_out);