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 gchar *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];
232 XInternAtoms (GDK_DISPLAY_XDISPLAY (display),
233 (char **)xatom_names, n_xatoms, False, xatoms);
235 for (i = 0; i < n_xatoms; i++)
236 insert_atom_pair (display, atoms[i], xatoms[i]);
239 g_free (xatom_names);
244 * gdk_x11_atom_to_xatom:
247 * Converts from a #GdkAtom to the X atom for the default GDK display
248 * with the same string value.
250 * Return value: the X atom corresponding to @atom.
253 gdk_x11_atom_to_xatom (GdkAtom atom)
255 return gdk_x11_atom_to_xatom_for_display (gdk_display_get_default (), atom);
259 * gdk_x11_xatom_to_atom_for_display:
260 * @display: A #GdkDisplay
263 * Convert from an X atom for a #GdkDisplay to the corresponding
266 * Return value: the corresponding #GdkAtom.
271 gdk_x11_xatom_to_atom_for_display (GdkDisplay *display,
274 GdkDisplayX11 *display_x11;
275 GdkAtom virtual_atom = GDK_NONE;
277 g_return_val_if_fail (GDK_IS_DISPLAY (display), GDK_NONE);
282 display_x11 = GDK_DISPLAY_X11 (display);
284 if (xatom < G_N_ELEMENTS (XAtomsStrings) - N_CUSTOM_PREDEFINED)
285 return INDEX_TO_ATOM (xatom);
287 if (display_x11->atom_to_virtual)
288 virtual_atom = GDK_POINTER_TO_ATOM (g_hash_table_lookup (display_x11->atom_to_virtual,
289 GUINT_TO_POINTER (xatom)));
293 /* If this atom doesn't exist, we'll die with an X error unless
294 * we take precautions
297 gdk_error_trap_push ();
298 name = XGetAtomName (GDK_DISPLAY_XDISPLAY (display), xatom);
299 if (gdk_error_trap_pop ())
301 g_warning (G_STRLOC " invalid X atom: %ld", xatom);
305 virtual_atom = gdk_atom_intern (name, FALSE);
308 insert_atom_pair (display, virtual_atom, xatom);
316 * gdk_x11_xatom_to_atom:
317 * @xatom: an X atom for the default GDK display
319 * Convert from an X atom for the default display to the corresponding
322 * Return value: the corresponding G#dkAtom.
325 gdk_x11_xatom_to_atom (Atom xatom)
327 return gdk_x11_xatom_to_atom_for_display (gdk_display_get_default (), xatom);
331 virtual_atom_check_init (void)
333 if (!virtual_atom_hash)
337 virtual_atom_hash = g_hash_table_new (g_str_hash, g_str_equal);
338 virtual_atom_array = g_ptr_array_new ();
340 for (i = 0; i < G_N_ELEMENTS (XAtomsStrings); i++)
342 g_ptr_array_add (virtual_atom_array, XAtomsStrings[i]);
343 g_hash_table_insert (virtual_atom_hash, XAtomsStrings[i],
344 GUINT_TO_POINTER (i));
350 gdk_atom_intern (const gchar *atom_name,
351 gboolean only_if_exists)
355 virtual_atom_check_init ();
357 result = GDK_POINTER_TO_ATOM (g_hash_table_lookup (virtual_atom_hash, atom_name));
360 result = INDEX_TO_ATOM (virtual_atom_array->len);
362 g_ptr_array_add (virtual_atom_array, g_strdup (atom_name));
363 g_hash_table_insert (virtual_atom_hash,
364 g_ptr_array_index (virtual_atom_array,
365 ATOM_TO_INDEX (result)),
366 GDK_ATOM_TO_POINTER (result));
372 static G_CONST_RETURN char *
373 get_atom_name (GdkAtom atom)
375 virtual_atom_check_init ();
377 if (ATOM_TO_INDEX (atom) < virtual_atom_array->len)
378 return g_ptr_array_index (virtual_atom_array, ATOM_TO_INDEX (atom));
384 gdk_atom_name (GdkAtom atom)
386 return g_strdup (get_atom_name (atom));
390 * gdk_x11_get_xatom_by_name_for_display:
391 * @display: a #GdkDisplay
392 * @atom_name: a string
394 * Returns the X atom for a #GdkDisplay corresponding to @atom_name.
395 * This function caches the result, so if called repeatedly it is much
396 * faster than XInternAtom(), which is a round trip to the server each time.
398 * Return value: a X atom for a #GdkDisplay
403 gdk_x11_get_xatom_by_name_for_display (GdkDisplay *display,
404 const gchar *atom_name)
406 g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
407 return gdk_x11_atom_to_xatom_for_display (display,
408 gdk_atom_intern (atom_name, FALSE));
412 * gdk_x11_get_xatom_by_name:
413 * @atom_name: a string
415 * Returns the X atom for GDK's default display corresponding to @atom_name.
416 * This function caches the result, so if called repeatedly it is much
417 * faster than <function>XInternAtom()</function>, which is a round trip to
418 * the server each time.
420 * Return value: a X atom for GDK's default display.
423 gdk_x11_get_xatom_by_name (const gchar *atom_name)
425 return gdk_x11_get_xatom_by_name_for_display (gdk_display_get_default (),
430 * gdk_x11_get_xatom_name_for_display:
431 * @display: the #GdkDisplay where @xatom is defined
434 * Returns the name of an X atom for its display. This
435 * function is meant mainly for debugging, so for convenience, unlike
436 * XAtomName() and gdk_atom_name(), the result doesn't need to
439 * Return value: name of the X atom; this string is owned by GDK,
440 * so it shouldn't be modifed or freed.
444 G_CONST_RETURN gchar *
445 gdk_x11_get_xatom_name_for_display (GdkDisplay *display,
448 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
450 return get_atom_name (gdk_x11_xatom_to_atom_for_display (display, xatom));
454 * gdk_x11_get_xatom_name:
455 * @xatom: an X atom for GDK's default display
457 * Returns the name of an X atom for GDK's default display. This
458 * function is meant mainly for debugging, so for convenience, unlike
459 * <function>XAtomName()</function> and gdk_atom_name(), the result
460 * doesn't need to be freed. Also, this function will never return %NULL,
461 * even if @xatom is invalid.
463 * Return value: name of the X atom; this string is owned by GTK+,
464 * so it shouldn't be modifed or freed.
466 G_CONST_RETURN gchar *
467 gdk_x11_get_xatom_name (Atom xatom)
469 return get_atom_name (gdk_x11_xatom_to_atom (xatom));
473 gdk_property_get (GdkWindow *window,
479 GdkAtom *actual_property_type,
480 gint *actual_format_type,
488 gulong ret_bytes_after;
495 g_return_val_if_fail (!window || GDK_IS_WINDOW (window), FALSE);
499 GdkScreen *screen = gdk_screen_get_default ();
500 window = gdk_screen_get_root_window (screen);
502 GDK_NOTE (MULTIHEAD, g_message ("gdk_property_get(): window is NULL\n"));
505 if (GDK_WINDOW_DESTROYED (window))
508 display = gdk_drawable_get_display (window);
509 xproperty = gdk_x11_atom_to_xatom_for_display (display, property);
510 if (type == GDK_NONE)
511 xtype = AnyPropertyType;
513 xtype = gdk_x11_atom_to_xatom_for_display (display, type);
517 res = XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
518 GDK_WINDOW_XWINDOW (window), xproperty,
519 offset, (length + 3) / 4, pdelete,
520 xtype, &ret_prop_type, &ret_format,
521 &ret_nitems, &ret_bytes_after,
524 if (res != Success || (ret_prop_type == None && ret_format == 0))
529 if (actual_property_type)
530 *actual_property_type = gdk_x11_xatom_to_atom_for_display (display, ret_prop_type);
531 if (actual_format_type)
532 *actual_format_type = ret_format;
534 if ((xtype != AnyPropertyType) && (ret_prop_type != xtype))
537 g_warning ("Couldn't match property type %s to %s\n",
538 gdk_x11_get_xatom_name_for_display (display, ret_prop_type),
539 gdk_x11_get_xatom_name_for_display (display, xtype));
543 /* FIXME: ignoring bytes_after could have very bad effects */
547 if (ret_prop_type == XA_ATOM ||
548 ret_prop_type == gdk_x11_get_xatom_by_name_for_display (display, "ATOM_PAIR"))
551 * data is an array of X atom, we need to convert it
552 * to an array of GDK Atoms
555 GdkAtom *ret_atoms = g_new (GdkAtom, ret_nitems);
556 Atom *xatoms = (Atom *)ret_data;
558 *data = (guchar *)ret_atoms;
560 for (i = 0; i < ret_nitems; i++)
561 ret_atoms[i] = gdk_x11_xatom_to_atom_for_display (display, xatoms[i]);
564 *actual_length = ret_nitems * sizeof (GdkAtom);
571 ret_length = ret_nitems;
574 ret_length = sizeof(short) * ret_nitems;
577 ret_length = sizeof(long) * ret_nitems;
580 g_warning ("unknown property return format: %d", ret_format);
585 *data = g_new (guchar, ret_length);
586 memcpy (*data, ret_data, ret_length);
588 *actual_length = ret_length;
598 gdk_property_change (GdkWindow *window,
611 g_return_if_fail (!window || GDK_IS_WINDOW (window));
617 screen = gdk_screen_get_default ();
618 window = gdk_screen_get_root_window (screen);
620 GDK_NOTE (MULTIHEAD, g_message ("gdk_property_change(): window is NULL\n"));
624 if (GDK_WINDOW_DESTROYED (window))
627 display = gdk_drawable_get_display (window);
629 xproperty = gdk_x11_atom_to_xatom_for_display (display, property);
630 xtype = gdk_x11_atom_to_xatom_for_display (display, type);
631 xwindow = GDK_WINDOW_XID (window);
633 if (xtype == XA_ATOM ||
634 xtype == gdk_x11_get_xatom_by_name_for_display (display, "ATOM_PAIR"))
637 * data is an array of GdkAtom, we need to convert it
638 * to an array of X Atoms
641 GdkAtom *atoms = (GdkAtom*) data;
644 xatoms = g_new (Atom, nelements);
645 for (i = 0; i < nelements; i++)
646 xatoms[i] = gdk_x11_atom_to_xatom_for_display (display, atoms[i]);
648 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), xwindow,
650 format, mode, (guchar *)xatoms, nelements);
654 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), xwindow, xproperty,
655 xtype, format, mode, (guchar *)data, nelements);
659 gdk_property_delete (GdkWindow *window,
662 g_return_if_fail (!window || GDK_IS_WINDOW (window));
666 GdkScreen *screen = gdk_screen_get_default ();
667 window = gdk_screen_get_root_window (screen);
670 g_message ("gdk_property_delete(): window is NULL\n"));
673 if (GDK_WINDOW_DESTROYED (window))
676 XDeleteProperty (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XWINDOW (window),
677 gdk_x11_atom_to_xatom_for_display (GDK_WINDOW_DISPLAY (window),