]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkproperty-x11.c
Use asynchronously _gdk_x11_set_input_focus_safe to avoid having to trap
[~andy/gtk] / gdk / x11 / gdkproperty-x11.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #include <X11/Xlib.h>
28 #include <X11/Xatom.h>
29 #include <string.h>
30
31 #include "gdk.h"          /* For gdk_error_trap_push/pop() */
32 #include "gdkx.h"
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 */
39
40 static GPtrArray *virtual_atom_array;
41 static GHashTable *virtual_atom_hash;
42
43 static gchar *XAtomsStrings[] = {
44   /* These are all the standard predefined X atoms */
45   "NONE",
46   "PRIMARY",
47   "SECONDARY",
48   "ARC",
49   "ATOM",
50   "BITMAP",
51   "CARDINAL",
52   "COLORMAP",
53   "CURSOR",
54   "CUT_BUFFER0",
55   "CUT_BUFFER1",
56   "CUT_BUFFER2",
57   "CUT_BUFFER3",
58   "CUT_BUFFER4",
59   "CUT_BUFFER5",
60   "CUT_BUFFER6",
61   "CUT_BUFFER7",
62   "DRAWABLE",
63   "FONT",
64   "INTEGER",
65   "PIXMAP",
66   "POINT",
67   "RECTANGLE",
68   "RESOURCE_MANAGER",
69   "RGB_COLOR_MAP",
70   "RGB_BEST_MAP",
71   "RGB_BLUE_MAP",
72   "RGB_DEFAULT_MAP",
73   "RGB_GRAY_MAP",
74   "RGB_GREEN_MAP",
75   "RGB_RED_MAP",
76   "STRING",
77   "VISUALID",
78   "WINDOW",
79   "WM_COMMAND",
80   "WM_HINTS",
81   "WM_CLIENT_MACHINE",
82   "WM_ICON_NAME",
83   "WM_ICON_SIZE",
84   "WM_NAME",
85   "WM_NORMAL_HINTS",
86   "WM_SIZE_HINTS",
87   "WM_ZOOM_HINTS",
88   "MIN_SPACE",
89   "NORM_SPACE",
90   "MAX_SPACE",
91   "END_SPACE",
92   "SUPERSCRIPT_X",
93   "SUPERSCRIPT_Y",
94   "SUBSCRIPT_X",
95   "SUBSCRIPT_Y",
96   "UNDERLINE_POSITION",
97   "UNDERLINE_THICKNESS",
98   "STRIKEOUT_ASCENT",
99   "STRIKEOUT_DESCENT",
100   "ITALIC_ANGLE",
101   "X_HEIGHT",
102   "QUAD_WIDTH",
103   "WEIGHT",
104   "POINT_SIZE",
105   "RESOLUTION",
106   "COPYRIGHT",
107   "NOTICE",
108   "FONT_NAME",
109   "FAMILY_NAME",
110   "FULL_NAME",
111   "CAP_HEIGHT",
112   "WM_CLASS",
113   "WM_TRANSIENT_FOR",
114   /* Below here, these are our additions. Increment N_CUSTOM_PREDEFINED
115    * if you add any.
116    */
117   "CLIPBOARD"                   /* = 69 */
118 };
119
120 #define N_CUSTOM_PREDEFINED 1
121
122 #define ATOM_TO_INDEX(atom) (GPOINTER_TO_UINT(atom))
123 #define INDEX_TO_ATOM(atom) ((GdkAtom)GUINT_TO_POINTER(atom))
124
125 static void
126 insert_atom_pair (GdkDisplay *display,
127                   GdkAtom     virtual_atom,
128                   Atom        xatom)
129 {
130   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);  
131   
132   if (!display_x11->atom_from_virtual)
133     {
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);
136     }
137   
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));
144 }
145
146 static Atom
147 lookup_cached_xatom (GdkDisplay *display,
148                      GdkAtom     atom)
149 {
150   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
151
152   if (ATOM_TO_INDEX (atom) < G_N_ELEMENTS (XAtomsStrings) - N_CUSTOM_PREDEFINED)
153     return ATOM_TO_INDEX (atom);
154   
155   if (display_x11->atom_from_virtual)
156     return GPOINTER_TO_UINT (g_hash_table_lookup (display_x11->atom_from_virtual,
157                                                   GDK_ATOM_TO_POINTER (atom)));
158
159   return None;
160 }
161
162 /**
163  * gdk_x11_atom_to_xatom_for_display:
164  * @display: A #GdkDisplay
165  * @atom: A #GdkAtom 
166  * 
167  * Converts from a #GdkAtom to the X atom for a #GdkDisplay
168  * with the same string value.
169  * 
170  * Return value: the X atom corresponding to @atom.
171  *
172  * Since: 2.2
173  **/
174 Atom
175 gdk_x11_atom_to_xatom_for_display (GdkDisplay *display, 
176                                    GdkAtom atom)
177 {
178   Atom xatom = None;
179   
180   g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
181
182   if (display->closed)
183     return None;
184   
185   xatom = lookup_cached_xatom (display, atom);
186   
187   if (!xatom)
188     {
189       char *name;
190       
191       g_return_val_if_fail (ATOM_TO_INDEX (atom) < virtual_atom_array->len, None);
192
193       name = g_ptr_array_index (virtual_atom_array, ATOM_TO_INDEX (atom));
194       
195       xatom = XInternAtom (GDK_DISPLAY_XDISPLAY (display), name, FALSE);
196       insert_atom_pair (display, atom, xatom);
197     }
198
199   return xatom;
200 }
201
202 void
203 _gdk_x11_precache_atoms (GdkDisplay          *display,
204                          const gchar * const *atom_names,
205                          gint                 n_atoms)
206 {
207   Atom *xatoms;
208   GdkAtom *atoms;
209   const gchar **xatom_names;
210   gint n_xatoms;
211   gint i;
212
213   xatoms = g_new (Atom, n_atoms);
214   xatom_names = g_new (const gchar *, n_atoms);
215   atoms = g_new (GdkAtom, n_atoms);
216
217   n_xatoms = 0;
218   for (i = 0; i < n_atoms; i++)
219     {
220       GdkAtom atom = gdk_atom_intern (atom_names[i], FALSE);
221       if (lookup_cached_xatom (display, atom) == None)
222         {
223           atoms[n_xatoms] = atom;
224           xatom_names[n_xatoms] = atom_names[i];
225           n_xatoms++;
226         }
227     }
228
229   if (n_xatoms)
230     XInternAtoms (GDK_DISPLAY_XDISPLAY (display),
231                   (char **)xatom_names, n_xatoms, False, xatoms);
232
233   for (i = 0; i < n_xatoms; i++)
234     insert_atom_pair (display, atoms[i], xatoms[i]);
235
236   g_free (xatoms);
237   g_free (atoms);
238   g_free (atom_names);
239 }
240
241 /**
242  * gdk_x11_atom_to_xatom:
243  * @atom: A #GdkAtom 
244  * 
245  * Converts from a #GdkAtom to the X atom for the default GDK display
246  * with the same string value.
247  * 
248  * Return value: the X atom corresponding to @atom.
249  **/
250 Atom
251 gdk_x11_atom_to_xatom (GdkAtom atom)
252 {
253   return gdk_x11_atom_to_xatom_for_display (gdk_display_get_default (), atom);
254 }
255
256 /**
257  * gdk_x11_xatom_to_atom_for_display:
258  * @display: A #GdkDisplay
259  * @xatom: an X atom 
260  * 
261  * Convert from an X atom for a #GdkDisplay to the corresponding
262  * #GdkAtom.
263  * 
264  * Return value: the corresponding #GdkAtom.
265  *
266  * Since: 2.2
267  **/
268 GdkAtom
269 gdk_x11_xatom_to_atom_for_display (GdkDisplay *display,
270                                    Atom        xatom)
271 {
272   GdkDisplayX11 *display_x11;
273   GdkAtom virtual_atom = GDK_NONE;
274   
275   g_return_val_if_fail (GDK_IS_DISPLAY (display), GDK_NONE);
276
277   if (display->closed)
278     return GDK_NONE;
279
280   display_x11 = GDK_DISPLAY_X11 (display);
281   
282   if (xatom < G_N_ELEMENTS (XAtomsStrings) - N_CUSTOM_PREDEFINED)
283     return INDEX_TO_ATOM (xatom);
284   
285   if (display_x11->atom_to_virtual)
286     virtual_atom = GDK_POINTER_TO_ATOM (g_hash_table_lookup (display_x11->atom_to_virtual,
287                                                              GUINT_TO_POINTER (xatom)));
288   
289   if (!virtual_atom)
290     {
291       /* If this atom doesn't exist, we'll die with an X error unless
292        * we take precautions
293        */
294       char *name;
295       gdk_error_trap_push ();
296       name = XGetAtomName (GDK_DISPLAY_XDISPLAY (display), xatom);
297       if (gdk_error_trap_pop ())
298         {
299           g_warning (G_STRLOC " invalid X atom: %ld", xatom);
300         }
301       else
302         {
303           virtual_atom = gdk_atom_intern (name, FALSE);
304           XFree (name);
305           
306           insert_atom_pair (display, virtual_atom, xatom);
307         }
308     }
309
310   return virtual_atom;
311 }
312
313 /**
314  * gdk_x11_xatom_to_atom:
315  * @xatom: an X atom for the default GDK display
316  * 
317  * Convert from an X atom for the default display to the corresponding
318  * #GdkAtom.
319  * 
320  * Return value: the corresponding G#dkAtom.
321  **/
322 GdkAtom
323 gdk_x11_xatom_to_atom (Atom xatom)
324 {
325   return gdk_x11_xatom_to_atom_for_display (gdk_display_get_default (), xatom);
326 }
327
328 static void
329 virtual_atom_check_init (void)
330 {
331   if (!virtual_atom_hash)
332     {
333       gint i;
334       
335       virtual_atom_hash = g_hash_table_new (g_str_hash, g_str_equal);
336       virtual_atom_array = g_ptr_array_new ();
337       
338       for (i = 0; i < G_N_ELEMENTS (XAtomsStrings); i++)
339         {
340           g_ptr_array_add (virtual_atom_array, XAtomsStrings[i]);
341           g_hash_table_insert (virtual_atom_hash, XAtomsStrings[i],
342                                GUINT_TO_POINTER (i));
343         }
344     }
345 }
346
347 GdkAtom
348 gdk_atom_intern (const gchar *atom_name, 
349                  gboolean     only_if_exists)
350 {
351   GdkAtom result;
352
353   virtual_atom_check_init ();
354   
355   result = GDK_POINTER_TO_ATOM (g_hash_table_lookup (virtual_atom_hash, atom_name));
356   if (!result)
357     {
358       result = INDEX_TO_ATOM (virtual_atom_array->len);
359       
360       g_ptr_array_add (virtual_atom_array, g_strdup (atom_name));
361       g_hash_table_insert (virtual_atom_hash, 
362                            g_ptr_array_index (virtual_atom_array,
363                                               ATOM_TO_INDEX (result)),
364                            GDK_ATOM_TO_POINTER (result));
365     }
366
367   return result;
368 }
369
370 static G_CONST_RETURN char *
371 get_atom_name (GdkAtom atom)
372 {
373   virtual_atom_check_init ();
374
375   if (ATOM_TO_INDEX (atom) < virtual_atom_array->len)
376     return g_ptr_array_index (virtual_atom_array, ATOM_TO_INDEX (atom));
377   else
378     return NULL;
379 }
380
381 gchar *
382 gdk_atom_name (GdkAtom atom)
383 {
384   return g_strdup (get_atom_name (atom));
385 }
386
387 /**
388  * gdk_x11_get_xatom_by_name_for_display:
389  * @display: a #GdkDisplay
390  * @atom_name: a string
391  * 
392  * Returns the X atom for a #GdkDisplay corresponding to @atom_name.
393  * This function caches the result, so if called repeatedly it is much
394  * faster than XInternAtom(), which is a round trip to the server each time.
395  * 
396  * Return value: a X atom for a #GdkDisplay
397  *
398  * Since: 2.2
399  **/
400 Atom
401 gdk_x11_get_xatom_by_name_for_display (GdkDisplay  *display,
402                                        const gchar *atom_name)
403 {
404   g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
405   return gdk_x11_atom_to_xatom_for_display (display,
406                                             gdk_atom_intern (atom_name, FALSE));
407 }
408
409 /**
410  * gdk_x11_get_xatom_by_name:
411  * @atom_name: a string
412  * 
413  * Returns the X atom for GDK's default display corresponding to @atom_name.
414  * This function caches the result, so if called repeatedly it is much
415  * faster than <function>XInternAtom()</function>, which is a round trip to 
416  * the server each time.
417  * 
418  * Return value: a X atom for GDK's default display.
419  **/
420 Atom
421 gdk_x11_get_xatom_by_name (const gchar *atom_name)
422 {
423   return gdk_x11_get_xatom_by_name_for_display (gdk_display_get_default (),
424                                                 atom_name);
425 }
426
427 /**
428  * gdk_x11_get_xatom_name_for_display:
429  * @display: the #GdkDisplay where @xatom is defined
430  * @xatom: an X atom 
431  * 
432  * Returns the name of an X atom for its display. This
433  * function is meant mainly for debugging, so for convenience, unlike
434  * XAtomName() and gdk_atom_name(), the result doesn't need to
435  * be freed. 
436  *
437  * Return value: name of the X atom; this string is owned by GDK,
438  *   so it shouldn't be modifed or freed. 
439  *
440  * Since: 2.2
441  **/
442 G_CONST_RETURN gchar *
443 gdk_x11_get_xatom_name_for_display (GdkDisplay *display,
444                                     Atom        xatom)
445 {
446   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
447
448   return get_atom_name (gdk_x11_xatom_to_atom_for_display (display, xatom));
449 }
450
451 /**
452  * gdk_x11_get_xatom_name:
453  * @xatom: an X atom for GDK's default display
454  * 
455  * Returns the name of an X atom for GDK's default display. This
456  * function is meant mainly for debugging, so for convenience, unlike
457  * <function>XAtomName()</function> and gdk_atom_name(), the result 
458  * doesn't need to be freed. Also, this function will never return %NULL, 
459  * even if @xatom is invalid.
460  * 
461  * Return value: name of the X atom; this string is owned by GTK+,
462  *   so it shouldn't be modifed or freed. 
463  **/
464 G_CONST_RETURN gchar *
465 gdk_x11_get_xatom_name (Atom xatom)
466 {
467   return get_atom_name (gdk_x11_xatom_to_atom (xatom));
468 }
469
470 gboolean
471 gdk_property_get (GdkWindow   *window,
472                   GdkAtom      property,
473                   GdkAtom      type,
474                   gulong       offset,
475                   gulong       length,
476                   gint         pdelete,
477                   GdkAtom     *actual_property_type,
478                   gint        *actual_format_type,
479                   gint        *actual_length,
480                   guchar     **data)
481 {
482   GdkDisplay *display;
483   Atom ret_prop_type;
484   gint ret_format;
485   gulong ret_nitems;
486   gulong ret_bytes_after;
487   gulong ret_length;
488   guchar *ret_data;
489   Atom xproperty;
490   Atom xtype;
491   int res;
492
493   g_return_val_if_fail (!window || GDK_IS_WINDOW (window), FALSE);
494
495   if (!window)
496     {
497       GdkScreen *screen = gdk_screen_get_default ();
498       window = gdk_screen_get_root_window (screen);
499       
500       GDK_NOTE (MULTIHEAD, g_message ("gdk_property_get(): window is NULL\n"));
501     }
502
503   if (GDK_WINDOW_DESTROYED (window))
504     return FALSE;
505
506   display = gdk_drawable_get_display (window);
507   xproperty = gdk_x11_atom_to_xatom_for_display (display, property);
508   xtype = gdk_x11_atom_to_xatom_for_display (display, type);
509
510   ret_data = NULL;
511   
512   res = XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
513                             GDK_WINDOW_XWINDOW (window), xproperty,
514                             offset, (length + 3) / 4, pdelete,
515                             xtype, &ret_prop_type, &ret_format,
516                             &ret_nitems, &ret_bytes_after,
517                             &ret_data);
518
519   if (res != Success || (ret_prop_type == None && ret_format == 0))
520     {
521       return FALSE;
522     }
523
524   if (actual_property_type)
525     *actual_property_type = gdk_x11_xatom_to_atom_for_display (display, ret_prop_type);
526   if (actual_format_type)
527     *actual_format_type = ret_format;
528
529   if ((xtype != AnyPropertyType) && (ret_prop_type != xtype))
530     {
531       XFree (ret_data);
532       g_warning ("Couldn't match property type %s to %s\n", 
533                  gdk_x11_get_xatom_name_for_display (display, ret_prop_type), 
534                  gdk_x11_get_xatom_name_for_display (display, xtype));
535       return FALSE;
536     }
537
538   /* FIXME: ignoring bytes_after could have very bad effects */
539
540   if (data)
541     {
542       if (ret_prop_type == XA_ATOM ||
543           ret_prop_type == gdk_x11_get_xatom_by_name_for_display (display, "ATOM_PAIR"))
544         {
545           /*
546            * data is an array of X atom, we need to convert it
547            * to an array of GDK Atoms
548            */
549           gint i;
550           GdkAtom *ret_atoms = g_new (GdkAtom, ret_nitems);
551           Atom *xatoms = (Atom *)ret_data;
552
553           *data = (guchar *)ret_atoms;
554
555           for (i = 0; i < ret_nitems; i++)
556             ret_atoms[i] = gdk_x11_xatom_to_atom_for_display (display, xatoms[i]);
557           
558           if (actual_length)
559             *actual_length = ret_nitems * sizeof (GdkAtom);
560         }
561       else
562         {
563           switch (ret_format)
564             {
565             case 8:
566               ret_length = ret_nitems;
567               break;
568             case 16:
569               ret_length = sizeof(short) * ret_nitems;
570               break;
571             case 32:
572               ret_length = sizeof(long) * ret_nitems;
573               break;
574             default:
575               g_warning ("unknown property return format: %d", ret_format);
576               XFree (ret_data);
577               return FALSE;
578             }
579           
580           *data = g_new (guchar, ret_length);
581           memcpy (*data, ret_data, ret_length);
582           if (actual_length)
583             *actual_length = ret_length;
584         }
585     }
586
587   XFree (ret_data);
588
589   return TRUE;
590 }
591
592 void
593 gdk_property_change (GdkWindow    *window,
594                      GdkAtom       property,
595                      GdkAtom       type,
596                      gint          format,
597                      GdkPropMode   mode,
598                      const guchar *data,
599                      gint          nelements)
600 {
601   GdkDisplay *display;
602   Window xwindow;
603   Atom xproperty;
604   Atom xtype;
605
606   g_return_if_fail (!window || GDK_IS_WINDOW (window));
607
608   if (!window)
609     {
610       GdkScreen *screen;
611       
612       screen = gdk_screen_get_default ();
613       window = gdk_screen_get_root_window (screen);
614       
615       GDK_NOTE (MULTIHEAD, g_message ("gdk_property_delete(): window is NULL\n"));
616     }
617
618
619   if (GDK_WINDOW_DESTROYED (window))
620     return;
621
622   display = gdk_drawable_get_display (window);
623   
624   xproperty = gdk_x11_atom_to_xatom_for_display (display, property);
625   xtype = gdk_x11_atom_to_xatom_for_display (display, type);
626   xwindow = GDK_WINDOW_XID (window);
627
628   if (xtype == XA_ATOM ||
629       xtype == gdk_x11_get_xatom_by_name_for_display (display, "ATOM_PAIR"))
630     {
631       /*
632        * data is an array of GdkAtom, we need to convert it
633        * to an array of X Atoms
634        */
635       gint i;
636       GdkAtom *atoms = (GdkAtom*) data;
637       Atom *xatoms;
638
639       xatoms = g_new (Atom, nelements);
640       for (i = 0; i < nelements; i++)
641         xatoms[i] = gdk_x11_atom_to_xatom_for_display (display, atoms[i]);
642
643       XChangeProperty (GDK_DISPLAY_XDISPLAY (display), xwindow,
644                        xproperty, xtype,
645                        format, mode, (guchar *)xatoms, nelements);
646       g_free (xatoms);
647     }
648   else
649     XChangeProperty (GDK_DISPLAY_XDISPLAY (display), xwindow, xproperty, 
650                      xtype, format, mode, (guchar *)data, nelements);
651 }
652
653 void
654 gdk_property_delete (GdkWindow *window,
655                      GdkAtom    property)
656 {
657   g_return_if_fail (!window || GDK_IS_WINDOW (window));
658
659   if (!window)
660     {
661       GdkScreen *screen = gdk_screen_get_default ();
662       window = gdk_screen_get_root_window (screen);
663       
664       GDK_NOTE (MULTIHEAD, 
665                 g_message ("gdk_property_delete(): window is NULL\n"));
666     }
667
668   if (GDK_WINDOW_DESTROYED (window))
669     return;
670
671   XDeleteProperty (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XWINDOW (window),
672                    gdk_x11_atom_to_xatom_for_display (GDK_WINDOW_DISPLAY (window),
673                                                       property));
674 }