2013-08-17 00:46:47 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* Clipboard Virtual Channel Extension
|
|
|
|
*
|
|
|
|
* Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
2013-08-17 05:17:24 +04:00
|
|
|
#include <winpr/print.h>
|
2013-08-17 00:46:47 +04:00
|
|
|
#include <winpr/stream.h>
|
|
|
|
|
2014-08-07 21:05:48 +04:00
|
|
|
#include <freerdp/utils/debug.h>
|
2013-08-17 00:46:47 +04:00
|
|
|
#include "cliprdr_main.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialization Sequence\n
|
|
|
|
* Client Server\n
|
|
|
|
* | |\n
|
|
|
|
* |<----------------------Server Clipboard Capabilities PDU-----------------|\n
|
|
|
|
* |<-----------------------------Monitor Ready PDU--------------------------|\n
|
|
|
|
* |-----------------------Client Clipboard Capabilities PDU---------------->|\n
|
|
|
|
* |---------------------------Temporary Directory PDU---------------------->|\n
|
|
|
|
* |-------------------------------Format List PDU-------------------------->|\n
|
|
|
|
* |<--------------------------Format List Response PDU----------------------|\n
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-08-18 23:39:28 +04:00
|
|
|
/**
|
|
|
|
* Data Transfer Sequences\n
|
|
|
|
* Shared Local\n
|
|
|
|
* Clipboard Owner Clipboard Owner\n
|
|
|
|
* | |\n
|
|
|
|
* |-------------------------------------------------------------------------|\n _
|
|
|
|
* |-------------------------------Format List PDU-------------------------->|\n |
|
|
|
|
* |<--------------------------Format List Response PDU----------------------|\n _| Copy Sequence
|
|
|
|
* |<---------------------Lock Clipboard Data PDU (Optional)-----------------|\n
|
|
|
|
* |-------------------------------------------------------------------------|\n
|
|
|
|
* |-------------------------------------------------------------------------|\n _
|
|
|
|
* |<--------------------------Format Data Request PDU-----------------------|\n | Paste Sequence Palette,
|
|
|
|
* |---------------------------Format Data Response PDU--------------------->|\n _| Metafile, File List Data
|
|
|
|
* |-------------------------------------------------------------------------|\n
|
|
|
|
* |-------------------------------------------------------------------------|\n _
|
|
|
|
* |<------------------------Format Contents Request PDU---------------------|\n | Paste Sequence
|
|
|
|
* |-------------------------Format Contents Response PDU------------------->|\n _| File Stream Data
|
|
|
|
* |<---------------------Lock Clipboard Data PDU (Optional)-----------------|\n
|
|
|
|
* |-------------------------------------------------------------------------|\n
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-08-17 00:46:47 +04:00
|
|
|
static int cliprdr_server_send_capabilities(CliprdrServerContext* context)
|
|
|
|
{
|
2013-08-17 05:17:24 +04:00
|
|
|
wStream* s;
|
|
|
|
BOOL status;
|
|
|
|
UINT32 generalFlags;
|
|
|
|
CLIPRDR_HEADER header;
|
2014-05-28 19:04:24 +04:00
|
|
|
ULONG written;
|
2013-08-17 05:17:24 +04:00
|
|
|
|
2014-08-07 18:51:49 +04:00
|
|
|
DEBUG_MSG("CliprdrServerSendCapabilities\n");
|
2013-08-17 05:17:24 +04:00
|
|
|
|
|
|
|
header.msgType = CB_CLIP_CAPS;
|
|
|
|
header.msgFlags = 0;
|
2013-08-18 23:39:28 +04:00
|
|
|
header.dataLen = 16;
|
2013-08-17 05:17:24 +04:00
|
|
|
|
|
|
|
generalFlags = 0;
|
|
|
|
|
|
|
|
if (context->priv->UseLongFormatNames)
|
|
|
|
generalFlags |= CB_USE_LONG_FORMAT_NAMES;
|
|
|
|
|
2013-08-18 23:39:28 +04:00
|
|
|
s = Stream_New(NULL, header.dataLen + CLIPRDR_HEADER_LENGTH);
|
2013-08-17 05:17:24 +04:00
|
|
|
|
|
|
|
Stream_Write_UINT16(s, header.msgType); /* msgType (2 bytes) */
|
|
|
|
Stream_Write_UINT16(s, header.msgFlags); /* msgFlags (2 bytes) */
|
|
|
|
Stream_Write_UINT32(s, header.dataLen); /* dataLen (4 bytes) */
|
|
|
|
|
|
|
|
Stream_Write_UINT16(s, 1); /* cCapabilitiesSets (2 bytes) */
|
|
|
|
Stream_Write_UINT16(s, 0); /* pad1 (2 bytes) */
|
|
|
|
|
|
|
|
Stream_Write_UINT16(s, CB_CAPSTYPE_GENERAL); /* capabilitySetType (2 bytes) */
|
|
|
|
Stream_Write_UINT16(s, CB_CAPSTYPE_GENERAL_LEN); /* lengthCapability (2 bytes) */
|
|
|
|
Stream_Write_UINT32(s, CB_CAPS_VERSION_2); /* version (4 bytes) */
|
|
|
|
Stream_Write_UINT32(s, generalFlags); /* generalFlags (4 bytes) */
|
|
|
|
|
|
|
|
Stream_SealLength(s);
|
|
|
|
|
2014-05-28 19:04:24 +04:00
|
|
|
status = WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR) Stream_Buffer(s), Stream_Length(s), &written);
|
2013-08-17 05:17:24 +04:00
|
|
|
|
|
|
|
Stream_Free(s, TRUE);
|
|
|
|
|
2013-08-17 00:46:47 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cliprdr_server_send_monitor_ready(CliprdrServerContext* context)
|
|
|
|
{
|
2013-08-17 05:17:24 +04:00
|
|
|
wStream* s;
|
|
|
|
BOOL status;
|
|
|
|
CLIPRDR_HEADER header;
|
2014-05-28 19:04:24 +04:00
|
|
|
ULONG written;
|
2013-08-17 05:17:24 +04:00
|
|
|
|
2014-08-07 18:51:49 +04:00
|
|
|
DEBUG_MSG("CliprdrServerSendMonitorReady\n");
|
2013-08-17 05:17:24 +04:00
|
|
|
|
|
|
|
header.msgType = CB_MONITOR_READY;
|
|
|
|
header.msgFlags = 0;
|
2013-08-18 23:39:28 +04:00
|
|
|
header.dataLen = 0;
|
2013-08-17 05:17:24 +04:00
|
|
|
|
2013-08-18 23:39:28 +04:00
|
|
|
s = Stream_New(NULL, header.dataLen + CLIPRDR_HEADER_LENGTH);
|
2013-08-17 05:17:24 +04:00
|
|
|
|
|
|
|
Stream_Write_UINT16(s, header.msgType); /* msgType (2 bytes) */
|
|
|
|
Stream_Write_UINT16(s, header.msgFlags); /* msgFlags (2 bytes) */
|
|
|
|
Stream_Write_UINT32(s, header.dataLen); /* dataLen (4 bytes) */
|
|
|
|
|
|
|
|
Stream_SealLength(s);
|
|
|
|
|
2014-05-28 19:04:24 +04:00
|
|
|
status = WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR) Stream_Buffer(s), Stream_Length(s), &written);
|
2013-08-17 05:17:24 +04:00
|
|
|
|
|
|
|
Stream_Free(s, TRUE);
|
|
|
|
|
2013-08-17 00:46:47 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cliprdr_server_send_format_list_response(CliprdrServerContext* context)
|
|
|
|
{
|
2013-08-18 23:39:28 +04:00
|
|
|
wStream* s;
|
|
|
|
BOOL status;
|
|
|
|
CLIPRDR_HEADER header;
|
2014-05-28 19:04:24 +04:00
|
|
|
ULONG written;
|
2013-08-18 23:39:28 +04:00
|
|
|
|
2014-08-07 18:51:49 +04:00
|
|
|
DEBUG_MSG("CliprdrServerSendFormatListResponse\n");
|
2013-08-18 23:39:28 +04:00
|
|
|
|
|
|
|
header.msgType = CB_FORMAT_LIST_RESPONSE;
|
|
|
|
header.msgFlags = CB_RESPONSE_OK;
|
|
|
|
header.dataLen = 0;
|
|
|
|
|
|
|
|
s = Stream_New(NULL, header.dataLen + CLIPRDR_HEADER_LENGTH);
|
|
|
|
|
|
|
|
Stream_Write_UINT16(s, header.msgType); /* msgType (2 bytes) */
|
|
|
|
Stream_Write_UINT16(s, header.msgFlags); /* msgFlags (2 bytes) */
|
|
|
|
Stream_Write_UINT32(s, header.dataLen); /* dataLen (4 bytes) */
|
|
|
|
|
|
|
|
Stream_SealLength(s);
|
|
|
|
|
2014-05-28 19:04:24 +04:00
|
|
|
status = WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR) Stream_Buffer(s), Stream_Length(s), &written);
|
2013-08-18 23:39:28 +04:00
|
|
|
|
|
|
|
Stream_Free(s, TRUE);
|
|
|
|
|
2013-08-17 00:46:47 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-17 05:17:24 +04:00
|
|
|
static int cliprdr_server_receive_capabilities(CliprdrServerContext* context, wStream* s, CLIPRDR_HEADER* header)
|
|
|
|
{
|
|
|
|
UINT32 version;
|
|
|
|
UINT32 generalFlags;
|
|
|
|
UINT16 cCapabilitiesSets;
|
|
|
|
UINT16 capabilitySetType;
|
|
|
|
UINT16 lengthCapability;
|
|
|
|
|
|
|
|
Stream_Read_UINT16(s, cCapabilitiesSets); /* cCapabilitiesSets (2 bytes) */
|
|
|
|
Stream_Seek_UINT16(s); /* pad1 (2 bytes) */
|
|
|
|
|
|
|
|
Stream_Read_UINT16(s, capabilitySetType); /* capabilitySetType (2 bytes) */
|
|
|
|
Stream_Read_UINT16(s, lengthCapability); /* lengthCapability (2 bytes) */
|
|
|
|
|
|
|
|
Stream_Read_UINT32(s, version); /* version (4 bytes) */
|
|
|
|
Stream_Read_UINT32(s, generalFlags); /* generalFlags (4 bytes) */
|
|
|
|
|
|
|
|
if (generalFlags & CB_USE_LONG_FORMAT_NAMES)
|
|
|
|
context->priv->UseLongFormatNames = TRUE;
|
|
|
|
|
|
|
|
if (generalFlags & CB_STREAM_FILECLIP_ENABLED)
|
|
|
|
context->priv->StreamFileClipEnabled = TRUE;
|
|
|
|
|
|
|
|
if (generalFlags & CB_FILECLIP_NO_FILE_PATHS)
|
|
|
|
context->priv->FileClipNoFilePaths = TRUE;
|
|
|
|
|
|
|
|
if (generalFlags & CB_CAN_LOCK_CLIPDATA)
|
|
|
|
context->priv->CanLockClipData = TRUE;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-19 04:27:50 +04:00
|
|
|
static int cliprdr_server_receive_temporary_directory(CliprdrServerContext* context, wStream* s, CLIPRDR_HEADER* header)
|
|
|
|
{
|
|
|
|
WCHAR* wszTempDir;
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < 520)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
wszTempDir = (WCHAR*) Stream_Pointer(s);
|
|
|
|
|
|
|
|
if (wszTempDir[260] != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ConvertFromUnicode(CP_UTF8, 0, wszTempDir, -1,
|
|
|
|
&(context->priv->ClientTemporaryDirectory), 0, NULL, NULL);
|
|
|
|
|
2014-08-07 18:51:49 +04:00
|
|
|
DEBUG_MSG("ClientTemporaryDirectory: %s\n", context->priv->ClientTemporaryDirectory);
|
2013-08-19 04:27:50 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-17 05:17:24 +04:00
|
|
|
int cliprdr_wcslen(const WCHAR* str, const WCHAR* end)
|
|
|
|
{
|
|
|
|
WCHAR* p = (WCHAR*) str;
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
while (*p)
|
|
|
|
{
|
|
|
|
if (p == end)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (p - str);
|
|
|
|
}
|
|
|
|
|
2013-08-18 23:39:28 +04:00
|
|
|
static void cliprdr_free_format_list(UINT32 count, CLIPRDR_FORMAT_NAME* formatNames)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (formatNames)
|
|
|
|
{
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
free(formatNames[i].name);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(formatNames);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-17 05:17:24 +04:00
|
|
|
static int cliprdr_server_receive_long_format_list(CliprdrServerContext* context, wStream* s, CLIPRDR_HEADER* header)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
WCHAR* end;
|
|
|
|
int length;
|
|
|
|
int position;
|
|
|
|
|
2014-08-07 18:51:49 +04:00
|
|
|
DEBUG_MSG("%s\n", __FUNCTION__);
|
2013-08-19 20:21:40 +04:00
|
|
|
|
2013-08-17 05:17:24 +04:00
|
|
|
position = Stream_GetPosition(s);
|
2013-08-18 23:39:28 +04:00
|
|
|
Stream_SetPosition(s, Stream_Length(s));
|
|
|
|
end = (WCHAR*) Stream_Pointer(s);
|
|
|
|
Stream_SetPosition(s, position);
|
2013-08-17 05:17:24 +04:00
|
|
|
|
2013-08-18 23:39:28 +04:00
|
|
|
cliprdr_free_format_list(context->priv->ClientFormatNameCount, context->priv->ClientFormatNames);
|
2013-08-17 05:17:24 +04:00
|
|
|
context->priv->ClientFormatNameCount = 0;
|
2013-08-18 23:39:28 +04:00
|
|
|
context->priv->ClientFormatNames = NULL;
|
2013-08-17 05:17:24 +04:00
|
|
|
|
|
|
|
while (Stream_GetRemainingLength(s) >= 6)
|
|
|
|
{
|
|
|
|
Stream_Seek(s, 4); /* formatId (4 bytes) */
|
|
|
|
|
|
|
|
length = cliprdr_wcslen((WCHAR*) Stream_Pointer(s), end);
|
|
|
|
|
|
|
|
if (length < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
Stream_Seek(s, (length + 1) * 2); /* wszFormatName */
|
|
|
|
|
2013-08-18 23:39:28 +04:00
|
|
|
context->priv->ClientFormatNameCount++;
|
2013-08-17 05:17:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
context->priv->ClientFormatNames = (CLIPRDR_FORMAT_NAME*)
|
|
|
|
malloc(sizeof(CLIPRDR_FORMAT_NAME) * context->priv->ClientFormatNameCount);
|
|
|
|
|
|
|
|
Stream_SetPosition(s, position);
|
|
|
|
|
|
|
|
for (i = 0; i < context->priv->ClientFormatNameCount; i++)
|
|
|
|
{
|
|
|
|
Stream_Read_UINT32(s, context->priv->ClientFormatNames[i].id); /* formatId (4 bytes) */
|
|
|
|
|
2013-08-19 20:21:40 +04:00
|
|
|
length = cliprdr_wcslen((WCHAR*) Stream_Pointer(s), end);
|
|
|
|
|
|
|
|
context->priv->ClientFormatNames[i].name = NULL;
|
2013-08-17 05:17:24 +04:00
|
|
|
|
2013-08-19 20:21:40 +04:00
|
|
|
if (length)
|
|
|
|
{
|
|
|
|
context->priv->ClientFormatNames[i].length = ConvertFromUnicode(CP_UTF8, 0, (WCHAR*) Stream_Pointer(s),
|
|
|
|
-1, &(context->priv->ClientFormatNames[i].name), 0, NULL, NULL) - 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
context->priv->ClientFormatNames[i].length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream_Seek(s, (length + 1) * 2); /* wszFormatName */
|
2013-08-17 05:17:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < context->priv->ClientFormatNameCount; i++)
|
|
|
|
{
|
2014-08-07 18:51:49 +04:00
|
|
|
DEBUG_MSG("Format %d: Id: 0x%04X Name: %s Length: %d\n", i,
|
2013-08-17 05:17:24 +04:00
|
|
|
context->priv->ClientFormatNames[i].id,
|
2013-08-19 20:21:40 +04:00
|
|
|
context->priv->ClientFormatNames[i].name,
|
|
|
|
context->priv->ClientFormatNames[i].length);
|
2013-08-17 05:17:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cliprdr_server_receive_short_format_list(CliprdrServerContext* context, wStream* s, CLIPRDR_HEADER* header)
|
|
|
|
{
|
2014-08-07 18:51:49 +04:00
|
|
|
DEBUG_MSG("%s: unimplemented\n", __FUNCTION__);
|
2013-08-17 05:17:24 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cliprdr_server_receive_format_list(CliprdrServerContext* context, wStream* s, CLIPRDR_HEADER* header)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
|
|
|
if (context->priv->UseLongFormatNames)
|
|
|
|
{
|
|
|
|
status = cliprdr_server_receive_long_format_list(context, s, header);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = cliprdr_server_receive_short_format_list(context, s, header);
|
|
|
|
}
|
|
|
|
|
2013-08-18 23:39:28 +04:00
|
|
|
cliprdr_server_send_format_list_response(context);
|
|
|
|
|
2013-08-17 05:17:24 +04:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2013-08-18 23:39:28 +04:00
|
|
|
static int cliprdr_server_receive_pdu(CliprdrServerContext* context, wStream* s, CLIPRDR_HEADER* header)
|
2013-08-17 05:17:24 +04:00
|
|
|
{
|
2014-08-07 18:51:49 +04:00
|
|
|
DEBUG_MSG("CliprdrServerReceivePdu: msgType: %d msgFlags: 0x%08X dataLen: %d\n",
|
2013-08-18 23:39:28 +04:00
|
|
|
header->msgType, header->msgFlags, header->dataLen);
|
2013-08-17 05:17:24 +04:00
|
|
|
|
2013-08-18 23:39:28 +04:00
|
|
|
switch (header->msgType)
|
2013-08-17 05:17:24 +04:00
|
|
|
{
|
|
|
|
case CB_CLIP_CAPS:
|
2013-08-18 23:39:28 +04:00
|
|
|
cliprdr_server_receive_capabilities(context, s, header);
|
2013-08-17 05:17:24 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CB_TEMP_DIRECTORY:
|
2013-08-19 04:27:50 +04:00
|
|
|
cliprdr_server_receive_temporary_directory(context, s, header);
|
2013-08-17 05:17:24 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CB_FORMAT_LIST:
|
2013-08-18 23:39:28 +04:00
|
|
|
cliprdr_server_receive_format_list(context, s, header);
|
2013-08-17 05:17:24 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CB_FORMAT_LIST_RESPONSE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CB_LOCK_CLIPDATA:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CB_UNLOCK_CLIPDATA:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CB_FORMAT_DATA_REQUEST:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CB_FORMAT_DATA_RESPONSE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CB_FILECONTENTS_REQUEST:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CB_FILECONTENTS_RESPONSE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2014-08-07 18:51:49 +04:00
|
|
|
DEBUG_MSG("Unexpected clipboard PDU type: %d\n", header->msgType);
|
2013-08-17 05:17:24 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-17 00:46:47 +04:00
|
|
|
static void* cliprdr_server_thread(void* arg)
|
|
|
|
{
|
|
|
|
wStream* s;
|
|
|
|
DWORD status;
|
|
|
|
DWORD nCount;
|
|
|
|
void* buffer;
|
2013-08-18 23:39:28 +04:00
|
|
|
int position;
|
2013-08-17 00:46:47 +04:00
|
|
|
HANDLE events[8];
|
|
|
|
HANDLE ChannelEvent;
|
2013-08-21 02:06:19 +04:00
|
|
|
DWORD BytesReturned;
|
2013-08-18 23:39:28 +04:00
|
|
|
CLIPRDR_HEADER header;
|
2013-08-17 00:46:47 +04:00
|
|
|
CliprdrServerContext* context;
|
|
|
|
|
|
|
|
context = (CliprdrServerContext*) arg;
|
|
|
|
|
|
|
|
buffer = NULL;
|
|
|
|
BytesReturned = 0;
|
|
|
|
ChannelEvent = NULL;
|
|
|
|
|
|
|
|
s = Stream_New(NULL, 4096);
|
|
|
|
|
|
|
|
if (WTSVirtualChannelQuery(context->priv->ChannelHandle, WTSVirtualEventHandle, &buffer, &BytesReturned) == TRUE)
|
|
|
|
{
|
|
|
|
if (BytesReturned == sizeof(HANDLE))
|
|
|
|
CopyMemory(&ChannelEvent, buffer, sizeof(HANDLE));
|
|
|
|
|
|
|
|
WTSFreeMemory(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
nCount = 0;
|
|
|
|
events[nCount++] = ChannelEvent;
|
|
|
|
events[nCount++] = context->priv->StopEvent;
|
|
|
|
|
2013-08-18 23:39:28 +04:00
|
|
|
cliprdr_server_send_capabilities(context);
|
|
|
|
cliprdr_server_send_monitor_ready(context);
|
2013-08-17 05:17:24 +04:00
|
|
|
|
2013-08-17 00:46:47 +04:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
|
|
|
|
|
|
|
|
if (WaitForSingleObject(context->priv->StopEvent, 0) == WAIT_OBJECT_0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-07-14 16:00:38 +04:00
|
|
|
WTSVirtualChannelRead(context->priv->ChannelHandle, 0, NULL, 0, &BytesReturned);
|
|
|
|
if (BytesReturned < 1)
|
|
|
|
continue;
|
|
|
|
Stream_EnsureRemainingCapacity(s, BytesReturned);
|
|
|
|
if (!WTSVirtualChannelRead(context->priv->ChannelHandle, 0,
|
|
|
|
(PCHAR) Stream_Buffer(s), Stream_Capacity(s), &BytesReturned))
|
2013-08-18 23:39:28 +04:00
|
|
|
{
|
2014-07-14 16:00:38 +04:00
|
|
|
break;
|
2013-08-18 23:39:28 +04:00
|
|
|
}
|
2013-08-17 00:46:47 +04:00
|
|
|
|
2013-08-18 23:39:28 +04:00
|
|
|
if (Stream_GetPosition(s) >= CLIPRDR_HEADER_LENGTH)
|
|
|
|
{
|
|
|
|
position = Stream_GetPosition(s);
|
|
|
|
Stream_SetPosition(s, 0);
|
2013-08-17 05:17:24 +04:00
|
|
|
|
2013-08-18 23:39:28 +04:00
|
|
|
Stream_Read_UINT16(s, header.msgType); /* msgType (2 bytes) */
|
|
|
|
Stream_Read_UINT16(s, header.msgFlags); /* msgFlags (2 bytes) */
|
|
|
|
Stream_Read_UINT32(s, header.dataLen); /* dataLen (4 bytes) */
|
|
|
|
|
|
|
|
Stream_SetPosition(s, position);
|
2013-08-17 05:17:24 +04:00
|
|
|
|
2013-08-18 23:39:28 +04:00
|
|
|
if (Stream_GetPosition(s) >= (header.dataLen + CLIPRDR_HEADER_LENGTH))
|
|
|
|
{
|
|
|
|
Stream_SealLength(s);
|
|
|
|
Stream_SetPosition(s, CLIPRDR_HEADER_LENGTH);
|
2013-08-17 05:17:24 +04:00
|
|
|
|
2013-08-18 23:39:28 +04:00
|
|
|
cliprdr_server_receive_pdu(context, s, &header);
|
|
|
|
Stream_SetPosition(s, 0);
|
|
|
|
}
|
|
|
|
}
|
2013-08-17 00:46:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Stream_Free(s, TRUE);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cliprdr_server_start(CliprdrServerContext* context)
|
|
|
|
{
|
2014-02-27 22:30:04 +04:00
|
|
|
context->priv->ChannelHandle = WTSVirtualChannelOpen(context->vcm, WTS_CURRENT_SESSION, "cliprdr");
|
2013-08-17 00:46:47 +04:00
|
|
|
|
|
|
|
if (!context->priv->ChannelHandle)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
context->priv->StopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
|
|
|
|
context->priv->Thread = CreateThread(NULL, 0,
|
|
|
|
(LPTHREAD_START_ROUTINE) cliprdr_server_thread, (void*) context, 0, NULL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cliprdr_server_stop(CliprdrServerContext* context)
|
|
|
|
{
|
|
|
|
SetEvent(context->priv->StopEvent);
|
|
|
|
|
|
|
|
WaitForSingleObject(context->priv->Thread, INFINITE);
|
|
|
|
CloseHandle(context->priv->Thread);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-27 22:30:04 +04:00
|
|
|
CliprdrServerContext* cliprdr_server_context_new(HANDLE vcm)
|
2013-08-17 00:46:47 +04:00
|
|
|
{
|
|
|
|
CliprdrServerContext* context;
|
|
|
|
|
2014-02-17 08:00:58 +04:00
|
|
|
context = (CliprdrServerContext*) calloc(1, sizeof(CliprdrServerContext));
|
2013-08-17 00:46:47 +04:00
|
|
|
|
|
|
|
if (context)
|
|
|
|
{
|
|
|
|
context->vcm = vcm;
|
|
|
|
|
|
|
|
context->Start = cliprdr_server_start;
|
|
|
|
context->Stop = cliprdr_server_stop;
|
|
|
|
|
|
|
|
context->priv = (CliprdrServerPrivate*) malloc(sizeof(CliprdrServerPrivate));
|
|
|
|
|
|
|
|
if (context->priv)
|
|
|
|
{
|
|
|
|
ZeroMemory(context->priv, sizeof(CliprdrServerPrivate));
|
2013-08-17 05:17:24 +04:00
|
|
|
|
|
|
|
context->priv->UseLongFormatNames = TRUE;
|
2013-08-18 23:39:28 +04:00
|
|
|
context->priv->StreamFileClipEnabled = TRUE;
|
|
|
|
context->priv->FileClipNoFilePaths = TRUE;
|
|
|
|
context->priv->CanLockClipData = TRUE;
|
2013-08-17 00:46:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cliprdr_server_context_free(CliprdrServerContext* context)
|
|
|
|
{
|
|
|
|
if (context)
|
|
|
|
{
|
|
|
|
if (context->priv)
|
|
|
|
{
|
|
|
|
free(context->priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(context);
|
|
|
|
}
|
|
|
|
}
|