]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkvisual-x11.c
32d44dfb78466645f9955870741a4fb76634c18e
[~andy/gtk] / gdk / x11 / gdkvisual-x11.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #include <X11/Xlib.h>
28 #include <X11/Xutil.h>
29
30 #include "gdkvisual.h"
31 #include "gdkprivate-x11.h"
32 #include "gdkinternals.h"
33
34 struct _GdkVisualClass
35 {
36   GObjectClass parent_class;
37 };
38
39 static void     gdk_visual_add            (GdkVisual *visual);
40 static void     gdk_visual_decompose_mask (gulong     mask,
41                                            gint      *shift,
42                                            gint      *prec);
43 static guint    gdk_visual_hash           (Visual    *key);
44 static gboolean gdk_visual_equal          (Visual    *a,
45                                            Visual    *b);
46
47
48 static GdkVisualPrivate *system_visual;
49 static GdkVisualPrivate **visuals;
50 static gint nvisuals;
51
52 static gint available_depths[7];
53 static gint navailable_depths;
54
55 static GdkVisualType available_types[6];
56 static gint navailable_types;
57
58 #ifdef G_ENABLE_DEBUG
59
60 static const gchar* visual_names[] =
61 {
62   "static gray",
63   "grayscale",
64   "static color",
65   "pseudo color",
66   "true color",
67   "direct color",
68 };
69
70 #endif /* G_ENABLE_DEBUG */
71
72 static GHashTable *visual_hash = NULL;
73
74 static void
75 gdk_visual_finalize (GObject *object)
76 {
77   g_error ("A GdkVisual object was finalized. This should not happen");
78 }
79
80 static void
81 gdk_visual_class_init (GObjectClass *class)
82 {
83   class->finalize = gdk_visual_finalize;
84 }
85
86
87 GType
88 gdk_visual_get_type (void)
89 {
90   static GType object_type = 0;
91
92   if (!object_type)
93     {
94       static const GTypeInfo object_info =
95       {
96         sizeof (GdkVisualClass),
97         (GBaseInitFunc) NULL,
98         (GBaseFinalizeFunc) NULL,
99         (GClassInitFunc) gdk_visual_class_init,
100         NULL,           /* class_finalize */
101         NULL,           /* class_data */
102         sizeof (GdkVisualPrivate),
103         0,              /* n_preallocs */
104         (GInstanceInitFunc) NULL,
105       };
106       
107       object_type = g_type_register_static (G_TYPE_OBJECT,
108                                             "GdkVisual",
109                                             &object_info, 0);
110     }
111   
112   return object_type;
113 }
114
115
116 void
117 _gdk_visual_init (void)
118 {
119   static const gint possible_depths[7] = { 32, 24, 16, 15, 8, 4, 1 };
120   static const GdkVisualType possible_types[6] =
121     {
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
128     };
129
130   static const gint npossible_depths = sizeof(possible_depths)/sizeof(gint);
131   static const gint npossible_types = sizeof(possible_types)/sizeof(GdkVisualType);
132
133   XVisualInfo *visual_list;
134   XVisualInfo visual_template;
135   GdkVisualPrivate *temp_visual;
136   Visual *default_xvisual;
137   int nxvisuals;
138   int i, j;
139
140   visual_template.screen = _gdk_screen;
141   visual_list = XGetVisualInfo (gdk_display, VisualScreenMask, &visual_template, &nxvisuals);
142   
143   visuals = g_new (GdkVisualPrivate *, nxvisuals);
144   for (i = 0; i < nxvisuals; i++)
145     visuals[i] = g_object_new (GDK_TYPE_VISUAL, NULL);
146
147   default_xvisual = DefaultVisual (gdk_display, _gdk_screen);
148
149   nvisuals = 0;
150   for (i = 0; i < nxvisuals; i++)
151     {
152       if (visual_list[i].depth >= 1)
153         {
154 #ifdef __cplusplus
155           switch (visual_list[i].c_class)
156 #else /* __cplusplus */
157           switch (visual_list[i].class)
158 #endif /* __cplusplus */
159             {
160             case StaticGray:
161               visuals[nvisuals]->visual.type = GDK_VISUAL_STATIC_GRAY;
162               break;
163             case GrayScale:
164               visuals[nvisuals]->visual.type = GDK_VISUAL_GRAYSCALE;
165               break;
166             case StaticColor:
167               visuals[nvisuals]->visual.type = GDK_VISUAL_STATIC_COLOR;
168               break;
169             case PseudoColor:
170               visuals[nvisuals]->visual.type = GDK_VISUAL_PSEUDO_COLOR;
171               break;
172             case TrueColor:
173               visuals[nvisuals]->visual.type = GDK_VISUAL_TRUE_COLOR;
174               break;
175             case DirectColor:
176               visuals[nvisuals]->visual.type = GDK_VISUAL_DIRECT_COLOR;
177               break;
178             }
179
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;
190
191           if ((visuals[nvisuals]->visual.type == GDK_VISUAL_TRUE_COLOR) ||
192               (visuals[nvisuals]->visual.type == GDK_VISUAL_DIRECT_COLOR))
193             {
194               gdk_visual_decompose_mask (visuals[nvisuals]->visual.red_mask,
195                                          &visuals[nvisuals]->visual.red_shift,
196                                          &visuals[nvisuals]->visual.red_prec);
197
198               gdk_visual_decompose_mask (visuals[nvisuals]->visual.green_mask,
199                                          &visuals[nvisuals]->visual.green_shift,
200                                          &visuals[nvisuals]->visual.green_prec);
201
202               gdk_visual_decompose_mask (visuals[nvisuals]->visual.blue_mask,
203                                          &visuals[nvisuals]->visual.blue_shift,
204                                          &visuals[nvisuals]->visual.blue_prec);
205             }
206           else
207             {
208               visuals[nvisuals]->visual.red_mask = 0;
209               visuals[nvisuals]->visual.red_shift = 0;
210               visuals[nvisuals]->visual.red_prec = 0;
211
212               visuals[nvisuals]->visual.green_mask = 0;
213               visuals[nvisuals]->visual.green_shift = 0;
214               visuals[nvisuals]->visual.green_prec = 0;
215
216               visuals[nvisuals]->visual.blue_mask = 0;
217               visuals[nvisuals]->visual.blue_shift = 0;
218               visuals[nvisuals]->visual.blue_prec = 0;
219             }
220
221           nvisuals += 1;
222         }
223     }
224
225   if (visual_list)
226     XFree (visual_list);
227
228   for (i = 0; i < nvisuals; i++)
229     {
230       for (j = i+1; j < nvisuals; j++)
231         {
232           if (visuals[j]->visual.depth >= visuals[i]->visual.depth)
233             {
234               if ((visuals[j]->visual.depth == 8) && (visuals[i]->visual.depth == 8))
235                 {
236                   if (visuals[j]->visual.type == GDK_VISUAL_PSEUDO_COLOR)
237                     {
238                       temp_visual = visuals[j];
239                       visuals[j] = visuals[i];
240                       visuals[i] = temp_visual;
241                     }
242                   else if ((visuals[i]->visual.type != GDK_VISUAL_PSEUDO_COLOR) &&
243                            visuals[j]->visual.type > visuals[i]->visual.type)
244                     {
245                       temp_visual = visuals[j];
246                       visuals[j] = visuals[i];
247                       visuals[i] = temp_visual;
248                     }
249                 }
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)))
253                 {
254                   temp_visual = visuals[j];
255                   visuals[j] = visuals[i];
256                   visuals[i] = temp_visual;
257                 }
258             }
259         }
260     }
261
262   for (i = 0; i < nvisuals; i++)
263     if (default_xvisual->visualid == visuals[i]->xvisual->visualid)
264       {
265         system_visual = visuals[i];
266         break;
267       }
268
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 */
276
277   navailable_depths = 0;
278   for (i = 0; i < npossible_depths; i++)
279     {
280       for (j = 0; j < nvisuals; j++)
281         {
282           if (visuals[j]->visual.depth == possible_depths[i])
283             {
284               available_depths[navailable_depths++] = visuals[j]->visual.depth;
285               break;
286             }
287         }
288     }
289
290   if (navailable_depths == 0)
291     g_error ("unable to find a usable depth");
292
293   navailable_types = 0;
294   for (i = 0; i < npossible_types; i++)
295     {
296       for (j = 0; j < nvisuals; j++)
297         {
298           if (visuals[j]->visual.type == possible_types[i])
299             {
300               available_types[navailable_types++] = visuals[j]->visual.type;
301               break;
302             }
303         }
304     }
305
306   for (i = 0; i < nvisuals; i++)
307     gdk_visual_add ((GdkVisual*) visuals[i]);
308
309   if (npossible_types == 0)
310     g_error ("unable to find a usable visual type");
311 }
312
313 /**
314  * gdk_visual_get_best_depth:
315  * 
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
318  * per pixel.
319  * 
320  * Return value: best available depth
321  **/
322 gint
323 gdk_visual_get_best_depth (void)
324 {
325   return available_depths[0];
326 }
327
328 /**
329  * gdk_visual_get_best_type:
330  * 
331  * Return the best available visual type (the one with the most
332  * colors) for the default GDK display.
333  * 
334  * Return value: best visual type
335  **/
336 GdkVisualType
337 gdk_visual_get_best_type (void)
338 {
339   return available_types[0];
340 }
341
342 /**
343  * gdk_visual_get_system:
344  * 
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.
348  * 
349  * Return value: system visual
350  **/
351 GdkVisual*
352 gdk_visual_get_system (void)
353 {
354   return ((GdkVisual*) system_visual);
355 }
356
357 /**
358  * gdk_visual_get_best:
359  *
360  * Get the visual with the most available colors for the default
361  * GDK display. The return value should not be freed.
362  * 
363  * Return value: best visual
364  **/
365 GdkVisual*
366 gdk_visual_get_best (void)
367 {
368   return ((GdkVisual*) visuals[0]);
369 }
370
371 /**
372  * gdk_visual_get_best_with_depth:
373  * @depth: a bit depth
374  * 
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.
379  * 
380  * Return value: best visual for the given depth
381  **/
382 GdkVisual*
383 gdk_visual_get_best_with_depth (gint depth)
384 {
385   GdkVisual *return_val;
386   int i;
387
388   return_val = NULL;
389   for (i = 0; i < nvisuals; i++)
390     if (depth == visuals[i]->visual.depth)
391       {
392         return_val = (GdkVisual*) visuals[i];
393         break;
394       }
395
396   return return_val;
397 }
398
399 /**
400  * gdk_visual_get_best_with_type:
401  * @visual_type: a visual type
402  *
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
406  * @visual_type.
407  * 
408  * Return value: best visual of the given type
409  **/
410 GdkVisual*
411 gdk_visual_get_best_with_type (GdkVisualType visual_type)
412 {
413   GdkVisual *return_val;
414   int i;
415
416   return_val = NULL;
417   for (i = 0; i < nvisuals; i++)
418     if (visual_type == visuals[i]->visual.type)
419       {
420         return_val = (GdkVisual*) visuals[i];
421         break;
422       }
423
424   return return_val;
425 }
426
427 /**
428  * gdk_visual_get_best_with_both:
429  * @depth: a bit depth
430  * @visual_type: a visual type
431  *
432  * Combines gdk_visual_get_best_with_depth() and gdk_visual_get_best_with_type().
433  * 
434  * Return value: best visual with both @depth and @visual_type, or %NULL if none
435  **/
436 GdkVisual*
437 gdk_visual_get_best_with_both (gint          depth,
438                                GdkVisualType visual_type)
439 {
440   GdkVisual *return_val;
441   int i;
442
443   return_val = NULL;
444   for (i = 0; i < nvisuals; i++)
445     if ((depth == visuals[i]->visual.depth) &&
446         (visual_type == visuals[i]->visual.type))
447       {
448         return_val = (GdkVisual*) visuals[i];
449         break;
450       }
451
452   return return_val;
453 }
454
455 /**
456  * gdk_query_depths:
457  * @depths: return location for available depths 
458  * @count: return location for number of available depths
459  *
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.
464  * 
465  * The array returned by this function should not be freed.
466  * 
467  **/
468 void
469 gdk_query_depths  (gint **depths,
470                    gint  *count)
471 {
472   *count = navailable_depths;
473   *depths = available_depths;
474 }
475
476 /**
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
480  *
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.
485  * 
486  * The array returned by this function should not be freed.
487  * 
488  **/
489 void
490 gdk_query_visual_types (GdkVisualType **visual_types,
491                         gint           *count)
492 {
493   *count = navailable_types;
494   *visual_types = available_types;
495 }
496
497 /**
498  * gdk_list_visuals:
499  * 
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.
504  *
505  * Call g_list_free() on the return value when you're finished with it.
506  * 
507  * Return value: a list of visuals; the list must be freed, but not its contents
508  **/
509 GList*
510 gdk_list_visuals (void)
511 {
512   GList *list;
513   guint i;
514
515   list = NULL;
516   for (i = 0; i < nvisuals; ++i)
517     list = g_list_append (list, (gpointer) visuals[i]);
518
519   return list;
520 }
521
522
523 GdkVisual*
524 gdk_visual_lookup (Visual *xvisual)
525 {
526   GdkVisual *visual;
527
528   if (!visual_hash)
529     return NULL;
530
531   visual = g_hash_table_lookup (visual_hash, xvisual);
532   return visual;
533 }
534
535 GdkVisual*
536 gdkx_visual_get (VisualID xvisualid)
537 {
538   int i;
539
540   for (i = 0; i < nvisuals; i++)
541     if (xvisualid == visuals[i]->xvisual->visualid)
542       return (GdkVisual*) visuals[i];
543
544   return NULL;
545 }
546
547
548 static void
549 gdk_visual_add (GdkVisual *visual)
550 {
551   GdkVisualPrivate *private;
552
553   if (!visual_hash)
554     visual_hash = g_hash_table_new ((GHashFunc) gdk_visual_hash,
555                                     (GEqualFunc) gdk_visual_equal);
556
557   private = (GdkVisualPrivate*) visual;
558
559   g_hash_table_insert (visual_hash, private->xvisual, visual);
560 }
561
562 static void
563 gdk_visual_decompose_mask (gulong  mask,
564                            gint   *shift,
565                            gint   *prec)
566 {
567   *shift = 0;
568   *prec = 0;
569
570   while (!(mask & 0x1))
571     {
572       (*shift)++;
573       mask >>= 1;
574     }
575
576   while (mask & 0x1)
577     {
578       (*prec)++;
579       mask >>= 1;
580     }
581 }
582
583 static guint
584 gdk_visual_hash (Visual *key)
585 {
586   return key->visualid;
587 }
588
589 static gboolean
590 gdk_visual_equal (Visual *a,
591                   Visual *b)
592 {
593   return (a->visualid == b->visualid);
594 }
595
596 Visual *
597 gdk_x11_visual_get_xvisual (GdkVisual *visual)
598 {
599   g_return_val_if_fail (visual != NULL, NULL);
600
601   return  ((GdkVisualPrivate*) visual)->xvisual;
602 }