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,
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_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;
298 .reserved = Stream_Get_UINT16(s),
299 .pduType = PDUTYPE_EMSC_RESERVED };
303 case PDUTYPE_CS_CAPS_ADVERTISE:
304 header.pduType = PDUTYPE_CS_CAPS_ADVERTISE;
306 mouse_cursor_server_recv_cs_caps_advertise(&mouse_cursor->context, s, &header);
309 WLog_ERR(TAG,
"mouse_cursor_process_message: unknown or invalid pduType %" PRIu8
"",
317 WLog_ERR(TAG,
"Response failed with error %" PRIu32
"!", error);
322static UINT mouse_cursor_server_context_poll_int(MouseCursorServerContext* context)
324 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
325 UINT error = ERROR_INTERNAL_ERROR;
327 WINPR_ASSERT(mouse_cursor);
329 switch (mouse_cursor->state)
331 case MOUSE_CURSOR_INITIAL:
332 error = mouse_cursor_server_open_channel(mouse_cursor);
334 WLog_ERR(TAG,
"mouse_cursor_server_open_channel failed with error %" PRIu32
"!",
337 mouse_cursor->state = MOUSE_CURSOR_OPENED;
339 case MOUSE_CURSOR_OPENED:
340 error = mouse_cursor_process_message(mouse_cursor);
349static HANDLE mouse_cursor_server_get_channel_handle(mouse_cursor_server* mouse_cursor)
352 DWORD BytesReturned = 0;
353 HANDLE ChannelEvent = NULL;
355 WINPR_ASSERT(mouse_cursor);
357 if (WTSVirtualChannelQuery(mouse_cursor->mouse_cursor_channel, WTSVirtualEventHandle, &buffer,
358 &BytesReturned) == TRUE)
360 if (BytesReturned ==
sizeof(HANDLE))
361 ChannelEvent = *(HANDLE*)buffer;
363 WTSFreeMemory(buffer);
369static DWORD WINAPI mouse_cursor_server_thread_func(LPVOID arg)
372 HANDLE events[2] = { 0 };
373 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)arg;
374 UINT error = CHANNEL_RC_OK;
377 WINPR_ASSERT(mouse_cursor);
380 events[nCount++] = mouse_cursor->stopEvent;
382 while ((error == CHANNEL_RC_OK) && (WaitForSingleObject(events[0], 0) != WAIT_OBJECT_0))
384 switch (mouse_cursor->state)
386 case MOUSE_CURSOR_INITIAL:
387 error = mouse_cursor_server_context_poll_int(&mouse_cursor->context);
388 if (error == CHANNEL_RC_OK)
390 events[1] = mouse_cursor_server_get_channel_handle(mouse_cursor);
394 case MOUSE_CURSOR_OPENED:
395 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
400 case WAIT_OBJECT_0 + 1:
402 error = mouse_cursor_server_context_poll_int(&mouse_cursor->context);
407 error = ERROR_INTERNAL_ERROR;
416 (void)WTSVirtualChannelClose(mouse_cursor->mouse_cursor_channel);
417 mouse_cursor->mouse_cursor_channel = NULL;
419 if (error && mouse_cursor->context.rdpcontext)
420 setChannelError(mouse_cursor->context.rdpcontext, error,
421 "mouse_cursor_server_thread_func reported an error");
427static UINT mouse_cursor_server_open(MouseCursorServerContext* context)
429 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
431 WINPR_ASSERT(mouse_cursor);
433 if (!mouse_cursor->externalThread && (mouse_cursor->thread == NULL))
435 mouse_cursor->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
436 if (!mouse_cursor->stopEvent)
438 WLog_ERR(TAG,
"CreateEvent failed!");
439 return ERROR_INTERNAL_ERROR;
442 mouse_cursor->thread =
443 CreateThread(NULL, 0, mouse_cursor_server_thread_func, mouse_cursor, 0, NULL);
444 if (!mouse_cursor->thread)
446 WLog_ERR(TAG,
"CreateThread failed!");
447 (void)CloseHandle(mouse_cursor->stopEvent);
448 mouse_cursor->stopEvent = NULL;
449 return ERROR_INTERNAL_ERROR;
452 mouse_cursor->isOpened = TRUE;
454 return CHANNEL_RC_OK;
457static UINT mouse_cursor_server_close(MouseCursorServerContext* context)
459 UINT error = CHANNEL_RC_OK;
460 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
462 WINPR_ASSERT(mouse_cursor);
464 if (!mouse_cursor->externalThread && mouse_cursor->thread)
466 (void)SetEvent(mouse_cursor->stopEvent);
468 if (WaitForSingleObject(mouse_cursor->thread, INFINITE) == WAIT_FAILED)
470 error = GetLastError();
471 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"", error);
475 (void)CloseHandle(mouse_cursor->thread);
476 (void)CloseHandle(mouse_cursor->stopEvent);
477 mouse_cursor->thread = NULL;
478 mouse_cursor->stopEvent = NULL;
480 if (mouse_cursor->externalThread)
482 if (mouse_cursor->state != MOUSE_CURSOR_INITIAL)
484 (void)WTSVirtualChannelClose(mouse_cursor->mouse_cursor_channel);
485 mouse_cursor->mouse_cursor_channel = NULL;
486 mouse_cursor->state = MOUSE_CURSOR_INITIAL;
489 mouse_cursor->isOpened = FALSE;
494static UINT mouse_cursor_server_context_poll(MouseCursorServerContext* context)
496 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
498 WINPR_ASSERT(mouse_cursor);
500 if (!mouse_cursor->externalThread)
501 return ERROR_INTERNAL_ERROR;
503 return mouse_cursor_server_context_poll_int(context);
506static BOOL mouse_cursor_server_context_handle(MouseCursorServerContext* context, HANDLE* handle)
508 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
510 WINPR_ASSERT(mouse_cursor);
511 WINPR_ASSERT(handle);
513 if (!mouse_cursor->externalThread)
515 if (mouse_cursor->state == MOUSE_CURSOR_INITIAL)
518 *handle = mouse_cursor_server_get_channel_handle(mouse_cursor);
523static wStream* mouse_cursor_server_packet_new(
size_t size, RDP_MOUSE_CURSOR_PDUTYPE pduType,
529 s = Stream_New(NULL, size + RDPEMSC_HEADER_SIZE);
532 WLog_ERR(TAG,
"Stream_New failed!");
536 WINPR_ASSERT(pduType <= UINT8_MAX);
537 Stream_Write_UINT8(s, (BYTE)pduType);
539 WINPR_ASSERT(header->updateType <= UINT8_MAX);
540 Stream_Write_UINT8(s, (BYTE)header->updateType);
541 Stream_Write_UINT16(s, header->reserved);
546static UINT mouse_cursor_server_packet_send(MouseCursorServerContext* context,
wStream* s)
548 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
549 UINT error = CHANNEL_RC_OK;
552 WINPR_ASSERT(mouse_cursor);
555 const size_t pos = Stream_GetPosition(s);
557 WINPR_ASSERT(pos <= UINT32_MAX);
558 if (!WTSVirtualChannelWrite(mouse_cursor->mouse_cursor_channel, Stream_BufferAs(s,
char),
559 (ULONG)pos, &written))
561 WLog_ERR(TAG,
"WTSVirtualChannelWrite failed!");
562 error = ERROR_INTERNAL_ERROR;
566 if (written < Stream_GetPosition(s))
568 WLog_WARN(TAG,
"Unexpected bytes written: %" PRIu32
"/%" PRIuz
"", written,
569 Stream_GetPosition(s));
573 Stream_Free(s, TRUE);
578mouse_cursor_server_send_sc_caps_confirm(MouseCursorServerContext* context,
582 RDP_MOUSE_CURSOR_PDUTYPE pduType = PDUTYPE_EMSC_RESERVED;
583 size_t caps_size = 0;
586 WINPR_ASSERT(context);
587 WINPR_ASSERT(capsConfirm);
589 capsetHeader = capsConfirm->capsSet;
590 WINPR_ASSERT(capsetHeader);
593 switch (capsetHeader->version)
602 pduType = PDUTYPE_SC_CAPS_CONFIRM;
603 s = mouse_cursor_server_packet_new(caps_size, pduType, &capsConfirm->header);
605 return ERROR_NOT_ENOUGH_MEMORY;
607 Stream_Write_UINT32(s, capsetHeader->signature);
608 Stream_Write_UINT32(s, capsetHeader->version);
609 Stream_Write_UINT32(s, capsetHeader->size);
612 switch (capsetHeader->version)
621 return mouse_cursor_server_packet_send(context, s);
627 WINPR_ASSERT(point16);
629 Stream_Write_UINT16(s, point16->xPos);
630 Stream_Write_UINT16(s, point16->yPos);
633static UINT mouse_cursor_server_send_sc_mouseptr_update(
639 RDP_MOUSE_CURSOR_PDUTYPE pduType = PDUTYPE_EMSC_RESERVED;
640 size_t update_size = 0;
643 WINPR_ASSERT(context);
644 WINPR_ASSERT(mouseptrUpdate);
646 position = mouseptrUpdate->position;
647 pointerAttribute = mouseptrUpdate->pointerAttribute;
648 largePointerAttribute = mouseptrUpdate->largePointerAttribute;
650 switch (mouseptrUpdate->header.updateType)
652 case TS_UPDATETYPE_MOUSEPTR_SYSTEM_NULL:
653 case TS_UPDATETYPE_MOUSEPTR_SYSTEM_DEFAULT:
656 case TS_UPDATETYPE_MOUSEPTR_POSITION:
657 WINPR_ASSERT(position);
660 case TS_UPDATETYPE_MOUSEPTR_CACHED:
661 WINPR_ASSERT(mouseptrUpdate->cachedPointerIndex);
664 case TS_UPDATETYPE_MOUSEPTR_POINTER:
665 WINPR_ASSERT(pointerAttribute);
666 update_size = 2 + 2 + 4 + 2 + 2 + 2 + 2;
667 update_size += pointerAttribute->lengthAndMask;
668 update_size += pointerAttribute->lengthXorMask;
670 case TS_UPDATETYPE_MOUSEPTR_LARGE_POINTER:
671 WINPR_ASSERT(largePointerAttribute);
672 update_size = 2 + 2 + 4 + 2 + 2 + 4 + 4;
673 update_size += largePointerAttribute->lengthAndMask;
674 update_size += largePointerAttribute->lengthXorMask;
681 pduType = PDUTYPE_SC_MOUSEPTR_UPDATE;
682 s = mouse_cursor_server_packet_new(update_size, pduType, &mouseptrUpdate->header);
684 return ERROR_NOT_ENOUGH_MEMORY;
686 switch (mouseptrUpdate->header.updateType)
688 case TS_UPDATETYPE_MOUSEPTR_SYSTEM_NULL:
689 case TS_UPDATETYPE_MOUSEPTR_SYSTEM_DEFAULT:
691 case TS_UPDATETYPE_MOUSEPTR_POSITION:
692 write_point16(s, position);
694 case TS_UPDATETYPE_MOUSEPTR_CACHED:
695 Stream_Write_UINT16(s, *mouseptrUpdate->cachedPointerIndex);
697 case TS_UPDATETYPE_MOUSEPTR_POINTER:
698 Stream_Write_UINT16(s, pointerAttribute->xorBpp);
699 Stream_Write_UINT16(s, pointerAttribute->cacheIndex);
700 write_point16(s, &pointerAttribute->hotSpot);
701 Stream_Write_UINT16(s, pointerAttribute->width);
702 Stream_Write_UINT16(s, pointerAttribute->height);
703 Stream_Write_UINT16(s, pointerAttribute->lengthAndMask);
704 Stream_Write_UINT16(s, pointerAttribute->lengthXorMask);
705 Stream_Write(s, pointerAttribute->xorMaskData, pointerAttribute->lengthXorMask);
706 Stream_Write(s, pointerAttribute->andMaskData, pointerAttribute->lengthAndMask);
708 case TS_UPDATETYPE_MOUSEPTR_LARGE_POINTER:
709 Stream_Write_UINT16(s, largePointerAttribute->xorBpp);
710 Stream_Write_UINT16(s, largePointerAttribute->cacheIndex);
711 write_point16(s, &largePointerAttribute->hotSpot);
712 Stream_Write_UINT16(s, largePointerAttribute->width);
713 Stream_Write_UINT16(s, largePointerAttribute->height);
714 Stream_Write_UINT32(s, largePointerAttribute->lengthAndMask);
715 Stream_Write_UINT32(s, largePointerAttribute->lengthXorMask);
716 Stream_Write(s, largePointerAttribute->xorMaskData,
717 largePointerAttribute->lengthXorMask);
718 Stream_Write(s, largePointerAttribute->andMaskData,
719 largePointerAttribute->lengthAndMask);
726 return mouse_cursor_server_packet_send(context, s);
729MouseCursorServerContext* mouse_cursor_server_context_new(HANDLE vcm)
731 mouse_cursor_server* mouse_cursor =
732 (mouse_cursor_server*)calloc(1,
sizeof(mouse_cursor_server));
737 mouse_cursor->context.vcm = vcm;
738 mouse_cursor->context.Initialize = mouse_cursor_server_initialize;
739 mouse_cursor->context.Open = mouse_cursor_server_open;
740 mouse_cursor->context.Close = mouse_cursor_server_close;
741 mouse_cursor->context.Poll = mouse_cursor_server_context_poll;
742 mouse_cursor->context.ChannelHandle = mouse_cursor_server_context_handle;
744 mouse_cursor->context.CapsConfirm = mouse_cursor_server_send_sc_caps_confirm;
745 mouse_cursor->context.MouseptrUpdate = mouse_cursor_server_send_sc_mouseptr_update;
747 mouse_cursor->buffer = Stream_New(NULL, 4096);
748 if (!mouse_cursor->buffer)
751 return &mouse_cursor->context;
753 WINPR_PRAGMA_DIAG_PUSH
754 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
755 mouse_cursor_server_context_free(&mouse_cursor->context);
756 WINPR_PRAGMA_DIAG_POP
760void mouse_cursor_server_context_free(MouseCursorServerContext* context)
762 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
766 mouse_cursor_server_close(context);
767 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