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/.
29 #include "gdkproperty.h"
31 #include "gdkprivate.h"
32 #include "gdkinternals.h"
33 #include "gdkselection.h"
34 #include "gdkprivate-x11.h"
35 #include "gdkdisplay-x11.h"
36 #include "gdkscreen-x11.h"
39 #include <X11/Xatom.h>
45 * @Short_description: Functions to manipulate properties on windows
46 * @Title: Properties and Atoms
48 * Each window under X can have any number of associated
49 * <firstterm>properties</firstterm> attached to it.
50 * Properties are arbitrary chunks of data identified by
51 * <firstterm>atom</firstterm>s. (An <firstterm>atom</firstterm>
52 * is a numeric index into a string table on the X server. They are used
53 * to transfer strings efficiently between clients without
54 * having to transfer the entire string.) A property
55 * has an associated type, which is also identified
58 * A property has an associated <firstterm>format</firstterm>,
59 * an integer describing how many bits are in each unit
60 * of data inside the property. It must be 8, 16, or 32.
61 * When data is transferred between the server and client,
62 * if they are of different endianesses it will be byteswapped
63 * as necessary according to the format of the property.
64 * Note that on the client side, properties of format 32
65 * will be stored with one unit per <emphasis>long</emphasis>,
66 * even if a long integer has more than 32 bits on the platform.
67 * (This decision was apparently made for Xlib to maintain
68 * compatibility with programs that assumed longs were 32
69 * bits, at the expense of programs that knew better.)
71 * The functions in this section are used to add, remove
72 * and change properties on windows, to convert atoms
73 * to and from strings and to manipulate some types of
74 * data commonly stored in X window properties.
78 static GPtrArray *virtual_atom_array;
79 static GHashTable *virtual_atom_hash;
81 static const gchar xatoms_string[] =
82 /* These are all the standard predefined X atoms */
83 "\0" /* leave a space for None, even though it is not a predefined atom */
119 "WM_CLIENT_MACHINE\0"
134 "UNDERLINE_POSITION\0"
135 "UNDERLINE_THICKNESS\0"
137 "STRIKEOUT_DESCENT\0"
152 /* Below here, these are our additions. Increment N_CUSTOM_PREDEFINED
155 "CLIPBOARD\0" /* = 69 */
158 static const gint xatoms_offset[] = {
159 0, 1, 9, 19, 23, 28, 35, 44, 53, 60, 72, 84,
160 96, 108, 120, 132, 144, 156, 165, 170, 178, 185, 189, 201,
161 218, 232, 245, 258, 274, 287, 301, 313, 320, 329, 336, 347,
162 356, 374, 387, 400, 408, 424, 438, 452, 462, 473, 483, 493,
163 507, 521, 533, 545, 564, 584, 601, 619, 632, 641, 652, 659,
164 670, 681, 691, 698, 708, 720, 730, 741, 750, 767
167 #define N_CUSTOM_PREDEFINED 1
169 #define ATOM_TO_INDEX(atom) (GPOINTER_TO_UINT(atom))
170 #define INDEX_TO_ATOM(atom) ((GdkAtom)GUINT_TO_POINTER(atom))
173 insert_atom_pair (GdkDisplay *display,
174 GdkAtom virtual_atom,
177 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
179 if (!display_x11->atom_from_virtual)
181 display_x11->atom_from_virtual = g_hash_table_new (g_direct_hash, NULL);
182 display_x11->atom_to_virtual = g_hash_table_new (g_direct_hash, NULL);
185 g_hash_table_insert (display_x11->atom_from_virtual,
186 GDK_ATOM_TO_POINTER (virtual_atom),
187 GUINT_TO_POINTER (xatom));
188 g_hash_table_insert (display_x11->atom_to_virtual,
189 GUINT_TO_POINTER (xatom),
190 GDK_ATOM_TO_POINTER (virtual_atom));
194 lookup_cached_xatom (GdkDisplay *display,
197 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
199 if (ATOM_TO_INDEX (atom) < G_N_ELEMENTS (xatoms_offset) - N_CUSTOM_PREDEFINED)
200 return ATOM_TO_INDEX (atom);
202 if (display_x11->atom_from_virtual)
203 return GPOINTER_TO_UINT (g_hash_table_lookup (display_x11->atom_from_virtual,
204 GDK_ATOM_TO_POINTER (atom)));
210 * gdk_x11_atom_to_xatom_for_display:
211 * @display: A #GdkDisplay
212 * @atom: A #GdkAtom, or %GDK_NONE
214 * Converts from a #GdkAtom to the X atom for a #GdkDisplay
215 * with the same string value. The special value %GDK_NONE
216 * is converted to %None.
218 * Return value: the X atom corresponding to @atom, or %None
223 gdk_x11_atom_to_xatom_for_display (GdkDisplay *display,
228 g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
230 if (atom == GDK_NONE)
233 if (gdk_display_is_closed (display))
236 xatom = lookup_cached_xatom (display, atom);
242 g_return_val_if_fail (ATOM_TO_INDEX (atom) < virtual_atom_array->len, None);
244 name = g_ptr_array_index (virtual_atom_array, ATOM_TO_INDEX (atom));
246 xatom = XInternAtom (GDK_DISPLAY_XDISPLAY (display), name, FALSE);
247 insert_atom_pair (display, atom, xatom);
254 _gdk_x11_precache_atoms (GdkDisplay *display,
255 const gchar * const *atom_names,
260 const gchar **xatom_names;
264 xatoms = g_new (Atom, n_atoms);
265 xatom_names = g_new (const gchar *, n_atoms);
266 atoms = g_new (GdkAtom, n_atoms);
269 for (i = 0; i < n_atoms; i++)
271 GdkAtom atom = gdk_atom_intern_static_string (atom_names[i]);
272 if (lookup_cached_xatom (display, atom) == None)
274 atoms[n_xatoms] = atom;
275 xatom_names[n_xatoms] = atom_names[i];
282 #ifdef HAVE_XINTERNATOMS
283 XInternAtoms (GDK_DISPLAY_XDISPLAY (display),
284 (char **)xatom_names, n_xatoms, False, xatoms);
286 for (i = 0; i < n_xatoms; i++)
287 xatoms[i] = XInternAtom (GDK_DISPLAY_XDISPLAY (display),
288 xatom_names[i], False);
292 for (i = 0; i < n_xatoms; i++)
293 insert_atom_pair (display, atoms[i], xatoms[i]);
296 g_free (xatom_names);
301 * gdk_x11_atom_to_xatom:
304 * Converts from a #GdkAtom to the X atom for the default GDK display
305 * with the same string value.
307 * Return value: the X atom corresponding to @atom.
310 gdk_x11_atom_to_xatom (GdkAtom atom)
312 return gdk_x11_atom_to_xatom_for_display (gdk_display_get_default (), atom);
316 * gdk_x11_xatom_to_atom_for_display:
317 * @display: A #GdkDisplay
320 * Convert from an X atom for a #GdkDisplay to the corresponding
323 * Return value: (transfer none): the corresponding #GdkAtom.
328 gdk_x11_xatom_to_atom_for_display (GdkDisplay *display,
331 GdkX11Display *display_x11;
332 GdkAtom virtual_atom = GDK_NONE;
334 g_return_val_if_fail (GDK_IS_DISPLAY (display), GDK_NONE);
339 if (gdk_display_is_closed (display))
342 display_x11 = GDK_X11_DISPLAY (display);
344 if (xatom < G_N_ELEMENTS (xatoms_offset) - N_CUSTOM_PREDEFINED)
345 return INDEX_TO_ATOM (xatom);
347 if (display_x11->atom_to_virtual)
348 virtual_atom = GDK_POINTER_TO_ATOM (g_hash_table_lookup (display_x11->atom_to_virtual,
349 GUINT_TO_POINTER (xatom)));
353 /* If this atom doesn't exist, we'll die with an X error unless
354 * we take precautions
357 gdk_x11_display_error_trap_push (display);
358 name = XGetAtomName (GDK_DISPLAY_XDISPLAY (display), xatom);
359 if (gdk_x11_display_error_trap_pop (display))
361 g_warning (G_STRLOC " invalid X atom: %ld", xatom);
365 virtual_atom = gdk_atom_intern (name, FALSE);
368 insert_atom_pair (display, virtual_atom, xatom);
376 * gdk_x11_xatom_to_atom:
377 * @xatom: an X atom for the default GDK display
379 * Convert from an X atom for the default display to the corresponding
382 * Return value: (transfer none): the corresponding G#dkAtom.
385 gdk_x11_xatom_to_atom (Atom xatom)
387 return gdk_x11_xatom_to_atom_for_display (gdk_display_get_default (), xatom);
391 virtual_atom_check_init (void)
393 if (!virtual_atom_hash)
397 virtual_atom_hash = g_hash_table_new (g_str_hash, g_str_equal);
398 virtual_atom_array = g_ptr_array_new ();
400 for (i = 0; i < G_N_ELEMENTS (xatoms_offset); i++)
402 g_ptr_array_add (virtual_atom_array, (gchar *)(xatoms_string + xatoms_offset[i]));
403 g_hash_table_insert (virtual_atom_hash, (gchar *)(xatoms_string + xatoms_offset[i]),
404 GUINT_TO_POINTER (i));
410 _gdk_x11_display_manager_atom_intern (GdkDisplayManager *manager,
411 const gchar *atom_name,
416 virtual_atom_check_init ();
418 result = GDK_POINTER_TO_ATOM (g_hash_table_lookup (virtual_atom_hash, atom_name));
421 result = INDEX_TO_ATOM (virtual_atom_array->len);
423 g_ptr_array_add (virtual_atom_array, dup ? g_strdup (atom_name) : (gchar *)atom_name);
424 g_hash_table_insert (virtual_atom_hash,
425 g_ptr_array_index (virtual_atom_array,
426 ATOM_TO_INDEX (result)),
427 GDK_ATOM_TO_POINTER (result));
434 get_atom_name (GdkAtom atom)
436 virtual_atom_check_init ();
438 if (ATOM_TO_INDEX (atom) < virtual_atom_array->len)
439 return g_ptr_array_index (virtual_atom_array, ATOM_TO_INDEX (atom));
446 _gdk_x11_display_manager_get_atom_name (GdkDisplayManager *manager,
449 return g_strdup (get_atom_name (atom));
453 * gdk_x11_get_xatom_by_name_for_display:
454 * @display: a #GdkDisplay
455 * @atom_name: a string
457 * Returns the X atom for a #GdkDisplay corresponding to @atom_name.
458 * This function caches the result, so if called repeatedly it is much
459 * faster than XInternAtom(), which is a round trip to the server each time.
461 * Return value: a X atom for a #GdkDisplay
466 gdk_x11_get_xatom_by_name_for_display (GdkDisplay *display,
467 const gchar *atom_name)
469 g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
470 return gdk_x11_atom_to_xatom_for_display (display,
471 gdk_atom_intern (atom_name, FALSE));
475 * gdk_x11_get_xatom_by_name:
476 * @atom_name: a string
478 * Returns the X atom for GDK's default display corresponding to @atom_name.
479 * This function caches the result, so if called repeatedly it is much
480 * faster than XInternAtom(), which is a round trip to the server each time.
482 * Return value: a X atom for GDK's default display.
485 gdk_x11_get_xatom_by_name (const gchar *atom_name)
487 return gdk_x11_get_xatom_by_name_for_display (gdk_display_get_default (),
492 * gdk_x11_get_xatom_name_for_display:
493 * @display: the #GdkDisplay where @xatom is defined
496 * Returns the name of an X atom for its display. This
497 * function is meant mainly for debugging, so for convenience, unlike
498 * XAtomName() and gdk_atom_name(), the result doesn't need to
501 * Return value: name of the X atom; this string is owned by GDK,
502 * so it shouldn't be modifed or freed.
507 gdk_x11_get_xatom_name_for_display (GdkDisplay *display,
510 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
512 return get_atom_name (gdk_x11_xatom_to_atom_for_display (display, xatom));
516 * gdk_x11_get_xatom_name:
517 * @xatom: an X atom for GDK's default display
519 * Returns the name of an X atom for GDK's default display. This
520 * function is meant mainly for debugging, so for convenience, unlike
521 * <function>XAtomName()</function> and gdk_atom_name(), the result
522 * doesn't need to be freed. Also, this function will never return %NULL,
523 * even if @xatom is invalid.
525 * Return value: name of the X atom; this string is owned by GTK+,
526 * so it shouldn't be modifed or freed.
529 gdk_x11_get_xatom_name (Atom xatom)
531 return get_atom_name (gdk_x11_xatom_to_atom (xatom));
535 _gdk_x11_window_get_property (GdkWindow *window,
541 GdkAtom *actual_property_type,
542 gint *actual_format_type,
550 gulong ret_bytes_after;
558 g_return_val_if_fail (!window || GDK_WINDOW_IS_X11 (window), FALSE);
562 GdkScreen *screen = gdk_screen_get_default ();
563 window = gdk_screen_get_root_window (screen);
565 GDK_NOTE (MULTIHEAD, g_message ("gdk_property_get(): window is NULL\n"));
567 else if (!GDK_WINDOW_IS_X11 (window))
570 if (GDK_WINDOW_DESTROYED (window))
573 display = gdk_window_get_display (window);
574 xproperty = gdk_x11_atom_to_xatom_for_display (display, property);
575 if (type == GDK_NONE)
576 xtype = AnyPropertyType;
578 xtype = gdk_x11_atom_to_xatom_for_display (display, type);
583 * Round up length to next 4 byte value. Some code is in the (bad?)
584 * habit of passing G_MAXLONG as the length argument, causing an
585 * overflow to negative on the add. In this case, we clamp the
586 * value to G_MAXLONG.
588 get_length = length + 3;
589 if (get_length > G_MAXLONG)
590 get_length = G_MAXLONG;
592 /* To fail, either the user passed 0 or G_MAXULONG */
593 get_length = get_length / 4;
596 g_warning ("gdk_propery-get(): invalid length 0");
600 res = XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
601 GDK_WINDOW_XID (window), xproperty,
602 offset, get_length, pdelete,
603 xtype, &ret_prop_type, &ret_format,
604 &ret_nitems, &ret_bytes_after,
607 if (res != Success || (ret_prop_type == None && ret_format == 0))
612 if (actual_property_type)
613 *actual_property_type = gdk_x11_xatom_to_atom_for_display (display, ret_prop_type);
614 if (actual_format_type)
615 *actual_format_type = ret_format;
617 if ((xtype != AnyPropertyType) && (ret_prop_type != xtype))
620 g_warning ("Couldn't match property type %s to %s\n",
621 gdk_x11_get_xatom_name_for_display (display, ret_prop_type),
622 gdk_x11_get_xatom_name_for_display (display, xtype));
626 /* FIXME: ignoring bytes_after could have very bad effects */
630 if (ret_prop_type == XA_ATOM ||
631 ret_prop_type == gdk_x11_get_xatom_by_name_for_display (display, "ATOM_PAIR"))
634 * data is an array of X atom, we need to convert it
635 * to an array of GDK Atoms
638 GdkAtom *ret_atoms = g_new (GdkAtom, ret_nitems);
639 Atom *xatoms = (Atom *)ret_data;
641 *data = (guchar *)ret_atoms;
643 for (i = 0; i < ret_nitems; i++)
644 ret_atoms[i] = gdk_x11_xatom_to_atom_for_display (display, xatoms[i]);
647 *actual_length = ret_nitems * sizeof (GdkAtom);
654 ret_length = ret_nitems;
657 ret_length = sizeof(short) * ret_nitems;
660 ret_length = sizeof(long) * ret_nitems;
663 g_warning ("unknown property return format: %d", ret_format);
668 *data = g_new (guchar, ret_length);
669 memcpy (*data, ret_data, ret_length);
671 *actual_length = ret_length;
681 _gdk_x11_window_change_property (GdkWindow *window,
694 g_return_if_fail (!window || GDK_WINDOW_IS_X11 (window));
700 screen = gdk_screen_get_default ();
701 window = gdk_screen_get_root_window (screen);
703 GDK_NOTE (MULTIHEAD, g_message ("gdk_property_change(): window is NULL\n"));
705 else if (!GDK_WINDOW_IS_X11 (window))
708 if (GDK_WINDOW_DESTROYED (window))
711 gdk_window_ensure_native (window);
713 display = gdk_window_get_display (window);
714 xproperty = gdk_x11_atom_to_xatom_for_display (display, property);
715 xtype = gdk_x11_atom_to_xatom_for_display (display, type);
716 xwindow = GDK_WINDOW_XID (window);
718 if (xtype == XA_ATOM ||
719 xtype == gdk_x11_get_xatom_by_name_for_display (display, "ATOM_PAIR"))
722 * data is an array of GdkAtom, we need to convert it
723 * to an array of X Atoms
726 GdkAtom *atoms = (GdkAtom*) data;
729 xatoms = g_new (Atom, nelements);
730 for (i = 0; i < nelements; i++)
731 xatoms[i] = gdk_x11_atom_to_xatom_for_display (display, atoms[i]);
733 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), xwindow,
735 format, mode, (guchar *)xatoms, nelements);
739 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), xwindow, xproperty,
740 xtype, format, mode, (guchar *)data, nelements);
744 _gdk_x11_window_delete_property (GdkWindow *window,
747 g_return_if_fail (!window || GDK_WINDOW_IS_X11 (window));
751 GdkScreen *screen = gdk_screen_get_default ();
752 window = gdk_screen_get_root_window (screen);
755 g_message ("gdk_property_delete(): window is NULL\n"));
757 else if (!GDK_WINDOW_IS_X11 (window))
760 if (GDK_WINDOW_DESTROYED (window))
763 XDeleteProperty (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XID (window),
764 gdk_x11_atom_to_xatom_for_display (GDK_WINDOW_DISPLAY (window),