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 "gdkscreen-x11.h"
33 #include "gdkinternals.h"
35 struct _GdkVisualClass
37 GObjectClass parent_class;
40 static void gdk_visual_add (GdkVisual *visual);
41 static void gdk_visual_decompose_mask (gulong mask,
44 static guint gdk_visual_hash (Visual *key);
45 static gboolean gdk_visual_equal (Visual *a,
51 static const gchar* visual_names[] =
61 #endif /* G_ENABLE_DEBUG */
64 gdk_visual_finalize (GObject *object)
66 g_error ("A GdkVisual object was finalized. This should not happen");
70 gdk_visual_class_init (GObjectClass *class)
72 class->finalize = gdk_visual_finalize;
77 gdk_visual_get_type (void)
79 static GType object_type = 0;
83 static const GTypeInfo object_info =
85 sizeof (GdkVisualClass),
87 (GBaseFinalizeFunc) NULL,
88 (GClassInitFunc) gdk_visual_class_init,
89 NULL, /* class_finalize */
90 NULL, /* class_data */
91 sizeof (GdkVisualPrivate),
93 (GInstanceInitFunc) NULL,
96 object_type = g_type_register_static (G_TYPE_OBJECT,
106 _gdk_visual_init (GdkScreen *screen)
108 static const gint possible_depths[7] = { 32, 24, 16, 15, 8, 4, 1 };
109 static const GdkVisualType possible_types[6] =
111 GDK_VISUAL_DIRECT_COLOR,
112 GDK_VISUAL_TRUE_COLOR,
113 GDK_VISUAL_PSEUDO_COLOR,
114 GDK_VISUAL_STATIC_COLOR,
115 GDK_VISUAL_GRAYSCALE,
116 GDK_VISUAL_STATIC_GRAY
119 GdkScreenX11 *screen_x11;
120 XVisualInfo *visual_list;
121 XVisualInfo visual_template;
122 GdkVisualPrivate *temp_visual;
123 Visual *default_xvisual;
124 GdkVisualPrivate **visuals;
129 g_return_if_fail (GDK_IS_SCREEN (screen));
130 screen_x11 = GDK_SCREEN_X11 (screen);
132 visual_template.screen = screen_x11->screen_num;
133 visual_list = XGetVisualInfo (screen_x11->xdisplay, VisualScreenMask, &visual_template, &nxvisuals);
135 visuals = g_new (GdkVisualPrivate *, nxvisuals);
136 for (i = 0; i < nxvisuals; i++)
137 visuals[i] = g_object_new (GDK_TYPE_VISUAL, NULL);
139 default_xvisual = DefaultVisual (screen_x11->xdisplay, screen_x11->screen_num);
142 for (i = 0; i < nxvisuals; i++)
144 visuals[nvisuals]->screen = screen;
146 if (visual_list[i].depth >= 1)
149 switch (visual_list[i].c_class)
150 #else /* __cplusplus */
151 switch (visual_list[i].class)
152 #endif /* __cplusplus */
155 visuals[nvisuals]->visual.type = GDK_VISUAL_STATIC_GRAY;
158 visuals[nvisuals]->visual.type = GDK_VISUAL_GRAYSCALE;
161 visuals[nvisuals]->visual.type = GDK_VISUAL_STATIC_COLOR;
164 visuals[nvisuals]->visual.type = GDK_VISUAL_PSEUDO_COLOR;
167 visuals[nvisuals]->visual.type = GDK_VISUAL_TRUE_COLOR;
170 visuals[nvisuals]->visual.type = GDK_VISUAL_DIRECT_COLOR;
174 visuals[nvisuals]->visual.depth = visual_list[i].depth;
175 visuals[nvisuals]->visual.byte_order =
176 (ImageByteOrder(screen_x11->xdisplay) == LSBFirst) ?
177 GDK_LSB_FIRST : GDK_MSB_FIRST;
178 visuals[nvisuals]->visual.red_mask = visual_list[i].red_mask;
179 visuals[nvisuals]->visual.green_mask = visual_list[i].green_mask;
180 visuals[nvisuals]->visual.blue_mask = visual_list[i].blue_mask;
181 visuals[nvisuals]->visual.colormap_size = visual_list[i].colormap_size;
182 visuals[nvisuals]->visual.bits_per_rgb = visual_list[i].bits_per_rgb;
183 visuals[nvisuals]->xvisual = visual_list[i].visual;
185 if ((visuals[nvisuals]->visual.type == GDK_VISUAL_TRUE_COLOR) ||
186 (visuals[nvisuals]->visual.type == GDK_VISUAL_DIRECT_COLOR))
188 gdk_visual_decompose_mask (visuals[nvisuals]->visual.red_mask,
189 &visuals[nvisuals]->visual.red_shift,
190 &visuals[nvisuals]->visual.red_prec);
192 gdk_visual_decompose_mask (visuals[nvisuals]->visual.green_mask,
193 &visuals[nvisuals]->visual.green_shift,
194 &visuals[nvisuals]->visual.green_prec);
196 gdk_visual_decompose_mask (visuals[nvisuals]->visual.blue_mask,
197 &visuals[nvisuals]->visual.blue_shift,
198 &visuals[nvisuals]->visual.blue_prec);
202 visuals[nvisuals]->visual.red_mask = 0;
203 visuals[nvisuals]->visual.red_shift = 0;
204 visuals[nvisuals]->visual.red_prec = 0;
206 visuals[nvisuals]->visual.green_mask = 0;
207 visuals[nvisuals]->visual.green_shift = 0;
208 visuals[nvisuals]->visual.green_prec = 0;
210 visuals[nvisuals]->visual.blue_mask = 0;
211 visuals[nvisuals]->visual.blue_shift = 0;
212 visuals[nvisuals]->visual.blue_prec = 0;
222 for (i = 0; i < nvisuals; i++)
224 for (j = i+1; j < nvisuals; j++)
226 if (visuals[j]->visual.depth >= visuals[i]->visual.depth)
228 if ((visuals[j]->visual.depth == 8) && (visuals[i]->visual.depth == 8))
230 if (visuals[j]->visual.type == GDK_VISUAL_PSEUDO_COLOR)
232 temp_visual = visuals[j];
233 visuals[j] = visuals[i];
234 visuals[i] = temp_visual;
236 else if ((visuals[i]->visual.type != GDK_VISUAL_PSEUDO_COLOR) &&
237 visuals[j]->visual.type > visuals[i]->visual.type)
239 temp_visual = visuals[j];
240 visuals[j] = visuals[i];
241 visuals[i] = temp_visual;
244 else if ((visuals[j]->visual.depth > visuals[i]->visual.depth) ||
245 ((visuals[j]->visual.depth == visuals[i]->visual.depth) &&
246 (visuals[j]->visual.type > visuals[i]->visual.type)))
248 temp_visual = visuals[j];
249 visuals[j] = visuals[i];
250 visuals[i] = temp_visual;
256 for (i = 0; i < nvisuals; i++)
257 if (default_xvisual->visualid == visuals[i]->xvisual->visualid)
259 screen_x11->system_visual = visuals[i];
263 #ifdef G_ENABLE_DEBUG
264 if (_gdk_debug_flags & GDK_DEBUG_MISC)
265 for (i = 0; i < nvisuals; i++)
266 g_message ("visual: %s: %d",
267 visual_names[visuals[i]->visual.type],
268 visuals[i]->visual.depth);
269 #endif /* G_ENABLE_DEBUG */
271 screen_x11->navailable_depths = 0;
272 for (i = 0; i < G_N_ELEMENTS (possible_depths); i++)
274 for (j = 0; j < nvisuals; j++)
276 if (visuals[j]->visual.depth == possible_depths[i])
278 screen_x11->available_depths[screen_x11->navailable_depths++] = visuals[j]->visual.depth;
284 if (screen_x11->navailable_depths == 0)
285 g_error ("unable to find a usable depth");
287 screen_x11->navailable_types = 0;
288 for (i = 0; i < G_N_ELEMENTS (possible_types); i++)
290 for (j = 0; j < nvisuals; j++)
292 if (visuals[j]->visual.type == possible_types[i])
294 screen_x11->available_types[screen_x11->navailable_types++] = visuals[j]->visual.type;
300 for (i = 0; i < nvisuals; i++)
301 gdk_visual_add ((GdkVisual*) visuals[i]);
303 if (screen_x11->navailable_types == 0)
304 g_error ("unable to find a usable visual type");
306 screen_x11->visuals = visuals;
307 screen_x11->nvisuals = nvisuals;
311 * gdk_visual_get_best_depth:
313 * Get the best available depth for the default GDK screen. "Best"
314 * means "largest," i.e. 32 preferred over 24 preferred over 8 bits
317 * Return value: best available depth
320 gdk_visual_get_best_depth (void)
322 GdkScreen *screen = gdk_screen_get_default();
324 return GDK_SCREEN_X11 (screen)->available_depths[0];
328 * gdk_visual_get_best_type:
330 * Return the best available visual type for the default GDK screen.
332 * Return value: best visual type
335 gdk_visual_get_best_type (void)
337 GdkScreen *screen = gdk_screen_get_default();
339 return GDK_SCREEN_X11 (screen)->available_types[0];
343 * gdk_screen_get_system_visual:
344 * @screen : a #GdkScreen.
346 * Get the system's default visual for @screen.
347 * This is the visual for the root window of the display.
348 * The return value should not be freed.
350 * Return value: system visual
353 gdk_screen_get_system_visual (GdkScreen * screen)
355 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
357 return ((GdkVisual *) GDK_SCREEN_X11 (screen)->system_visual);
361 * gdk_visual_get_best:
363 * Get the visual with the most available colors for the default
364 * GDK screen. The return value should not be freed.
366 * Return value: best visual
369 gdk_visual_get_best (void)
371 GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (gdk_screen_get_default());
373 return (GdkVisual *)screen_x11->visuals[0];
377 * gdk_visual_get_best_with_depth:
378 * @depth: a bit depth
380 * Get the best visual with depth @depth for the default GDK screen.
381 * Color visuals and visuals with mutable colormaps are preferred
382 * over grayscale or fixed-colormap visuals. The return value should not
383 * be freed. %NULL may be returned if no visual supports @depth.
385 * Return value: best visual for the given depth
388 gdk_visual_get_best_with_depth (gint depth)
390 GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (gdk_screen_get_default ());
391 GdkVisual *return_val;
395 for (i = 0; i < screen_x11->nvisuals; i++)
396 if (depth == screen_x11->visuals[i]->visual.depth)
398 return_val = (GdkVisual *) & (screen_x11->visuals[i]);
406 * gdk_visual_get_best_with_type:
407 * @visual_type: a visual type
409 * Get the best visual of the given @visual_type for the default GDK screen.
410 * Visuals with higher color depths are considered better. The return value
411 * should not be freed. %NULL may be returned if no visual has type
414 * Return value: best visual of the given type
417 gdk_visual_get_best_with_type (GdkVisualType visual_type)
419 GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (gdk_screen_get_default ());
420 GdkVisual *return_val;
424 for (i = 0; i < screen_x11->nvisuals; i++)
425 if (visual_type == screen_x11->visuals[i]->visual.type)
427 return_val = (GdkVisual *) screen_x11->visuals[i];
435 * gdk_visual_get_best_with_both:
436 * @depth: a bit depth
437 * @visual_type: a visual type
439 * Combines gdk_visual_get_best_with_depth() and gdk_visual_get_best_with_type().
441 * Return value: best visual with both @depth and @visual_type, or %NULL if none
444 gdk_visual_get_best_with_both (gint depth,
445 GdkVisualType visual_type)
447 GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (gdk_screen_get_default ());
448 GdkVisual *return_val;
452 for (i = 0; i < screen_x11->nvisuals; i++)
453 if ((depth == screen_x11->visuals[i]->visual.depth) &&
454 (visual_type == screen_x11->visuals[i]->visual.type))
456 return_val = (GdkVisual *) screen_x11->visuals[i];
465 * @depths: return location for available depths
466 * @count: return location for number of available depths
468 * This function returns the available bit depths for the default
469 * screen. It's equivalent to listing the visuals
470 * (gdk_list_visuals()) and then looking at the depth field in each
471 * visual, removing duplicates.
473 * The array returned by this function should not be freed.
477 gdk_query_depths (gint **depths,
480 GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (gdk_screen_get_default ());
482 *count = screen_x11->navailable_depths;
483 *depths = screen_x11->available_depths;
487 * gdk_query_visual_types:
488 * @visual_types: return location for the available visual types
489 * @count: return location for the number of available visual types
491 * This function returns the available visual types for the default
492 * screen. It's equivalent to listing the visuals
493 * (gdk_list_visuals()) and then looking at the type field in each
494 * visual, removing duplicates.
496 * The array returned by this function should not be freed.
499 gdk_query_visual_types (GdkVisualType **visual_types,
502 GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (gdk_screen_get_default ());
504 *count = screen_x11->navailable_types;
505 *visual_types = screen_x11->available_types;
509 * gdk_screen_list_visuals:
510 * @screen : the relevant #GdkScreen.
512 * Lists the available visuals for the specified @screen.
513 * A visual describes a hardware image data format.
514 * For example, a visual might support 24-bit color, or 8-bit color,
515 * and might expect pixels to be in a certain format.
517 * Call g_list_free() on the return value when you're finished with it.
519 * Return value: a list of visuals; the list must be freed, but not its contents
522 gdk_screen_list_visuals (GdkScreen *screen)
525 GdkScreenX11 *screen_x11;
528 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
529 screen_x11 = GDK_SCREEN_X11 (screen);
533 for (i = 0; i < screen_x11->nvisuals; ++i)
534 list = g_list_append (list, screen_x11->visuals[i]);
540 * gdk_x11_screen_lookup_visual
541 * @screen: a #GdkScreen.
542 * @xvisualid: an X Visual ID.
544 * Looks up the #GdkVisual for a particular screen and X Visual ID.
546 * Returns: the #GdkVisual (owned by the screen object), or %NULL
547 * if the visual ID wasn't found.
550 gdk_x11_screen_lookup_visual (GdkScreen *screen,
554 GdkScreenX11 *screen_x11;
555 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
556 screen_x11 = GDK_SCREEN_X11 (screen);
558 for (i = 0; i < screen_x11->nvisuals; i++)
559 if (xvisualid == screen_x11->visuals[i]->xvisual->visualid)
560 return (GdkVisual *) screen_x11->visuals[i];
567 * @xvisualid: a X visual id.
569 * Returns a #GdkVisual corresponding to a X visual.
571 * Return value: the #GdkVisual.
574 gdkx_visual_get (VisualID xvisualid)
576 return gdk_x11_screen_lookup_visual (gdk_screen_get_default (), xvisualid);
580 gdk_visual_add (GdkVisual *visual)
582 GdkVisualPrivate *private = (GdkVisualPrivate *) visual;
583 GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (private->screen);
585 if (!screen_x11->visual_hash)
586 screen_x11->visual_hash = g_hash_table_new ((GHashFunc) gdk_visual_hash,
587 (GEqualFunc) gdk_visual_equal);
589 g_hash_table_insert (screen_x11->visual_hash, private->xvisual, visual);
593 gdk_visual_decompose_mask (gulong mask,
600 while (!(mask & 0x1))
614 gdk_visual_hash (Visual *key)
616 return key->visualid;
620 gdk_visual_equal (Visual *a,
623 return (a->visualid == b->visualid);
627 * gdk_x11_visual_get_xvisual:
628 * @visual: a #GdkVisual.
630 * Returns the X visual belonging to a #GdkVisual.
632 * Return value: an Xlib <type>Visual*</type>.
635 gdk_x11_visual_get_xvisual (GdkVisual *visual)
637 g_return_val_if_fail (visual != NULL, NULL);
639 return ((GdkVisualPrivate*) visual)->xvisual;
643 * gdk_visual_get_screen:
644 * @visual: a #GdkVisual
646 * Gets the screen to which this visual belongs
648 * Return value: the screen to which this visual belongs.
651 gdk_visual_get_screen (GdkVisual *visual)
653 g_return_val_if_fail (GDK_IS_VISUAL (visual), NULL);
655 return ((GdkVisualPrivate*) visual)->screen;