/[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 385 by forsberg, Fri Jun 6 09:23:28 2003 UTC revision 422 by forsberg, Mon Jun 16 09:03:55 2003 UTC
# Line 20  Line 20 
20    
21  #include <X11/Xlib.h>  #include <X11/Xlib.h>
22  #include <X11/Xatom.h>  #include <X11/Xatom.h>
23    #include <sys/time.h>
24    #include <sys/types.h>
25    #include <unistd.h>
26  #include "rdesktop.h"  #include "rdesktop.h"
27    
28  extern BOOL encryption;  extern BOOL encryption;
29  extern Display *display;  extern Display *display;
30  extern Window wnd;  extern Window wnd;
31  extern Time last_keyrelease;  extern Time last_gesturetime;
32    extern Atom ipc_atom;
33    
34    // static Time selection_timestamp;
35  static Atom clipboard_atom, primary_atom, targets_atom, timestamp_atom;  static Atom clipboard_atom, primary_atom, targets_atom, timestamp_atom;
36    static Atom rdesktop_clipboard_target_atom, incr_atom;
37  static cliprdr_dataformat *server_formats = NULL;  static cliprdr_dataformat *server_formats = NULL;
38  static uint16 num_server_formats = 0;  static uint16 num_server_formats = 0;
39  static XSelectionEvent selection_event;  static XSelectionEvent selection_event;
40    static uint16 clipboard_channelno;
41    static Atom targets[NUM_TARGETS];
42    static int have_primary = 0;
43    static int rdesktop_is_selection_owner = 0;
44    
45  static void  static void
46  cliprdr_print_server_formats(void)  cliprdr_print_server_formats(void)
47  {  {
48  #ifdef WITH_DEBUG_CLIPBOARD  #ifdef WITH_DEBUG_CLIPBOARD
49          cliprdr_dataformat *this;          cliprdr_dataformat *this;
50          uint16 i = 0;          uint16 i = 0;
51          this = server_formats;          this = server_formats;
52          DEBUG_CLIPBOARD(("There should be %d server formats.\n", num_server_formats));          DEBUG_CLIPBOARD(("There should be %d server formats.\n", num_server_formats));
53          while (NULL != this)          while (NULL != this)
54          {          {
55                  DEBUG_CLIPBOARD(("Format code %d\n", this->identifier));                  DEBUG_CLIPBOARD(("Format code %d\n", this->identifier));
56                  i++;                  i++;
# Line 50  cliprdr_print_server_formats(void) Line 60  cliprdr_print_server_formats(void)
60  #endif  #endif
61  }  }
62    
63  void  /*
64  cliprdr_handle_SelectionNotify(void)  static void
65    cliprdr_set_selection_timestamp(void)
66  {  {
67          DEBUG_CLIPBOARD(("cliprdr_handle_SelectionNotify\n"));          XEvent xev;
68            DEBUG_CLIPBOARD(("Changing a property in order to get a timestamp\n"));
69            fflush(stdout);
70            XChangeProperty(display, wnd, rdesktop_clipboard_target_atom,
71                            XA_ATOM, 32, PropModeAppend, 0, 0);
72            DEBUG_CLIPBOARD(("Waiting for PropertyChange on wnd\n"));
73            fflush(stdout);
74            XWindowEvent(display, wnd,
75                         PropertyChangeMask, &xev);
76            DEBUG_CLIPBOARD(("Setting selection_timestamp\n"));
77            fflush(stdout);
78            selection_timestamp = xev.xproperty.time;
79    }      
80    */
81    
82    static void
83    cliprdr_send_format_announce(void)
84    {
85            STREAM s;
86            int number_of_formats = 1;
87            DEBUG_CLIPBOARD(("Sending (empty) format announce\n"));
88            s = sec_init(encryption ? SEC_ENCRYPT : 0, number_of_formats * 36 + 12 + 4 + 4);
89            out_uint32_le(s, number_of_formats * 36 + 12);
90            out_uint32_le(s, 0x13);
91            out_uint16_le(s, 2);
92            out_uint16_le(s, 0);
93            out_uint32_le(s, number_of_formats * 36);
94    
95            //      out_uint32_le(s, 0xd); // FIXME: This is a rather bogus unicode text description..
96            //      rdp_out_unistr(s, "", 16);
97            //      out_uint8s(s, 32);
98    
99    
100            out_uint32_le(s, 1);    // FIXME: This is a rather bogus text description..
101            out_uint8s(s, 32);
102    
103            out_uint32_le(s, 0);
104    
105            s_mark_end(s);
106            sec_send_to_channel(s, encryption ? SEC_ENCRYPT : 0, clipboard_channelno);
107  }  }
108    
109  void  void
110  cliprdr_handle_SelectionClear(void)  cliprdr_ipc_format_announce(unsigned char *data, uint16 length)
111  {  {
112          DEBUG_CLIPBOARD(("cliprdr_handle_SelectionClear\n"));          STREAM s;
113            rdesktop_is_selection_owner = 1;
114            DEBUG_CLIPBOARD(("cliprdr_ipc_format_announce called, length is %d, data is %s, sending native format announce\n", length, data));
115    
116            s = sec_init(encryption ? SEC_ENCRYPT : 0, length + 12 + 4 + 4);
117            out_uint32_le(s, length + 12);
118            out_uint32_le(s, 0x13);
119            out_uint16_le(s, 2);
120            out_uint16_le(s, 0);
121            out_uint32_le(s, length);
122            out_uint8p(s, data, length);
123            out_uint32_le(s, 0);    // Pad
124            s_mark_end(s);
125    
126            sec_send_to_channel(s, encryption ? SEC_ENCRYPT : 0, clipboard_channelno);
127  }  }
128    
129  void print_X_error(int res)  
130    
131    
132    static void
133    cliprdr_send_empty_datapacket(void)
134  {  {
135          switch(res) {          STREAM out;
136          case Success:          out = sec_init(encryption ? SEC_ENCRYPT : 0, 20);
137                  DEBUG_CLIPBOARD(("Success\n"));          out_uint32_le(out, 12);
138                  break;          out_uint32_le(out, 0x13);
139            out_uint16_le(out, 5);
140          case BadAtom:          out_uint16_le(out, 1);
141                  DEBUG_CLIPBOARD(("BadAtom\n"));          out_uint32_le(out, 0);
142                  break;          /* Insert null string here? */
143            out_uint32_le(out, 0);
144          case BadRequest:          s_mark_end(out);
145                  DEBUG_CLIPBOARD(("BadRequest\n"));  
146                  break;          sec_send_to_channel(out, encryption ? SEC_ENCRYPT : 0, clipboard_channelno);
147    }
148          case BadAlloc:  
149                  DEBUG_CLIPBOARD(("BadAlloc\n"));  
150                  break;  void
151    cliprdr_handle_SelectionNotify(XSelectionEvent * event)
152          case BadMatch:  {
153                  DEBUG_CLIPBOARD(("BadMatch\n"));  
154                  break;          unsigned char *data, *datap;
155            unsigned long nitems, bytes_left;
156          case BadValue:  
157                  DEBUG_CLIPBOARD(("BadValue\n"));          unsigned long bytes_left_to_transfer;
158                  break;          int res, i;
159    
160          case BadWindow:          int format;
161                  DEBUG_CLIPBOARD(("BadWindow\n"));          Atom type_return;
162                  break;          Atom best_target, text_target;
163            Atom *supported_targets;
164    
165            STREAM out;
166    
167            DEBUG_CLIPBOARD(("cliprdr_handle_SelectionNotify\n"));
168    
169            if (None == event->property)
170            {
171                    cliprdr_send_empty_datapacket();
172                    return;         /* Selection failed */
173            }
174    
175            DEBUG_CLIPBOARD(("selection: %s, target: %s, property: %s\n",
176                             XGetAtomName(display, event->selection),
177                             XGetAtomName(display, event->target),
178                             XGetAtomName(display, event->property)));
179    
180            if (targets_atom == event->target)
181            {
182                    /* Response to TARGETS request. Let's find the target
183                       we want and request that */
184                    res = XGetWindowProperty(display, wnd,
185                                             rdesktop_clipboard_target_atom,
186                                             0L, 4096L, False, AnyPropertyType,
187                                             &type_return, &format, &nitems, &bytes_left, &data);
188    
189                    if (Success != res)
190                    {
191                            DEBUG_CLIPBOARD(("XGetWindowProperty failed!\n"));
192                            cliprdr_send_empty_datapacket();
193                            return;
194                    }
195    
196                    if (None == type_return)
197                            /* The owner might no support TARGETS. Just try
198                               STRING */
199                            best_target = XA_STRING;
200                    else
201                    {
202                            /* FIXME: We should choose format here based
203                               on what the server wanted */
204                            supported_targets = (Atom *) data;
205                            best_target = XInternAtom(display, "STRING", False);
206                            text_target = XInternAtom(display, "TEXT", False);
207                            for (i = 0; i < nitems; i++)
208                            {
209                                    DEBUG_CLIPBOARD(("Target %d: %s\n",
210                                                     i, XGetAtomName(display, supported_targets[i])));
211                                    if (text_target == supported_targets[i])
212                                    {
213                                            DEBUG_CLIPBOARD(("Other party supports TEXT, choosing that as best_target\n"));
214                                            best_target = supported_targets[i];
215                                    }
216                            }
217    
218    
219    
220                    }
221    
222                    XConvertSelection(display, primary_atom,
223                                      best_target, rdesktop_clipboard_target_atom, wnd, event->time);
224    
225            }
226    
227            else                    /* Other clipboard data */
228            {
229    
230                    res = XGetWindowProperty(display, wnd,
231                                             rdesktop_clipboard_target_atom,
232                                             0L, 0x1FFFFFF,
233                                             True, AnyPropertyType,
234                                             &type_return, &format, &nitems, &bytes_left, &data);
235    
236    
237                    /* FIXME: We need to handle INCR as well,
238                       this is a temporary solution. */
239    
240                    if (incr_atom == type_return)
241                    {
242                            warning("We don't support INCR transfers at this time. Try cutting less data\n");
243                            cliprdr_send_empty_datapacket();
244                    }
245    
246    
247                    if (Success != res)
248                    {
249                            DEBUG_CLIPBOARD(("XGetWindowProperty failed!\n"));
250                            cliprdr_send_empty_datapacket();
251                            return;
252                    }
253    
254                    datap = data;
255    
256                    if (nitems + 1 <= MAX_CLIPRDR_STANDALONE_DATASIZE)
257                    {
258                            out = sec_init(encryption ? SEC_ENCRYPT : 0, 20 + nitems + 1);
259                            out_uint32_le(out, 12 + nitems + 1);
260                            out_uint32_le(out, 0x13);
261                            out_uint16_le(out, 5);
262                            out_uint16_le(out, 1);
263                            out_uint32_le(out, nitems + 1);
264                            out_uint8p(out, datap, nitems + 1);
265                            out_uint32_le(out, 0);
266                            s_mark_end(out);
267    
268                            sec_send_to_channel(out, encryption ? SEC_ENCRYPT : 0, clipboard_channelno);
269    
270                    }
271                    else
272                    {
273                            DEBUG_CLIPBOARD(("Sending %d bytes of data\n",
274                                             16 + MAX_CLIPRDR_STANDALONE_DATASIZE));
275                            out = sec_init(encryption ? SEC_ENCRYPT : 0,
276                                           16 + MAX_CLIPRDR_STANDALONE_DATASIZE);
277                            out_uint32_le(out, nitems + 12);
278                            out_uint32_le(out, 0x11);
279                            out_uint16_le(out, 5);
280                            out_uint16_le(out, 1);
281                            out_uint32_le(out, nitems);
282                            out_uint8p(out, datap, MAX_CLIPRDR_STANDALONE_DATASIZE);
283                            s_mark_end(out);
284    
285                            sec_send_to_channel(out, encryption ? SEC_ENCRYPT : 0, clipboard_channelno);
286    
287                            bytes_left_to_transfer = nitems - MAX_CLIPRDR_STANDALONE_DATASIZE;
288                            datap += MAX_CLIPRDR_STANDALONE_DATASIZE;
289    
290                            while (bytes_left_to_transfer > MAX_CLIPRDR_STANDALONE_DATASIZE)
291                            {
292                                    DEBUG_CLIPBOARD(("Sending %d bytes of data\n",
293                                                     16 + MAX_CLIPRDR_CONTINUATION_DATASIZE));
294                                    out = sec_init(encryption ? SEC_ENCRYPT : 0,
295                                                   8 + MAX_CLIPRDR_CONTINUATION_DATASIZE);
296                                    out_uint32_le(out, nitems);
297                                    out_uint32_le(out, 0x10);
298                                    out_uint8p(out, datap, MAX_CLIPRDR_CONTINUATION_DATASIZE);
299                                    s_mark_end(out);
300    
301                                    sec_send_to_channel(out,
302                                                        encryption ? SEC_ENCRYPT : 0,
303                                                        clipboard_channelno);
304                                    bytes_left_to_transfer -= MAX_CLIPRDR_CONTINUATION_DATASIZE;
305                                    datap += MAX_CLIPRDR_CONTINUATION_DATASIZE;
306    
307                            }
308                            DEBUG_CLIPBOARD(("Sending %u bytes of data\n",
309                                             12 + bytes_left_to_transfer));
310                            out = sec_init(encryption ? SEC_ENCRYPT : 0, 12 + bytes_left_to_transfer);
311                            out_uint32_le(out, nitems);
312                            out_uint32_le(out, 0x12);
313                            out_uint8p(out, datap, bytes_left_to_transfer);
314                            out_uint32_le(out, 0x0);
315                            s_mark_end(out);
316    
317                            sec_send_to_channel(out, encryption ? SEC_ENCRYPT : 0, clipboard_channelno);
318    
319                    }
320    
321    
322                    XFree(data);
323                    if (!rdesktop_is_selection_owner)
324                            cliprdr_send_format_announce();
325    
         default:  
                 DEBUG_CLIPBOARD(("Unknown X error code %d\n", res));  
326          }          }
327    
328    
329  }  }
330    
331    void
332    cliprdr_handle_SelectionClear(void)
333    {
334            DEBUG_CLIPBOARD(("cliprdr_handle_SelectionClear\n"));
335            have_primary = 0;
336            ipc_send_message(RDESKTOP_IPC_CLIPRDR_PRIMARY_LOST, "", 0);
337            cliprdr_send_format_announce();
338    }
339    
340    
341  static void  static void
342  cliprdr_request_clipboard_data(uint32 formatcode)  cliprdr_request_clipboard_data(uint32 formatcode)
343  {  {
344          STREAM s;          STREAM s;
345          s = sec_init(encryption ? SEC_ENCRYPT : 0, 24);          s = sec_init(encryption ? SEC_ENCRYPT : 0, 24);
# Line 107  cliprdr_request_clipboard_data(uint32 fo Line 347  cliprdr_request_clipboard_data(uint32 fo
347          out_uint32_le(s, 0x13);          out_uint32_le(s, 0x13);
348          out_uint16_le(s, 4);          out_uint16_le(s, 4);
349          out_uint16_le(s, 0);          out_uint16_le(s, 0);
350          out_uint32_le(s, 4); // Remaining length          out_uint32_le(s, 4);    // Remaining length
351          out_uint32_le(s, formatcode);          out_uint32_le(s, formatcode);
352          out_uint32_le(s, 0); // Unknown. Garbage pad?          out_uint32_le(s, 0);    // Unknown. Garbage pad?
353    
354          s_mark_end(s);          s_mark_end(s);
355    
356          sec_send_to_channel(s, encryption ? SEC_ENCRYPT : 0, 1005); // FIXME: Don't hardcode channel!          sec_send_to_channel(s, encryption ? SEC_ENCRYPT : 0, clipboard_channelno);
357  }  }
358    
359    
360  void  void
361  cliprdr_handle_SelectionRequest(XSelectionRequestEvent *xevent)  cliprdr_handle_SelectionRequest(XSelectionRequestEvent * xevent)
362  {  {
363    
         Atom *targets;  
         int res;  
   
364          XSelectionEvent xev;          XSelectionEvent xev;
365            unsigned long nitems, bytes_left;
366            Atom type_return;
367            uint32 *wanted_formatcode;
368            int format;
369    
370          DEBUG_CLIPBOARD(("cliprdr_handle_SelectionRequest\n"));          DEBUG_CLIPBOARD(("cliprdr_handle_SelectionRequest\n"));
371          DEBUG_CLIPBOARD(("Requestor window id 0x%x ",          DEBUG_CLIPBOARD(("Requestor window id 0x%x ", (unsigned) xevent->requestor));
372                           (unsigned)xevent->requestor));          if (clipboard_atom == xevent->selection)
373          if (clipboard_atom == xevent->selection) {          {
374                  DEBUG_CLIPBOARD(("wants CLIPBOARD\n"));                  DEBUG_CLIPBOARD(("wants CLIPBOARD\n"));
375          }          }
376          if (primary_atom == xevent->selection) {          if (primary_atom == xevent->selection)
377            {
378                  DEBUG_CLIPBOARD(("wants PRIMARY\n"));                  DEBUG_CLIPBOARD(("wants PRIMARY\n"));
379          }            }
380          DEBUG_CLIPBOARD(("Target is %s (0x%x), property is %s (0x%x)\n",          DEBUG_CLIPBOARD(("Target is %s (0x%x), property is %s (0x%x)\n",
381                           XGetAtomName(display, xevent->target),                           XGetAtomName(display, xevent->target),
382                           (unsigned)xevent->target,                           (unsigned) xevent->target,
383                           XGetAtomName(display, xevent->property),                           XGetAtomName(display, xevent->property), (unsigned) xevent->property));
                          (unsigned)xevent->property));  
384    
385          xev.type = SelectionNotify;          xev.type = SelectionNotify;
386          xev.serial = 0;          xev.serial = 0;
# Line 149  cliprdr_handle_SelectionRequest(XSelecti Line 391  cliprdr_handle_SelectionRequest(XSelecti
391          xev.property = xevent->property;          xev.property = xevent->property;
392          xev.time = xevent->time;          xev.time = xevent->time;
393    
394          if (targets_atom == xevent->target)          memcpy(&selection_event, &xev, sizeof(xev));
395    
396            if (ipc_atom == xevent->target)
397            {
398                    DEBUG_CLIPBOARD(("Target atom is ipc_atom, getting INTEGER from requestor\n"));
399                    XGetWindowProperty(display, xevent->requestor,
400                                       rdesktop_clipboard_target_atom,
401                                       0,
402                                       1,
403                                       True, XA_INTEGER,
404                                       &type_return,
405                                       &format,
406                                       &nitems, &bytes_left, (unsigned char **) &wanted_formatcode);
407                    DEBUG_CLIPBOARD(("Got wanted formatcode %d, format is %d\n", *wanted_formatcode,
408                                     format));
409                    cliprdr_request_clipboard_data(*wanted_formatcode);
410            }
411    
412            else if (targets_atom == xevent->target)
413          {          {
414                  DEBUG_CLIPBOARD(("TARGETS requested, sending list..\n"));                  DEBUG_CLIPBOARD(("TARGETS requested, sending list..\n"));
415                  targets = xmalloc(4*sizeof(Atom));                  XChangeProperty(display,
416                  targets[0] = xevent->target;                                  xevent->requestor,
417                  targets[1] = XInternAtom(display, "TEXT", True);                                  xevent->property,
418                  targets[2] = XInternAtom(display, "UTF8_STRING", True);                                  XA_ATOM,
419                  targets[3] = XInternAtom(display, "TIMESTAMP", True);                                  32, PropModeAppend, (unsigned char *) &targets, NUM_TARGETS);
420                  res = XChangeProperty(display,  
421                                        xevent->requestor,                  XSendEvent(display, xevent->requestor, False, NoEventMask, (XEvent *) & xev);
                                       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);  
422                  return;                  return;
423          } else if (timestamp_atom == xevent->target)          }
424            else if (timestamp_atom == xevent->target)
425          {          {
426                  DEBUG_CLIPBOARD(("TIMESTAMP requested... sending 0x%x\n",                  DEBUG_CLIPBOARD(("Sending TIMESTAMP\n"));
427                                   (unsigned)last_keyrelease));                  XChangeProperty(display,
428                  res = XChangeProperty(display,                                  xevent->requestor,
429                                        xevent->requestor,                                  xevent->property,
430                                        xevent->property,                                  XA_INTEGER,
431                                        XA_INTEGER,                                  32, PropModeAppend, (unsigned char *) &last_gesturetime, 1);
432                                        32,                  XSendEvent(display, xevent->requestor, False, NoEventMask, (XEvent *) & xev);
433                                        PropModeAppend,          }
434                                        (unsigned char *)&last_keyrelease,          else                    /* Some other target */
                                       1);  
                 res = XSendEvent(display,  
                                  xevent->requestor,  
                                  False,  
                                  NoEventMask,  
                                  (XEvent *)&xev);  
         } else /* Some other target */  
435          {          {
436                  cliprdr_request_clipboard_data(CF_TEXT);                  cliprdr_request_clipboard_data(CF_TEXT);
                 memcpy(&selection_event, &xev, sizeof(xev));  
437                  /* Return and wait for data, handled by                  /* Return and wait for data, handled by
438                     cliprdr_handle_server_data */                     cliprdr_handle_server_data */
439          }          }
440  }  }
441    
442    
443  static void  static void
444  cliprdr_ack_format_list(void)  cliprdr_ack_format_list(void)
445  {  {
446          STREAM s;          STREAM s;
447          s = sec_init(encryption ? SEC_ENCRYPT : 0, 20);          s = sec_init(encryption ? SEC_ENCRYPT : 0, 20);
# Line 215  cliprdr_ack_format_list(void) Line 454  cliprdr_ack_format_list(void)
454    
455          s_mark_end(s);          s_mark_end(s);
456    
457          sec_send_to_channel(s, encryption ? SEC_ENCRYPT : 0, 1005); // FIXME: Don't hardcode channel!          sec_send_to_channel(s, encryption ? SEC_ENCRYPT : 0, clipboard_channelno);
458  }  }
459    
460                    
461    
462    
463    
464  static void  static void
465  cliprdr_register_server_formats(STREAM s)  cliprdr_register_server_formats(STREAM s)
466  {  {
467          uint32 remaining_length, pad;          uint32 remaining_length, pad;
468          uint16 num_formats;          uint16 num_formats;
469          cliprdr_dataformat *this, *next;          cliprdr_dataformat *this, *next;
470    
         DEBUG_CLIPBOARD(("cliprdr_register_server_formats\n"));  
471          in_uint32_le(s, remaining_length);          in_uint32_le(s, remaining_length);
472            DEBUG_CLIPBOARD(("cliprdr_register_server_formats, remaining_length is %d\n",
473                             remaining_length));
474    
475    
476          num_formats = remaining_length / 36;          num_formats = remaining_length / 36;
477          if (NULL != server_formats) {  
478            ipc_send_message(RDESKTOP_IPC_CLIPRDR_FORMAT_ANNOUNCE, s->p, remaining_length);
479            if (NULL != server_formats)
480            {
481                  this = server_formats;                  this = server_formats;
482                  next = this->next;                  next = this->next;
483                  while (NULL != next) {                  while (NULL != next)
484                    {
485                          xfree(this);                          xfree(this);
486                          this = NULL;                          this = NULL;
487                          this = next;                          this = next;
488                          next = this->next;                          next = this->next;
489                  }                  }
490          }          }
491          this = xmalloc(sizeof(cliprdr_dataformat));          this = xmalloc(sizeof(cliprdr_dataformat));
492          this->next = NULL;          this->next = NULL;
493          server_formats = this;          server_formats = this;
494          num_server_formats = num_formats;          num_server_formats = num_formats;
495          while (1 < num_formats) {          while (1 < num_formats)
496            {
497                  in_uint32_le(s, this->identifier);                  in_uint32_le(s, this->identifier);
498                  in_uint8a(s, this->textual_description, 32);                  in_uint8a(s, this->textual_description, 32);
499                  DEBUG_CLIPBOARD(("Stored format description with numeric id %d\n",                  DEBUG_CLIPBOARD(("Stored format description with numeric id %d\n",
500                                   this->identifier));                                   this->identifier));
501                  this-> next = xmalloc(sizeof(cliprdr_dataformat));                  this->next = xmalloc(sizeof(cliprdr_dataformat));
502                  this = this->next;                  this = this->next;
503                  num_formats--;                  num_formats--;
504          }          }
505          in_uint32_le(s, this->identifier);          in_uint32_le(s, this->identifier);
506          DEBUG_CLIPBOARD(("Stored format description with numeric id %d\n", this->identifier));          DEBUG_CLIPBOARD(("Stored format description with numeric id %d\n", this->identifier));
507          in_uint8a(s, this->textual_description, 32);          in_uint8a(s, this->textual_description, 32);
508          this -> next = NULL;          this->next = NULL;
509          in_uint32_le(s, pad);          in_uint32_le(s, pad);
510          cliprdr_print_server_formats();          cliprdr_print_server_formats();
511  }  }
512    
513  static void  static void
514  cliprdr_select_X_clipboards(void)  cliprdr_select_X_clipboards(void)
515  {  {
516          XSetSelectionOwner(display, primary_atom, wnd, last_keyrelease);          XSetSelectionOwner(display, primary_atom, wnd, last_gesturetime);
517          if (wnd != XGetSelectionOwner(display, primary_atom))          if (wnd != XGetSelectionOwner(display, primary_atom))
518          {          {
519                  warning("Failed to aquire ownership of PRIMARY clipboard\n");                  warning("Failed to aquire ownership of PRIMARY clipboard\n");
520          }          }
521          XSetSelectionOwner(display, clipboard_atom, wnd, CurrentTime);          else
522          if (wnd != XGetSelectionOwner(display, clipboard_atom))          {
523                    have_primary = 1;
524            }
525            XSetSelectionOwner(display, clipboard_atom, wnd, last_gesturetime);
526            if (wnd != XGetSelectionOwner(display, clipboard_atom))
527          {          {
528                  warning("Failed to aquire ownership of CLIPBOARD clipboard\n");                  warning("Failed to aquire ownership of CLIPBOARD clipboard\n");
529          }                        }
           
 }  
   
           
   
 static void  
 cliprdr_send_format_announce(void)  
 {  
         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);  
530    
         s_mark_end(s);  
         sec_send_to_channel(s, encryption ? SEC_ENCRYPT : 0, 1005); // FIXME: Don't hardcode channel!  
531  }  }
532    
533    
534  static void  
535    static void
536  cliprdr_handle_first_handshake(STREAM s)  cliprdr_handle_first_handshake(STREAM s)
537  {  {
538          uint32 remaining_length, pad;          uint32 remaining_length, pad;
539          in_uint32_le(s, remaining_length);          in_uint32_le(s, remaining_length);
540          in_uint32_le(s, pad);          in_uint32_le(s, pad);
541          DEBUG_CLIPBOARD(("Remaining length in first handshake frm server is %d, pad is %d\n",          DEBUG_CLIPBOARD(("Remaining length in first handshake frm server is %d, pad is %d\n",
542                           remaining_length, pad));                           remaining_length, pad));
543          cliprdr_send_format_announce();          cliprdr_send_format_announce();
544  }  }
545    
546  void cliprdr_handle_server_data(uint32 length, STREAM s)  void
547    cliprdr_handle_server_data(uint32 length, uint32 flags, STREAM s)
548  {  {
549          uint32 remaining_length;          static uint32 remaining_length;
550          char *data;          static char *data, *datap;
551          int res;          static uint32 bytes_left_to_read;
552          in_uint32_le(s, remaining_length);          DEBUG_CLIPBOARD(("In cliprdr_handle_server_data, flags is %d\n", flags));
553          data = s->p;          if (3 == flags)         /* One-op write, no packets follows */
554          res = XChangeProperty(display,          {
555                                selection_event.requestor,                  in_uint32_le(s, remaining_length);
556                                selection_event.property,                  data = s->p;
557                                XInternAtom(display, "STRING", False),          }
558                                8,          else if (1 == flags)    /* First of several packets */
559                                PropModeAppend,          {
560                                data,                  in_uint32_le(s, remaining_length);
561                                remaining_length);                  DEBUG_CLIPBOARD(("Remaining length is %d\n", remaining_length));
562                    data = xmalloc(remaining_length);
563          DEBUG_CLIPBOARD(("res after XChangeProperty is "));                  datap = data;
564          print_X_error(res);                      DEBUG_CLIPBOARD(("Copying first %d bytes\n", MAX_CLIPRDR_STANDALONE_DATASIZE));
565                            memcpy(datap, s->p, MAX_CLIPRDR_STANDALONE_DATASIZE);
566          res = XSendEvent(display,  
567                           selection_event.requestor,                  datap += MAX_CLIPRDR_STANDALONE_DATASIZE;
568                           False,                  bytes_left_to_read = remaining_length - MAX_CLIPRDR_STANDALONE_DATASIZE;
569                           NoEventMask,                  return;
570                           (XEvent *)&selection_event);          }
571                    else if (0 == flags)
572          DEBUG_CLIPBOARD(("res after XSendEvent is "));          {
573          print_X_error(res);                  DEBUG_CLIPBOARD(("Copying %d middle bytes", MAX_CLIPRDR_CONTINUATION_DATASIZE));
574                    memcpy(datap, s->p, MAX_CLIPRDR_CONTINUATION_DATASIZE);
575    
576                    datap += MAX_CLIPRDR_CONTINUATION_DATASIZE;
577                    bytes_left_to_read -= MAX_CLIPRDR_CONTINUATION_DATASIZE;
578                    return;
579            }
580            else if (2 == flags)
581            {
582                    DEBUG_CLIPBOARD(("Copying last %d bytes\n", bytes_left_to_read));
583                    memcpy(datap, s->p, bytes_left_to_read);
584            }
585            DEBUG_CLIPBOARD(("Setting target atom (%s) on %d\n",
586                             XGetAtomName(display, selection_event.property),
587                             selection_event.requestor));
588            XChangeProperty(display,
589                            selection_event.requestor,
590                            selection_event.property,
591                            XInternAtom(display, "STRING", False),
592                            8, PropModeAppend, data, remaining_length - 1);
593    
594            XSendEvent(display,
595                       selection_event.requestor, False, NoEventMask, (XEvent *) & selection_event);
596    
597            if (2 == flags)
598                    xfree(data);
599    
600  }  }
601    
602  void cliprdr_handle_server_data_request(STREAM s)  void
603    cliprdr_handle_server_data_request(STREAM s)
604  {  {
605            Window selectionowner;
606          uint32 remaining_length;          uint32 remaining_length;
607          uint32 wanted_formatcode, pad;          uint32 wanted_formatcode, pad;
         int ret;  
         STREAM out;  
608    
609          in_uint32_le(s, remaining_length);          in_uint32_le(s, remaining_length);
610          in_uint32_le(s, wanted_formatcode);          in_uint32_le(s, wanted_formatcode);
# Line 363  void cliprdr_handle_server_data_request( Line 612  void cliprdr_handle_server_data_request(
612    
613          /* FIXME: Check that we support this formatcode */          /* FIXME: Check that we support this formatcode */
614    
615          DEBUG_CLIPBOARD(("Request from server for format %d\n",          DEBUG_CLIPBOARD(("Request from server for format %d\n", wanted_formatcode));
                          wanted_formatcode));  
616    
617          out =  sec_init(encryption ? SEC_ENCRYPT : 0,          selectionowner = XGetSelectionOwner(display, primary_atom);
                         26);  
         out_uint32_le(out, 18);  
         out_uint32_le(out, 0x13);  
         out_uint16_le(out, 5);  
         out_uint16_le(out, 1);  
         out_uint32_le(out, 6);  
         out_uint8p(out, "fnorp", 6);  
         out_uint32_le(out, 0);  
618    
619          s_mark_end(out);          if (rdesktop_is_selection_owner)
620                    {
621          sec_send_to_channel(out, encryption ? SEC_ENCRYPT : 0, 1005); // FIXME: Don't hardcode channel!                    DEBUG_CLIPBOARD(("XChangeProperty, rdesktop_is_selection_owner\n"));
622                    XChangeProperty(display, wnd, rdesktop_clipboard_target_atom,
623                                    XA_INTEGER, 32, PropModeReplace,
624                                    (unsigned char *) &wanted_formatcode, 1);
625    
626          /*                        XConvertSelection(display, primary_atom,
627          if (1 != wanted_formatcode)                                    ipc_atom, rdesktop_clipboard_target_atom, wnd, CurrentTime);
628                    return;
629            }
630    
631    
632            if (None != selectionowner)
633          {          {
634                  out =  sec_init(encryption ? SEC_ENCRYPT : 0,  
635                                  20);                  /* FIXME: Perhaps we should check if we are the owner? */
636                  out_uint32_le(s, 12);  
637                  out_uint32_le(s, 0x13);                  XConvertSelection(display, primary_atom,
638                  out_uint16_le(s, 5);                                    targets_atom, rdesktop_clipboard_target_atom, wnd, CurrentTime);
639                  out_uint16_le(s, 2);  
640                  out_uint32_le(s, 0);                  /* The rest of the transfer is handled in
641                  out_uint32_le(s, 0);                     cliprdr_handle_SelectionNotify */
642                  s_mark_end(s);  
                 sec_send_to_channel(s, encryption ? SEC_ENCRYPT : 0, 1005); // FIXME: Don't hardcode channel!  
643          }          }
644          */                        else
645            {
646    
647                    selectionowner = XGetSelectionOwner(display, clipboard_atom);
648                    if (None != selectionowner)
649                    {
650                            XConvertSelection(display, clipboard_atom,
651                                              targets_atom,
652                                              rdesktop_clipboard_target_atom, wnd, CurrentTime);
653    
654                            /* The rest of the transfer is handled in
655                               cliprdr_handle_SelectionNotify */
656    
657                    }
658                    else
659                    {
660    
661                            DEBUG_CLIPBOARD(("There were no owner for PRIMARY nor CLIPBOARD, sending empty string\n"));     // FIXME: Should we always send an empty string?
662    
663                            cliprdr_send_empty_datapacket();
664                    }
665            }
666    
667    
668  }  }
           
669    
670  void cliprdr_callback(STREAM s)  
671    void
672    cliprdr_callback(STREAM s, uint16 channelno)
673  {  {
674            static int failed_clipboard_acks = 0;
675            struct timeval timeval;
676          uint32 length, flags;          uint32 length, flags;
677          uint16 ptype0, ptype1;          uint16 ptype0, ptype1;
678          DEBUG_CLIPBOARD(("cliprdr_callback called, clipboard data:\n"));          clipboard_channelno = channelno;
679            DEBUG_CLIPBOARD(("cliprdr_callback called with channelno %d, clipboard data:\n",
680                             channelno));
681  #ifdef WITH_DEBUG_CLIPBOARD  #ifdef WITH_DEBUG_CLIPBOARD
682          hexdump(s->p, s->end - s->p);          //      hexdump(s->p, s->end - s->p);
683  #endif  #endif
684          in_uint32_le(s, length);          in_uint32_le(s, length);
685          in_uint32_le(s, flags);          in_uint32_le(s, flags);
686    
687          DEBUG_CLIPBOARD(("length is %d, flags are %d\n", length, flags));          DEBUG_CLIPBOARD(("length is %d, flags are %d\n", length, flags));
688    
689          if (flags & 0x03 || flags & 0x01) /* Single-write op or first-packet-of-several op */          if (3 == flags || 1 == flags)   /* Single-write op or first-packet-of-several op */
690          {          {
691                  in_uint16_le(s, ptype0);                  in_uint16_le(s, ptype0);
692                  in_uint16_le(s, ptype1);                  in_uint16_le(s, ptype1);
693                  DEBUG_CLIPBOARD(("ptype0 is %d, ptype1 is %d\n", ptype0, ptype1));                  DEBUG_CLIPBOARD(("ptype0 is %d, ptype1 is %d\n", ptype0, ptype1));
694                  if (1 == ptype0 && 0 == ptype1) {                  if (1 == ptype0 && 0 == ptype1)
695                    {
696                          cliprdr_handle_first_handshake(s);                          cliprdr_handle_first_handshake(s);
697                          return;                          return;
698                  } else if (3 == ptype0 && 1 == ptype1)                  }
699                    else if (3 == ptype0 && 1 == ptype1)
700                  {                  {
701                          // Acknowledgment on our format announce. Do we care? Not right now.                          // Acknowledgment on our format announce. Do we care? Not right now.
702                          // There is a strange pad in this packet that we might need some time,                          // There is a strange pad in this packet that we might need some time,
703                          // but probably not.                          // but probably not.
704                          DEBUG_CLIPBOARD(("Received format announce ACK\n"));                          DEBUG_CLIPBOARD(("Received format announce ACK\n"));
705                            failed_clipboard_acks = 0;
706                          return;                          return;
707    
708                  } else if (2 == ptype0 && 0 == ptype1)                  }
709                    else if (3 == ptype0 && 2 == ptype1)
710                    {
711                            DEBUG_CLIPBOARD(("Received failed clipboard format announce ACK, retrying\n"));
712    
713                            /* This is a fairly portable way to sleep 1/10 of
714                               a second.. */
715                            timeval.tv_sec = 0;
716                            timeval.tv_usec = 100;
717                            select(0, NULL, NULL, NULL, &timeval);
718                            if (failed_clipboard_acks < 3)
719                            {
720    
721                                    cliprdr_send_format_announce();
722                                    /* Make sure we don't get stuck in this loop */
723                                    failed_clipboard_acks++;
724                            }
725                            else
726                            {
727                                    warning("Reached maximum number of clipboard format announce attempts. Pasting in Windows probably won't work well now.\n");
728                            }
729                    }
730                    else if (2 == ptype0 && 0 == ptype1)
731                  {                  {
732                          cliprdr_register_server_formats(s);                          cliprdr_register_server_formats(s);
733                          cliprdr_select_X_clipboards();                          cliprdr_select_X_clipboards();
734                          cliprdr_ack_format_list();                          cliprdr_ack_format_list();
735                          return;                          return;
736                  } else if (5 == ptype0 && 1 == ptype1)                  }
737                    else if (5 == ptype0 && 1 == ptype1)
738                  {                  {
739                          cliprdr_handle_server_data(length, s);                          cliprdr_handle_server_data(length, flags, s);
740                  } else if (4 == ptype0 && 0 == ptype1)                  }
741                    else if (4 == ptype0 && 0 == ptype1)
742                  {                  {
743                          cliprdr_handle_server_data_request(s);                          cliprdr_handle_server_data_request(s);
744                  }                  }
745    
746                    
747            }
748            else
749            {
750                    DEBUG_CLIPBOARD(("Handling middle or last packet\n"));
751                    cliprdr_handle_server_data(length, flags, s);
752          }          }
753  }  }
754    
755    void
756    cliprdr_ipc_primary_lost(unsigned char *data, uint16 length)
757    {
758            DEBUG_CLIPBOARD(("cliprdr_ipc_primary_lost called\n"));
759            if (!have_primary)
760                    cliprdr_send_format_announce();
761            rdesktop_is_selection_owner = 0;
762    }
763    
764    
765  void cliprdr_init(void)  void
766    cliprdr_init(void)
767  {  {
768          primary_atom = XInternAtom(display, "PRIMARY", False);          primary_atom = XInternAtom(display, "PRIMARY", False);
769          clipboard_atom = XInternAtom(display, "CLIPBOARD", False);          clipboard_atom = XInternAtom(display, "CLIPBOARD", False);
770          targets_atom = XInternAtom(display, "TARGETS", True);          targets_atom = XInternAtom(display, "TARGETS", False);
771          timestamp_atom = XInternAtom(display, "TIMESTAMP", True);          timestamp_atom = XInternAtom(display, "TIMESTAMP", False);
772            rdesktop_clipboard_target_atom = XInternAtom(display, "_RDESKTOP_CLIPBOARD_TARGET", False);
773            incr_atom = XInternAtom(display, "INCR", False);
774            targets[0] = targets_atom;
775            targets[1] = XInternAtom(display, "TEXT", False);
776            targets[2] = XInternAtom(display, "UTF8_STRING", False);
777            targets[3] = XInternAtom(display, "text/unicode", False);
778            targets[4] = XInternAtom(display, "TIMESTAMP", False);
779            targets[5] = XInternAtom(display, "STRING", False);
780            ipc_register_ipcnotify(RDESKTOP_IPC_CLIPRDR_FORMAT_ANNOUNCE, cliprdr_ipc_format_announce);
781            ipc_register_ipcnotify(RDESKTOP_IPC_CLIPRDR_FORMAT_ANNOUNCE, cliprdr_ipc_primary_lost);
782    
783    
784  }  }

Legend:
Removed from v.385  
changed lines
  Added in v.422

  ViewVC Help
Powered by ViewVC 1.1.26