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"
33 #include "gdkprivate.h"
34 #include "gdkinternals.h"
35 #include "gdkdisplay-x11.h"
36 #include "gdkscreen-x11.h"
37 #include "gdkselection.h"
40 #include <X11/Xatom.h>
44 static GPtrArray *virtual_atom_array;
45 static GHashTable *virtual_atom_hash;
47 static const gchar xatoms_string[] =
48 /* These are all the standard predefined X atoms */
49 "\0" /* leave a space for None, even though it is not a predefined atom */
100 "UNDERLINE_POSITION\0"
101 "UNDERLINE_THICKNESS\0"
103 "STRIKEOUT_DESCENT\0"
118 /* Below here, these are our additions. Increment N_CUSTOM_PREDEFINED
121 "CLIPBOARD\0" /* = 69 */
124 static const gint xatoms_offset[] = {
125 0, 1, 9, 19, 23, 28, 35, 44, 53, 60, 72, 84,
126 96, 108, 120, 132, 144, 156, 165, 170, 178, 185, 189, 201,
127 218, 232, 245, 258, 274, 287, 301, 313, 320, 329, 336, 347,
128 356, 374, 387, 400, 408, 424, 438, 452, 462, 473, 483, 493,
129 507, 521, 533, 545, 564, 584, 601, 619, 632, 641, 652, 659,
130 670, 681, 691, 698, 708, 720, 730, 741, 750, 767
133 #define N_CUSTOM_PREDEFINED 1
135 #define ATOM_TO_INDEX(atom) (GPOINTER_TO_UINT(atom))
136 #define INDEX_TO_ATOM(atom) ((GdkAtom)GUINT_TO_POINTER(atom))
139 insert_atom_pair (GdkDisplay *display,
140 GdkAtom virtual_atom,
143 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
145 if (!display_x11->atom_from_virtual)
147 display_x11->atom_from_virtual = g_hash_table_new (g_direct_hash, NULL);
148 display_x11->atom_to_virtual = g_hash_table_new (g_direct_hash, NULL);
151 g_hash_table_insert (display_x11->atom_from_virtual,
152 GDK_ATOM_TO_POINTER (virtual_atom),
153 GUINT_TO_POINTER (xatom));
154 g_hash_table_insert (display_x11->atom_to_virtual,
155 GUINT_TO_POINTER (xatom),
156 GDK_ATOM_TO_POINTER (virtual_atom));
160 lookup_cached_xatom (GdkDisplay *display,
163 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
165 if (ATOM_TO_INDEX (atom) < G_N_ELEMENTS (xatoms_offset) - N_CUSTOM_PREDEFINED)
166 return ATOM_TO_INDEX (atom);
168 if (display_x11->atom_from_virtual)
169 return GPOINTER_TO_UINT (g_hash_table_lookup (display_x11->atom_from_virtual,
170 GDK_ATOM_TO_POINTER (atom)));
176 * gdk_x11_atom_to_xatom_for_display:
177 * @display: A #GdkDisplay
178 * @atom: A #GdkAtom, or %GDK_NONE
180 * Converts from a #GdkAtom to the X atom for a #GdkDisplay
181 * with the same string value. The special value %GDK_NONE
182 * is converted to %None.
184 * Return value: the X atom corresponding to @atom, or %None
189 gdk_x11_atom_to_xatom_for_display (GdkDisplay *display,
194 g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
196 if (atom == GDK_NONE)
202 xatom = lookup_cached_xatom (display, atom);
208 g_return_val_if_fail (ATOM_TO_INDEX (atom) < virtual_atom_array->len, None);
210 name = g_ptr_array_index (virtual_atom_array, ATOM_TO_INDEX (atom));
212 xatom = XInternAtom (GDK_DISPLAY_XDISPLAY (display), name, FALSE);
213 insert_atom_pair (display, atom, xatom);
220 _gdk_x11_precache_atoms (GdkDisplay *display,
221 const gchar * const *atom_names,
226 const gchar **xatom_names;
230 xatoms = g_new (Atom, n_atoms);
231 xatom_names = g_new (const gchar *, n_atoms);
232 atoms = g_new (GdkAtom, n_atoms);
235 for (i = 0; i < n_atoms; i++)
237 GdkAtom atom = gdk_atom_intern_static_string (atom_names[i]);
238 if (lookup_cached_xatom (display, atom) == None)
240 atoms[n_xatoms] = atom;
241 xatom_names[n_xatoms] = atom_names[i];
248 #ifdef HAVE_XINTERNATOMS
249 XInternAtoms (GDK_DISPLAY_XDISPLAY (display),
250 (char **)xatom_names, n_xatoms, False, xatoms);
252 for (i = 0; i < n_xatoms; i++)
253 xatoms[i] = XInternAtom (GDK_DISPLAY_XDISPLAY (display),
254 xatom_names[i], False);
258 for (i = 0; i < n_xatoms; i++)
259 insert_atom_pair (display, atoms[i], xatoms[i]);
262 g_free (xatom_names);
267 * gdk_x11_atom_to_xatom:
270 * Converts from a #GdkAtom to the X atom for the default GDK display
271 * with the same string value.
273 * Return value: the X atom corresponding to @atom.
276 gdk_x11_atom_to_xatom (GdkAtom atom)
278 return gdk_x11_atom_to_xatom_for_display (gdk_display_get_default (), atom);
282 * gdk_x11_xatom_to_atom_for_display:
283 * @display: A #GdkDisplay
286 * Convert from an X atom for a #GdkDisplay to the corresponding
289 * Return value: the corresponding #GdkAtom.
294 gdk_x11_xatom_to_atom_for_display (GdkDisplay *display,
297 GdkDisplayX11 *display_x11;
298 GdkAtom virtual_atom = GDK_NONE;
300 g_return_val_if_fail (GDK_IS_DISPLAY (display), GDK_NONE);
308 display_x11 = GDK_DISPLAY_X11 (display);
310 if (xatom < G_N_ELEMENTS (xatoms_offset) - N_CUSTOM_PREDEFINED)
311 return INDEX_TO_ATOM (xatom);
313 if (display_x11->atom_to_virtual)
314 virtual_atom = GDK_POINTER_TO_ATOM (g_hash_table_lookup (display_x11->atom_to_virtual,
315 GUINT_TO_POINTER (xatom)));
319 /* If this atom doesn't exist, we'll die with an X error unless
320 * we take precautions
323 gdk_error_trap_push ();
324 name = XGetAtomName (GDK_DISPLAY_XDISPLAY (display), xatom);
325 if (gdk_error_trap_pop ())
327 g_warning (G_STRLOC " invalid X atom: %ld", xatom);
331 virtual_atom = gdk_atom_intern (name, FALSE);
334 insert_atom_pair (display, virtual_atom, xatom);
342 * gdk_x11_xatom_to_atom:
343 * @xatom: an X atom for the default GDK display
345 * Convert from an X atom for the default display to the corresponding
348 * Return value: the corresponding G#dkAtom.
351 gdk_x11_xatom_to_atom (Atom xatom)
353 return gdk_x11_xatom_to_atom_for_display (gdk_display_get_default (), xatom);
357 virtual_atom_check_init (void)
359 if (!virtual_atom_hash)
363 virtual_atom_hash = g_hash_table_new (g_str_hash, g_str_equal);
364 virtual_atom_array = g_ptr_array_new ();
366 for (i = 0; i < G_N_ELEMENTS (xatoms_offset); i++)
368 g_ptr_array_add (virtual_atom_array, (gchar *)(xatoms_string + xatoms_offset[i]));
369 g_hash_table_insert (virtual_atom_hash, (gchar *)(xatoms_string + xatoms_offset[i]),
370 GUINT_TO_POINTER (i));
376 intern_atom (const gchar *atom_name,
381 virtual_atom_check_init ();
383 result = GDK_POINTER_TO_ATOM (g_hash_table_lookup (virtual_atom_hash, atom_name));
386 result = INDEX_TO_ATOM (virtual_atom_array->len);
388 g_ptr_array_add (virtual_atom_array, dup ? g_strdup (atom_name) : (gchar *)atom_name);
389 g_hash_table_insert (virtual_atom_hash,
390 g_ptr_array_index (virtual_atom_array,
391 ATOM_TO_INDEX (result)),
392 GDK_ATOM_TO_POINTER (result));
399 gdk_atom_intern (const gchar *atom_name,
400 gboolean only_if_exists)
402 return intern_atom (atom_name, TRUE);
406 * gdk_atom_intern_static_string:
407 * @atom_name: a static string
409 * Finds or creates an atom corresponding to a given string.
411 * Note that this function is identical to gdk_atom_intern() except
412 * that if a new #GdkAtom is created the string itself is used rather
413 * than a copy. This saves memory, but can only be used if the string
414 * will <emphasis>always</emphasis> exist. It can be used with statically
415 * allocated strings in the main program, but not with statically
416 * allocated memory in dynamically loaded modules, if you expect to
417 * ever unload the module again (e.g. do not use this function in
418 * GTK+ theme engines).
420 * Returns: the atom corresponding to @atom_name
425 gdk_atom_intern_static_string (const gchar *atom_name)
427 return intern_atom (atom_name, FALSE);
430 static G_CONST_RETURN char *
431 get_atom_name (GdkAtom atom)
433 virtual_atom_check_init ();
435 if (ATOM_TO_INDEX (atom) < virtual_atom_array->len)
436 return g_ptr_array_index (virtual_atom_array, ATOM_TO_INDEX (atom));
442 gdk_atom_name (GdkAtom atom)
444 return g_strdup (get_atom_name (atom));
448 * gdk_x11_get_xatom_by_name_for_display:
449 * @display: a #GdkDisplay
450 * @atom_name: a string
452 * Returns the X atom for a #GdkDisplay corresponding to @atom_name.
453 * This function caches the result, so if called repeatedly it is much
454 * faster than XInternAtom(), which is a round trip to the server each time.
456 * Return value: a X atom for a #GdkDisplay
461 gdk_x11_get_xatom_by_name_for_display (GdkDisplay *display,
462 const gchar *atom_name)
464 g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
465 return gdk_x11_atom_to_xatom_for_display (display,
466 gdk_atom_intern (atom_name, FALSE));
470 * gdk_x11_get_xatom_by_name:
471 * @atom_name: a string
473 * Returns the X atom for GDK's default display corresponding to @atom_name.
474 * This function caches the result, so if called repeatedly it is much
475 * faster than XInternAtom(), which is a round trip to the server each time.
477 * Return value: a X atom for GDK's default display.
480 gdk_x11_get_xatom_by_name (const gchar *atom_name)
482 return gdk_x11_get_xatom_by_name_for_display (gdk_display_get_default (),
487 * gdk_x11_get_xatom_name_for_display:
488 * @display: the #GdkDisplay where @xatom is defined
491 * Returns the name of an X atom for its display. This
492 * function is meant mainly for debugging, so for convenience, unlike
493 * XAtomName() and gdk_atom_name(), the result doesn't need to
496 * Return value: name of the X atom; this string is owned by GDK,
497 * so it shouldn't be modifed or freed.
501 G_CONST_RETURN gchar *
502 gdk_x11_get_xatom_name_for_display (GdkDisplay *display,
505 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
507 return get_atom_name (gdk_x11_xatom_to_atom_for_display (display, xatom));
511 * gdk_x11_get_xatom_name:
512 * @xatom: an X atom for GDK's default display
514 * Returns the name of an X atom for GDK's default display. This
515 * function is meant mainly for debugging, so for convenience, unlike
516 * <function>XAtomName()</function> and gdk_atom_name(), the result
517 * doesn't need to be freed. Also, this function will never return %NULL,
518 * even if @xatom is invalid.
520 * Return value: name of the X atom; this string is owned by GTK+,
521 * so it shouldn't be modifed or freed.
523 G_CONST_RETURN gchar *
524 gdk_x11_get_xatom_name (Atom xatom)
526 return get_atom_name (gdk_x11_xatom_to_atom (xatom));
530 gdk_property_get (GdkWindow *window,
536 GdkAtom *actual_property_type,
537 gint *actual_format_type,
545 gulong ret_bytes_after;
553 g_return_val_if_fail (!window || GDK_WINDOW_IS_X11 (window), FALSE);
557 GdkScreen *screen = gdk_screen_get_default ();
558 window = gdk_screen_get_root_window (screen);
560 GDK_NOTE (MULTIHEAD, g_message ("gdk_property_get(): window is NULL\n"));
562 else if (!GDK_WINDOW_IS_X11 (window))
565 if (GDK_WINDOW_DESTROYED (window))
568 display = gdk_window_get_display (window);
569 xproperty = gdk_x11_atom_to_xatom_for_display (display, property);
570 if (type == GDK_NONE)
571 xtype = AnyPropertyType;
573 xtype = gdk_x11_atom_to_xatom_for_display (display, type);
578 * Round up length to next 4 byte value. Some code is in the (bad?)
579 * habit of passing G_MAXLONG as the length argument, causing an
580 * overflow to negative on the add. In this case, we clamp the
581 * value to G_MAXLONG.
583 get_length = length + 3;
584 if (get_length > G_MAXLONG)
585 get_length = G_MAXLONG;
587 /* To fail, either the user passed 0 or G_MAXULONG */
588 get_length = get_length / 4;
591 g_warning ("gdk_propery-get(): invalid length 0");
595 res = XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
596 GDK_WINDOW_XWINDOW (window), xproperty,
597 offset, get_length, pdelete,
598 xtype, &ret_prop_type, &ret_format,
599 &ret_nitems, &ret_bytes_after,
602 if (res != Success || (ret_prop_type == None && ret_format == 0))
607 if (actual_property_type)
608 *actual_property_type = gdk_x11_xatom_to_atom_for_display (display, ret_prop_type);
609 if (actual_format_type)
610 *actual_format_type = ret_format;
612 if ((xtype != AnyPropertyType) && (ret_prop_type != xtype))
615 g_warning ("Couldn't match property type %s to %s\n",
616 gdk_x11_get_xatom_name_for_display (display, ret_prop_type),
617 gdk_x11_get_xatom_name_for_display (display, xtype));
621 /* FIXME: ignoring bytes_after could have very bad effects */
625 if (ret_prop_type == XA_ATOM ||
626 ret_prop_type == gdk_x11_get_xatom_by_name_for_display (display, "ATOM_PAIR"))
629 * data is an array of X atom, we need to convert it
630 * to an array of GDK Atoms
633 GdkAtom *ret_atoms = g_new (GdkAtom, ret_nitems);
634 Atom *xatoms = (Atom *)ret_data;
636 *data = (guchar *)ret_atoms;
638 for (i = 0; i < ret_nitems; i++)
639 ret_atoms[i] = gdk_x11_xatom_to_atom_for_display (display, xatoms[i]);
642 *actual_length = ret_nitems * sizeof (GdkAtom);
649 ret_length = ret_nitems;
652 ret_length = sizeof(short) * ret_nitems;
655 ret_length = sizeof(long) * ret_nitems;
658 g_warning ("unknown property return format: %d", ret_format);
663 *data = g_new (guchar, ret_length);
664 memcpy (*data, ret_data, ret_length);
666 *actual_length = ret_length;
676 gdk_property_change (GdkWindow *window,
689 g_return_if_fail (!window || GDK_WINDOW_IS_X11 (window));
695 screen = gdk_screen_get_default ();
696 window = gdk_screen_get_root_window (screen);
698 GDK_NOTE (MULTIHEAD, g_message ("gdk_property_change(): window is NULL\n"));
700 else if (!GDK_WINDOW_IS_X11 (window))
703 if (GDK_WINDOW_DESTROYED (window))
706 gdk_window_ensure_native (window);
708 display = gdk_window_get_display (window);
709 xproperty = gdk_x11_atom_to_xatom_for_display (display, property);
710 xtype = gdk_x11_atom_to_xatom_for_display (display, type);
711 xwindow = GDK_WINDOW_XID (window);
713 if (xtype == XA_ATOM ||
714 xtype == gdk_x11_get_xatom_by_name_for_display (display, "ATOM_PAIR"))
717 * data is an array of GdkAtom, we need to convert it
718 * to an array of X Atoms
721 GdkAtom *atoms = (GdkAtom*) data;
724 xatoms = g_new (Atom, nelements);
725 for (i = 0; i < nelements; i++)
726 xatoms[i] = gdk_x11_atom_to_xatom_for_display (display, atoms[i]);
728 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), xwindow,
730 format, mode, (guchar *)xatoms, nelements);
734 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), xwindow, xproperty,
735 xtype, format, mode, (guchar *)data, nelements);
739 gdk_property_delete (GdkWindow *window,
742 g_return_if_fail (!window || GDK_WINDOW_IS_X11 (window));
746 GdkScreen *screen = gdk_screen_get_default ();
747 window = gdk_screen_get_root_window (screen);
750 g_message ("gdk_property_delete(): window is NULL\n"));
752 else if (!GDK_WINDOW_IS_X11 (window))
755 if (GDK_WINDOW_DESTROYED (window))
758 XDeleteProperty (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XWINDOW (window),
759 gdk_x11_atom_to_xatom_for_display (GDK_WINDOW_DISPLAY (window),