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;
52 } mouse_cursor_server;
54 static 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;
73 static 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;
117 static BOOL read_cap_set(
wStream* s, wArrayList* capsSets)
120 UINT32 signature = 0;
123 size_t capsDataSize = 0;
125 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
128 Stream_Read_UINT32(s, signature);
129 Stream_Read_UINT32(s, version);
130 Stream_Read_UINT32(s, size);
134 WLog_ERR(TAG,
"Size of caps set is invalid: %u", size);
138 capsDataSize = size - 12;
139 if (!Stream_CheckAndLogRequiredLength(TAG, s, capsDataSize))
156 WLog_WARN(TAG,
"Received caps set with unknown version %u", version);
157 Stream_Seek(s, capsDataSize);
160 WINPR_ASSERT(capsSet);
162 capsSet->signature = signature;
163 capsSet->version = version;
164 capsSet->size = size;
166 if (!ArrayList_Append(capsSets, capsSet))
168 WLog_ERR(TAG,
"Failed to append caps set to arraylist");
177 static UINT mouse_cursor_server_recv_cs_caps_advertise(MouseCursorServerContext* context,
182 UINT error = CHANNEL_RC_OK;
184 WINPR_ASSERT(context);
186 WINPR_ASSERT(header);
188 pdu.header = *header;
191 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
192 return ERROR_NO_DATA;
194 pdu.capsSets = ArrayList_New(FALSE);
197 WLog_ERR(TAG,
"Failed to allocate arraylist");
198 return ERROR_NOT_ENOUGH_MEMORY;
201 wObject* aobj = ArrayList_Object(pdu.capsSets);
203 aobj->fnObjectFree = free;
205 while (Stream_GetRemainingLength(s) > 0)
207 if (!read_cap_set(s, pdu.capsSets))
209 ArrayList_Free(pdu.capsSets);
210 return ERROR_INVALID_DATA;
214 IFCALLRET(context->CapsAdvertise, error, context, &pdu);
216 WLog_ERR(TAG,
"context->CapsAdvertise failed with error %" PRIu32
"", error);
218 ArrayList_Free(pdu.capsSets);
223 static UINT mouse_cursor_process_message(mouse_cursor_server* mouse_cursor)
226 UINT error = ERROR_INTERNAL_ERROR;
227 ULONG BytesReturned = 0;
231 WINPR_ASSERT(mouse_cursor);
232 WINPR_ASSERT(mouse_cursor->mouse_cursor_channel);
234 s = mouse_cursor->buffer;
237 Stream_SetPosition(s, 0);
238 rc = WTSVirtualChannelRead(mouse_cursor->mouse_cursor_channel, 0, NULL, 0, &BytesReturned);
242 if (BytesReturned < 1)
244 error = CHANNEL_RC_OK;
248 if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
250 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
251 error = CHANNEL_RC_NO_MEMORY;
255 if (WTSVirtualChannelRead(mouse_cursor->mouse_cursor_channel, 0, Stream_BufferAs(s,
char),
256 (ULONG)Stream_Capacity(s), &BytesReturned) == FALSE)
258 WLog_ERR(TAG,
"WTSVirtualChannelRead failed!");
262 Stream_SetLength(s, BytesReturned);
263 if (!Stream_CheckAndLogRequiredLength(TAG, s, RDPEMSC_HEADER_SIZE))
264 return ERROR_NO_DATA;
266 Stream_Read_UINT8(s, header.pduType);
267 Stream_Read_UINT8(s, header.updateType);
268 Stream_Read_UINT16(s, header.reserved);
270 switch (header.pduType)
272 case PDUTYPE_CS_CAPS_ADVERTISE:
273 error = mouse_cursor_server_recv_cs_caps_advertise(&mouse_cursor->context, s, &header);
276 WLog_ERR(TAG,
"mouse_cursor_process_message: unknown or invalid pduType %" PRIu8
"",
283 WLog_ERR(TAG,
"Response failed with error %" PRIu32
"!", error);
288 static UINT mouse_cursor_server_context_poll_int(MouseCursorServerContext* context)
290 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
291 UINT error = ERROR_INTERNAL_ERROR;
293 WINPR_ASSERT(mouse_cursor);
295 switch (mouse_cursor->state)
297 case MOUSE_CURSOR_INITIAL:
298 error = mouse_cursor_server_open_channel(mouse_cursor);
300 WLog_ERR(TAG,
"mouse_cursor_server_open_channel failed with error %" PRIu32
"!",
303 mouse_cursor->state = MOUSE_CURSOR_OPENED;
305 case MOUSE_CURSOR_OPENED:
306 error = mouse_cursor_process_message(mouse_cursor);
315 static HANDLE mouse_cursor_server_get_channel_handle(mouse_cursor_server* mouse_cursor)
318 DWORD BytesReturned = 0;
319 HANDLE ChannelEvent = NULL;
321 WINPR_ASSERT(mouse_cursor);
323 if (WTSVirtualChannelQuery(mouse_cursor->mouse_cursor_channel, WTSVirtualEventHandle, &buffer,
324 &BytesReturned) == TRUE)
326 if (BytesReturned ==
sizeof(HANDLE))
327 CopyMemory(&ChannelEvent, buffer,
sizeof(HANDLE));
329 WTSFreeMemory(buffer);
335 static DWORD WINAPI mouse_cursor_server_thread_func(LPVOID arg)
338 HANDLE events[2] = { 0 };
339 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)arg;
340 UINT error = CHANNEL_RC_OK;
343 WINPR_ASSERT(mouse_cursor);
346 events[nCount++] = mouse_cursor->stopEvent;
348 while ((error == CHANNEL_RC_OK) && (WaitForSingleObject(events[0], 0) != WAIT_OBJECT_0))
350 switch (mouse_cursor->state)
352 case MOUSE_CURSOR_INITIAL:
353 error = mouse_cursor_server_context_poll_int(&mouse_cursor->context);
354 if (error == CHANNEL_RC_OK)
356 events[1] = mouse_cursor_server_get_channel_handle(mouse_cursor);
360 case MOUSE_CURSOR_OPENED:
361 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
366 case WAIT_OBJECT_0 + 1:
368 error = mouse_cursor_server_context_poll_int(&mouse_cursor->context);
373 error = ERROR_INTERNAL_ERROR;
382 (void)WTSVirtualChannelClose(mouse_cursor->mouse_cursor_channel);
383 mouse_cursor->mouse_cursor_channel = NULL;
385 if (error && mouse_cursor->context.rdpcontext)
386 setChannelError(mouse_cursor->context.rdpcontext, error,
387 "mouse_cursor_server_thread_func reported an error");
393 static UINT mouse_cursor_server_open(MouseCursorServerContext* context)
395 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
397 WINPR_ASSERT(mouse_cursor);
399 if (!mouse_cursor->externalThread && (mouse_cursor->thread == NULL))
401 mouse_cursor->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
402 if (!mouse_cursor->stopEvent)
404 WLog_ERR(TAG,
"CreateEvent failed!");
405 return ERROR_INTERNAL_ERROR;
408 mouse_cursor->thread =
409 CreateThread(NULL, 0, mouse_cursor_server_thread_func, mouse_cursor, 0, NULL);
410 if (!mouse_cursor->thread)
412 WLog_ERR(TAG,
"CreateThread failed!");
413 (void)CloseHandle(mouse_cursor->stopEvent);
414 mouse_cursor->stopEvent = NULL;
415 return ERROR_INTERNAL_ERROR;
418 mouse_cursor->isOpened = TRUE;
420 return CHANNEL_RC_OK;
423 static UINT mouse_cursor_server_close(MouseCursorServerContext* context)
425 UINT error = CHANNEL_RC_OK;
426 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
428 WINPR_ASSERT(mouse_cursor);
430 if (!mouse_cursor->externalThread && mouse_cursor->thread)
432 (void)SetEvent(mouse_cursor->stopEvent);
434 if (WaitForSingleObject(mouse_cursor->thread, INFINITE) == WAIT_FAILED)
436 error = GetLastError();
437 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"", error);
441 (void)CloseHandle(mouse_cursor->thread);
442 (void)CloseHandle(mouse_cursor->stopEvent);
443 mouse_cursor->thread = NULL;
444 mouse_cursor->stopEvent = NULL;
446 if (mouse_cursor->externalThread)
448 if (mouse_cursor->state != MOUSE_CURSOR_INITIAL)
450 (void)WTSVirtualChannelClose(mouse_cursor->mouse_cursor_channel);
451 mouse_cursor->mouse_cursor_channel = NULL;
452 mouse_cursor->state = MOUSE_CURSOR_INITIAL;
455 mouse_cursor->isOpened = FALSE;
460 static UINT mouse_cursor_server_context_poll(MouseCursorServerContext* context)
462 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
464 WINPR_ASSERT(mouse_cursor);
466 if (!mouse_cursor->externalThread)
467 return ERROR_INTERNAL_ERROR;
469 return mouse_cursor_server_context_poll_int(context);
472 static BOOL mouse_cursor_server_context_handle(MouseCursorServerContext* context, HANDLE* handle)
474 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
476 WINPR_ASSERT(mouse_cursor);
477 WINPR_ASSERT(handle);
479 if (!mouse_cursor->externalThread)
481 if (mouse_cursor->state == MOUSE_CURSOR_INITIAL)
484 *handle = mouse_cursor_server_get_channel_handle(mouse_cursor);
489 static wStream* mouse_cursor_server_packet_new(
size_t size, RDP_MOUSE_CURSOR_PDUTYPE pduType,
495 s = Stream_New(NULL, size + RDPEMSC_HEADER_SIZE);
498 WLog_ERR(TAG,
"Stream_New failed!");
502 WINPR_ASSERT(pduType <= UINT8_MAX);
503 Stream_Write_UINT8(s, (BYTE)pduType);
505 WINPR_ASSERT(header->updateType <= UINT8_MAX);
506 Stream_Write_UINT8(s, (BYTE)header->updateType);
507 Stream_Write_UINT16(s, header->reserved);
512 static UINT mouse_cursor_server_packet_send(MouseCursorServerContext* context,
wStream* s)
514 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
515 UINT error = CHANNEL_RC_OK;
518 WINPR_ASSERT(mouse_cursor);
521 const size_t pos = Stream_GetPosition(s);
523 WINPR_ASSERT(pos <= UINT32_MAX);
524 if (!WTSVirtualChannelWrite(mouse_cursor->mouse_cursor_channel, Stream_BufferAs(s,
char),
525 (ULONG)pos, &written))
527 WLog_ERR(TAG,
"WTSVirtualChannelWrite failed!");
528 error = ERROR_INTERNAL_ERROR;
532 if (written < Stream_GetPosition(s))
534 WLog_WARN(TAG,
"Unexpected bytes written: %" PRIu32
"/%" PRIuz
"", written,
535 Stream_GetPosition(s));
539 Stream_Free(s, TRUE);
544 mouse_cursor_server_send_sc_caps_confirm(MouseCursorServerContext* context,
548 RDP_MOUSE_CURSOR_PDUTYPE pduType = PDUTYPE_EMSC_RESERVED;
549 size_t caps_size = 0;
552 WINPR_ASSERT(context);
553 WINPR_ASSERT(capsConfirm);
555 capsetHeader = capsConfirm->capsSet;
556 WINPR_ASSERT(capsetHeader);
559 switch (capsetHeader->version)
568 pduType = PDUTYPE_SC_CAPS_CONFIRM;
569 s = mouse_cursor_server_packet_new(caps_size, pduType, &capsConfirm->header);
571 return ERROR_NOT_ENOUGH_MEMORY;
573 Stream_Write_UINT32(s, capsetHeader->signature);
574 Stream_Write_UINT32(s, capsetHeader->version);
575 Stream_Write_UINT32(s, capsetHeader->size);
578 switch (capsetHeader->version)
587 return mouse_cursor_server_packet_send(context, s);
593 WINPR_ASSERT(point16);
595 Stream_Write_UINT16(s, point16->xPos);
596 Stream_Write_UINT16(s, point16->yPos);
599 static UINT mouse_cursor_server_send_sc_mouseptr_update(
605 RDP_MOUSE_CURSOR_PDUTYPE pduType = PDUTYPE_EMSC_RESERVED;
606 size_t update_size = 0;
609 WINPR_ASSERT(context);
610 WINPR_ASSERT(mouseptrUpdate);
612 position = mouseptrUpdate->position;
613 pointerAttribute = mouseptrUpdate->pointerAttribute;
614 largePointerAttribute = mouseptrUpdate->largePointerAttribute;
616 switch (mouseptrUpdate->header.updateType)
618 case TS_UPDATETYPE_MOUSEPTR_SYSTEM_NULL:
619 case TS_UPDATETYPE_MOUSEPTR_SYSTEM_DEFAULT:
622 case TS_UPDATETYPE_MOUSEPTR_POSITION:
623 WINPR_ASSERT(position);
626 case TS_UPDATETYPE_MOUSEPTR_CACHED:
627 WINPR_ASSERT(mouseptrUpdate->cachedPointerIndex);
630 case TS_UPDATETYPE_MOUSEPTR_POINTER:
631 WINPR_ASSERT(pointerAttribute);
632 update_size = 2 + 2 + 4 + 2 + 2 + 2 + 2;
633 update_size += pointerAttribute->lengthAndMask;
634 update_size += pointerAttribute->lengthXorMask;
636 case TS_UPDATETYPE_MOUSEPTR_LARGE_POINTER:
637 WINPR_ASSERT(largePointerAttribute);
638 update_size = 2 + 2 + 4 + 2 + 2 + 4 + 4;
639 update_size += largePointerAttribute->lengthAndMask;
640 update_size += largePointerAttribute->lengthXorMask;
647 pduType = PDUTYPE_SC_MOUSEPTR_UPDATE;
648 s = mouse_cursor_server_packet_new(update_size, pduType, &mouseptrUpdate->header);
650 return ERROR_NOT_ENOUGH_MEMORY;
652 switch (mouseptrUpdate->header.updateType)
654 case TS_UPDATETYPE_MOUSEPTR_SYSTEM_NULL:
655 case TS_UPDATETYPE_MOUSEPTR_SYSTEM_DEFAULT:
657 case TS_UPDATETYPE_MOUSEPTR_POSITION:
658 write_point16(s, position);
660 case TS_UPDATETYPE_MOUSEPTR_CACHED:
661 Stream_Write_UINT16(s, *mouseptrUpdate->cachedPointerIndex);
663 case TS_UPDATETYPE_MOUSEPTR_POINTER:
664 Stream_Write_UINT16(s, pointerAttribute->xorBpp);
665 Stream_Write_UINT16(s, pointerAttribute->cacheIndex);
666 write_point16(s, &pointerAttribute->hotSpot);
667 Stream_Write_UINT16(s, pointerAttribute->width);
668 Stream_Write_UINT16(s, pointerAttribute->height);
669 Stream_Write_UINT16(s, pointerAttribute->lengthAndMask);
670 Stream_Write_UINT16(s, pointerAttribute->lengthXorMask);
671 Stream_Write(s, pointerAttribute->xorMaskData, pointerAttribute->lengthXorMask);
672 Stream_Write(s, pointerAttribute->andMaskData, pointerAttribute->lengthAndMask);
674 case TS_UPDATETYPE_MOUSEPTR_LARGE_POINTER:
675 Stream_Write_UINT16(s, largePointerAttribute->xorBpp);
676 Stream_Write_UINT16(s, largePointerAttribute->cacheIndex);
677 write_point16(s, &largePointerAttribute->hotSpot);
678 Stream_Write_UINT16(s, largePointerAttribute->width);
679 Stream_Write_UINT16(s, largePointerAttribute->height);
680 Stream_Write_UINT32(s, largePointerAttribute->lengthAndMask);
681 Stream_Write_UINT32(s, largePointerAttribute->lengthXorMask);
682 Stream_Write(s, largePointerAttribute->xorMaskData,
683 largePointerAttribute->lengthXorMask);
684 Stream_Write(s, largePointerAttribute->andMaskData,
685 largePointerAttribute->lengthAndMask);
692 return mouse_cursor_server_packet_send(context, s);
695 MouseCursorServerContext* mouse_cursor_server_context_new(HANDLE vcm)
697 mouse_cursor_server* mouse_cursor =
698 (mouse_cursor_server*)calloc(1,
sizeof(mouse_cursor_server));
703 mouse_cursor->context.vcm = vcm;
704 mouse_cursor->context.Initialize = mouse_cursor_server_initialize;
705 mouse_cursor->context.Open = mouse_cursor_server_open;
706 mouse_cursor->context.Close = mouse_cursor_server_close;
707 mouse_cursor->context.Poll = mouse_cursor_server_context_poll;
708 mouse_cursor->context.ChannelHandle = mouse_cursor_server_context_handle;
710 mouse_cursor->context.CapsConfirm = mouse_cursor_server_send_sc_caps_confirm;
711 mouse_cursor->context.MouseptrUpdate = mouse_cursor_server_send_sc_mouseptr_update;
713 mouse_cursor->buffer = Stream_New(NULL, 4096);
714 if (!mouse_cursor->buffer)
717 return &mouse_cursor->context;
719 WINPR_PRAGMA_DIAG_PUSH
720 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
721 mouse_cursor_server_context_free(&mouse_cursor->context);
722 WINPR_PRAGMA_DIAG_POP
726 void mouse_cursor_server_context_free(MouseCursorServerContext* context)
728 mouse_cursor_server* mouse_cursor = (mouse_cursor_server*)context;
732 mouse_cursor_server_close(context);
733 Stream_Free(mouse_cursor->buffer, TRUE);
RDP_MOUSE_CURSOR_CAPVERSION
@ RDP_MOUSE_CURSOR_CAPVERSION_1
This struct contains function pointer to initialize/free objects.