Refactored ClipboardSetData.

This commit is contained in:
Armin Novak 2016-10-06 13:31:25 +02:00
parent 2db04736ac
commit 8f1adf64ee
7 changed files with 302 additions and 369 deletions

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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);

View File

@ -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;
}

File diff suppressed because it is too large Load Diff

View File

@ -58,7 +58,8 @@ 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;
@ -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;
@ -157,10 +159,8 @@ BOOL ClipboardEmpty(wClipboard* clipboard)
}
clipboard->size = 0;
clipboard->formatId = 0;
clipboard->sequenceNumber++;
return TRUE;
}
@ -172,7 +172,8 @@ UINT32 ClipboardCountRegisteredFormats(wClipboard* clipboard)
return clipboard->numFormats;
}
UINT32 ClipboardGetRegisteredFormatIds(wClipboard* clipboard, UINT32** ppFormatIds)
UINT32 ClipboardGetRegisteredFormatIds(wClipboard* clipboard,
UINT32** ppFormatIds)
{
UINT32 index;
UINT32* pFormatIds;
@ -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,12 +245,11 @@ 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;
@ -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;
}
@ -308,7 +304,6 @@ UINT32 ClipboardCountFormats(wClipboard* clipboard)
return 0;
count = 1 + format->numSynthesizers;
return count;
}
@ -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;
}
@ -445,7 +440,6 @@ void* ClipboardGetData(wClipboard* clipboard, UINT32 formatId, UINT32* pSize)
if (formatId == format->formatId)
{
DstSize = SrcSize;
pDstData = malloc(DstSize);
if (!pDstData)
@ -462,15 +456,16 @@ 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;
@ -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;
}
@ -511,7 +509,6 @@ void ClipboardSetOwner(wClipboard* clipboard, UINT64 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);
}

View File

@ -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);