Debian Codegraph @

gdk/x11/gdkproperty-x11.c

raw
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, see <http://www.gnu.org/licenses/>.
16 */
17
18 /*
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
23 */
24
25 #include "config.h"
26
27 #include "gdkproperty.h"
28 #include "gdkmain.h"
29 #include "gdkprivate.h"
30 #include "gdkinternals.h"
31 #include "gdkselection.h"
32 #include "gdkprivate-x11.h"
33 #include "gdkdisplay-x11.h"
34 #include "gdkscreen-x11.h"
35
36 #include <X11/Xlib.h>
37 #include <X11/Xatom.h>
38 #include <string.h>
39
40 #define N_PREDEFINED_ATOMS 69
41
42 #define ATOM_TO_INDEX(atom) (GPOINTER_TO_UINT(atom))
43 #define INDEX_TO_ATOM(atom) ((GdkAtom)GUINT_TO_POINTER(atom))
44
45 static void
46 insert_atom_pair (GdkDisplay *display,
47 GdkAtom virtual_atom,
48 Atom xatom)
49 {
50 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
51
52 if (!display_x11->atom_from_virtual)
53 {
54 display_x11->atom_from_virtual = g_hash_table_new (g_direct_hash, NULL);
55 display_x11->atom_to_virtual = g_hash_table_new (g_direct_hash, NULL);
56 }
57
58 g_hash_table_insert (display_x11->atom_from_virtual,
59 GDK_ATOM_TO_POINTER (virtual_atom),
60 GUINT_TO_POINTER (xatom));
61 g_hash_table_insert (display_x11->atom_to_virtual,
62 GUINT_TO_POINTER (xatom),
63 GDK_ATOM_TO_POINTER (virtual_atom));
64 }
65
66 static Atom
67 lookup_cached_xatom (GdkDisplay *display,
68 GdkAtom atom)
69 {
70 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
71
72 if (ATOM_TO_INDEX (atom) < N_PREDEFINED_ATOMS)
73 return ATOM_TO_INDEX (atom);
74
75 if (display_x11->atom_from_virtual)
76 return GPOINTER_TO_UINT (g_hash_table_lookup (display_x11->atom_from_virtual,
77 GDK_ATOM_TO_POINTER (atom)));
78
79 return None;
80 }
81
82 /**
83 * gdk_x11_atom_to_xatom_for_display:
84 * @display: (type GdkX11Display): A #GdkDisplay
85 * @atom: A #GdkAtom, or %GDK_NONE
86 *
87 * Converts from a #GdkAtom to the X atom for a #GdkDisplay
88 * with the same string value. The special value %GDK_NONE
89 * is converted to %None.
90 *
91 * Returns: the X atom corresponding to @atom, or %None
92 *
93 * Since: 2.2
94 **/
95 Atom
96 gdk_x11_atom_to_xatom_for_display (GdkDisplay *display,
97 GdkAtom atom)
98 {
99 Atom xatom = None;
100
101 g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
102
103 if (atom == GDK_NONE)
104 return None;
105
106 if (gdk_display_is_closed (display))
107 return None;
108
109 xatom = lookup_cached_xatom (display, atom);
110
111 if (!xatom)
112 {
113 char *name = gdk_atom_name (atom);
114
115 xatom = XInternAtom (GDK_DISPLAY_XDISPLAY (display), name, FALSE);
116 insert_atom_pair (display, atom, xatom);
117
118 g_free (name);
119 }
120
121 return xatom;
122 }
123
124 void
125 _gdk_x11_precache_atoms (GdkDisplay *display,
126 const gchar * const *atom_names,
127 gint n_atoms)
128 {
129 Atom *xatoms;
130 GdkAtom *atoms;
131 const gchar **xatom_names;
132 gint n_xatoms;
133 gint i;
134
135 xatoms = g_new (Atom, n_atoms);
136 xatom_names = g_new (const gchar *, n_atoms);
137 atoms = g_new (GdkAtom, n_atoms);
138
139 n_xatoms = 0;
140 for (i = 0; i < n_atoms; i++)
141 {
142 GdkAtom atom = gdk_atom_intern_static_string (atom_names[i]);
143 if (lookup_cached_xatom (display, atom) == None)
144 {
145 atoms[n_xatoms] = atom;
146 xatom_names[n_xatoms] = atom_names[i];
147 n_xatoms++;
148 }
149 }
150
151 if (n_xatoms)
152 XInternAtoms (GDK_DISPLAY_XDISPLAY (display),
153 (char **)xatom_names, n_xatoms, False, xatoms);
154
155 for (i = 0; i < n_xatoms; i++)
156 insert_atom_pair (display, atoms[i], xatoms[i]);
157
158 g_free (xatoms);
159 g_free (xatom_names);
160 g_free (atoms);
161 }
162
163 /**
164 * gdk_x11_atom_to_xatom:
165 * @atom: A #GdkAtom
166 *
167 * Converts from a #GdkAtom to the X atom for the default GDK display
168 * with the same string value.
169 *
170 * Returns: the X atom corresponding to @atom.
171 **/
172 Atom
173 gdk_x11_atom_to_xatom (GdkAtom atom)
174 {
175 return gdk_x11_atom_to_xatom_for_display (gdk_display_get_default (), atom);
176 }
177
178 /**
179 * gdk_x11_xatom_to_atom_for_display:
180 * @display: (type GdkX11Display): A #GdkDisplay
181 * @xatom: an X atom
182 *
183 * Convert from an X atom for a #GdkDisplay to the corresponding
184 * #GdkAtom.
185 *
186 * Returns: (transfer none): the corresponding #GdkAtom.
187 *
188 * Since: 2.2
189 **/
190 GdkAtom
191 gdk_x11_xatom_to_atom_for_display (GdkDisplay *display,
192 Atom xatom)
193 {
194 GdkX11Display *display_x11;
195 GdkAtom virtual_atom = GDK_NONE;
196
197 g_return_val_if_fail (GDK_IS_DISPLAY (display), GDK_NONE);
198
199 if (xatom == None)
200 return GDK_NONE;
201
202 if (gdk_display_is_closed (display))
203 return GDK_NONE;
204
205 display_x11 = GDK_X11_DISPLAY (display);
206
207 if (xatom < N_PREDEFINED_ATOMS)
208 return INDEX_TO_ATOM (xatom);
209
210 if (display_x11->atom_to_virtual)
211 virtual_atom = GDK_POINTER_TO_ATOM (g_hash_table_lookup (display_x11->atom_to_virtual,
212 GUINT_TO_POINTER (xatom)));
213
214 if (!virtual_atom)
215 {
216 /* If this atom doesn't exist, we'll die with an X error unless
217 * we take precautions
218 */
219 char *name;
220 gdk_x11_display_error_trap_push (display);
221 name = XGetAtomName (GDK_DISPLAY_XDISPLAY (display), xatom);
222 if (gdk_x11_display_error_trap_pop (display))
223 {
224 g_warning (G_STRLOC " invalid X atom: %ld", xatom);
225 }
226 else
227 {
228 virtual_atom = gdk_atom_intern (name, FALSE);
229 XFree (name);
230
231 insert_atom_pair (display, virtual_atom, xatom);
232 }
233 }
234
235 return virtual_atom;
236 }
237
238 /**
239 * gdk_x11_xatom_to_atom:
240 * @xatom: an X atom for the default GDK display
241 *
242 * Convert from an X atom for the default display to the corresponding
243 * #GdkAtom.
244 *
245 * Returns: (transfer none): the corresponding G#dkAtom.
246 **/
247 GdkAtom
248 gdk_x11_xatom_to_atom (Atom xatom)
249 {
250 return gdk_x11_xatom_to_atom_for_display (gdk_display_get_default (), xatom);
251 }
252
253 /**
254 * gdk_x11_get_xatom_by_name_for_display:
255 * @display: (type GdkX11Display): a #GdkDisplay
256 * @atom_name: a string
257 *
258 * Returns the X atom for a #GdkDisplay corresponding to @atom_name.
259 * This function caches the result, so if called repeatedly it is much
260 * faster than XInternAtom(), which is a round trip to the server each time.
261 *
262 * Returns: a X atom for a #GdkDisplay
263 *
264 * Since: 2.2
265 **/
266 Atom
267 gdk_x11_get_xatom_by_name_for_display (GdkDisplay *display,
268 const gchar *atom_name)
269 {
270 g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
271 return gdk_x11_atom_to_xatom_for_display (display,
272 gdk_atom_intern (atom_name, FALSE));
273 }
274
275 Atom
276 _gdk_x11_get_xatom_for_display_printf (GdkDisplay *display,
277 const gchar *format,
278 ...)
279 {
280 va_list args;
281 char *atom_name;
282 Atom atom;
283
284 va_start (args, format);
285 atom_name = g_strdup_vprintf (format, args);
286 va_end (args);
287
288 atom = gdk_x11_get_xatom_by_name_for_display (display, atom_name);
289
290 g_free (atom_name);
291
292 return atom;
293 }
294
295 /**
296 * gdk_x11_get_xatom_by_name:
297 * @atom_name: a string
298 *
299 * Returns the X atom for GDK’s default display corresponding to @atom_name.
300 * This function caches the result, so if called repeatedly it is much
301 * faster than XInternAtom(), which is a round trip to the server each time.
302 *
303 * Returns: a X atom for GDK’s default display.
304 **/
305 Atom
306 gdk_x11_get_xatom_by_name (const gchar *atom_name)
307 {
308 return gdk_x11_get_xatom_by_name_for_display (gdk_display_get_default (),
309 atom_name);
310 }
311
312 /**
313 * gdk_x11_get_xatom_name_for_display:
314 * @display: (type GdkX11Display): the #GdkDisplay where @xatom is defined
315 * @xatom: an X atom
316 *
317 * Returns the name of an X atom for its display. This
318 * function is meant mainly for debugging, so for convenience, unlike
319 * XAtomName() and gdk_atom_name(), the result doesn’t need to
320 * be freed.
321 *
322 * Returns: name of the X atom; this string is owned by GDK,
323 * so it shouldn’t be modifed or freed.
324 *
325 * Since: 2.2
326 **/
327 const gchar *
328 gdk_x11_get_xatom_name_for_display (GdkDisplay *display,
329 Atom xatom)
330 {
331 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
332
333 return _gdk_atom_name_const (gdk_x11_xatom_to_atom_for_display (display, xatom));
334 }
335
336 /**
337 * gdk_x11_get_xatom_name:
338 * @xatom: an X atom for GDK’s default display
339 *
340 * Returns the name of an X atom for GDK’s default display. This
341 * function is meant mainly for debugging, so for convenience, unlike
342 * XAtomName() and gdk_atom_name(), the result
343 * doesn’t need to be freed. Also, this function will never return %NULL,
344 * even if @xatom is invalid.
345 *
346 * Returns: name of the X atom; this string is owned by GTK+,
347 * so it shouldn’t be modifed or freed.
348 **/
349 const gchar *
350 gdk_x11_get_xatom_name (Atom xatom)
351 {
352 return _gdk_atom_name_const (gdk_x11_xatom_to_atom (xatom));
353 }
354
355 gboolean
356 _gdk_x11_window_get_property (GdkWindow *window,
357 GdkAtom property,
358 GdkAtom type,
359 gulong offset,
360 gulong length,
361 gint pdelete,
362 GdkAtom *actual_property_type,
363 gint *actual_format_type,
364 gint *actual_length,
365 guchar **data)
366 {
367 GdkDisplay *display;
368 Atom ret_prop_type;
369 gint ret_format;
370 gulong ret_nitems;
371 gulong ret_bytes_after;
372 gulong get_length;
373 gulong ret_length;
374 guchar *ret_data;
375 Atom xproperty;
376 Atom xtype;
377 int res;
378
379 g_return_val_if_fail (!window || GDK_WINDOW_IS_X11 (window), FALSE);
380
381 if (!window)
382 {
383 GdkScreen *screen = gdk_screen_get_default ();
384 window = gdk_screen_get_root_window (screen);
385 }
386 else if (!GDK_WINDOW_IS_X11 (window))
387 return FALSE;
388
389 if (GDK_WINDOW_DESTROYED (window))
390 return FALSE;
391
392 display = gdk_window_get_display (window);
393 xproperty = gdk_x11_atom_to_xatom_for_display (display, property);
394 if (type == GDK_NONE)
395 xtype = AnyPropertyType;
396 else
397 xtype = gdk_x11_atom_to_xatom_for_display (display, type);
398
399 ret_data = NULL;
400
401 /*
402 * Round up length to next 4 byte value. Some code is in the (bad?)
403 * habit of passing G_MAXLONG as the length argument, causing an
404 * overflow to negative on the add. In this case, we clamp the
405 * value to G_MAXLONG.
406 */
407 get_length = length + 3;
408 if (get_length > G_MAXLONG)
409 get_length = G_MAXLONG;
410
411 /* To fail, either the user passed 0 or G_MAXULONG */
412 get_length = get_length / 4;
413 if (get_length == 0)
414 {
415 g_warning ("gdk_propery-get(): invalid length 0");
416 return FALSE;
417 }
418
419 res = XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
420 GDK_WINDOW_XID (window), xproperty,
421 offset, get_length, pdelete,
422 xtype, &ret_prop_type, &ret_format,
423 &ret_nitems, &ret_bytes_after,
424 &ret_data);
425
426 if (res != Success || (ret_prop_type == None && ret_format == 0))
427 {
428 return FALSE;
429 }
430
431 if (actual_property_type)
432 *actual_property_type = gdk_x11_xatom_to_atom_for_display (display, ret_prop_type);
433 if (actual_format_type)
434 *actual_format_type = ret_format;
435
436 if ((xtype != AnyPropertyType) && (ret_prop_type != xtype))
437 {
438 XFree (ret_data);
439 g_warning ("Couldn't match property type %s to %s\n",
440 gdk_x11_get_xatom_name_for_display (display, ret_prop_type),
441 gdk_x11_get_xatom_name_for_display (display, xtype));
442 return FALSE;
443 }
444
445 /* FIXME: ignoring bytes_after could have very bad effects */
446
447 if (data)
448 {
449 if (ret_prop_type == XA_ATOM ||
450 ret_prop_type == gdk_x11_get_xatom_by_name_for_display (display, "ATOM_PAIR"))
451 {
452 /*
453 * data is an array of X atom, we need to convert it
454 * to an array of GDK Atoms
455 */
456 gint i;
457 GdkAtom *ret_atoms = g_new (GdkAtom, ret_nitems);
458 Atom *xatoms = (Atom *)ret_data;
459
460 *data = (guchar *)ret_atoms;
461
462 for (i = 0; i < ret_nitems; i++)
463 ret_atoms[i] = gdk_x11_xatom_to_atom_for_display (display, xatoms[i]);
464
465 if (actual_length)
466 *actual_length = ret_nitems * sizeof (GdkAtom);
467 }
468 else
469 {
470 switch (ret_format)
471 {
472 case 8:
473 ret_length = ret_nitems;
474 break;
475 case 16:
476 ret_length = sizeof(short) * ret_nitems;
477 break;
478 case 32:
479 ret_length = sizeof(long) * ret_nitems;
480 break;
481 default:
482 g_warning ("unknown property return format: %d", ret_format);
483 XFree (ret_data);
484 return FALSE;
485 }
486
487 *data = g_new (guchar, ret_length);
488 memcpy (*data, ret_data, ret_length);
489 if (actual_length)
490 *actual_length = ret_length;
491 }
492 }
493
494 XFree (ret_data);
495
496 return TRUE;
497 }
498
499 void
500 _gdk_x11_window_change_property (GdkWindow *window,
501 GdkAtom property,
502 GdkAtom type,
503 gint format,
504 GdkPropMode mode,
505 const guchar *data,
506 gint nelements)
507 {
508 GdkDisplay *display;
509 Window xwindow;
510 Atom xproperty;
511 Atom xtype;
512
513 g_return_if_fail (!window || GDK_WINDOW_IS_X11 (window));
514
515 if (!window)
516 {
517 GdkScreen *screen;
518
519 screen = gdk_screen_get_default ();
520 window = gdk_screen_get_root_window (screen);
521 }
522 else if (!GDK_WINDOW_IS_X11 (window))
523 return;
524
525 if (GDK_WINDOW_DESTROYED (window))
526 return;
527
528 gdk_window_ensure_native (window);
529
530 display = gdk_window_get_display (window);
531 xproperty = gdk_x11_atom_to_xatom_for_display (display, property);
532 xtype = gdk_x11_atom_to_xatom_for_display (display, type);
533 xwindow = GDK_WINDOW_XID (window);
534
535 if (xtype == XA_ATOM ||
536 xtype == gdk_x11_get_xatom_by_name_for_display (display, "ATOM_PAIR"))
537 {
538 /*
539 * data is an array of GdkAtom, we need to convert it
540 * to an array of X Atoms
541 */
542 gint i;
543 GdkAtom *atoms = (GdkAtom*) data;
544 Atom *xatoms;
545
546 xatoms = g_new (Atom, nelements);
547 for (i = 0; i < nelements; i++)
548 xatoms[i] = gdk_x11_atom_to_xatom_for_display (display, atoms[i]);
549
550 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), xwindow,
551 xproperty, xtype,
552 format, mode, (guchar *)xatoms, nelements);
553 g_free (xatoms);
554 }
555 else
556 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), xwindow, xproperty,
557 xtype, format, mode, (guchar *)data, nelements);
558 }
559
560 void
561 _gdk_x11_window_delete_property (GdkWindow *window,
562 GdkAtom property)
563 {
564 g_return_if_fail (!window || GDK_WINDOW_IS_X11 (window));
565
566 if (!window)
567 {
568 GdkScreen *screen = gdk_screen_get_default ();
569 window = gdk_screen_get_root_window (screen);
570 }
571 else if (!GDK_WINDOW_IS_X11 (window))
572 return;
573
574 if (GDK_WINDOW_DESTROYED (window))
575 return;
576
577 XDeleteProperty (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XID (window),
578 gdk_x11_atom_to_xatom_for_display (GDK_WINDOW_DISPLAY (window),
579 property));
580 }