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));
146 * gdk_x11_atom_to_xatom_for_display:
147 * @display: A #GdkDisplay
150 * Converts from a #GdkAtom to the X atom for a #GdkDisplay
151 * with the same string value.
153 * Return value: the X atom corresponding to @atom.
158 gdk_x11_atom_to_xatom_for_display (GdkDisplay *display,
161 GdkDisplayX11 *display_x11;
164 g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
169 display_x11 = GDK_DISPLAY_X11 (display);
171 if (ATOM_TO_INDEX (atom) < G_N_ELEMENTS (XAtomsStrings) - N_CUSTOM_PREDEFINED)
172 return ATOM_TO_INDEX (atom);
174 if (display_x11->atom_from_virtual)
175 xatom = GPOINTER_TO_UINT (g_hash_table_lookup (display_x11->atom_from_virtual,
176 GDK_ATOM_TO_POINTER (atom)));
181 g_return_val_if_fail (ATOM_TO_INDEX (atom) < virtual_atom_array->len, None);
183 name = g_ptr_array_index (virtual_atom_array, ATOM_TO_INDEX (atom));
185 xatom = XInternAtom (GDK_DISPLAY_XDISPLAY (display), name, FALSE);
186 insert_atom_pair (display, atom, xatom);
193 * gdk_x11_atom_to_xatom:
196 * Converts from a #GdkAtom to the X atom for the default GDK display
197 * with the same string value.
199 * Return value: the X atom corresponding to @atom.
202 gdk_x11_atom_to_xatom (GdkAtom atom)
204 return gdk_x11_atom_to_xatom_for_display (gdk_display_get_default (), atom);
208 * gdk_x11_xatom_to_atom_for_display:
209 * @display: A #GdkDisplay
212 * Convert from an X atom for a #GdkDisplay to the corresponding
215 * Return value: the corresponding #GdkAtom.
220 gdk_x11_xatom_to_atom_for_display (GdkDisplay *display,
223 GdkDisplayX11 *display_x11;
224 GdkAtom virtual_atom = GDK_NONE;
226 g_return_val_if_fail (GDK_IS_DISPLAY (display), GDK_NONE);
231 display_x11 = GDK_DISPLAY_X11 (display);
233 if (xatom < G_N_ELEMENTS (XAtomsStrings) - N_CUSTOM_PREDEFINED)
234 return INDEX_TO_ATOM (xatom);
236 if (display_x11->atom_to_virtual)
237 virtual_atom = GDK_POINTER_TO_ATOM (g_hash_table_lookup (display_x11->atom_to_virtual,
238 GUINT_TO_POINTER (xatom)));
242 /* If this atom doesn't exist, we'll die with an X error unless
243 * we take precautions
246 gdk_error_trap_push ();
247 name = XGetAtomName (GDK_DISPLAY_XDISPLAY (display), xatom);
248 if (gdk_error_trap_pop ())
250 g_warning (G_STRLOC " invalid X atom: %ld", xatom);
254 virtual_atom = gdk_atom_intern (name, FALSE);
257 insert_atom_pair (display, virtual_atom, xatom);
265 * gdk_x11_xatom_to_atom:
266 * @xatom: an X atom for the default GDK display
268 * Convert from an X atom for the default display to the corresponding
271 * Return value: the corresponding G#dkAtom.
274 gdk_x11_xatom_to_atom (Atom xatom)
276 return gdk_x11_xatom_to_atom_for_display (gdk_display_get_default (), xatom);
280 virtual_atom_check_init (void)
282 if (!virtual_atom_hash)
286 virtual_atom_hash = g_hash_table_new (g_str_hash, g_str_equal);
287 virtual_atom_array = g_ptr_array_new ();
289 for (i = 0; i < G_N_ELEMENTS (XAtomsStrings); i++)
291 g_ptr_array_add (virtual_atom_array, XAtomsStrings[i]);
292 g_hash_table_insert (virtual_atom_hash, XAtomsStrings[i],
293 GUINT_TO_POINTER (i));
299 gdk_atom_intern (const gchar *atom_name,
300 gboolean only_if_exists)
304 virtual_atom_check_init ();
306 result = GDK_POINTER_TO_ATOM (g_hash_table_lookup (virtual_atom_hash, atom_name));
309 result = INDEX_TO_ATOM (virtual_atom_array->len);
311 g_ptr_array_add (virtual_atom_array, g_strdup (atom_name));
312 g_hash_table_insert (virtual_atom_hash,
313 g_ptr_array_index (virtual_atom_array,
314 ATOM_TO_INDEX (result)),
315 GDK_ATOM_TO_POINTER (result));
321 static G_CONST_RETURN char *
322 get_atom_name (GdkAtom atom)
324 virtual_atom_check_init ();
326 if (ATOM_TO_INDEX (atom) < virtual_atom_array->len)
327 return g_ptr_array_index (virtual_atom_array, ATOM_TO_INDEX (atom));
333 gdk_atom_name (GdkAtom atom)
335 return g_strdup (get_atom_name (atom));
339 * gdk_x11_get_xatom_by_name_for_display:
340 * @display: a #GdkDisplay
341 * @atom_name: a string
343 * Returns the X atom for a #GdkDisplay corresponding to @atom_name.
344 * This function caches the result, so if called repeatedly it is much
345 * faster than XInternAtom(), which is a round trip to the server each time.
347 * Return value: a X atom for a #GdkDisplay
352 gdk_x11_get_xatom_by_name_for_display (GdkDisplay *display,
353 const gchar *atom_name)
355 g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
356 return gdk_x11_atom_to_xatom_for_display (display,
357 gdk_atom_intern (atom_name, FALSE));
361 * gdk_x11_get_xatom_by_name:
362 * @atom_name: a string
364 * Returns the X atom for GDK's default display corresponding to @atom_name.
365 * This function caches the result, so if called repeatedly it is much
366 * faster than <function>XInternAtom()</function>, which is a round trip to
367 * the server each time.
369 * Return value: a X atom for GDK's default display.
372 gdk_x11_get_xatom_by_name (const gchar *atom_name)
374 return gdk_x11_get_xatom_by_name_for_display (gdk_display_get_default (),
379 * gdk_x11_get_xatom_name_for_display:
380 * @display: the #GdkDisplay where @xatom is defined
383 * Returns the name of an X atom for its display. This
384 * function is meant mainly for debugging, so for convenience, unlike
385 * XAtomName() and gdk_atom_name(), the result doesn't need to
388 * Return value: name of the X atom; this string is owned by GDK,
389 * so it shouldn't be modifed or freed.
393 G_CONST_RETURN gchar *
394 gdk_x11_get_xatom_name_for_display (GdkDisplay *display,
397 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
399 return get_atom_name (gdk_x11_xatom_to_atom_for_display (display, xatom));
403 * gdk_x11_get_xatom_name:
404 * @xatom: an X atom for GDK's default display
406 * Returns the name of an X atom for GDK's default display. This
407 * function is meant mainly for debugging, so for convenience, unlike
408 * <function>XAtomName()</function> and gdk_atom_name(), the result
409 * doesn't need to be freed. Also, this function will never return %NULL,
410 * even if @xatom is invalid.
412 * Return value: name of the X atom; this string is owned by GTK+,
413 * so it shouldn't be modifed or freed.
415 G_CONST_RETURN gchar *
416 gdk_x11_get_xatom_name (Atom xatom)
418 return get_atom_name (gdk_x11_xatom_to_atom (xatom));
422 gdk_property_get (GdkWindow *window,
428 GdkAtom *actual_property_type,
429 gint *actual_format_type,
437 gulong ret_bytes_after;
444 g_return_val_if_fail (!window || GDK_IS_WINDOW (window), FALSE);
448 GdkScreen *screen = gdk_screen_get_default ();
449 window = gdk_screen_get_root_window (screen);
451 GDK_NOTE (MULTIHEAD, g_message ("gdk_property_get(): window is NULL\n"));
454 if (GDK_WINDOW_DESTROYED (window))
457 display = gdk_drawable_get_display (window);
458 xproperty = gdk_x11_atom_to_xatom_for_display (display, property);
459 xtype = gdk_x11_atom_to_xatom_for_display (display, type);
463 res = XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
464 GDK_WINDOW_XWINDOW (window), xproperty,
465 offset, (length + 3) / 4, pdelete,
466 xtype, &ret_prop_type, &ret_format,
467 &ret_nitems, &ret_bytes_after,
470 if (res != Success || (ret_prop_type == None && ret_format == 0))
475 if (actual_property_type)
476 *actual_property_type = gdk_x11_xatom_to_atom_for_display (display, ret_prop_type);
477 if (actual_format_type)
478 *actual_format_type = ret_format;
480 if ((xtype != AnyPropertyType) && (ret_prop_type != xtype))
483 g_warning ("Couldn't match property type %s to %s\n",
484 gdk_x11_get_xatom_name_for_display (display, ret_prop_type),
485 gdk_x11_get_xatom_name_for_display (display, xtype));
489 /* FIXME: ignoring bytes_after could have very bad effects */
493 if (ret_prop_type == XA_ATOM ||
494 ret_prop_type == gdk_x11_get_xatom_by_name_for_display (display, "ATOM_PAIR"))
497 * data is an array of X atom, we need to convert it
498 * to an array of GDK Atoms
501 GdkAtom *ret_atoms = g_new (GdkAtom, ret_nitems);
502 Atom *xatoms = (Atom *)ret_data;
504 *data = (guchar *)ret_atoms;
506 for (i = 0; i < ret_nitems; i++)
507 ret_atoms[i] = gdk_x11_xatom_to_atom_for_display (display, xatoms[i]);
510 *actual_length = ret_nitems * sizeof (GdkAtom);
517 ret_length = ret_nitems;
520 ret_length = sizeof(short) * ret_nitems;
523 ret_length = sizeof(long) * ret_nitems;
526 g_warning ("unknown property return format: %d", ret_format);
531 *data = g_new (guchar, ret_length);
532 memcpy (*data, ret_data, ret_length);
534 *actual_length = ret_length;
544 gdk_property_change (GdkWindow *window,
557 g_return_if_fail (!window || GDK_IS_WINDOW (window));
563 screen = gdk_screen_get_default ();
564 window = gdk_screen_get_root_window (screen);
566 GDK_NOTE (MULTIHEAD, g_message ("gdk_property_delete(): window is NULL\n"));
570 if (GDK_WINDOW_DESTROYED (window))
573 display = gdk_drawable_get_display (window);
575 xproperty = gdk_x11_atom_to_xatom_for_display (display, property);
576 xtype = gdk_x11_atom_to_xatom_for_display (display, type);
577 xwindow = GDK_WINDOW_XID (window);
579 if (xtype == XA_ATOM ||
580 xtype == gdk_x11_get_xatom_by_name_for_display (display, "ATOM_PAIR"))
583 * data is an array of GdkAtom, we need to convert it
584 * to an array of X Atoms
587 GdkAtom *atoms = (GdkAtom*) data;
590 xatoms = g_new (Atom, nelements);
591 for (i = 0; i < nelements; i++)
592 xatoms[i] = gdk_x11_atom_to_xatom_for_display (display, atoms[i]);
594 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), xwindow,
596 format, mode, (guchar *)xatoms, nelements);
600 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), xwindow, xproperty,
601 xtype, format, mode, (guchar *)data, nelements);
605 gdk_property_delete (GdkWindow *window,
608 g_return_if_fail (!window || GDK_IS_WINDOW (window));
612 GdkScreen *screen = gdk_screen_get_default ();
613 window = gdk_screen_get_root_window (screen);
616 g_message ("gdk_property_delete(): window is NULL\n"));
619 if (GDK_WINDOW_DESTROYED (window))
622 XDeleteProperty (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XWINDOW (window),
623 gdk_x11_atom_to_xatom_for_display (GDK_WINDOW_DISPLAY (window),