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 <X11/Xatom.h>
32 #include "gdk.h" /* For gdk_error_trap_push/pop() */
34 #include "gdkproperty.h"
35 #include "gdkprivate.h"
36 #include "gdkinternals.h"
37 #include "gdkdisplay-x11.h"
38 #include "gdkscreen-x11.h"
39 #include "gdkselection.h" /* only from predefined atom */
42 static GPtrArray *virtual_atom_array;
43 static GHashTable *virtual_atom_hash;
45 static const gchar xatoms_string[] =
46 /* These are all the standard predefined X atoms */
47 "\0" /* leave a space for None, even though it is not a predefined atom */
98 "UNDERLINE_POSITION\0"
99 "UNDERLINE_THICKNESS\0"
101 "STRIKEOUT_DESCENT\0"
116 /* Below here, these are our additions. Increment N_CUSTOM_PREDEFINED
119 "CLIPBOARD\0" /* = 69 */
122 static const gint xatoms_offset[] = {
123 0, 1, 9, 19, 23, 28, 35, 44, 53, 60, 72, 84,
124 96, 108, 120, 132, 144, 156, 165, 170, 178, 185, 189, 201,
125 218, 232, 245, 258, 274, 287, 301, 313, 320, 329, 336, 347,
126 356, 374, 387, 400, 408, 424, 438, 452, 462, 473, 483, 493,
127 507, 521, 533, 545, 564, 584, 601, 619, 632, 641, 652, 659,
128 670, 681, 691, 698, 708, 720, 730, 741, 750, 767
131 #define N_CUSTOM_PREDEFINED 1
133 #define ATOM_TO_INDEX(atom) (GPOINTER_TO_UINT(atom))
134 #define INDEX_TO_ATOM(atom) ((GdkAtom)GUINT_TO_POINTER(atom))
137 insert_atom_pair (GdkDisplay *display,
138 GdkAtom virtual_atom,
141 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
143 if (!display_x11->atom_from_virtual)
145 display_x11->atom_from_virtual = g_hash_table_new (g_direct_hash, NULL);
146 display_x11->atom_to_virtual = g_hash_table_new (g_direct_hash, NULL);
149 g_hash_table_insert (display_x11->atom_from_virtual,
150 GDK_ATOM_TO_POINTER (virtual_atom),
151 GUINT_TO_POINTER (xatom));
152 g_hash_table_insert (display_x11->atom_to_virtual,
153 GUINT_TO_POINTER (xatom),
154 GDK_ATOM_TO_POINTER (virtual_atom));
158 lookup_cached_xatom (GdkDisplay *display,
161 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
163 if (ATOM_TO_INDEX (atom) < G_N_ELEMENTS (xatoms_offset) - N_CUSTOM_PREDEFINED)
164 return ATOM_TO_INDEX (atom);
166 if (display_x11->atom_from_virtual)
167 return GPOINTER_TO_UINT (g_hash_table_lookup (display_x11->atom_from_virtual,
168 GDK_ATOM_TO_POINTER (atom)));
174 * gdk_x11_atom_to_xatom_for_display:
175 * @display: A #GdkDisplay
176 * @atom: A #GdkAtom, or %GDK_NONE
178 * Converts from a #GdkAtom to the X atom for a #GdkDisplay
179 * with the same string value. The special value %GDK_NONE
180 * is converted to %None.
182 * Return value: the X atom corresponding to @atom, or %None
187 gdk_x11_atom_to_xatom_for_display (GdkDisplay *display,
192 g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
194 if (atom == GDK_NONE)
200 xatom = lookup_cached_xatom (display, atom);
206 g_return_val_if_fail (ATOM_TO_INDEX (atom) < virtual_atom_array->len, None);
208 name = g_ptr_array_index (virtual_atom_array, ATOM_TO_INDEX (atom));
210 xatom = XInternAtom (GDK_DISPLAY_XDISPLAY (display), name, FALSE);
211 insert_atom_pair (display, atom, xatom);
218 _gdk_x11_precache_atoms (GdkDisplay *display,
219 const gchar * const *atom_names,
224 const gchar **xatom_names;
228 xatoms = g_new (Atom, n_atoms);
229 xatom_names = g_new (const gchar *, n_atoms);
230 atoms = g_new (GdkAtom, n_atoms);
233 for (i = 0; i < n_atoms; i++)
235 GdkAtom atom = gdk_atom_intern_static_string (atom_names[i]);
236 if (lookup_cached_xatom (display, atom) == None)
238 atoms[n_xatoms] = atom;
239 xatom_names[n_xatoms] = atom_names[i];
246 #ifdef HAVE_XINTERNATOMS
247 XInternAtoms (GDK_DISPLAY_XDISPLAY (display),
248 (char **)xatom_names, n_xatoms, False, xatoms);
250 for (i = 0; i < n_xatoms; i++)
251 xatoms[i] = XInternAtom (GDK_DISPLAY_XDISPLAY (display),
252 xatom_names[i], False);
256 for (i = 0; i < n_xatoms; i++)
257 insert_atom_pair (display, atoms[i], xatoms[i]);
260 g_free (xatom_names);
265 * gdk_x11_atom_to_xatom:
268 * Converts from a #GdkAtom to the X atom for the default GDK display
269 * with the same string value.
271 * Return value: the X atom corresponding to @atom.
274 gdk_x11_atom_to_xatom (GdkAtom atom)
276 return gdk_x11_atom_to_xatom_for_display (gdk_display_get_default (), atom);
280 * gdk_x11_xatom_to_atom_for_display:
281 * @display: A #GdkDisplay
284 * Convert from an X atom for a #GdkDisplay to the corresponding
287 * Return value: the corresponding #GdkAtom.
292 gdk_x11_xatom_to_atom_for_display (GdkDisplay *display,
295 GdkDisplayX11 *display_x11;
296 GdkAtom virtual_atom = GDK_NONE;
298 g_return_val_if_fail (GDK_IS_DISPLAY (display), GDK_NONE);
306 display_x11 = GDK_DISPLAY_X11 (display);
308 if (xatom < G_N_ELEMENTS (xatoms_offset) - N_CUSTOM_PREDEFINED)
309 return INDEX_TO_ATOM (xatom);
311 if (display_x11->atom_to_virtual)
312 virtual_atom = GDK_POINTER_TO_ATOM (g_hash_table_lookup (display_x11->atom_to_virtual,
313 GUINT_TO_POINTER (xatom)));
317 /* If this atom doesn't exist, we'll die with an X error unless
318 * we take precautions
321 gdk_error_trap_push ();
322 name = XGetAtomName (GDK_DISPLAY_XDISPLAY (display), xatom);
323 if (gdk_error_trap_pop ())
325 g_warning (G_STRLOC " invalid X atom: %ld", xatom);
329 virtual_atom = gdk_atom_intern (name, FALSE);
332 insert_atom_pair (display, virtual_atom, xatom);
340 * gdk_x11_xatom_to_atom:
341 * @xatom: an X atom for the default GDK display
343 * Convert from an X atom for the default display to the corresponding
346 * Return value: the corresponding G#dkAtom.
349 gdk_x11_xatom_to_atom (Atom xatom)
351 return gdk_x11_xatom_to_atom_for_display (gdk_display_get_default (), xatom);
355 virtual_atom_check_init (void)
357 if (!virtual_atom_hash)
361 virtual_atom_hash = g_hash_table_new (g_str_hash, g_str_equal);
362 virtual_atom_array = g_ptr_array_new ();
364 for (i = 0; i < G_N_ELEMENTS (xatoms_offset); i++)
366 g_ptr_array_add (virtual_atom_array, (gchar *)(xatoms_string + xatoms_offset[i]));
367 g_hash_table_insert (virtual_atom_hash, (gchar *)(xatoms_string + xatoms_offset[i]),
368 GUINT_TO_POINTER (i));
374 intern_atom (const gchar *atom_name,
379 virtual_atom_check_init ();
381 result = GDK_POINTER_TO_ATOM (g_hash_table_lookup (virtual_atom_hash, atom_name));
384 result = INDEX_TO_ATOM (virtual_atom_array->len);
386 g_ptr_array_add (virtual_atom_array, dup ? g_strdup (atom_name) : (gchar *)atom_name);
387 g_hash_table_insert (virtual_atom_hash,
388 g_ptr_array_index (virtual_atom_array,
389 ATOM_TO_INDEX (result)),
390 GDK_ATOM_TO_POINTER (result));
397 gdk_atom_intern (const gchar *atom_name,
398 gboolean only_if_exists)
400 return intern_atom (atom_name, TRUE);
404 * gdk_atom_intern_static_string:
405 * @atom_name: a static string
407 * Finds or creates an atom corresponding to a given string.
409 * Note that this function is identical to gdk_atom_intern() except
410 * that if a new #GdkAtom is created the string itself is used rather
411 * than a copy. This saves memory, but can only be used if the string
412 * will <emphasis>always</emphasis> exist. It can be used with statically
413 * allocated strings in the main program, but not with statically
414 * allocated memory in dynamically loaded modules, if you expect to
415 * ever unload the module again (e.g. do not use this function in
416 * GTK+ theme engines).
418 * Returns: the atom corresponding to @atom_name
423 gdk_atom_intern_static_string (const gchar *atom_name)
425 return intern_atom (atom_name, FALSE);
428 static G_CONST_RETURN char *
429 get_atom_name (GdkAtom atom)
431 virtual_atom_check_init ();
433 if (ATOM_TO_INDEX (atom) < virtual_atom_array->len)
434 return g_ptr_array_index (virtual_atom_array, ATOM_TO_INDEX (atom));
440 gdk_atom_name (GdkAtom atom)
442 return g_strdup (get_atom_name (atom));
446 * gdk_x11_get_xatom_by_name_for_display:
447 * @display: a #GdkDisplay
448 * @atom_name: a string
450 * Returns the X atom for a #GdkDisplay corresponding to @atom_name.
451 * This function caches the result, so if called repeatedly it is much
452 * faster than XInternAtom(), which is a round trip to the server each time.
454 * Return value: a X atom for a #GdkDisplay
459 gdk_x11_get_xatom_by_name_for_display (GdkDisplay *display,
460 const gchar *atom_name)
462 g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
463 return gdk_x11_atom_to_xatom_for_display (display,
464 gdk_atom_intern (atom_name, FALSE));
468 * gdk_x11_get_xatom_by_name:
469 * @atom_name: a string
471 * Returns the X atom for GDK's default display corresponding to @atom_name.
472 * This function caches the result, so if called repeatedly it is much
473 * faster than XInternAtom(), which is a round trip to the server each time.
475 * Return value: a X atom for GDK's default display.
478 gdk_x11_get_xatom_by_name (const gchar *atom_name)
480 return gdk_x11_get_xatom_by_name_for_display (gdk_display_get_default (),
485 * gdk_x11_get_xatom_name_for_display:
486 * @display: the #GdkDisplay where @xatom is defined
489 * Returns the name of an X atom for its display. This
490 * function is meant mainly for debugging, so for convenience, unlike
491 * XAtomName() and gdk_atom_name(), the result doesn't need to
494 * Return value: name of the X atom; this string is owned by GDK,
495 * so it shouldn't be modifed or freed.
499 G_CONST_RETURN gchar *
500 gdk_x11_get_xatom_name_for_display (GdkDisplay *display,
503 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
505 return get_atom_name (gdk_x11_xatom_to_atom_for_display (display, xatom));
509 * gdk_x11_get_xatom_name:
510 * @xatom: an X atom for GDK's default display
512 * Returns the name of an X atom for GDK's default display. This
513 * function is meant mainly for debugging, so for convenience, unlike
514 * <function>XAtomName()</function> and gdk_atom_name(), the result
515 * doesn't need to be freed. Also, this function will never return %NULL,
516 * even if @xatom is invalid.
518 * Return value: name of the X atom; this string is owned by GTK+,
519 * so it shouldn't be modifed or freed.
521 G_CONST_RETURN gchar *
522 gdk_x11_get_xatom_name (Atom xatom)
524 return get_atom_name (gdk_x11_xatom_to_atom (xatom));
528 gdk_property_get (GdkWindow *window,
534 GdkAtom *actual_property_type,
535 gint *actual_format_type,
543 gulong ret_bytes_after;
551 g_return_val_if_fail (!window || GDK_WINDOW_IS_X11 (window), FALSE);
555 GdkScreen *screen = gdk_screen_get_default ();
556 window = gdk_screen_get_root_window (screen);
558 GDK_NOTE (MULTIHEAD, g_message ("gdk_property_get(): window is NULL\n"));
560 else if (!GDK_WINDOW_IS_X11 (window))
563 if (GDK_WINDOW_DESTROYED (window))
566 display = gdk_drawable_get_display (window);
567 xproperty = gdk_x11_atom_to_xatom_for_display (display, property);
568 if (type == GDK_NONE)
569 xtype = AnyPropertyType;
571 xtype = gdk_x11_atom_to_xatom_for_display (display, type);
576 * Round up length to next 4 byte value. Some code is in the (bad?)
577 * habit of passing G_MAXLONG as the length argument, causing an
578 * overflow to negative on the add. In this case, we clamp the
579 * value to G_MAXLONG.
581 get_length = length + 3;
582 if (get_length > G_MAXLONG)
583 get_length = G_MAXLONG;
585 /* To fail, either the user passed 0 or G_MAXULONG */
586 get_length = get_length / 4;
589 g_warning ("gdk_propery-get(): invalid length 0");
593 res = XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
594 GDK_WINDOW_XWINDOW (window), xproperty,
595 offset, get_length, pdelete,
596 xtype, &ret_prop_type, &ret_format,
597 &ret_nitems, &ret_bytes_after,
600 if (res != Success || (ret_prop_type == None && ret_format == 0))
605 if (actual_property_type)
606 *actual_property_type = gdk_x11_xatom_to_atom_for_display (display, ret_prop_type);
607 if (actual_format_type)
608 *actual_format_type = ret_format;
610 if ((xtype != AnyPropertyType) && (ret_prop_type != xtype))
613 g_warning ("Couldn't match property type %s to %s\n",
614 gdk_x11_get_xatom_name_for_display (display, ret_prop_type),
615 gdk_x11_get_xatom_name_for_display (display, xtype));
619 /* FIXME: ignoring bytes_after could have very bad effects */
623 if (ret_prop_type == XA_ATOM ||
624 ret_prop_type == gdk_x11_get_xatom_by_name_for_display (display, "ATOM_PAIR"))
627 * data is an array of X atom, we need to convert it
628 * to an array of GDK Atoms
631 GdkAtom *ret_atoms = g_new (GdkAtom, ret_nitems);
632 Atom *xatoms = (Atom *)ret_data;
634 *data = (guchar *)ret_atoms;
636 for (i = 0; i < ret_nitems; i++)
637 ret_atoms[i] = gdk_x11_xatom_to_atom_for_display (display, xatoms[i]);
640 *actual_length = ret_nitems * sizeof (GdkAtom);
647 ret_length = ret_nitems;
650 ret_length = sizeof(short) * ret_nitems;
653 ret_length = sizeof(long) * ret_nitems;
656 g_warning ("unknown property return format: %d", ret_format);
661 *data = g_new (guchar, ret_length);
662 memcpy (*data, ret_data, ret_length);
664 *actual_length = ret_length;
674 gdk_property_change (GdkWindow *window,
687 g_return_if_fail (!window || GDK_WINDOW_IS_X11 (window));
693 screen = gdk_screen_get_default ();
694 window = gdk_screen_get_root_window (screen);
696 GDK_NOTE (MULTIHEAD, g_message ("gdk_property_change(): window is NULL\n"));
698 else if (!GDK_WINDOW_IS_X11 (window))
701 if (GDK_WINDOW_DESTROYED (window))
704 gdk_window_ensure_native (window);
706 display = gdk_drawable_get_display (window);
707 xproperty = gdk_x11_atom_to_xatom_for_display (display, property);
708 xtype = gdk_x11_atom_to_xatom_for_display (display, type);
709 xwindow = GDK_WINDOW_XID (window);
711 if (xtype == XA_ATOM ||
712 xtype == gdk_x11_get_xatom_by_name_for_display (display, "ATOM_PAIR"))
715 * data is an array of GdkAtom, we need to convert it
716 * to an array of X Atoms
719 GdkAtom *atoms = (GdkAtom*) data;
722 xatoms = g_new (Atom, nelements);
723 for (i = 0; i < nelements; i++)
724 xatoms[i] = gdk_x11_atom_to_xatom_for_display (display, atoms[i]);
726 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), xwindow,
728 format, mode, (guchar *)xatoms, nelements);
732 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), xwindow, xproperty,
733 xtype, format, mode, (guchar *)data, nelements);
737 gdk_property_delete (GdkWindow *window,
740 g_return_if_fail (!window || GDK_WINDOW_IS_X11 (window));
744 GdkScreen *screen = gdk_screen_get_default ();
745 window = gdk_screen_get_root_window (screen);
748 g_message ("gdk_property_delete(): window is NULL\n"));
750 else if (!GDK_WINDOW_IS_X11 (window))
753 if (GDK_WINDOW_DESTROYED (window))
756 XDeleteProperty (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XWINDOW (window),
757 gdk_x11_atom_to_xatom_for_display (GDK_WINDOW_DISPLAY (window),