FreeRDP/client/Mac/Clipboard.m

435 lines
12 KiB
Mathematica
Raw Normal View History

/**
* FreeRDP: A Remote Desktop Protocol Implementation
*
* Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
* Copyright 2015 Thincast Technologies GmbH
* Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import "Clipboard.h"
2022-02-01 18:08:24 +03:00
#import "MRDPView.h"
2019-11-06 17:24:51 +03:00
int mac_cliprdr_send_client_format_list(CliprdrClientContext *cliprdr)
{
UINT32 index;
UINT32 formatId;
UINT32 numFormats;
2019-11-06 17:24:51 +03:00
UINT32 *pFormatIds;
const char *formatName;
CLIPRDR_FORMAT *formats;
CLIPRDR_FORMAT_LIST formatList = { 0 };
2019-11-06 17:24:51 +03:00
mfContext *mfc = (mfContext *)cliprdr->custom;
ZeroMemory(&formatList, sizeof(CLIPRDR_FORMAT_LIST));
2019-11-06 17:24:51 +03:00
pFormatIds = NULL;
numFormats = ClipboardGetFormatIds(mfc->clipboard, &pFormatIds);
2019-11-06 17:24:51 +03:00
formats = (CLIPRDR_FORMAT *)calloc(numFormats, sizeof(CLIPRDR_FORMAT));
if (!formats)
return -1;
2019-11-06 17:24:51 +03:00
for (index = 0; index < numFormats; index++)
{
formatId = pFormatIds[index];
formatName = ClipboardGetFormatName(mfc->clipboard, formatId);
2019-11-06 17:24:51 +03:00
formats[index].formatId = formatId;
formats[index].formatName = NULL;
2019-11-06 17:24:51 +03:00
if ((formatId > CF_MAX) && formatName)
formats[index].formatName = _strdup(formatName);
}
2019-11-06 17:24:51 +03:00
formatList.msgFlags = CB_RESPONSE_OK;
formatList.numFormats = numFormats;
formatList.formats = formats;
formatList.msgType = CB_FORMAT_LIST;
2019-11-06 17:24:51 +03:00
mfc->cliprdr->ClientFormatList(mfc->cliprdr, &formatList);
2019-11-06 17:24:51 +03:00
for (index = 0; index < numFormats; index++)
{
free(formats[index].formatName);
}
2019-11-06 17:24:51 +03:00
free(pFormatIds);
free(formats);
2019-11-06 17:24:51 +03:00
return 1;
}
2019-11-06 17:24:51 +03:00
static int mac_cliprdr_send_client_format_list_response(CliprdrClientContext *cliprdr, BOOL status)
{
CLIPRDR_FORMAT_LIST_RESPONSE formatListResponse;
2019-11-06 17:24:51 +03:00
formatListResponse.msgType = CB_FORMAT_LIST_RESPONSE;
formatListResponse.msgFlags = status ? CB_RESPONSE_OK : CB_RESPONSE_FAIL;
formatListResponse.dataLen = 0;
2019-11-06 17:24:51 +03:00
cliprdr->ClientFormatListResponse(cliprdr, &formatListResponse);
2019-11-06 17:24:51 +03:00
return 1;
}
2019-11-06 17:24:51 +03:00
static int mac_cliprdr_send_client_format_data_request(CliprdrClientContext *cliprdr,
UINT32 formatId)
{
CLIPRDR_FORMAT_DATA_REQUEST formatDataRequest;
2019-11-06 17:24:51 +03:00
mfContext *mfc = (mfContext *)cliprdr->custom;
ZeroMemory(&formatDataRequest, sizeof(CLIPRDR_FORMAT_DATA_REQUEST));
2019-11-06 17:24:51 +03:00
formatDataRequest.msgType = CB_FORMAT_DATA_REQUEST;
formatDataRequest.msgFlags = 0;
2019-11-06 17:24:51 +03:00
formatDataRequest.requestedFormatId = formatId;
mfc->requestedFormatId = formatId;
ResetEvent(mfc->clipboardRequestEvent);
2019-11-06 17:24:51 +03:00
cliprdr->ClientFormatDataRequest(cliprdr, &formatDataRequest);
2019-11-06 17:24:51 +03:00
return 1;
}
2019-11-06 17:24:51 +03:00
static int mac_cliprdr_send_client_capabilities(CliprdrClientContext *cliprdr)
{
CLIPRDR_CAPABILITIES capabilities;
CLIPRDR_GENERAL_CAPABILITY_SET generalCapabilitySet;
2019-11-06 17:24:51 +03:00
capabilities.cCapabilitiesSets = 1;
2019-11-06 17:24:51 +03:00
capabilities.capabilitySets = (CLIPRDR_CAPABILITY_SET *)&(generalCapabilitySet);
generalCapabilitySet.capabilitySetType = CB_CAPSTYPE_GENERAL;
generalCapabilitySet.capabilitySetLength = 12;
2019-11-06 17:24:51 +03:00
generalCapabilitySet.version = CB_CAPS_VERSION_2;
generalCapabilitySet.generalFlags = CB_USE_LONG_FORMAT_NAMES;
2019-11-06 17:24:51 +03:00
cliprdr->ClientCapabilities(cliprdr, &capabilities);
2019-11-06 17:24:51 +03:00
return 1;
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
2019-11-06 17:24:51 +03:00
static UINT mac_cliprdr_monitor_ready(CliprdrClientContext *cliprdr,
const CLIPRDR_MONITOR_READY *monitorReady)
{
2019-11-06 17:24:51 +03:00
mfContext *mfc = (mfContext *)cliprdr->custom;
mfc->clipboardSync = TRUE;
mac_cliprdr_send_client_capabilities(cliprdr);
mac_cliprdr_send_client_format_list(cliprdr);
2019-11-06 17:24:51 +03:00
return CHANNEL_RC_OK;
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
2019-11-06 17:24:51 +03:00
static UINT mac_cliprdr_server_capabilities(CliprdrClientContext *cliprdr,
const CLIPRDR_CAPABILITIES *capabilities)
{
UINT32 index;
2019-11-06 17:24:51 +03:00
CLIPRDR_CAPABILITY_SET *capabilitySet;
mfContext *mfc = (mfContext *)cliprdr->custom;
for (index = 0; index < capabilities->cCapabilitiesSets; index++)
{
capabilitySet = &(capabilities->capabilitySets[index]);
2019-11-06 17:24:51 +03:00
if ((capabilitySet->capabilitySetType == CB_CAPSTYPE_GENERAL) &&
(capabilitySet->capabilitySetLength >= CB_CAPSTYPE_GENERAL_LEN))
{
2019-11-06 17:24:51 +03:00
CLIPRDR_GENERAL_CAPABILITY_SET *generalCapabilitySet =
(CLIPRDR_GENERAL_CAPABILITY_SET *)capabilitySet;
mfc->clipboardCapabilities = generalCapabilitySet->generalFlags;
break;
}
}
2019-11-06 17:24:51 +03:00
return CHANNEL_RC_OK;
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
2019-11-06 17:24:51 +03:00
static UINT mac_cliprdr_server_format_list(CliprdrClientContext *cliprdr,
const CLIPRDR_FORMAT_LIST *formatList)
{
UINT32 index;
2019-11-06 17:24:51 +03:00
CLIPRDR_FORMAT *format;
mfContext *mfc = (mfContext *)cliprdr->custom;
if (mfc->serverFormats)
{
for (index = 0; index < mfc->numServerFormats; index++)
{
free(mfc->serverFormats[index].formatName);
}
2019-11-06 17:24:51 +03:00
free(mfc->serverFormats);
mfc->serverFormats = NULL;
mfc->numServerFormats = 0;
}
2019-11-06 17:24:51 +03:00
if (formatList->numFormats < 1)
return CHANNEL_RC_OK;
2019-11-06 17:24:51 +03:00
mfc->numServerFormats = formatList->numFormats;
2019-11-06 17:24:51 +03:00
mfc->serverFormats = (CLIPRDR_FORMAT *)calloc(mfc->numServerFormats, sizeof(CLIPRDR_FORMAT));
if (!mfc->serverFormats)
return CHANNEL_RC_NO_MEMORY;
2019-11-06 17:24:51 +03:00
for (index = 0; index < mfc->numServerFormats; index++)
{
mfc->serverFormats[index].formatId = formatList->formats[index].formatId;
mfc->serverFormats[index].formatName = NULL;
2019-11-06 17:24:51 +03:00
if (formatList->formats[index].formatName)
mfc->serverFormats[index].formatName = _strdup(formatList->formats[index].formatName);
}
2019-11-06 17:24:51 +03:00
mac_cliprdr_send_client_format_list_response(cliprdr, TRUE);
2019-11-06 17:24:51 +03:00
for (index = 0; index < mfc->numServerFormats; index++)
{
format = &(mfc->serverFormats[index]);
2019-11-06 17:24:51 +03:00
if (format->formatId == CF_UNICODETEXT)
{
mac_cliprdr_send_client_format_data_request(cliprdr, CF_UNICODETEXT);
break;
}
else if (format->formatId == CF_OEMTEXT)
{
mac_cliprdr_send_client_format_data_request(cliprdr, CF_OEMTEXT);
break;
}
else if (format->formatId == CF_TEXT)
{
mac_cliprdr_send_client_format_data_request(cliprdr, CF_TEXT);
break;
}
}
2019-11-06 17:24:51 +03:00
return CHANNEL_RC_OK;
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
2019-11-06 17:24:51 +03:00
static UINT
mac_cliprdr_server_format_list_response(CliprdrClientContext *cliprdr,
const CLIPRDR_FORMAT_LIST_RESPONSE *formatListResponse)
{
return CHANNEL_RC_OK;
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
2019-11-06 17:24:51 +03:00
static UINT
mac_cliprdr_server_lock_clipboard_data(CliprdrClientContext *cliprdr,
const CLIPRDR_LOCK_CLIPBOARD_DATA *lockClipboardData)
{
return CHANNEL_RC_OK;
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
2019-11-06 17:24:51 +03:00
static UINT
mac_cliprdr_server_unlock_clipboard_data(CliprdrClientContext *cliprdr,
const CLIPRDR_UNLOCK_CLIPBOARD_DATA *unlockClipboardData)
{
return CHANNEL_RC_OK;
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
2019-11-06 17:24:51 +03:00
static UINT
mac_cliprdr_server_format_data_request(CliprdrClientContext *cliprdr,
const CLIPRDR_FORMAT_DATA_REQUEST *formatDataRequest)
{
2019-11-06 17:24:51 +03:00
BYTE *data;
UINT32 size;
UINT32 formatId;
CLIPRDR_FORMAT_DATA_RESPONSE response;
2019-11-06 17:24:51 +03:00
mfContext *mfc = (mfContext *)cliprdr->custom;
ZeroMemory(&response, sizeof(CLIPRDR_FORMAT_DATA_RESPONSE));
2019-11-06 17:24:51 +03:00
formatId = formatDataRequest->requestedFormatId;
2019-11-06 17:24:51 +03:00
data = (BYTE *)ClipboardGetData(mfc->clipboard, formatId, &size);
response.msgFlags = CB_RESPONSE_OK;
response.dataLen = size;
response.requestedFormatData = data;
2019-11-06 17:24:51 +03:00
if (!data)
{
response.msgFlags = CB_RESPONSE_FAIL;
response.dataLen = 0;
response.requestedFormatData = NULL;
}
2019-11-06 17:24:51 +03:00
cliprdr->ClientFormatDataResponse(cliprdr, &response);
2019-11-06 17:24:51 +03:00
free(data);
2019-11-06 17:24:51 +03:00
return CHANNEL_RC_OK;
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
2019-11-06 17:24:51 +03:00
static UINT
mac_cliprdr_server_format_data_response(CliprdrClientContext *cliprdr,
const CLIPRDR_FORMAT_DATA_RESPONSE *formatDataResponse)
{
2019-11-06 17:24:51 +03:00
BYTE *data;
UINT32 size;
UINT32 index;
UINT32 formatId;
2019-11-06 17:24:51 +03:00
CLIPRDR_FORMAT *format = NULL;
mfContext *mfc = (mfContext *)cliprdr->custom;
MRDPView *view = (MRDPView *)mfc->view;
if (formatDataResponse->msgFlags & CB_RESPONSE_FAIL)
{
SetEvent(mfc->clipboardRequestEvent);
return ERROR_INTERNAL_ERROR;
}
2019-11-06 17:24:51 +03:00
for (index = 0; index < mfc->numServerFormats; index++)
{
if (mfc->requestedFormatId == mfc->serverFormats[index].formatId)
format = &(mfc->serverFormats[index]);
}
2019-11-06 17:24:51 +03:00
if (!format)
{
SetEvent(mfc->clipboardRequestEvent);
return ERROR_INTERNAL_ERROR;
}
2019-11-06 17:24:51 +03:00
if (format->formatName)
formatId = ClipboardRegisterFormat(mfc->clipboard, format->formatName);
else
formatId = format->formatId;
2019-11-06 17:24:51 +03:00
size = formatDataResponse->dataLen;
2019-11-06 17:24:51 +03:00
2016-10-06 14:31:25 +03:00
ClipboardSetData(mfc->clipboard, formatId, formatDataResponse->requestedFormatData, size);
2019-11-06 17:24:51 +03:00
SetEvent(mfc->clipboardRequestEvent);
2019-11-06 17:24:51 +03:00
if ((formatId == CF_TEXT) || (formatId == CF_OEMTEXT) || (formatId == CF_UNICODETEXT))
{
formatId = ClipboardRegisterFormat(mfc->clipboard, "UTF8_STRING");
2019-11-06 17:24:51 +03:00
data = (void *)ClipboardGetData(mfc->clipboard, formatId, &size);
if (size > 1)
size--; /* we need the size without the null terminator */
2019-11-06 17:24:51 +03:00
NSString *str = [[NSString alloc] initWithBytes:(void *)data
length:size
encoding:NSUTF8StringEncoding];
free(data);
2019-11-06 17:24:51 +03:00
2022-02-01 18:19:43 +03:00
NSArray *types = [[NSArray alloc] initWithObjects:NSPasteboardTypeString, nil];
[view->pasteboard_wr declareTypes:types owner:view];
2022-02-01 18:19:43 +03:00
[view->pasteboard_wr setString:str forType:NSPasteboardTypeString];
}
2019-11-06 17:24:51 +03:00
return CHANNEL_RC_OK;
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
2019-11-06 17:24:51 +03:00
static UINT
mac_cliprdr_server_file_contents_request(CliprdrClientContext *cliprdr,
const CLIPRDR_FILE_CONTENTS_REQUEST *fileContentsRequest)
{
return CHANNEL_RC_OK;
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
2019-11-06 17:24:51 +03:00
static UINT mac_cliprdr_server_file_contents_response(
CliprdrClientContext *cliprdr, const CLIPRDR_FILE_CONTENTS_RESPONSE *fileContentsResponse)
{
return CHANNEL_RC_OK;
}
2019-11-06 17:24:51 +03:00
void mac_cliprdr_init(mfContext *mfc, CliprdrClientContext *cliprdr)
{
2019-11-06 17:24:51 +03:00
cliprdr->custom = (void *)mfc;
mfc->cliprdr = cliprdr;
2019-11-06 17:24:51 +03:00
mfc->clipboard = ClipboardCreate();
mfc->clipboardRequestEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
2019-11-06 17:24:51 +03:00
cliprdr->MonitorReady = mac_cliprdr_monitor_ready;
cliprdr->ServerCapabilities = mac_cliprdr_server_capabilities;
cliprdr->ServerFormatList = mac_cliprdr_server_format_list;
cliprdr->ServerFormatListResponse = mac_cliprdr_server_format_list_response;
cliprdr->ServerLockClipboardData = mac_cliprdr_server_lock_clipboard_data;
cliprdr->ServerUnlockClipboardData = mac_cliprdr_server_unlock_clipboard_data;
cliprdr->ServerFormatDataRequest = mac_cliprdr_server_format_data_request;
cliprdr->ServerFormatDataResponse = mac_cliprdr_server_format_data_response;
cliprdr->ServerFileContentsRequest = mac_cliprdr_server_file_contents_request;
cliprdr->ServerFileContentsResponse = mac_cliprdr_server_file_contents_response;
}
2019-11-06 17:24:51 +03:00
void mac_cliprdr_uninit(mfContext *mfc, CliprdrClientContext *cliprdr)
{
cliprdr->custom = NULL;
mfc->cliprdr = NULL;
2019-11-06 17:24:51 +03:00
ClipboardDestroy(mfc->clipboard);
CloseHandle(mfc->clipboardRequestEvent);
}