CVS: rdesktop xclip.c,1.23,1.24

Erik Forsberg <[email protected]>
Newsgroups gmane.network.rdesktop.cvs
Message-ID <[email protected]>
Update of /cvsroot/rdesktop/rdesktop
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24071

Modified Files:
	xclip.c 
Log Message:
Applied patch [1] with unicode support in clipboard code from Ilya Konstantinov

[1] https://sourceforge.net/tracker/?func=detail&atid=381349&aid=1394324&group_id=24366


Index: xclip.c
===================================================================
RCS file: /cvsroot/rdesktop/rdesktop/xclip.c,v
retrieving revision 1.23
retrieving revision 1.24
diff -C2 -d -r1.23 -r1.24
*** xclip.c	14 Nov 2005 14:46:16 -0000	1.23
--- xclip.c	2 Jan 2006 15:55:59 -0000	1.24
***************
*** 33,37 ****
  */
  
! #define NUM_TARGETS 6
  
  extern Display *g_display;
--- 33,53 ----
  */
  
! #ifdef HAVE_ICONV
! #ifdef HAVE_LANGINFO_H
! #ifdef HAVE_ICONV_H
! #include <langinfo.h>
! #include <iconv.h>
! #define USE_UNICODE_CLIPBOARD
! #endif
! #endif
! #endif
! 
! #ifdef USE_UNICODE_CLIPBOARD
! #define RDP_CF_TEXT CF_UNICODETEXT
! #else
! #define RDP_CF_TEXT CF_TEXT
! #endif
! 
! #define MAX_TARGETS 7
  
  extern Display *g_display;
***************
*** 64,67 ****
--- 80,84 ----
       requests of the _RDESKTOP_CLIPBOARD_FORMATS target. */
  static Atom rdesktop_clipboard_formats_atom;
+ static Atom format_string_atom, format_utf8_string_atom, format_unicode_atom;
  /* Atom of the INCR clipboard type (see ICCCM on "INCR Properties") */
  static Atom incr_atom;
***************
*** 71,76 ****
     the context to proceed. */
  static XSelectionRequestEvent selection_request;
  /* Array of offered clipboard targets that will be sent to fellow X clients upon a TARGETS request. */
! static Atom targets[NUM_TARGETS];
  /* Denotes that this client currently holds the PRIMARY selection. */
  static int have_primary = 0;
--- 88,102 ----
     the context to proceed. */
  static XSelectionRequestEvent selection_request;
+ /* Denotes we have a pending selection request. */
+ static Bool has_selection_request;
+ /* Stores the clipboard format (CF_TEXT, CF_UNICODETEXT etc.) requested in the last
+    CLIPDR_DATA_REQUEST (= the RDP server requesting clipboard data from us).
+    When we receive this data from whatever X client offering it, this variable gives us
+    the context to proceed.
+  */
+ static int rdp_clipboard_request_format;
  /* Array of offered clipboard targets that will be sent to fellow X clients upon a TARGETS request. */
! static Atom targets[MAX_TARGETS];
! static int num_targets;
  /* Denotes that this client currently holds the PRIMARY selection. */
  static int have_primary = 0;
***************
*** 81,84 ****
--- 107,112 ----
  /* Denotes that an INCR ("chunked") transfer is in progress. */
  static int g_waiting_for_INCR = 0;
+ /* Denotes the target format of the ongoing INCR ("chunked") transfer. */
+ static Atom g_incr_target = 0;
  /* Buffers an INCR transfer. */
  static uint8 *g_clip_buffer = 0;
***************
*** 104,108 ****
  }
  
! /* Translate LF to CR-LF. To do this, we must allocate more memory.  
     The length is updated. */
  static uint8 *
--- 132,172 ----
  }
  
! #ifdef USE_UNICODE_CLIPBOARD
! /* Translate LF to CR-LF. To do this, we must allocate more memory.
!    The returned string is null-terminated, as required by CF_UNICODETEXT.
!    The size is updated. */
! static uint8 *
! utf16_lf2crlf(uint8 * data, uint32 * size)
! {
! 	uint8 *result;
! 	uint16 *inptr, *outptr;
! 
! 	/* Worst case: Every char is LF */
! 	result = xmalloc((*size * 2) + 2);
! 	if (result == NULL)
! 		return NULL;
! 
! 	inptr = (uint16*)data;
! 	outptr = (uint16*)result;
! 
! 	/* Check for a reversed BOM */
! 	Bool swap_endianess = (*inptr == 0xfffe);
! 
! 	while ((uint8*)inptr < data + *size)
! 	{
! 		uint16 uvalue = *inptr;
! 		if (swap_endianess)
! 			uvalue = ((uvalue << 8) & 0xff00) + (uvalue >> 8);
! 		if (uvalue == 0x0a)
! 			*outptr++ = swap_endianess ? 0x0d00 : 0x0d;
! 		*outptr++ = *inptr++;
! 	}
! 	*outptr++ = 0; /* null termination */
! 	*size = (uint8*)outptr - result;
! 
! 	return result;
! }
! #else
! /* Translate LF to CR-LF. To do this, we must allocate more memory.
     The length is updated. */
  static uint8 *
***************
*** 130,134 ****
  	return result;
  }
! 
  
  static void
--- 194,198 ----
  	return result;
  }
! #endif
  
  static void
***************
*** 152,157 ****
  }
  
  /* This function is called for SelectionNotify events.
!    The SelectionNotify message is sent from the clipboard owner to the requestor
     after his request was satisfied.
     If this function is called, we're the requestor side. */
--- 216,376 ----
  }
  
+ /* Replies a clipboard requestor, telling that we're unable to satisfy his request for whatever reason.
+    This has the benefit of finalizing the clipboard negotiation and thus not leaving our requestor
+    lingering (and, potentially, stuck). */
+ static void
+ xclip_refuse_selection(XSelectionRequestEvent * req)
+ {
+ 	XEvent xev;
+ 
+ 	xev.xselection.type = SelectionNotify;
+ 	xev.xselection.serial = 0;
+ 	xev.xselection.send_event = True;
+ 	xev.xselection.requestor = req->requestor;
+ 	xev.xselection.selection = req->selection;
+ 	xev.xselection.target = req->target;
+ 	xev.xselection.property = None;
+ 	xev.xselection.time = req->time;
+ 	XSendEvent(g_display, req->requestor, False, NoEventMask, &xev);
+ }
+ 
+ /* Wrapper for cliprdr_send_data which also cleans the request state. */
+ static void
+ helper_cliprdr_send_response(uint8 * data, uint32 length)
+ {
+ 	if (rdp_clipboard_request_format != 0)
+ 	{
+ 		cliprdr_send_data(data, length);
+ 		rdp_clipboard_request_format = 0;
+ 		if (!rdesktop_is_selection_owner)
+ 			cliprdr_send_simple_native_format_announce(RDP_CF_TEXT);
+ 	}
+ }
+ 
+ /* Last resort, when we have to provide clipboard data but for whatever
+    reason couldn't get any.
+  */
+ static void
+ helper_cliprdr_send_empty_response()
+ {
+ 	helper_cliprdr_send_response(NULL, 0);
+ }
+ 
+ /* Replies with clipboard data to RDP, converting it from the target format
+    to the expected RDP format as necessary. Returns true if data was sent.
+  */
+ static Bool
+ xclip_send_data_with_convert(uint8* source, size_t source_size, Atom target)
+ {
+ 	#ifdef USE_UNICODE_CLIPBOARD
+ 	if (target == format_string_atom ||
+ 	    target == format_unicode_atom ||
+ 	    target == format_utf8_string_atom)
+ 	{
+ 		if (rdp_clipboard_request_format != RDP_CF_TEXT)
+ 			return False;
+ 
+ 		/* Make an attempt to convert any string we send to Unicode.
+ 		   We don't know what the RDP server's ANSI Codepage is, or how to convert
+ 		   to it, so using CF_TEXT is not safe (and is unnecessary, since all
+ 		   WinNT versions are Unicode-minded).
+ 		 */
+ 		size_t unicode_buffer_size;
+ 		char* unicode_buffer;
+ 		iconv_t cd;
+ 
+ 		if (target == format_string_atom)
+ 		{
+ 			char* locale_charset = nl_langinfo(CODESET);
+ 			cd = iconv_open(WINDOWS_CODEPAGE, locale_charset);
+ 			if (cd == (iconv_t)-1)
+ 			{
+ 				DEBUG_CLIPBOARD(("Locale charset %s not found in iconv. Unable to convert clipboard text.\n", locale_charset));
+ 				return False;
+ 			}
+ 			unicode_buffer_size = source_size * 4;
+ 		}
+ 		else if (target == format_unicode_atom)
+ 		{
+ 			cd = iconv_open(WINDOWS_CODEPAGE, "UCS-2");
+ 			if (cd == (iconv_t)-1)
+ 			{
+ 				return False;
+ 			}
+ 			unicode_buffer_size = source_size;
+ 		}
+ 		else if (target == format_utf8_string_atom)
+ 		{
+ 			cd = iconv_open(WINDOWS_CODEPAGE, "UTF-8");
+ 			if (cd == (iconv_t)-1)
+ 			{
+ 				return False;
+ 			}
+ 			/* UTF-8 is guaranteed to be less or equally compact
+ 			   as UTF-16 for all Unicode chars >=2 bytes.
+ 			 */
+ 			unicode_buffer_size = source_size * 2;
+ 		}
+ 		else
+ 		{
+ 			return False;
+ 		}
+ 
+ 		unicode_buffer = xmalloc(unicode_buffer_size);
+ 		size_t unicode_buffer_size_remaining = unicode_buffer_size;
+ 		char* unicode_buffer_remaining = unicode_buffer;
+ 		char* data_remaining = (char*)source;
+ 		size_t data_size_remaining = source_size;
+ 		iconv(cd, &data_remaining, &data_size_remaining, &unicode_buffer_remaining, &unicode_buffer_size_remaining);
+ 		iconv_close(cd);
+ 
+ 		/* translate linebreaks */
+ 		uint32 translated_data_size = unicode_buffer_size - unicode_buffer_size_remaining;
+ 		uint8* translated_data = utf16_lf2crlf((uint8*)unicode_buffer, &translated_data_size);
+ 		if (translated_data != NULL)
+ 		{
+ 			DEBUG_CLIPBOARD(("Sending Unicode string of %d bytes\n", translated_data_size));
+ 			cliprdr_send_data(translated_data, translated_data_size);
+ 			xfree(translated_data);	/* Not the same thing as XFree! */
+ 		}
+ 
+ 		xfree(unicode_buffer);
+ 
+ 		return True;
+ 	}
+ 	#else
+ 	if (target == format_string_atom)
+ 	{
+ 		uint8 *translated_data;
+ 		uint32 length = source_size;
+ 
+ 		if (rdp_clipboard_request_format != RDP_CF_TEXT)
+ 			return False;
+ 
+ 		DEBUG_CLIPBOARD(("Translating linebreaks before sending data\n"));
+ 		translated_data = lf2crlf(source, &length);
+ 		if (translated_data != NULL)
+ 		{
+ 			cliprdr_send_data(translated_data, length);
+ 			xfree(translated_data);	/* Not the same thing as XFree! */
+ 		}
+ 
+ 		return True;
+ 	}
+ 	#endif
+ 	else if (target == rdesktop_clipboard_formats_atom)
+ 	{
+ 		helper_cliprdr_send_response(source, source_size + 1);
+ 
+ 		return True;
+ 	}
+ 	else
+ 	{
+ 		return False;
+ 	}
+ }
+ 
  /* This function is called for SelectionNotify events.
!    The SelectionNotify event is sent from the clipboard owner to the requestor
     after his request was satisfied.
     If this function is called, we're the requestor side. */
***************
*** 162,166 ****
  	unsigned long nitems, bytes_left;
  	XWindowAttributes wa;
! 	Atom type, best_target, text_target;
  	Atom *supported_targets;
  	int res, i, format;
--- 381,385 ----
  	unsigned long nitems, bytes_left;
  	XWindowAttributes wa;
! 	Atom type;
  	Atom *supported_targets;
  	int res, i, format;
***************
*** 188,192 ****
  	}
  
- 
  	if (type == incr_atom)
  	{
--- 407,410 ----
***************
*** 200,203 ****
--- 418,422 ----
  		XDeleteProperty(g_display, g_wnd, rdesktop_clipboard_target_atom);
  		XFree(data);
+ 		g_incr_target = event->target;
  		g_waiting_for_INCR = 1;
  		return;
***************
*** 209,257 ****
  	if (event->target == targets_atom)
  	{
! 		/* FIXME: We should choose format here based on what the server wanted */
! 		best_target = XA_STRING;
  		if (type != None)
  		{
  			supported_targets = (Atom *) data;
- 			text_target = XInternAtom(g_display, "TEXT", False);
  			for (i = 0; i < nitems; i++)
  			{
  				DEBUG_CLIPBOARD(("Target %d: %s\n", i,
  						 XGetAtomName(g_display, supported_targets[i])));
! 				if (supported_targets[i] == text_target)
  				{
! 					DEBUG_CLIPBOARD(("Other party supports TEXT, choosing that as best_target\n"));
! 					best_target = text_target;
! 					break;
  				}
  			}
- 			XFree(data);
  		}
  
! 		XConvertSelection(g_display, primary_atom, best_target,
! 				  rdesktop_clipboard_target_atom, g_wnd, event->time);
! 		return;
! 	}
! 
! 	/* Translate linebreaks, but only if not getting data from
! 	   other rdesktop instance */
! 	if (event->target != rdesktop_clipboard_formats_atom)
! 	{
! 		uint8 *translated_data;
! 		uint32 length = nitems;
! 
! 		DEBUG_CLIPBOARD(("Translating linebreaks before sending data\n"));
! 		translated_data = lf2crlf(data, &length);
! 		cliprdr_send_data(translated_data, length + 1);
! 		xfree(translated_data);	/* Not the same thing as XFree! */
  	}
  	else
  	{
! 		cliprdr_send_data(data, nitems + 1);
  	}
  	XFree(data);
  
- 	if (!rdesktop_is_selection_owner)
- 		cliprdr_send_simple_native_format_announce(CF_TEXT);
  	return;
  
--- 428,500 ----
  	if (event->target == targets_atom)
  	{
! 		/* Determine the best of text targets that we have available:
! 		   Prefer UTF8_STRING > text/unicode (unspecified encoding) > STRING
! 		   (ignore TEXT and COMPOUND_TEXT because we don't have code to handle them)
! 		 */
! 		int text_target_satisfaction = 0;
! 		Atom best_text_target = 0; /* measures how much we're satisfied with what we found */
  		if (type != None)
  		{
  			supported_targets = (Atom *) data;
  			for (i = 0; i < nitems; i++)
  			{
  				DEBUG_CLIPBOARD(("Target %d: %s\n", i,
  						 XGetAtomName(g_display, supported_targets[i])));
! 				if (supported_targets[i] == format_string_atom)
  				{
! 					if (text_target_satisfaction < 1)
! 					{
! 						DEBUG_CLIPBOARD(("Other party supports STRING, choosing that as best_target\n"));
! 						best_text_target = supported_targets[i];
! 						text_target_satisfaction = 1;
! 					}
! 				}
! 				#ifdef USE_UNICODE_CLIPBOARD
! 				else if (supported_targets[i] == format_unicode_atom)
! 				{
! 					if (text_target_satisfaction < 2)
! 					{
! 						DEBUG_CLIPBOARD(("Other party supports text/unicode, choosing that as best_target\n"));
! 						best_text_target = supported_targets[i];
! 						text_target_satisfaction = 2;
! 					}
  				}
+ 				else if (supported_targets[i] == format_utf8_string_atom)
+ 				{
+ 					if (text_target_satisfaction < 3)
+ 					{
+ 						DEBUG_CLIPBOARD(("Other party supports UTF8_STRING, choosing that as best_target\n"));
+ 						best_text_target = supported_targets[i];
+ 						text_target_satisfaction = 3;
+ 					}
+ 				}
+ 				#endif
  			}
  		}
  
! 		/* Kickstarting the next step in the process of satisfying RDP's
! 		   clipboard request -- specifically, requesting the actual clipboard data.
! 		 */
! 		if (best_text_target != 0)
! 		{
! 			XConvertSelection(g_display, clipboard_atom, best_text_target, rdesktop_clipboard_target_atom, g_wnd, event->time);
! 			return;
! 		}
! 		else
! 		{
! 			DEBUG_CLIPBOARD(("Unable to find a textual target to satisfy RDP clipboard text request\n"));
! 			goto fail;
! 		}
  	}
  	else
  	{
! 		if (!xclip_send_data_with_convert(data, nitems, event->target))
! 		{
! 			goto fail;
! 		}
  	}
+ 
  	XFree(data);
  
  	return;
  
***************
*** 259,267 ****
  	XDeleteProperty(g_display, g_wnd, rdesktop_clipboard_target_atom);
  	XFree(data);
! 	cliprdr_send_data(NULL, 0);
  }
  
  /* This function is called for SelectionRequest events.
!    The SelectionRequest message is sent from the requestor to the clipboard owner
     to request clipboard data.
   */
--- 502,510 ----
  	XDeleteProperty(g_display, g_wnd, rdesktop_clipboard_target_atom);
  	XFree(data);
! 	helper_cliprdr_send_empty_response();
  }
  
  /* This function is called for SelectionRequest events.
!    The SelectionRequest event is sent from the requestor to the clipboard owner
     to request clipboard data.
   */
***************
*** 282,286 ****
  	if (event->target == targets_atom)
  	{
! 		xclip_provide_selection(event, XA_ATOM, 32, (uint8 *) & targets, NUM_TARGETS);
  		return;
  	}
--- 525,529 ----
  	if (event->target == targets_atom)
  	{
! 		xclip_provide_selection(event, XA_ATOM, 32, (uint8 *) & targets, num_targets);
  		return;
  	}
***************
*** 290,315 ****
  		return;
  	}
- 	else if (event->target == rdesktop_clipboard_formats_atom)
- 	{
- 		res = XGetWindowProperty(g_display, event->requestor,
- 					 rdesktop_clipboard_target_atom, 0, 1, True, XA_INTEGER,
- 					 &type, &format, &nitems, &bytes_left, &prop_return);
- 		wanted_format = (uint32 *) prop_return;
- 		format = (res == Success) ? *wanted_format : CF_TEXT;
- 		/* FIXME: Need to free returned data? */
- 	}
  	else
  	{
! 		format = CF_TEXT;
! 	}
  
! 	cliprdr_send_data_request(format);
! 	selection_request = *event;
! 	/* wait for data */
  }
  
! /* When this rdesktop holds ownership over the clipboard, it means the clipboard data
!    is offered by the RDP server (and when its pasted inside RDP, there's no network
!    roundtrip). This event symbolizes this rdesktop lost onwership of the clipboard
     to some other X client. We should find out what clipboard formats this other
     client offers and announce that to RDP. */
--- 533,600 ----
  		return;
  	}
  	else
  	{
! 		/* All the following targets require an async operation with the RDP server
! 		   and currently we don't do X clipboard request queueing so we can only
! 		   handle one such request at a time. */
! 		if (has_selection_request)
! 		{
! 			DEBUG_CLIPBOARD(("Error: Another clipboard request was already sent to the RDP server and not yet responded. Refusing this request.\n"));
! 			xclip_refuse_selection(event);
! 			return;
! 		}
! 		if (event->target == rdesktop_clipboard_formats_atom)
! 		{
! 			/* Before the requestor makes a request for the _RDESKTOP_CLIPBOARD_FORMATS target,
! 			   he should declare requestor[_RDESKTOP_CLIPBOARD_TARGET] = CF_SOMETHING.
! 			   Otherwise, we default to RDP_CF_TEXT.
! 			 */
! 			res = XGetWindowProperty(g_display, event->requestor,
! 						 rdesktop_clipboard_target_atom, 0, 1, True, XA_INTEGER,
! 						 &type, &format, &nitems, &bytes_left, &prop_return);
! 			wanted_format = (uint32 *) prop_return;
! 			format = (res == Success) ? *wanted_format : RDP_CF_TEXT;
! 			XFree(prop_return);
! 		}
! 		else if (event->target == format_string_atom ||
! 			 event->target == XA_STRING)
! 		{
! 			/* STRING and XA_STRING are defined to be ISO8859-1 */
! 			format = CF_TEXT;
! 		}
! 		else if (event->target == format_utf8_string_atom)
! 		{
! 			#ifdef USE_UNICODE_CLIPBOARD
! 			format = CF_UNICODETEXT;
! 			#else
! 			DEBUG_CLIPBOARD(("Requested target unavailable due to lack of Unicode support. (It was not in TARGETS, so why did you ask for it?!)\n"));
! 			xclip_refuse_selection(event);
! 			return;
! 			#endif
! 		}
! 		else if (event->target == format_unicode_atom)
! 		{
! 			/* Assuming text/unicode to be UTF-16 */
! 			format = CF_UNICODETEXT;
! 		}
! 		else
! 		{
! 			DEBUG_CLIPBOARD(("Requested target unavailable. (It was not in TARGETS, so why did you ask for it?!)\n"));
! 			xclip_refuse_selection(event);
! 			return;
! 		}
  
! 		cliprdr_send_data_request(format);
! 		selection_request = *event;
! 		has_selection_request = True;
! 		return;	/* wait for data */
! 	}
  }
  
! /* While this rdesktop holds ownership over the clipboard, it means the clipboard data
!    is offered by the RDP server (and when it is pasted inside RDP, there's no network
!    roundtrip).
! 
!    This event (SelectionClear) symbolizes this rdesktop lost onwership of the clipboard
     to some other X client. We should find out what clipboard formats this other
     client offers and announce that to RDP. */
***************
*** 320,324 ****
  	have_primary = 0;
  	XDeleteProperty(g_display, DefaultRootWindow(g_display), rdesktop_clipboard_formats_atom);
! 	cliprdr_send_simple_native_format_announce(CF_TEXT);
  }
  
--- 605,613 ----
  	have_primary = 0;
  	XDeleteProperty(g_display, DefaultRootWindow(g_display), rdesktop_clipboard_formats_atom);
! 	/* FIXME:
! 	   Without XFIXES, we cannot reliably know the formats offered by the
! 	   new owner of the X11 clipboard, so we just lie about him
! 	   offering RDP_CF_TEXT. */
! 	cliprdr_send_simple_native_format_announce(RDP_CF_TEXT);
  }
  
***************
*** 341,344 ****
--- 630,635 ----
  		while (bytes_left > 0)
  		{
+ 			/* Unlike the specification, we don't set the 'delete' arugment to True
+ 			   since we slurp the INCR's chunks in even-smaller chunks of 4096 bytes. */
  			if ((XGetWindowProperty
  			     (g_display, g_wnd, rdesktop_clipboard_target_atom, offset, 4096L,
***************
*** 352,355 ****
--- 643,647 ----
  			if (nitems == 0)
  			{
+ 				/* INCR transfer finished */
  				XGetWindowAttributes(g_display, g_wnd, &wa);
  				XSelectInput(g_display, g_wnd,
***************
*** 360,370 ****
  				if (g_clip_buflen > 0)
  				{
! 					cliprdr_send_data(g_clip_buffer, g_clip_buflen + 1);
! 
! 					if (!rdesktop_is_selection_owner)
! 						cliprdr_send_simple_native_format_announce(CF_TEXT);
! 
  					xfree(g_clip_buffer);
! 					g_clip_buffer = 0;
  					g_clip_buflen = 0;
  				}
--- 652,661 ----
  				if (g_clip_buflen > 0)
  				{
! 					if (!xclip_send_data_with_convert(g_clip_buffer, g_clip_buflen, g_incr_target))
! 					{
! 						helper_cliprdr_send_empty_response();
! 					}
  					xfree(g_clip_buffer);
! 					g_clip_buffer = NULL;
  					g_clip_buflen = 0;
  				}
***************
*** 372,393 ****
  			else
  			{
! 				uint8 *translated_data;
! 				uint32 length = nitems;
! 				uint8 *tmp;
! 
! 				offset += (length / 4);
! 				DEBUG_CLIPBOARD(("Translating linebreaks before sending data\n"));
! 				translated_data = lf2crlf(data, &length);
! 
! 				tmp = xmalloc(length + g_clip_buflen);
! 				strncpy((char *) tmp, (char *) g_clip_buffer, g_clip_buflen);
! 				xfree(g_clip_buffer);
! 
! 				strncpy((char *) (tmp + g_clip_buflen), (char *) translated_data,
! 					length);
! 				xfree(translated_data);
! 
! 				g_clip_buffer = tmp;
! 				g_clip_buflen += length;
  
  				XFree(data);
--- 663,671 ----
  			else
  			{
! 				/* Another chunk in the INCR transfer */
! 				offset += (nitems / 4); /* offset at which to begin the next slurp */
! 				g_clip_buffer = xrealloc(g_clip_buffer, g_clip_buflen + nitems);
! 				memcpy(g_clip_buffer + g_clip_buflen, data, nitems);
! 				g_clip_buflen += nitems;
  
  				XFree(data);
***************
*** 395,424 ****
  		}
  		XDeleteProperty(g_display, g_wnd, rdesktop_clipboard_target_atom);
- 	}
- 
- 	if (event->atom != rdesktop_clipboard_formats_atom)
- 		return;
- 
- 	if (have_primary)	/* from us */
  		return;
  
! 	if (event->state == PropertyNewValue)
  	{
! 		res = XGetWindowProperty(g_display, DefaultRootWindow(g_display),
! 					 rdesktop_clipboard_formats_atom, 0,
! 					 XMaxRequestSize(g_display), False, XA_STRING, &type,
! 					 &format, &nitems, &bytes_left, &data);
! 
! 		if ((res == Success) && (nitems > 0))
  		{
! 			cliprdr_send_native_format_announce(data, nitems);
! 			rdesktop_is_selection_owner = 1;
! 			return;
  		}
- 	}
  
! 	/* PropertyDelete, or XGetWindowProperty failed */
! 	cliprdr_send_simple_native_format_announce(CF_TEXT);
! 	rdesktop_is_selection_owner = 0;
  }
  #endif
--- 673,704 ----
  		}
  		XDeleteProperty(g_display, g_wnd, rdesktop_clipboard_target_atom);
  		return;
+ 	}
  
! 	if ((event->atom == rdesktop_clipboard_formats_atom) &&
! 	    (event->window == DefaultRootWindow(g_display)) &&
! 	    !have_primary /* not interested in our own events */)
  	{
! 		if (event->state == PropertyNewValue)
  		{
! 			DEBUG_CLIPBOARD(("xclip_handle_PropertyNotify: getting fellow rdesktop formats\n"));
! 
! 			res = XGetWindowProperty(g_display, DefaultRootWindow(g_display),
! 						 rdesktop_clipboard_formats_atom, 0,
! 						 XMaxRequestSize(g_display), False, XA_STRING, &type,
! 						 &format, &nitems, &bytes_left, &data);
! 
! 			if ((res == Success) && (nitems > 0))
! 			{
! 				cliprdr_send_native_format_announce(data, nitems);
! 				rdesktop_is_selection_owner = 1;
! 				return;
! 			}
  		}
  
! 		/* For some reason, we couldn't announce the native formats */
! 		cliprdr_send_simple_native_format_announce(RDP_CF_TEXT);
! 		rdesktop_is_selection_owner = 0;
! 	}
  }
  #endif
***************
*** 450,473 ****
  }
  
! 
  void
  ui_clip_handle_data(uint8 * data, uint32 length)
  {
! 	if (selection_request.target != rdesktop_clipboard_formats_atom)
! 	{
! 		uint8 *firstnull;
  
! 		/* translate linebreaks */
! 		crlf2lf(data, &length);
  
! 		/* Only send data up to null byte, if any */
! 		firstnull = (uint8 *) strchr((char *) data, '\0');
! 		if (firstnull)
  		{
! 			length = firstnull - data + 1;
  		}
  	}
  
! 	xclip_provide_selection(&selection_request, XA_STRING, 8, data, length - 1);
  }
  
--- 730,803 ----
  }
  
! /* Called when the RDP server responds with clipboard data (after we've requested it). */
  void
  ui_clip_handle_data(uint8 * data, uint32 length)
  {
! 	BOOL free_data = False;
  
! 	if (selection_request.target == format_string_atom ||
! 	    selection_request.target == XA_STRING)
!  	{
! 		/* We're expecting a CF_TEXT response */
!  		uint8 *firstnull;
  
!  		/* translate linebreaks */
!  		crlf2lf(data, &length);
! 
!  		/* Only send data up to null byte, if any */
!  		firstnull = (uint8 *) strchr((char *) data, '\0');
!  		if (firstnull)
!  		{
!  			length = firstnull - data + 1;
!  		}
!  	}
! #ifdef USE_UNICODE_CLIPBOARD
! 	else if (selection_request.target == format_utf8_string_atom)
! 	{
! 		/* We're expecting a CF_UNICODETEXT response */
! 		iconv_t cd = iconv_open("UTF-8", WINDOWS_CODEPAGE);
! 		if (cd != (iconv_t)-1)
  		{
! 			size_t utf8_length = length * 2;
! 			char* utf8_data = malloc(utf8_length);
! 			size_t utf8_length_remaining = utf8_length;
! 			char* utf8_data_remaining = utf8_data;
! 			char* data_remaining = (char*)data;
! 			size_t length_remaining = (size_t)length;
! 			if (utf8_data == NULL)
! 			{
! 				iconv_close(cd);
! 				return;
! 			}
! 			iconv(cd, &data_remaining, &length_remaining, &utf8_data_remaining, &utf8_length_remaining);
! 			iconv_close(cd);
! 			free_data = True;
! 			data = (uint8*)utf8_data;
! 			length = utf8_length - utf8_length_remaining;
  		}
  	}
+ 	else if (selection_request.target == format_unicode_atom)
+ 	{
+ 		/* We're expecting a CF_UNICODETEXT response, so what we're
+ 		   receiving matches our requirements and there's no need
+ 		   for further conversions. */
+ 	}
+ #endif
+ 	else if (selection_request.target == rdesktop_clipboard_formats_atom)
+ 	{
+ 		/* Pass as-is */
+ 	}
+ 	else
+ 	{
+ 		xclip_refuse_selection(&selection_request);
+ 		has_selection_request = False;
+ 		return;
+ 	}
  
! 	xclip_provide_selection(&selection_request, selection_request.target, 8, data, length - 1);
! 	has_selection_request = False;
! 
! 	if (free_data)
! 		free(data);
  }
  
***************
*** 478,481 ****
--- 808,812 ----
  
  	DEBUG_CLIPBOARD(("Request from server for format %d\n", format));
+ 	rdp_clipboard_request_format = format;
  
  	if (rdesktop_is_selection_owner)
***************
*** 513,517 ****
  ui_clip_sync(void)
  {
! 	cliprdr_send_simple_native_format_announce(CF_TEXT);
  }
  
--- 844,848 ----
  ui_clip_sync(void)
  {
! 	cliprdr_send_simple_native_format_announce(RDP_CF_TEXT);
  }
  
***************
*** 530,539 ****
  		XInternAtom(g_display, "_RDESKTOP_CLIPBOARD_TARGET", False);
  	incr_atom = XInternAtom(g_display, "INCR", False);
! 	targets[0] = targets_atom;
! 	targets[1] = XInternAtom(g_display, "TEXT", False);
! 	targets[2] = XInternAtom(g_display, "STRING", False);
! 	targets[3] = XInternAtom(g_display, "text/unicode", False);
! 	targets[4] = XInternAtom(g_display, "TIMESTAMP", False);
! 	targets[5] = XA_STRING;
  
  	/* rdesktop sets _RDESKTOP_CLIPBOARD_FORMATS on the root window when acquiring the clipboard.
--- 861,877 ----
  		XInternAtom(g_display, "_RDESKTOP_CLIPBOARD_TARGET", False);
  	incr_atom = XInternAtom(g_display, "INCR", False);
! 	format_string_atom = XInternAtom(g_display, "STRING", False);
! 	format_utf8_string_atom = XInternAtom(g_display, "UTF8_STRING", False);
! 	format_unicode_atom = XInternAtom(g_display, "text/unicode", False);
! 	num_targets = 0;
! 	targets[num_targets++] = targets_atom;
! 	targets[num_targets++] = timestamp_atom;
! 	targets[num_targets++] = rdesktop_clipboard_formats_atom;
! 	targets[num_targets++] = format_string_atom;
! 	#ifdef USE_UNICODE_CLIPBOARD
! 	targets[num_targets++] = format_utf8_string_atom;
! 	#endif
! 	targets[num_targets++] = format_unicode_atom;
! 	targets[num_targets++] = XA_STRING;
  
  	/* rdesktop sets _RDESKTOP_CLIPBOARD_FORMATS on the root window when acquiring the clipboard.



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.