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

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

  ViewVC Help
Powered by ViewVC 1.1.26