/[rdesktop]/sourceforge.net/trunk/rdesktop/xclip.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /sourceforge.net/trunk/rdesktop/xclip.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1037 by forsberg, Mon Jan 2 15:55:59 2006 UTC revision 1203 by ossman_, Mon Mar 27 08:39:20 2006 UTC
# Line 146  utf16_lf2crlf(uint8 * data, uint32 * siz Line 146  utf16_lf2crlf(uint8 * data, uint32 * siz
146          if (result == NULL)          if (result == NULL)
147                  return NULL;                  return NULL;
148    
149          inptr = (uint16*)data;          inptr = (uint16 *) data;
150          outptr = (uint16*)result;          outptr = (uint16 *) result;
151    
152          /* Check for a reversed BOM */          /* Check for a reversed BOM */
153          Bool swap_endianess = (*inptr == 0xfffe);          Bool swap_endianess = (*inptr == 0xfffe);
154    
155          while ((uint8*)inptr < data + *size)          while ((uint8 *) inptr < data + *size)
156          {          {
157                  uint16 uvalue = *inptr;                  uint16 uvalue = *inptr;
158                  if (swap_endianess)                  if (swap_endianess)
# Line 161  utf16_lf2crlf(uint8 * data, uint32 * siz Line 161  utf16_lf2crlf(uint8 * data, uint32 * siz
161                          *outptr++ = swap_endianess ? 0x0d00 : 0x0d;                          *outptr++ = swap_endianess ? 0x0d00 : 0x0d;
162                  *outptr++ = *inptr++;                  *outptr++ = *inptr++;
163          }          }
164          *outptr++ = 0; /* null termination */          *outptr++ = 0;          /* null termination */
165          *size = (uint8*)outptr - result;          *size = (uint8 *) outptr - result;
166    
167          return result;          return result;
168  }  }
# Line 260  helper_cliprdr_send_empty_response() Line 260  helper_cliprdr_send_empty_response()
260     to the expected RDP format as necessary. Returns true if data was sent.     to the expected RDP format as necessary. Returns true if data was sent.
261   */   */
262  static Bool  static Bool
263  xclip_send_data_with_convert(uint8* source, size_t source_size, Atom target)  xclip_send_data_with_convert(uint8 * source, size_t source_size, Atom target)
264  {  {
265          #ifdef USE_UNICODE_CLIPBOARD  #ifdef USE_UNICODE_CLIPBOARD
266          if (target == format_string_atom ||          if (target == format_string_atom ||
267              target == format_unicode_atom ||              target == format_unicode_atom || target == format_utf8_string_atom)
             target == format_utf8_string_atom)  
268          {          {
269                  if (rdp_clipboard_request_format != RDP_CF_TEXT)                  if (rdp_clipboard_request_format != RDP_CF_TEXT)
270                          return False;                          return False;
# Line 276  xclip_send_data_with_convert(uint8* sour Line 275  xclip_send_data_with_convert(uint8* sour
275                     WinNT versions are Unicode-minded).                     WinNT versions are Unicode-minded).
276                   */                   */
277                  size_t unicode_buffer_size;                  size_t unicode_buffer_size;
278                  char* unicode_buffer;                  char *unicode_buffer;
279                  iconv_t cd;                  iconv_t cd;
280    
281                  if (target == format_string_atom)                  if (target == format_string_atom)
282                  {                  {
283                          char* locale_charset = nl_langinfo(CODESET);                          char *locale_charset = nl_langinfo(CODESET);
284                          cd = iconv_open(WINDOWS_CODEPAGE, locale_charset);                          cd = iconv_open(WINDOWS_CODEPAGE, locale_charset);
285                          if (cd == (iconv_t)-1)                          if (cd == (iconv_t) - 1)
286                          {                          {
287                                  DEBUG_CLIPBOARD(("Locale charset %s not found in iconv. Unable to convert clipboard text.\n", locale_charset));                                  DEBUG_CLIPBOARD(("Locale charset %s not found in iconv. Unable to convert clipboard text.\n", locale_charset));
288                                  return False;                                  return False;
# Line 293  xclip_send_data_with_convert(uint8* sour Line 292  xclip_send_data_with_convert(uint8* sour
292                  else if (target == format_unicode_atom)                  else if (target == format_unicode_atom)
293                  {                  {
294                          cd = iconv_open(WINDOWS_CODEPAGE, "UCS-2");                          cd = iconv_open(WINDOWS_CODEPAGE, "UCS-2");
295                          if (cd == (iconv_t)-1)                          if (cd == (iconv_t) - 1)
296                          {                          {
297                                  return False;                                  return False;
298                          }                          }
# Line 302  xclip_send_data_with_convert(uint8* sour Line 301  xclip_send_data_with_convert(uint8* sour
301                  else if (target == format_utf8_string_atom)                  else if (target == format_utf8_string_atom)
302                  {                  {
303                          cd = iconv_open(WINDOWS_CODEPAGE, "UTF-8");                          cd = iconv_open(WINDOWS_CODEPAGE, "UTF-8");
304                          if (cd == (iconv_t)-1)                          if (cd == (iconv_t) - 1)
305                          {                          {
306                                  return False;                                  return False;
307                          }                          }
# Line 318  xclip_send_data_with_convert(uint8* sour Line 317  xclip_send_data_with_convert(uint8* sour
317    
318                  unicode_buffer = xmalloc(unicode_buffer_size);                  unicode_buffer = xmalloc(unicode_buffer_size);
319                  size_t unicode_buffer_size_remaining = unicode_buffer_size;                  size_t unicode_buffer_size_remaining = unicode_buffer_size;
320                  char* unicode_buffer_remaining = unicode_buffer;                  char *unicode_buffer_remaining = unicode_buffer;
321                  char* data_remaining = (char*)source;                  char *data_remaining = (char *) source;
322                  size_t data_size_remaining = source_size;                  size_t data_size_remaining = source_size;
323                  iconv(cd, &data_remaining, &data_size_remaining, &unicode_buffer_remaining, &unicode_buffer_size_remaining);                  iconv(cd, (ICONV_CONST char **) &data_remaining, &data_size_remaining,
324                          &unicode_buffer_remaining, &unicode_buffer_size_remaining);
325                  iconv_close(cd);                  iconv_close(cd);
326    
327                  /* translate linebreaks */                  /* translate linebreaks */
328                  uint32 translated_data_size = unicode_buffer_size - unicode_buffer_size_remaining;                  uint32 translated_data_size = unicode_buffer_size - unicode_buffer_size_remaining;
329                  uint8* translated_data = utf16_lf2crlf((uint8*)unicode_buffer, &translated_data_size);                  uint8 *translated_data =
330                            utf16_lf2crlf((uint8 *) unicode_buffer, &translated_data_size);
331                  if (translated_data != NULL)                  if (translated_data != NULL)
332                  {                  {
333                          DEBUG_CLIPBOARD(("Sending Unicode string of %d bytes\n", translated_data_size));                          DEBUG_CLIPBOARD(("Sending Unicode string of %d bytes\n",
334                          cliprdr_send_data(translated_data, translated_data_size);                                           translated_data_size));
335                            helper_cliprdr_send_response(translated_data, translated_data_size);
336                          xfree(translated_data); /* Not the same thing as XFree! */                          xfree(translated_data); /* Not the same thing as XFree! */
337                  }                  }
338    
# Line 338  xclip_send_data_with_convert(uint8* sour Line 340  xclip_send_data_with_convert(uint8* sour
340    
341                  return True;                  return True;
342          }          }
343          #else  #else
344          if (target == format_string_atom)          if (target == format_string_atom)
345          {          {
346                  uint8 *translated_data;                  uint8 *translated_data;
# Line 351  xclip_send_data_with_convert(uint8* sour Line 353  xclip_send_data_with_convert(uint8* sour
353                  translated_data = lf2crlf(source, &length);                  translated_data = lf2crlf(source, &length);
354                  if (translated_data != NULL)                  if (translated_data != NULL)
355                  {                  {
356                          cliprdr_send_data(translated_data, length);                          helper_cliprdr_send_response(translated_data, length);
357                          xfree(translated_data); /* Not the same thing as XFree! */                          xfree(translated_data); /* Not the same thing as XFree! */
358                  }                  }
359    
360                  return True;                  return True;
361          }          }
362          #endif  #endif
363          else if (target == rdesktop_clipboard_formats_atom)          else if (target == rdesktop_clipboard_formats_atom)
364          {          {
365                  helper_cliprdr_send_response(source, source_size + 1);                  helper_cliprdr_send_response(source, source_size + 1);
# Line 383  xclip_handle_SelectionNotify(XSelectionE Line 385  xclip_handle_SelectionNotify(XSelectionE
385          Atom type;          Atom type;
386          Atom *supported_targets;          Atom *supported_targets;
387          int res, i, format;          int res, i, format;
388          uint8 *data;          uint8 *data = NULL;
389    
390          if (event->property == None)          if (event->property == None)
391                  goto fail;                  goto fail;
# Line 432  xclip_handle_SelectionNotify(XSelectionE Line 434  xclip_handle_SelectionNotify(XSelectionE
434                     (ignore TEXT and COMPOUND_TEXT because we don't have code to handle them)                     (ignore TEXT and COMPOUND_TEXT because we don't have code to handle them)
435                   */                   */
436                  int text_target_satisfaction = 0;                  int text_target_satisfaction = 0;
437                  Atom best_text_target = 0; /* measures how much we're satisfied with what we found */                  Atom best_text_target = 0;      /* measures how much we're satisfied with what we found */
438                  if (type != None)                  if (type != None)
439                  {                  {
440                          supported_targets = (Atom *) data;                          supported_targets = (Atom *) data;
# Line 449  xclip_handle_SelectionNotify(XSelectionE Line 451  xclip_handle_SelectionNotify(XSelectionE
451                                                  text_target_satisfaction = 1;                                                  text_target_satisfaction = 1;
452                                          }                                          }
453                                  }                                  }
454                                  #ifdef USE_UNICODE_CLIPBOARD  #ifdef USE_UNICODE_CLIPBOARD
455                                  else if (supported_targets[i] == format_unicode_atom)                                  else if (supported_targets[i] == format_unicode_atom)
456                                  {                                  {
457                                          if (text_target_satisfaction < 2)                                          if (text_target_satisfaction < 2)
# Line 468  xclip_handle_SelectionNotify(XSelectionE Line 470  xclip_handle_SelectionNotify(XSelectionE
470                                                  text_target_satisfaction = 3;                                                  text_target_satisfaction = 3;
471                                          }                                          }
472                                  }                                  }
473                                  #endif  #endif
474                          }                          }
475                  }                  }
476    
# Line 477  xclip_handle_SelectionNotify(XSelectionE Line 479  xclip_handle_SelectionNotify(XSelectionE
479                   */                   */
480                  if (best_text_target != 0)                  if (best_text_target != 0)
481                  {                  {
482                          XConvertSelection(g_display, clipboard_atom, best_text_target, rdesktop_clipboard_target_atom, g_wnd, event->time);                          XConvertSelection(g_display, clipboard_atom, best_text_target,
483                                              rdesktop_clipboard_target_atom, g_wnd, event->time);
484                          return;                          return;
485                  }                  }
486                  else                  else
# Line 500  xclip_handle_SelectionNotify(XSelectionE Line 503  xclip_handle_SelectionNotify(XSelectionE
503    
504        fail:        fail:
505          XDeleteProperty(g_display, g_wnd, rdesktop_clipboard_target_atom);          XDeleteProperty(g_display, g_wnd, rdesktop_clipboard_target_atom);
506          XFree(data);          if (data)
507                    XFree(data);
508          helper_cliprdr_send_empty_response();          helper_cliprdr_send_empty_response();
509  }  }
510    
# Line 550  xclip_handle_SelectionRequest(XSelection Line 554  xclip_handle_SelectionRequest(XSelection
554                             Otherwise, we default to RDP_CF_TEXT.                             Otherwise, we default to RDP_CF_TEXT.
555                           */                           */
556                          res = XGetWindowProperty(g_display, event->requestor,                          res = XGetWindowProperty(g_display, event->requestor,
557                                                   rdesktop_clipboard_target_atom, 0, 1, True, XA_INTEGER,                                                   rdesktop_clipboard_target_atom, 0, 1, True,
558                                                   &type, &format, &nitems, &bytes_left, &prop_return);                                                   XA_INTEGER, &type, &format, &nitems, &bytes_left,
559                                                     &prop_return);
560                          wanted_format = (uint32 *) prop_return;                          wanted_format = (uint32 *) prop_return;
561                          format = (res == Success) ? *wanted_format : RDP_CF_TEXT;                          format = (res == Success) ? *wanted_format : RDP_CF_TEXT;
562                          XFree(prop_return);                          XFree(prop_return);
563                  }                  }
564                  else if (event->target == format_string_atom ||                  else if (event->target == format_string_atom || event->target == XA_STRING)
                          event->target == XA_STRING)  
565                  {                  {
566                          /* STRING and XA_STRING are defined to be ISO8859-1 */                          /* STRING and XA_STRING are defined to be ISO8859-1 */
567                          format = CF_TEXT;                          format = CF_TEXT;
568                  }                  }
569                  else if (event->target == format_utf8_string_atom)                  else if (event->target == format_utf8_string_atom)
570                  {                  {
571                          #ifdef USE_UNICODE_CLIPBOARD  #ifdef USE_UNICODE_CLIPBOARD
572                          format = CF_UNICODETEXT;                          format = CF_UNICODETEXT;
573                          #else  #else
574                          DEBUG_CLIPBOARD(("Requested target unavailable due to lack of Unicode support. (It was not in TARGETS, so why did you ask for it?!)\n"));                          DEBUG_CLIPBOARD(("Requested target unavailable due to lack of Unicode support. (It was not in TARGETS, so why did you ask for it?!)\n"));
575                          xclip_refuse_selection(event);                          xclip_refuse_selection(event);
576                          return;                          return;
577                          #endif  #endif
578                  }                  }
579                  else if (event->target == format_unicode_atom)                  else if (event->target == format_unicode_atom)
580                  {                  {
# Line 587  xclip_handle_SelectionRequest(XSelection Line 591  xclip_handle_SelectionRequest(XSelection
591                  cliprdr_send_data_request(format);                  cliprdr_send_data_request(format);
592                  selection_request = *event;                  selection_request = *event;
593                  has_selection_request = True;                  has_selection_request = True;
594                  return; /* wait for data */                  return;         /* wait for data */
595          }          }
596  }  }
597    
# Line 651  xclip_handle_PropertyNotify(XPropertyEve Line 655  xclip_handle_PropertyNotify(XPropertyEve
655    
656                                  if (g_clip_buflen > 0)                                  if (g_clip_buflen > 0)
657                                  {                                  {
658                                          if (!xclip_send_data_with_convert(g_clip_buffer, g_clip_buflen, g_incr_target))                                          if (!xclip_send_data_with_convert
659                                                (g_clip_buffer, g_clip_buflen, g_incr_target))
660                                          {                                          {
661                                                  helper_cliprdr_send_empty_response();                                                  helper_cliprdr_send_empty_response();
662                                          }                                          }
# Line 663  xclip_handle_PropertyNotify(XPropertyEve Line 668  xclip_handle_PropertyNotify(XPropertyEve
668                          else                          else
669                          {                          {
670                                  /* Another chunk in the INCR transfer */                                  /* Another chunk in the INCR transfer */
671                                  offset += (nitems / 4); /* offset at which to begin the next slurp */                                  offset += (nitems / 4); /* offset at which to begin the next slurp */
672                                  g_clip_buffer = xrealloc(g_clip_buffer, g_clip_buflen + nitems);                                  g_clip_buffer = xrealloc(g_clip_buffer, g_clip_buflen + nitems);
673                                  memcpy(g_clip_buffer + g_clip_buflen, data, nitems);                                  memcpy(g_clip_buffer + g_clip_buflen, data, nitems);
674                                  g_clip_buflen += nitems;                                  g_clip_buflen += nitems;
# Line 677  xclip_handle_PropertyNotify(XPropertyEve Line 682  xclip_handle_PropertyNotify(XPropertyEve
682    
683          if ((event->atom == rdesktop_clipboard_formats_atom) &&          if ((event->atom == rdesktop_clipboard_formats_atom) &&
684              (event->window == DefaultRootWindow(g_display)) &&              (event->window == DefaultRootWindow(g_display)) &&
685              !have_primary /* not interested in our own events */)              !have_primary /* not interested in our own events */ )
686          {          {
687                  if (event->state == PropertyNewValue)                  if (event->state == PropertyNewValue)
688                  {                  {
# Line 685  xclip_handle_PropertyNotify(XPropertyEve Line 690  xclip_handle_PropertyNotify(XPropertyEve
690    
691                          res = XGetWindowProperty(g_display, DefaultRootWindow(g_display),                          res = XGetWindowProperty(g_display, DefaultRootWindow(g_display),
692                                                   rdesktop_clipboard_formats_atom, 0,                                                   rdesktop_clipboard_formats_atom, 0,
693                                                   XMaxRequestSize(g_display), False, XA_STRING, &type,                                                   XMaxRequestSize(g_display), False, XA_STRING,
694                                                   &format, &nitems, &bytes_left, &data);                                                   &type, &format, &nitems, &bytes_left, &data);
695    
696                          if ((res == Success) && (nitems > 0))                          if ((res == Success) && (nitems > 0))
697                          {                          {
# Line 735  ui_clip_handle_data(uint8 * data, uint32 Line 740  ui_clip_handle_data(uint8 * data, uint32
740  {  {
741          BOOL free_data = False;          BOOL free_data = False;
742    
743          if (selection_request.target == format_string_atom ||          if (selection_request.target == format_string_atom || selection_request.target == XA_STRING)
744              selection_request.target == XA_STRING)          {
         {  
745                  /* We're expecting a CF_TEXT response */                  /* We're expecting a CF_TEXT response */
746                  uint8 *firstnull;                  uint8 *firstnull;
747    
748                  /* translate linebreaks */                  /* translate linebreaks */
749                  crlf2lf(data, &length);                  crlf2lf(data, &length);
750    
751                  /* Only send data up to null byte, if any */                  /* Only send data up to null byte, if any */
752                  firstnull = (uint8 *) strchr((char *) data, '\0');                  firstnull = (uint8 *) strchr((char *) data, '\0');
753                  if (firstnull)                  if (firstnull)
754                  {                  {
755                          length = firstnull - data + 1;                          length = firstnull - data + 1;
756                  }                  }
757          }          }
758  #ifdef USE_UNICODE_CLIPBOARD  #ifdef USE_UNICODE_CLIPBOARD
759          else if (selection_request.target == format_utf8_string_atom)          else if (selection_request.target == format_utf8_string_atom)
760          {          {
761                  /* We're expecting a CF_UNICODETEXT response */                  /* We're expecting a CF_UNICODETEXT response */
762                  iconv_t cd = iconv_open("UTF-8", WINDOWS_CODEPAGE);                  iconv_t cd = iconv_open("UTF-8", WINDOWS_CODEPAGE);
763                  if (cd != (iconv_t)-1)                  if (cd != (iconv_t) - 1)
764                  {                  {
765                          size_t utf8_length = length * 2;                          size_t utf8_length = length * 2;
766                          char* utf8_data = malloc(utf8_length);                          char *utf8_data = malloc(utf8_length);
767                          size_t utf8_length_remaining = utf8_length;                          size_t utf8_length_remaining = utf8_length;
768                          char* utf8_data_remaining = utf8_data;                          char *utf8_data_remaining = utf8_data;
769                          char* data_remaining = (char*)data;                          char *data_remaining = (char *) data;
770                          size_t length_remaining = (size_t)length;                          size_t length_remaining = (size_t) length;
771                          if (utf8_data == NULL)                          if (utf8_data == NULL)
772                          {                          {
773                                  iconv_close(cd);                                  iconv_close(cd);
774                                  return;                                  return;
775                          }                          }
776                          iconv(cd, &data_remaining, &length_remaining, &utf8_data_remaining, &utf8_length_remaining);                          iconv(cd, (ICONV_CONST char **) &data_remaining, &length_remaining,
777                                  &utf8_data_remaining, &utf8_length_remaining);
778                          iconv_close(cd);                          iconv_close(cd);
779                          free_data = True;                          free_data = True;
780                          data = (uint8*)utf8_data;                          data = (uint8 *) utf8_data;
781                          length = utf8_length - utf8_length_remaining;                          length = utf8_length - utf8_length_remaining;
782                  }                  }
783          }          }
# Line 837  ui_clip_request_data(uint32 format) Line 842  ui_clip_request_data(uint32 format)
842          }          }
843    
844          /* No data available */          /* No data available */
845          cliprdr_send_data(NULL, 0);          helper_cliprdr_send_empty_response();
846  }  }
847    
848  void  void
# Line 868  xclip_init(void) Line 873  xclip_init(void)
873          targets[num_targets++] = timestamp_atom;          targets[num_targets++] = timestamp_atom;
874          targets[num_targets++] = rdesktop_clipboard_formats_atom;          targets[num_targets++] = rdesktop_clipboard_formats_atom;
875          targets[num_targets++] = format_string_atom;          targets[num_targets++] = format_string_atom;
876          #ifdef USE_UNICODE_CLIPBOARD  #ifdef USE_UNICODE_CLIPBOARD
877          targets[num_targets++] = format_utf8_string_atom;          targets[num_targets++] = format_utf8_string_atom;
878          #endif  #endif
879          targets[num_targets++] = format_unicode_atom;          targets[num_targets++] = format_unicode_atom;
880          targets[num_targets++] = XA_STRING;          targets[num_targets++] = XA_STRING;
881    

Legend:
Removed from v.1037  
changed lines
  Added in v.1203

  ViewVC Help
Powered by ViewVC 1.1.26