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>
33 #include "gdk.h" /* For gdk_error_trap_push/pop() */
35 #include "gdkproperty.h"
36 #include "gdkprivate.h"
37 #include "gdkinternals.h"
38 #include "gdkdisplay-x11.h"
39 #include "gdkscreen-x11.h"
40 #include "gdkselection.h" /* only from predefined atom */
42 static GPtrArray *virtual_atom_array;
43 static GHashTable *virtual_atom_hash;
45 static const gchar *const XAtomsStrings[] = {
46 /* These are all the standard predefined X atoms */
99 "UNDERLINE_THICKNESS",
116 /* Below here, these are our additions. Increment N_CUSTOM_PREDEFINED
119 "CLIPBOARD" /* = 69 */
122 #define N_CUSTOM_PREDEFINED 1
124 #define ATOM_TO_INDEX(atom) (GPOINTER_TO_UINT(atom))
125 #define INDEX_TO_ATOM(atom) ((GdkAtom)GUINT_TO_POINTER(atom))
128 insert_atom_pair (GdkDisplay *display,
129 GdkAtom virtual_atom,
132 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
134 if (!display_x11->atom_from_virtual)
136 display_x11->atom_from_virtual = g_hash_table_new (g_direct_hash, NULL);
137 display_x11->atom_to_virtual = g_hash_table_new (g_direct_hash, NULL);
140 g_hash_table_insert (display_x11->atom_from_virtual,
141 GDK_ATOM_TO_POINTER (virtual_atom),
142 GUINT_TO_POINTER (xatom));
143 g_hash_table_insert (display_x11->atom_to_virtual,
144 GUINT_TO_POINTER (xatom),
145 GDK_ATOM_TO_POINTER (virtual_atom));
149 lookup_cached_xatom (GdkDisplay *display,
152 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
154 if (ATOM_TO_INDEX (atom) < G_N_ELEMENTS (XAtomsStrings) - N_CUSTOM_PREDEFINED)
155 return ATOM_TO_INDEX (atom);
157 if (display_x11->atom_from_virtual)
158 return GPOINTER_TO_UINT (g_hash_table_lookup (display_x11->atom_from_virtual,
159 GDK_ATOM_TO_POINTER (atom)));
165 * gdk_x11_atom_to_xatom_for_display:
166 * @display: A #GdkDisplay
169 * Converts from a #GdkAtom to the X atom for a #GdkDisplay
170 * with the same string value.
172 * Return value: the X atom corresponding to @atom.
177 gdk_x11_atom_to_xatom_for_display (GdkDisplay *display,
182 g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
187 xatom = lookup_cached_xatom (display, atom);
193 g_return_val_if_fail (ATOM_TO_INDEX (atom) < virtual_atom_array->len, None);
195 name = g_ptr_array_index (virtual_atom_array, ATOM_TO_INDEX (atom));
197 xatom = XInternAtom (GDK_DISPLAY_XDISPLAY (display), name, FALSE);
198 insert_atom_pair (display, atom, xatom);
205 _gdk_x11_precache_atoms (GdkDisplay *display,
206 const gchar * const *atom_names,
211 const gchar **xatom_names;
215 xatoms = g_new (Atom, n_atoms);
216 xatom_names = g_new (const gchar *, n_atoms);
217 atoms = g_new (GdkAtom, n_atoms);
220 for (i = 0; i < n_atoms; i++)
222 GdkAtom atom = gdk_atom_intern (atom_names[i], FALSE);
223 if (lookup_cached_xatom (display, atom) == None)
225 atoms[n_xatoms] = atom;
226 xatom_names[n_xatoms] = atom_names[i];
233 #ifdef HAVE_XINTERNATOMS
234 XInternAtoms (GDK_DISPLAY_XDISPLAY (display),
235 (char **)xatom_names, n_xatoms, False, xatoms);
237 for (i = 0; i < n_xatoms; i++)
238 xatoms[i] = XInternAtom (GDK_DISPLAY_XDISPLAY (display),
239 xatom_names[i], False);
243 for (i = 0; i < n_xatoms; i++)
244 insert_atom_pair (display, atoms[i], xatoms[i]);
247 g_free (xatom_names);
252 * gdk_x11_atom_to_xatom:
255 * Converts from a #GdkAtom to the X atom for the default GDK display
256 * with the same string value.
258 * Return value: the X atom corresponding to @atom.
261 gdk_x11_atom_to_xatom (GdkAtom atom)
263 return gdk_x11_atom_to_xatom_for_display (gdk_display_get_default (), atom);
267 * gdk_x11_xatom_to_atom_for_display:
268 * @display: A #GdkDisplay
271 * Convert from an X atom for a #GdkDisplay to the corresponding
274 * Return value: the corresponding #GdkAtom.
279 gdk_x11_xatom_to_atom_for_display (GdkDisplay *display,
282 GdkDisplayX11 *display_x11;
283 GdkAtom virtual_atom = GDK_NONE;
285 g_return_val_if_fail (GDK_IS_DISPLAY (display), GDK_NONE);
290 display_x11 = GDK_DISPLAY_X11 (display);
292 if (xatom < G_N_ELEMENTS (XAtomsStrings) - N_CUSTOM_PREDEFINED)
293 return INDEX_TO_ATOM (xatom);
295 if (display_x11->atom_to_virtual)
296 virtual_atom = GDK_POINTER_TO_ATOM (g_hash_table_lookup (display_x11->atom_to_virtual,
297 GUINT_TO_POINTER (xatom)));
301 /* If this atom doesn't exist, we'll die with an X error unless
302 * we take precautions
305 gdk_error_trap_push ();
306 name = XGetAtomName (GDK_DISPLAY_XDISPLAY (display), xatom);
307 if (gdk_error_trap_pop ())
309 g_warning (G_STRLOC " invalid X atom: %ld", xatom);
313 virtual_atom = gdk_atom_intern (name, FALSE);
316 insert_atom_pair (display, virtual_atom, xatom);
324 * gdk_x11_xatom_to_atom:
325 * @xatom: an X atom for the default GDK display
327 * Convert from an X atom for the default display to the corresponding
330 * Return value: the corresponding G#dkAtom.
333 gdk_x11_xatom_to_atom (Atom xatom)
335 return gdk_x11_xatom_to_atom_for_display (gdk_display_get_default (), xatom);
339 virtual_atom_check_init (void)
341 if (!virtual_atom_hash)
345 virtual_atom_hash = g_hash_table_new (g_str_hash, g_str_equal);
346 virtual_atom_array = g_ptr_array_new ();
348 for (i = 0; i < G_N_ELEMENTS (XAtomsStrings); i++)
350 g_ptr_array_add (virtual_atom_array, (gchar *) XAtomsStrings[i]);
351 g_hash_table_insert (virtual_atom_hash, (gchar *) XAtomsStrings[i],
352 GUINT_TO_POINTER (i));
358 gdk_atom_intern (const gchar *atom_name,
359 gboolean only_if_exists)
363 virtual_atom_check_init ();
365 result = GDK_POINTER_TO_ATOM (g_hash_table_lookup (virtual_atom_hash, atom_name));
368 result = INDEX_TO_ATOM (virtual_atom_array->len);
370 g_ptr_array_add (virtual_atom_array, g_strdup (atom_name));
371 g_hash_table_insert (virtual_atom_hash,
372 g_ptr_array_index (virtual_atom_array,
373 ATOM_TO_INDEX (result)),
374 GDK_ATOM_TO_POINTER (result));
380 static G_CONST_RETURN char *
381 get_atom_name (GdkAtom atom)
383 virtual_atom_check_init ();
385 if (ATOM_TO_INDEX (atom) < virtual_atom_array->len)
386 return g_ptr_array_index (virtual_atom_array, ATOM_TO_INDEX (atom));
392 gdk_atom_name (GdkAtom atom)
394 return g_strdup (get_atom_name (atom));
398 * gdk_x11_get_xatom_by_name_for_display:
399 * @display: a #GdkDisplay
400 * @atom_name: a string
402 * Returns the X atom for a #GdkDisplay corresponding to @atom_name.
403 * This function caches the result, so if called repeatedly it is much
404 * faster than XInternAtom(), which is a round trip to the server each time.
406 * Return value: a X atom for a #GdkDisplay
411 gdk_x11_get_xatom_by_name_for_display (GdkDisplay *display,
412 const gchar *atom_name)
414 g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
415 return gdk_x11_atom_to_xatom_for_display (display,
416 gdk_atom_intern (atom_name, FALSE));
420 * gdk_x11_get_xatom_by_name:
421 * @atom_name: a string
423 * Returns the X atom for GDK's default display corresponding to @atom_name.
424 * This function caches the result, so if called repeatedly it is much
425 * faster than XInternAtom(), which is a round trip to the server each time.
427 * Return value: a X atom for GDK's default display.
430 gdk_x11_get_xatom_by_name (const gchar *atom_name)
432 return gdk_x11_get_xatom_by_name_for_display (gdk_display_get_default (),
437 * gdk_x11_get_xatom_name_for_display:
438 * @display: the #GdkDisplay where @xatom is defined
441 * Returns the name of an X atom for its display. This
442 * function is meant mainly for debugging, so for convenience, unlike
443 * XAtomName() and gdk_atom_name(), the result doesn't need to
446 * Return value: name of the X atom; this string is owned by GDK,
447 * so it shouldn't be modifed or freed.
451 G_CONST_RETURN gchar *
452 gdk_x11_get_xatom_name_for_display (GdkDisplay *display,
455 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
457 return get_atom_name (gdk_x11_xatom_to_atom_for_display (display, xatom));
461 * gdk_x11_get_xatom_name:
462 * @xatom: an X atom for GDK's default display
464 * Returns the name of an X atom for GDK's default display. This
465 * function is meant mainly for debugging, so for convenience, unlike
466 * <function>XAtomName()</function> and gdk_atom_name(), the result
467 * doesn't need to be freed. Also, this function will never return %NULL,
468 * even if @xatom is invalid.
470 * Return value: name of the X atom; this string is owned by GTK+,
471 * so it shouldn't be modifed or freed.
473 G_CONST_RETURN gchar *
474 gdk_x11_get_xatom_name (Atom xatom)
476 return get_atom_name (gdk_x11_xatom_to_atom (xatom));
480 gdk_property_get (GdkWindow *window,
486 GdkAtom *actual_property_type,
487 gint *actual_format_type,
495 gulong ret_bytes_after;
502 g_return_val_if_fail (!window || GDK_IS_WINDOW (window), FALSE);
506 GdkScreen *screen = gdk_screen_get_default ();
507 window = gdk_screen_get_root_window (screen);
509 GDK_NOTE (MULTIHEAD, g_message ("gdk_property_get(): window is NULL\n"));
512 if (GDK_WINDOW_DESTROYED (window))
515 display = gdk_drawable_get_display (window);
516 xproperty = gdk_x11_atom_to_xatom_for_display (display, property);
517 if (type == GDK_NONE)
518 xtype = AnyPropertyType;
520 xtype = gdk_x11_atom_to_xatom_for_display (display, type);
524 res = XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
525 GDK_WINDOW_XWINDOW (window), xproperty,
526 offset, (length + 3) / 4, pdelete,
527 xtype, &ret_prop_type, &ret_format,
528 &ret_nitems, &ret_bytes_after,
531 if (res != Success || (ret_prop_type == None && ret_format == 0))
536 if (actual_property_type)
537 *actual_property_type = gdk_x11_xatom_to_atom_for_display (display, ret_prop_type);
538 if (actual_format_type)
539 *actual_format_type = ret_format;
541 if ((xtype != AnyPropertyType) && (ret_prop_type != xtype))
544 g_warning ("Couldn't match property type %s to %s\n",
545 gdk_x11_get_xatom_name_for_display (display, ret_prop_type),
546 gdk_x11_get_xatom_name_for_display (display, xtype));
550 /* FIXME: ignoring bytes_after could have very bad effects */
554 if (ret_prop_type == XA_ATOM ||
555 ret_prop_type == gdk_x11_get_xatom_by_name_for_display (display, "ATOM_PAIR"))
558 * data is an array of X atom, we need to convert it
559 * to an array of GDK Atoms
562 GdkAtom *ret_atoms = g_new (GdkAtom, ret_nitems);
563 Atom *xatoms = (Atom *)ret_data;
565 *data = (guchar *)ret_atoms;
567 for (i = 0; i < ret_nitems; i++)
568 ret_atoms[i] = gdk_x11_xatom_to_atom_for_display (display, xatoms[i]);
571 *actual_length = ret_nitems * sizeof (GdkAtom);
578 ret_length = ret_nitems;
581 ret_length = sizeof(short) * ret_nitems;
584 ret_length = sizeof(long) * ret_nitems;
587 g_warning ("unknown property return format: %d", ret_format);
592 *data = g_new (guchar, ret_length);
593 memcpy (*data, ret_data, ret_length);
595 *actual_length = ret_length;
605 gdk_property_change (GdkWindow *window,
618 g_return_if_fail (!window || GDK_IS_WINDOW (window));
624 screen = gdk_screen_get_default ();
625 window = gdk_screen_get_root_window (screen);
627 GDK_NOTE (MULTIHEAD, g_message ("gdk_property_change(): window is NULL\n"));
631 if (GDK_WINDOW_DESTROYED (window))
634 display = gdk_drawable_get_display (window);
636 xproperty = gdk_x11_atom_to_xatom_for_display (display, property);
637 xtype = gdk_x11_atom_to_xatom_for_display (display, type);
638 xwindow = GDK_WINDOW_XID (window);
640 if (xtype == XA_ATOM ||
641 xtype == gdk_x11_get_xatom_by_name_for_display (display, "ATOM_PAIR"))
644 * data is an array of GdkAtom, we need to convert it
645 * to an array of X Atoms
648 GdkAtom *atoms = (GdkAtom*) data;
651 xatoms = g_new (Atom, nelements);
652 for (i = 0; i < nelements; i++)
653 xatoms[i] = gdk_x11_atom_to_xatom_for_display (display, atoms[i]);
655 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), xwindow,
657 format, mode, (guchar *)xatoms, nelements);
661 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), xwindow, xproperty,
662 xtype, format, mode, (guchar *)data, nelements);
666 gdk_property_delete (GdkWindow *window,
669 g_return_if_fail (!window || GDK_IS_WINDOW (window));
673 GdkScreen *screen = gdk_screen_get_default ();
674 window = gdk_screen_get_root_window (screen);
677 g_message ("gdk_property_delete(): window is NULL\n"));
680 if (GDK_WINDOW_DESTROYED (window))
683 XDeleteProperty (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XWINDOW (window),
684 gdk_x11_atom_to_xatom_for_display (GDK_WINDOW_DISPLAY (window),