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

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

revision 383 by forsberg, Fri Jun 6 09:20:53 2003 UTC revision 1211 by ossman_, Mon Mar 27 12:29:29 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_keyrelease;  #define CLIPRDR_DATA_REQUEST            4
28    #define CLIPRDR_DATA_RESPONSE           5
29  static Atom clipboard_atom, primary_atom, targets_atom, timestamp_atom;  
30  static cliprdr_dataformat *server_formats = NULL;  #define CLIPRDR_REQUEST                 0
31  static uint16 num_server_formats = 0;  #define CLIPRDR_RESPONSE                1
32    #define CLIPRDR_ERROR                   2
33    
34    static VCHANNEL *cliprdr_channel;
35    
36    static uint8 *last_formats = NULL;
37    static uint32 last_formats_length = 0;
38    
39  static void  static void
40  cliprdr_print_server_formats(void)  cliprdr_send_packet(uint16 type, uint16 status, uint8 * data, uint32 length)
41  {  {
42  #ifdef WITH_DEBUG_CLIPBOARD          STREAM s;
         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 was %d server formats.\n", i));  
 #endif  
 }  
43    
44  void          DEBUG_CLIPBOARD(("CLIPRDR send: type=%d, status=%d, length=%d\n", type, status, length));
45  cliprdr_handle_SelectionNotify(void)  
46  {          s = channel_init(cliprdr_channel, length + 12);
47          DEBUG_CLIPBOARD(("cliprdr_handle_SelectionNotify\n"));          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)
63  {  {
64          DEBUG_CLIPBOARD(("cliprdr_handle_SelectionClear\n"));          uint8 buffer[36];
 }  
65    
66  void print_X_error(int res)          DEBUG_CLIPBOARD(("cliprdr_send_simple_native_format_announce\n"));
 {  
         switch(res) {  
         case Success:  
                 DEBUG_CLIPBOARD(("Success\n"));  
                 break;  
   
         case BadAtom:  
                 DEBUG_CLIPBOARD(("BadAtom\n"));  
                 break;  
   
         case BadRequest:  
                 DEBUG_CLIPBOARD(("BadRequest\n"));  
                 break;  
   
         case BadAlloc:  
                 DEBUG_CLIPBOARD(("BadAlloc\n"));  
                 break;  
   
         case BadMatch:  
                 DEBUG_CLIPBOARD(("BadMatch\n"));  
                 break;  
   
         case BadValue:  
                 DEBUG_CLIPBOARD(("BadValue\n"));  
                 break;  
   
         case BadWindow:  
                 DEBUG_CLIPBOARD(("BadWindo\n"));  
                 break;  
67    
68          default:          buf_out_uint32(buffer, format);
69                  DEBUG_CLIPBOARD(("Unknown X error code %d\n", res));          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          Atom type_return;          cliprdr_send_packet(CLIPRDR_FORMAT_ANNOUNCE, CLIPRDR_REQUEST, formats_data,
83          Atom *targets;                              formats_data_length);
         int format_return;  
         long nitems_return;  
         long bytes_after_return;  
         char **prop_return;  
         int res;  
   
         XSelectionEvent xev;  
         DEBUG_CLIPBOARD(("cliprdr_handle_SelectionRequest\n"));  
         DEBUG_CLIPBOARD(("Requestor window id 0x%x ", 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),  
                          xevent->target,  
                          XGetAtomName(display, xevent->property),  
                          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)
86          {          {
87                  DEBUG_CLIPBOARD(("TARGETS requested, sending list..\n"));                  if (last_formats)
88                  targets = xmalloc(4*sizeof(Atom));                          xfree(last_formats);
                 targets[0] = xevent->target;  
                 targets[1] = XInternAtom(display, "TEXT", True);  
                 targets[2] = XInternAtom(display, "UTF8_STRING", True);  
                 targets[3] = XInternAtom(display, "TIMESTAMP", True);  
                 res = XChangeProperty(display,  
                                       xevent->requestor,  
                                       xevent->property,  
                                       XA_ATOM,  
                                       32,  
                                       PropModeAppend,  
                                       (unsigned char *)targets,  
                                       3);  
                 DEBUG_CLIPBOARD(("res after XChangeProperty is "));  
                 print_X_error(res);      
   
                 res = XSendEvent(display,  
                                  xevent->requestor,  
                                  False,  
                                  NoEventMask,  
                                  (XEvent *)&xev);  
                 return;  
         } else if (timestamp_atom == xevent->target)  
         {  
                 DEBUG_CLIPBOARD(("TIMESTAMP requested... sending 0x%x\n",  
                                  last_keyrelease));  
                 res = XChangeProperty(display,  
                                       xevent->requestor,  
                                       xevent->property,  
                                       XA_INTEGER,  
                                       32,  
                                       PropModeAppend,  
                                       (unsigned char *)&last_keyrelease,  
                                       1);  
                 res = XSendEvent(display,  
                                  xevent->requestor,  
                                  False,  
                                  NoEventMask,  
                                  (XEvent *)&xev);  
         } else /* Some other target */  
         {  
                 res = XChangeProperty(display,  
                                       xevent->requestor,  
                                       xevent->property,  
                                       XInternAtom(display, "STRING", False),  
                                       8,  
                                       PropModeAppend,  
                                       "krattoflabkat",  
                                       13);  
   
                 DEBUG_CLIPBOARD(("res after XChangeProperty is "));  
                 print_X_error(res);      
           
                 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;  
   
                 res = XSendEvent(display,  
                                  xevent->requestor,  
                                  False,  
                                  NoEventMask,  
                                  (XEvent *)&xev);  
           
                 DEBUG_CLIPBOARD(("res after XSendEvent is "));  
                 print_X_error(res);  
         }  
 }  
   
   
 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--;  
         }  
         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();  
 }  
89    
90  static void                  last_formats = xmalloc(formats_data_length);
91  cliprdr_select_X_clipboards(void)                  memcpy(last_formats, formats_data, formats_data_length);
92  {                  last_formats_length = formats_data_length;
         XSetSelectionOwner(display, primary_atom, wnd, last_keyrelease);  
         if (wnd != XGetSelectionOwner(display, primary_atom))  
         {  
                 warning("Failed to aquire ownership of PRIMARY clipboard\n");  
93          }          }
         XSetSelectionOwner(display, clipboard_atom, wnd, CurrentTime);  
         if (wnd != XGetSelectionOwner(display, clipboard_atom))  
         {  
                 warning("Failed to aquire ownership of CLIPBOARD clipboard\n");  
         }                
           
94  }  }
95    
96  static void  void
97  cliprdr_send_format_announce(void)  cliprdr_send_data_request(uint32 format)
98  {  {
99          STREAM s;          uint8 buffer[4];
         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, 0);  
100    
101          s_mark_end(s);          DEBUG_CLIPBOARD(("cliprdr_send_data_request\n"));
102          sec_send_to_channel(s, encryption ? SEC_ENCRYPT : 0, 1005); // FIXME: Don't hardcode channel!          buf_out_uint32(buffer, format);
103            cliprdr_send_packet(CLIPRDR_DATA_REQUEST, CLIPRDR_REQUEST, buffer, sizeof(buffer));
104  }  }
                   
105    
106  static void  void
107  cliprdr_handle_first_handshake(STREAM s)  cliprdr_send_data(uint8 * data, uint32 length)
108  {  {
109          uint32 remaining_length, pad;          DEBUG_CLIPBOARD(("cliprdr_send_data\n"));
110          in_uint32_le(s, remaining_length);          cliprdr_send_packet(CLIPRDR_DATA_RESPONSE, CLIPRDR_RESPONSE, data, 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();  
111  }  }
112    
113  void cliprdr_callback(STREAM s)  static void
114    cliprdr_process(STREAM s)
115  {  {
116          uint32 length, flags;          uint16 type, status;
117          uint16 ptype0, ptype1;          uint32 length, format;
118          DEBUG_CLIPBOARD(("cliprdr_callback called, clipboard data:\n"));          uint8 *data;
119  #ifdef WITH_DEBUG_CLIPBOARD  
120          hexdump(s->p, s->end - s->p);          in_uint16_le(s, type);
121  #endif          in_uint16_le(s, status);
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 (flags & 0x03 || flags & 0x01) /* Single-write op or first-packet-of-several op */          if (status == CLIPRDR_ERROR)
128          {          {
129                  in_uint16_le(s, ptype0);                  switch (type)
                 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)  
130                  {                  {
131                          // Acknowledgment on our format announce. Do we care? Not right now.                          case CLIPRDR_FORMAT_ACK:
132                          // There is a strange pad in this packet that we might need some time,                                  /* FIXME: We seem to get this when we send an announce while the server is
133                          // but probably not.                                     still processing a paste. Try sending another announce. */
134                          DEBUG_CLIPBOARD(("Received format announce ACK\n"));                                  cliprdr_send_native_format_announce(last_formats,
135                          return;                                                                      last_formats_length);
136                  } else if (2 == ptype0 && 0 == ptype1)                                  break;
137                  {                          case CLIPRDR_DATA_RESPONSE:
138                          cliprdr_register_server_formats(s);                                  ui_clip_request_failed();
139                          cliprdr_select_X_clipboards();                                  break;
140                          return;                          default:
141                                    DEBUG_CLIPBOARD(("CLIPRDR error (type=%d)\n", type));
142                  }                  }
143                    
144                    return;
145            }
146    
147            switch (type)
148            {
149                    case CLIPRDR_CONNECT:
150                            ui_clip_sync();
151                            break;
152                    case CLIPRDR_FORMAT_ANNOUNCE:
153                            ui_clip_format_announce(data, length);
154                            cliprdr_send_packet(CLIPRDR_FORMAT_ACK, CLIPRDR_RESPONSE, NULL, 0);
155                            return;
156                    case CLIPRDR_FORMAT_ACK:
157                            break;
158                    case CLIPRDR_DATA_REQUEST:
159                            in_uint32_le(s, format);
160                            ui_clip_request_data(format);
161                            break;
162                    case CLIPRDR_DATA_RESPONSE:
163                            ui_clip_handle_data(data, length);
164                            break;
165                    case 7: /* TODO: W2K3 SP1 sends this on connect with a value of 1 */
166                            break;
167                    default:
168                            unimpl("CLIPRDR packet type %d\n", type);
169          }          }
170  }  }
171    
172  void cliprdr_init(void)  void
173    cliprdr_set_mode(const char *optarg)
174    {
175            ui_clip_set_mode(optarg);
176    }
177    
178    BOOL
179    cliprdr_init(void)
180  {  {
181          primary_atom = XInternAtom(display, "PRIMARY", False);          cliprdr_channel =
182          clipboard_atom = XInternAtom(display, "CLIPBOARD", False);                  channel_register("cliprdr",
183          targets_atom = XInternAtom(display, "TARGETS", True);                                   CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP |
184          timestamp_atom = XInternAtom(display, "TIMESTAMP", True);                                   CHANNEL_OPTION_COMPRESS_RDP | CHANNEL_OPTION_SHOW_PROTOCOL,
185                                     cliprdr_process);
186            return (cliprdr_channel != NULL);
187  }  }

Legend:
Removed from v.383  
changed lines
  Added in v.1211

  ViewVC Help
Powered by ViewVC 1.1.26