From 8f1adf64eee1112e2c0942496d7b39841bf1434c Mon Sep 17 00:00:00 2001 From: Armin Novak Date: Thu, 6 Oct 2016 13:31:25 +0200 Subject: [PATCH] Refactored ClipboardSetData. --- client/Android/android_cliprdr.c | 9 +- client/Android/android_event.c | 125 +++---- client/Mac/Clipboard.m | 11 +- client/Mac/MRDPView.m | 9 +- client/X11/xf_cliprdr.c | 415 ++++++++++----------- winpr/libwinpr/clipboard/clipboard.c | 99 +++-- .../libwinpr/clipboard/test/TestClipboardFormats.c | 9 +- 7 files changed, 305 insertions(+), 372 deletions(-) diff --git a/client/Android/android_cliprdr.c b/client/Android/android_cliprdr.c index c6323ca..b3ebdcb 100644 --- a/client/Android/android_cliprdr.c +++ b/client/Android/android_cliprdr.c @@ -420,14 +420,9 @@ static UINT android_cliprdr_server_format_data_response( formatId = format->formatId; size = formatDataResponse->dataLen; - data = (BYTE*) malloc(size); - if (!data) - return ERROR_INTERNAL_ERROR; - - CopyMemory(data, formatDataResponse->requestedFormatData, size); - - if (!ClipboardSetData(afc->clipboard, formatId, data, size)) + if (!ClipboardSetData(afc->clipboard, formatId, + formatDataResponse->requestedFormatData, size)) return ERROR_INTERNAL_ERROR; SetEvent(afc->clipboardRequestEvent); diff --git a/client/Android/android_event.c b/client/Android/android_event.c index a81deab..83fa591 100644 --- a/client/Android/android_event.c +++ b/client/Android/android_event.c @@ -28,30 +28,30 @@ #include "android_freerdp.h" #include "android_cliprdr.h" -BOOL android_push_event(freerdp * inst, ANDROID_EVENT* event) +BOOL android_push_event(freerdp* inst, ANDROID_EVENT* event) { - androidContext* aCtx = (androidContext*)inst->context; + if (aCtx->event_queue->count >= aCtx->event_queue->size) { int new_size; void* new_events; - new_size = aCtx->event_queue->size * 2; new_events = realloc((void*) aCtx->event_queue->events, - sizeof(ANDROID_EVENT*) * new_size); + sizeof(ANDROID_EVENT*) * new_size); + if (!new_events) return FALSE; + aCtx->event_queue->events = new_events; aCtx->event_queue->size = new_size; } aCtx->event_queue->events[(aCtx->event_queue->count)++] = event; - return SetEvent(aCtx->event_queue->isSet); } -static ANDROID_EVENT* android_peek_event(ANDROID_EVENT_QUEUE * queue) +static ANDROID_EVENT* android_peek_event(ANDROID_EVENT_QUEUE* queue) { ANDROID_EVENT* event; @@ -59,11 +59,10 @@ static ANDROID_EVENT* android_peek_event(ANDROID_EVENT_QUEUE * queue) return NULL; event = queue->events[0]; - return event; } -static ANDROID_EVENT* android_pop_event(ANDROID_EVENT_QUEUE * queue) +static ANDROID_EVENT* android_pop_event(ANDROID_EVENT_QUEUE* queue) { int i; ANDROID_EVENT* event; @@ -101,44 +100,31 @@ static BOOL android_process_event(ANDROID_EVENT_QUEUE* queue, freerdp* inst) else if (event->type == EVENT_TYPE_KEY_UNICODE) { ANDROID_EVENT_KEY* key_event = (ANDROID_EVENT_KEY*) event; - inst->input->UnicodeKeyboardEvent(inst->input, key_event->flags, key_event->scancode); + inst->input->UnicodeKeyboardEvent(inst->input, key_event->flags, + key_event->scancode); android_event_free((ANDROID_EVENT*)key_event); } else if (event->type == EVENT_TYPE_CURSOR) { ANDROID_EVENT_CURSOR* cursor_event = (ANDROID_EVENT_CURSOR*) event; - inst->input->MouseEvent(inst->input, cursor_event->flags, cursor_event->x, cursor_event->y); + inst->input->MouseEvent(inst->input, cursor_event->flags, cursor_event->x, + cursor_event->y); android_event_free((ANDROID_EVENT*)cursor_event); } else if (event->type == EVENT_TYPE_CLIPBOARD) { - BYTE* data; UINT32 size; UINT32 formatId; ANDROID_EVENT_CLIPBOARD* clipboard_event = (ANDROID_EVENT_CLIPBOARD*) event; - formatId = ClipboardRegisterFormat(afc->clipboard, "UTF8_STRING"); - size = clipboard_event->data_length; if (size) - { - data = (BYTE*) malloc(size); - - if (!data) - return -1; - - CopyMemory(data, clipboard_event->data, size); - - ClipboardSetData(afc->clipboard, formatId, (void*) data, size); - } + ClipboardSetData(afc->clipboard, formatId, clipboard_event->data, size); else - { ClipboardEmpty(afc->clipboard); - } android_cliprdr_send_client_format_list(afc->cliprdr); - android_event_free((ANDROID_EVENT*)clipboard_event); } else if (event->type == EVENT_TYPE_DISCONNECT) @@ -151,7 +137,7 @@ static BOOL android_process_event(ANDROID_EVENT_QUEUE* queue, freerdp* inst) return TRUE; } -HANDLE android_get_handle(freerdp *inst) +HANDLE android_get_handle(freerdp* inst) { androidContext* aCtx; @@ -159,13 +145,14 @@ HANDLE android_get_handle(freerdp *inst) return NULL; aCtx = (androidContext*)inst->context; + if (!aCtx->event_queue || !aCtx->event_queue->isSet) return NULL; return aCtx->event_queue->isSet; } -BOOL android_check_handle(freerdp * inst) +BOOL android_check_handle(freerdp* inst) { androidContext* aCtx; @@ -173,6 +160,7 @@ BOOL android_check_handle(freerdp * inst) return FALSE; aCtx = (androidContext*)inst->context; + if (!aCtx->event_queue || !aCtx->event_queue->isSet) return FALSE; @@ -180,7 +168,8 @@ BOOL android_check_handle(freerdp * inst) { if (!ResetEvent(aCtx->event_queue->isSet)) return FALSE; - if(!android_process_event(aCtx->event_queue, inst)) + + if (!android_process_event(aCtx->event_queue, inst)) return FALSE; } @@ -190,15 +179,14 @@ BOOL android_check_handle(freerdp * inst) ANDROID_EVENT_KEY* android_event_key_new(int flags, UINT16 scancode) { ANDROID_EVENT_KEY* event; - event = (ANDROID_EVENT_KEY*) calloc(1, sizeof(ANDROID_EVENT_KEY)); + if (!event) return NULL; event->type = EVENT_TYPE_KEY; event->flags = flags; event->scancode = scancode; - return event; } @@ -210,14 +198,13 @@ static void android_event_key_free(ANDROID_EVENT_KEY* event) ANDROID_EVENT_KEY* android_event_unicodekey_new(UINT16 key) { ANDROID_EVENT_KEY* event; - event = (ANDROID_EVENT_KEY*) calloc(1, sizeof(ANDROID_EVENT_KEY)); + if (!event) return NULL; event->type = EVENT_TYPE_KEY_UNICODE; event->scancode = key; - return event; } @@ -229,8 +216,8 @@ static void android_event_unicodekey_free(ANDROID_EVENT_KEY* event) ANDROID_EVENT_CURSOR* android_event_cursor_new(UINT16 flags, UINT16 x, UINT16 y) { ANDROID_EVENT_CURSOR* event; - event = (ANDROID_EVENT_CURSOR*) calloc(1, sizeof(ANDROID_EVENT_CURSOR)); + if (!event) return NULL; @@ -238,7 +225,6 @@ ANDROID_EVENT_CURSOR* android_event_cursor_new(UINT16 flags, UINT16 x, UINT16 y) event->x = x; event->y = y; event->flags = flags; - return event; } @@ -250,8 +236,8 @@ static void android_event_cursor_free(ANDROID_EVENT_CURSOR* event) ANDROID_EVENT* android_event_disconnect_new() { ANDROID_EVENT* event; - event = (ANDROID_EVENT*) calloc(1, sizeof(ANDROID_EVENT)); + if (!event) return NULL; @@ -264,23 +250,27 @@ static void android_event_disconnect_free(ANDROID_EVENT* event) free(event); } -ANDROID_EVENT_CLIPBOARD* android_event_clipboard_new(void* data, int data_length) +ANDROID_EVENT_CLIPBOARD* android_event_clipboard_new(void* data, + int data_length) { ANDROID_EVENT_CLIPBOARD* event; - event = (ANDROID_EVENT_CLIPBOARD*) calloc(1, sizeof(ANDROID_EVENT_CLIPBOARD)); + if (!event) return NULL; event->type = EVENT_TYPE_CLIPBOARD; + if (data) { event->data = malloc(data_length); + if (!event->data) { free(event); return NULL; } + memcpy(event->data, data, data_length); event->data_length = data_length; } @@ -297,12 +287,12 @@ static void android_event_clipboard_free(ANDROID_EVENT_CLIPBOARD* event) } } -BOOL android_event_queue_init(freerdp * inst) +BOOL android_event_queue_init(freerdp* inst) { androidContext* aCtx = (androidContext*)inst->context; ANDROID_EVENT_QUEUE* queue; - queue = (ANDROID_EVENT_QUEUE*) calloc(1, sizeof(ANDROID_EVENT_QUEUE)); + if (!queue) { WLog_ERR(TAG, "android_event_queue_init: memory allocation failed"); @@ -311,15 +301,16 @@ BOOL android_event_queue_init(freerdp * inst) queue->size = 16; queue->count = 0; - queue->isSet = CreateEventA(NULL, TRUE, FALSE, NULL); + if (!queue->isSet) { - free (queue); + free(queue); return FALSE; } queue->events = (ANDROID_EVENT**) calloc(sizeof(ANDROID_EVENT*), queue->size); + if (!queue->events) { WLog_ERR(TAG, "android_event_queue_init: memory allocation failed"); @@ -327,12 +318,12 @@ BOOL android_event_queue_init(freerdp * inst) free(queue); return FALSE; } - aCtx->event_queue = queue; + aCtx->event_queue = queue; return TRUE; } -void android_event_queue_uninit(freerdp * inst) +void android_event_queue_uninit(freerdp* inst) { androidContext* aCtx; ANDROID_EVENT_QUEUE* queue; @@ -350,13 +341,15 @@ void android_event_queue_uninit(freerdp * inst) CloseHandle(queue->isSet); queue->isSet = NULL; } + if (queue->events) { - free (queue->events); + free(queue->events); queue->events = NULL; queue->size = 0; queue->count = 0; } + free(queue); } } @@ -366,23 +359,29 @@ void android_event_free(ANDROID_EVENT* event) if (!event) return; - switch(event->type) { - case EVENT_TYPE_KEY: - android_event_key_free((ANDROID_EVENT_KEY*)event); - break; - case EVENT_TYPE_KEY_UNICODE: - android_event_unicodekey_free((ANDROID_EVENT_KEY*)event); - break; - case EVENT_TYPE_CURSOR: - android_event_cursor_free((ANDROID_EVENT_CURSOR*)event); - break; - case EVENT_TYPE_DISCONNECT: - android_event_disconnect_free((ANDROID_EVENT*)event); - break; - case EVENT_TYPE_CLIPBOARD: - android_event_clipboard_free((ANDROID_EVENT_CLIPBOARD*)event); - break; - default: - break; + switch (event->type) + { + case EVENT_TYPE_KEY: + android_event_key_free((ANDROID_EVENT_KEY*)event); + break; + + case EVENT_TYPE_KEY_UNICODE: + android_event_unicodekey_free((ANDROID_EVENT_KEY*)event); + break; + + case EVENT_TYPE_CURSOR: + android_event_cursor_free((ANDROID_EVENT_CURSOR*)event); + break; + + case EVENT_TYPE_DISCONNECT: + android_event_disconnect_free((ANDROID_EVENT*)event); + break; + + case EVENT_TYPE_CLIPBOARD: + android_event_clipboard_free((ANDROID_EVENT_CLIPBOARD*)event); + break; + + default: + break; } } diff --git a/client/Mac/Clipboard.m b/client/Mac/Clipboard.m index 57b7b49..0d68965 100644 --- a/client/Mac/Clipboard.m +++ b/client/Mac/Clipboard.m @@ -338,17 +338,8 @@ UINT mac_cliprdr_server_format_data_response(CliprdrClientContext* cliprdr, CLIP formatId = format->formatId; size = formatDataResponse->dataLen; - data = (BYTE*) malloc(size); - if (!data) - { - SetEvent(mfc->clipboardRequestEvent); - return CHANNEL_RC_NO_MEMORY; - } - - CopyMemory(data, formatDataResponse->requestedFormatData, size); - - ClipboardSetData(mfc->clipboard, formatId, data, size); + ClipboardSetData(mfc->clipboard, formatId, formatDataResponse->requestedFormatData, size); SetEvent(mfc->clipboardRequestEvent); diff --git a/client/Mac/MRDPView.m b/client/Mac/MRDPView.m index 76e9f82..ee6bc2d 100644 --- a/client/Mac/MRDPView.m +++ b/client/Mac/MRDPView.m @@ -771,12 +771,9 @@ DWORD fixKeyCode(DWORD keyCode, unichar keyChar, enum APPLE_KEYBOARD_TYPE type) formatData = [item dataForType:type]; formatId = ClipboardRegisterFormat(mfc->clipboard, "UTF8_STRING"); /* length does not include null terminator */ - size = (UINT32) [formatData length]; - data = (BYTE*) malloc(size + 1); - [formatData getBytes:data length:size]; - data[size] = '\0'; - size++; - ClipboardSetData(mfc->clipboard, formatId, (void*) data, size); + size = (UINT32) [formatData length] + 1; + data = [formatData bytes]; + ClipboardSetData(mfc->clipboard, formatId, data, size); formatMatch = TRUE; break; } diff --git a/client/X11/xf_cliprdr.c b/client/X11/xf_cliprdr.c index 569f448..80b54c5 100644 --- a/client/X11/xf_cliprdr.c +++ b/client/X11/xf_cliprdr.c @@ -124,17 +124,17 @@ static void xf_cliprdr_check_owner(xfClipboard* clipboard) static BOOL xf_cliprdr_is_self_owned(xfClipboard* clipboard) { xfContext* xfc = clipboard->xfc; - - return XGetSelectionOwner(xfc->display, clipboard->clipboard_atom) == xfc->drawable; + return XGetSelectionOwner(xfc->display, + clipboard->clipboard_atom) == xfc->drawable; } -static void xf_cliprdr_set_raw_transfer_enabled(xfClipboard* clipboard, BOOL enabled) +static void xf_cliprdr_set_raw_transfer_enabled(xfClipboard* clipboard, + BOOL enabled) { UINT32 data = enabled; xfContext* xfc = clipboard->xfc; - XChangeProperty(xfc->display, xfc->drawable, clipboard->raw_transfer_atom, - XA_INTEGER, 32, PropModeReplace, (BYTE*) &data, 1); + XA_INTEGER, 32, PropModeReplace, (BYTE*) &data, 1); } static BOOL xf_cliprdr_is_raw_transfer_available(xfClipboard* clipboard) @@ -148,14 +148,13 @@ static BOOL xf_cliprdr_is_raw_transfer_available(xfClipboard* clipboard) UINT32 is_enabled = 0; Window owner = None; xfContext* xfc = clipboard->xfc; - owner = XGetSelectionOwner(xfc->display, clipboard->clipboard_atom); if (owner != None) { result = XGetWindowProperty(xfc->display, owner, - clipboard->raw_transfer_atom, 0, 4, 0, XA_INTEGER, - &type, &format, &length, &bytes_left, (BYTE**) &data); + clipboard->raw_transfer_atom, 0, 4, 0, XA_INTEGER, + &type, &format, &length, &bytes_left, (BYTE**) &data); } if (data) @@ -173,12 +172,14 @@ static BOOL xf_cliprdr_is_raw_transfer_available(xfClipboard* clipboard) return is_enabled ? TRUE : FALSE; } -static BOOL xf_cliprdr_formats_equal(const CLIPRDR_FORMAT* server, const xfCliprdrFormat* client) +static BOOL xf_cliprdr_formats_equal(const CLIPRDR_FORMAT* server, + const xfCliprdrFormat* client) { if (server->formatName && client->formatName) { /* The server may be using short format names while we store them in full form. */ - return (0 == strncmp(server->formatName, client->formatName, strlen(server->formatName))); + return (0 == strncmp(server->formatName, client->formatName, + strlen(server->formatName))); } if (!server->formatName && !client->formatName) @@ -189,7 +190,8 @@ static BOOL xf_cliprdr_formats_equal(const CLIPRDR_FORMAT* server, const xfClipr return FALSE; } -static xfCliprdrFormat* xf_cliprdr_get_client_format_by_id(xfClipboard* clipboard, UINT32 formatId) +static xfCliprdrFormat* xf_cliprdr_get_client_format_by_id( + xfClipboard* clipboard, UINT32 formatId) { int index; xfCliprdrFormat* format; @@ -205,7 +207,8 @@ static xfCliprdrFormat* xf_cliprdr_get_client_format_by_id(xfClipboard* clipboar return NULL; } -static xfCliprdrFormat* xf_cliprdr_get_client_format_by_atom(xfClipboard* clipboard, Atom atom) +static xfCliprdrFormat* xf_cliprdr_get_client_format_by_atom( + xfClipboard* clipboard, Atom atom) { int i; xfCliprdrFormat* format; @@ -221,7 +224,8 @@ static xfCliprdrFormat* xf_cliprdr_get_client_format_by_atom(xfClipboard* clipbo return NULL; } -static CLIPRDR_FORMAT* xf_cliprdr_get_server_format_by_atom(xfClipboard* clipboard, Atom atom) +static CLIPRDR_FORMAT* xf_cliprdr_get_server_format_by_atom( + xfClipboard* clipboard, Atom atom) { int i, j; xfCliprdrFormat* client_format; @@ -251,15 +255,14 @@ static CLIPRDR_FORMAT* xf_cliprdr_get_server_format_by_atom(xfClipboard* clipboa * * @return 0 on success, otherwise a Win32 error code */ -static UINT xf_cliprdr_send_data_request(xfClipboard* clipboard, UINT32 formatId) +static UINT xf_cliprdr_send_data_request(xfClipboard* clipboard, + UINT32 formatId) { CLIPRDR_FORMAT_DATA_REQUEST request; - ZeroMemory(&request, sizeof(CLIPRDR_FORMAT_DATA_REQUEST)); - request.requestedFormatId = formatId; - - return clipboard->context->ClientFormatDataRequest(clipboard->context, &request); + return clipboard->context->ClientFormatDataRequest(clipboard->context, + &request); } /** @@ -267,17 +270,16 @@ static UINT xf_cliprdr_send_data_request(xfClipboard* clipboard, UINT32 formatId * * @return 0 on success, otherwise a Win32 error code */ -static UINT xf_cliprdr_send_data_response(xfClipboard* clipboard, BYTE* data, int size) +static UINT xf_cliprdr_send_data_response(xfClipboard* clipboard, BYTE* data, + int size) { CLIPRDR_FORMAT_DATA_RESPONSE response; - ZeroMemory(&response, sizeof(CLIPRDR_FORMAT_DATA_RESPONSE)); - response.msgFlags = (data) ? CB_RESPONSE_OK : CB_RESPONSE_FAIL; response.dataLen = size; response.requestedFormatData = data; - - return clipboard->context->ClientFormatDataResponse(clipboard->context, &response); + return clipboard->context->ClientFormatDataResponse(clipboard->context, + &response); } static wStream* xf_cliprdr_serialize_server_format_list(xfClipboard* clipboard) @@ -294,8 +296,8 @@ static wStream* xf_cliprdr_serialize_server_format_list(xfClipboard* clipboard) } /* If present, the last format is always synthetic CF_RAW. Do not include it. */ - formatCount = (clipboard->numServerFormats > 0) ? clipboard->numServerFormats - 1 : 0; - + formatCount = (clipboard->numServerFormats > 0) ? clipboard->numServerFormats - + 1 : 0; Stream_Write_UINT32(s, formatCount); for (i = 0; i < formatCount; i++) @@ -315,15 +317,14 @@ static wStream* xf_cliprdr_serialize_server_format_list(xfClipboard* clipboard) } Stream_SealLength(s); - return s; - error: Stream_Free(s, TRUE); return NULL; } -static CLIPRDR_FORMAT* xf_cliprdr_parse_server_format_list(BYTE* data, size_t length, UINT32* numFormats) +static CLIPRDR_FORMAT* xf_cliprdr_parse_server_format_list(BYTE* data, + size_t length, UINT32* numFormats) { UINT32 i; wStream* s = NULL; @@ -367,13 +368,13 @@ static CLIPRDR_FORMAT* xf_cliprdr_parse_server_format_list(BYTE* data, size_t le } Stream_Read_UINT32(s, formats[i].formatId); - formatName = (const char*) Stream_Pointer(s); formatNameLength = strnlen(formatName, Stream_GetRemainingLength(s)); if (formatNameLength == Stream_GetRemainingLength(s)) { - WLog_ERR(TAG, "missing terminating null byte, %zu bytes left to read", formatNameLength); + WLog_ERR(TAG, "missing terminating null byte, %zu bytes left to read", + formatNameLength); goto error; } @@ -382,9 +383,7 @@ static CLIPRDR_FORMAT* xf_cliprdr_parse_server_format_list(BYTE* data, size_t le } Stream_Free(s, FALSE); - return formats; - error: Stream_Free(s, FALSE); free(formats); @@ -404,7 +403,8 @@ static void xf_cliprdr_free_formats(CLIPRDR_FORMAT* formats, UINT32 numFormats) free(formats); } -static CLIPRDR_FORMAT* xf_cliprdr_get_raw_server_formats(xfClipboard* clipboard, UINT32* numFormats) +static CLIPRDR_FORMAT* xf_cliprdr_get_raw_server_formats(xfClipboard* clipboard, + UINT32* numFormats) { Atom type = None; int format = 0; @@ -413,21 +413,22 @@ static CLIPRDR_FORMAT* xf_cliprdr_get_raw_server_formats(xfClipboard* clipboard, BYTE* data = NULL; CLIPRDR_FORMAT* formats = NULL; xfContext* xfc = clipboard->xfc; - *numFormats = 0; + XGetWindowProperty(xfc->display, clipboard->owner, + clipboard->raw_format_list_atom, + 0, 4096, False, clipboard->raw_format_list_atom, &type, &format, + &length, &remaining, &data); - XGetWindowProperty(xfc->display, clipboard->owner, clipboard->raw_format_list_atom, - 0, 4096, False, clipboard->raw_format_list_atom, &type, &format, - &length, &remaining, &data); - - if (data && length > 0 && format == 8 && type == clipboard->raw_format_list_atom) + if (data && length > 0 && format == 8 + && type == clipboard->raw_format_list_atom) { formats = xf_cliprdr_parse_server_format_list(data, length, numFormats); } else { - WLog_ERR(TAG, "failed to retrieve raw format list: data=%p, length=%lu, format=%d, type=%d (expected=%d)", - data, length, format, type, clipboard->raw_format_list_atom); + WLog_ERR(TAG, + "failed to retrieve raw format list: data=%p, length=%lu, format=%d, type=%d (expected=%d)", + data, length, format, type, clipboard->raw_format_list_atom); } if (data) @@ -436,7 +437,8 @@ static CLIPRDR_FORMAT* xf_cliprdr_get_raw_server_formats(xfClipboard* clipboard, return formats; } -static CLIPRDR_FORMAT* xf_cliprdr_get_formats_from_targets(xfClipboard* clipboard, UINT32* numFormats) +static CLIPRDR_FORMAT* xf_cliprdr_get_formats_from_targets( + xfClipboard* clipboard, UINT32* numFormats) { int i; Atom atom; @@ -447,11 +449,9 @@ static CLIPRDR_FORMAT* xf_cliprdr_get_formats_from_targets(xfClipboard* clipboar xfCliprdrFormat* format = NULL; CLIPRDR_FORMAT* formats = NULL; xfContext* xfc = clipboard->xfc; - *numFormats = 0; - XGetWindowProperty(xfc->display, xfc->drawable, clipboard->property_atom, - 0, 200, 0, XA_ATOM, &atom, &format_property, &length, &bytes_left, &data); + 0, 200, 0, XA_ATOM, &atom, &format_property, &length, &bytes_left, &data); if (length > 0) { @@ -460,6 +460,7 @@ static CLIPRDR_FORMAT* xf_cliprdr_get_formats_from_targets(xfClipboard* clipboar WLog_ERR(TAG, "XGetWindowProperty set length = %d but data is NULL", length); goto out; } + if (!(formats = (CLIPRDR_FORMAT*) calloc(length, sizeof(CLIPRDR_FORMAT)))) { WLog_ERR(TAG, "failed to allocate %d CLIPRDR_FORMAT structs", length); @@ -470,7 +471,6 @@ static CLIPRDR_FORMAT* xf_cliprdr_get_formats_from_targets(xfClipboard* clipboar for (i = 0; i < length; i++) { atom = ((Atom*) data)[i]; - format = xf_cliprdr_get_client_format_by_atom(clipboard, atom); if (format) @@ -482,16 +482,17 @@ static CLIPRDR_FORMAT* xf_cliprdr_get_formats_from_targets(xfClipboard* clipboar } out: + if (data) XFree(data); return formats; } -static CLIPRDR_FORMAT* xf_cliprdr_get_client_formats(xfClipboard* clipboard, UINT32* numFormats) +static CLIPRDR_FORMAT* xf_cliprdr_get_client_formats(xfClipboard* clipboard, + UINT32* numFormats) { CLIPRDR_FORMAT* formats = NULL; - *numFormats = 0; if (xf_cliprdr_is_raw_transfer_available(clipboard)) @@ -502,7 +503,6 @@ static CLIPRDR_FORMAT* xf_cliprdr_get_client_formats(xfClipboard* clipboard, UIN if (*numFormats == 0) { xf_cliprdr_free_formats(formats, *numFormats); - formats = xf_cliprdr_get_formats_from_targets(clipboard, numFormats); } @@ -513,14 +513,13 @@ static void xf_cliprdr_provide_server_format_list(xfClipboard* clipboard) { wStream* formats = NULL; xfContext* xfc = clipboard->xfc; - formats = xf_cliprdr_serialize_server_format_list(clipboard); if (formats) { XChangeProperty(xfc->display, xfc->drawable, clipboard->raw_format_list_atom, - clipboard->raw_format_list_atom, 8, PropModeReplace, - Stream_Buffer(formats), Stream_Length(formats)); + clipboard->raw_format_list_atom, 8, PropModeReplace, + Stream_Buffer(formats), Stream_Length(formats)); } else { @@ -535,35 +534,31 @@ static void xf_cliprdr_get_requested_targets(xfClipboard* clipboard) UINT32 numFormats = 0; CLIPRDR_FORMAT* formats = NULL; CLIPRDR_FORMAT_LIST formatList; - formats = xf_cliprdr_get_client_formats(clipboard, &numFormats); - ZeroMemory(&formatList, sizeof(CLIPRDR_FORMAT_LIST)); - formatList.msgFlags = CB_RESPONSE_OK; formatList.numFormats = numFormats; formatList.formats = formats; - clipboard->context->ClientFormatList(clipboard->context, &formatList); - xf_cliprdr_free_formats(formats, numFormats); } -static void xf_cliprdr_process_requested_data(xfClipboard* clipboard, BOOL hasData, BYTE* data, int size) +static void xf_cliprdr_process_requested_data(xfClipboard* clipboard, + BOOL hasData, BYTE* data, int size) { BOOL bSuccess; UINT32 SrcSize; UINT32 DstSize; UINT32 srcFormatId; UINT32 dstFormatId; - BYTE* pSrcData = NULL; BYTE* pDstData = NULL; xfCliprdrFormat* format; if (clipboard->incr_starts && hasData) return; - format = xf_cliprdr_get_client_format_by_id(clipboard, clipboard->requestedFormatId); + format = xf_cliprdr_get_client_format_by_id(clipboard, + clipboard->requestedFormatId); if (!hasData || !data || !format) { @@ -572,7 +567,6 @@ static void xf_cliprdr_process_requested_data(xfClipboard* clipboard, BOOL hasDa } srcFormatId = 0; - dstFormatId = 0; switch (format->formatId) { @@ -598,26 +592,12 @@ static void xf_cliprdr_process_requested_data(xfClipboard* clipboard, BOOL hasDa } SrcSize = (UINT32) size; - pSrcData = (BYTE*) malloc(SrcSize); - - if (!pSrcData) - return; - - CopyMemory(pSrcData, data, SrcSize); - - bSuccess = ClipboardSetData(clipboard->system, srcFormatId, (void*) pSrcData, SrcSize); - - if (!bSuccess) - free(pSrcData); + bSuccess = ClipboardSetData(clipboard->system, srcFormatId, data, SrcSize); if (format->formatName) - { dstFormatId = ClipboardGetFormatId(clipboard->system, format->formatName); - } else - { dstFormatId = format->formatId; - } if (bSuccess) { @@ -646,8 +626,8 @@ static BOOL xf_cliprdr_get_requested_data(xfClipboard* clipboard, Atom target) unsigned long bytes_left; xfCliprdrFormat* format; xfContext* xfc = clipboard->xfc; - - format = xf_cliprdr_get_client_format_by_id(clipboard, clipboard->requestedFormatId); + format = xf_cliprdr_get_client_format_by_id(clipboard, + clipboard->requestedFormatId); if (!format || (format->atom != target)) { @@ -656,8 +636,8 @@ static BOOL xf_cliprdr_get_requested_data(xfClipboard* clipboard, Atom target) } XGetWindowProperty(xfc->display, xfc->drawable, - clipboard->property_atom, 0, 0, 0, target, - &type, &format_property, &length, &bytes_left, &data); + clipboard->property_atom, 0, 0, 0, target, + &type, &format_property, &length, &bytes_left, &data); if (data) { @@ -667,7 +647,6 @@ static BOOL xf_cliprdr_get_requested_data(xfClipboard* clipboard, Atom target) if (bytes_left <= 0 && !clipboard->incr_starts) { - } else if (type == clipboard->incr_atom) { @@ -695,33 +674,35 @@ static BOOL xf_cliprdr_get_requested_data(xfClipboard* clipboard, Atom target) has_data = TRUE; } else if (XGetWindowProperty(xfc->display, xfc->drawable, - clipboard->property_atom, 0, bytes_left, 0, target, - &type, &format_property, &length, &dummy, &data) == Success) + clipboard->property_atom, 0, bytes_left, 0, target, + &type, &format_property, &length, &dummy, &data) == Success) { if (clipboard->incr_starts) { - BYTE *new_data; - + BYTE* new_data; bytes_left = length * format_property / 8; - new_data = (BYTE*) realloc(clipboard->incr_data, clipboard->incr_data_length + bytes_left); + new_data = (BYTE*) realloc(clipboard->incr_data, + clipboard->incr_data_length + bytes_left); + if (!new_data) return FALSE; + clipboard->incr_data = new_data; - CopyMemory(clipboard->incr_data + clipboard->incr_data_length, data, bytes_left); + CopyMemory(clipboard->incr_data + clipboard->incr_data_length, data, + bytes_left); clipboard->incr_data_length += bytes_left; XFree(data); data = NULL; } + has_data = TRUE; } else { - } } XDeleteProperty(xfc->display, xfc->drawable, clipboard->property_atom); - xf_cliprdr_process_requested_data(clipboard, has_data, data, (int) bytes_left); if (data) @@ -753,24 +734,26 @@ static void xf_cliprdr_provide_targets(xfClipboard* clipboard, XEvent* respond) if (respond->xselection.property != None) { XChangeProperty(xfc->display, respond->xselection.requestor, - respond->xselection.property, XA_ATOM, 32, PropModeReplace, - (BYTE*) clipboard->targets, clipboard->numTargets); + respond->xselection.property, XA_ATOM, 32, PropModeReplace, + (BYTE*) clipboard->targets, clipboard->numTargets); } } -static void xf_cliprdr_provide_data(xfClipboard* clipboard, XEvent* respond, BYTE* data, UINT32 size) +static void xf_cliprdr_provide_data(xfClipboard* clipboard, XEvent* respond, + BYTE* data, UINT32 size) { xfContext* xfc = clipboard->xfc; if (respond->xselection.property != None) { XChangeProperty(xfc->display, respond->xselection.requestor, - respond->xselection.property, respond->xselection.target, - 8, PropModeReplace, data, size); + respond->xselection.property, respond->xselection.target, + 8, PropModeReplace, data, size); } } -static BOOL xf_cliprdr_process_selection_notify(xfClipboard* clipboard, XEvent* xevent) +static BOOL xf_cliprdr_process_selection_notify(xfClipboard* clipboard, + XEvent* xevent) { if (xevent->xselection.target == clipboard->targets[1]) { @@ -791,7 +774,8 @@ static BOOL xf_cliprdr_process_selection_notify(xfClipboard* clipboard, XEvent* } } -static BOOL xf_cliprdr_process_selection_request(xfClipboard* clipboard, XEvent* xevent) +static BOOL xf_cliprdr_process_selection_request(xfClipboard* clipboard, + XEvent* xevent) { int fmt; Atom type; @@ -829,7 +813,8 @@ static BOOL xf_cliprdr_process_selection_request(xfClipboard* clipboard, XEvent* { /* TODO */ } - else if (xevent->xselectionrequest.target == clipboard->targets[1]) /* TARGETS */ + else if (xevent->xselectionrequest.target == + clipboard->targets[1]) /* TARGETS */ { /* Someone else requests our available formats */ respond->xselection.property = xevent->xselectionrequest.property; @@ -837,7 +822,8 @@ static BOOL xf_cliprdr_process_selection_request(xfClipboard* clipboard, XEvent* } else { - format = xf_cliprdr_get_server_format_by_atom(clipboard, xevent->xselectionrequest.target); + format = xf_cliprdr_get_server_format_by_atom(clipboard, + xevent->xselectionrequest.target); if (format && (xevent->xselectionrequest.requestor != xfc->drawable)) { @@ -848,10 +834,9 @@ static BOOL xf_cliprdr_process_selection_request(xfClipboard* clipboard, XEvent* if (formatId == CF_RAW) { if (XGetWindowProperty(xfc->display, xevent->xselectionrequest.requestor, - clipboard->property_atom, 0, 4, 0, XA_INTEGER, - &type, &fmt, &length, &bytes_left, &data) != Success) + clipboard->property_atom, 0, 4, 0, XA_INTEGER, + &type, &fmt, &length, &bytes_left, &data) != Success) { - } if (data) @@ -862,11 +847,13 @@ static BOOL xf_cliprdr_process_selection_request(xfClipboard* clipboard, XEvent* } } - if ((clipboard->data != 0) && (formatId == clipboard->data_format_id) && (formatName == clipboard->data_format_name)) + if ((clipboard->data != 0) && (formatId == clipboard->data_format_id) + && (formatName == clipboard->data_format_name)) { /* Cached clipboard data available. Send it now */ respond->xselection.property = xevent->xselectionrequest.property; - xf_cliprdr_provide_data(clipboard, respond, clipboard->data, clipboard->data_length); + xf_cliprdr_provide_data(clipboard, respond, clipboard->data, + clipboard->data_length); } else if (clipboard->respond) { @@ -890,7 +877,6 @@ static BOOL xf_cliprdr_process_selection_request(xfClipboard* clipboard, XEvent* clipboard->data_format_name = formatName; clipboard->data_raw_format = rawTransfer; delayRespond = TRUE; - xf_cliprdr_send_data_request(clipboard, formatId); } } @@ -906,7 +892,8 @@ static BOOL xf_cliprdr_process_selection_request(xfClipboard* clipboard, XEvent* return TRUE; } -static BOOL xf_cliprdr_process_selection_clear(xfClipboard* clipboard, XEvent* xevent) +static BOOL xf_cliprdr_process_selection_clear(xfClipboard* clipboard, + XEvent* xevent) { xfContext* xfc = clipboard->xfc; @@ -914,11 +901,11 @@ static BOOL xf_cliprdr_process_selection_clear(xfClipboard* clipboard, XEvent* x return FALSE; XDeleteProperty(xfc->display, clipboard->root_window, clipboard->property_atom); - return TRUE; } -static BOOL xf_cliprdr_process_property_notify(xfClipboard* clipboard, XEvent* xevent) +static BOOL xf_cliprdr_process_property_notify(xfClipboard* clipboard, + XEvent* xevent) { xfCliprdrFormat* format; xfContext* xfc = clipboard->xfc; @@ -934,9 +921,10 @@ static BOOL xf_cliprdr_process_property_notify(xfClipboard* clipboard, XEvent* x xf_cliprdr_send_client_format_list(clipboard); } else if ((xevent->xproperty.window == xfc->drawable) && - (xevent->xproperty.state == PropertyNewValue) && clipboard->incr_starts) + (xevent->xproperty.state == PropertyNewValue) && clipboard->incr_starts) { - format = xf_cliprdr_get_client_format_by_id(clipboard, clipboard->requestedFormatId); + format = xf_cliprdr_get_client_format_by_id(clipboard, + clipboard->requestedFormatId); if (format) xf_cliprdr_get_requested_data(clipboard, format->atom); @@ -958,7 +946,9 @@ void xf_cliprdr_handle_xevent(xfContext* xfc, XEvent* event) return; #ifdef WITH_XFIXES - if (clipboard->xfixes_supported && event->type == XFixesSelectionNotify + clipboard->xfixes_event_base) + + if (clipboard->xfixes_supported + && event->type == XFixesSelectionNotify + clipboard->xfixes_event_base) { XFixesSelectionNotifyEvent* se = (XFixesSelectionNotifyEvent*) event; @@ -976,6 +966,7 @@ void xf_cliprdr_handle_xevent(xfContext* xfc, XEvent* event) return; } + #endif switch (event->type) @@ -1001,6 +992,7 @@ void xf_cliprdr_handle_xevent(xfContext* xfc, XEvent* event) { xf_cliprdr_check_owner(clipboard); } + break; } } @@ -1014,17 +1006,15 @@ UINT xf_cliprdr_send_client_capabilities(xfClipboard* clipboard) { CLIPRDR_CAPABILITIES capabilities; CLIPRDR_GENERAL_CAPABILITY_SET generalCapabilitySet; - capabilities.cCapabilitiesSets = 1; - capabilities.capabilitySets = (CLIPRDR_CAPABILITY_SET*) &(generalCapabilitySet); - + capabilities.capabilitySets = (CLIPRDR_CAPABILITY_SET*) & + (generalCapabilitySet); generalCapabilitySet.capabilitySetType = CB_CAPSTYPE_GENERAL; generalCapabilitySet.capabilitySetLength = 12; - generalCapabilitySet.version = CB_CAPS_VERSION_2; generalCapabilitySet.generalFlags = CB_USE_LONG_FORMAT_NAMES; - - return clipboard->context->ClientCapabilities(clipboard->context, &capabilities); + return clipboard->context->ClientCapabilities(clipboard->context, + &capabilities); } /** @@ -1039,10 +1029,9 @@ UINT xf_cliprdr_send_client_format_list(xfClipboard* clipboard) CLIPRDR_FORMAT_LIST formatList; xfContext* xfc = clipboard->xfc; UINT ret; - ZeroMemory(&formatList, sizeof(CLIPRDR_FORMAT_LIST)); - numFormats = clipboard->numClientFormats; + if (numFormats) { if (!(formats = (CLIPRDR_FORMAT*) calloc(numFormats, sizeof(CLIPRDR_FORMAT)))) @@ -1061,16 +1050,14 @@ UINT xf_cliprdr_send_client_format_list(xfClipboard* clipboard) formatList.msgFlags = CB_RESPONSE_OK; formatList.numFormats = numFormats; formatList.formats = formats; - ret = clipboard->context->ClientFormatList(clipboard->context, &formatList); - free(formats); if (clipboard->owner && clipboard->owner != xfc->drawable) { /* Request the owner for TARGETS, and wait for SelectionNotify event */ XConvertSelection(xfc->display, clipboard->clipboard_atom, - clipboard->targets[1], clipboard->property_atom, xfc->drawable, CurrentTime); + clipboard->targets[1], clipboard->property_atom, xfc->drawable, CurrentTime); } return ret; @@ -1081,15 +1068,15 @@ UINT xf_cliprdr_send_client_format_list(xfClipboard* clipboard) * * @return 0 on success, otherwise a Win32 error code */ -UINT xf_cliprdr_send_client_format_list_response(xfClipboard* clipboard, BOOL status) +UINT xf_cliprdr_send_client_format_list_response(xfClipboard* clipboard, + BOOL status) { CLIPRDR_FORMAT_LIST_RESPONSE formatListResponse; - formatListResponse.msgType = CB_FORMAT_LIST_RESPONSE; formatListResponse.msgFlags = status ? CB_RESPONSE_OK : CB_RESPONSE_FAIL; formatListResponse.dataLen = 0; - - return clipboard->context->ClientFormatListResponse(clipboard->context, &formatListResponse); + return clipboard->context->ClientFormatListResponse(clipboard->context, + &formatListResponse); } /** @@ -1097,18 +1084,19 @@ UINT xf_cliprdr_send_client_format_list_response(xfClipboard* clipboard, BOOL st * * @return 0 on success, otherwise a Win32 error code */ -static UINT xf_cliprdr_monitor_ready(CliprdrClientContext* context, CLIPRDR_MONITOR_READY* monitorReady) +static UINT xf_cliprdr_monitor_ready(CliprdrClientContext* context, + CLIPRDR_MONITOR_READY* monitorReady) { xfClipboard* clipboard = (xfClipboard*) context->custom; UINT ret; if ((ret = xf_cliprdr_send_client_capabilities(clipboard)) != CHANNEL_RC_OK) return ret; + if ((ret = xf_cliprdr_send_client_format_list(clipboard)) != CHANNEL_RC_OK) return ret; clipboard->sync = TRUE; - return CHANNEL_RC_OK; } @@ -1117,10 +1105,10 @@ static UINT xf_cliprdr_monitor_ready(CliprdrClientContext* context, CLIPRDR_MONI * * @return 0 on success, otherwise a Win32 error code */ -static UINT xf_cliprdr_server_capabilities(CliprdrClientContext* context, CLIPRDR_CAPABILITIES* capabilities) +static UINT xf_cliprdr_server_capabilities(CliprdrClientContext* context, + CLIPRDR_CAPABILITIES* capabilities) { //xfClipboard* clipboard = (xfClipboard*) context->custom; - return CHANNEL_RC_OK; } @@ -1129,7 +1117,8 @@ static UINT xf_cliprdr_server_capabilities(CliprdrClientContext* context, CLIPRD * * @return 0 on success, otherwise a Win32 error code */ -static UINT xf_cliprdr_server_format_list(CliprdrClientContext* context, CLIPRDR_FORMAT_LIST* formatList) +static UINT xf_cliprdr_server_format_list(CliprdrClientContext* context, + CLIPRDR_FORMAT_LIST* formatList) { int i, j; CLIPRDR_FORMAT* format; @@ -1153,14 +1142,16 @@ static UINT xf_cliprdr_server_format_list(CliprdrClientContext* context, CLIPRDR free(clipboard->serverFormats); clipboard->serverFormats = NULL; - clipboard->numServerFormats = 0; } clipboard->numServerFormats = formatList->numFormats + 1; /* +1 for CF_RAW */ - if (!(clipboard->serverFormats = (CLIPRDR_FORMAT*) calloc(clipboard->numServerFormats, sizeof(CLIPRDR_FORMAT)))) { - WLog_ERR(TAG, "failed to allocate %d CLIPRDR_FORMAT structs", clipboard->numServerFormats); + if (!(clipboard->serverFormats = (CLIPRDR_FORMAT*) calloc( + clipboard->numServerFormats, sizeof(CLIPRDR_FORMAT)))) + { + WLog_ERR(TAG, "failed to allocate %d CLIPRDR_FORMAT structs", + clipboard->numServerFormats); return CHANNEL_RC_NO_MEMORY; } @@ -1168,9 +1159,11 @@ static UINT xf_cliprdr_server_format_list(CliprdrClientContext* context, CLIPRDR { format = &formatList->formats[i]; clipboard->serverFormats[i].formatId = format->formatId; + if (format->formatName) { clipboard->serverFormats[i].formatName = _strdup(format->formatName); + if (!clipboard->serverFormats[i].formatName) { for (--i; i >= 0; --i) @@ -1188,9 +1181,7 @@ static UINT xf_cliprdr_server_format_list(CliprdrClientContext* context, CLIPRDR format = &clipboard->serverFormats[formatList->numFormats]; format->formatId = CF_RAW; format->formatName = NULL; - xf_cliprdr_provide_server_format_list(clipboard); - clipboard->numTargets = 2; for (i = 0; i < formatList->numFormats; i++) @@ -1207,11 +1198,9 @@ static UINT xf_cliprdr_server_format_list(CliprdrClientContext* context, CLIPRDR } ret = xf_cliprdr_send_client_format_list_response(clipboard, TRUE); - - XSetSelectionOwner(xfc->display, clipboard->clipboard_atom, xfc->drawable, CurrentTime); - + XSetSelectionOwner(xfc->display, clipboard->clipboard_atom, xfc->drawable, + CurrentTime); XFlush(xfc->display); - return ret; } @@ -1220,10 +1209,10 @@ static UINT xf_cliprdr_server_format_list(CliprdrClientContext* context, CLIPRDR * * @return 0 on success, otherwise a Win32 error code */ -static UINT xf_cliprdr_server_format_list_response(CliprdrClientContext* context, CLIPRDR_FORMAT_LIST_RESPONSE* formatListResponse) +static UINT xf_cliprdr_server_format_list_response(CliprdrClientContext* + context, CLIPRDR_FORMAT_LIST_RESPONSE* formatListResponse) { //xfClipboard* clipboard = (xfClipboard*) context->custom; - return CHANNEL_RC_OK; } @@ -1232,22 +1221,21 @@ static UINT xf_cliprdr_server_format_list_response(CliprdrClientContext* context * * @return 0 on success, otherwise a Win32 error code */ -static UINT xf_cliprdr_server_format_data_request(CliprdrClientContext* context, CLIPRDR_FORMAT_DATA_REQUEST* formatDataRequest) +static UINT xf_cliprdr_server_format_data_request(CliprdrClientContext* context, + CLIPRDR_FORMAT_DATA_REQUEST* formatDataRequest) { BOOL rawTransfer; xfCliprdrFormat* format = NULL; UINT32 formatId = formatDataRequest->requestedFormatId; xfClipboard* clipboard = (xfClipboard*) context->custom; xfContext* xfc = clipboard->xfc; - rawTransfer = xf_cliprdr_is_raw_transfer_available(clipboard); if (rawTransfer) { format = xf_cliprdr_get_client_format_by_id(clipboard, CF_RAW); - XChangeProperty(xfc->display, xfc->drawable, clipboard->property_atom, - XA_INTEGER, 32, PropModeReplace, (BYTE*) &formatId, 1); + XA_INTEGER, 32, PropModeReplace, (BYTE*) &formatId, 1); } else format = xf_cliprdr_get_client_format_by_id(clipboard, formatId); @@ -1256,14 +1244,10 @@ static UINT xf_cliprdr_server_format_data_request(CliprdrClientContext* context, return xf_cliprdr_send_data_response(clipboard, NULL, 0); clipboard->requestedFormatId = rawTransfer ? CF_RAW : formatId; - XConvertSelection(xfc->display, clipboard->clipboard_atom, - format->atom, clipboard->property_atom, xfc->drawable, CurrentTime); - + format->atom, clipboard->property_atom, xfc->drawable, CurrentTime); XFlush(xfc->display); - /* After this point, we expect a SelectionNotify event from the clipboard owner. */ - return CHANNEL_RC_OK; } @@ -1272,10 +1256,10 @@ static UINT xf_cliprdr_server_format_data_request(CliprdrClientContext* context, * * @return 0 on success, otherwise a Win32 error code */ -static UINT xf_cliprdr_server_format_data_response(CliprdrClientContext* context, CLIPRDR_FORMAT_DATA_RESPONSE* formatDataResponse) +static UINT xf_cliprdr_server_format_data_response(CliprdrClientContext* + context, CLIPRDR_FORMAT_DATA_RESPONSE* formatDataResponse) { BOOL bSuccess; - BYTE* pSrcData; BYTE* pDstData; UINT32 DstSize; UINT32 SrcSize; @@ -1298,7 +1282,6 @@ static UINT xf_cliprdr_server_format_data_response(CliprdrClientContext* context pDstData = NULL; DstSize = 0; - srcFormatId = 0; dstFormatId = 0; @@ -1320,43 +1303,33 @@ static UINT xf_cliprdr_server_format_data_response(CliprdrClientContext* context { switch (clipboard->data_format_id) { - case CF_TEXT: - srcFormatId = CF_TEXT; - dstFormatId = ClipboardGetFormatId(clipboard->system, "UTF8_STRING"); - nullTerminated = TRUE; - break; - - case CF_OEMTEXT: - srcFormatId = CF_OEMTEXT; - dstFormatId = ClipboardGetFormatId(clipboard->system, "UTF8_STRING"); - nullTerminated = TRUE; - break; - - case CF_UNICODETEXT: - srcFormatId = CF_UNICODETEXT; - dstFormatId = ClipboardGetFormatId(clipboard->system, "UTF8_STRING"); - nullTerminated = TRUE; - break; - - case CF_DIB: - srcFormatId = CF_DIB; - dstFormatId = ClipboardGetFormatId(clipboard->system, "image/bmp"); - break; + case CF_TEXT: + srcFormatId = CF_TEXT; + dstFormatId = ClipboardGetFormatId(clipboard->system, "UTF8_STRING"); + nullTerminated = TRUE; + break; + + case CF_OEMTEXT: + srcFormatId = CF_OEMTEXT; + dstFormatId = ClipboardGetFormatId(clipboard->system, "UTF8_STRING"); + nullTerminated = TRUE; + break; + + case CF_UNICODETEXT: + srcFormatId = CF_UNICODETEXT; + dstFormatId = ClipboardGetFormatId(clipboard->system, "UTF8_STRING"); + nullTerminated = TRUE; + break; + + case CF_DIB: + srcFormatId = CF_DIB; + dstFormatId = ClipboardGetFormatId(clipboard->system, "image/bmp"); + break; } } SrcSize = (UINT32) size; - pSrcData = (BYTE*) malloc(SrcSize); - - if (!pSrcData) - return CHANNEL_RC_NO_MEMORY; - - CopyMemory(pSrcData, data, SrcSize); - - bSuccess = ClipboardSetData(clipboard->system, srcFormatId, (void*) pSrcData, SrcSize); - - if (!bSuccess) - free (pSrcData); + bSuccess = ClipboardSetData(clipboard->system, srcFormatId, data, SrcSize); if (bSuccess) { @@ -1372,15 +1345,12 @@ static UINT xf_cliprdr_server_format_data_response(CliprdrClientContext* context clipboard->data = pDstData; clipboard->data_length = DstSize; - xf_cliprdr_provide_data(clipboard, clipboard->respond, pDstData, DstSize); - - XSendEvent(xfc->display, clipboard->respond->xselection.requestor, 0, 0, clipboard->respond); + XSendEvent(xfc->display, clipboard->respond->xselection.requestor, 0, 0, + clipboard->respond); XFlush(xfc->display); - free(clipboard->respond); clipboard->respond = NULL; - return CHANNEL_RC_OK; } @@ -1397,16 +1367,11 @@ xfClipboard* xf_clipboard_new(xfContext* xfc) } xfc->clipboard = clipboard; - clipboard->xfc = xfc; - channels = ((rdpContext*) xfc)->channels; clipboard->channels = channels; - clipboard->system = ClipboardCreate(); - clipboard->requestedFormatId = -1; - clipboard->root_window = DefaultRootWindow(xfc->display); clipboard->clipboard_atom = XInternAtom(xfc->display, "CLIPBOARD", FALSE); @@ -1418,23 +1383,23 @@ xfClipboard* xf_clipboard_new(xfContext* xfc) } clipboard->property_atom = XInternAtom(xfc->display, "_FREERDP_CLIPRDR", FALSE); - - clipboard->raw_transfer_atom = XInternAtom(xfc->display, "_FREERDP_CLIPRDR_RAW", FALSE); - clipboard->raw_format_list_atom = XInternAtom(xfc->display, "_FREERDP_CLIPRDR_FORMATS", FALSE); - + clipboard->raw_transfer_atom = XInternAtom(xfc->display, "_FREERDP_CLIPRDR_RAW", + FALSE); + clipboard->raw_format_list_atom = XInternAtom(xfc->display, + "_FREERDP_CLIPRDR_FORMATS", FALSE); xf_cliprdr_set_raw_transfer_enabled(clipboard, TRUE); - XSelectInput(xfc->display, clipboard->root_window, PropertyChangeMask); - #ifdef WITH_XFIXES - if (XFixesQueryExtension(xfc->display, &clipboard->xfixes_event_base, &clipboard->xfixes_error_base)) + + if (XFixesQueryExtension(xfc->display, &clipboard->xfixes_event_base, + &clipboard->xfixes_error_base)) { int xfmajor, xfminor; if (XFixesQueryVersion(xfc->display, &xfmajor, &xfminor)) { XFixesSelectSelectionInput(xfc->display, clipboard->root_window, - clipboard->clipboard_atom, XFixesSetSelectionOwnerNotifyMask); + clipboard->clipboard_atom, XFixesSetSelectionOwnerNotifyMask); clipboard->xfixes_supported = TRUE; } else @@ -1446,59 +1411,57 @@ xfClipboard* xf_clipboard_new(xfContext* xfc) { WLog_ERR(TAG, "Error loading X Fixes extension"); } + #else - WLog_ERR(TAG, "Warning: Using clipboard redirection without XFIXES extension is strongly discouraged!"); + WLog_ERR(TAG, + "Warning: Using clipboard redirection without XFIXES extension is strongly discouraged!"); #endif - n = 0; - - clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "_FREERDP_RAW", False); + clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "_FREERDP_RAW", + False); clipboard->clientFormats[n].formatId = CF_RAW; n++; - - clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "UTF8_STRING", False); + clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "UTF8_STRING", + False); clipboard->clientFormats[n].formatId = CF_UNICODETEXT; n++; - clipboard->clientFormats[n].atom = XA_STRING; clipboard->clientFormats[n].formatId = CF_TEXT; n++; - - clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "image/png", False); + clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "image/png", + False); clipboard->clientFormats[n].formatId = CB_FORMAT_PNG; n++; - - clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "image/jpeg", False); + clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "image/jpeg", + False); clipboard->clientFormats[n].formatId = CB_FORMAT_JPEG; n++; - - clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "image/gif", False); + clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "image/gif", + False); clipboard->clientFormats[n].formatId = CB_FORMAT_GIF; n++; - - clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "image/bmp", False); + clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "image/bmp", + False); clipboard->clientFormats[n].formatId = CF_DIB; n++; - - clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "text/html", False); + clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "text/html", + False); clipboard->clientFormats[n].formatId = CB_FORMAT_HTML; clipboard->clientFormats[n].formatName = _strdup("HTML Format"); + if (!clipboard->clientFormats[n].formatName) { ClipboardDestroy(clipboard->system); free(clipboard); return NULL; } - n++; + n++; clipboard->numClientFormats = n; - clipboard->targets[0] = XInternAtom(xfc->display, "TIMESTAMP", FALSE); clipboard->targets[1] = XInternAtom(xfc->display, "TARGETS", FALSE); clipboard->numTargets = 2; - clipboard->incr_atom = XInternAtom(xfc->display, "INCR", FALSE); - return clipboard; } @@ -1525,7 +1488,6 @@ void xf_clipboard_free(xfClipboard* clipboard) } ClipboardDestroy(clipboard->system); - free(clipboard->data); free(clipboard->respond); free(clipboard->incr_data); @@ -1537,7 +1499,6 @@ void xf_cliprdr_init(xfContext* xfc, CliprdrClientContext* cliprdr) xfc->cliprdr = cliprdr; xfc->clipboard->context = cliprdr; cliprdr->custom = (void*) xfc->clipboard; - cliprdr->MonitorReady = xf_cliprdr_monitor_ready; cliprdr->ServerCapabilities = xf_cliprdr_server_capabilities; cliprdr->ServerFormatList = xf_cliprdr_server_format_list; diff --git a/winpr/libwinpr/clipboard/clipboard.c b/winpr/libwinpr/clipboard/clipboard.c index 01a7550..c42f941 100644 --- a/winpr/libwinpr/clipboard/clipboard.c +++ b/winpr/libwinpr/clipboard/clipboard.c @@ -58,14 +58,15 @@ const char* CF_STANDARD_STRINGS[CF_MAX] = "CF_DIBV5" /* 17 */ }; -wClipboardFormat* ClipboardFindFormat(wClipboard* clipboard, UINT32 formatId, const char* name) +wClipboardFormat* ClipboardFindFormat(wClipboard* clipboard, UINT32 formatId, + const char* name) { UINT32 index; wClipboardFormat* format = NULL; if (!clipboard) return NULL; - + if (formatId) { for (index = 0; index < clipboard->numFormats; index++) @@ -91,7 +92,6 @@ wClipboardFormat* ClipboardFindFormat(wClipboard* clipboard, UINT32 formatId, co else { /* special "CF_RAW" case */ - if (clipboard->numFormats > 0) { format = &clipboard->formats[0]; @@ -99,7 +99,8 @@ wClipboardFormat* ClipboardFindFormat(wClipboard* clipboard, UINT32 formatId, co if (format->formatId) return NULL; - if (!format->formatName || (strcmp(format->formatName, CF_STANDARD_STRINGS[0]) == 0)) + if (!format->formatName + || (strcmp(format->formatName, CF_STANDARD_STRINGS[0]) == 0)) return format; } } @@ -107,7 +108,8 @@ wClipboardFormat* ClipboardFindFormat(wClipboard* clipboard, UINT32 formatId, co return format; } -wClipboardSynthesizer* ClipboardFindSynthesizer(wClipboardFormat* format, UINT32 formatId) +wClipboardSynthesizer* ClipboardFindSynthesizer(wClipboardFormat* format, + UINT32 formatId) { UINT32 index; wClipboardSynthesizer* synthesizer; @@ -133,7 +135,7 @@ void ClipboardLock(wClipboard* clipboard) { if (!clipboard) return; - + EnterCriticalSection(&(clipboard->lock)); } @@ -141,7 +143,7 @@ void ClipboardUnlock(wClipboard* clipboard) { if (!clipboard) return; - + LeaveCriticalSection(&(clipboard->lock)); } @@ -149,7 +151,7 @@ BOOL ClipboardEmpty(wClipboard* clipboard) { if (!clipboard) return FALSE; - + if (clipboard->data) { free((void*) clipboard->data); @@ -157,10 +159,8 @@ BOOL ClipboardEmpty(wClipboard* clipboard) } clipboard->size = 0; - clipboard->formatId = 0; clipboard->sequenceNumber++; - return TRUE; } @@ -168,16 +168,17 @@ UINT32 ClipboardCountRegisteredFormats(wClipboard* clipboard) { if (!clipboard) return 0; - + return clipboard->numFormats; } -UINT32 ClipboardGetRegisteredFormatIds(wClipboard* clipboard, UINT32** ppFormatIds) +UINT32 ClipboardGetRegisteredFormatIds(wClipboard* clipboard, + UINT32** ppFormatIds) { UINT32 index; UINT32* pFormatIds; wClipboardFormat* format; - + if (!clipboard) return 0; @@ -208,7 +209,7 @@ UINT32 ClipboardGetRegisteredFormatIds(wClipboard* clipboard, UINT32** ppFormatI UINT32 ClipboardRegisterFormat(wClipboard* clipboard, const char* name) { wClipboardFormat* format; - + if (!clipboard) return 0; @@ -220,10 +221,9 @@ UINT32 ClipboardRegisterFormat(wClipboard* clipboard, const char* name) if ((clipboard->numFormats + 1) >= clipboard->maxFormats) { UINT32 numFormats = clipboard->maxFormats * 2; - wClipboardFormat *tmpFormat; - + wClipboardFormat* tmpFormat; tmpFormat = (wClipboardFormat*) realloc(clipboard->formats, - numFormats * sizeof(wClipboardFormat)); + numFormats * sizeof(wClipboardFormat)); if (!tmpFormat) return 0; @@ -245,17 +245,16 @@ UINT32 ClipboardRegisterFormat(wClipboard* clipboard, const char* name) format->formatId = clipboard->nextFormatId++; clipboard->numFormats++; - return format->formatId; } BOOL ClipboardRegisterSynthesizer(wClipboard* clipboard, UINT32 formatId, - UINT32 syntheticId, CLIPBOARD_SYNTHESIZE_FN pfnSynthesize) + UINT32 syntheticId, CLIPBOARD_SYNTHESIZE_FN pfnSynthesize) { UINT32 index; wClipboardFormat* format; wClipboardSynthesizer* synthesizer; - + if (!clipboard) return FALSE; @@ -271,11 +270,10 @@ BOOL ClipboardRegisterSynthesizer(wClipboard* clipboard, UINT32 formatId, if (!synthesizer) { - wClipboardSynthesizer *tmpSynthesizer; + wClipboardSynthesizer* tmpSynthesizer; UINT32 numSynthesizers = format->numSynthesizers + 1; - tmpSynthesizer = (wClipboardSynthesizer*) realloc(format->synthesizers, - numSynthesizers * sizeof(wClipboardSynthesizer)); + numSynthesizers * sizeof(wClipboardSynthesizer)); if (!tmpSynthesizer) return FALSE; @@ -287,10 +285,8 @@ BOOL ClipboardRegisterSynthesizer(wClipboard* clipboard, UINT32 formatId, } ZeroMemory(synthesizer, sizeof(wClipboardSynthesizer)); - synthesizer->syntheticId = syntheticId; synthesizer->pfnSynthesize = pfnSynthesize; - return TRUE; } @@ -298,7 +294,7 @@ UINT32 ClipboardCountFormats(wClipboard* clipboard) { UINT32 count; wClipboardFormat* format; - + if (!clipboard) return 0; @@ -308,7 +304,6 @@ UINT32 ClipboardCountFormats(wClipboard* clipboard) return 0; count = 1 + format->numSynthesizers; - return count; } @@ -319,7 +314,7 @@ UINT32 ClipboardGetFormatIds(wClipboard* clipboard, UINT32** ppFormatIds) UINT32* pFormatIds; wClipboardFormat* format; wClipboardSynthesizer* synthesizer; - + if (!clipboard) return 0; @@ -360,7 +355,7 @@ BOOL ClipboardInitFormats(wClipboard* clipboard) { UINT32 formatId = 0; wClipboardFormat* format; - + if (!clipboard) return FALSE; @@ -368,24 +363,24 @@ BOOL ClipboardInitFormats(wClipboard* clipboard) { format = &(clipboard->formats[clipboard->numFormats]); ZeroMemory(format, sizeof(wClipboardFormat)); - format->formatId = formatId; format->formatName = _strdup(CF_STANDARD_STRINGS[formatId]); if (!format->formatName) { int i; - for (i = formatId-1; i >= 0; --i) + + for (i = formatId - 1; i >= 0; --i) { format = &(clipboard->formats[--clipboard->numFormats]); - free((void *)format->formatName); + free((void*)format->formatName); } + return FALSE; } } ClipboardInitSynthesizers(clipboard); - return TRUE; } @@ -395,7 +390,7 @@ UINT32 ClipboardGetFormatId(wClipboard* clipboard, const char* name) if (!clipboard) return 0; - + format = ClipboardFindFormat(clipboard, 0, name); if (!format) @@ -407,7 +402,7 @@ UINT32 ClipboardGetFormatId(wClipboard* clipboard, const char* name) const char* ClipboardGetFormatName(wClipboard* clipboard, UINT32 formatId) { wClipboardFormat* format; - + if (!clipboard) return NULL; @@ -427,7 +422,7 @@ void* ClipboardGetData(wClipboard* clipboard, UINT32 formatId, UINT32* pSize) void* pDstData = NULL; wClipboardFormat* format; wClipboardSynthesizer* synthesizer; - + if (!clipboard) return NULL; @@ -445,7 +440,6 @@ void* ClipboardGetData(wClipboard* clipboard, UINT32 formatId, UINT32* pSize) if (formatId == format->formatId) { DstSize = SrcSize; - pDstData = malloc(DstSize); if (!pDstData) @@ -462,18 +456,19 @@ void* ClipboardGetData(wClipboard* clipboard, UINT32 formatId, UINT32* pSize) return NULL; DstSize = SrcSize; - pDstData = synthesizer->pfnSynthesize(clipboard, format->formatId, pSrcData, &DstSize); - + pDstData = synthesizer->pfnSynthesize(clipboard, format->formatId, pSrcData, + &DstSize); *pSize = DstSize; } return pDstData; } -BOOL ClipboardSetData(wClipboard* clipboard, UINT32 formatId, const void* data, UINT32 size) +BOOL ClipboardSetData(wClipboard* clipboard, UINT32 formatId, const void* data, + UINT32 size) { wClipboardFormat* format; - + if (!clipboard) return FALSE; @@ -483,12 +478,15 @@ BOOL ClipboardSetData(wClipboard* clipboard, UINT32 formatId, const void* data, return FALSE; free((void*) clipboard->data); - clipboard->data = data; - clipboard->size = size; + clipboard->data = malloc(size); + if (!clipboard->data) + return FALSE; + + memcpy(clipboard->data, data, size); + clipboard->size = size; clipboard->formatId = formatId; clipboard->sequenceNumber++; - return TRUE; } @@ -496,7 +494,7 @@ UINT64 ClipboardGetOwner(wClipboard* clipboard) { if (!clipboard) return 0; - + return clipboard->ownerId; } @@ -504,14 +502,13 @@ void ClipboardSetOwner(wClipboard* clipboard, UINT64 ownerId) { if (!clipboard) return; - + clipboard->ownerId = ownerId; } wClipboard* ClipboardCreate() { wClipboard* clipboard; - clipboard = (wClipboard*) calloc(1, sizeof(wClipboard)); if (clipboard) @@ -527,7 +524,8 @@ wClipboard* ClipboardCreate() clipboard->numFormats = 0; clipboard->maxFormats = 64; - clipboard->formats = (wClipboardFormat*) malloc(clipboard->maxFormats * sizeof(wClipboardFormat)); + clipboard->formats = (wClipboardFormat*) malloc(clipboard->maxFormats * sizeof( + wClipboardFormat)); if (!clipboard->formats) { @@ -536,7 +534,7 @@ wClipboard* ClipboardCreate() return NULL; } - if(!ClipboardInitFormats(clipboard)) + if (!ClipboardInitFormats(clipboard)) { DeleteCriticalSection(&(clipboard->lock)); free(clipboard); @@ -571,11 +569,8 @@ void ClipboardDestroy(wClipboard* clipboard) free((void*) clipboard->data); clipboard->data = NULL; clipboard->size = 0; - clipboard->numFormats = 0; free(clipboard->formats); - DeleteCriticalSection(&(clipboard->lock)); - free(clipboard); } diff --git a/winpr/libwinpr/clipboard/test/TestClipboardFormats.c b/winpr/libwinpr/clipboard/test/TestClipboardFormats.c index 668af2c..6c0bc4a 100644 --- a/winpr/libwinpr/clipboard/test/TestClipboardFormats.c +++ b/winpr/libwinpr/clipboard/test/TestClipboardFormats.c @@ -34,9 +34,8 @@ int TestClipboardFormats(int argc, char* argv[]) BOOL bSuccess; UINT32 SrcSize; UINT32 DstSize; - char* pSrcData; + const char* pSrcData = "this is a test string"; char* pDstData; - pSrcData = _strdup("this is a test string"); if (!pSrcData) { @@ -45,12 +44,8 @@ int TestClipboardFormats(int argc, char* argv[]) } SrcSize = (UINT32)(strlen(pSrcData) + 1); - bSuccess = ClipboardSetData(clipboard, utf8StringFormatId, (void*) pSrcData, + bSuccess = ClipboardSetData(clipboard, utf8StringFormatId, pSrcData, SrcSize); - - if (!bSuccess) - free(pSrcData); - fprintf(stderr, "ClipboardSetData: %d\n", bSuccess); DstSize = 0; pDstData = (char*) ClipboardGetData(clipboard, utf8StringFormatId, &DstSize); -- 2.7.4