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 "config.h"
28 #include <X11/Xlib.h>
29 #include <X11/Xutil.h>
30
31 #include "gdkx.h"
32 #include "gdkvisual.h"
33 #include "gdkprivate-x11.h"
34 #include "gdkscreen-x11.h"
35 #include "gdkinternals.h"
36 #include "gdkalias.h"
37
38 struct _GdkVisualClass
39 {
40 GObjectClass parent_class;
41 };
42
43 static void gdk_visual_add (GdkVisual *visual);
44 static void gdk_visual_decompose_mask (gulong mask,
45 gint *shift,
46 gint *prec);
47 static guint gdk_visual_hash (Visual *key);
48 static gboolean gdk_visual_equal (Visual *a,
49 Visual *b);
50
51
52 #ifdef G_ENABLE_DEBUG
53
54 static const gchar *const visual_names[] =
55 {
56 "static gray",
57 "grayscale",
58 "static color",
59 "pseudo color",
60 "true color",
61 "direct color",
62 };
63
64 #endif /* G_ENABLE_DEBUG */
65
66 GType
gdk_visual_get_type(void)67 gdk_visual_get_type (void)
68 {
69 static GType object_type = 0;
70
71 if (!object_type)
72 {
73 const GTypeInfo object_info =
74 {
75 sizeof (GdkVisualClass),
76 (GBaseInitFunc) NULL,
77 (GBaseFinalizeFunc) NULL,
78 (GClassInitFunc) NULL,
79 NULL, /* class_finalize */
80 NULL, /* class_data */
81 sizeof (GdkVisualPrivate),
82 0, /* n_preallocs */
83 (GInstanceInitFunc) NULL,
84 };
85
86 object_type = g_type_register_static (G_TYPE_OBJECT,
87 g_intern_static_string ("GdkVisual"),
88 &object_info, 0);
89 }
90
91 return object_type;
92 }
93
94
95 void
_gdk_visual_init(GdkScreen * screen)96 _gdk_visual_init (GdkScreen *screen)
97 {
98 static const gint possible_depths[8] = { 32, 30, 24, 16, 15, 8, 4, 1 };
99 static const GdkVisualType possible_types[6] =
100 {
101 GDK_VISUAL_DIRECT_COLOR,
102 GDK_VISUAL_TRUE_COLOR,
103 GDK_VISUAL_PSEUDO_COLOR,
104 GDK_VISUAL_STATIC_COLOR,
105 GDK_VISUAL_GRAYSCALE,
106 GDK_VISUAL_STATIC_GRAY
107 };
108
109 GdkScreenX11 *screen_x11;
110 XVisualInfo *visual_list;
111 XVisualInfo visual_template;
112 GdkVisualPrivate *temp_visual;
113 Visual *default_xvisual;
114 GdkVisualPrivate **visuals;
115 int nxvisuals;
116 int nvisuals;
117 int i, j;
118
119 g_return_if_fail (GDK_IS_SCREEN (screen));
120 screen_x11 = GDK_SCREEN_X11 (screen);
121
122 nxvisuals = 0;
123 visual_template.screen = screen_x11->screen_num;
124 visual_list = XGetVisualInfo (screen_x11->xdisplay, VisualScreenMask, &visual_template, &nxvisuals);
125
126 visuals = g_new (GdkVisualPrivate *, nxvisuals);
127 for (i = 0; i < nxvisuals; i++)
128 visuals[i] = g_object_new (GDK_TYPE_VISUAL, NULL);
129
130 default_xvisual = DefaultVisual (screen_x11->xdisplay, screen_x11->screen_num);
131
132 nvisuals = 0;
133 for (i = 0; i < nxvisuals; i++)
134 {
135 visuals[nvisuals]->screen = screen;
136
137 if (visual_list[i].depth >= 1)
138 {
139 #ifdef __cplusplus
140 switch (visual_list[i].c_class)
141 #else /* __cplusplus */
142 switch (visual_list[i].class)
143 #endif /* __cplusplus */
144 {
145 case StaticGray:
146 visuals[nvisuals]->visual.type = GDK_VISUAL_STATIC_GRAY;
147 break;
148 case GrayScale:
149 visuals[nvisuals]->visual.type = GDK_VISUAL_GRAYSCALE;
150 break;
151 case StaticColor:
152 visuals[nvisuals]->visual.type = GDK_VISUAL_STATIC_COLOR;
153 break;
154 case PseudoColor:
155 visuals[nvisuals]->visual.type = GDK_VISUAL_PSEUDO_COLOR;
156 break;
157 case TrueColor:
158 visuals[nvisuals]->visual.type = GDK_VISUAL_TRUE_COLOR;
159 break;
160 case DirectColor:
161 visuals[nvisuals]->visual.type = GDK_VISUAL_DIRECT_COLOR;
162 break;
163 }
164
165 visuals[nvisuals]->visual.depth = visual_list[i].depth;
166 visuals[nvisuals]->visual.byte_order =
167 (ImageByteOrder(screen_x11->xdisplay) == LSBFirst) ?
168 GDK_LSB_FIRST : GDK_MSB_FIRST;
169 visuals[nvisuals]->visual.red_mask = visual_list[i].red_mask;
170 visuals[nvisuals]->visual.green_mask = visual_list[i].green_mask;
171 visuals[nvisuals]->visual.blue_mask = visual_list[i].blue_mask;
172 visuals[nvisuals]->visual.colormap_size = visual_list[i].colormap_size;
173 visuals[nvisuals]->visual.bits_per_rgb = visual_list[i].bits_per_rgb;
174 visuals[nvisuals]->xvisual = visual_list[i].visual;
175
176 if ((visuals[nvisuals]->visual.type == GDK_VISUAL_TRUE_COLOR) ||
177 (visuals[nvisuals]->visual.type == GDK_VISUAL_DIRECT_COLOR))
178 {
179 gdk_visual_decompose_mask (visuals[nvisuals]->visual.red_mask,
180 &visuals[nvisuals]->visual.red_shift,
181 &visuals[nvisuals]->visual.red_prec);
182
183 gdk_visual_decompose_mask (visuals[nvisuals]->visual.green_mask,
184 &visuals[nvisuals]->visual.green_shift,
185 &visuals[nvisuals]->visual.green_prec);
186
187 gdk_visual_decompose_mask (visuals[nvisuals]->visual.blue_mask,
188 &visuals[nvisuals]->visual.blue_shift,
189 &visuals[nvisuals]->visual.blue_prec);
190 }
191 else
192 {
193 visuals[nvisuals]->visual.red_mask = 0;
194 visuals[nvisuals]->visual.red_shift = 0;
195 visuals[nvisuals]->visual.red_prec = 0;
196
197 visuals[nvisuals]->visual.green_mask = 0;
198 visuals[nvisuals]->visual.green_shift = 0;
199 visuals[nvisuals]->visual.green_prec = 0;
200
201 visuals[nvisuals]->visual.blue_mask = 0;
202 visuals[nvisuals]->visual.blue_shift = 0;
203 visuals[nvisuals]->visual.blue_prec = 0;
204 }
205
206 nvisuals += 1;
207 }
208 }
209
210 if (visual_list)
211 XFree (visual_list);
212
213 for (i = 0; i < nvisuals; i++)
214 {
215 for (j = i+1; j < nvisuals; j++)
216 {
217 if (visuals[j]->visual.depth >= visuals[i]->visual.depth)
218 {
219 if ((visuals[j]->visual.depth == 8) && (visuals[i]->visual.depth == 8))
220 {
221 if (visuals[j]->visual.type == GDK_VISUAL_PSEUDO_COLOR)
222 {
223 temp_visual = visuals[j];
224 visuals[j] = visuals[i];
225 visuals[i] = temp_visual;
226 }
227 else if ((visuals[i]->visual.type != GDK_VISUAL_PSEUDO_COLOR) &&
228 visuals[j]->visual.type > visuals[i]->visual.type)
229 {
230 temp_visual = visuals[j];
231 visuals[j] = visuals[i];
232 visuals[i] = temp_visual;
233 }
234 }
235 else if ((visuals[j]->visual.depth > visuals[i]->visual.depth) ||
236 ((visuals[j]->visual.depth == visuals[i]->visual.depth) &&
237 (visuals[j]->visual.type > visuals[i]->visual.type)))
238 {
239 temp_visual = visuals[j];
240 visuals[j] = visuals[i];
241 visuals[i] = temp_visual;
242 }
243 }
244 }
245 }
246
247 for (i = 0; i < nvisuals; i++)
248 {
249 if (default_xvisual->visualid == visuals[i]->xvisual->visualid)
250 screen_x11->system_visual = visuals[i];
251
252 /* For now, we only support 8888 ARGB for the "rgba visual".
253 * Additional formats (like ABGR) could be added later if they
254 * turn up.
255 */
256 if (visuals[i]->visual.depth == 32 &&
257 (visuals[i]->visual.red_mask == 0xff0000 &&
258 visuals[i]->visual.green_mask == 0x00ff00 &&
259 visuals[i]->visual.blue_mask == 0x0000ff))
260 {
261 screen_x11->rgba_visual = GDK_VISUAL (visuals[i]);
262 }
263 }
264
265 #ifdef G_ENABLE_DEBUG
266 if (_gdk_debug_flags & GDK_DEBUG_MISC)
267 for (i = 0; i < nvisuals; i++)
268 g_message ("visual: %s: %d",
269 visual_names[visuals[i]->visual.type],
270 visuals[i]->visual.depth);
271 #endif /* G_ENABLE_DEBUG */
272
273 screen_x11->navailable_depths = 0;
274 for (i = 0; i < G_N_ELEMENTS (possible_depths); i++)
275 {
276 for (j = 0; j < nvisuals; j++)
277 {
278 if (visuals[j]->visual.depth == possible_depths[i])
279 {
280 screen_x11->available_depths[screen_x11->navailable_depths++] = visuals[j]->visual.depth;
281 break;
282 }
283 }
284 }
285
286 if (screen_x11->navailable_depths == 0)
287 g_error ("unable to find a usable depth");
288
289 screen_x11->navailable_types = 0;
290 for (i = 0; i < G_N_ELEMENTS (possible_types); i++)
291 {
292 for (j = 0; j < nvisuals; j++)
293 {
294 if (visuals[j]->visual.type == possible_types[i])
295 {
296 screen_x11->available_types[screen_x11->navailable_types++] = visuals[j]->visual.type;
297 break;
298 }
299 }
300 }
301
302 for (i = 0; i < nvisuals; i++)
303 gdk_visual_add ((GdkVisual*) visuals[i]);
304
305 if (screen_x11->navailable_types == 0)
306 g_error ("unable to find a usable visual type");
307
308 screen_x11->visuals = visuals;
309 screen_x11->nvisuals = nvisuals;
310 }
311
312 /**
313 * gdk_visual_get_best_depth:
314 *
315 * Get the best available depth for the default GDK screen. "Best"
316 * means "largest," i.e. 32 preferred over 24 preferred over 8 bits
317 * per pixel.
318 *
319 * Return value: best available depth
320 **/
321 gint
gdk_visual_get_best_depth(void)322 gdk_visual_get_best_depth (void)
323 {
324 GdkScreen *screen = gdk_screen_get_default();
325
326 return GDK_SCREEN_X11 (screen)->available_depths[0];
327 }
328
329 /**
330 * gdk_visual_get_best_type:
331 *
332 * Return the best available visual type for the default GDK screen.
333 *
334 * Return value: best visual type
335 **/
336 GdkVisualType
gdk_visual_get_best_type(void)337 gdk_visual_get_best_type (void)
338 {
339 GdkScreen *screen = gdk_screen_get_default();
340
341 return GDK_SCREEN_X11 (screen)->available_types[0];
342 }
343
344 /**
345 * gdk_screen_get_system_visual:
346 * @screen: a #GdkScreen.
347 *
348 * Get the system's default visual for @screen.
349 * This is the visual for the root window of the display.
350 * The return value should not be freed.
351 *
352 * Return value: (transfer none): the system visual
353 *
354 * Since: 2.2
355 **/
356 GdkVisual *
gdk_screen_get_system_visual(GdkScreen * screen)357 gdk_screen_get_system_visual (GdkScreen * screen)
358 {
359 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
360
361 return ((GdkVisual *) GDK_SCREEN_X11 (screen)->system_visual);
362 }
363
364 /**
365 * gdk_visual_get_best:
366 *
367 * Get the visual with the most available colors for the default
368 * GDK screen. The return value should not be freed.
369 *
370 * Return value: (transfer none): best visual
371 **/
372 GdkVisual*
gdk_visual_get_best(void)373 gdk_visual_get_best (void)
374 {
375 GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (gdk_screen_get_default());
376
377 return (GdkVisual *)screen_x11->visuals[0];
378 }
379
380 /**
381 * gdk_visual_get_best_with_depth:
382 * @depth: a bit depth
383 *
384 * Get the best visual with depth @depth for the default GDK screen.
385 * Color visuals and visuals with mutable colormaps are preferred
386 * over grayscale or fixed-colormap visuals. The return value should not
387 * be freed. %NULL may be returned if no visual supports @depth.
388 *
389 * Return value: (transfer none): best visual for the given depth
390 **/
391 GdkVisual*
gdk_visual_get_best_with_depth(gint depth)392 gdk_visual_get_best_with_depth (gint depth)
393 {
394 GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (gdk_screen_get_default ());
395 GdkVisual *return_val;
396 int i;
397
398 return_val = NULL;
399 for (i = 0; i < screen_x11->nvisuals; i++)
400 if (depth == screen_x11->visuals[i]->visual.depth)
401 {
402 return_val = (GdkVisual *) screen_x11->visuals[i];
403 break;
404 }
405
406 return return_val;
407 }
408
409 /**
410 * gdk_visual_get_best_with_type:
411 * @visual_type: a visual type
412 *
413 * Get the best visual of the given @visual_type for the default GDK screen.
414 * Visuals with higher color depths are considered better. The return value
415 * should not be freed. %NULL may be returned if no visual has type
416 * @visual_type.
417 *
418 * Return value: (transfer none): best visual of the given type
419 **/
420 GdkVisual*
gdk_visual_get_best_with_type(GdkVisualType visual_type)421 gdk_visual_get_best_with_type (GdkVisualType visual_type)
422 {
423 GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (gdk_screen_get_default ());
424 GdkVisual *return_val;
425 int i;
426
427 return_val = NULL;
428 for (i = 0; i < screen_x11->nvisuals; i++)
429 if (visual_type == screen_x11->visuals[i]->visual.type)
430 {
431 return_val = (GdkVisual *) screen_x11->visuals[i];
432 break;
433 }
434
435 return return_val;
436 }
437
438 /**
439 * gdk_visual_get_best_with_both:
440 * @depth: a bit depth
441 * @visual_type: a visual type
442 *
443 * Combines gdk_visual_get_best_with_depth() and gdk_visual_get_best_with_type().
444 *
445 * Return value: (transfer none): best visual with both @depth and
446 * @visual_type, or %NULL if none
447 **/
448 GdkVisual*
gdk_visual_get_best_with_both(gint depth,GdkVisualType visual_type)449 gdk_visual_get_best_with_both (gint depth,
450 GdkVisualType visual_type)
451 {
452 GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (gdk_screen_get_default ());
453 GdkVisual *return_val;
454 int i;
455
456 return_val = NULL;
457 for (i = 0; i < screen_x11->nvisuals; i++)
458 if ((depth == screen_x11->visuals[i]->visual.depth) &&
459 (visual_type == screen_x11->visuals[i]->visual.type))
460 {
461 return_val = (GdkVisual *) screen_x11->visuals[i];
462 break;
463 }
464
465 return return_val;
466 }
467
468 /**
469 * gdk_query_depths:
470 * @depths: (out) (array): return location for available depths
471 * @count: (out): return location for number of available depths
472 *
473 * This function returns the available bit depths for the default
474 * screen. It's equivalent to listing the visuals
475 * (gdk_list_visuals()) and then looking at the depth field in each
476 * visual, removing duplicates.
477 *
478 * The array returned by this function should not be freed.
479 *
480 **/
481 void
gdk_query_depths(gint ** depths,gint * count)482 gdk_query_depths (gint **depths,
483 gint *count)
484 {
485 GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (gdk_screen_get_default ());
486
487 *count = screen_x11->navailable_depths;
488 *depths = screen_x11->available_depths;
489 }
490
491 /**
492 * gdk_query_visual_types:
493 * @visual_types: return location for the available visual types
494 * @count: return location for the number of available visual types
495 *
496 * This function returns the available visual types for the default
497 * screen. It's equivalent to listing the visuals
498 * (gdk_list_visuals()) and then looking at the type field in each
499 * visual, removing duplicates.
500 *
501 * The array returned by this function should not be freed.
502 **/
503 void
gdk_query_visual_types(GdkVisualType ** visual_types,gint * count)504 gdk_query_visual_types (GdkVisualType **visual_types,
505 gint *count)
506 {
507 GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (gdk_screen_get_default ());
508
509 *count = screen_x11->navailable_types;
510 *visual_types = screen_x11->available_types;
511 }
512
513 /**
514 * gdk_screen_list_visuals:
515 * @screen: the relevant #GdkScreen.
516 *
517 * Lists the available visuals for the specified @screen.
518 * A visual describes a hardware image data format.
519 * For example, a visual might support 24-bit color, or 8-bit color,
520 * and might expect pixels to be in a certain format.
521 *
522 * Call g_list_free() on the return value when you're finished with it.
523 *
524 * Return value: (transfer container) (element-type GdkVisual):
525 * a list of visuals; the list must be freed, but not its contents
526 *
527 * Since: 2.2
528 **/
529 GList *
gdk_screen_list_visuals(GdkScreen * screen)530 gdk_screen_list_visuals (GdkScreen *screen)
531 {
532 GList *list;
533 GdkScreenX11 *screen_x11;
534 guint i;
535
536 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
537 screen_x11 = GDK_SCREEN_X11 (screen);
538
539 list = NULL;
540
541 for (i = 0; i < screen_x11->nvisuals; ++i)
542 list = g_list_append (list, screen_x11->visuals[i]);
543
544 return list;
545 }
546
547 /**
548 * gdk_x11_screen_lookup_visual:
549 * @screen: a #GdkScreen.
550 * @xvisualid: an X Visual ID.
551 *
552 * Looks up the #GdkVisual for a particular screen and X Visual ID.
553 *
554 * Returns: (transfer none): the #GdkVisual (owned by the screen
555 * object), or %NULL if the visual ID wasn't found.
556 *
557 * Since: 2.2
558 */
559 GdkVisual *
gdk_x11_screen_lookup_visual(GdkScreen * screen,VisualID xvisualid)560 gdk_x11_screen_lookup_visual (GdkScreen *screen,
561 VisualID xvisualid)
562 {
563 int i;
564 GdkScreenX11 *screen_x11;
565 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
566 screen_x11 = GDK_SCREEN_X11 (screen);
567
568 for (i = 0; i < screen_x11->nvisuals; i++)
569 if (xvisualid == screen_x11->visuals[i]->xvisual->visualid)
570 return (GdkVisual *) screen_x11->visuals[i];
571
572 return NULL;
573 }
574
575 /**
576 * gdkx_visual_get:
577 * @xvisualid: a X visual id.
578 *
579 * Returns a #GdkVisual corresponding to a X visual.
580 *
581 * Return value: the #GdkVisual.
582 *
583 * Deprecated:2.24: Use gdk_x11_screen_lookup_visual() instead
584 */
585 GdkVisual*
gdkx_visual_get(VisualID xvisualid)586 gdkx_visual_get (VisualID xvisualid)
587 {
588 return gdk_x11_screen_lookup_visual (gdk_screen_get_default (), xvisualid);
589 }
590
591 static void
gdk_visual_add(GdkVisual * visual)592 gdk_visual_add (GdkVisual *visual)
593 {
594 GdkVisualPrivate *private = (GdkVisualPrivate *) visual;
595 GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (private->screen);
596
597 if (!screen_x11->visual_hash)
598 screen_x11->visual_hash = g_hash_table_new ((GHashFunc) gdk_visual_hash,
599 (GEqualFunc) gdk_visual_equal);
600
601 g_hash_table_insert (screen_x11->visual_hash, private->xvisual, visual);
602 }
603
604 static void
gdk_visual_decompose_mask(gulong mask,gint * shift,gint * prec)605 gdk_visual_decompose_mask (gulong mask,
606 gint *shift,
607 gint *prec)
608 {
609 *shift = 0;
610 *prec = 0;
611
612 if (mask == 0)
613 {
614 g_warning ("Mask is 0 in visual. Server bug ?");
615 return;
616 }
617
618 while (!(mask & 0x1))
619 {
620 (*shift)++;
621 mask >>= 1;
622 }
623
624 while (mask & 0x1)
625 {
626 (*prec)++;
627 mask >>= 1;
628 }
629 }
630
631 static guint
gdk_visual_hash(Visual * key)632 gdk_visual_hash (Visual *key)
633 {
634 return key->visualid;
635 }
636
637 static gboolean
gdk_visual_equal(Visual * a,Visual * b)638 gdk_visual_equal (Visual *a,
639 Visual *b)
640 {
641 return (a->visualid == b->visualid);
642 }
643
644 /**
645 * gdk_x11_visual_get_xvisual:
646 * @visual: a #GdkVisual.
647 *
648 * Returns the X visual belonging to a #GdkVisual.
649 *
650 * Return value: an Xlib <type>Visual*</type>.
651 **/
652 Visual *
gdk_x11_visual_get_xvisual(GdkVisual * visual)653 gdk_x11_visual_get_xvisual (GdkVisual *visual)
654 {
655 g_return_val_if_fail (visual != NULL, NULL);
656
657 return ((GdkVisualPrivate*) visual)->xvisual;
658 }
659
660 /**
661 * gdk_visual_get_screen:
662 * @visual: a #GdkVisual
663 *
664 * Gets the screen to which this visual belongs
665 *
666 * Return value: (transfer none): the screen to which this visual belongs.
667 *
668 * Since: 2.2
669 **/
670 GdkScreen *
gdk_visual_get_screen(GdkVisual * visual)671 gdk_visual_get_screen (GdkVisual *visual)
672 {
673 g_return_val_if_fail (GDK_IS_VISUAL (visual), NULL);
674
675 return ((GdkVisualPrivate*) visual)->screen;
676 }
677
678 #define __GDK_VISUAL_X11_C__
679 #include "gdkaliasdef.c"
680