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.
156 gdk_x11_atom_to_xatom_for_display (GdkDisplay *display,
159 GdkDisplayX11 *display_x11;
162 g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
167 display_x11 = GDK_DISPLAY_X11 (display);
169 if (ATOM_TO_INDEX (atom) < G_N_ELEMENTS (XAtomsStrings) - N_CUSTOM_PREDEFINED)
170 return ATOM_TO_INDEX (atom);
172 if (display_x11->atom_from_virtual)
173 xatom = GPOINTER_TO_UINT (g_hash_table_lookup (display_x11->atom_from_virtual,
174 GDK_ATOM_TO_POINTER (atom)));
179 g_return_val_if_fail (ATOM_TO_INDEX (atom) < virtual_atom_array->len, None);
181 name = g_ptr_array_index (virtual_atom_array, ATOM_TO_INDEX (atom));
183 xatom = XInternAtom (GDK_DISPLAY_XDISPLAY (display), name, FALSE);
184 insert_atom_pair (display, atom, xatom);
191 * gdk_x11_atom_to_xatom:
194 * Converts from a #GdkAtom to the X atom for the default GDK display
195 * with the same string value.
197 * Return value: the X atom corresponding to @atom.
200 gdk_x11_atom_to_xatom (GdkAtom atom)
202 return gdk_x11_atom_to_xatom_for_display (gdk_display_get_default (), atom);
206 * gdk_x11_xatom_to_atom_for_display:
207 * @display: A #GdkDisplay
210 * Convert from an X atom for a #GdkDisplay to the corresponding
213 * Return value: the corresponding #GdkAtom.
216 gdk_x11_xatom_to_atom_for_display (GdkDisplay *display,
219 GdkDisplayX11 *display_x11;
220 GdkAtom virtual_atom = GDK_NONE;
222 g_return_val_if_fail (GDK_IS_DISPLAY (display), GDK_NONE);
227 display_x11 = GDK_DISPLAY_X11 (display);
229 if (xatom < G_N_ELEMENTS (XAtomsStrings) - N_CUSTOM_PREDEFINED)
230 return INDEX_TO_ATOM (xatom);
232 if (display_x11->atom_to_virtual)
233 virtual_atom = GDK_POINTER_TO_ATOM (g_hash_table_lookup (display_x11->atom_to_virtual,
234 GUINT_TO_POINTER (xatom)));
238 /* If this atom doesn't exist, we'll die with an X error unless
239 * we take precautions
242 gdk_error_trap_push ();
243 name = XGetAtomName (GDK_DISPLAY_XDISPLAY (display), xatom);
244 if (gdk_error_trap_pop ())
246 g_warning (G_STRLOC " invalid X atom: %ld", xatom);
250 virtual_atom = gdk_atom_intern (name, FALSE);
253 insert_atom_pair (display, virtual_atom, xatom);
261 * gdk_x11_xatom_to_atom:
262 * @xatom: an X atom for the default GDK display
264 * Convert from an X atom for the default display to the corresponding
267 * Return value: the corresponding G#dkAtom.
270 gdk_x11_xatom_to_atom (Atom xatom)
272 return gdk_x11_xatom_to_atom_for_display (gdk_display_get_default (), xatom);
276 virtual_atom_check_init (void)
278 if (!virtual_atom_hash)
282 virtual_atom_hash = g_hash_table_new (g_str_hash, g_str_equal);
283 virtual_atom_array = g_ptr_array_new ();
285 for (i = 0; i < G_N_ELEMENTS (XAtomsStrings); i++)
287 g_ptr_array_add (virtual_atom_array, XAtomsStrings[i]);
288 g_hash_table_insert (virtual_atom_hash, XAtomsStrings[i],
289 GUINT_TO_POINTER (i));
295 gdk_atom_intern (const gchar *atom_name,
296 gboolean only_if_exists)
300 virtual_atom_check_init ();
302 result = GDK_POINTER_TO_ATOM (g_hash_table_lookup (virtual_atom_hash, atom_name));
305 result = INDEX_TO_ATOM (virtual_atom_array->len);
307 g_ptr_array_add (virtual_atom_array, g_strdup (atom_name));
308 g_hash_table_insert (virtual_atom_hash,
309 g_ptr_array_index (virtual_atom_array,
310 ATOM_TO_INDEX (result)),
311 GDK_ATOM_TO_POINTER (result));
317 static G_CONST_RETURN char *
318 get_atom_name (GdkAtom atom)
320 virtual_atom_check_init ();
322 if (ATOM_TO_INDEX (atom) < virtual_atom_array->len)
323 return g_ptr_array_index (virtual_atom_array, ATOM_TO_INDEX (atom));
329 gdk_atom_name (GdkAtom atom)
331 return g_strdup (get_atom_name (atom));
335 * gdk_x11_get_xatom_by_name_for_display:
336 * @display: a #GdkDisplay
337 * @atom_name: a string
339 * Returns the X atom for a #GdkDisplay corresponding to @atom_name.
340 * This function caches the result, so if called repeatedly it is much
341 * faster than XInternAtom, which is a round trip to the server each time.
343 * Return value: a X atom for a #GdkDisplay
346 gdk_x11_get_xatom_by_name_for_display (GdkDisplay *display,
347 const gchar *atom_name)
349 g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
350 return gdk_x11_atom_to_xatom_for_display (display,
351 gdk_atom_intern (atom_name, FALSE));
355 * gdk_x11_get_xatom_by_name:
356 * @atom_name: a string
358 * Returns the X atom for GDK's default display corresponding to @atom_name.
359 * This function caches the result, so if called repeatedly it is much
360 * faster than <function>XInternAtom()</function>, which is a round trip to
361 * the server each time.
363 * Return value: a X atom for GDK's default display.
366 gdk_x11_get_xatom_by_name (const gchar *atom_name)
368 return gdk_x11_get_xatom_by_name_for_display (gdk_display_get_default (),
373 * gdk_x11_get_xatom_name_for_display:
374 * @display : the #GdkDisplay where @xatom is defined
377 * Returns the name of an X atom for its display. This
378 * function is meant mainly for debugging, so for convenience, unlike
379 * XAtomName() and gdk_atom_name(), the result doesn't need to
382 * Return value: name of the X atom; this string is owned by GTK+,
383 * so it shouldn't be modifed or freed.
385 G_CONST_RETURN gchar *
386 gdk_x11_get_xatom_name_for_display (GdkDisplay *display,
389 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
391 return get_atom_name (gdk_x11_xatom_to_atom_for_display (display, xatom));
395 * gdk_x11_get_xatom_name:
396 * @xatom: an X atom for GDK's default display
398 * Returns the name of an X atom for GDK's default display. This
399 * function is meant mainly for debugging, so for convenience, unlike
400 * <function>XAtomName()</function> and gdk_atom_name(), the result
401 * doesn't need to be freed. Also, this function will never return %NULL,
402 * even if @xatom is invalid.
404 * Return value: name of the X atom; this string is owned by GTK+,
405 * so it shouldn't be modifed or freed.
407 G_CONST_RETURN gchar *
408 gdk_x11_get_xatom_name (Atom xatom)
410 return get_atom_name (gdk_x11_xatom_to_atom (xatom));
414 gdk_property_get (GdkWindow *window,
420 GdkAtom *actual_property_type,
421 gint *actual_format_type,
429 gulong ret_bytes_after;
436 g_return_val_if_fail (!window || GDK_IS_WINDOW (window), FALSE);
440 GdkScreen *screen = gdk_screen_get_default ();
441 window = gdk_screen_get_root_window (screen);
443 GDK_NOTE (MULTIHEAD, g_message ("gdk_property_get(): window is NULL\n"));
446 if (GDK_WINDOW_DESTROYED (window))
449 display = gdk_drawable_get_display (window);
450 xproperty = gdk_x11_atom_to_xatom_for_display (display, property);
451 xtype = gdk_x11_atom_to_xatom_for_display (display, type);
455 res = XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
456 GDK_WINDOW_XWINDOW (window), xproperty,
457 offset, (length + 3) / 4, pdelete,
458 xtype, &ret_prop_type, &ret_format,
459 &ret_nitems, &ret_bytes_after,
462 if (res != Success || (ret_prop_type == None && ret_format == 0))
467 if (actual_property_type)
468 *actual_property_type = gdk_x11_xatom_to_atom_for_display (display, ret_prop_type);
469 if (actual_format_type)
470 *actual_format_type = ret_format;
472 if ((xtype != AnyPropertyType) && (ret_prop_type != xtype))
475 g_warning ("Couldn't match property type %s to %s\n",
476 gdk_x11_get_xatom_name_for_display (display, ret_prop_type),
477 gdk_x11_get_xatom_name_for_display (display, xtype));
481 /* FIXME: ignoring bytes_after could have very bad effects */
485 if (ret_prop_type == XA_ATOM ||
486 ret_prop_type == gdk_x11_get_xatom_by_name_for_display (display, "ATOM_PAIR"))
489 * data is an array of X atom, we need to convert it
490 * to an array of GDK Atoms
493 GdkAtom *ret_atoms = g_new (GdkAtom, ret_nitems);
494 Atom *xatoms = (Atom *)ret_data;
496 *data = (guchar *)ret_atoms;
498 for (i = 0; i < ret_nitems; i++)
499 ret_atoms[i] = gdk_x11_xatom_to_atom_for_display (display, xatoms[i]);
502 *actual_length = ret_nitems * sizeof (GdkAtom);
509 ret_length = ret_nitems;
512 ret_length = sizeof(short) * ret_nitems;
515 ret_length = sizeof(long) * ret_nitems;
518 g_warning ("unknown property return format: %d", ret_format);
523 *data = g_new (guchar, ret_length);
524 memcpy (*data, ret_data, ret_length);
526 *actual_length = ret_length;
536 gdk_property_change (GdkWindow *window,
549 g_return_if_fail (!window || GDK_IS_WINDOW (window));
555 screen = gdk_screen_get_default ();
556 window = gdk_screen_get_root_window (screen);
558 GDK_NOTE (MULTIHEAD, g_message ("gdk_property_delete(): window is NULL\n"));
562 if (GDK_WINDOW_DESTROYED (window))
565 display = gdk_drawable_get_display (window);
567 xproperty = gdk_x11_atom_to_xatom_for_display (display, property);
568 xtype = gdk_x11_atom_to_xatom_for_display (display, type);
569 xwindow = GDK_WINDOW_XID (window);
571 if (xtype == XA_ATOM ||
572 xtype == gdk_x11_get_xatom_by_name_for_display (display, "ATOM_PAIR"))
575 * data is an array of GdkAtom, we need to convert it
576 * to an array of X Atoms
579 GdkAtom *atoms = (GdkAtom*) data;
582 xatoms = g_new (Atom, nelements);
583 for (i = 0; i < nelements; i++)
584 xatoms[i] = gdk_x11_atom_to_xatom_for_display (display, atoms[i]);
586 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), xwindow,
588 format, mode, (guchar *)xatoms, nelements);
592 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), xwindow, xproperty,
593 xtype, format, mode, (guchar *)data, nelements);
597 gdk_property_delete (GdkWindow *window,
600 g_return_if_fail (!window || GDK_IS_WINDOW (window));
604 GdkScreen *screen = gdk_screen_get_default ();
605 window = gdk_screen_get_root_window (screen);
608 g_message ("gdk_property_delete(): window is NULL\n"));
611 if (GDK_WINDOW_DESTROYED (window))
614 XDeleteProperty (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XWINDOW (window),
615 gdk_x11_atom_to_xatom_for_display (GDK_WINDOW_DISPLAY (window),