--- sourceforge.net/trunk/rdesktop/cliprdr.c 2003/06/06 09:28:21 390 +++ sourceforge.net/trunk/rdesktop/cliprdr.c 2003/06/06 09:31:28 394 @@ -26,14 +26,18 @@ extern Display *display; extern Window wnd; extern Time last_gesturetime; +extern Atom ipc_atom; // static Time selection_timestamp; static Atom clipboard_atom, primary_atom, targets_atom, timestamp_atom; -static Atom rdesktop_clipboard_target_atom; +static Atom rdesktop_clipboard_target_atom, incr_atom; static cliprdr_dataformat *server_formats = NULL; static uint16 num_server_formats = 0; static XSelectionEvent selection_event; static uint16 clipboard_channelno; +static Atom targets[NUM_TARGETS]; +static int have_primary = 0; +static int rdesktop_is_selection_owner = 0; static void cliprdr_print_server_formats(void) @@ -74,7 +78,7 @@ static void cliprdr_send_format_announce(void) { - DEBUG_CLIPBOARD(("Sending format announce\n")); + DEBUG_CLIPBOARD(("Sending (empty) format announce\n")); STREAM s; int number_of_formats = 1; @@ -100,6 +104,28 @@ clipboard_channelno); } +void cliprdr_ipc_format_announce(unsigned char *data, uint16 length) +{ + STREAM s; + rdesktop_is_selection_owner = 1; + DEBUG_CLIPBOARD(("cliprdr_ipc_format_announce called, length is %d, data is %s, sending native format announce\n", length, data)); + + s = sec_init(encryption ? SEC_ENCRYPT : 0, length+12+4+4); + out_uint32_le(s, length+12); + out_uint32_le(s, 0x13); + out_uint16_le(s, 2); + out_uint16_le(s, 0); + out_uint32_le(s, length); + out_uint8p(s, data, length); + out_uint32_le(s, 0); // Pad + s_mark_end(s); + + sec_send_to_channel(s, encryption ? SEC_ENCRYPT : 0, + clipboard_channelno); +} + + + static void cliprdr_send_empty_datapacket(void) @@ -125,13 +151,16 @@ cliprdr_handle_SelectionNotify(XSelectionEvent *event) { - unsigned char *data; + unsigned char *data, *datap; unsigned long nitems, bytes_left; - int res; + + unsigned long bytes_left_to_transfer; + int res, i; int format; Atom type_return; Atom best_target; + Atom *supported_targets; STREAM out; @@ -171,6 +200,13 @@ { /* FIXME: We should choose format here based on what the server wanted */ + supported_targets = (Atom *)data; + for (i=0;itime); } + else /* Other clipboard data */ { res = XGetWindowProperty(display, wnd, rdesktop_clipboard_target_atom, - 0L, 4096L, False, AnyPropertyType, + 0L, 0x1FFFFFF, + True, AnyPropertyType, &type_return, &format, &nitems, &bytes_left, &data); + + /* FIXME: We need to handle INCR as well, + this is a temporary solution. */ + + if (incr_atom == type_return) + { + warning("We don't support INCR transfers at this time. Try cutting less data\n"); + cliprdr_send_empty_datapacket(); + } + + if (Success != res) { DEBUG_CLIPBOARD(("XGetWindowProperty failed!\n")); @@ -198,24 +247,90 @@ return; } - /* We need to handle INCR as well */ + DEBUG_CLIPBOARD(("Received %d bytes of clipboard data from X, there is %d remaining\n", + nitems, bytes_left)); + DEBUG_CLIPBOARD(("type_return is %s\n", + XGetAtomName(display, type_return))); + + datap = data; - out = sec_init(encryption ? SEC_ENCRYPT : 0, - 20+nitems+1); - out_uint32_le(out, 12+nitems+1); - out_uint32_le(out, 0x13); - out_uint16_le(out, 5); - out_uint16_le(out, 1); - out_uint32_le(out, nitems+1); - out_uint8p(out, data, nitems+1); - /* Insert null string here? */ - out_uint32_le(out, 0); - s_mark_end(out); + if (nitems+1 <= MAX_CLIPRDR_STANDALONE_DATASIZE) + { + out = sec_init(encryption ? SEC_ENCRYPT : 0, + 20+nitems+1); + out_uint32_le(out, 12+nitems+1); + out_uint32_le(out, 0x13); + out_uint16_le(out, 5); + out_uint16_le(out, 1); + out_uint32_le(out, nitems+1); + out_uint8p(out, datap, nitems+1); + out_uint32_le(out, 0); + s_mark_end(out); - sec_send_to_channel(out, encryption ? SEC_ENCRYPT : 0, - clipboard_channelno); + sec_send_to_channel(out, encryption ? SEC_ENCRYPT : 0, + clipboard_channelno); - cliprdr_send_format_announce(); + } + else + { + DEBUG_CLIPBOARD(("Sending %d bytes of data\n", + 16+MAX_CLIPRDR_STANDALONE_DATASIZE)); + out = sec_init(encryption ? SEC_ENCRYPT : 0, + 16+MAX_CLIPRDR_STANDALONE_DATASIZE); + out_uint32_le(out, nitems+12); + out_uint32_le(out, 0x11); + out_uint16_le(out, 5); + out_uint16_le(out, 1); + out_uint32_le(out, nitems); + out_uint8p(out, datap, + MAX_CLIPRDR_STANDALONE_DATASIZE); + s_mark_end(out); + + sec_send_to_channel(out, encryption ? SEC_ENCRYPT : 0, + clipboard_channelno); + + bytes_left_to_transfer = nitems - MAX_CLIPRDR_STANDALONE_DATASIZE; + datap+=MAX_CLIPRDR_STANDALONE_DATASIZE; + + while (bytes_left_to_transfer > MAX_CLIPRDR_STANDALONE_DATASIZE) + { + DEBUG_CLIPBOARD(("Sending %d bytes of data\n", + 16+MAX_CLIPRDR_CONTINUATION_DATASIZE)); + out = sec_init(encryption ? SEC_ENCRYPT : 0, + 8+MAX_CLIPRDR_CONTINUATION_DATASIZE); + out_uint32_le(out, nitems); + out_uint32_le(out, 0x10); + out_uint8p(out, datap, + MAX_CLIPRDR_CONTINUATION_DATASIZE); + s_mark_end(out); + + sec_send_to_channel(out, + encryption ? SEC_ENCRYPT : 0, + clipboard_channelno); + bytes_left_to_transfer-= MAX_CLIPRDR_CONTINUATION_DATASIZE; + datap+=MAX_CLIPRDR_CONTINUATION_DATASIZE; + + } + DEBUG_CLIPBOARD(("Sending %d bytes of data\n", + 12+bytes_left_to_transfer)); + out = sec_init(encryption ? SEC_ENCRYPT : 0, + 12+bytes_left_to_transfer); + out_uint32_le(out, nitems); + out_uint32_le(out, 0x12); + out_uint8p(out, datap, + bytes_left_to_transfer); + out_uint32_le(out, 0x0); + s_mark_end(out); + + sec_send_to_channel(out, encryption ? SEC_ENCRYPT : 0, + clipboard_channelno); + + } + + + XFree(data); + if (!rdesktop_clipboard_target_atom) + cliprdr_send_format_announce(); } @@ -226,6 +341,9 @@ cliprdr_handle_SelectionClear(void) { DEBUG_CLIPBOARD(("cliprdr_handle_SelectionClear\n")); + have_primary = 0; + ipc_send_message(RDESKTOP_IPC_CLIPRDR_PRIMARY_LOST, + "", 0); cliprdr_send_format_announce(); } @@ -254,10 +372,13 @@ cliprdr_handle_SelectionRequest(XSelectionRequestEvent *xevent) { - Atom *targets; - int res; - XSelectionEvent xev; + unsigned char *data; + unsigned long nitems, bytes_left; + Atom type_return; + uint32 *wanted_formatcode; + int format; + DEBUG_CLIPBOARD(("cliprdr_handle_SelectionRequest\n")); DEBUG_CLIPBOARD(("Requestor window id 0x%x ", (unsigned)xevent->requestor)); @@ -282,50 +403,61 @@ xev.property = xevent->property; xev.time = xevent->time; - if (targets_atom == xevent->target) + memcpy(&selection_event, &xev, sizeof(xev)); + + if (ipc_atom == xevent->target) + { + DEBUG_CLIPBOARD(("Target atom is ipc_atom, getting INTEGER from requestor\n")); + XGetWindowProperty(display, xevent->requestor, + rdesktop_clipboard_target_atom, + 0, + 1, + True, XA_INTEGER, + &type_return, + &format, + &nitems, + &bytes_left, + &wanted_formatcode); + DEBUG_CLIPBOARD(("Got wanted formatcode %d, format is %d\n", *wanted_formatcode, format)); + cliprdr_request_clipboard_data(*wanted_formatcode); + } + + else if (targets_atom == xevent->target) { DEBUG_CLIPBOARD(("TARGETS requested, sending list..\n")); - targets = xmalloc(4*sizeof(Atom)); - targets[0] = xevent->target; - targets[1] = XInternAtom(display, "TEXT", True); - targets[2] = XInternAtom(display, "UTF8_STRING", True); - targets[3] = XInternAtom(display, "TIMESTAMP", True); - res = XChangeProperty(display, - xevent->requestor, - xevent->property, - XA_ATOM, - 32, - PropModeAppend, - (unsigned char *)targets, - 3); - - res = XSendEvent(display, - xevent->requestor, - False, - NoEventMask, - (XEvent *)&xev); + XChangeProperty(display, + xevent->requestor, + xevent->property, + XA_ATOM, + 32, + PropModeAppend, + (unsigned char *)&targets, + NUM_TARGETS); + + XSendEvent(display, + xevent->requestor, + False, + NoEventMask, + (XEvent *)&xev); return; } else if (timestamp_atom == xevent->target) { - DEBUG_CLIPBOARD(("TIMESTAMP requested... sending 0x%x\n", - (unsigned)last_gesturetime)); - res = XChangeProperty(display, - xevent->requestor, - xevent->property, - XA_INTEGER, - 32, - PropModeAppend, - (unsigned char *)&last_gesturetime, - 1); - res = XSendEvent(display, - xevent->requestor, - False, - NoEventMask, - (XEvent *)&xev); + XChangeProperty(display, + xevent->requestor, + xevent->property, + XA_INTEGER, + 32, + PropModeAppend, + (unsigned char *)&last_gesturetime, + 1); + XSendEvent(display, + xevent->requestor, + False, + NoEventMask, + (XEvent *)&xev); } else /* Some other target */ { cliprdr_request_clipboard_data(CF_TEXT); - memcpy(&selection_event, &xev, sizeof(xev)); /* Return and wait for data, handled by cliprdr_handle_server_data */ } @@ -361,10 +493,14 @@ uint16 num_formats; cliprdr_dataformat *this, *next; - DEBUG_CLIPBOARD(("cliprdr_register_server_formats\n")); in_uint32_le(s, remaining_length); + DEBUG_CLIPBOARD(("cliprdr_register_server_formats, remaining_length is %d\n", remaining_length)); + num_formats = remaining_length / 36; + + ipc_send_message(RDESKTOP_IPC_CLIPRDR_FORMAT_ANNOUNCE, + s->p, remaining_length); if (NULL != server_formats) { this = server_formats; next = this->next; @@ -403,6 +539,10 @@ if (wnd != XGetSelectionOwner(display, primary_atom)) { warning("Failed to aquire ownership of PRIMARY clipboard\n"); + } + else + { + have_primary = 1; } XSetSelectionOwner(display, clipboard_atom, wnd, last_gesturetime); if (wnd != XGetSelectionOwner(display, clipboard_atom)) @@ -412,8 +552,6 @@ } - - static void @@ -427,12 +565,46 @@ cliprdr_send_format_announce(); } -void cliprdr_handle_server_data(uint32 length, STREAM s) +void cliprdr_handle_server_data(uint32 length, uint32 flags, STREAM s) { - uint32 remaining_length; - char *data; - in_uint32_le(s, remaining_length); - data = s->p; + static uint32 remaining_length; + static char *data, *datap; + static uint32 bytes_left_to_read; + DEBUG_CLIPBOARD(("In cliprdr_handle_server_data, flags is %d\n", + flags)); + if (3 == flags) /* One-op write, no packets follows */ + { + in_uint32_le(s, remaining_length); + data = s->p; + } else if (1 == flags) /* First of several packets */ + { + in_uint32_le(s, remaining_length); + DEBUG_CLIPBOARD(("Remaining length is %d\n", + remaining_length)); + data = xmalloc(remaining_length); + datap = data; + DEBUG_CLIPBOARD(("Copying first %d bytes\n", + MAX_CLIPRDR_STANDALONE_DATASIZE)); + memcpy(datap, s->p, MAX_CLIPRDR_STANDALONE_DATASIZE); + + datap+=MAX_CLIPRDR_STANDALONE_DATASIZE; + bytes_left_to_read = remaining_length-MAX_CLIPRDR_STANDALONE_DATASIZE; + return; + } else if (0 == flags) + { + DEBUG_CLIPBOARD(("Copying %d middle bytes", + MAX_CLIPRDR_CONTINUATION_DATASIZE)); + memcpy(datap, s->p, MAX_CLIPRDR_CONTINUATION_DATASIZE); + + datap+=MAX_CLIPRDR_CONTINUATION_DATASIZE; + bytes_left_to_read-=MAX_CLIPRDR_CONTINUATION_DATASIZE; + return; + } else if (2 == flags) + { + DEBUG_CLIPBOARD(("Copying last %d bytes\n", + bytes_left_to_read)); + memcpy(datap, s->p, bytes_left_to_read); + } XChangeProperty(display, selection_event.requestor, selection_event.property, @@ -440,14 +612,16 @@ 8, PropModeAppend, data, - remaining_length); + remaining_length-1); XSendEvent(display, selection_event.requestor, False, NoEventMask, (XEvent *)&selection_event); - + + if (2 == flags) + xfree(data); } @@ -468,6 +642,20 @@ selectionowner = XGetSelectionOwner(display, primary_atom); + if (rdesktop_is_selection_owner) + { + XChangeProperty(display, wnd, rdesktop_clipboard_target_atom, + XA_INTEGER, 32, PropModeReplace, + (unsigned char *)&wanted_formatcode, 1); + + XConvertSelection(display, primary_atom, + ipc_atom, + rdesktop_clipboard_target_atom, + wnd, CurrentTime); + return; + } + + if (None != selectionowner) { @@ -501,14 +689,14 @@ clipboard_channelno = channelno; DEBUG_CLIPBOARD(("cliprdr_callback called with channelno %d, clipboard data:\n", channelno)); #ifdef WITH_DEBUG_CLIPBOARD - hexdump(s->p, s->end - s->p); + // hexdump(s->p, s->end - s->p); #endif in_uint32_le(s, length); in_uint32_le(s, flags); DEBUG_CLIPBOARD(("length is %d, flags are %d\n", length, flags)); - 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 */ { in_uint16_le(s, ptype0); in_uint16_le(s, ptype1); @@ -553,16 +741,29 @@ return; } else if (5 == ptype0 && 1 == ptype1) { - cliprdr_handle_server_data(length, s); + cliprdr_handle_server_data(length, flags, s); } else if (4 == ptype0 && 0 == ptype1) { cliprdr_handle_server_data_request(s); } + } + else + { + DEBUG_CLIPBOARD(("Handling middle or last packet\n")); + cliprdr_handle_server_data(length, flags, s); } } +void cliprdr_ipc_primary_lost(unsigned char *data, uint16 length) +{ + DEBUG_CLIPBOARD(("cliprdr_ipc_primary_lost called\n")); + if (!have_primary) + cliprdr_send_format_announce(); + rdesktop_is_selection_owner = 0; +} + void cliprdr_init(void) { @@ -571,4 +772,17 @@ targets_atom = XInternAtom(display, "TARGETS", False); timestamp_atom = XInternAtom(display, "TIMESTAMP", False); rdesktop_clipboard_target_atom = XInternAtom(display, "_RDESKTOP_CLIPBOARD_TARGET", False); + incr_atom = XInternAtom(display, "INCR", False); + targets[0] = targets_atom; + targets[1] = XInternAtom(display, "TEXT", False); + targets[2] = XInternAtom(display, "UTF8_STRING", False); + targets[3] = XInternAtom(display, "text/unicode", False); + targets[4] = XInternAtom(display, "TIMESTAMP", False); + targets[5] = XInternAtom(display, "STRING", False); + ipc_register_ipcnotify(RDESKTOP_IPC_CLIPRDR_FORMAT_ANNOUNCE, + cliprdr_ipc_format_announce); + ipc_register_ipcnotify(RDESKTOP_IPC_CLIPRDR_FORMAT_ANNOUNCE, + cliprdr_ipc_primary_lost); + + }