1/* GDK - The GIMP Drawing Kit
2 * gdkvisual.c
3 *
4 * Copyright 2001 Sun Microsystems Inc.
5 *
6 * Erwann Chenede <erwann.chenede@sun.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "config.h"
23
24#include "gdkvisualprivate.h"
25#include "gdkscreenprivate.h"
26
27
28/**
29 * SECTION:visuals
30 * @Short_description: Low-level display hardware information
31 * @Title: Visuals
32 *
33 * A #GdkVisual describes a particular video hardware display format.
34 * It includes information about the number of bits used for each color,
35 * the way the bits are translated into an RGB value for display, and
36 * the way the bits are stored in memory. For example, a piece of display
37 * hardware might support 24-bit color, 16-bit color, or 8-bit color;
38 * meaning 24/16/8-bit pixel sizes. For a given pixel size, pixels can
39 * be in different formats; for example the “red” element of an RGB pixel
40 * may be in the top 8 bits of the pixel, or may be in the lower 4 bits.
41 *
42 * There are several standard visuals. The visual returned by
43 * gdk_screen_get_system_visual() is the system’s default visual, and
44 * the visual returned by gdk_screen_get_rgba_visual() should be used for
45 * creating windows with an alpha channel.
46 *
47 * A number of functions are provided for determining the “best” available
48 * visual. For the purposes of making this determination, higher bit depths
49 * are considered better, and for visuals of the same bit depth,
50 * %GDK_VISUAL_PSEUDO_COLOR is preferred at 8bpp, otherwise, the visual
51 * types are ranked in the order of(highest to lowest)
52 * %GDK_VISUAL_DIRECT_COLOR, %GDK_VISUAL_TRUE_COLOR,
53 * %GDK_VISUAL_PSEUDO_COLOR, %GDK_VISUAL_STATIC_COLOR,
54 * %GDK_VISUAL_GRAYSCALE, then %GDK_VISUAL_STATIC_GRAY.
55 */
56
57G_DEFINE_TYPE (GdkVisual, gdk_visual, G_TYPE_OBJECT)
58
59static void
60gdk_visual_init (GdkVisual *visual)
61{
62}
63
64static void
65gdk_visual_finalize (GObject *object)
66{
67 G_OBJECT_CLASS (gdk_visual_parent_class)->finalize (object);
68}
69
70static void
71gdk_visual_class_init (GdkVisualClass *visual_class)
72{
73 GObjectClass *object_class = G_OBJECT_CLASS (visual_class);
74
75 object_class->finalize = gdk_visual_finalize;
76}
77
78/**
79 * gdk_list_visuals:
80 *
81 * Lists the available visuals for the default screen.
82 * (See gdk_screen_list_visuals())
83 * A visual describes a hardware image data format.
84 * For example, a visual might support 24-bit color, or 8-bit color,
85 * and might expect pixels to be in a certain format.
86 *
87 * Call g_list_free() on the return value when you’re finished with it.
88 *
89 * Returns: (transfer container) (element-type GdkVisual):
90 * a list of visuals; the list must be freed, but not its contents
91 */
92GList*
93gdk_list_visuals (void)
94{
95 return gdk_screen_list_visuals (gdk_screen_get_default ());
96}
97
98/**
99 * gdk_visual_get_system:
100 *
101 * Get the system’s default visual for the default GDK screen.
102 * This is the visual for the root window of the display.
103 * The return value should not be freed.
104 *
105 * Returns: (transfer none): system visual
106 */
107GdkVisual*
108gdk_visual_get_system (void)
109{
110 return gdk_screen_get_system_visual (gdk_screen_get_default());
111}
112
113/**
114 * gdk_visual_get_best_depth:
115 *
116 * Get the best available depth for the default GDK screen. “Best”
117 * means “largest,” i.e. 32 preferred over 24 preferred over 8 bits
118 * per pixel.
119 *
120 * Returns: best available depth
121 */
122gint
123gdk_visual_get_best_depth (void)
124{
125 GdkScreen *screen = gdk_screen_get_default();
126
127 return GDK_SCREEN_GET_CLASS(screen)->visual_get_best_depth (screen);
128}
129
130/**
131 * gdk_visual_get_best_type:
132 *
133 * Return the best available visual type for the default GDK screen.
134 *
135 * Returns: best visual type
136 */
137GdkVisualType
138gdk_visual_get_best_type (void)
139{
140 GdkScreen *screen = gdk_screen_get_default();
141
142 return GDK_SCREEN_GET_CLASS(screen)->visual_get_best_type (screen);
143}
144
145/**
146 * gdk_visual_get_best:
147 *
148 * Get the visual with the most available colors for the default
149 * GDK screen. The return value should not be freed.
150 *
151 * Returns: (transfer none): best visual
152 */
153GdkVisual*
154gdk_visual_get_best (void)
155{
156 GdkScreen *screen = gdk_screen_get_default();
157
158 return GDK_SCREEN_GET_CLASS(screen)->visual_get_best (screen);
159}
160
161/**
162 * gdk_visual_get_best_with_depth:
163 * @depth: a bit depth
164 *
165 * Get the best visual with depth @depth for the default GDK screen.
166 * Color visuals and visuals with mutable colormaps are preferred
167 * over grayscale or fixed-colormap visuals. The return value should
168 * not be freed. %NULL may be returned if no visual supports @depth.
169 *
170 * Returns: (transfer none): best visual for the given depth
171 */
172GdkVisual*
173gdk_visual_get_best_with_depth (gint depth)
174{
175 GdkScreen *screen = gdk_screen_get_default();
176
177 return GDK_SCREEN_GET_CLASS(screen)->visual_get_best_with_depth (screen, depth);
178}
179
180/**
181 * gdk_visual_get_best_with_type:
182 * @visual_type: a visual type
183 *
184 * Get the best visual of the given @visual_type for the default GDK screen.
185 * Visuals with higher color depths are considered better. The return value
186 * should not be freed. %NULL may be returned if no visual has type
187 * @visual_type.
188 *
189 * Returns: (transfer none): best visual of the given type
190 */
191GdkVisual*
192gdk_visual_get_best_with_type (GdkVisualType visual_type)
193{
194 GdkScreen *screen = gdk_screen_get_default();
195
196 return GDK_SCREEN_GET_CLASS(screen)->visual_get_best_with_type (screen,
197 visual_type);
198}
199
200/**
201 * gdk_visual_get_best_with_both:
202 * @depth: a bit depth
203 * @visual_type: a visual type
204 *
205 * Combines gdk_visual_get_best_with_depth() and
206 * gdk_visual_get_best_with_type().
207 *
208 * Returns: (nullable) (transfer none): best visual with both @depth
209 * and @visual_type, or %NULL if none
210 */
211GdkVisual*
212gdk_visual_get_best_with_both (gint depth,
213 GdkVisualType visual_type)
214{
215 GdkScreen *screen = gdk_screen_get_default();
216
217 return GDK_SCREEN_GET_CLASS(screen)->visual_get_best_with_both (screen, depth, visual_type);
218}
219
220/**
221 * gdk_query_depths:
222 * @depths: (out) (array length=count) (transfer none): return
223 * location for available depths
224 * @count: return location for number of available depths
225 *
226 * This function returns the available bit depths for the default
227 * screen. It’s equivalent to listing the visuals
228 * (gdk_list_visuals()) and then looking at the depth field in each
229 * visual, removing duplicates.
230 *
231 * The array returned by this function should not be freed.
232 */
233void
234gdk_query_depths (gint **depths,
235 gint *count)
236{
237 GdkScreen *screen = gdk_screen_get_default();
238
239 GDK_SCREEN_GET_CLASS(screen)->query_depths (screen, depths, count);
240}
241
242/**
243 * gdk_query_visual_types:
244 * @visual_types: (out) (array length=count) (transfer none): return
245 * location for the available visual types
246 * @count: return location for the number of available visual types
247 *
248 * This function returns the available visual types for the default
249 * screen. It’s equivalent to listing the visuals
250 * (gdk_list_visuals()) and then looking at the type field in each
251 * visual, removing duplicates.
252 *
253 * The array returned by this function should not be freed.
254 */
255void
256gdk_query_visual_types (GdkVisualType **visual_types,
257 gint *count)
258{
259 GdkScreen *screen = gdk_screen_get_default();
260
261 GDK_SCREEN_GET_CLASS(screen)->query_visual_types (screen, visual_types, count);
262}
263
264/**
265 * gdk_visual_get_visual_type:
266 * @visual: A #GdkVisual.
267 *
268 * Returns the type of visual this is (PseudoColor, TrueColor, etc).
269 *
270 * Returns: A #GdkVisualType stating the type of @visual.
271 *
272 * Since: 2.22
273 */
274GdkVisualType
275gdk_visual_get_visual_type (GdkVisual *visual)
276{
277 g_return_val_if_fail (GDK_IS_VISUAL (visual), 0);
278
279 return visual->type;
280}
281
282/**
283 * gdk_visual_get_depth:
284 * @visual: A #GdkVisual.
285 *
286 * Returns the bit depth of this visual.
287 *
288 * Returns: The bit depth of this visual.
289 *
290 * Since: 2.22
291 */
292gint
293gdk_visual_get_depth (GdkVisual *visual)
294{
295 g_return_val_if_fail (GDK_IS_VISUAL (visual), 0);
296
297 return visual->depth;
298}
299
300/**
301 * gdk_visual_get_byte_order:
302 * @visual: A #GdkVisual.
303 *
304 * Returns the byte order of this visual.
305 *
306 * The information returned by this function is only relevant
307 * when working with XImages, and not all backends return
308 * meaningful information for this.
309 *
310 * Returns: A #GdkByteOrder stating the byte order of @visual.
311 *
312 * Since: 2.22
313 *
314 * Deprecated: 3.22: This information is not useful
315 */
316GdkByteOrder
317gdk_visual_get_byte_order (GdkVisual *visual)
318{
319 g_return_val_if_fail (GDK_IS_VISUAL (visual), 0);
320
321 return visual->byte_order;
322}
323
324/**
325 * gdk_visual_get_colormap_size:
326 * @visual: A #GdkVisual.
327 *
328 * Returns the size of a colormap for this visual.
329 *
330 * You have to use platform-specific APIs to manipulate colormaps.
331 *
332 * Returns: The size of a colormap that is suitable for @visual.
333 *
334 * Since: 2.22
335 *
336 * Deprecated: 3.22: This information is not useful, since GDK does not
337 * provide APIs to operate on colormaps.
338 */
339gint
340gdk_visual_get_colormap_size (GdkVisual *visual)
341{
342 g_return_val_if_fail (GDK_IS_VISUAL (visual), 0);
343
344 return visual->colormap_size;
345}
346
347/**
348 * gdk_visual_get_bits_per_rgb:
349 * @visual: a #GdkVisual
350 *
351 * Returns the number of significant bits per red, green and blue value.
352 *
353 * Not all GDK backend provide a meaningful value for this function.
354 *
355 * Returns: The number of significant bits per color value for @visual.
356 *
357 * Since: 2.22
358 *
359 * Deprecated: 3.22. Use gdk_visual_get_red_pixel_details() and its variants to
360 * learn about the pixel layout of TrueColor and DirectColor visuals
361 */
362gint
363gdk_visual_get_bits_per_rgb (GdkVisual *visual)
364{
365 g_return_val_if_fail (GDK_IS_VISUAL (visual), 0);
366
367 return visual->bits_per_rgb;
368}
369
370static void
371gdk_visual_get_pixel_details (GdkVisual *visual,
372 gulong pixel_mask,
373 guint32 *mask,
374 gint *shift,
375 gint *precision)
376{
377 gulong m = 0;
378 gint s = 0;
379 gint p = 0;
380
381 if (pixel_mask != 0)
382 {
383 m = pixel_mask;
384 while (!(m & 0x1))
385 {
386 s++;
387 m >>= 1;
388 }
389
390 while (m & 0x1)
391 {
392 p++;
393 m >>= 1;
394 }
395 }
396
397 if (mask)
398 *mask = pixel_mask;
399
400 if (shift)
401 *shift = s;
402
403 if (precision)
404 *precision = p;
405}
406
407/**
408 * gdk_visual_get_red_pixel_details:
409 * @visual: A #GdkVisual
410 * @mask: (out) (allow-none): A pointer to a #guint32 to be filled in, or %NULL
411 * @shift: (out) (allow-none): A pointer to a #gint to be filled in, or %NULL
412 * @precision: (out) (allow-none): A pointer to a #gint to be filled in, or %NULL
413 *
414 * Obtains values that are needed to calculate red pixel values in TrueColor
415 * and DirectColor. The “mask” is the significant bits within the pixel.
416 * The “shift” is the number of bits left we must shift a primary for it
417 * to be in position (according to the "mask"). Finally, "precision" refers
418 * to how much precision the pixel value contains for a particular primary.
419 *
420 * Since: 2.22
421 */
422void
423gdk_visual_get_red_pixel_details (GdkVisual *visual,
424 guint32 *mask,
425 gint *shift,
426 gint *precision)
427{
428 g_return_if_fail (GDK_IS_VISUAL (visual));
429
430 gdk_visual_get_pixel_details (visual, visual->red_mask, mask, shift, precision);
431}
432
433/**
434 * gdk_visual_get_green_pixel_details:
435 * @visual: a #GdkVisual
436 * @mask: (out) (allow-none): A pointer to a #guint32 to be filled in, or %NULL
437 * @shift: (out) (allow-none): A pointer to a #gint to be filled in, or %NULL
438 * @precision: (out) (allow-none): A pointer to a #gint to be filled in, or %NULL
439 *
440 * Obtains values that are needed to calculate green pixel values in TrueColor
441 * and DirectColor. The “mask” is the significant bits within the pixel.
442 * The “shift” is the number of bits left we must shift a primary for it
443 * to be in position (according to the "mask"). Finally, "precision" refers
444 * to how much precision the pixel value contains for a particular primary.
445 *
446 * Since: 2.22
447 */
448void
449gdk_visual_get_green_pixel_details (GdkVisual *visual,
450 guint32 *mask,
451 gint *shift,
452 gint *precision)
453{
454 g_return_if_fail (GDK_IS_VISUAL (visual));
455
456 gdk_visual_get_pixel_details (visual, visual->green_mask, mask, shift, precision);
457}
458
459/**
460 * gdk_visual_get_blue_pixel_details:
461 * @visual: a #GdkVisual
462 * @mask: (out) (allow-none): A pointer to a #guint32 to be filled in, or %NULL
463 * @shift: (out) (allow-none): A pointer to a #gint to be filled in, or %NULL
464 * @precision: (out) (allow-none): A pointer to a #gint to be filled in, or %NULL
465 *
466 * Obtains values that are needed to calculate blue pixel values in TrueColor
467 * and DirectColor. The “mask” is the significant bits within the pixel.
468 * The “shift” is the number of bits left we must shift a primary for it
469 * to be in position (according to the "mask"). Finally, "precision" refers
470 * to how much precision the pixel value contains for a particular primary.
471 *
472 * Since: 2.22
473 */
474void
475gdk_visual_get_blue_pixel_details (GdkVisual *visual,
476 guint32 *mask,
477 gint *shift,
478 gint *precision)
479{
480 g_return_if_fail (GDK_IS_VISUAL (visual));
481
482 gdk_visual_get_pixel_details (visual, visual->blue_mask, mask, shift, precision);
483}
484
485/**
486 * gdk_visual_get_screen:
487 * @visual: a #GdkVisual
488 *
489 * Gets the screen to which this visual belongs
490 *
491 * Returns: (transfer none): the screen to which this visual belongs.
492 *
493 * Since: 2.2
494 */
495GdkScreen *
496gdk_visual_get_screen (GdkVisual *visual)
497{
498 g_return_val_if_fail (GDK_IS_VISUAL (visual), NULL);
499
500 return visual->screen;
501}
502