FreeRDP/client/Wayland/wlf_cliprdr.c

1019 lines
26 KiB
C
Raw Normal View History

2018-12-19 18:18:13 +03:00
/**
* FreeRDP: A Remote Desktop Protocol Implementation
* Wayland Clipboard Redirection
*
* Copyright 2018 Armin Novak <armin.novak@thincast.com>
* Copyright 2018 Thincast Technologies GmbH
*
* 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.
*/
2022-02-16 13:20:38 +03:00
#include <freerdp/config.h>
2018-12-19 18:18:13 +03:00
#include <stdlib.h>
#include <winpr/crt.h>
#include <winpr/image.h>
#include <winpr/stream.h>
#include <winpr/clipboard.h>
#include <freerdp/log.h>
#include <freerdp/client/cliprdr.h>
#include <freerdp/channels/channels.h>
#include <freerdp/channels/cliprdr.h>
2023-02-27 13:15:21 +03:00
#include <freerdp/client/client_cliprdr_file.h>
2018-12-19 18:18:13 +03:00
#include "wlf_cliprdr.h"
#define TAG CLIENT_TAG("wayland.cliprdr")
2019-11-06 17:24:51 +03:00
#define MAX_CLIPBOARD_FORMATS 255
2018-12-19 18:18:13 +03:00
2023-02-27 13:15:21 +03:00
#define mime_text_plain "text/plain"
#define mime_text_utf8 mime_text_plain ";charset=utf-8"
static const char* mime_text[] = { mime_text_plain, mime_text_utf8, "UTF8_STRING",
"COMPOUND_TEXT", "TEXT", "STRING" };
2018-12-19 18:18:13 +03:00
static const char mime_png[] = "image/png";
static const char mime_webp[] = "image/webp";
static const char mime_jpg[] = "image/jpeg";
static const char mime_tiff[] = "image/tiff";
static const char mime_uri_list[] = "text/uri-list";
static const char mime_html[] = "text/html";
#define BMP_MIME_LIST "image/bmp", "image/x-bmp", "image/x-MS-bmp", "image/x-win-bitmap"
static const char* mime_bitmap[] = { BMP_MIME_LIST };
static const char* mime_image[] = { mime_png, mime_webp, mime_jpg, mime_tiff, BMP_MIME_LIST };
2023-02-27 13:15:21 +03:00
static const char mime_gnome_copied_files[] = "x-special/gnome-copied-files";
static const char mime_mate_copied_files[] = "x-special/mate-copied-files";
2023-02-27 13:15:21 +03:00
static const char type_FileGroupDescriptorW[] = "FileGroupDescriptorW";
static const char type_HtmlFormat[] = "HTML Format";
2023-02-27 13:15:21 +03:00
typedef struct
{
FILE* responseFile;
UINT32 responseFormat;
char* responseMime;
} wlf_request;
2018-12-19 18:18:13 +03:00
typedef struct
{
const FILE* responseFile;
UINT32 responseFormat;
const char* responseMime;
} wlf_const_request;
2018-12-19 18:18:13 +03:00
struct wlf_clipboard
{
wlfContext* wfc;
rdpChannels* channels;
CliprdrClientContext* context;
wLog* log;
UwacSeat* seat;
wClipboard* system;
size_t numClientFormats;
CLIPRDR_FORMAT* clientFormats;
size_t numServerFormats;
CLIPRDR_FORMAT* serverFormats;
BOOL sync;
CRITICAL_SECTION lock;
2023-02-27 13:15:21 +03:00
CliprdrFileContext* file;
wQueue* request_queue;
2018-12-19 18:18:13 +03:00
};
static void wlf_request_free(void* rq)
2023-02-27 13:15:21 +03:00
{
wlf_request* request = rq;
if (request)
{
free(request->responseMime);
if (request->responseFile)
fclose(request->responseFile);
}
free(request);
}
static wlf_request* wlf_request_new(void)
2023-02-27 13:15:21 +03:00
{
return calloc(1, sizeof(wlf_request));
}
static void* wlf_request_clone(const void* oth)
2023-02-27 13:15:21 +03:00
{
const wlf_request* other = (const wlf_request*)oth;
2023-02-27 13:15:21 +03:00
wlf_request* copy = wlf_request_new();
if (!copy)
return NULL;
*copy = *other;
if (other->responseMime)
{
copy->responseMime = _strdup(other->responseMime);
if (!copy->responseMime)
goto fail;
}
return copy;
fail:
wlf_request_free(copy);
return NULL;
}
static BOOL wlf_mime_is_file(const char* mime)
{
if (strncmp(mime_uri_list, mime, sizeof(mime_uri_list)) == 0)
return TRUE;
if (strncmp(mime_gnome_copied_files, mime, sizeof(mime_gnome_copied_files)) == 0)
return TRUE;
if (strncmp(mime_mate_copied_files, mime, sizeof(mime_mate_copied_files)) == 0)
return TRUE;
return FALSE;
}
2018-12-19 18:18:13 +03:00
static BOOL wlf_mime_is_text(const char* mime)
{
for (size_t x = 0; x < ARRAYSIZE(mime_text); x++)
2018-12-19 18:18:13 +03:00
{
if (strcmp(mime, mime_text[x]) == 0)
return TRUE;
}
return FALSE;
}
static BOOL wlf_mime_is_image(const char* mime)
{
for (size_t x = 0; x < ARRAYSIZE(mime_image); x++)
2018-12-19 18:18:13 +03:00
{
if (strcmp(mime, mime_image[x]) == 0)
return TRUE;
}
return FALSE;
}
static BOOL wlf_mime_is_html(const char* mime)
{
2023-02-27 13:15:21 +03:00
if (strcmp(mime, mime_html) == 0)
return TRUE;
2018-12-19 18:18:13 +03:00
return FALSE;
}
static void wlf_cliprdr_free_server_formats(wfClipboard* clipboard)
{
if (clipboard && clipboard->serverFormats)
{
2023-02-27 13:15:21 +03:00
for (size_t j = 0; j < clipboard->numServerFormats; j++)
2018-12-19 18:18:13 +03:00
{
CLIPRDR_FORMAT* format = &clipboard->serverFormats[j];
free(format->formatName);
}
free(clipboard->serverFormats);
clipboard->serverFormats = NULL;
clipboard->numServerFormats = 0;
}
if (clipboard)
UwacClipboardOfferDestroy(clipboard->seat);
2018-12-19 18:18:13 +03:00
}
static void wlf_cliprdr_free_client_formats(wfClipboard* clipboard)
{
if (clipboard && clipboard->numClientFormats)
{
2023-02-27 13:15:21 +03:00
for (size_t j = 0; j < clipboard->numClientFormats; j++)
2018-12-19 18:18:13 +03:00
{
CLIPRDR_FORMAT* format = &clipboard->clientFormats[j];
free(format->formatName);
}
free(clipboard->clientFormats);
clipboard->clientFormats = NULL;
clipboard->numClientFormats = 0;
}
if (clipboard)
UwacClipboardOfferDestroy(clipboard->seat);
2018-12-19 18:18:13 +03:00
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
static UINT wlf_cliprdr_send_client_format_list(wfClipboard* clipboard)
{
2022-07-01 12:04:22 +03:00
WINPR_ASSERT(clipboard);
2023-02-27 13:15:21 +03:00
const CLIPRDR_FORMAT_LIST formatList = { .common.msgFlags = 0,
2023-02-27 13:15:21 +03:00
.numFormats = (UINT32)clipboard->numClientFormats,
.formats = clipboard->clientFormats,
.common.msgType = CB_FORMAT_LIST };
cliprdr_file_context_clear(clipboard->file);
WLog_VRB(TAG, "-------------- client format list [%" PRIu32 "] ------------------",
formatList.numFormats);
for (UINT32 x = 0; x < formatList.numFormats; x++)
{
const CLIPRDR_FORMAT* format = &formatList.formats[x];
WLog_VRB(TAG, "client announces %" PRIu32 " [%s][%s]", format->formatId,
ClipboardGetFormatIdString(format->formatId), format->formatName);
}
2022-07-01 12:04:22 +03:00
WINPR_ASSERT(clipboard->context);
WINPR_ASSERT(clipboard->context->ClientFormatList);
2018-12-19 18:18:13 +03:00
return clipboard->context->ClientFormatList(clipboard->context, &formatList);
}
static void wfl_cliprdr_add_client_format_id(wfClipboard* clipboard, UINT32 formatId)
{
2023-02-27 13:15:21 +03:00
CLIPRDR_FORMAT* format = NULL;
2018-12-19 18:18:13 +03:00
const char* name = ClipboardGetFormatName(clipboard->system, formatId);
2023-02-27 13:15:21 +03:00
for (size_t x = 0; x < clipboard->numClientFormats; x++)
2018-12-19 18:18:13 +03:00
{
format = &clipboard->clientFormats[x];
2018-12-19 18:18:13 +03:00
if (format->formatId == formatId)
return;
}
format = realloc(clipboard->clientFormats,
(clipboard->numClientFormats + 1) * sizeof(CLIPRDR_FORMAT));
2018-12-19 18:18:13 +03:00
if (!format)
return;
clipboard->clientFormats = format;
format = &clipboard->clientFormats[clipboard->numClientFormats++];
format->formatId = formatId;
format->formatName = NULL;
2018-12-19 18:18:13 +03:00
if (name && (formatId >= CF_MAX))
format->formatName = _strdup(name);
}
2023-02-27 13:15:21 +03:00
static BOOL wlf_cliprdr_add_client_format(wfClipboard* clipboard, const char* mime)
2018-12-19 18:18:13 +03:00
{
2023-02-27 13:15:21 +03:00
WINPR_ASSERT(mime);
2023-03-22 11:53:30 +03:00
ClipboardLock(clipboard->system);
2018-12-19 18:18:13 +03:00
if (wlf_mime_is_html(mime))
{
2023-02-27 13:15:21 +03:00
UINT32 formatId = ClipboardGetFormatId(clipboard->system, type_HtmlFormat);
2018-12-19 18:18:13 +03:00
wfl_cliprdr_add_client_format_id(clipboard, formatId);
}
else if (wlf_mime_is_text(mime))
{
wfl_cliprdr_add_client_format_id(clipboard, CF_TEXT);
wfl_cliprdr_add_client_format_id(clipboard, CF_OEMTEXT);
wfl_cliprdr_add_client_format_id(clipboard, CF_UNICODETEXT);
}
else if (wlf_mime_is_image(mime))
{
for (size_t x = 0; x < ARRAYSIZE(mime_image); x++)
{
const char* mime_bmp = mime_image[x];
UINT32 formatId = ClipboardGetFormatId(clipboard->system, mime_bmp);
if (formatId != 0)
wfl_cliprdr_add_client_format_id(clipboard, formatId);
}
2018-12-19 18:18:13 +03:00
wfl_cliprdr_add_client_format_id(clipboard, CF_DIB);
wfl_cliprdr_add_client_format_id(clipboard, CF_TIFF);
2018-12-19 18:18:13 +03:00
}
2023-02-27 13:15:21 +03:00
else if (wlf_mime_is_file(mime))
{
const UINT32 fileFormatId =
ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW);
wfl_cliprdr_add_client_format_id(clipboard, fileFormatId);
}
2023-03-22 11:53:30 +03:00
ClipboardUnlock(clipboard->system);
2023-02-27 13:15:21 +03:00
if (wlf_cliprdr_send_client_format_list(clipboard) != CHANNEL_RC_OK)
return FALSE;
return TRUE;
2018-12-19 18:18:13 +03:00
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
static UINT wlf_cliprdr_send_data_request(wfClipboard* clipboard, const wlf_const_request* rq)
2018-12-19 18:18:13 +03:00
{
2023-02-27 13:15:21 +03:00
WINPR_ASSERT(rq);
CLIPRDR_FORMAT_DATA_REQUEST request = { .requestedFormatId = rq->responseFormat };
if (!Queue_Enqueue(clipboard->request_queue, rq))
return ERROR_INTERNAL_ERROR;
2022-07-01 12:04:22 +03:00
WINPR_ASSERT(clipboard);
WINPR_ASSERT(clipboard->context);
WINPR_ASSERT(clipboard->context->ClientFormatDataRequest);
2019-11-06 17:24:51 +03:00
return clipboard->context->ClientFormatDataRequest(clipboard->context, &request);
2018-12-19 18:18:13 +03:00
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
2019-11-06 17:24:51 +03:00
static UINT wlf_cliprdr_send_data_response(wfClipboard* clipboard, const BYTE* data, size_t size)
2018-12-19 18:18:13 +03:00
{
CLIPRDR_FORMAT_DATA_RESPONSE response = { 0 };
if (size > UINT32_MAX)
return ERROR_INVALID_PARAMETER;
2022-07-01 12:04:22 +03:00
response.common.msgFlags = (data) ? CB_RESPONSE_OK : CB_RESPONSE_FAIL;
response.common.dataLen = (UINT32)size;
2018-12-19 18:18:13 +03:00
response.requestedFormatData = data;
2022-07-01 12:04:22 +03:00
WINPR_ASSERT(clipboard);
WINPR_ASSERT(clipboard->context);
WINPR_ASSERT(clipboard->context->ClientFormatDataResponse);
2019-11-06 17:24:51 +03:00
return clipboard->context->ClientFormatDataResponse(clipboard->context, &response);
2018-12-19 18:18:13 +03:00
}
BOOL wlf_cliprdr_handle_event(wfClipboard* clipboard, const UwacClipboardEvent* event)
{
if (!clipboard || !event)
return FALSE;
if (!clipboard->context)
return TRUE;
2018-12-19 18:18:13 +03:00
switch (event->type)
{
case UWAC_EVENT_CLIPBOARD_AVAILABLE:
clipboard->seat = event->seat;
return TRUE;
case UWAC_EVENT_CLIPBOARD_OFFER:
WLog_Print(clipboard->log, WLOG_DEBUG, "client announces mime %s", event->mime);
2023-02-27 13:15:21 +03:00
return wlf_cliprdr_add_client_format(clipboard, event->mime);
2018-12-19 18:18:13 +03:00
case UWAC_EVENT_CLIPBOARD_SELECT:
WLog_Print(clipboard->log, WLOG_DEBUG, "client announces new data");
wlf_cliprdr_free_client_formats(clipboard);
return TRUE;
default:
return FALSE;
}
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
static UINT wlf_cliprdr_send_client_capabilities(wfClipboard* clipboard)
{
2023-02-27 13:15:21 +03:00
WINPR_ASSERT(clipboard);
2018-12-19 18:18:13 +03:00
CLIPRDR_GENERAL_CAPABILITY_SET generalCapabilitySet = {
2023-02-27 13:15:21 +03:00
.capabilitySetType = CB_CAPSTYPE_GENERAL,
.capabilitySetLength = 12,
.version = CB_CAPS_VERSION_2,
.generalFlags =
CB_USE_LONG_FORMAT_NAMES | cliprdr_file_context_current_flags(clipboard->file)
};
CLIPRDR_CAPABILITIES capabilities = { .cCapabilitiesSets = 1,
.capabilitySets =
(CLIPRDR_CAPABILITY_SET*)&(generalCapabilitySet) };
2018-12-19 18:18:13 +03:00
2022-07-01 12:04:22 +03:00
WINPR_ASSERT(clipboard);
WINPR_ASSERT(clipboard->context);
WINPR_ASSERT(clipboard->context->ClientCapabilities);
2019-11-06 17:24:51 +03:00
return clipboard->context->ClientCapabilities(clipboard->context, &capabilities);
2018-12-19 18:18:13 +03:00
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
2019-11-06 17:24:51 +03:00
static UINT wlf_cliprdr_send_client_format_list_response(wfClipboard* clipboard, BOOL status)
2018-12-19 18:18:13 +03:00
{
2023-02-27 13:15:21 +03:00
const CLIPRDR_FORMAT_LIST_RESPONSE formatListResponse = {
.common.msgType = CB_FORMAT_LIST_RESPONSE,
.common.msgFlags = status ? CB_RESPONSE_OK : CB_RESPONSE_FAIL,
.common.dataLen = 0
};
2022-07-01 12:04:22 +03:00
WINPR_ASSERT(clipboard);
WINPR_ASSERT(clipboard->context);
WINPR_ASSERT(clipboard->context->ClientFormatListResponse);
2019-11-06 17:24:51 +03:00
return clipboard->context->ClientFormatListResponse(clipboard->context, &formatListResponse);
2018-12-19 18:18:13 +03:00
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
static UINT wlf_cliprdr_monitor_ready(CliprdrClientContext* context,
const CLIPRDR_MONITOR_READY* monitorReady)
{
UINT ret = 0;
WINPR_UNUSED(monitorReady);
WINPR_ASSERT(context);
WINPR_ASSERT(monitorReady);
2023-02-27 13:15:21 +03:00
wfClipboard* clipboard = cliprdr_file_context_get_context(context->custom);
WINPR_ASSERT(clipboard);
2018-12-19 18:18:13 +03:00
if ((ret = wlf_cliprdr_send_client_capabilities(clipboard)) != CHANNEL_RC_OK)
return ret;
if ((ret = wlf_cliprdr_send_client_format_list(clipboard)) != CHANNEL_RC_OK)
return ret;
clipboard->sync = TRUE;
return CHANNEL_RC_OK;
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
static UINT wlf_cliprdr_server_capabilities(CliprdrClientContext* context,
2019-11-06 17:24:51 +03:00
const CLIPRDR_CAPABILITIES* capabilities)
2018-12-19 18:18:13 +03:00
{
WINPR_ASSERT(context);
WINPR_ASSERT(capabilities);
2023-02-27 13:15:21 +03:00
const BYTE* capsPtr = (const BYTE*)capabilities->capabilitySets;
WINPR_ASSERT(capsPtr);
2023-02-27 13:15:21 +03:00
wfClipboard* clipboard = cliprdr_file_context_get_context(context->custom);
WINPR_ASSERT(clipboard);
2023-02-27 13:15:21 +03:00
if (!cliprdr_file_context_remote_set_flags(clipboard->file, 0))
return ERROR_INTERNAL_ERROR;
2018-12-19 18:18:13 +03:00
2023-02-27 13:15:21 +03:00
for (UINT32 i = 0; i < capabilities->cCapabilitiesSets; i++)
2018-12-19 18:18:13 +03:00
{
2019-11-06 17:24:51 +03:00
const CLIPRDR_CAPABILITY_SET* caps = (const CLIPRDR_CAPABILITY_SET*)capsPtr;
2018-12-19 18:18:13 +03:00
if (caps->capabilitySetType == CB_CAPSTYPE_GENERAL)
{
2019-11-06 17:24:51 +03:00
const CLIPRDR_GENERAL_CAPABILITY_SET* generalCaps =
(const CLIPRDR_GENERAL_CAPABILITY_SET*)caps;
2018-12-19 18:18:13 +03:00
2023-02-27 13:15:21 +03:00
if (!cliprdr_file_context_remote_set_flags(clipboard->file, generalCaps->generalFlags))
return ERROR_INTERNAL_ERROR;
2018-12-19 18:18:13 +03:00
}
capsPtr += caps->capabilitySetLength;
}
return CHANNEL_RC_OK;
}
2023-02-27 13:15:21 +03:00
static UINT32 wlf_get_server_format_id(const wfClipboard* clipboard, const char* name)
{
WINPR_ASSERT(clipboard);
WINPR_ASSERT(name);
for (UINT32 x = 0; x < clipboard->numServerFormats; x++)
{
const CLIPRDR_FORMAT* format = &clipboard->serverFormats[x];
if (!format->formatName)
continue;
if (strcmp(name, format->formatName) == 0)
return format->formatId;
}
return 0;
}
static const char* wlf_get_server_format_name(const wfClipboard* clipboard, UINT32 formatId)
{
WINPR_ASSERT(clipboard);
for (UINT32 x = 0; x < clipboard->numServerFormats; x++)
{
const CLIPRDR_FORMAT* format = &clipboard->serverFormats[x];
if (format->formatId == formatId)
return format->formatName;
}
return NULL;
}
2018-12-19 18:18:13 +03:00
static void wlf_cliprdr_transfer_data(UwacSeat* seat, void* context, const char* mime, int fd)
{
wfClipboard* clipboard = (wfClipboard*)context;
WINPR_UNUSED(seat);
EnterCriticalSection(&clipboard->lock);
2018-12-19 18:18:13 +03:00
wlf_const_request request = { 0 };
2023-02-27 13:15:21 +03:00
if (wlf_mime_is_html(mime))
2018-12-19 18:18:13 +03:00
{
2023-02-27 13:15:21 +03:00
request.responseMime = mime_html;
request.responseFormat = wlf_get_server_format_id(clipboard, type_HtmlFormat);
2018-12-19 18:18:13 +03:00
}
2023-02-27 13:15:21 +03:00
else if (wlf_mime_is_file(mime))
2018-12-19 18:18:13 +03:00
{
2023-02-27 13:15:21 +03:00
request.responseMime = mime;
request.responseFormat = wlf_get_server_format_id(clipboard, type_FileGroupDescriptorW);
2018-12-19 18:18:13 +03:00
}
2023-02-27 13:15:21 +03:00
else if (wlf_mime_is_text(mime))
2018-12-19 18:18:13 +03:00
{
2023-02-27 13:15:21 +03:00
request.responseMime = mime_text_plain;
request.responseFormat = CF_UNICODETEXT;
}
else if (wlf_mime_is_image(mime))
{
request.responseMime = mime;
if (strcmp(mime, mime_tiff) == 0)
request.responseFormat = CF_TIFF;
else
request.responseFormat = CF_DIB;
2018-12-19 18:18:13 +03:00
}
2023-02-27 13:15:21 +03:00
if (request.responseMime != NULL)
2018-12-19 18:18:13 +03:00
{
2023-02-27 13:15:21 +03:00
request.responseFile = fdopen(fd, "w");
2018-12-19 18:18:13 +03:00
2023-02-27 13:15:21 +03:00
if (request.responseFile)
wlf_cliprdr_send_data_request(clipboard, &request);
else
2019-11-06 17:24:51 +03:00
WLog_Print(clipboard->log, WLOG_ERROR,
"failed to open clipboard file descriptor for MIME %s",
2023-02-27 13:15:21 +03:00
request.responseMime);
2018-12-19 18:18:13 +03:00
}
2023-02-27 13:15:21 +03:00
LeaveCriticalSection(&clipboard->lock);
2018-12-19 18:18:13 +03:00
}
static void wlf_cliprdr_cancel_data(UwacSeat* seat, void* context)
{
2023-02-27 13:15:21 +03:00
wfClipboard* clipboard = (wfClipboard*)context;
2018-12-19 18:18:13 +03:00
WINPR_UNUSED(seat);
2023-02-27 13:15:21 +03:00
WINPR_ASSERT(clipboard);
cliprdr_file_context_clear(clipboard->file);
2018-12-19 18:18:13 +03:00
}
/**
* Called when the clipboard changes server side.
*
* Clear the local clipboard offer and replace it with a new one
* that announces the formats we get listed here.
*
* @return 0 on success, otherwise a Win32 error code
*/
static UINT wlf_cliprdr_server_format_list(CliprdrClientContext* context,
2019-11-06 17:24:51 +03:00
const CLIPRDR_FORMAT_LIST* formatList)
2018-12-19 18:18:13 +03:00
{
BOOL html = FALSE;
BOOL text = FALSE;
BOOL image = FALSE;
2023-02-27 13:15:21 +03:00
BOOL file = FALSE;
2018-12-19 18:18:13 +03:00
if (!context || !context->custom)
return ERROR_INVALID_PARAMETER;
2023-02-27 13:15:21 +03:00
wfClipboard* clipboard = cliprdr_file_context_get_context(context->custom);
WINPR_ASSERT(clipboard);
2018-12-19 18:18:13 +03:00
wlf_cliprdr_free_server_formats(clipboard);
2023-02-27 13:15:21 +03:00
cliprdr_file_context_clear(clipboard->file);
2018-12-19 18:18:13 +03:00
2019-11-06 17:24:51 +03:00
if (!(clipboard->serverFormats =
(CLIPRDR_FORMAT*)calloc(formatList->numFormats, sizeof(CLIPRDR_FORMAT))))
2018-12-19 18:18:13 +03:00
{
2019-11-06 17:24:51 +03:00
WLog_Print(clipboard->log, WLOG_ERROR,
"failed to allocate %" PRIuz " CLIPRDR_FORMAT structs",
2018-12-19 18:18:13 +03:00
clipboard->numServerFormats);
return CHANNEL_RC_NO_MEMORY;
}
clipboard->numServerFormats = formatList->numFormats;
if (!clipboard->seat)
{
2019-11-06 17:24:51 +03:00
WLog_Print(clipboard->log, WLOG_ERROR,
"clipboard->seat=NULL, check your client implementation");
2018-12-19 18:18:13 +03:00
return ERROR_INTERNAL_ERROR;
}
2018-12-19 18:18:13 +03:00
2023-02-27 13:15:21 +03:00
for (UINT32 i = 0; i < formatList->numFormats; i++)
2018-12-19 18:18:13 +03:00
{
const CLIPRDR_FORMAT* format = &formatList->formats[i];
CLIPRDR_FORMAT* srvFormat = &clipboard->serverFormats[i];
srvFormat->formatId = format->formatId;
if (format->formatName)
{
srvFormat->formatName = _strdup(format->formatName);
if (!srvFormat->formatName)
{
wlf_cliprdr_free_server_formats(clipboard);
return CHANNEL_RC_NO_MEMORY;
}
}
if (format->formatName)
{
2023-02-27 13:15:21 +03:00
if (strcmp(format->formatName, type_HtmlFormat) == 0)
2018-12-19 18:18:13 +03:00
{
text = TRUE;
html = TRUE;
}
2023-02-27 13:15:21 +03:00
else if (strcmp(format->formatName, type_FileGroupDescriptorW) == 0)
{
file = TRUE;
text = TRUE;
}
2018-12-19 18:18:13 +03:00
}
else
{
switch (format->formatId)
{
case CF_TEXT:
case CF_OEMTEXT:
case CF_UNICODETEXT:
text = TRUE;
break;
case CF_DIB:
image = TRUE;
break;
default:
break;
}
}
}
if (html)
{
2023-02-27 13:15:21 +03:00
UwacClipboardOfferCreate(clipboard->seat, mime_html);
}
2018-12-19 18:18:13 +03:00
if (file && cliprdr_file_context_has_local_support(clipboard->file))
2023-02-27 13:15:21 +03:00
{
UwacClipboardOfferCreate(clipboard->seat, mime_uri_list);
UwacClipboardOfferCreate(clipboard->seat, mime_gnome_copied_files);
UwacClipboardOfferCreate(clipboard->seat, mime_mate_copied_files);
2018-12-19 18:18:13 +03:00
}
if (text)
{
for (size_t x = 0; x < ARRAYSIZE(mime_text); x++)
2018-12-19 18:18:13 +03:00
UwacClipboardOfferCreate(clipboard->seat, mime_text[x]);
}
if (image)
{
2023-02-27 13:15:21 +03:00
for (size_t x = 0; x < ARRAYSIZE(mime_image); x++)
2018-12-19 18:18:13 +03:00
UwacClipboardOfferCreate(clipboard->seat, mime_image[x]);
}
UwacClipboardOfferAnnounce(clipboard->seat, clipboard, wlf_cliprdr_transfer_data,
wlf_cliprdr_cancel_data);
return wlf_cliprdr_send_client_format_list_response(clipboard, TRUE);
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
2019-11-06 17:24:51 +03:00
static UINT
wlf_cliprdr_server_format_list_response(CliprdrClientContext* context,
const CLIPRDR_FORMAT_LIST_RESPONSE* formatListResponse)
2018-12-19 18:18:13 +03:00
{
2023-02-27 13:15:21 +03:00
WINPR_ASSERT(context);
WINPR_ASSERT(formatListResponse);
if (formatListResponse->common.msgFlags & CB_RESPONSE_FAIL)
WLog_WARN(TAG, "format list update failed");
2018-12-19 18:18:13 +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
wlf_cliprdr_server_format_data_request(CliprdrClientContext* context,
const CLIPRDR_FORMAT_DATA_REQUEST* formatDataRequest)
2018-12-19 18:18:13 +03:00
{
2019-02-07 16:38:55 +03:00
UINT rc = CHANNEL_RC_OK;
char* data = NULL;
2023-02-27 13:15:21 +03:00
size_t size = 0;
const char* mime = NULL;
UINT32 formatId = 0;
UINT32 localFormatId = 0;
wfClipboard* clipboard = 0;
UINT32 dsize = 0;
BYTE* ddata = NULL;
WINPR_ASSERT(context);
WINPR_ASSERT(formatDataRequest);
2023-02-27 13:15:21 +03:00
localFormatId = formatId = formatDataRequest->requestedFormatId;
clipboard = cliprdr_file_context_get_context(context->custom);
WINPR_ASSERT(clipboard);
2018-12-19 18:18:13 +03:00
2023-03-22 11:53:30 +03:00
ClipboardLock(clipboard->system);
2023-02-27 13:15:21 +03:00
const UINT32 fileFormatId = ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW);
const UINT32 htmlFormatId = ClipboardGetFormatId(clipboard->system, type_HtmlFormat);
switch (formatId)
2018-12-19 18:18:13 +03:00
{
case CF_TEXT:
case CF_OEMTEXT:
case CF_UNICODETEXT:
2023-02-27 13:15:21 +03:00
localFormatId = ClipboardGetFormatId(clipboard->system, mime_text_plain);
mime = mime_text_utf8;
2018-12-19 18:18:13 +03:00
break;
2018-12-19 18:18:13 +03:00
case CF_DIB:
case CF_DIBV5:
mime = mime_bitmap[0];
break;
case CF_TIFF:
mime = mime_tiff;
2018-12-19 18:18:13 +03:00
break;
2018-12-19 18:18:13 +03:00
default:
2023-02-27 13:15:21 +03:00
if (formatId == fileFormatId)
{
localFormatId = ClipboardGetFormatId(clipboard->system, mime_uri_list);
mime = mime_uri_list;
}
else if (formatId == htmlFormatId)
{
localFormatId = ClipboardGetFormatId(clipboard->system, mime_html);
mime = mime_html;
}
2018-12-19 18:18:13 +03:00
else
2023-02-27 13:15:21 +03:00
goto fail;
2018-12-19 18:18:13 +03:00
break;
}
data = UwacClipboardDataGet(clipboard->seat, mime, &size);
2018-12-19 18:18:13 +03:00
if (!data)
2023-02-27 13:15:21 +03:00
goto fail;
2018-12-19 18:18:13 +03:00
2023-02-27 13:15:21 +03:00
if (fileFormatId == formatId)
2018-12-19 18:18:13 +03:00
{
2023-02-27 13:15:21 +03:00
if (!cliprdr_file_context_update_client_data(clipboard->file, data, size))
goto fail;
}
2023-02-27 13:15:21 +03:00
const BOOL res = ClipboardSetData(clipboard->system, localFormatId, data, size);
free(data);
2018-12-19 18:18:13 +03:00
2023-02-27 13:15:21 +03:00
UINT32 len = 0;
data = NULL;
if (res)
data = ClipboardGetData(clipboard->system, formatId, &len);
if (!res || !data)
2023-02-27 13:15:21 +03:00
goto fail;
2018-12-19 18:18:13 +03:00
2023-02-27 13:15:21 +03:00
if (fileFormatId == formatId)
{
2023-02-27 13:15:21 +03:00
const UINT32 flags = cliprdr_file_context_remote_get_flags(clipboard->file);
const UINT32 error = cliprdr_serialize_file_list_ex(
flags, (const FILEDESCRIPTORW*)data, len / sizeof(FILEDESCRIPTORW), &ddata, &dsize);
if (error)
goto fail;
}
2023-02-27 13:15:21 +03:00
fail:
2023-03-22 11:53:30 +03:00
ClipboardUnlock(clipboard->system);
2023-02-27 13:15:21 +03:00
rc = wlf_cliprdr_send_data_response(clipboard, ddata, dsize);
2018-12-19 18:18:13 +03:00
free(data);
return rc;
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
2019-11-06 17:24:51 +03:00
static UINT
wlf_cliprdr_server_format_data_response(CliprdrClientContext* context,
const CLIPRDR_FORMAT_DATA_RESPONSE* formatDataResponse)
2018-12-19 18:18:13 +03:00
{
UINT rc = ERROR_INTERNAL_ERROR;
WINPR_ASSERT(context);
WINPR_ASSERT(formatDataResponse);
2023-02-27 13:15:21 +03:00
const UINT32 size = formatDataResponse->common.dataLen;
const BYTE* data = formatDataResponse->requestedFormatData;
2023-02-27 13:15:21 +03:00
wfClipboard* clipboard = cliprdr_file_context_get_context(context->custom);
WINPR_ASSERT(clipboard);
2018-12-19 18:18:13 +03:00
2023-02-27 13:15:21 +03:00
wlf_request* request = Queue_Dequeue(clipboard->request_queue);
if (!request)
goto fail;
2023-02-27 13:15:21 +03:00
rc = CHANNEL_RC_OK;
if (formatDataResponse->common.msgFlags & CB_RESPONSE_FAIL)
{
WLog_WARN(TAG, "clipboard data request for format %" PRIu32 " [%s], mime %s failed",
request->responseFormat, ClipboardGetFormatIdString(request->responseFormat),
request->responseMime);
goto fail;
}
rc = ERROR_INTERNAL_ERROR;
2023-03-22 11:53:30 +03:00
ClipboardLock(clipboard->system);
2023-02-27 13:15:21 +03:00
EnterCriticalSection(&clipboard->lock);
2024-04-11 14:19:34 +03:00
BYTE* cdata = NULL;
2023-02-27 13:15:21 +03:00
UINT32 srcFormatId = 0;
UINT32 dstFormatId = 0;
switch (request->responseFormat)
2018-12-19 18:18:13 +03:00
{
2023-02-27 13:15:21 +03:00
case CF_TEXT:
case CF_OEMTEXT:
2018-12-19 18:18:13 +03:00
case CF_UNICODETEXT:
2023-02-27 13:15:21 +03:00
srcFormatId = request->responseFormat;
dstFormatId = ClipboardGetFormatId(clipboard->system, request->responseMime);
break;
2023-02-27 13:15:21 +03:00
case CF_DIB:
case CF_DIBV5:
srcFormatId = request->responseFormat;
dstFormatId = ClipboardGetFormatId(clipboard->system, request->responseMime);
2018-12-19 18:18:13 +03:00
break;
default:
2023-02-27 13:15:21 +03:00
{
const char* name = wlf_get_server_format_name(clipboard, request->responseFormat);
if (name)
{
if (strcmp(type_FileGroupDescriptorW, name) == 0)
{
srcFormatId =
ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW);
dstFormatId = ClipboardGetFormatId(clipboard->system, request->responseMime);
if (!cliprdr_file_context_update_server_data(clipboard->file, clipboard->system,
data, size))
2023-02-27 13:15:21 +03:00
goto unlock;
}
else if (strcmp(type_HtmlFormat, name) == 0)
{
srcFormatId = ClipboardGetFormatId(clipboard->system, type_HtmlFormat);
dstFormatId = ClipboardGetFormatId(clipboard->system, request->responseMime);
}
}
}
break;
2018-12-19 18:18:13 +03:00
}
2023-02-27 13:15:21 +03:00
UINT32 len = 0;
const BOOL sres = ClipboardSetData(clipboard->system, srcFormatId, data, size);
if (sres)
2024-04-11 14:19:34 +03:00
cdata = ClipboardGetData(clipboard->system, dstFormatId, &len);
2024-04-11 14:19:34 +03:00
if (!sres || !cdata)
2023-02-27 13:15:21 +03:00
goto unlock;
if (request->responseFile)
{
2024-04-11 14:19:34 +03:00
const size_t res = fwrite(cdata, 1, len, request->responseFile);
2023-02-27 13:15:21 +03:00
if (res == len)
rc = CHANNEL_RC_OK;
}
2023-02-27 13:15:21 +03:00
else
rc = CHANNEL_RC_OK;
2023-02-27 13:15:21 +03:00
unlock:
2024-04-11 14:19:34 +03:00
free(cdata);
2023-03-22 11:53:30 +03:00
ClipboardUnlock(clipboard->system);
LeaveCriticalSection(&clipboard->lock);
2023-02-27 13:15:21 +03:00
fail:
wlf_request_free(request);
2018-12-19 18:18:13 +03:00
return rc;
}
wfClipboard* wlf_clipboard_new(wlfContext* wfc)
{
rdpChannels* channels = NULL;
wfClipboard* clipboard = NULL;
WINPR_ASSERT(wfc);
clipboard = (wfClipboard*)calloc(1, sizeof(wfClipboard));
2018-12-19 18:18:13 +03:00
if (!clipboard)
goto fail;
2018-12-19 18:18:13 +03:00
InitializeCriticalSection(&clipboard->lock);
2018-12-19 18:18:13 +03:00
clipboard->wfc = wfc;
channels = wfc->common.context.channels;
2018-12-19 18:18:13 +03:00
clipboard->log = WLog_Get(TAG);
clipboard->channels = channels;
clipboard->system = ClipboardCreate();
if (!clipboard->system)
goto fail;
2023-02-27 13:15:21 +03:00
clipboard->file = cliprdr_file_context_new(clipboard);
if (!clipboard->file)
goto fail;
if (!cliprdr_file_context_set_locally_available(clipboard->file, TRUE))
goto fail;
clipboard->request_queue = Queue_New(TRUE, -1, -1);
if (!clipboard->request_queue)
goto fail;
wObject* obj = Queue_Object(clipboard->request_queue);
WINPR_ASSERT(obj);
obj->fnObjectFree = wlf_request_free;
obj->fnObjectNew = wlf_request_clone;
2018-12-19 18:18:13 +03:00
return clipboard;
fail:
wlf_clipboard_free(clipboard);
return NULL;
2018-12-19 18:18:13 +03:00
}
void wlf_clipboard_free(wfClipboard* clipboard)
{
if (!clipboard)
return;
2023-02-27 13:15:21 +03:00
cliprdr_file_context_free(clipboard->file);
2018-12-19 18:18:13 +03:00
wlf_cliprdr_free_server_formats(clipboard);
wlf_cliprdr_free_client_formats(clipboard);
ClipboardDestroy(clipboard->system);
EnterCriticalSection(&clipboard->lock);
2023-02-27 13:15:21 +03:00
Queue_Free(clipboard->request_queue);
LeaveCriticalSection(&clipboard->lock);
DeleteCriticalSection(&clipboard->lock);
2018-12-19 18:18:13 +03:00
free(clipboard);
}
BOOL wlf_cliprdr_init(wfClipboard* clipboard, CliprdrClientContext* cliprdr)
{
2023-02-27 13:15:21 +03:00
WINPR_ASSERT(clipboard);
WINPR_ASSERT(cliprdr);
2018-12-19 18:18:13 +03:00
clipboard->context = cliprdr;
cliprdr->MonitorReady = wlf_cliprdr_monitor_ready;
cliprdr->ServerCapabilities = wlf_cliprdr_server_capabilities;
cliprdr->ServerFormatList = wlf_cliprdr_server_format_list;
cliprdr->ServerFormatListResponse = wlf_cliprdr_server_format_list_response;
cliprdr->ServerFormatDataRequest = wlf_cliprdr_server_format_data_request;
2023-02-27 13:15:21 +03:00
cliprdr->ServerFormatDataResponse = wlf_cliprdr_server_format_data_response;
2023-02-25 18:46:12 +03:00
2023-02-27 13:15:21 +03:00
return cliprdr_file_context_init(clipboard->file, cliprdr);
2018-12-19 18:18:13 +03:00
}
BOOL wlf_cliprdr_uninit(wfClipboard* clipboard, CliprdrClientContext* cliprdr)
{
2023-02-27 13:15:21 +03:00
WINPR_ASSERT(clipboard);
if (!cliprdr_file_context_uninit(clipboard->file, cliprdr))
return FALSE;
2018-12-19 18:18:13 +03:00
if (cliprdr)
cliprdr->custom = NULL;
return TRUE;
}