1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
28 #include <X11/Xutil.h>
30 #include "gdkvisual.h"
31 #include "gdkprivate-x11.h"
32 #include "gdkinternals.h"
34 struct _GdkVisualClass
36 GObjectClass parent_class;
39 static void gdk_visual_add (GdkVisual *visual);
40 static void gdk_visual_decompose_mask (gulong mask,
43 static guint gdk_visual_hash (Visual *key);
44 static gboolean gdk_visual_equal (Visual *a,
48 static GdkVisualPrivate *system_visual;
49 static GdkVisualPrivate **visuals;
52 static gint available_depths[7];
53 static gint navailable_depths;
55 static GdkVisualType available_types[6];
56 static gint navailable_types;
60 static const gchar* visual_names[] =
70 #endif /* G_ENABLE_DEBUG */
72 static GHashTable *visual_hash = NULL;
75 gdk_visual_finalize (GObject *object)
77 g_error ("A GdkVisual object was finalized. This should not happen");
81 gdk_visual_class_init (GObjectClass *class)
83 class->finalize = gdk_visual_finalize;
88 gdk_visual_get_type (void)
90 static GType object_type = 0;
94 static const GTypeInfo object_info =
96 sizeof (GdkVisualClass),
98 (GBaseFinalizeFunc) NULL,
99 (GClassInitFunc) gdk_visual_class_init,
100 NULL, /* class_finalize */
101 NULL, /* class_data */
102 sizeof (GdkVisualPrivate),
104 (GInstanceInitFunc) NULL,
107 object_type = g_type_register_static (G_TYPE_OBJECT,
117 _gdk_visual_init (void)
119 static const gint possible_depths[7] = { 32, 24, 16, 15, 8, 4, 1 };
120 static const GdkVisualType possible_types[6] =
122 GDK_VISUAL_DIRECT_COLOR,
123 GDK_VISUAL_TRUE_COLOR,
124 GDK_VISUAL_PSEUDO_COLOR,
125 GDK_VISUAL_STATIC_COLOR,
126 GDK_VISUAL_GRAYSCALE,
127 GDK_VISUAL_STATIC_GRAY
130 static const gint npossible_depths = sizeof(possible_depths)/sizeof(gint);
131 static const gint npossible_types = sizeof(possible_types)/sizeof(GdkVisualType);
133 XVisualInfo *visual_list;
134 XVisualInfo visual_template;
135 GdkVisualPrivate *temp_visual;
136 Visual *default_xvisual;
140 visual_template.screen = _gdk_screen;
141 visual_list = XGetVisualInfo (gdk_display, VisualScreenMask, &visual_template, &nxvisuals);
143 visuals = g_new (GdkVisualPrivate *, nxvisuals);
144 for (i = 0; i < nxvisuals; i++)
145 visuals[i] = g_object_new (GDK_TYPE_VISUAL, NULL);
147 default_xvisual = DefaultVisual (gdk_display, _gdk_screen);
150 for (i = 0; i < nxvisuals; i++)
152 if (visual_list[i].depth >= 1)
155 switch (visual_list[i].c_class)
156 #else /* __cplusplus */
157 switch (visual_list[i].class)
158 #endif /* __cplusplus */
161 visuals[nvisuals]->visual.type = GDK_VISUAL_STATIC_GRAY;
164 visuals[nvisuals]->visual.type = GDK_VISUAL_GRAYSCALE;
167 visuals[nvisuals]->visual.type = GDK_VISUAL_STATIC_COLOR;
170 visuals[nvisuals]->visual.type = GDK_VISUAL_PSEUDO_COLOR;
173 visuals[nvisuals]->visual.type = GDK_VISUAL_TRUE_COLOR;
176 visuals[nvisuals]->visual.type = GDK_VISUAL_DIRECT_COLOR;
180 visuals[nvisuals]->visual.depth = visual_list[i].depth;
181 visuals[nvisuals]->visual.byte_order =
182 (ImageByteOrder(gdk_display) == LSBFirst) ?
183 GDK_LSB_FIRST : GDK_MSB_FIRST;
184 visuals[nvisuals]->visual.red_mask = visual_list[i].red_mask;
185 visuals[nvisuals]->visual.green_mask = visual_list[i].green_mask;
186 visuals[nvisuals]->visual.blue_mask = visual_list[i].blue_mask;
187 visuals[nvisuals]->visual.colormap_size = visual_list[i].colormap_size;
188 visuals[nvisuals]->visual.bits_per_rgb = visual_list[i].bits_per_rgb;
189 visuals[nvisuals]->xvisual = visual_list[i].visual;
191 if ((visuals[nvisuals]->visual.type == GDK_VISUAL_TRUE_COLOR) ||
192 (visuals[nvisuals]->visual.type == GDK_VISUAL_DIRECT_COLOR))
194 gdk_visual_decompose_mask (visuals[nvisuals]->visual.red_mask,
195 &visuals[nvisuals]->visual.red_shift,
196 &visuals[nvisuals]->visual.red_prec);
198 gdk_visual_decompose_mask (visuals[nvisuals]->visual.green_mask,
199 &visuals[nvisuals]->visual.green_shift,
200 &visuals[nvisuals]->visual.green_prec);
202 gdk_visual_decompose_mask (visuals[nvisuals]->visual.blue_mask,
203 &visuals[nvisuals]->visual.blue_shift,
204 &visuals[nvisuals]->visual.blue_prec);
208 visuals[nvisuals]->visual.red_mask = 0;
209 visuals[nvisuals]->visual.red_shift = 0;
210 visuals[nvisuals]->visual.red_prec = 0;
212 visuals[nvisuals]->visual.green_mask = 0;
213 visuals[nvisuals]->visual.green_shift = 0;
214 visuals[nvisuals]->visual.green_prec = 0;
216 visuals[nvisuals]->visual.blue_mask = 0;
217 visuals[nvisuals]->visual.blue_shift = 0;
218 visuals[nvisuals]->visual.blue_prec = 0;
228 for (i = 0; i < nvisuals; i++)
230 for (j = i+1; j < nvisuals; j++)
232 if (visuals[j]->visual.depth >= visuals[i]->visual.depth)
234 if ((visuals[j]->visual.depth == 8) && (visuals[i]->visual.depth == 8))
236 if (visuals[j]->visual.type == GDK_VISUAL_PSEUDO_COLOR)
238 temp_visual = visuals[j];
239 visuals[j] = visuals[i];
240 visuals[i] = temp_visual;
242 else if ((visuals[i]->visual.type != GDK_VISUAL_PSEUDO_COLOR) &&
243 visuals[j]->visual.type > visuals[i]->visual.type)
245 temp_visual = visuals[j];
246 visuals[j] = visuals[i];
247 visuals[i] = temp_visual;
250 else if ((visuals[j]->visual.depth > visuals[i]->visual.depth) ||
251 ((visuals[j]->visual.depth == visuals[i]->visual.depth) &&
252 (visuals[j]->visual.type > visuals[i]->visual.type)))
254 temp_visual = visuals[j];
255 visuals[j] = visuals[i];
256 visuals[i] = temp_visual;
262 for (i = 0; i < nvisuals; i++)
263 if (default_xvisual->visualid == visuals[i]->xvisual->visualid)
265 system_visual = visuals[i];
269 #ifdef G_ENABLE_DEBUG
270 if (_gdk_debug_flags & GDK_DEBUG_MISC)
271 for (i = 0; i < nvisuals; i++)
272 g_message ("visual: %s: %d",
273 visual_names[visuals[i]->visual.type],
274 visuals[i]->visual.depth);
275 #endif /* G_ENABLE_DEBUG */
277 navailable_depths = 0;
278 for (i = 0; i < npossible_depths; i++)
280 for (j = 0; j < nvisuals; j++)
282 if (visuals[j]->visual.depth == possible_depths[i])
284 available_depths[navailable_depths++] = visuals[j]->visual.depth;
290 if (navailable_depths == 0)
291 g_error ("unable to find a usable depth");
293 navailable_types = 0;
294 for (i = 0; i < npossible_types; i++)
296 for (j = 0; j < nvisuals; j++)
298 if (visuals[j]->visual.type == possible_types[i])
300 available_types[navailable_types++] = visuals[j]->visual.type;
306 for (i = 0; i < nvisuals; i++)
307 gdk_visual_add ((GdkVisual*) visuals[i]);
309 if (npossible_types == 0)
310 g_error ("unable to find a usable visual type");
314 * gdk_visual_get_best_depth:
316 * Get the best available depth for the default GDK display. "Best"
317 * means "largest," i.e. 32 preferred over 24 preferred over 8 bits
320 * Return value: best available depth
323 gdk_visual_get_best_depth (void)
325 return available_depths[0];
329 * gdk_visual_get_best_type:
331 * Return the best available visual type (the one with the most
332 * colors) for the default GDK display.
334 * Return value: best visual type
337 gdk_visual_get_best_type (void)
339 return available_types[0];
343 * gdk_visual_get_system:
345 * Get the default or system visual for the default GDK display.
346 * This is the visual for the root window of the display.
347 * The return value should not be freed.
349 * Return value: system visual
352 gdk_visual_get_system (void)
354 return ((GdkVisual*) system_visual);
358 * gdk_visual_get_best:
360 * Get the visual with the most available colors for the default
361 * GDK display. The return value should not be freed.
363 * Return value: best visual
366 gdk_visual_get_best (void)
368 return ((GdkVisual*) visuals[0]);
372 * gdk_visual_get_best_with_depth:
373 * @depth: a bit depth
375 * Get the best visual with depth @depth for the default GDK display.
376 * Color visuals and visuals with mutable colormaps are preferred
377 * over grayscale or fixed-colormap visuals. The return value should not
378 * be freed. %NULL may be returned if no visual supports @depth.
380 * Return value: best visual for the given depth
383 gdk_visual_get_best_with_depth (gint depth)
385 GdkVisual *return_val;
389 for (i = 0; i < nvisuals; i++)
390 if (depth == visuals[i]->visual.depth)
392 return_val = (GdkVisual*) visuals[i];
400 * gdk_visual_get_best_with_type:
401 * @visual_type: a visual type
403 * Get the best visual of the given @visual_type for the default GDK display.
404 * Visuals with higher color depths are considered better. The return value
405 * should not be freed. %NULL may be returned if no visual has type
408 * Return value: best visual of the given type
411 gdk_visual_get_best_with_type (GdkVisualType visual_type)
413 GdkVisual *return_val;
417 for (i = 0; i < nvisuals; i++)
418 if (visual_type == visuals[i]->visual.type)
420 return_val = (GdkVisual*) visuals[i];
428 * gdk_visual_get_best_with_both:
429 * @depth: a bit depth
430 * @visual_type: a visual type
432 * Combines gdk_visual_get_best_with_depth() and gdk_visual_get_best_with_type().
434 * Return value: best visual with both @depth and @visual_type, or %NULL if none
437 gdk_visual_get_best_with_both (gint depth,
438 GdkVisualType visual_type)
440 GdkVisual *return_val;
444 for (i = 0; i < nvisuals; i++)
445 if ((depth == visuals[i]->visual.depth) &&
446 (visual_type == visuals[i]->visual.type))
448 return_val = (GdkVisual*) visuals[i];
457 * @depths: return location for available depths
458 * @count: return location for number of available depths
460 * This function returns the available bit depths for the default
461 * display. It's equivalent to listing the visuals
462 * (gdk_list_visuals()) and then looking at the depth field in each
463 * visual, removing duplicates.
465 * The array returned by this function should not be freed.
469 gdk_query_depths (gint **depths,
472 *count = navailable_depths;
473 *depths = available_depths;
477 * gdk_query_visual_types:
478 * @visual_types: return location for the available visual types
479 * @count: return location for the number of available visual types
481 * This function returns the available visual types for the default
482 * display. It's equivalent to listing the visuals
483 * (gdk_list_visuals()) and then looking at the type field in each
484 * visual, removing duplicates.
486 * The array returned by this function should not be freed.
490 gdk_query_visual_types (GdkVisualType **visual_types,
493 *count = navailable_types;
494 *visual_types = available_types;
500 * Lists the available visuals for the default display.
501 * A visual describes a hardware image data format.
502 * For example, a visual might support 24-bit color, or 8-bit color,
503 * and might expect pixels to be in a certain format.
505 * Call g_list_free() on the return value when you're finished with it.
507 * Return value: a list of visuals; the list must be freed, but not its contents
510 gdk_list_visuals (void)
516 for (i = 0; i < nvisuals; ++i)
517 list = g_list_append (list, (gpointer) visuals[i]);
524 gdk_visual_lookup (Visual *xvisual)
531 visual = g_hash_table_lookup (visual_hash, xvisual);
536 gdkx_visual_get (VisualID xvisualid)
540 for (i = 0; i < nvisuals; i++)
541 if (xvisualid == visuals[i]->xvisual->visualid)
542 return (GdkVisual*) visuals[i];
549 gdk_visual_add (GdkVisual *visual)
551 GdkVisualPrivate *private;
554 visual_hash = g_hash_table_new ((GHashFunc) gdk_visual_hash,
555 (GEqualFunc) gdk_visual_equal);
557 private = (GdkVisualPrivate*) visual;
559 g_hash_table_insert (visual_hash, private->xvisual, visual);
563 gdk_visual_decompose_mask (gulong mask,
570 while (!(mask & 0x1))
584 gdk_visual_hash (Visual *key)
586 return key->visualid;
590 gdk_visual_equal (Visual *a,
593 return (a->visualid == b->visualid);
597 gdk_x11_visual_get_xvisual (GdkVisual *visual)
599 g_return_val_if_fail (visual != NULL, NULL);
601 return ((GdkVisualPrivate*) visual)->xvisual;