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"
36 static GPtrArray *virtual_atom_array;
37 static GHashTable *virtual_atom_hash;
38 static GHashTable *atom_to_virtual;
39 static GHashTable *atom_from_virtual;
41 static gchar *XAtomsStrings[] = {
42 /* These are all the standard predefined X atoms */
88 "MAX_SPACE", "END_SPACE",
94 "UNDERLINE_THICKNESS",
111 /* Below here, these our our additions. Increment N_CUSTOM_PREDEFINED
114 "CLIPBOARD" /* = 69 */
117 #define N_CUSTOM_PREDEFINED 1
119 #define ATOM_TO_INDEX(atom) (GPOINTER_TO_UINT(atom))
120 #define INDEX_TO_ATOM(atom) ((GdkAtom)GUINT_TO_POINTER(atom))
123 insert_atom_pair (GdkAtom virtual_atom,
126 if (!atom_from_virtual)
128 atom_from_virtual = g_hash_table_new (g_direct_hash, NULL);
129 atom_to_virtual = g_hash_table_new (g_direct_hash, NULL);
132 g_hash_table_insert (atom_from_virtual,
133 GDK_ATOM_TO_POINTER (virtual_atom), GUINT_TO_POINTER (xatom));
134 g_hash_table_insert (atom_to_virtual,
135 GUINT_TO_POINTER (xatom), GDK_ATOM_TO_POINTER (virtual_atom));
139 * gdk_x11_atom_to_xatom:
142 * Converts from a #GdkAtom to the X atom for the default GDK display
143 * with the same string value.
145 * Return value: the X atom corresponding to @atom.
148 gdk_x11_atom_to_xatom (GdkAtom atom)
152 if (ATOM_TO_INDEX (atom) < G_N_ELEMENTS (XAtomsStrings) - N_CUSTOM_PREDEFINED)
153 return ATOM_TO_INDEX (atom);
155 if (atom_from_virtual)
156 xatom = GPOINTER_TO_UINT (g_hash_table_lookup (atom_from_virtual,
157 GDK_ATOM_TO_POINTER (atom)));
162 g_return_val_if_fail (ATOM_TO_INDEX (atom) < virtual_atom_array->len, None);
164 name = g_ptr_array_index (virtual_atom_array, ATOM_TO_INDEX (atom));
166 xatom = XInternAtom (gdk_display, name, FALSE);
167 insert_atom_pair (atom, xatom);
174 * gdk_x11_xatom_to_atom:
175 * @xatom: an X atom for the default GDK display
177 * Converts from an X atom for the default display to the corresponding
180 * Return value: the corresponding #GdkAtom.
183 gdk_x11_xatom_to_atom (Atom xatom)
185 GdkAtom virtual_atom = GDK_NONE;
187 if (xatom < G_N_ELEMENTS (XAtomsStrings) - N_CUSTOM_PREDEFINED)
188 return INDEX_TO_ATOM (xatom);
191 virtual_atom = GDK_POINTER_TO_ATOM (g_hash_table_lookup (atom_to_virtual,
192 GUINT_TO_POINTER (xatom)));
196 /* If this atom doesn't exist, we'll die with an X error unless
197 * we take precautions
200 gdk_error_trap_push ();
201 name = XGetAtomName (gdk_display, xatom);
202 if (gdk_error_trap_pop ())
204 g_warning (G_STRLOC " invalid X atom: %ld", xatom);
208 virtual_atom = gdk_atom_intern (name, FALSE);
211 insert_atom_pair (virtual_atom, xatom);
219 virtual_atom_check_init (void)
221 if (!virtual_atom_hash)
225 virtual_atom_hash = g_hash_table_new (g_str_hash, g_str_equal);
226 virtual_atom_array = g_ptr_array_new ();
228 for (i = 0; i < G_N_ELEMENTS (XAtomsStrings); i++)
230 g_ptr_array_add (virtual_atom_array, XAtomsStrings[i]);
231 g_hash_table_insert (virtual_atom_hash, XAtomsStrings[i],
232 GUINT_TO_POINTER (i));
238 gdk_atom_intern (const gchar *atom_name,
239 gboolean only_if_exists)
243 virtual_atom_check_init ();
245 result = GDK_POINTER_TO_ATOM (g_hash_table_lookup (virtual_atom_hash, atom_name));
248 result = INDEX_TO_ATOM (virtual_atom_array->len);
250 g_ptr_array_add (virtual_atom_array, g_strdup (atom_name));
251 g_hash_table_insert (virtual_atom_hash,
252 g_ptr_array_index (virtual_atom_array,
253 ATOM_TO_INDEX (result)),
254 GDK_ATOM_TO_POINTER (result));
260 static G_CONST_RETURN char *
261 get_atom_name (GdkAtom atom)
263 virtual_atom_check_init ();
265 if (ATOM_TO_INDEX (atom) < virtual_atom_array->len)
266 return g_ptr_array_index (virtual_atom_array, ATOM_TO_INDEX (atom));
272 gdk_atom_name (GdkAtom atom)
274 return g_strdup (get_atom_name (atom));
278 * gdk_x11_get_xatom_by_name:
279 * @atom_name: a string
281 * Returns the X atom for GDK's default display corresponding to @atom_name.
282 * This function caches the result, so if called repeatedly it is much
283 * faster than <function>XInternAtom()</function>, which is a round trip to
284 * the server each time.
286 * Return value: a X atom for GDK's default display.
289 gdk_x11_get_xatom_by_name (const gchar *atom_name)
291 return gdk_x11_atom_to_xatom (gdk_atom_intern (atom_name, FALSE));
295 * gdk_x11_get_xatom_name:
296 * @xatom: an X atom for GDK's default display
298 * Returns the name of an X atom for GDK's default display. This
299 * function is meant mainly for debugging, so for convenience, unlike
300 * <function>XAtomName()</function> and gdk_atom_name(), the result
301 * doesn't need to be freed. Also, this function will never return %NULL,
302 * even if @xatom is invalid.
304 * Return value: name of the X atom; this string is owned by GTK+,
305 * so it shouldn't be modifed or freed.
307 G_CONST_RETURN gchar *
308 gdk_x11_get_xatom_name (Atom xatom)
310 return get_atom_name (gdk_x11_xatom_to_atom (xatom));
314 gdk_property_get (GdkWindow *window,
320 GdkAtom *actual_property_type,
321 gint *actual_format_type,
332 gulong ret_bytes_after;
336 g_return_val_if_fail (!window || GDK_IS_WINDOW (window), FALSE);
338 xproperty = gdk_x11_atom_to_xatom (property);
339 xtype = gdk_x11_atom_to_xatom (type);
343 if (GDK_WINDOW_DESTROYED (window))
346 xdisplay = GDK_WINDOW_XDISPLAY (window);
347 xwindow = GDK_WINDOW_XID (window);
351 xdisplay = gdk_display;
352 xwindow = _gdk_root_window;
356 XGetWindowProperty (xdisplay, xwindow, xproperty,
357 offset, (length + 3) / 4, pdelete,
358 xtype, &ret_prop_type, &ret_format,
359 &ret_nitems, &ret_bytes_after,
362 if ((ret_prop_type == None) && (ret_format == 0)) {
366 if (actual_property_type)
367 *actual_property_type = gdk_x11_xatom_to_atom (ret_prop_type);
368 if (actual_format_type)
369 *actual_format_type = ret_format;
371 if ((type != AnyPropertyType) && (ret_prop_type != xtype))
375 g_warning("Couldn't match property type %s to %s\n",
376 gdk_x11_get_xatom_name (ret_prop_type),
377 gdk_x11_get_xatom_name (xtype));
381 /* FIXME: ignoring bytes_after could have very bad effects */
385 if (ret_prop_type == XA_ATOM ||
386 ret_prop_type == gdk_x11_get_xatom_by_name ("ATOM_PAIR"))
389 * data is an array of X atom, we need to convert it
390 * to an array of GDK Atoms
393 GdkAtom *ret_atoms = g_new (GdkAtom, ret_nitems);
394 Atom *xatoms = (Atom *)ret_data;
396 *data = (guchar *)ret_atoms;
398 for (i = 0; i < ret_nitems; i++)
399 ret_atoms[i] = gdk_x11_xatom_to_atom (xatoms[i]);
406 ret_length = ret_nitems;
409 ret_length = sizeof(short) * ret_nitems;
412 ret_length = sizeof(long) * ret_nitems;
415 g_warning ("unknown property return format: %d", ret_format);
420 *data = g_new (guchar, ret_length);
421 memcpy (*data, ret_data, ret_length);
423 *actual_length = ret_length;
433 gdk_property_change (GdkWindow *window,
446 g_return_if_fail (!window || GDK_IS_WINDOW (window));
448 xproperty = gdk_x11_atom_to_xatom (property);
449 xtype = gdk_x11_atom_to_xatom (type);
453 if (GDK_WINDOW_DESTROYED (window))
456 xdisplay = GDK_WINDOW_XDISPLAY (window);
457 xwindow = GDK_WINDOW_XID (window);
461 xdisplay = gdk_display;
462 xwindow = _gdk_root_window;
465 if (xtype == XA_ATOM || xtype == gdk_x11_get_xatom_by_name ("ATOM_PAIR"))
468 * data is an array of GdkAtom, we need to convert it
469 * to an array of X Atoms
472 GdkAtom *atoms = (GdkAtom*) data;
475 xatoms = g_new (Atom, nelements);
476 for (i = 0; i < nelements; i++)
477 xatoms[i] = gdk_x11_atom_to_xatom (atoms[i]);
479 XChangeProperty (xdisplay, xwindow, xproperty, xtype,
480 format, mode, (guchar *)xatoms, nelements);
484 XChangeProperty (xdisplay, xwindow, xproperty, xtype,
485 format, mode, (guchar *)data, nelements);
489 gdk_property_delete (GdkWindow *window,
495 g_return_if_fail (!window || GDK_IS_WINDOW (window));
499 if (GDK_WINDOW_DESTROYED (window))
502 xdisplay = GDK_WINDOW_XDISPLAY (window);
503 xwindow = GDK_WINDOW_XID (window);
507 xdisplay = gdk_display;
508 xwindow = _gdk_root_window;
511 XDeleteProperty (xdisplay, xwindow, gdk_x11_atom_to_xatom (property));