20#include <freerdp/config.h>
22#include <freerdp/freerdp.h>
23#include <freerdp/channels/log.h>
24#include <freerdp/server/rdpemsc.h>
26#define TAG CHANNELS_TAG("rdpemsc.server")
32} eMouseCursorChannelState;
36 MouseCursorServerContext context;
41 void* mouse_cursor_channel;
49 eMouseCursorChannelState state;
54static UINT mouse_cursor_server_initialize(MouseCursorServerContext* context, BOOL externalThread)
56 UINT error = CHANNEL_RC_OK;
57 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
59 WINPR_ASSERT(mouse_cursor);
61 if (mouse_cursor->isOpened)
63 WLog_WARN(TAG,
"Application error: Mouse Cursor channel already initialized, "
64 "calling in this state is not possible!");
65 return ERROR_INVALID_STATE;
68 mouse_cursor->externalThread = externalThread;
73static UINT mouse_cursor_server_open_channel(mouse_cursor_server* mouse_cursor)
75 MouseCursorServerContext* context = NULL;
76 DWORD Error = ERROR_SUCCESS;
77 DWORD BytesReturned = 0;
78 PULONG pSessionId = NULL;
82 WINPR_ASSERT(mouse_cursor);
83 context = &mouse_cursor->context;
84 WINPR_ASSERT(context);
86 if (WTSQuerySessionInformationA(mouse_cursor->context.vcm, WTS_CURRENT_SESSION, WTSSessionId,
87 (LPSTR*)&pSessionId, &BytesReturned) == FALSE)
89 WLog_ERR(TAG,
"WTSQuerySessionInformationA failed!");
90 return ERROR_INTERNAL_ERROR;
93 mouse_cursor->SessionId = (DWORD)*pSessionId;
94 WTSFreeMemory(pSessionId);
96 mouse_cursor->mouse_cursor_channel = WTSVirtualChannelOpenEx(
97 mouse_cursor->SessionId, RDPEMSC_DVC_CHANNEL_NAME, WTS_CHANNEL_OPTION_DYNAMIC);
98 if (!mouse_cursor->mouse_cursor_channel)
100 Error = GetLastError();
101 WLog_ERR(TAG,
"WTSVirtualChannelOpenEx failed with error %" PRIu32
"!", Error);
105 channelId = WTSChannelGetIdByHandle(mouse_cursor->mouse_cursor_channel);
107 IFCALLRET(context->ChannelIdAssigned, status, context, channelId);
110 WLog_ERR(TAG,
"context->ChannelIdAssigned failed!");
111 return ERROR_INTERNAL_ERROR;
117static BOOL read_cap_set(
wStream* s, wArrayList* capsSets)
120 UINT32 signature = 0;
122 size_t capsDataSize = 0;
124 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
127 Stream_Read_UINT32(s, signature);
131 const UINT32 val = Stream_Get_UINT32(s);
138 WLog_WARN(TAG,
"Received caps set with unknown version %" PRIu32, val);
143 Stream_Read_UINT32(s, size);
147 WLog_ERR(TAG,
"Size of caps set is invalid: %u", size);
151 capsDataSize = size - 12;
152 if (!Stream_CheckAndLogRequiredLength(TAG, s, capsDataSize))
169 Stream_Seek(s, capsDataSize);
172 WINPR_ASSERT(capsSet);
174 capsSet->signature = signature;
175 capsSet->version = version;
176 capsSet->size = size;
178 if (!ArrayList_Append(capsSets, capsSet))
180 WLog_ERR(TAG,
"Failed to append caps set to arraylist");
189static UINT mouse_cursor_server_recv_cs_caps_advertise(MouseCursorServerContext* context,
194 UINT error = CHANNEL_RC_OK;
196 WINPR_ASSERT(context);
198 WINPR_ASSERT(header);
200 pdu.header = *header;
203 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
204 return ERROR_NO_DATA;
206 pdu.capsSets = ArrayList_New(FALSE);
209 WLog_ERR(TAG,
"Failed to allocate arraylist");
210 return ERROR_NOT_ENOUGH_MEMORY;
213 wObject* aobj = ArrayList_Object(pdu.capsSets);
215 aobj->fnObjectFree = free;
217 while (Stream_GetRemainingLength(s) > 0)
219 if (!read_cap_set(s, pdu.capsSets))
221 ArrayList_Free(pdu.capsSets);
222 return ERROR_INVALID_DATA;
226 IFCALLRET(context->CapsAdvertise, error, context, &pdu);
228 WLog_ERR(TAG,
"context->CapsAdvertise failed with error %" PRIu32
"", error);
230 ArrayList_Free(pdu.capsSets);
235static UINT mouse_cursor_process_message(mouse_cursor_server* mouse_cursor)
238 UINT error = ERROR_INTERNAL_ERROR;
239 ULONG BytesReturned = 0;
243 WINPR_ASSERT(mouse_cursor);
244 WINPR_ASSERT(mouse_cursor->mouse_cursor_channel);
246 s = mouse_cursor->buffer;
249 Stream_SetPosition(s, 0);
250 rc = WTSVirtualChannelRead(mouse_cursor->mouse_cursor_channel, 0, NULL, 0, &BytesReturned);
254 if (BytesReturned < 1)
256 error = CHANNEL_RC_OK;
260 if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
262 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
263 error = CHANNEL_RC_NO_MEMORY;
267 if (WTSVirtualChannelRead(mouse_cursor->mouse_cursor_channel, 0, Stream_BufferAs(s,
char),
268 (ULONG)Stream_Capacity(s), &BytesReturned) == FALSE)
270 WLog_ERR(TAG,
"WTSVirtualChannelRead failed!");
274 Stream_SetLength(s, BytesReturned);
275 if (!Stream_CheckAndLogRequiredLength(TAG, s, RDPEMSC_HEADER_SIZE))
276 return ERROR_NO_DATA;
279 const UINT8 pduType = Stream_Get_UINT8(s);
280 const UINT8 updateType = Stream_Get_UINT8(s);
283 case TS_UPDATETYPE_MOUSEPTR_SYSTEM_NULL:
284 case TS_UPDATETYPE_MOUSEPTR_SYSTEM_DEFAULT:
285 case TS_UPDATETYPE_MOUSEPTR_POSITION:
286 case TS_UPDATETYPE_MOUSEPTR_CACHED:
287 case TS_UPDATETYPE_MOUSEPTR_POINTER:
288 case TS_UPDATETYPE_MOUSEPTR_LARGE_POINTER:
292 "mouse_cursor_process_message: unknown or invalid updateType %" PRIu8
"",
294 return ERROR_INVALID_DATA;
296 header.updateType = (TS_UPDATETYPE_MOUSEPTR)updateType;
298 Stream_Read_UINT16(s, header.reserved);
302 case PDUTYPE_CS_CAPS_ADVERTISE:
303 header.pduType = PDUTYPE_CS_CAPS_ADVERTISE;
305 mouse_cursor_server_recv_cs_caps_advertise(&mouse_cursor->context, s, &header);
308 WLog_ERR(TAG,
"mouse_cursor_process_message: unknown or invalid pduType %" PRIu8
"",
316 WLog_ERR(TAG,
"Response failed with error %" PRIu32
"!", error);
321static UINT mouse_cursor_server_context_poll_int(MouseCursorServerContext* context)
323 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
324 UINT error = ERROR_INTERNAL_ERROR;
326 WINPR_ASSERT(mouse_cursor);
328 switch (mouse_cursor->state)
330 case MOUSE_CURSOR_INITIAL:
331 error = mouse_cursor_server_open_channel(mouse_cursor);
333 WLog_ERR(TAG,
"mouse_cursor_server_open_channel failed with error %" PRIu32
"!",
336 mouse_cursor->state = MOUSE_CURSOR_OPENED;
338 case MOUSE_CURSOR_OPENED:
339 error = mouse_cursor_process_message(mouse_cursor);
348static HANDLE mouse_cursor_server_get_channel_handle(mouse_cursor_server* mouse_cursor)
351 DWORD BytesReturned = 0;
352 HANDLE ChannelEvent = NULL;
354 WINPR_ASSERT(mouse_cursor);
356 if (WTSVirtualChannelQuery(mouse_cursor->mouse_cursor_channel, WTSVirtualEventHandle, &buffer,
357 &BytesReturned) == TRUE)
359 if (BytesReturned ==
sizeof(HANDLE))
360 ChannelEvent = *(HANDLE*)buffer;
362 WTSFreeMemory(buffer);
368static DWORD WINAPI mouse_cursor_server_thread_func(LPVOID arg)
371 HANDLE events[2] = { 0 };
372 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)arg;
373 UINT error = CHANNEL_RC_OK;
376 WINPR_ASSERT(mouse_cursor);
379 events[nCount++] = mouse_cursor->stopEvent;
381 while ((error == CHANNEL_RC_OK) && (WaitForSingleObject(events[0], 0) != WAIT_OBJECT_0))
383 switch (mouse_cursor->state)
385 case MOUSE_CURSOR_INITIAL:
386 error = mouse_cursor_server_context_poll_int(&mouse_cursor->context);
387 if (error == CHANNEL_RC_OK)
389 events[1] = mouse_cursor_server_get_channel_handle(mouse_cursor);
393 case MOUSE_CURSOR_OPENED:
394 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
399 case WAIT_OBJECT_0 + 1:
401 error = mouse_cursor_server_context_poll_int(&mouse_cursor->context);
406 error = ERROR_INTERNAL_ERROR;
415 (void)WTSVirtualChannelClose(mouse_cursor->mouse_cursor_channel);
416 mouse_cursor->mouse_cursor_channel = NULL;
418 if (error && mouse_cursor->context.rdpcontext)
419 setChannelError(mouse_cursor->context.rdpcontext, error,
420 "mouse_cursor_server_thread_func reported an error");
426static UINT mouse_cursor_server_open(MouseCursorServerContext* context)
428 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
430 WINPR_ASSERT(mouse_cursor);
432 if (!mouse_cursor->externalThread && (mouse_cursor->thread == NULL))
434 mouse_cursor->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
435 if (!mouse_cursor->stopEvent)
437 WLog_ERR(TAG,
"CreateEvent failed!");
438 return ERROR_INTERNAL_ERROR;
441 mouse_cursor->thread =
442 CreateThread(NULL, 0, mouse_cursor_server_thread_func, mouse_cursor, 0, NULL);
443 if (!mouse_cursor->thread)
445 WLog_ERR(TAG,
"CreateThread failed!");
446 (void)CloseHandle(mouse_cursor->stopEvent);
447 mouse_cursor->stopEvent = NULL;
448 return ERROR_INTERNAL_ERROR;
451 mouse_cursor->isOpened = TRUE;
453 return CHANNEL_RC_OK;
456static UINT mouse_cursor_server_close(MouseCursorServerContext* context)
458 UINT error = CHANNEL_RC_OK;
459 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
461 WINPR_ASSERT(mouse_cursor);
463 if (!mouse_cursor->externalThread && mouse_cursor->thread)
465 (void)SetEvent(mouse_cursor->stopEvent);
467 if (WaitForSingleObject(mouse_cursor->thread, INFINITE) == WAIT_FAILED)
469 error = GetLastError();
470 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"", error);
474 (void)CloseHandle(mouse_cursor->thread);
475 (void)CloseHandle(mouse_cursor->stopEvent);
476 mouse_cursor->thread = NULL;
477 mouse_cursor->stopEvent = NULL;
479 if (mouse_cursor->externalThread)
481 if (mouse_cursor->state != MOUSE_CURSOR_INITIAL)
483 (void)WTSVirtualChannelClose(mouse_cursor->mouse_cursor_channel);
484 mouse_cursor->mouse_cursor_channel = NULL;
485 mouse_cursor->state = MOUSE_CURSOR_INITIAL;
488 mouse_cursor->isOpened = FALSE;
493static UINT mouse_cursor_server_context_poll(MouseCursorServerContext* context)
495 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
497 WINPR_ASSERT(mouse_cursor);
499 if (!mouse_cursor->externalThread)
500 return ERROR_INTERNAL_ERROR;
502 return mouse_cursor_server_context_poll_int(context);
505static BOOL mouse_cursor_server_context_handle(MouseCursorServerContext* context, HANDLE* handle)
507 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
509 WINPR_ASSERT(mouse_cursor);
510 WINPR_ASSERT(handle);
512 if (!mouse_cursor->externalThread)
514 if (mouse_cursor->state == MOUSE_CURSOR_INITIAL)
517 *handle = mouse_cursor_server_get_channel_handle(mouse_cursor);
522static wStream* mouse_cursor_server_packet_new(
size_t size, RDP_MOUSE_CURSOR_PDUTYPE pduType,
528 s = Stream_New(NULL, size + RDPEMSC_HEADER_SIZE);
531 WLog_ERR(TAG,
"Stream_New failed!");
535 WINPR_ASSERT(pduType <= UINT8_MAX);
536 Stream_Write_UINT8(s, (BYTE)pduType);
538 WINPR_ASSERT(header->updateType <= UINT8_MAX);
539 Stream_Write_UINT8(s, (BYTE)header->updateType);
540 Stream_Write_UINT16(s, header->reserved);
545static UINT mouse_cursor_server_packet_send(MouseCursorServerContext* context,
wStream* s)
547 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
548 UINT error = CHANNEL_RC_OK;
551 WINPR_ASSERT(mouse_cursor);
554 const size_t pos = Stream_GetPosition(s);
556 WINPR_ASSERT(pos <= UINT32_MAX);
557 if (!WTSVirtualChannelWrite(mouse_cursor->mouse_cursor_channel, Stream_BufferAs(s,
char),
558 (ULONG)pos, &written))
560 WLog_ERR(TAG,
"WTSVirtualChannelWrite failed!");
561 error = ERROR_INTERNAL_ERROR;
565 if (written < Stream_GetPosition(s))
567 WLog_WARN(TAG,
"Unexpected bytes written: %" PRIu32
"/%" PRIuz
"", written,
568 Stream_GetPosition(s));
572 Stream_Free(s, TRUE);
577mouse_cursor_server_send_sc_caps_confirm(MouseCursorServerContext* context,
581 RDP_MOUSE_CURSOR_PDUTYPE pduType = PDUTYPE_EMSC_RESERVED;
582 size_t caps_size = 0;
585 WINPR_ASSERT(context);
586 WINPR_ASSERT(capsConfirm);
588 capsetHeader = capsConfirm->capsSet;
589 WINPR_ASSERT(capsetHeader);
592 switch (capsetHeader->version)
601 pduType = PDUTYPE_SC_CAPS_CONFIRM;
602 s = mouse_cursor_server_packet_new(caps_size, pduType, &capsConfirm->header);
604 return ERROR_NOT_ENOUGH_MEMORY;
606 Stream_Write_UINT32(s, capsetHeader->signature);
607 Stream_Write_UINT32(s, capsetHeader->version);
608 Stream_Write_UINT32(s, capsetHeader->size);
611 switch (capsetHeader->version)
620 return mouse_cursor_server_packet_send(context, s);
626 WINPR_ASSERT(point16);
628 Stream_Write_UINT16(s, point16->xPos);
629 Stream_Write_UINT16(s, point16->yPos);
632static UINT mouse_cursor_server_send_sc_mouseptr_update(
638 RDP_MOUSE_CURSOR_PDUTYPE pduType = PDUTYPE_EMSC_RESERVED;
639 size_t update_size = 0;
642 WINPR_ASSERT(context);
643 WINPR_ASSERT(mouseptrUpdate);
645 position = mouseptrUpdate->position;
646 pointerAttribute = mouseptrUpdate->pointerAttribute;
647 largePointerAttribute = mouseptrUpdate->largePointerAttribute;
649 switch (mouseptrUpdate->header.updateType)
651 case TS_UPDATETYPE_MOUSEPTR_SYSTEM_NULL:
652 case TS_UPDATETYPE_MOUSEPTR_SYSTEM_DEFAULT:
655 case TS_UPDATETYPE_MOUSEPTR_POSITION:
656 WINPR_ASSERT(position);
659 case TS_UPDATETYPE_MOUSEPTR_CACHED:
660 WINPR_ASSERT(mouseptrUpdate->cachedPointerIndex);
663 case TS_UPDATETYPE_MOUSEPTR_POINTER:
664 WINPR_ASSERT(pointerAttribute);
665 update_size = 2 + 2 + 4 + 2 + 2 + 2 + 2;
666 update_size += pointerAttribute->lengthAndMask;
667 update_size += pointerAttribute->lengthXorMask;
669 case TS_UPDATETYPE_MOUSEPTR_LARGE_POINTER:
670 WINPR_ASSERT(largePointerAttribute);
671 update_size = 2 + 2 + 4 + 2 + 2 + 4 + 4;
672 update_size += largePointerAttribute->lengthAndMask;
673 update_size += largePointerAttribute->lengthXorMask;
680 pduType = PDUTYPE_SC_MOUSEPTR_UPDATE;
681 s = mouse_cursor_server_packet_new(update_size, pduType, &mouseptrUpdate->header);
683 return ERROR_NOT_ENOUGH_MEMORY;
685 switch (mouseptrUpdate->header.updateType)
687 case TS_UPDATETYPE_MOUSEPTR_SYSTEM_NULL:
688 case TS_UPDATETYPE_MOUSEPTR_SYSTEM_DEFAULT:
690 case TS_UPDATETYPE_MOUSEPTR_POSITION:
691 write_point16(s, position);
693 case TS_UPDATETYPE_MOUSEPTR_CACHED:
694 Stream_Write_UINT16(s, *mouseptrUpdate->cachedPointerIndex);
696 case TS_UPDATETYPE_MOUSEPTR_POINTER:
697 Stream_Write_UINT16(s, pointerAttribute->xorBpp);
698 Stream_Write_UINT16(s, pointerAttribute->cacheIndex);
699 write_point16(s, &pointerAttribute->hotSpot);
700 Stream_Write_UINT16(s, pointerAttribute->width);
701 Stream_Write_UINT16(s, pointerAttribute->height);
702 Stream_Write_UINT16(s, pointerAttribute->lengthAndMask);
703 Stream_Write_UINT16(s, pointerAttribute->lengthXorMask);
704 Stream_Write(s, pointerAttribute->xorMaskData, pointerAttribute->lengthXorMask);
705 Stream_Write(s, pointerAttribute->andMaskData, pointerAttribute->lengthAndMask);
707 case TS_UPDATETYPE_MOUSEPTR_LARGE_POINTER:
708 Stream_Write_UINT16(s, largePointerAttribute->xorBpp);
709 Stream_Write_UINT16(s, largePointerAttribute->cacheIndex);
710 write_point16(s, &largePointerAttribute->hotSpot);
711 Stream_Write_UINT16(s, largePointerAttribute->width);
712 Stream_Write_UINT16(s, largePointerAttribute->height);
713 Stream_Write_UINT32(s, largePointerAttribute->lengthAndMask);
714 Stream_Write_UINT32(s, largePointerAttribute->lengthXorMask);
715 Stream_Write(s, largePointerAttribute->xorMaskData,
716 largePointerAttribute->lengthXorMask);
717 Stream_Write(s, largePointerAttribute->andMaskData,
718 largePointerAttribute->lengthAndMask);
725 return mouse_cursor_server_packet_send(context, s);
728MouseCursorServerContext* mouse_cursor_server_context_new(HANDLE vcm)
730 mouse_cursor_server* mouse_cursor =
731 (mouse_cursor_server*)calloc(1,
sizeof(mouse_cursor_server));
736 mouse_cursor->context.vcm = vcm;
737 mouse_cursor->context.Initialize = mouse_cursor_server_initialize;
738 mouse_cursor->context.Open = mouse_cursor_server_open;
739 mouse_cursor->context.Close = mouse_cursor_server_close;
740 mouse_cursor->context.Poll = mouse_cursor_server_context_poll;
741 mouse_cursor->context.ChannelHandle = mouse_cursor_server_context_handle;
743 mouse_cursor->context.CapsConfirm = mouse_cursor_server_send_sc_caps_confirm;
744 mouse_cursor->context.MouseptrUpdate = mouse_cursor_server_send_sc_mouseptr_update;
746 mouse_cursor->buffer = Stream_New(NULL, 4096);
747 if (!mouse_cursor->buffer)
750 return &mouse_cursor->context;
752 WINPR_PRAGMA_DIAG_PUSH
753 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
754 mouse_cursor_server_context_free(&mouse_cursor->context);
755 WINPR_PRAGMA_DIAG_POP
759void mouse_cursor_server_context_free(MouseCursorServerContext* context)
761 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
765 mouse_cursor_server_close(context);
766 Stream_Free(mouse_cursor->buffer, TRUE);
RDP_MOUSE_CURSOR_CAPVERSION
@ RDP_MOUSE_CURSOR_CAPVERSION_1
This struct contains function pointer to initialize/free objects.