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 
G_DEFINE_TYPE(GdkVisual,gdk_visual,G_TYPE_OBJECT)57 G_DEFINE_TYPE (GdkVisual, gdk_visual, G_TYPE_OBJECT)
58 
59 static void
60 gdk_visual_init (GdkVisual *visual)
61 {
62 }
63 
64 static void
gdk_visual_finalize(GObject * object)65 gdk_visual_finalize (GObject *object)
66 {
67   G_OBJECT_CLASS (gdk_visual_parent_class)->finalize (object);
68 }
69 
70 static void
gdk_visual_class_init(GdkVisualClass * visual_class)71 gdk_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  *
92  * Deprecated: 3.22: Use gdk_screen_list_visuals (gdk_screen_get_default ()).
93  */
94 GList*
gdk_list_visuals(void)95 gdk_list_visuals (void)
96 {
97   return gdk_screen_list_visuals (gdk_screen_get_default ());
98 }
99 
100 /**
101  * gdk_visual_get_system:
102  *
103  * Get the system’s default visual for the default GDK screen.
104  * This is the visual for the root window of the display.
105  * The return value should not be freed.
106  *
107  * Returns: (transfer none): system visual
108  *
109  * Deprecated: 3.22: Use gdk_screen_get_system_visual (gdk_screen_get_default ()).
110  */
111 GdkVisual*
gdk_visual_get_system(void)112 gdk_visual_get_system (void)
113 {
114   return gdk_screen_get_system_visual (gdk_screen_get_default());
115 }
116 
117 /**
118  * gdk_visual_get_best_depth:
119  *
120  * Get the best available depth for the default GDK screen.  “Best”
121  * means “largest,” i.e. 32 preferred over 24 preferred over 8 bits
122  * per pixel.
123  *
124  * Returns: best available depth
125  *
126  * Deprecated: 3.22: Visual selection should be done using
127  *     gdk_screen_get_system_visual() and gdk_screen_get_rgba_visual()
128  */
129 gint
gdk_visual_get_best_depth(void)130 gdk_visual_get_best_depth (void)
131 {
132   GdkScreen *screen = gdk_screen_get_default();
133 
134   return GDK_SCREEN_GET_CLASS(screen)->visual_get_best_depth (screen);
135 }
136 
137 /**
138  * gdk_visual_get_best_type:
139  *
140  * Return the best available visual type for the default GDK screen.
141  *
142  * Returns: best visual type
143  *
144  * Deprecated: 3.22: Visual selection should be done using
145  *     gdk_screen_get_system_visual() and gdk_screen_get_rgba_visual()
146  */
147 GdkVisualType
gdk_visual_get_best_type(void)148 gdk_visual_get_best_type (void)
149 {
150   GdkScreen *screen = gdk_screen_get_default();
151 
152   return GDK_SCREEN_GET_CLASS(screen)->visual_get_best_type (screen);
153 }
154 
155 /**
156  * gdk_visual_get_best:
157  *
158  * Get the visual with the most available colors for the default
159  * GDK screen. The return value should not be freed.
160  *
161  * Returns: (transfer none): best visual
162  *
163  * Deprecated: 3.22: Visual selection should be done using
164  *     gdk_screen_get_system_visual() and gdk_screen_get_rgba_visual()
165  */
166 GdkVisual*
gdk_visual_get_best(void)167 gdk_visual_get_best (void)
168 {
169   GdkScreen *screen = gdk_screen_get_default();
170 
171   return GDK_SCREEN_GET_CLASS(screen)->visual_get_best (screen);
172 }
173 
174 /**
175  * gdk_visual_get_best_with_depth:
176  * @depth: a bit depth
177  *
178  * Get the best visual with depth @depth for the default GDK screen.
179  * Color visuals and visuals with mutable colormaps are preferred
180  * over grayscale or fixed-colormap visuals. The return value should
181  * not be freed. %NULL may be returned if no visual supports @depth.
182  *
183  * Returns: (transfer none): best visual for the given depth
184  *
185  * Deprecated: 3.22: Visual selection should be done using
186  *     gdk_screen_get_system_visual() and gdk_screen_get_rgba_visual()
187  */
188 GdkVisual*
gdk_visual_get_best_with_depth(gint depth)189 gdk_visual_get_best_with_depth (gint depth)
190 {
191   GdkScreen *screen = gdk_screen_get_default();
192 
193   return GDK_SCREEN_GET_CLASS(screen)->visual_get_best_with_depth (screen, depth);
194 }
195 
196 /**
197  * gdk_visual_get_best_with_type:
198  * @visual_type: a visual type
199  *
200  * Get the best visual of the given @visual_type for the default GDK screen.
201  * Visuals with higher color depths are considered better. The return value
202  * should not be freed. %NULL may be returned if no visual has type
203  * @visual_type.
204  *
205  * Returns: (transfer none): best visual of the given type
206  *
207  * Deprecated: 3.22: Visual selection should be done using
208  *     gdk_screen_get_system_visual() and gdk_screen_get_rgba_visual()
209  */
210 GdkVisual*
gdk_visual_get_best_with_type(GdkVisualType visual_type)211 gdk_visual_get_best_with_type (GdkVisualType visual_type)
212 {
213   GdkScreen *screen = gdk_screen_get_default();
214 
215   return GDK_SCREEN_GET_CLASS(screen)->visual_get_best_with_type (screen,
216                                                                   visual_type);
217 }
218 
219 /**
220  * gdk_visual_get_best_with_both:
221  * @depth: a bit depth
222  * @visual_type: a visual type
223  *
224  * Combines gdk_visual_get_best_with_depth() and
225  * gdk_visual_get_best_with_type().
226  *
227  * Returns: (nullable) (transfer none): best visual with both @depth
228  *     and @visual_type, or %NULL if none
229  *
230  * Deprecated: 3.22: Visual selection should be done using
231  *     gdk_screen_get_system_visual() and gdk_screen_get_rgba_visual()
232  */
233 GdkVisual*
gdk_visual_get_best_with_both(gint depth,GdkVisualType visual_type)234 gdk_visual_get_best_with_both (gint          depth,
235 			       GdkVisualType visual_type)
236 {
237   GdkScreen *screen = gdk_screen_get_default();
238 
239   return GDK_SCREEN_GET_CLASS(screen)->visual_get_best_with_both (screen, depth, visual_type);
240 }
241 
242 /**
243  * gdk_query_depths:
244  * @depths: (out) (array length=count) (transfer none): return
245  *     location for available depths
246  * @count: return location for number of available depths
247  *
248  * This function returns the available bit depths for the default
249  * screen. It’s equivalent to listing the visuals
250  * (gdk_list_visuals()) and then looking at the depth field in each
251  * visual, removing duplicates.
252  *
253  * The array returned by this function should not be freed.
254  *
255  * Deprecated: 3.22: Visual selection should be done using
256  *     gdk_screen_get_system_visual() and gdk_screen_get_rgba_visual()
257  */
258 void
gdk_query_depths(gint ** depths,gint * count)259 gdk_query_depths (gint **depths,
260                   gint  *count)
261 {
262   GdkScreen *screen = gdk_screen_get_default();
263 
264   GDK_SCREEN_GET_CLASS(screen)->query_depths (screen, depths, count);
265 }
266 
267 /**
268  * gdk_query_visual_types:
269  * @visual_types: (out) (array length=count) (transfer none): return
270  *     location for the available visual types
271  * @count: return location for the number of available visual types
272  *
273  * This function returns the available visual types for the default
274  * screen. It’s equivalent to listing the visuals
275  * (gdk_list_visuals()) and then looking at the type field in each
276  * visual, removing duplicates.
277  *
278  * The array returned by this function should not be freed.
279  *
280  * Deprecated: 3.22: Visual selection should be done using
281  *     gdk_screen_get_system_visual() and gdk_screen_get_rgba_visual()
282  */
283 void
gdk_query_visual_types(GdkVisualType ** visual_types,gint * count)284 gdk_query_visual_types (GdkVisualType **visual_types,
285                         gint           *count)
286 {
287   GdkScreen *screen = gdk_screen_get_default();
288 
289   GDK_SCREEN_GET_CLASS(screen)->query_visual_types (screen, visual_types, count);
290 }
291 
292 /**
293  * gdk_visual_get_visual_type:
294  * @visual: A #GdkVisual.
295  *
296  * Returns the type of visual this is (PseudoColor, TrueColor, etc).
297  *
298  * Returns: A #GdkVisualType stating the type of @visual.
299  *
300  * Since: 2.22
301  */
302 GdkVisualType
gdk_visual_get_visual_type(GdkVisual * visual)303 gdk_visual_get_visual_type (GdkVisual *visual)
304 {
305   g_return_val_if_fail (GDK_IS_VISUAL (visual), 0);
306 
307   return visual->type;
308 }
309 
310 /**
311  * gdk_visual_get_depth:
312  * @visual: A #GdkVisual.
313  *
314  * Returns the bit depth of this visual.
315  *
316  * Returns: The bit depth of this visual.
317  *
318  * Since: 2.22
319  */
320 gint
gdk_visual_get_depth(GdkVisual * visual)321 gdk_visual_get_depth (GdkVisual *visual)
322 {
323   g_return_val_if_fail (GDK_IS_VISUAL (visual), 0);
324 
325   return visual->depth;
326 }
327 
328 /**
329  * gdk_visual_get_byte_order:
330  * @visual: A #GdkVisual.
331  *
332  * Returns the byte order of this visual.
333  *
334  * The information returned by this function is only relevant
335  * when working with XImages, and not all backends return
336  * meaningful information for this.
337  *
338  * Returns: A #GdkByteOrder stating the byte order of @visual.
339  *
340  * Since: 2.22
341  *
342  * Deprecated: 3.22: This information is not useful
343  */
344 GdkByteOrder
gdk_visual_get_byte_order(GdkVisual * visual)345 gdk_visual_get_byte_order (GdkVisual *visual)
346 {
347   g_return_val_if_fail (GDK_IS_VISUAL (visual), 0);
348 
349   return visual->byte_order;
350 }
351 
352 /**
353  * gdk_visual_get_colormap_size:
354  * @visual: A #GdkVisual.
355  *
356  * Returns the size of a colormap for this visual.
357  *
358  * You have to use platform-specific APIs to manipulate colormaps.
359  *
360  * Returns: The size of a colormap that is suitable for @visual.
361  *
362  * Since: 2.22
363  *
364  * Deprecated: 3.22: This information is not useful, since GDK does not
365  *     provide APIs to operate on colormaps.
366  */
367 gint
gdk_visual_get_colormap_size(GdkVisual * visual)368 gdk_visual_get_colormap_size (GdkVisual *visual)
369 {
370   g_return_val_if_fail (GDK_IS_VISUAL (visual), 0);
371 
372   return visual->colormap_size;
373 }
374 
375 /**
376  * gdk_visual_get_bits_per_rgb:
377  * @visual: a #GdkVisual
378  *
379  * Returns the number of significant bits per red, green and blue value.
380  *
381  * Not all GDK backend provide a meaningful value for this function.
382  *
383  * Returns: The number of significant bits per color value for @visual.
384  *
385  * Since: 2.22
386  *
387  * Deprecated: 3.22. Use gdk_visual_get_red_pixel_details() and its variants to
388  *     learn about the pixel layout of TrueColor and DirectColor visuals
389  */
390 gint
gdk_visual_get_bits_per_rgb(GdkVisual * visual)391 gdk_visual_get_bits_per_rgb (GdkVisual *visual)
392 {
393   g_return_val_if_fail (GDK_IS_VISUAL (visual), 0);
394 
395   return visual->bits_per_rgb;
396 }
397 
398 static void
gdk_visual_get_pixel_details(GdkVisual * visual,gulong pixel_mask,guint32 * mask,gint * shift,gint * precision)399 gdk_visual_get_pixel_details (GdkVisual *visual,
400                               gulong     pixel_mask,
401                               guint32   *mask,
402                               gint      *shift,
403                               gint      *precision)
404 {
405   gulong m = 0;
406   gint s = 0;
407   gint p = 0;
408 
409   if (pixel_mask != 0)
410     {
411       m = pixel_mask;
412       while (!(m & 0x1))
413         {
414           s++;
415           m >>= 1;
416         }
417 
418       while (m & 0x1)
419         {
420           p++;
421           m >>= 1;
422         }
423     }
424 
425   if (mask)
426     *mask = pixel_mask;
427 
428   if (shift)
429     *shift = s;
430 
431   if (precision)
432     *precision = p;
433 }
434 
435 /**
436  * gdk_visual_get_red_pixel_details:
437  * @visual: A #GdkVisual
438  * @mask: (out) (allow-none): A pointer to a #guint32 to be filled in, or %NULL
439  * @shift: (out) (allow-none): A pointer to a #gint to be filled in, or %NULL
440  * @precision: (out) (allow-none): A pointer to a #gint to be filled in, or %NULL
441  *
442  * Obtains values that are needed to calculate red pixel values in TrueColor
443  * and DirectColor. The “mask” is the significant bits within the pixel.
444  * The “shift” is the number of bits left we must shift a primary for it
445  * to be in position (according to the "mask"). Finally, "precision" refers
446  * to how much precision the pixel value contains for a particular primary.
447  *
448  * Since: 2.22
449  */
450 void
gdk_visual_get_red_pixel_details(GdkVisual * visual,guint32 * mask,gint * shift,gint * precision)451 gdk_visual_get_red_pixel_details (GdkVisual *visual,
452                                   guint32   *mask,
453                                   gint      *shift,
454                                   gint      *precision)
455 {
456   g_return_if_fail (GDK_IS_VISUAL (visual));
457 
458   gdk_visual_get_pixel_details (visual, visual->red_mask, mask, shift, precision);
459 }
460 
461 /**
462  * gdk_visual_get_green_pixel_details:
463  * @visual: a #GdkVisual
464  * @mask: (out) (allow-none): A pointer to a #guint32 to be filled in, or %NULL
465  * @shift: (out) (allow-none): A pointer to a #gint to be filled in, or %NULL
466  * @precision: (out) (allow-none): A pointer to a #gint to be filled in, or %NULL
467  *
468  * Obtains values that are needed to calculate green pixel values in TrueColor
469  * and DirectColor. The “mask” is the significant bits within the pixel.
470  * The “shift” is the number of bits left we must shift a primary for it
471  * to be in position (according to the "mask"). Finally, "precision" refers
472  * to how much precision the pixel value contains for a particular primary.
473  *
474  * Since: 2.22
475  */
476 void
gdk_visual_get_green_pixel_details(GdkVisual * visual,guint32 * mask,gint * shift,gint * precision)477 gdk_visual_get_green_pixel_details (GdkVisual *visual,
478                                     guint32   *mask,
479                                     gint      *shift,
480                                     gint      *precision)
481 {
482   g_return_if_fail (GDK_IS_VISUAL (visual));
483 
484   gdk_visual_get_pixel_details (visual, visual->green_mask, mask, shift, precision);
485 }
486 
487 /**
488  * gdk_visual_get_blue_pixel_details:
489  * @visual: a #GdkVisual
490  * @mask: (out) (allow-none): A pointer to a #guint32 to be filled in, or %NULL
491  * @shift: (out) (allow-none): A pointer to a #gint to be filled in, or %NULL
492  * @precision: (out) (allow-none): A pointer to a #gint to be filled in, or %NULL
493  *
494  * Obtains values that are needed to calculate blue pixel values in TrueColor
495  * and DirectColor. The “mask” is the significant bits within the pixel.
496  * The “shift” is the number of bits left we must shift a primary for it
497  * to be in position (according to the "mask"). Finally, "precision" refers
498  * to how much precision the pixel value contains for a particular primary.
499  *
500  * Since: 2.22
501  */
502 void
gdk_visual_get_blue_pixel_details(GdkVisual * visual,guint32 * mask,gint * shift,gint * precision)503 gdk_visual_get_blue_pixel_details (GdkVisual *visual,
504                                    guint32   *mask,
505                                    gint      *shift,
506                                    gint      *precision)
507 {
508   g_return_if_fail (GDK_IS_VISUAL (visual));
509 
510   gdk_visual_get_pixel_details (visual, visual->blue_mask, mask, shift, precision);
511 }
512 
513 /**
514  * gdk_visual_get_screen:
515  * @visual: a #GdkVisual
516  *
517  * Gets the screen to which this visual belongs
518  *
519  * Returns: (transfer none): the screen to which this visual belongs.
520  *
521  * Since: 2.2
522  */
523 GdkScreen *
gdk_visual_get_screen(GdkVisual * visual)524 gdk_visual_get_screen (GdkVisual *visual)
525 {
526   g_return_val_if_fail (GDK_IS_VISUAL (visual), NULL);
527 
528   return visual->screen;
529 }
530