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;
118static BOOL read_cap_set(
wStream* s, wArrayList* capsSets)
122 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
125 const UINT32 signature = Stream_Get_UINT32(s);
128 const UINT32 val = Stream_Get_UINT32(s);
135 WLog_WARN(TAG,
"Received caps set with unknown version %" PRIu32, val);
139 const UINT32 size = Stream_Get_UINT32(s);
143 WLog_ERR(TAG,
"Size of caps set is invalid: %u", size);
147 const size_t capsDataSize = size - 12;
148 if (!Stream_CheckAndLogRequiredLength(TAG, s, capsDataSize))
157 WLog_WARN(TAG,
"Unexpected remaining PDU size %" PRIuz
", expected 0! ignoring...",
159 Stream_Seek(s, capsDataSize);
166 capsSet = &capsSetV1->capsetHeader;
170 Stream_Seek(s, capsDataSize);
173 WINPR_ASSERT(capsSet);
175 capsSet->signature = signature;
176 capsSet->version = version;
177 capsSet->size = size;
179 if (!ArrayList_Append(capsSets, capsSet))
181 WLog_ERR(TAG,
"Failed to append caps set to arraylist");
190static UINT mouse_cursor_server_recv_cs_caps_advertise(MouseCursorServerContext* context,
194 UINT error = CHANNEL_RC_OK;
196 WINPR_ASSERT(context);
198 WINPR_ASSERT(header);
201 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
202 return ERROR_NO_DATA;
206 .capsSets = ArrayList_New(FALSE),
211 WLog_ERR(TAG,
"Failed to allocate arraylist");
212 return ERROR_NOT_ENOUGH_MEMORY;
215 wObject* aobj = ArrayList_Object(pdu.capsSets);
219 while (Stream_GetRemainingLength(s) > 0)
221 if (!read_cap_set(s, pdu.capsSets))
223 ArrayList_Free(pdu.capsSets);
224 return ERROR_INVALID_DATA;
228 IFCALLRET(context->CapsAdvertise, error, context, &pdu);
230 WLog_ERR(TAG,
"context->CapsAdvertise failed with error %" PRIu32
"", error);
232 ArrayList_Free(pdu.capsSets);
237static UINT mouse_cursor_process_message(mouse_cursor_server* mouse_cursor)
240 UINT error = ERROR_INTERNAL_ERROR;
241 ULONG BytesReturned = 0;
244 WINPR_ASSERT(mouse_cursor);
245 WINPR_ASSERT(mouse_cursor->mouse_cursor_channel);
247 s = mouse_cursor->buffer;
250 Stream_ResetPosition(s);
251 rc = WTSVirtualChannelRead(mouse_cursor->mouse_cursor_channel, 0,
nullptr, 0, &BytesReturned);
255 if (BytesReturned < 1)
257 error = CHANNEL_RC_OK;
261 if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
263 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
264 error = CHANNEL_RC_NO_MEMORY;
268 if (WTSVirtualChannelRead(mouse_cursor->mouse_cursor_channel, 0, Stream_BufferAs(s,
char),
269 (ULONG)Stream_Capacity(s), &BytesReturned) == FALSE)
271 WLog_ERR(TAG,
"WTSVirtualChannelRead failed!");
275 if (!Stream_SetLength(s, BytesReturned))
278 if (!Stream_CheckAndLogRequiredLength(TAG, s, RDPEMSC_HEADER_SIZE))
279 return ERROR_NO_DATA;
282 const UINT8 pduType = Stream_Get_UINT8(s);
283 const UINT8 updateType = Stream_Get_UINT8(s);
286 case TS_UPDATETYPE_MOUSEPTR_SYSTEM_NULL:
287 case TS_UPDATETYPE_MOUSEPTR_SYSTEM_DEFAULT:
288 case TS_UPDATETYPE_MOUSEPTR_POSITION:
289 case TS_UPDATETYPE_MOUSEPTR_CACHED:
290 case TS_UPDATETYPE_MOUSEPTR_POINTER:
291 case TS_UPDATETYPE_MOUSEPTR_LARGE_POINTER:
295 "mouse_cursor_process_message: unknown or invalid updateType %" PRIu8
"",
297 return ERROR_INVALID_DATA;
301 .reserved = Stream_Get_UINT16(s),
302 .pduType = PDUTYPE_EMSC_RESERVED };
306 case PDUTYPE_CS_CAPS_ADVERTISE:
307 header.pduType = PDUTYPE_CS_CAPS_ADVERTISE;
309 mouse_cursor_server_recv_cs_caps_advertise(&mouse_cursor->context, s, &header);
312 WLog_ERR(TAG,
"mouse_cursor_process_message: unknown or invalid pduType %" PRIu8
"",
320 WLog_ERR(TAG,
"Response failed with error %" PRIu32
"!", error);
325static UINT mouse_cursor_server_context_poll_int(MouseCursorServerContext* context)
327 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
328 UINT error = ERROR_INTERNAL_ERROR;
330 WINPR_ASSERT(mouse_cursor);
332 switch (mouse_cursor->state)
334 case MOUSE_CURSOR_INITIAL:
335 error = mouse_cursor_server_open_channel(mouse_cursor);
337 WLog_ERR(TAG,
"mouse_cursor_server_open_channel failed with error %" PRIu32
"!",
340 mouse_cursor->state = MOUSE_CURSOR_OPENED;
342 case MOUSE_CURSOR_OPENED:
343 error = mouse_cursor_process_message(mouse_cursor);
352static HANDLE mouse_cursor_server_get_channel_handle(mouse_cursor_server* mouse_cursor)
354 void* buffer =
nullptr;
355 DWORD BytesReturned = 0;
356 HANDLE ChannelEvent =
nullptr;
358 WINPR_ASSERT(mouse_cursor);
360 if (WTSVirtualChannelQuery(mouse_cursor->mouse_cursor_channel, WTSVirtualEventHandle, &buffer,
361 &BytesReturned) == TRUE)
363 if (BytesReturned ==
sizeof(HANDLE))
364 ChannelEvent = *(HANDLE*)buffer;
366 WTSFreeMemory(buffer);
372static DWORD WINAPI mouse_cursor_server_thread_func(LPVOID arg)
375 HANDLE events[2] = WINPR_C_ARRAY_INIT;
376 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)arg;
377 UINT error = CHANNEL_RC_OK;
380 WINPR_ASSERT(mouse_cursor);
383 events[nCount++] = mouse_cursor->stopEvent;
385 while ((error == CHANNEL_RC_OK) && (WaitForSingleObject(events[0], 0) != WAIT_OBJECT_0))
387 switch (mouse_cursor->state)
389 case MOUSE_CURSOR_INITIAL:
390 error = mouse_cursor_server_context_poll_int(&mouse_cursor->context);
391 if (error == CHANNEL_RC_OK)
393 events[1] = mouse_cursor_server_get_channel_handle(mouse_cursor);
397 case MOUSE_CURSOR_OPENED:
398 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
403 case WAIT_OBJECT_0 + 1:
405 error = mouse_cursor_server_context_poll_int(&mouse_cursor->context);
410 error = ERROR_INTERNAL_ERROR;
419 (void)WTSVirtualChannelClose(mouse_cursor->mouse_cursor_channel);
420 mouse_cursor->mouse_cursor_channel =
nullptr;
422 if (error && mouse_cursor->context.rdpcontext)
423 setChannelError(mouse_cursor->context.rdpcontext, error,
424 "mouse_cursor_server_thread_func reported an error");
430static UINT mouse_cursor_server_open(MouseCursorServerContext* context)
432 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
434 WINPR_ASSERT(mouse_cursor);
436 if (!mouse_cursor->externalThread && (mouse_cursor->thread ==
nullptr))
438 mouse_cursor->stopEvent = CreateEvent(
nullptr, TRUE, FALSE,
nullptr);
439 if (!mouse_cursor->stopEvent)
441 WLog_ERR(TAG,
"CreateEvent failed!");
442 return ERROR_INTERNAL_ERROR;
445 mouse_cursor->thread =
446 CreateThread(
nullptr, 0, mouse_cursor_server_thread_func, mouse_cursor, 0,
nullptr);
447 if (!mouse_cursor->thread)
449 WLog_ERR(TAG,
"CreateThread failed!");
450 (void)CloseHandle(mouse_cursor->stopEvent);
451 mouse_cursor->stopEvent =
nullptr;
452 return ERROR_INTERNAL_ERROR;
455 mouse_cursor->isOpened = TRUE;
457 return CHANNEL_RC_OK;
460static UINT mouse_cursor_server_close(MouseCursorServerContext* context)
462 UINT error = CHANNEL_RC_OK;
463 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
465 WINPR_ASSERT(mouse_cursor);
467 if (!mouse_cursor->externalThread && mouse_cursor->thread)
469 (void)SetEvent(mouse_cursor->stopEvent);
471 if (WaitForSingleObject(mouse_cursor->thread, INFINITE) == WAIT_FAILED)
473 error = GetLastError();
474 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"", error);
478 (void)CloseHandle(mouse_cursor->thread);
479 (void)CloseHandle(mouse_cursor->stopEvent);
480 mouse_cursor->thread =
nullptr;
481 mouse_cursor->stopEvent =
nullptr;
483 if (mouse_cursor->externalThread)
485 if (mouse_cursor->state != MOUSE_CURSOR_INITIAL)
487 (void)WTSVirtualChannelClose(mouse_cursor->mouse_cursor_channel);
488 mouse_cursor->mouse_cursor_channel =
nullptr;
489 mouse_cursor->state = MOUSE_CURSOR_INITIAL;
492 mouse_cursor->isOpened = FALSE;
497static UINT mouse_cursor_server_context_poll(MouseCursorServerContext* context)
499 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
501 WINPR_ASSERT(mouse_cursor);
503 if (!mouse_cursor->externalThread)
504 return ERROR_INTERNAL_ERROR;
506 return mouse_cursor_server_context_poll_int(context);
509static BOOL mouse_cursor_server_context_handle(MouseCursorServerContext* context, HANDLE* handle)
511 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
513 WINPR_ASSERT(mouse_cursor);
514 WINPR_ASSERT(handle);
516 if (!mouse_cursor->externalThread)
518 if (mouse_cursor->state == MOUSE_CURSOR_INITIAL)
521 *handle = mouse_cursor_server_get_channel_handle(mouse_cursor);
526static wStream* mouse_cursor_server_packet_new(
size_t size, RDP_MOUSE_CURSOR_PDUTYPE pduType,
532 s = Stream_New(
nullptr, size + RDPEMSC_HEADER_SIZE);
535 WLog_ERR(TAG,
"Stream_New failed!");
539 WINPR_ASSERT(pduType <= UINT8_MAX);
540 Stream_Write_UINT8(s, (BYTE)pduType);
542 WINPR_ASSERT(header->updateType <= UINT8_MAX);
543 Stream_Write_UINT8(s, (BYTE)header->updateType);
544 Stream_Write_UINT16(s, header->reserved);
549static UINT mouse_cursor_server_packet_send(MouseCursorServerContext* context,
wStream* s)
551 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
552 UINT error = CHANNEL_RC_OK;
555 WINPR_ASSERT(mouse_cursor);
558 const size_t pos = Stream_GetPosition(s);
560 WINPR_ASSERT(pos <= UINT32_MAX);
561 if (!WTSVirtualChannelWrite(mouse_cursor->mouse_cursor_channel, Stream_BufferAs(s,
char),
562 (ULONG)pos, &written))
564 WLog_ERR(TAG,
"WTSVirtualChannelWrite failed!");
565 error = ERROR_INTERNAL_ERROR;
569 if (written < Stream_GetPosition(s))
571 WLog_WARN(TAG,
"Unexpected bytes written: %" PRIu32
"/%" PRIuz
"", written,
572 Stream_GetPosition(s));
576 Stream_Free(s, TRUE);
581mouse_cursor_server_send_sc_caps_confirm(MouseCursorServerContext* context,
585 RDP_MOUSE_CURSOR_PDUTYPE pduType = PDUTYPE_EMSC_RESERVED;
586 size_t caps_size = 0;
589 WINPR_ASSERT(context);
590 WINPR_ASSERT(capsConfirm);
592 capsetHeader = capsConfirm->capsSet;
593 WINPR_ASSERT(capsetHeader);
596 switch (capsetHeader->version)
605 pduType = PDUTYPE_SC_CAPS_CONFIRM;
606 s = mouse_cursor_server_packet_new(caps_size, pduType, &capsConfirm->header);
608 return ERROR_NOT_ENOUGH_MEMORY;
610 Stream_Write_UINT32(s, capsetHeader->signature);
611 Stream_Write_UINT32(s, capsetHeader->version);
612 Stream_Write_UINT32(s, capsetHeader->size);
615 switch (capsetHeader->version)
624 return mouse_cursor_server_packet_send(context, s);
630 WINPR_ASSERT(point16);
632 Stream_Write_UINT16(s, point16->xPos);
633 Stream_Write_UINT16(s, point16->yPos);
636static UINT mouse_cursor_server_send_sc_mouseptr_update(
642 RDP_MOUSE_CURSOR_PDUTYPE pduType = PDUTYPE_EMSC_RESERVED;
643 size_t update_size = 0;
646 WINPR_ASSERT(context);
647 WINPR_ASSERT(mouseptrUpdate);
649 position = mouseptrUpdate->position;
650 pointerAttribute = mouseptrUpdate->pointerAttribute;
651 largePointerAttribute = mouseptrUpdate->largePointerAttribute;
653 switch (mouseptrUpdate->header.updateType)
655 case TS_UPDATETYPE_MOUSEPTR_SYSTEM_NULL:
656 case TS_UPDATETYPE_MOUSEPTR_SYSTEM_DEFAULT:
659 case TS_UPDATETYPE_MOUSEPTR_POSITION:
660 WINPR_ASSERT(position);
663 case TS_UPDATETYPE_MOUSEPTR_CACHED:
664 WINPR_ASSERT(mouseptrUpdate->cachedPointerIndex);
667 case TS_UPDATETYPE_MOUSEPTR_POINTER:
668 WINPR_ASSERT(pointerAttribute);
669 update_size = 2 + 2 + 4 + 2 + 2 + 2 + 2;
670 update_size += pointerAttribute->lengthAndMask;
671 update_size += pointerAttribute->lengthXorMask;
673 case TS_UPDATETYPE_MOUSEPTR_LARGE_POINTER:
674 WINPR_ASSERT(largePointerAttribute);
675 update_size = 2 + 2 + 4 + 2 + 2 + 4 + 4;
676 update_size += largePointerAttribute->lengthAndMask;
677 update_size += largePointerAttribute->lengthXorMask;
684 pduType = PDUTYPE_SC_MOUSEPTR_UPDATE;
685 s = mouse_cursor_server_packet_new(update_size, pduType, &mouseptrUpdate->header);
687 return ERROR_NOT_ENOUGH_MEMORY;
689 switch (mouseptrUpdate->header.updateType)
691 case TS_UPDATETYPE_MOUSEPTR_SYSTEM_NULL:
692 case TS_UPDATETYPE_MOUSEPTR_SYSTEM_DEFAULT:
694 case TS_UPDATETYPE_MOUSEPTR_POSITION:
695 write_point16(s, position);
697 case TS_UPDATETYPE_MOUSEPTR_CACHED:
698 Stream_Write_UINT16(s, *mouseptrUpdate->cachedPointerIndex);
700 case TS_UPDATETYPE_MOUSEPTR_POINTER:
701 Stream_Write_UINT16(s, pointerAttribute->xorBpp);
702 Stream_Write_UINT16(s, pointerAttribute->cacheIndex);
703 write_point16(s, &pointerAttribute->hotSpot);
704 Stream_Write_UINT16(s, pointerAttribute->width);
705 Stream_Write_UINT16(s, pointerAttribute->height);
706 Stream_Write_UINT16(s, pointerAttribute->lengthAndMask);
707 Stream_Write_UINT16(s, pointerAttribute->lengthXorMask);
708 Stream_Write(s, pointerAttribute->xorMaskData, pointerAttribute->lengthXorMask);
709 Stream_Write(s, pointerAttribute->andMaskData, pointerAttribute->lengthAndMask);
711 case TS_UPDATETYPE_MOUSEPTR_LARGE_POINTER:
712 Stream_Write_UINT16(s, largePointerAttribute->xorBpp);
713 Stream_Write_UINT16(s, largePointerAttribute->cacheIndex);
714 write_point16(s, &largePointerAttribute->hotSpot);
715 Stream_Write_UINT16(s, largePointerAttribute->width);
716 Stream_Write_UINT16(s, largePointerAttribute->height);
717 Stream_Write_UINT32(s, largePointerAttribute->lengthAndMask);
718 Stream_Write_UINT32(s, largePointerAttribute->lengthXorMask);
719 Stream_Write(s, largePointerAttribute->xorMaskData,
720 largePointerAttribute->lengthXorMask);
721 Stream_Write(s, largePointerAttribute->andMaskData,
722 largePointerAttribute->lengthAndMask);
729 return mouse_cursor_server_packet_send(context, s);
732MouseCursorServerContext* mouse_cursor_server_context_new(HANDLE vcm)
734 mouse_cursor_server* mouse_cursor =
735 (mouse_cursor_server*)calloc(1,
sizeof(mouse_cursor_server));
740 mouse_cursor->context.vcm = vcm;
741 mouse_cursor->context.Initialize = mouse_cursor_server_initialize;
742 mouse_cursor->context.Open = mouse_cursor_server_open;
743 mouse_cursor->context.Close = mouse_cursor_server_close;
744 mouse_cursor->context.Poll = mouse_cursor_server_context_poll;
745 mouse_cursor->context.ChannelHandle = mouse_cursor_server_context_handle;
747 mouse_cursor->context.CapsConfirm = mouse_cursor_server_send_sc_caps_confirm;
748 mouse_cursor->context.MouseptrUpdate = mouse_cursor_server_send_sc_mouseptr_update;
750 mouse_cursor->buffer = Stream_New(
nullptr, 4096);
751 if (!mouse_cursor->buffer)
754 return &mouse_cursor->context;
756 WINPR_PRAGMA_DIAG_PUSH
757 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
758 mouse_cursor_server_context_free(&mouse_cursor->context);
759 WINPR_PRAGMA_DIAG_POP
763void mouse_cursor_server_context_free(MouseCursorServerContext* context)
765 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
769 mouse_cursor_server_close(context);
770 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