/[rdesktop]/sourceforge.net/trunk/rdesktop/cliprdr.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/cliprdr.c

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

revision 392 by forsberg, Fri Jun 6 09:30:06 2003 UTC revision 1202 by ossman_, Mon Mar 27 08:30:15 2006 UTC
# Line 2  Line 2 
2     rdesktop: A Remote Desktop Protocol client.     rdesktop: A Remote Desktop Protocol client.
3     Protocol services - Clipboard functions     Protocol services - Clipboard functions
4     Copyright (C) Erik Forsberg <forsberg@cendio.se> 2003     Copyright (C) Erik Forsberg <forsberg@cendio.se> 2003
5       Copyright (C) Matthew Chapman 2003
6    
7     This program is free software; you can redistribute it and/or modify     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by     it under the terms of the GNU General Public License as published by
# Line 18  Line 19 
19     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */  */
21    
 #include <X11/Xlib.h>  
 #include <X11/Xatom.h>  
22  #include "rdesktop.h"  #include "rdesktop.h"
23    
24  extern BOOL encryption;  #define CLIPRDR_CONNECT                 1
25  extern Display *display;  #define CLIPRDR_FORMAT_ANNOUNCE         2
26  extern Window wnd;  #define CLIPRDR_FORMAT_ACK              3
27  extern Time last_gesturetime;  #define CLIPRDR_DATA_REQUEST            4
28    #define CLIPRDR_DATA_RESPONSE           5
 // static Time selection_timestamp;  
 static Atom clipboard_atom, primary_atom, targets_atom, timestamp_atom;  
 static Atom rdesktop_clipboard_target_atom, incr_atom;  
 static cliprdr_dataformat *server_formats = NULL;  
 static uint16 num_server_formats = 0;  
 static XSelectionEvent selection_event;  
 static uint16 clipboard_channelno;  
 static Atom targets[NUM_TARGETS];  
29    
30  static void  #define CLIPRDR_REQUEST                 0
31  cliprdr_print_server_formats(void)  #define CLIPRDR_RESPONSE                1
32  {  #define CLIPRDR_ERROR                   2
 #ifdef WITH_DEBUG_CLIPBOARD  
         cliprdr_dataformat *this;  
         uint16 i = 0;  
         this = server_formats;  
         DEBUG_CLIPBOARD(("There should be %d server formats.\n", num_server_formats));  
         while (NULL != this)  
         {  
                 DEBUG_CLIPBOARD(("Format code %d\n", this->identifier));  
                 i++;  
                 this = this->next;  
         }  
         DEBUG_CLIPBOARD(("There were %d server formats.\n", i));  
 #endif  
 }  
 /*  
 static void  
 cliprdr_set_selection_timestamp(void)  
 {  
         XEvent xev;  
         DEBUG_CLIPBOARD(("Changing a property in order to get a timestamp\n"));  
         fflush(stdout);  
         XChangeProperty(display, wnd, rdesktop_clipboard_target_atom,  
                         XA_ATOM, 32, PropModeAppend, 0, 0);  
         DEBUG_CLIPBOARD(("Waiting for PropertyChange on wnd\n"));  
         fflush(stdout);  
         XWindowEvent(display, wnd,  
                      PropertyChangeMask, &xev);  
         DEBUG_CLIPBOARD(("Setting selection_timestamp\n"));  
         fflush(stdout);  
         selection_timestamp = xev.xproperty.time;  
 }        
 */        
33    
34  static void  static VCHANNEL *cliprdr_channel;
 cliprdr_send_format_announce(void)  
 {  
         DEBUG_CLIPBOARD(("Sending format announce\n"));  
   
         STREAM s;  
         int number_of_formats = 1;  
         s = sec_init(encryption ? SEC_ENCRYPT : 0, number_of_formats*36+12+4+4);  
         out_uint32_le(s, number_of_formats*36+12);  
         out_uint32_le(s, 0x13);  
         out_uint16_le(s, 2);  
         out_uint16_le(s, 0);  
         out_uint32_le(s, number_of_formats*36);  
           
         //      out_uint32_le(s, 0xd); // FIXME: This is a rather bogus unicode text description..  
         //      rdp_out_unistr(s, "", 16);  
         //      out_uint8s(s, 32);  
   
   
         out_uint32_le(s, 1); // FIXME: This is a rather bogus text description..  
         out_uint8s(s, 32);  
   
         out_uint32_le(s, 0);  
   
         s_mark_end(s);  
         sec_send_to_channel(s, encryption ? SEC_ENCRYPT : 0,  
                             clipboard_channelno);  
 }  
35    
36    static uint8 *last_formats = NULL;
37    static uint32 last_formats_length = 0;
38    
39  static void  static void
40  cliprdr_send_empty_datapacket(void)  cliprdr_send_packet(uint16 type, uint16 status, uint8 * data, uint32 length)
41  {  {
42          STREAM out;          STREAM s;
         out =  sec_init(encryption ? SEC_ENCRYPT : 0,  
                         20);  
         out_uint32_le(out, 12);  
         out_uint32_le(out, 0x13);  
         out_uint16_le(out, 5);  
         out_uint16_le(out, 1);  
         out_uint32_le(out, 0);  
         /* Insert null string here? */  
         out_uint32_le(out, 0);  
         s_mark_end(out);  
           
         sec_send_to_channel(out, encryption ? SEC_ENCRYPT : 0,  
                             clipboard_channelno);  
 }  
   
   
 void  
 cliprdr_handle_SelectionNotify(XSelectionEvent *event)  
 {  
   
         unsigned char   *data, *datap;  
         unsigned long   nitems, bytes_left;  
           
         unsigned long bytes_left_to_transfer;  
         int res, i;  
   
         int format;  
         Atom type_return;  
         Atom best_target;  
         Atom *supported_targets;  
   
         STREAM out;  
           
         DEBUG_CLIPBOARD(("cliprdr_handle_SelectionNotify\n"));  
   
         if (None == event->property) {  
                 cliprdr_send_empty_datapacket();  
                 return; /* Selection failed */  
         }  
   
         DEBUG_CLIPBOARD(("selection: %s, target: %s, property: %s\n",  
                          XGetAtomName(display, event->selection),  
                          XGetAtomName(display, event->target),  
                          XGetAtomName(display, event->property)));  
   
         if (targets_atom == event->target) {  
                 /* Response to TARGETS request. Let's find the target  
                    we want and request that */  
                 res = XGetWindowProperty(display, wnd,  
                                          rdesktop_clipboard_target_atom,  
                                          0L, 4096L, False, AnyPropertyType,  
                                          &type_return,  
                                          &format, &nitems, &bytes_left, &data);  
   
                 if (Success != res)  
                 {  
                         DEBUG_CLIPBOARD(("XGetWindowProperty failed!\n"));  
                         cliprdr_send_empty_datapacket();  
                         return;  
                 }  
   
                 if (None == type_return)  
                         /* The owner might no support TARGETS. Just try  
                            STRING */  
                         best_target = XA_STRING;  
                 else  
                 {  
                         /* FIXME: We should choose format here based  
                            on what the server wanted */  
                         supported_targets = (Atom *)data;  
                         for (i=0;i<nitems;i++)  
                         {  
                                 DEBUG_CLIPBOARD(("Target %d: %s\n",  
                                                  i, XGetAtomName(display,  
                                                                  supported_targets[i])));  
                         }  
                         best_target = XInternAtom(display, "TEXT", False);  
                           
                           
                 }  
   
                 XConvertSelection(display, primary_atom,  
                                   best_target,  
                                   rdesktop_clipboard_target_atom,  
                                   wnd, event->time);  
   
         }  
         else  /* Other clipboard data */  
         {  
                   
                 res = XGetWindowProperty(display, wnd,  
                                          rdesktop_clipboard_target_atom,  
                                          0L, 0x1FFFFFF,  
                                          True, AnyPropertyType,  
                                          &type_return,  
                                          &format, &nitems, &bytes_left, &data);  
   
   
                 /* FIXME: We need to handle INCR as well,  
                  this is a temporary solution. */  
   
                 if (incr_atom == type_return)  
                 {  
                         warning("We don't support INCR transfers at this time. Try cutting less data\n");  
                         cliprdr_send_empty_datapacket();  
                 }  
   
   
                 if (Success != res)  
                 {  
                         DEBUG_CLIPBOARD(("XGetWindowProperty failed!\n"));  
                         cliprdr_send_empty_datapacket();  
                         return;  
                 }  
   
                 DEBUG_CLIPBOARD(("Received %d bytes of clipboard data from X, there is %d remaining\n",  
                                  nitems, bytes_left));  
                 DEBUG_CLIPBOARD(("type_return is %s\n",  
                                  XGetAtomName(display, type_return)));  
   
                 datap = data;  
   
                 if (nitems+1 <= MAX_CLIPRDR_STANDALONE_DATASIZE)  
                 {  
                         out =  sec_init(encryption ? SEC_ENCRYPT : 0,  
                                         20+nitems+1);  
                         out_uint32_le(out, 12+nitems+1);  
                         out_uint32_le(out, 0x13);  
                         out_uint16_le(out, 5);  
                         out_uint16_le(out, 1);  
                         out_uint32_le(out, nitems+1);  
                         out_uint8p(out, datap, nitems+1);  
                         out_uint32_le(out, 0);  
                         s_mark_end(out);  
           
                         sec_send_to_channel(out, encryption ? SEC_ENCRYPT : 0,  
                                             clipboard_channelno);  
   
                 }  
                 else  
                 {  
                         DEBUG_CLIPBOARD(("Sending %d bytes of data\n",  
                                          16+MAX_CLIPRDR_STANDALONE_DATASIZE));  
                         out =  sec_init(encryption ? SEC_ENCRYPT : 0,  
                                         16+MAX_CLIPRDR_STANDALONE_DATASIZE);  
                         out_uint32_le(out, nitems+12);  
                         out_uint32_le(out, 0x11);  
                         out_uint16_le(out, 5);  
                         out_uint16_le(out, 1);  
                         out_uint32_le(out, nitems);  
                         out_uint8p(out, datap,  
                                    MAX_CLIPRDR_STANDALONE_DATASIZE);  
                         s_mark_end(out);  
           
                         sec_send_to_channel(out, encryption ? SEC_ENCRYPT : 0,  
                                             clipboard_channelno);  
   
                         bytes_left_to_transfer = nitems - MAX_CLIPRDR_STANDALONE_DATASIZE;  
                         datap+=MAX_CLIPRDR_STANDALONE_DATASIZE;  
   
                         while (bytes_left_to_transfer > MAX_CLIPRDR_STANDALONE_DATASIZE)  
                         {  
                                 DEBUG_CLIPBOARD(("Sending %d bytes of data\n",  
                                          16+MAX_CLIPRDR_CONTINUATION_DATASIZE));  
                                 out =  sec_init(encryption ? SEC_ENCRYPT : 0,  
                                                 8+MAX_CLIPRDR_CONTINUATION_DATASIZE);  
                                 out_uint32_le(out, nitems);  
                                 out_uint32_le(out, 0x10);  
                                 out_uint8p(out, datap,  
                                            MAX_CLIPRDR_CONTINUATION_DATASIZE);  
                                 s_mark_end(out);  
   
                                 sec_send_to_channel(out,  
                                                     encryption ? SEC_ENCRYPT : 0,  
                                                     clipboard_channelno);  
                                 bytes_left_to_transfer-= MAX_CLIPRDR_CONTINUATION_DATASIZE;  
                                 datap+=MAX_CLIPRDR_CONTINUATION_DATASIZE;  
                                   
                         }  
                         DEBUG_CLIPBOARD(("Sending %d bytes of data\n",  
                                          12+bytes_left_to_transfer));  
                         out =  sec_init(encryption ? SEC_ENCRYPT : 0,  
                                         12+bytes_left_to_transfer);  
                         out_uint32_le(out, nitems);  
                         out_uint32_le(out, 0x12);  
                         out_uint8p(out, datap,  
                                    bytes_left_to_transfer);  
                         out_uint32_le(out, 0x0);  
                         s_mark_end(out);  
           
                         sec_send_to_channel(out, encryption ? SEC_ENCRYPT : 0,  
                                             clipboard_channelno);  
   
                 }  
43    
44            DEBUG_CLIPBOARD(("CLIPRDR send: type=%d, status=%d, length=%d\n", type, status, length));
45    
46                  XFree(data);          s = channel_init(cliprdr_channel, length + 12);
47                  cliprdr_send_format_announce();          out_uint16_le(s, type);
48                            out_uint16_le(s, status);
49          }          out_uint32_le(s, length);
50                    out_uint8p(s, data, length);
51                    out_uint32(s, 0);       /* pad? */
52            s_mark_end(s);
53            channel_send(s, cliprdr_channel);
54  }  }
55    
56    /* Helper which announces our readiness to supply clipboard data
57       in a single format (such as CF_TEXT) to the RDP side.
58       To announce more than one format at a time, use
59       cliprdr_send_native_format_announce.
60     */
61  void  void
62  cliprdr_handle_SelectionClear(void)  cliprdr_send_simple_native_format_announce(uint32 format)
 {  
         DEBUG_CLIPBOARD(("cliprdr_handle_SelectionClear\n"));  
         cliprdr_send_format_announce();  
 }  
   
   
 static void  
 cliprdr_request_clipboard_data(uint32 formatcode)  
63  {  {
64          STREAM s;          uint8 buffer[36];
         s = sec_init(encryption ? SEC_ENCRYPT : 0, 24);  
         out_uint32_le(s, 16);  
         out_uint32_le(s, 0x13);  
         out_uint16_le(s, 4);  
         out_uint16_le(s, 0);  
         out_uint32_le(s, 4); // Remaining length  
         out_uint32_le(s, formatcode);  
         out_uint32_le(s, 0); // Unknown. Garbage pad?  
65    
66          s_mark_end(s);          DEBUG_CLIPBOARD(("cliprdr_send_simple_native_format_announce\n"));
67    
68          sec_send_to_channel(s, encryption ? SEC_ENCRYPT : 0,          buf_out_uint32(buffer, format);
69                              clipboard_channelno);          memset(buffer + 4, 0, sizeof(buffer) - 4);      /* description */
70            cliprdr_send_native_format_announce(buffer, sizeof(buffer));
71  }  }
72    
73    /* Announces our readiness to supply clipboard data in multiple
74       formats, each denoted by a 36-byte format descriptor of
75       [ uint32 format + 32-byte description ].
76     */
77  void  void
78  cliprdr_handle_SelectionRequest(XSelectionRequestEvent *xevent)  cliprdr_send_native_format_announce(uint8 * formats_data, uint32 formats_data_length)
79  {  {
80            DEBUG_CLIPBOARD(("cliprdr_send_native_format_announce\n"));
81    
82          XSelectionEvent xev;          cliprdr_send_packet(CLIPRDR_FORMAT_ANNOUNCE, CLIPRDR_REQUEST, formats_data,
83          DEBUG_CLIPBOARD(("cliprdr_handle_SelectionRequest\n"));                              formats_data_length);
         DEBUG_CLIPBOARD(("Requestor window id 0x%x ",  
                          (unsigned)xevent->requestor));  
         if (clipboard_atom == xevent->selection) {  
                 DEBUG_CLIPBOARD(("wants CLIPBOARD\n"));  
         }  
         if (primary_atom == xevent->selection) {  
                 DEBUG_CLIPBOARD(("wants PRIMARY\n"));  
         }    
         DEBUG_CLIPBOARD(("Target is %s (0x%x), property is %s (0x%x)\n",  
                          XGetAtomName(display, xevent->target),  
                          (unsigned)xevent->target,  
                          XGetAtomName(display, xevent->property),  
                          (unsigned)xevent->property));  
   
         xev.type = SelectionNotify;  
         xev.serial = 0;  
         xev.send_event = True;  
         xev.requestor = xevent->requestor;  
         xev.selection = xevent->selection;  
         xev.target = xevent->target;  
         xev.property = xevent->property;  
         xev.time = xevent->time;  
84    
85          if (targets_atom == xevent->target)          if (formats_data != last_formats)
         {  
                 DEBUG_CLIPBOARD(("TARGETS requested, sending list..\n"));  
                 XChangeProperty(display,  
                                 xevent->requestor,  
                                 xevent->property,  
                                 XA_ATOM,  
                                 32,  
                                 PropModeAppend,  
                                 (unsigned char *)&targets,  
                                 NUM_TARGETS);  
   
                 XSendEvent(display,  
                            xevent->requestor,  
                            False,  
                            NoEventMask,  
                            (XEvent *)&xev);  
                 return;  
         } else if (timestamp_atom == xevent->target)  
86          {          {
87                  XChangeProperty(display,                  if (last_formats)
88                                  xevent->requestor,                          xfree(last_formats);
                                 xevent->property,  
                                 XA_INTEGER,  
                                 32,  
                                 PropModeAppend,  
                                 (unsigned char *)&last_gesturetime,  
                                 1);  
                 XSendEvent(display,  
                            xevent->requestor,  
                            False,  
                            NoEventMask,  
                            (XEvent *)&xev);  
         } else /* Some other target */  
         {  
                 cliprdr_request_clipboard_data(CF_TEXT);  
                 memcpy(&selection_event, &xev, sizeof(xev));  
                 /* Return and wait for data, handled by  
                    cliprdr_handle_server_data */  
         }  
 }  
   
89    
90  static void                  last_formats = xmalloc(formats_data_length);
91  cliprdr_ack_format_list(void)                  memcpy(last_formats, formats_data, formats_data_length);
92  {                  last_formats_length = formats_data_length;
         STREAM s;  
         s = sec_init(encryption ? SEC_ENCRYPT : 0, 20);  
         out_uint32_le(s, 12);  
         out_uint32_le(s, 0x13);  
         out_uint16_le(s, 3);  
         out_uint16_le(s, 1);  
         out_uint32_le(s, 0);  
         out_uint32_le(s, 0x0000c0da);  
   
         s_mark_end(s);  
   
         sec_send_to_channel(s, encryption ? SEC_ENCRYPT : 0,  
                             clipboard_channelno);  
 }  
   
                   
   
   
   
 static void  
 cliprdr_register_server_formats(STREAM s)  
 {  
         uint32 remaining_length, pad;  
         uint16 num_formats;  
         cliprdr_dataformat *this, *next;  
   
         DEBUG_CLIPBOARD(("cliprdr_register_server_formats\n"));  
         in_uint32_le(s, remaining_length);  
   
         num_formats = remaining_length / 36;  
         if (NULL != server_formats) {  
                 this = server_formats;  
                 next = this->next;  
                 while (NULL != next) {  
                         xfree(this);  
                         this = NULL;  
                         this = next;  
                         next = this->next;  
                 }  
         }  
         this = xmalloc(sizeof(cliprdr_dataformat));  
         this->next = NULL;  
         server_formats = this;  
         num_server_formats = num_formats;  
         while (1 < num_formats) {  
                 in_uint32_le(s, this->identifier);  
                 in_uint8a(s, this->textual_description, 32);  
                 DEBUG_CLIPBOARD(("Stored format description with numeric id %d\n",  
                                  this->identifier));  
                 this-> next = xmalloc(sizeof(cliprdr_dataformat));  
                 this = this->next;  
                 num_formats--;  
93          }          }
         in_uint32_le(s, this->identifier);  
         DEBUG_CLIPBOARD(("Stored format description with numeric id %d\n", this->identifier));  
         in_uint8a(s, this->textual_description, 32);  
         this -> next = NULL;  
         in_uint32_le(s, pad);  
         cliprdr_print_server_formats();  
94  }  }
95    
96  static void  void
97  cliprdr_select_X_clipboards(void)  cliprdr_send_data_request(uint32 format)
98  {  {
99          XSetSelectionOwner(display, primary_atom, wnd, last_gesturetime);          uint8 buffer[4];
         if (wnd != XGetSelectionOwner(display, primary_atom))  
         {  
                 warning("Failed to aquire ownership of PRIMARY clipboard\n");  
         }  
         XSetSelectionOwner(display, clipboard_atom, wnd, last_gesturetime);  
         if (wnd != XGetSelectionOwner(display, clipboard_atom))  
         {  
                 warning("Failed to aquire ownership of CLIPBOARD clipboard\n");  
         }                
           
 }  
   
           
   
100    
101            DEBUG_CLIPBOARD(("cliprdr_send_data_request\n"));
102  static void          buf_out_uint32(buffer, format);
103  cliprdr_handle_first_handshake(STREAM s)          cliprdr_send_packet(CLIPRDR_DATA_REQUEST, CLIPRDR_REQUEST, buffer, sizeof(buffer));
 {  
         uint32 remaining_length, pad;  
         in_uint32_le(s, remaining_length);  
         in_uint32_le(s, pad);  
         DEBUG_CLIPBOARD(("Remaining length in first handshake frm server is %d, pad is %d\n",  
                          remaining_length, pad));  
         cliprdr_send_format_announce();  
104  }  }
105    
106  void cliprdr_handle_server_data(uint32 length, uint32 flags, STREAM s)  void
107    cliprdr_send_data(uint8 * data, uint32 length)
108  {  {
109          static uint32 remaining_length;          DEBUG_CLIPBOARD(("cliprdr_send_data\n"));
110          static char *data, *datap;          cliprdr_send_packet(CLIPRDR_DATA_RESPONSE, CLIPRDR_RESPONSE, data, length);
         static uint32 bytes_left_to_read;  
         DEBUG_CLIPBOARD(("In cliprdr_handle_server_data, flags is %d\n",  
                          flags));  
         if (3 == flags)  /* One-op write, no packets follows */  
         {  
                 in_uint32_le(s, remaining_length);  
                 data = s->p;  
         } else if (1 == flags) /* First of several packets */  
         {        
                 in_uint32_le(s, remaining_length);  
                 DEBUG_CLIPBOARD(("Remaining length is %d\n",  
                                  remaining_length));  
                 data = xmalloc(remaining_length);  
                 datap = data;  
                 DEBUG_CLIPBOARD(("Copying first %d bytes\n",  
                                  MAX_CLIPRDR_STANDALONE_DATASIZE));  
                 memcpy(datap, s->p, MAX_CLIPRDR_STANDALONE_DATASIZE);  
   
                 datap+=MAX_CLIPRDR_STANDALONE_DATASIZE;  
                 bytes_left_to_read = remaining_length-MAX_CLIPRDR_STANDALONE_DATASIZE;  
                 return;  
         } else if (0 == flags)  
         {  
                 DEBUG_CLIPBOARD(("Copying %d middle bytes",  
                                  MAX_CLIPRDR_CONTINUATION_DATASIZE));  
                 memcpy(datap, s->p, MAX_CLIPRDR_CONTINUATION_DATASIZE);  
   
                 datap+=MAX_CLIPRDR_CONTINUATION_DATASIZE;  
                 bytes_left_to_read-=MAX_CLIPRDR_CONTINUATION_DATASIZE;  
                 return;  
         } else if (2 == flags)  
         {  
                 DEBUG_CLIPBOARD(("Copying last %d bytes\n",  
                                  bytes_left_to_read));  
                 memcpy(datap, s->p, bytes_left_to_read);  
         }  
         XChangeProperty(display,  
                         selection_event.requestor,  
                         selection_event.property,  
                         XInternAtom(display, "STRING", False),  
                         8,  
                         PropModeAppend,  
                         data,  
                         remaining_length-1);  
   
         XSendEvent(display,  
                    selection_event.requestor,  
                    False,  
                    NoEventMask,  
                    (XEvent *)&selection_event);  
   
         if (flags & 0x12)  
                 xfree(data);  
   
111  }  }
112    
113  void cliprdr_handle_server_data_request(STREAM s)  static void
114    cliprdr_process(STREAM s)
115  {  {
116          Window selectionowner;          uint16 type, status;
117          uint32 remaining_length;          uint32 length, format;
118          uint32 wanted_formatcode, pad;          uint8 *data;
   
         in_uint32_le(s, remaining_length);  
         in_uint32_le(s, wanted_formatcode);  
         in_uint32_le(s, pad);  
   
         /* FIXME: Check that we support this formatcode */  
   
         DEBUG_CLIPBOARD(("Request from server for format %d\n",  
                          wanted_formatcode));  
   
         selectionowner = XGetSelectionOwner(display, primary_atom);  
   
         if (None != selectionowner)  
         {  
           
                 /* FIXME: Perhaps we should check if we are the owner? */  
   
                 XConvertSelection(display, primary_atom,  
                                   targets_atom,  
                                   rdesktop_clipboard_target_atom,  
                                   wnd, CurrentTime);  
   
                 /* The rest of the transfer is handled in  
                    cliprdr_handle_SelectionNotify */  
   
         } else  
         {  
                 DEBUG_CLIPBOARD(("There were no owner for PRIMARY, sending empty string\n")); // FIXME: Should we always send an empty string?  
   
                 cliprdr_send_empty_datapacket();  
         }  
   
   
 }  
           
119    
120  void cliprdr_callback(STREAM s, uint16 channelno)          in_uint16_le(s, type);
121  {          in_uint16_le(s, status);
         static int failed_clipboard_acks = 0;  
         struct timeval timeval;  
         uint32 length, flags;  
         uint16 ptype0, ptype1;  
         clipboard_channelno = channelno;  
         DEBUG_CLIPBOARD(("cliprdr_callback called with channelno %d, clipboard data:\n", channelno));  
 #ifdef WITH_DEBUG_CLIPBOARD  
         //      hexdump(s->p, s->end - s->p);  
 #endif  
122          in_uint32_le(s, length);          in_uint32_le(s, length);
123          in_uint32_le(s, flags);          data = s->p;
124    
125          DEBUG_CLIPBOARD(("length is %d, flags are %d\n", length, flags));          DEBUG_CLIPBOARD(("CLIPRDR recv: type=%d, status=%d, length=%d\n", type, status, length));
126    
127          if (3 == flags || 1 == flags) /* Single-write op or first-packet-of-several op */          if (status == CLIPRDR_ERROR)
128          {          {
129                  in_uint16_le(s, ptype0);                  if (type == CLIPRDR_FORMAT_ACK)
                 in_uint16_le(s, ptype1);  
                 DEBUG_CLIPBOARD(("ptype0 is %d, ptype1 is %d\n", ptype0, ptype1));  
                 if (1 == ptype0 && 0 == ptype1) {  
                         cliprdr_handle_first_handshake(s);  
                         return;  
                 } else if (3 == ptype0 && 1 == ptype1)  
                 {  
                         // Acknowledgment on our format announce. Do we care? Not right now.  
                         // There is a strange pad in this packet that we might need some time,  
                         // but probably not.  
                         DEBUG_CLIPBOARD(("Received format announce ACK\n"));  
                         failed_clipboard_acks = 0;  
                         return;  
   
                 } else if (3 == ptype0 && 2 == ptype1)  
                 {  
                         DEBUG_CLIPBOARD(("Received failed clipboard format announce ACK, retrying\n"));  
   
                         /* This is a fairly portable way to sleep 1/10 of  
                            a second.. */  
                         timeval.tv_sec = 0;  
                         timeval.tv_usec = 100;  
                         select(0, NULL, NULL, NULL, &timeval);  
                         if (failed_clipboard_acks < 3)  
                         {  
                                   
                                 cliprdr_send_format_announce();  
                                 /* Make sure we don't get stuck in this loop */  
                                 failed_clipboard_acks++;  
                         }  
                         else  
                         {  
                                 warning("Reached maximum number of clipboard format announce attempts. Pasting in Windows probably won't work well now.\n");  
                         }  
                 } else if (2 == ptype0 && 0 == ptype1)  
130                  {                  {
131                          cliprdr_register_server_formats(s);                          /* FIXME: We seem to get this when we send an announce while the server is
132                          cliprdr_select_X_clipboards();                             still processing a paste. Try sending another announce. */
133                          cliprdr_ack_format_list();                          cliprdr_send_native_format_announce(last_formats, last_formats_length);
134                          return;                          return;
                 } else if (5 == ptype0 && 1 == ptype1)  
                 {  
                         cliprdr_handle_server_data(length, flags, s);  
                 } else if (4 == ptype0 && 0 == ptype1)  
                 {  
                         cliprdr_handle_server_data_request(s);  
135                  }                  }
136    
137                                    DEBUG_CLIPBOARD(("CLIPRDR error (type=%d)\n", type));
138          }                  return;
139          else          }
140    
141            switch (type)
142          {          {
143                  DEBUG_CLIPBOARD(("Handling middle or last packet\n"));                  case CLIPRDR_CONNECT:
144                  cliprdr_handle_server_data(length, flags, s);                          ui_clip_sync();
145                            break;
146                    case CLIPRDR_FORMAT_ANNOUNCE:
147                            ui_clip_format_announce(data, length);
148                            cliprdr_send_packet(CLIPRDR_FORMAT_ACK, CLIPRDR_RESPONSE, NULL, 0);
149                            return;
150                    case CLIPRDR_FORMAT_ACK:
151                            break;
152                    case CLIPRDR_DATA_REQUEST:
153                            in_uint32_le(s, format);
154                            ui_clip_request_data(format);
155                            break;
156                    case CLIPRDR_DATA_RESPONSE:
157                            ui_clip_handle_data(data, length);
158                            break;
159                    case 7: /* TODO: W2K3 SP1 sends this on connect with a value of 1 */
160                            break;
161                    default:
162                            unimpl("CLIPRDR packet type %d\n", type);
163          }          }
164  }  }
165    
166    BOOL
167  void cliprdr_init(void)  cliprdr_init(void)
168  {  {
169          primary_atom = XInternAtom(display, "PRIMARY", False);          cliprdr_channel =
170          clipboard_atom = XInternAtom(display, "CLIPBOARD", False);                  channel_register("cliprdr",
171          targets_atom = XInternAtom(display, "TARGETS", False);                                   CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP |
172          timestamp_atom = XInternAtom(display, "TIMESTAMP", False);                                   CHANNEL_OPTION_COMPRESS_RDP | CHANNEL_OPTION_SHOW_PROTOCOL,
173          rdesktop_clipboard_target_atom = XInternAtom(display, "_RDESKTOP_CLIPBOARD_TARGET", False);                                   cliprdr_process);
174          incr_atom = XInternAtom(display, "INCR", False);          return (cliprdr_channel != NULL);
         targets[0] = targets_atom;  
         targets[1] = XInternAtom(display, "TEXT", False);  
         targets[2] = XInternAtom(display, "UTF8_STRING", False);  
         targets[3] = XInternAtom(display, "text/unicode", False);  
         targets[4] = XInternAtom(display, "TIMESTAMP", False);  
         targets[5] = XInternAtom(display, "STRING", False);  
   
175  }  }

Legend:
Removed from v.392  
changed lines
  Added in v.1202

  ViewVC Help
Powered by ViewVC 1.1.26