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 =
nullptr;
76 DWORD Error = ERROR_SUCCESS;
77 DWORD BytesReturned = 0;
78 PULONG pSessionId =
nullptr;
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,
193 UINT error = CHANNEL_RC_OK;
195 WINPR_ASSERT(context);
197 WINPR_ASSERT(header);
200 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
201 return ERROR_NO_DATA;
205 .capsSets = ArrayList_New(FALSE),
210 WLog_ERR(TAG,
"Failed to allocate arraylist");
211 return ERROR_NOT_ENOUGH_MEMORY;
214 wObject* aobj = ArrayList_Object(pdu.capsSets);
218 while (Stream_GetRemainingLength(s) > 0)
220 if (!read_cap_set(s, pdu.capsSets))
222 ArrayList_Free(pdu.capsSets);
223 return ERROR_INVALID_DATA;
227 IFCALLRET(context->CapsAdvertise, error, context, &pdu);
229 WLog_ERR(TAG,
"context->CapsAdvertise failed with error %" PRIu32
"", error);
231 ArrayList_Free(pdu.capsSets);
236static UINT mouse_cursor_process_message(mouse_cursor_server* mouse_cursor)
239 UINT error = ERROR_INTERNAL_ERROR;
240 ULONG BytesReturned = 0;
243 WINPR_ASSERT(mouse_cursor);
244 WINPR_ASSERT(mouse_cursor->mouse_cursor_channel);
246 s = mouse_cursor->buffer;
249 Stream_ResetPosition(s);
250 rc = WTSVirtualChannelRead(mouse_cursor->mouse_cursor_channel, 0,
nullptr, 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 if (!Stream_SetLength(s, BytesReturned))
277 if (!Stream_CheckAndLogRequiredLength(TAG, s, RDPEMSC_HEADER_SIZE))
278 return ERROR_NO_DATA;
281 const UINT8 pduType = Stream_Get_UINT8(s);
282 const UINT8 updateType = Stream_Get_UINT8(s);
285 case TS_UPDATETYPE_MOUSEPTR_SYSTEM_NULL:
286 case TS_UPDATETYPE_MOUSEPTR_SYSTEM_DEFAULT:
287 case TS_UPDATETYPE_MOUSEPTR_POSITION:
288 case TS_UPDATETYPE_MOUSEPTR_CACHED:
289 case TS_UPDATETYPE_MOUSEPTR_POINTER:
290 case TS_UPDATETYPE_MOUSEPTR_LARGE_POINTER:
294 "mouse_cursor_process_message: unknown or invalid updateType %" PRIu8
"",
296 return ERROR_INVALID_DATA;
300 .reserved = Stream_Get_UINT16(s),
301 .pduType = PDUTYPE_EMSC_RESERVED };
305 case PDUTYPE_CS_CAPS_ADVERTISE:
306 header.pduType = PDUTYPE_CS_CAPS_ADVERTISE;
308 mouse_cursor_server_recv_cs_caps_advertise(&mouse_cursor->context, s, &header);
311 WLog_ERR(TAG,
"mouse_cursor_process_message: unknown or invalid pduType %" PRIu8
"",
319 WLog_ERR(TAG,
"Response failed with error %" PRIu32
"!", error);
324static UINT mouse_cursor_server_context_poll_int(MouseCursorServerContext* context)
326 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
327 UINT error = ERROR_INTERNAL_ERROR;
329 WINPR_ASSERT(mouse_cursor);
331 switch (mouse_cursor->state)
333 case MOUSE_CURSOR_INITIAL:
334 error = mouse_cursor_server_open_channel(mouse_cursor);
336 WLog_ERR(TAG,
"mouse_cursor_server_open_channel failed with error %" PRIu32
"!",
339 mouse_cursor->state = MOUSE_CURSOR_OPENED;
341 case MOUSE_CURSOR_OPENED:
342 error = mouse_cursor_process_message(mouse_cursor);
351static HANDLE mouse_cursor_server_get_channel_handle(mouse_cursor_server* mouse_cursor)
353 void* buffer =
nullptr;
354 DWORD BytesReturned = 0;
355 HANDLE ChannelEvent =
nullptr;
357 WINPR_ASSERT(mouse_cursor);
359 if (WTSVirtualChannelQuery(mouse_cursor->mouse_cursor_channel, WTSVirtualEventHandle, &buffer,
360 &BytesReturned) == TRUE)
362 if (BytesReturned ==
sizeof(HANDLE))
363 ChannelEvent = *(HANDLE*)buffer;
365 WTSFreeMemory(buffer);
371static DWORD WINAPI mouse_cursor_server_thread_func(LPVOID arg)
374 HANDLE events[2] = WINPR_C_ARRAY_INIT;
375 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)arg;
376 UINT error = CHANNEL_RC_OK;
379 WINPR_ASSERT(mouse_cursor);
382 events[nCount++] = mouse_cursor->stopEvent;
384 while ((error == CHANNEL_RC_OK) && (WaitForSingleObject(events[0], 0) != WAIT_OBJECT_0))
386 switch (mouse_cursor->state)
388 case MOUSE_CURSOR_INITIAL:
389 error = mouse_cursor_server_context_poll_int(&mouse_cursor->context);
390 if (error == CHANNEL_RC_OK)
392 events[1] = mouse_cursor_server_get_channel_handle(mouse_cursor);
396 case MOUSE_CURSOR_OPENED:
397 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
402 case WAIT_OBJECT_0 + 1:
404 error = mouse_cursor_server_context_poll_int(&mouse_cursor->context);
409 error = ERROR_INTERNAL_ERROR;
418 (void)WTSVirtualChannelClose(mouse_cursor->mouse_cursor_channel);
419 mouse_cursor->mouse_cursor_channel =
nullptr;
421 if (error && mouse_cursor->context.rdpcontext)
422 setChannelError(mouse_cursor->context.rdpcontext, error,
423 "mouse_cursor_server_thread_func reported an error");
429static UINT mouse_cursor_server_open(MouseCursorServerContext* context)
431 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
433 WINPR_ASSERT(mouse_cursor);
435 if (!mouse_cursor->externalThread && (mouse_cursor->thread ==
nullptr))
437 mouse_cursor->stopEvent = CreateEvent(
nullptr, TRUE, FALSE,
nullptr);
438 if (!mouse_cursor->stopEvent)
440 WLog_ERR(TAG,
"CreateEvent failed!");
441 return ERROR_INTERNAL_ERROR;
444 mouse_cursor->thread =
445 CreateThread(
nullptr, 0, mouse_cursor_server_thread_func, mouse_cursor, 0,
nullptr);
446 if (!mouse_cursor->thread)
448 WLog_ERR(TAG,
"CreateThread failed!");
449 (void)CloseHandle(mouse_cursor->stopEvent);
450 mouse_cursor->stopEvent =
nullptr;
451 return ERROR_INTERNAL_ERROR;
454 mouse_cursor->isOpened = TRUE;
456 return CHANNEL_RC_OK;
459static UINT mouse_cursor_server_close(MouseCursorServerContext* context)
461 UINT error = CHANNEL_RC_OK;
462 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
464 WINPR_ASSERT(mouse_cursor);
466 if (!mouse_cursor->externalThread && mouse_cursor->thread)
468 (void)SetEvent(mouse_cursor->stopEvent);
470 if (WaitForSingleObject(mouse_cursor->thread, INFINITE) == WAIT_FAILED)
472 error = GetLastError();
473 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"", error);
477 (void)CloseHandle(mouse_cursor->thread);
478 (void)CloseHandle(mouse_cursor->stopEvent);
479 mouse_cursor->thread =
nullptr;
480 mouse_cursor->stopEvent =
nullptr;
482 if (mouse_cursor->externalThread)
484 if (mouse_cursor->state != MOUSE_CURSOR_INITIAL)
486 (void)WTSVirtualChannelClose(mouse_cursor->mouse_cursor_channel);
487 mouse_cursor->mouse_cursor_channel =
nullptr;
488 mouse_cursor->state = MOUSE_CURSOR_INITIAL;
491 mouse_cursor->isOpened = FALSE;
496static UINT mouse_cursor_server_context_poll(MouseCursorServerContext* context)
498 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
500 WINPR_ASSERT(mouse_cursor);
502 if (!mouse_cursor->externalThread)
503 return ERROR_INTERNAL_ERROR;
505 return mouse_cursor_server_context_poll_int(context);
508static BOOL mouse_cursor_server_context_handle(MouseCursorServerContext* context, HANDLE* handle)
510 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
512 WINPR_ASSERT(mouse_cursor);
513 WINPR_ASSERT(handle);
515 if (!mouse_cursor->externalThread)
517 if (mouse_cursor->state == MOUSE_CURSOR_INITIAL)
520 *handle = mouse_cursor_server_get_channel_handle(mouse_cursor);
525static wStream* mouse_cursor_server_packet_new(
size_t size, RDP_MOUSE_CURSOR_PDUTYPE pduType,
531 s = Stream_New(
nullptr, size + RDPEMSC_HEADER_SIZE);
534 WLog_ERR(TAG,
"Stream_New failed!");
538 WINPR_ASSERT(pduType <= UINT8_MAX);
539 Stream_Write_UINT8(s, (BYTE)pduType);
541 WINPR_ASSERT(header->updateType <= UINT8_MAX);
542 Stream_Write_UINT8(s, (BYTE)header->updateType);
543 Stream_Write_UINT16(s, header->reserved);
548static UINT mouse_cursor_server_packet_send(MouseCursorServerContext* context,
wStream* s)
550 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
551 UINT error = CHANNEL_RC_OK;
554 WINPR_ASSERT(mouse_cursor);
557 const size_t pos = Stream_GetPosition(s);
559 WINPR_ASSERT(pos <= UINT32_MAX);
560 if (!WTSVirtualChannelWrite(mouse_cursor->mouse_cursor_channel, Stream_BufferAs(s,
char),
561 (ULONG)pos, &written))
563 WLog_ERR(TAG,
"WTSVirtualChannelWrite failed!");
564 error = ERROR_INTERNAL_ERROR;
568 if (written < Stream_GetPosition(s))
570 WLog_WARN(TAG,
"Unexpected bytes written: %" PRIu32
"/%" PRIuz
"", written,
571 Stream_GetPosition(s));
575 Stream_Free(s, TRUE);
580mouse_cursor_server_send_sc_caps_confirm(MouseCursorServerContext* context,
584 RDP_MOUSE_CURSOR_PDUTYPE pduType = PDUTYPE_EMSC_RESERVED;
585 size_t caps_size = 0;
588 WINPR_ASSERT(context);
589 WINPR_ASSERT(capsConfirm);
591 capsetHeader = capsConfirm->capsSet;
592 WINPR_ASSERT(capsetHeader);
595 switch (capsetHeader->version)
604 pduType = PDUTYPE_SC_CAPS_CONFIRM;
605 s = mouse_cursor_server_packet_new(caps_size, pduType, &capsConfirm->header);
607 return ERROR_NOT_ENOUGH_MEMORY;
609 Stream_Write_UINT32(s, capsetHeader->signature);
610 Stream_Write_UINT32(s, capsetHeader->version);
611 Stream_Write_UINT32(s, capsetHeader->size);
614 switch (capsetHeader->version)
623 return mouse_cursor_server_packet_send(context, s);
629 WINPR_ASSERT(point16);
631 Stream_Write_UINT16(s, point16->xPos);
632 Stream_Write_UINT16(s, point16->yPos);
635static UINT mouse_cursor_server_send_sc_mouseptr_update(
641 RDP_MOUSE_CURSOR_PDUTYPE pduType = PDUTYPE_EMSC_RESERVED;
642 size_t update_size = 0;
645 WINPR_ASSERT(context);
646 WINPR_ASSERT(mouseptrUpdate);
648 position = mouseptrUpdate->position;
649 pointerAttribute = mouseptrUpdate->pointerAttribute;
650 largePointerAttribute = mouseptrUpdate->largePointerAttribute;
652 switch (mouseptrUpdate->header.updateType)
654 case TS_UPDATETYPE_MOUSEPTR_SYSTEM_NULL:
655 case TS_UPDATETYPE_MOUSEPTR_SYSTEM_DEFAULT:
658 case TS_UPDATETYPE_MOUSEPTR_POSITION:
659 WINPR_ASSERT(position);
662 case TS_UPDATETYPE_MOUSEPTR_CACHED:
663 WINPR_ASSERT(mouseptrUpdate->cachedPointerIndex);
666 case TS_UPDATETYPE_MOUSEPTR_POINTER:
667 WINPR_ASSERT(pointerAttribute);
668 update_size = 2 + 2 + 4 + 2 + 2 + 2 + 2;
669 update_size += pointerAttribute->lengthAndMask;
670 update_size += pointerAttribute->lengthXorMask;
672 case TS_UPDATETYPE_MOUSEPTR_LARGE_POINTER:
673 WINPR_ASSERT(largePointerAttribute);
674 update_size = 2 + 2 + 4 + 2 + 2 + 4 + 4;
675 update_size += largePointerAttribute->lengthAndMask;
676 update_size += largePointerAttribute->lengthXorMask;
683 pduType = PDUTYPE_SC_MOUSEPTR_UPDATE;
684 s = mouse_cursor_server_packet_new(update_size, pduType, &mouseptrUpdate->header);
686 return ERROR_NOT_ENOUGH_MEMORY;
688 switch (mouseptrUpdate->header.updateType)
690 case TS_UPDATETYPE_MOUSEPTR_SYSTEM_NULL:
691 case TS_UPDATETYPE_MOUSEPTR_SYSTEM_DEFAULT:
693 case TS_UPDATETYPE_MOUSEPTR_POSITION:
694 write_point16(s, position);
696 case TS_UPDATETYPE_MOUSEPTR_CACHED:
697 Stream_Write_UINT16(s, *mouseptrUpdate->cachedPointerIndex);
699 case TS_UPDATETYPE_MOUSEPTR_POINTER:
700 Stream_Write_UINT16(s, pointerAttribute->xorBpp);
701 Stream_Write_UINT16(s, pointerAttribute->cacheIndex);
702 write_point16(s, &pointerAttribute->hotSpot);
703 Stream_Write_UINT16(s, pointerAttribute->width);
704 Stream_Write_UINT16(s, pointerAttribute->height);
705 Stream_Write_UINT16(s, pointerAttribute->lengthAndMask);
706 Stream_Write_UINT16(s, pointerAttribute->lengthXorMask);
707 Stream_Write(s, pointerAttribute->xorMaskData, pointerAttribute->lengthXorMask);
708 Stream_Write(s, pointerAttribute->andMaskData, pointerAttribute->lengthAndMask);
710 case TS_UPDATETYPE_MOUSEPTR_LARGE_POINTER:
711 Stream_Write_UINT16(s, largePointerAttribute->xorBpp);
712 Stream_Write_UINT16(s, largePointerAttribute->cacheIndex);
713 write_point16(s, &largePointerAttribute->hotSpot);
714 Stream_Write_UINT16(s, largePointerAttribute->width);
715 Stream_Write_UINT16(s, largePointerAttribute->height);
716 Stream_Write_UINT32(s, largePointerAttribute->lengthAndMask);
717 Stream_Write_UINT32(s, largePointerAttribute->lengthXorMask);
718 Stream_Write(s, largePointerAttribute->xorMaskData,
719 largePointerAttribute->lengthXorMask);
720 Stream_Write(s, largePointerAttribute->andMaskData,
721 largePointerAttribute->lengthAndMask);
728 return mouse_cursor_server_packet_send(context, s);
731MouseCursorServerContext* mouse_cursor_server_context_new(HANDLE vcm)
733 mouse_cursor_server* mouse_cursor =
734 (mouse_cursor_server*)calloc(1,
sizeof(mouse_cursor_server));
739 mouse_cursor->context.vcm = vcm;
740 mouse_cursor->context.Initialize = mouse_cursor_server_initialize;
741 mouse_cursor->context.Open = mouse_cursor_server_open;
742 mouse_cursor->context.Close = mouse_cursor_server_close;
743 mouse_cursor->context.Poll = mouse_cursor_server_context_poll;
744 mouse_cursor->context.ChannelHandle = mouse_cursor_server_context_handle;
746 mouse_cursor->context.CapsConfirm = mouse_cursor_server_send_sc_caps_confirm;
747 mouse_cursor->context.MouseptrUpdate = mouse_cursor_server_send_sc_mouseptr_update;
749 mouse_cursor->buffer = Stream_New(
nullptr, 4096);
750 if (!mouse_cursor->buffer)
753 return &mouse_cursor->context;
755 WINPR_PRAGMA_DIAG_PUSH
756 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
757 mouse_cursor_server_context_free(&mouse_cursor->context);
758 WINPR_PRAGMA_DIAG_POP
762void mouse_cursor_server_context_free(MouseCursorServerContext* context)
764 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
768 mouse_cursor_server_close(context);
769 Stream_Free(mouse_cursor->buffer, TRUE);
RDP_MOUSE_CURSOR_CAPVERSION
@ RDP_MOUSE_CURSOR_CAPVERSION_1
This struct contains function pointer to initialize/free objects.
OBJECT_FREE_FN fnObjectFree