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/.
28 #include <X11/Xatom.h>
31 #include "gdk.h" /* For gdk_error_trap_push/pop() */
33 #include "gdkproperty.h"
34 #include "gdkprivate.h"
35 #include "gdkinternals.h"
36 #include "gdkdisplay-x11.h"
37 #include "gdkscreen-x11.h"
38 #include "gdkselection.h" /* only from predefined atom */
40 static GPtrArray *virtual_atom_array;
41 static GHashTable *virtual_atom_hash;
43 static gchar *XAtomsStrings[] = {
44 /* These are all the standard predefined X atoms */
97 "UNDERLINE_THICKNESS",
114 /* Below here, these are our additions. Increment N_CUSTOM_PREDEFINED
117 "CLIPBOARD" /* = 69 */
120 #define N_CUSTOM_PREDEFINED 1
122 #define ATOM_TO_INDEX(atom) (GPOINTER_TO_UINT(atom))
123 #define INDEX_TO_ATOM(atom) ((GdkAtom)GUINT_TO_POINTER(atom))
126 insert_atom_pair (GdkDisplay *display,
127 GdkAtom virtual_atom,
130 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
132 if (!display_x11->atom_from_virtual)
134 display_x11->atom_from_virtual = g_hash_table_new (g_direct_hash, NULL);
135 display_x11->atom_to_virtual = g_hash_table_new (g_direct_hash, NULL);
138 g_hash_table_insert (display_x11->atom_from_virtual,
139 GDK_ATOM_TO_POINTER (virtual_atom),
140 GUINT_TO_POINTER (xatom));
141 g_hash_table_insert (display_x11->atom_to_virtual,
142 GUINT_TO_POINTER (xatom),
143 GDK_ATOM_TO_POINTER (virtual_atom));
147 lookup_cached_xatom (GdkDisplay *display,
150 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
152 if (ATOM_TO_INDEX (atom) < G_N_ELEMENTS (XAtomsStrings) - N_CUSTOM_PREDEFINED)
153 return ATOM_TO_INDEX (atom);
155 if (display_x11->atom_from_virtual)
156 return GPOINTER_TO_UINT (g_hash_table_lookup (display_x11->atom_from_virtual,
157 GDK_ATOM_TO_POINTER (atom)));
163 * gdk_x11_atom_to_xatom_for_display:
164 * @display: A #GdkDisplay
167 * Converts from a #GdkAtom to the X atom for a #GdkDisplay
168 * with the same string value.
170 * Return value: the X atom corresponding to @atom.
175 gdk_x11_atom_to_xatom_for_display (GdkDisplay *display,
180 g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
185 xatom = lookup_cached_xatom (display, atom);
191 g_return_val_if_fail (ATOM_TO_INDEX (atom) < virtual_atom_array->len, None);
193 name = g_ptr_array_index (virtual_atom_array, ATOM_TO_INDEX (atom));
195 xatom = XInternAtom (GDK_DISPLAY_XDISPLAY (display), name, FALSE);
196 insert_atom_pair (display, atom, xatom);
203 _gdk_x11_precache_atoms (GdkDisplay *display,
204 const gchar * const *atom_names,
209 const gchar **xatom_names;
213 xatoms = g_new (Atom, n_atoms);
214 xatom_names = g_new (const gchar *, n_atoms);
215 atoms = g_new (GdkAtom, n_atoms);
218 for (i = 0; i < n_atoms; i++)
220 GdkAtom atom = gdk_atom_intern (atom_names[i], FALSE);
221 if (lookup_cached_xatom (display, atom) == None)
223 atoms[n_xatoms] = atom;
224 xatom_names[n_xatoms] = atom_names[i];
230 XInternAtoms (GDK_DISPLAY_XDISPLAY (display),
231 (char **)xatom_names, n_xatoms, False, xatoms);
233 for (i = 0; i < n_xatoms; i++)
234 insert_atom_pair (display, atoms[i], xatoms[i]);
242 * gdk_x11_atom_to_xatom:
245 * Converts from a #GdkAtom to the X atom for the default GDK display
246 * with the same string value.
248 * Return value: the X atom corresponding to @atom.
251 gdk_x11_atom_to_xatom (GdkAtom atom)
253 return gdk_x11_atom_to_xatom_for_display (gdk_display_get_default (), atom);
257 * gdk_x11_xatom_to_atom_for_display:
258 * @display: A #GdkDisplay
261 * Convert from an X atom for a #GdkDisplay to the corresponding
264 * Return value: the corresponding #GdkAtom.
269 gdk_x11_xatom_to_atom_for_display (GdkDisplay *display,
272 GdkDisplayX11 *display_x11;
273 GdkAtom virtual_atom = GDK_NONE;
275 g_return_val_if_fail (GDK_IS_DISPLAY (display), GDK_NONE);
280 display_x11 = GDK_DISPLAY_X11 (display);
282 if (xatom < G_N_ELEMENTS (XAtomsStrings) - N_CUSTOM_PREDEFINED)
283 return INDEX_TO_ATOM (xatom);
285 if (display_x11->atom_to_virtual)
286 virtual_atom = GDK_POINTER_TO_ATOM (g_hash_table_lookup (display_x11->atom_to_virtual,
287 GUINT_TO_POINTER (xatom)));
291 /* If this atom doesn't exist, we'll die with an X error unless
292 * we take precautions
295 gdk_error_trap_push ();
296 name = XGetAtomName (GDK_DISPLAY_XDISPLAY (display), xatom);
297 if (gdk_error_trap_pop ())
299 g_warning (G_STRLOC " invalid X atom: %ld", xatom);
303 virtual_atom = gdk_atom_intern (name, FALSE);
306 insert_atom_pair (display, virtual_atom, xatom);
314 * gdk_x11_xatom_to_atom:
315 * @xatom: an X atom for the default GDK display
317 * Convert from an X atom for the default display to the corresponding
320 * Return value: the corresponding G#dkAtom.
323 gdk_x11_xatom_to_atom (Atom xatom)
325 return gdk_x11_xatom_to_atom_for_display (gdk_display_get_default (), xatom);
329 virtual_atom_check_init (void)
331 if (!virtual_atom_hash)
335 virtual_atom_hash = g_hash_table_new (g_str_hash, g_str_equal);
336 virtual_atom_array = g_ptr_array_new ();
338 for (i = 0; i < G_N_ELEMENTS (XAtomsStrings); i++)
340 g_ptr_array_add (virtual_atom_array, XAtomsStrings[i]);
341 g_hash_table_insert (virtual_atom_hash, XAtomsStrings[i],
342 GUINT_TO_POINTER (i));
348 gdk_atom_intern (const gchar *atom_name,
349 gboolean only_if_exists)
353 virtual_atom_check_init ();
355 result = GDK_POINTER_TO_ATOM (g_hash_table_lookup (virtual_atom_hash, atom_name));
358 result = INDEX_TO_ATOM (virtual_atom_array->len);
360 g_ptr_array_add (virtual_atom_array, g_strdup (atom_name));
361 g_hash_table_insert (virtual_atom_hash,
362 g_ptr_array_index (virtual_atom_array,
363 ATOM_TO_INDEX (result)),
364 GDK_ATOM_TO_POINTER (result));
370 static G_CONST_RETURN char *
371 get_atom_name (GdkAtom atom)
373 virtual_atom_check_init ();
375 if (ATOM_TO_INDEX (atom) < virtual_atom_array->len)
376 return g_ptr_array_index (virtual_atom_array, ATOM_TO_INDEX (atom));
382 gdk_atom_name (GdkAtom atom)
384 return g_strdup (get_atom_name (atom));
388 * gdk_x11_get_xatom_by_name_for_display:
389 * @display: a #GdkDisplay
390 * @atom_name: a string
392 * Returns the X atom for a #GdkDisplay corresponding to @atom_name.
393 * This function caches the result, so if called repeatedly it is much
394 * faster than XInternAtom(), which is a round trip to the server each time.
396 * Return value: a X atom for a #GdkDisplay
401 gdk_x11_get_xatom_by_name_for_display (GdkDisplay *display,
402 const gchar *atom_name)
404 g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
405 return gdk_x11_atom_to_xatom_for_display (display,
406 gdk_atom_intern (atom_name, FALSE));
410 * gdk_x11_get_xatom_by_name:
411 * @atom_name: a string
413 * Returns the X atom for GDK's default display corresponding to @atom_name.
414 * This function caches the result, so if called repeatedly it is much
415 * faster than <function>XInternAtom()</function>, which is a round trip to
416 * the server each time.
418 * Return value: a X atom for GDK's default display.
421 gdk_x11_get_xatom_by_name (const gchar *atom_name)
423 return gdk_x11_get_xatom_by_name_for_display (gdk_display_get_default (),
428 * gdk_x11_get_xatom_name_for_display:
429 * @display: the #GdkDisplay where @xatom is defined
432 * Returns the name of an X atom for its display. This
433 * function is meant mainly for debugging, so for convenience, unlike
434 * XAtomName() and gdk_atom_name(), the result doesn't need to
437 * Return value: name of the X atom; this string is owned by GDK,
438 * so it shouldn't be modifed or freed.
442 G_CONST_RETURN gchar *
443 gdk_x11_get_xatom_name_for_display (GdkDisplay *display,
446 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
448 return get_atom_name (gdk_x11_xatom_to_atom_for_display (display, xatom));
452 * gdk_x11_get_xatom_name:
453 * @xatom: an X atom for GDK's default display
455 * Returns the name of an X atom for GDK's default display. This
456 * function is meant mainly for debugging, so for convenience, unlike
457 * <function>XAtomName()</function> and gdk_atom_name(), the result
458 * doesn't need to be freed. Also, this function will never return %NULL,
459 * even if @xatom is invalid.
461 * Return value: name of the X atom; this string is owned by GTK+,
462 * so it shouldn't be modifed or freed.
464 G_CONST_RETURN gchar *
465 gdk_x11_get_xatom_name (Atom xatom)
467 return get_atom_name (gdk_x11_xatom_to_atom (xatom));
471 gdk_property_get (GdkWindow *window,
477 GdkAtom *actual_property_type,
478 gint *actual_format_type,
486 gulong ret_bytes_after;
493 g_return_val_if_fail (!window || GDK_IS_WINDOW (window), FALSE);
497 GdkScreen *screen = gdk_screen_get_default ();
498 window = gdk_screen_get_root_window (screen);
500 GDK_NOTE (MULTIHEAD, g_message ("gdk_property_get(): window is NULL\n"));
503 if (GDK_WINDOW_DESTROYED (window))
506 display = gdk_drawable_get_display (window);
507 xproperty = gdk_x11_atom_to_xatom_for_display (display, property);
508 xtype = gdk_x11_atom_to_xatom_for_display (display, type);
512 res = XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
513 GDK_WINDOW_XWINDOW (window), xproperty,
514 offset, (length + 3) / 4, pdelete,
515 xtype, &ret_prop_type, &ret_format,
516 &ret_nitems, &ret_bytes_after,
519 if (res != Success || (ret_prop_type == None && ret_format == 0))
524 if (actual_property_type)
525 *actual_property_type = gdk_x11_xatom_to_atom_for_display (display, ret_prop_type);
526 if (actual_format_type)
527 *actual_format_type = ret_format;
529 if ((xtype != AnyPropertyType) && (ret_prop_type != xtype))
532 g_warning ("Couldn't match property type %s to %s\n",
533 gdk_x11_get_xatom_name_for_display (display, ret_prop_type),
534 gdk_x11_get_xatom_name_for_display (display, xtype));
538 /* FIXME: ignoring bytes_after could have very bad effects */
542 if (ret_prop_type == XA_ATOM ||
543 ret_prop_type == gdk_x11_get_xatom_by_name_for_display (display, "ATOM_PAIR"))
546 * data is an array of X atom, we need to convert it
547 * to an array of GDK Atoms
550 GdkAtom *ret_atoms = g_new (GdkAtom, ret_nitems);
551 Atom *xatoms = (Atom *)ret_data;
553 *data = (guchar *)ret_atoms;
555 for (i = 0; i < ret_nitems; i++)
556 ret_atoms[i] = gdk_x11_xatom_to_atom_for_display (display, xatoms[i]);
559 *actual_length = ret_nitems * sizeof (GdkAtom);
566 ret_length = ret_nitems;
569 ret_length = sizeof(short) * ret_nitems;
572 ret_length = sizeof(long) * ret_nitems;
575 g_warning ("unknown property return format: %d", ret_format);
580 *data = g_new (guchar, ret_length);
581 memcpy (*data, ret_data, ret_length);
583 *actual_length = ret_length;
593 gdk_property_change (GdkWindow *window,
606 g_return_if_fail (!window || GDK_IS_WINDOW (window));
612 screen = gdk_screen_get_default ();
613 window = gdk_screen_get_root_window (screen);
615 GDK_NOTE (MULTIHEAD, g_message ("gdk_property_delete(): window is NULL\n"));
619 if (GDK_WINDOW_DESTROYED (window))
622 display = gdk_drawable_get_display (window);
624 xproperty = gdk_x11_atom_to_xatom_for_display (display, property);
625 xtype = gdk_x11_atom_to_xatom_for_display (display, type);
626 xwindow = GDK_WINDOW_XID (window);
628 if (xtype == XA_ATOM ||
629 xtype == gdk_x11_get_xatom_by_name_for_display (display, "ATOM_PAIR"))
632 * data is an array of GdkAtom, we need to convert it
633 * to an array of X Atoms
636 GdkAtom *atoms = (GdkAtom*) data;
639 xatoms = g_new (Atom, nelements);
640 for (i = 0; i < nelements; i++)
641 xatoms[i] = gdk_x11_atom_to_xatom_for_display (display, atoms[i]);
643 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), xwindow,
645 format, mode, (guchar *)xatoms, nelements);
649 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), xwindow, xproperty,
650 xtype, format, mode, (guchar *)data, nelements);
654 gdk_property_delete (GdkWindow *window,
657 g_return_if_fail (!window || GDK_IS_WINDOW (window));
661 GdkScreen *screen = gdk_screen_get_default ();
662 window = gdk_screen_get_root_window (screen);
665 g_message ("gdk_property_delete(): window is NULL\n"));
668 if (GDK_WINDOW_DESTROYED (window))
671 XDeleteProperty (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XWINDOW (window),
672 gdk_x11_atom_to_xatom_for_display (GDK_WINDOW_DISPLAY (window),