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, see <http://www.gnu.org/licenses/>.
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/.
27 #include "gdkselection.h"
28 #include "gdkproperty.h"
29 #include "gdkprivate.h"
30 #include "gdkprivate-x11.h"
31 #include "gdkdisplay-x11.h"
34 #include <X11/Xatom.h>
38 typedef struct _OwnerInfo OwnerInfo;
47 static GSList *owner_list;
49 /* When a window is destroyed we check if it is the owner
50 * of any selections. This is somewhat inefficient, but
51 * owner_list is typically short, and it is a low memory,
55 _gdk_x11_selection_window_destroyed (GdkWindow *window)
57 GSList *tmp_list = owner_list;
60 OwnerInfo *info = tmp_list->data;
61 tmp_list = tmp_list->next;
63 if (info->owner == window)
65 owner_list = g_slist_remove (owner_list, info);
71 /* We only pass through those SelectionClear events that actually
72 * reflect changes to the selection owner that we didn't make ourself.
75 _gdk_x11_selection_filter_clear_event (XSelectionClearEvent *event)
77 GSList *tmp_list = owner_list;
78 GdkDisplay *display = gdk_x11_lookup_xdisplay (event->display);
82 OwnerInfo *info = tmp_list->data;
84 if (gdk_window_get_display (info->owner) == display &&
85 info->selection == gdk_x11_xatom_to_atom_for_display (display, event->selection))
87 if ((GDK_WINDOW_XID (info->owner) == event->window &&
88 event->serial >= info->serial))
90 owner_list = g_slist_remove (owner_list, info);
97 tmp_list = tmp_list->next;
104 _gdk_x11_display_set_selection_owner (GdkDisplay *display,
116 if (gdk_display_is_closed (display))
121 if (GDK_WINDOW_DESTROYED (owner) || !GDK_WINDOW_IS_X11 (owner))
124 gdk_window_ensure_native (owner);
125 xdisplay = GDK_WINDOW_XDISPLAY (owner);
126 xwindow = GDK_WINDOW_XID (owner);
130 xdisplay = GDK_DISPLAY_XDISPLAY (display);
134 xselection = gdk_x11_atom_to_xatom_for_display (display, selection);
136 tmp_list = owner_list;
139 info = tmp_list->data;
140 if (info->selection == selection)
142 owner_list = g_slist_remove (owner_list, info);
146 tmp_list = tmp_list->next;
151 info = g_new (OwnerInfo, 1);
153 info->serial = NextRequest (GDK_WINDOW_XDISPLAY (owner));
154 info->selection = selection;
156 owner_list = g_slist_prepend (owner_list, info);
159 XSetSelectionOwner (xdisplay, xselection, xwindow, time);
161 return (XGetSelectionOwner (xdisplay, xselection) == xwindow);
165 _gdk_x11_display_get_selection_owner (GdkDisplay *display,
170 if (gdk_display_is_closed (display))
173 xwindow = XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display),
174 gdk_x11_atom_to_xatom_for_display (display,
179 return gdk_x11_window_lookup_for_display (display, xwindow);
183 _gdk_x11_display_convert_selection (GdkDisplay *display,
184 GdkWindow *requestor,
189 g_return_if_fail (selection != GDK_NONE);
191 if (GDK_WINDOW_DESTROYED (requestor) || !GDK_WINDOW_IS_X11 (requestor))
194 gdk_window_ensure_native (requestor);
196 XConvertSelection (GDK_WINDOW_XDISPLAY (requestor),
197 gdk_x11_atom_to_xatom_for_display (display, selection),
198 gdk_x11_atom_to_xatom_for_display (display, target),
199 gdk_x11_get_xatom_by_name_for_display (display, "GDK_SELECTION"),
200 GDK_WINDOW_XID (requestor), time);
204 _gdk_x11_display_get_selection_property (GdkDisplay *display,
205 GdkWindow *requestor,
217 if (GDK_WINDOW_DESTROYED (requestor) || !GDK_WINDOW_IS_X11 (requestor))
222 /* We can't delete the selection here, because it might be the INCR
223 protocol, in which case the client has to make sure they'll be
224 notified of PropertyChange events _before_ the property is deleted.
225 Otherwise there's no guarantee we'll win the race ... */
226 if (XGetWindowProperty (GDK_WINDOW_XDISPLAY (requestor),
227 GDK_WINDOW_XID (requestor),
228 gdk_x11_get_xatom_by_name_for_display (display, "GDK_SELECTION"),
229 0, 0x1FFFFFFF /* MAXINT32 / 4 */, False,
230 AnyPropertyType, &prop_type, &prop_format,
231 &nitems, &nbytes, &t) != Success)
234 if (prop_type != None)
237 *ret_type = gdk_x11_xatom_to_atom_for_display (display, prop_type);
239 *ret_format = prop_format;
241 if (prop_type == XA_ATOM ||
242 prop_type == gdk_x11_get_xatom_by_name_for_display (display, "ATOM_PAIR"))
244 Atom* atoms = (Atom*) t;
248 if (prop_format != 32)
252 length = sizeof (GdkAtom) * num_atom + 1;
256 *data = g_malloc (length);
257 (*data)[length - 1] = '\0';
258 atoms_dest = (GdkAtom *)(*data);
260 for (i=0; i < num_atom; i++)
261 atoms_dest[i] = gdk_x11_xatom_to_atom_for_display (display, atoms[i]);
272 length = sizeof(short) * nitems;
275 length = sizeof(long) * nitems;
278 g_assert_not_reached ();
282 /* Add on an extra byte to handle null termination. X guarantees
283 that t will be 1 longer than nitems and null terminated */
287 *data = g_memdup (t, length);
298 *ret_type = GDK_NONE;
308 _gdk_x11_display_send_selection_notify (GdkDisplay *display,
309 GdkWindow *requestor,
315 XSelectionEvent xevent;
317 xevent.type = SelectionNotify;
319 xevent.send_event = True;
320 xevent.requestor = GDK_WINDOW_XID (requestor);
321 xevent.selection = gdk_x11_atom_to_xatom_for_display (display, selection);
322 xevent.target = gdk_x11_atom_to_xatom_for_display (display, target);
323 if (property == GDK_NONE)
324 xevent.property = None;
326 xevent.property = gdk_x11_atom_to_xatom_for_display (display, property);
329 _gdk_x11_display_send_xevent (display, xevent.requestor, False, NoEventMask, (XEvent*) & xevent);
333 * gdk_x11_display_text_property_to_text_list:
334 * @display: (type GdkX11Display): The #GdkDisplay where the encoding is defined
335 * @encoding: an atom representing the encoding. The most
336 * common values for this are STRING, or COMPOUND_TEXT.
337 * This is value used as the type for the property
338 * @format: the format of the property
339 * @text: The text data
340 * @length: The number of items to transform
341 * @list: location to store an array of strings in
342 * the encoding of the current locale. This array should be
343 * freed using gdk_free_text_list().
345 * Convert a text string from the encoding as it is stored
346 * in a property into an array of strings in the encoding of
347 * the current locale. (The elements of the array represent the
348 * nul-separated elements of the original text string.)
350 * Returns: the number of strings stored in list, or 0,
351 * if the conversion failed
356 gdk_x11_display_text_property_to_text_list (GdkDisplay *display,
363 XTextProperty property;
367 g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
372 if (gdk_display_is_closed (display))
375 property.value = (guchar *)text;
376 property.encoding = gdk_x11_atom_to_xatom_for_display (display, encoding);
377 property.format = format;
378 property.nitems = length;
379 res = XmbTextPropertyToTextList (GDK_DISPLAY_XDISPLAY (display), &property,
380 &local_list, &count);
381 if (res == XNoMemory || res == XLocaleNotSupported || res == XConverterNotFound)
388 XFreeStringList (local_list);
395 * gdk_x11_free_text_list:
396 * @list: the value stored in the @list parameter by
397 * a call to gdk_x11_display_text_property_to_text_list().
399 * Frees the array of strings created by
400 * gdk_x11_display_text_property_to_text_list().
405 gdk_x11_free_text_list (gchar **list)
407 g_return_if_fail (list != NULL);
409 XFreeStringList (list);
413 make_list (const gchar *text,
418 GSList *strings = NULL;
421 const gchar *p = text;
424 GError *error = NULL;
426 while (p < text + length)
431 while (*q && q < text + length)
436 str = g_convert (p, q - p,
437 "UTF-8", "ISO-8859-1",
442 g_warning ("Error converting selection from STRING: %s",
444 g_error_free (error);
449 str = g_strndup (p, q - p);
450 if (!g_utf8_validate (str, -1, NULL))
452 g_warning ("Error converting selection from UTF8_STRING");
460 strings = g_slist_prepend (strings, str);
469 *list = g_new (gchar *, n_strings + 1);
470 (*list)[n_strings] = NULL;
478 (*list)[--i] = tmp_list->data;
480 g_free (tmp_list->data);
482 tmp_list = tmp_list->next;
485 g_slist_free (strings);
491 _gdk_x11_display_text_property_to_utf8_list (GdkDisplay *display,
498 if (encoding == GDK_TARGET_STRING)
500 return make_list ((gchar *)text, length, TRUE, list);
502 else if (encoding == gdk_atom_intern_static_string ("UTF8_STRING"))
504 return make_list ((gchar *)text, length, FALSE, list);
511 const gchar *charset = NULL;
512 gboolean need_conversion = !g_get_charset (&charset);
514 GError *error = NULL;
516 /* Probably COMPOUND text, we fall back to Xlib routines
518 local_count = gdk_x11_display_text_property_to_text_list (display,
525 *list = g_new (gchar *, local_count + 1);
527 for (i=0; i<local_count; i++)
529 /* list contains stuff in our default encoding
533 gchar *utf = g_convert (local_list[i], -1,
539 (*list)[count++] = utf;
545 g_warning ("Error converting to UTF-8 from '%s': %s",
546 charset, error->message);
547 g_error_free (error);
555 if (g_utf8_validate (local_list[i], -1, NULL))
556 (*list)[count++] = g_strdup (local_list[i]);
558 g_warning ("Error converting selection");
564 gdk_x11_free_text_list (local_list);
567 (*list)[count] = NULL;
574 * gdk_x11_display_string_to_compound_text:
575 * @display: (type GdkX11Display): the #GdkDisplay where the encoding is defined
576 * @str: a nul-terminated string
577 * @encoding: (out) (transfer none): location to store the encoding atom
578 * (to be used as the type for the property)
579 * @format: (out): location to store the format of the property
580 * @ctext: (out) (array length=length): location to store newly
581 * allocated data for the property
582 * @length: the length of @ctext, in bytes
584 * Convert a string from the encoding of the current
585 * locale into a form suitable for storing in a window property.
587 * Returns: 0 upon success, non-zero upon failure
592 gdk_x11_display_string_to_compound_text (GdkDisplay *display,
600 XTextProperty property;
602 g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
604 if (gdk_display_is_closed (display))
605 res = XLocaleNotSupported;
607 res = XmbTextListToTextProperty (GDK_DISPLAY_XDISPLAY (display),
608 (char **)&str, 1, XCompoundTextStyle,
612 property.encoding = None;
613 property.format = None;
614 property.value = NULL;
619 *encoding = gdk_x11_xatom_to_atom_for_display (display, property.encoding);
621 *format = property.format;
623 *ctext = property.value;
625 *length = property.nitems;
630 /* The specifications for COMPOUND_TEXT and STRING specify that C0 and
631 * C1 are not allowed except for \n and \t, however the X conversions
632 * routines for COMPOUND_TEXT only enforce this in one direction,
633 * causing cut-and-paste of \r and \r\n separated text to fail.
634 * This routine strips out all non-allowed C0 and C1 characters
635 * from the input string and also canonicalizes \r, and \r\n to \n
638 sanitize_utf8 (const gchar *src,
639 gboolean return_latin1)
641 gint len = strlen (src);
642 GString *result = g_string_sized_new (len);
643 const gchar *p = src;
653 g_string_append_c (result, '\n');
657 gunichar ch = g_utf8_get_char (p);
659 if (!((ch < 0x20 && ch != '\t' && ch != '\n') || (ch >= 0x7f && ch < 0xa0)))
664 g_string_append_c (result, ch);
666 g_string_append_printf (result,
667 ch < 0x10000 ? "\\u%04x" : "\\U%08x",
675 buflen = g_unichar_to_utf8 (ch, buf);
676 g_string_append_len (result, buf, buflen);
680 p = g_utf8_next_char (p);
684 return g_string_free (result, FALSE);
688 _gdk_x11_display_utf8_to_string_target (GdkDisplay *display,
691 return sanitize_utf8 (str, TRUE);
695 * gdk_x11_display_utf8_to_compound_text:
696 * @display: (type GdkX11Display): a #GdkDisplay
697 * @str: a UTF-8 string
698 * @encoding: (out): location to store resulting encoding
699 * @format: (out): location to store format of the result
700 * @ctext: (out) (array length=length): location to store the data of the result
701 * @length: location to store the length of the data
704 * Converts from UTF-8 to compound text.
706 * Return value: %TRUE if the conversion succeeded,
712 gdk_x11_display_utf8_to_compound_text (GdkDisplay *display,
719 gboolean need_conversion;
720 const gchar *charset;
721 gchar *locale_str, *tmp_str;
722 GError *error = NULL;
725 g_return_val_if_fail (str != NULL, FALSE);
726 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
728 need_conversion = !g_get_charset (&charset);
730 tmp_str = sanitize_utf8 (str, FALSE);
734 locale_str = g_convert (tmp_str, -1,
741 if (!(error->domain = G_CONVERT_ERROR &&
742 error->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
744 g_warning ("Error converting from UTF-8 to '%s': %s",
745 charset, error->message);
747 g_error_free (error);
762 locale_str = tmp_str;
764 result = gdk_x11_display_string_to_compound_text (display, locale_str,
767 result = (result == Success? TRUE : FALSE);
775 * gdk_x11_free_compound_text:
776 * @ctext: The pointer stored in @ctext from a call to
777 * gdk_x11_display_string_to_compound_text().
779 * Frees the data returned from gdk_x11_display_string_to_compound_text().
784 gdk_x11_free_compound_text (guchar *ctext)