FreeRDP
Loading...
Searching...
No Matches
server/rdpei_main.c
1
23#include <freerdp/config.h>
24
25#include <winpr/cast.h>
26#include <winpr/crt.h>
27#include <winpr/print.h>
28#include <winpr/stream.h>
29
30#include "rdpei_main.h"
31#include "../rdpei_common.h"
32#include <freerdp/channels/rdpei.h>
33#include <freerdp/server/rdpei.h>
34
35typedef enum
36{
37 RDPEI_INITIAL,
38 RDPEI_OPENED,
39} eRdpEiChannelState;
40
41enum RdpEiState
42{
43 STATE_INITIAL,
44 STATE_WAITING_CLIENT_READY,
45 STATE_WAITING_FRAME,
46 STATE_SUSPENDED,
47};
48
49struct s_rdpei_server_private
50{
51 HANDLE channelHandle;
52 HANDLE eventHandle;
53
54 HANDLE stopEvent;
55 HANDLE thread;
56
57 /* Channel state */
58 eRdpEiChannelState channelState;
59
60 UINT32 expectedBytes;
61 BOOL waitingHeaders;
62 wStream* inputStream;
63 wStream* outputStream;
64
65 UINT16 currentMsgType;
66
67 RDPINPUT_TOUCH_EVENT touchEvent;
68 RDPINPUT_PEN_EVENT penEvent;
69
70 enum RdpEiState automataState;
71};
72
73static UINT rdpei_server_open_channel(RdpeiServerContext* context)
74{
75 DWORD error = ERROR_SUCCESS;
76 DWORD bytesReturned = 0;
77 PULONG pSessionId = nullptr;
78 BOOL status = TRUE;
79
80 WINPR_ASSERT(context);
81
82 RdpeiServerPrivate* priv = context->priv;
83 WINPR_ASSERT(priv);
84
85 if (WTSQuerySessionInformationA(context->vcm, WTS_CURRENT_SESSION, WTSSessionId,
86 (LPSTR*)&pSessionId, &bytesReturned) == FALSE)
87 {
88 WLog_ERR(TAG, "WTSQuerySessionInformationA failed!");
89 return ERROR_INTERNAL_ERROR;
90 }
91
92 DWORD sessionId = (DWORD)*pSessionId;
93 WTSFreeMemory(pSessionId);
94
95 priv->channelHandle =
96 WTSVirtualChannelOpenEx(sessionId, RDPEI_DVC_CHANNEL_NAME, WTS_CHANNEL_OPTION_DYNAMIC);
97 if (!priv->channelHandle)
98 {
99 error = GetLastError();
100 WLog_ERR(TAG, "WTSVirtualChannelOpenEx failed with error %" PRIu32 "!", error);
101 return error;
102 }
103
104 const UINT32 channelId = WTSChannelGetIdByHandle(priv->channelHandle);
105
106 IFCALLRET(context->onChannelIdAssigned, status, context, channelId);
107 if (!status)
108 {
109 WLog_ERR(TAG, "context->onChannelIdAssigned failed!");
110 return ERROR_INTERNAL_ERROR;
111 }
112
113 return error;
114}
115
116static UINT rdpei_server_context_poll_int(RdpeiServerContext* context)
117{
118 RdpeiServerPrivate* priv = nullptr;
119 UINT error = ERROR_INTERNAL_ERROR;
120
121 WINPR_ASSERT(context);
122 priv = context->priv;
123 WINPR_ASSERT(priv);
124
125 switch (priv->channelState)
126 {
127 case RDPEI_INITIAL:
128 error = rdpei_server_open_channel(context);
129 if (error)
130 WLog_ERR(TAG, "rdpei_server_open_channel failed with error %" PRIu32 "!", error);
131 else
132 priv->channelState = RDPEI_OPENED;
133 break;
134 case RDPEI_OPENED:
135 error = rdpei_server_handle_messages(context);
136 break;
137 default:
138 break;
139 }
140
141 return error;
142}
143
144static HANDLE rdpei_server_get_channel_handle(RdpeiServerContext* context)
145{
146 RdpeiServerPrivate* priv = nullptr;
147 void* buffer = nullptr;
148 DWORD bytesReturned = 0;
149 HANDLE channelEvent = nullptr;
150
151 WINPR_ASSERT(context);
152 priv = context->priv;
153 WINPR_ASSERT(priv);
154
155 if (WTSVirtualChannelQuery(priv->channelHandle, WTSVirtualEventHandle, &buffer,
156 &bytesReturned) == TRUE)
157 {
158 if (bytesReturned == sizeof(HANDLE))
159 channelEvent = *(HANDLE*)buffer;
160
161 WTSFreeMemory(buffer);
162 }
163
164 return channelEvent;
165}
166
167static DWORD WINAPI rdpei_server_thread_func(LPVOID arg)
168{
169 RdpeiServerContext* context = (RdpeiServerContext*)arg;
170 RdpeiServerPrivate* priv = nullptr;
171 HANDLE events[2] = WINPR_C_ARRAY_INIT;
172 DWORD nCount = 0;
173 UINT error = CHANNEL_RC_OK;
174 DWORD status = 0;
175
176 WINPR_ASSERT(context);
177 priv = context->priv;
178 WINPR_ASSERT(priv);
179
180 events[nCount++] = priv->stopEvent;
181
182 while ((error == CHANNEL_RC_OK) && (WaitForSingleObject(events[0], 0) != WAIT_OBJECT_0))
183 {
184 switch (priv->channelState)
185 {
186 case RDPEI_INITIAL:
187 error = rdpei_server_context_poll_int(context);
188 if (error == CHANNEL_RC_OK)
189 {
190 events[1] = rdpei_server_get_channel_handle(context);
191 nCount = 2;
192 }
193 break;
194 case RDPEI_OPENED:
195 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
196 switch (status)
197 {
198 case WAIT_OBJECT_0:
199 break;
200 case WAIT_OBJECT_0 + 1:
201 case WAIT_TIMEOUT:
202 error = rdpei_server_context_poll_int(context);
203 break;
204
205 case WAIT_FAILED:
206 default:
207 error = ERROR_INTERNAL_ERROR;
208 break;
209 }
210 break;
211 default:
212 break;
213 }
214 }
215
216 (void)WTSVirtualChannelClose(priv->channelHandle);
217 priv->channelHandle = nullptr;
218
219 ExitThread(error);
220 return error;
221}
222
223static UINT rdpei_server_open(RdpeiServerContext* context)
224{
225 RdpeiServerPrivate* priv = nullptr;
226
227 priv = context->priv;
228 WINPR_ASSERT(priv);
229
230 if (!priv->thread)
231 {
232 priv->stopEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr);
233 if (!priv->stopEvent)
234 {
235 WLog_ERR(TAG, "CreateEvent failed!");
236 return ERROR_INTERNAL_ERROR;
237 }
238
239 priv->thread = CreateThread(nullptr, 0, rdpei_server_thread_func, context, 0, nullptr);
240 if (!priv->thread)
241 {
242 WLog_ERR(TAG, "CreateThread failed!");
243 (void)CloseHandle(priv->stopEvent);
244 priv->stopEvent = nullptr;
245 return ERROR_INTERNAL_ERROR;
246 }
247 }
248
249 return CHANNEL_RC_OK;
250}
251
252static UINT rdpei_server_close(RdpeiServerContext* context)
253{
254 RdpeiServerPrivate* priv = nullptr;
255 UINT error = CHANNEL_RC_OK;
256
257 priv = context->priv;
258 WINPR_ASSERT(priv);
259
260 if (priv->thread)
261 {
262 (void)SetEvent(priv->stopEvent);
263
264 if (WaitForSingleObject(priv->thread, INFINITE) == WAIT_FAILED)
265 {
266 error = GetLastError();
267 WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error);
268 return error;
269 }
270
271 (void)CloseHandle(priv->thread);
272 (void)CloseHandle(priv->stopEvent);
273 priv->thread = nullptr;
274 priv->stopEvent = nullptr;
275 }
276
277 return error;
278}
279
280RdpeiServerContext* rdpei_server_context_new(HANDLE vcm)
281{
282 RdpeiServerContext* ret = calloc(1, sizeof(*ret));
283
284 if (!ret)
285 return nullptr;
286
287 ret->Open = rdpei_server_open;
288 ret->Close = rdpei_server_close;
289
290 ret->priv = calloc(1, sizeof(*ret->priv));
291 if (!ret->priv)
292 goto fail;
293
294 ret->priv->inputStream = Stream_New(nullptr, 256);
295 if (!ret->priv->inputStream)
296 goto fail;
297
298 ret->priv->outputStream = Stream_New(nullptr, 200);
299 if (!ret->priv->outputStream)
300 goto fail;
301
302 ret->vcm = vcm;
303 rdpei_server_context_reset(ret);
304 return ret;
305
306fail:
307 WINPR_PRAGMA_DIAG_PUSH
308 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
309 rdpei_server_context_free(ret);
310 WINPR_PRAGMA_DIAG_POP
311 return nullptr;
312}
313
319UINT rdpei_server_init(RdpeiServerContext* context)
320{
321 RdpeiServerPrivate* priv = context->priv;
322 UINT error = rdpei_server_open_channel(context);
323 if (error)
324 return error;
325
326 priv->eventHandle = rdpei_server_get_channel_handle(context);
327 if (!priv->eventHandle)
328 {
329 WLog_ERR(TAG, "Failed to get channel handle!");
330 goto out_close;
331 }
332
333 return CHANNEL_RC_OK;
334
335out_close:
336 (void)WTSVirtualChannelClose(priv->channelHandle);
337 priv->channelHandle = INVALID_HANDLE_VALUE;
338 return CHANNEL_RC_INITIALIZATION_ERROR;
339}
340
341void rdpei_server_context_reset(RdpeiServerContext* context)
342{
343 RdpeiServerPrivate* priv = context->priv;
344
345 priv->channelHandle = INVALID_HANDLE_VALUE;
346 priv->expectedBytes = RDPINPUT_HEADER_LENGTH;
347 priv->waitingHeaders = TRUE;
348 priv->automataState = STATE_INITIAL;
349 Stream_ResetPosition(priv->inputStream);
350}
351
352void rdpei_server_context_free(RdpeiServerContext* context)
353{
354 if (!context)
355 return;
356 RdpeiServerPrivate* priv = context->priv;
357 if (priv)
358 {
359 if (priv->channelHandle && priv->channelHandle != INVALID_HANDLE_VALUE)
360 (void)WTSVirtualChannelClose(priv->channelHandle);
361 Stream_Free(priv->inputStream, TRUE);
362 Stream_Free(priv->outputStream, TRUE);
363 }
364 free(priv);
365 free(context);
366}
367
368HANDLE rdpei_server_get_event_handle(RdpeiServerContext* context)
369{
370 return context->priv->eventHandle;
371}
372
378static UINT read_cs_ready_message(RdpeiServerContext* context, wStream* s)
379{
380 UINT error = CHANNEL_RC_OK;
381 if (!Stream_CheckAndLogRequiredLength(TAG, s, 10))
382 return ERROR_INVALID_DATA;
383
384 Stream_Read_UINT32(s, context->protocolFlags);
385 Stream_Read_UINT32(s, context->clientVersion);
386 Stream_Read_UINT16(s, context->maxTouchPoints);
387
388 switch (context->clientVersion)
389 {
390 case RDPINPUT_PROTOCOL_V10:
391 case RDPINPUT_PROTOCOL_V101:
392 case RDPINPUT_PROTOCOL_V200:
393 case RDPINPUT_PROTOCOL_V300:
394 break;
395 default:
396 WLog_ERR(TAG, "unhandled RPDEI protocol version 0x%" PRIx32 "", context->clientVersion);
397 break;
398 }
399
400 IFCALLRET(context->onClientReady, error, context);
401 if (error)
402 WLog_ERR(TAG, "context->onClientReady failed with error %" PRIu32 "", error);
403
404 return error;
405}
406
412static UINT read_touch_contact_data(RdpeiServerContext* context, wStream* s,
413 RDPINPUT_CONTACT_DATA* contactData)
414{
415 WINPR_UNUSED(context);
416 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
417 return ERROR_INVALID_DATA;
418
419 Stream_Read_UINT8(s, contactData->contactId);
420 if (!rdpei_read_2byte_unsigned(s, &contactData->fieldsPresent) ||
421 !rdpei_read_4byte_signed(s, &contactData->x) ||
422 !rdpei_read_4byte_signed(s, &contactData->y) ||
423 !rdpei_read_4byte_unsigned(s, &contactData->contactFlags))
424 {
425 WLog_ERR(TAG, "rdpei_read_ failed!");
426 return ERROR_INTERNAL_ERROR;
427 }
428
429 if (contactData->fieldsPresent & CONTACT_DATA_CONTACTRECT_PRESENT)
430 {
431 if (!rdpei_read_2byte_signed(s, &contactData->contactRectLeft) ||
432 !rdpei_read_2byte_signed(s, &contactData->contactRectTop) ||
433 !rdpei_read_2byte_signed(s, &contactData->contactRectRight) ||
434 !rdpei_read_2byte_signed(s, &contactData->contactRectBottom))
435 {
436 WLog_ERR(TAG, "rdpei_read_ failed!");
437 return ERROR_INTERNAL_ERROR;
438 }
439 }
440
441 if ((contactData->fieldsPresent & CONTACT_DATA_ORIENTATION_PRESENT) &&
442 !rdpei_read_4byte_unsigned(s, &contactData->orientation))
443 {
444 WLog_ERR(TAG, "rdpei_read_ failed!");
445 return ERROR_INTERNAL_ERROR;
446 }
447
448 if ((contactData->fieldsPresent & CONTACT_DATA_PRESSURE_PRESENT) &&
449 !rdpei_read_4byte_unsigned(s, &contactData->pressure))
450 {
451 WLog_ERR(TAG, "rdpei_read_ failed!");
452 return ERROR_INTERNAL_ERROR;
453 }
454
455 return CHANNEL_RC_OK;
456}
457
458static UINT read_pen_contact(RdpeiServerContext* context, wStream* s,
459 RDPINPUT_PEN_CONTACT* contactData)
460{
461 WINPR_UNUSED(context);
462 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
463 return ERROR_INVALID_DATA;
464
465 Stream_Read_UINT8(s, contactData->deviceId);
466 if (!rdpei_read_2byte_unsigned(s, &contactData->fieldsPresent) ||
467 !rdpei_read_4byte_signed(s, &contactData->x) ||
468 !rdpei_read_4byte_signed(s, &contactData->y) ||
469 !rdpei_read_4byte_unsigned(s, &contactData->contactFlags))
470 {
471 WLog_ERR(TAG, "rdpei_read_ failed!");
472 return ERROR_INTERNAL_ERROR;
473 }
474
475 if (contactData->fieldsPresent & RDPINPUT_PEN_CONTACT_PENFLAGS_PRESENT)
476 {
477 if (!rdpei_read_4byte_unsigned(s, &contactData->penFlags))
478 return ERROR_INVALID_DATA;
479 }
480 if (contactData->fieldsPresent & RDPINPUT_PEN_CONTACT_PRESSURE_PRESENT)
481 {
482 if (!rdpei_read_4byte_unsigned(s, &contactData->pressure))
483 return ERROR_INVALID_DATA;
484 }
485 if (contactData->fieldsPresent & RDPINPUT_PEN_CONTACT_ROTATION_PRESENT)
486 {
487 if (!rdpei_read_2byte_unsigned(s, &contactData->rotation))
488 return ERROR_INVALID_DATA;
489 }
490 if (contactData->fieldsPresent & RDPINPUT_PEN_CONTACT_TILTX_PRESENT)
491 {
492 if (!rdpei_read_2byte_signed(s, &contactData->tiltX))
493 return ERROR_INVALID_DATA;
494 }
495 if (contactData->fieldsPresent & RDPINPUT_PEN_CONTACT_TILTY_PRESENT)
496 {
497 if (!rdpei_read_2byte_signed(s, &contactData->tiltY))
498 return ERROR_INVALID_DATA;
499 }
500
501 return CHANNEL_RC_OK;
502}
503
509static UINT read_touch_frame(RdpeiServerContext* context, wStream* s, RDPINPUT_TOUCH_FRAME* frame)
510{
511 RDPINPUT_CONTACT_DATA* contact = nullptr;
512 UINT error = 0;
513
514 if (!rdpei_read_2byte_unsigned(s, &frame->contactCount) ||
515 !rdpei_read_8byte_unsigned(s, &frame->frameOffset))
516 {
517 WLog_ERR(TAG, "rdpei_read_ failed!");
518 return ERROR_INTERNAL_ERROR;
519 }
520
521 frame->contacts = contact = calloc(frame->contactCount, sizeof(RDPINPUT_CONTACT_DATA));
522 if (!frame->contacts)
523 {
524 WLog_ERR(TAG, "calloc failed!");
525 return CHANNEL_RC_NO_MEMORY;
526 }
527
528 for (UINT32 i = 0; i < frame->contactCount; i++, contact++)
529 {
530 if ((error = read_touch_contact_data(context, s, contact)))
531 {
532 WLog_ERR(TAG, "read_touch_contact_data failed with error %" PRIu32 "!", error);
533 frame->contactCount = WINPR_ASSERTING_INT_CAST(UINT16, i);
534 touch_frame_reset(frame);
535 return error;
536 }
537 }
538 return CHANNEL_RC_OK;
539}
540
541static UINT read_pen_frame(RdpeiServerContext* context, wStream* s, RDPINPUT_PEN_FRAME* frame)
542{
543 RDPINPUT_PEN_CONTACT* contact = nullptr;
544 UINT error = 0;
545
546 if (!rdpei_read_2byte_unsigned(s, &frame->contactCount) ||
547 !rdpei_read_8byte_unsigned(s, &frame->frameOffset))
548 {
549 WLog_ERR(TAG, "rdpei_read_ failed!");
550 return ERROR_INTERNAL_ERROR;
551 }
552
553 frame->contacts = contact = calloc(frame->contactCount, sizeof(RDPINPUT_PEN_CONTACT));
554 if (!frame->contacts)
555 {
556 WLog_ERR(TAG, "calloc failed!");
557 return CHANNEL_RC_NO_MEMORY;
558 }
559
560 for (UINT32 i = 0; i < frame->contactCount; i++, contact++)
561 {
562 if ((error = read_pen_contact(context, s, contact)))
563 {
564 WLog_ERR(TAG, "read_touch_contact_data failed with error %" PRIu32 "!", error);
565 frame->contactCount = WINPR_ASSERTING_INT_CAST(UINT16, i);
566
567 pen_frame_reset(frame);
568 return error;
569 }
570 }
571 return CHANNEL_RC_OK;
572}
573
579static UINT read_touch_event(RdpeiServerContext* context, wStream* s)
580{
581 UINT16 frameCount = 0;
582
583 WINPR_ASSERT(context);
584 WINPR_ASSERT(context->priv);
585
586 RDPINPUT_TOUCH_EVENT* event = &context->priv->touchEvent;
587 WINPR_ASSERT(event);
588
589 RDPINPUT_TOUCH_FRAME* frame = nullptr;
590 UINT error = CHANNEL_RC_OK;
591
592 if (!rdpei_read_4byte_unsigned(s, &event->encodeTime) ||
593 !rdpei_read_2byte_unsigned(s, &frameCount))
594 {
595 WLog_ERR(TAG, "rdpei_read_ failed!");
596 return ERROR_INTERNAL_ERROR;
597 }
598
599 event->frameCount = frameCount;
600 event->frames = frame = calloc(event->frameCount, sizeof(RDPINPUT_TOUCH_FRAME));
601 if (!event->frames)
602 {
603 WLog_ERR(TAG, "calloc failed!");
604 return CHANNEL_RC_NO_MEMORY;
605 }
606
607 for (UINT32 i = 0; i < frameCount; i++, frame++)
608 {
609 if ((error = read_touch_frame(context, s, frame)))
610 {
611 WLog_ERR(TAG, "read_touch_contact_data failed with error %" PRIu32 "!", error);
612 event->frameCount = WINPR_ASSERTING_INT_CAST(UINT16, i);
613
614 goto out_cleanup;
615 }
616 }
617
618 IFCALLRET(context->onTouchEvent, error, context, event);
619 if (error)
620 WLog_ERR(TAG, "context->onTouchEvent failed with error %" PRIu32 "", error);
621
622out_cleanup:
623 touch_event_reset(event);
624 return error;
625}
626
627static UINT read_pen_event(RdpeiServerContext* context, wStream* s)
628{
629 UINT16 frameCount = 0;
630 RDPINPUT_PEN_EVENT* event = &context->priv->penEvent;
631 RDPINPUT_PEN_FRAME* frame = nullptr;
632 UINT error = CHANNEL_RC_OK;
633
634 if (!rdpei_read_4byte_unsigned(s, &event->encodeTime) ||
635 !rdpei_read_2byte_unsigned(s, &frameCount))
636 {
637 WLog_ERR(TAG, "rdpei_read_ failed!");
638 return ERROR_INTERNAL_ERROR;
639 }
640
641 event->frameCount = frameCount;
642 event->frames = frame = calloc(event->frameCount, sizeof(RDPINPUT_PEN_FRAME));
643 if (!event->frames)
644 {
645 WLog_ERR(TAG, "calloc failed!");
646 return CHANNEL_RC_NO_MEMORY;
647 }
648
649 for (UINT32 i = 0; i < frameCount; i++, frame++)
650 {
651 if ((error = read_pen_frame(context, s, frame)))
652 {
653 WLog_ERR(TAG, "read_pen_frame failed with error %" PRIu32 "!", error);
654 event->frameCount = WINPR_ASSERTING_INT_CAST(UINT16, i);
655
656 goto out_cleanup;
657 }
658 }
659
660 IFCALLRET(context->onPenEvent, error, context, event);
661 if (error)
662 WLog_ERR(TAG, "context->onPenEvent failed with error %" PRIu32 "", error);
663
664out_cleanup:
665 pen_event_reset(event);
666 return error;
667}
668
674static UINT read_dismiss_hovering_contact(RdpeiServerContext* context, wStream* s)
675{
676 BYTE contactId = 0;
677 UINT error = CHANNEL_RC_OK;
678
679 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
680 return ERROR_INVALID_DATA;
681
682 Stream_Read_UINT8(s, contactId);
683
684 IFCALLRET(context->onTouchReleased, error, context, contactId);
685 if (error)
686 WLog_ERR(TAG, "context->onTouchReleased failed with error %" PRIu32 "", error);
687
688 return error;
689}
690
696UINT rdpei_server_handle_messages(RdpeiServerContext* context)
697{
698 DWORD bytesReturned = 0;
699
700 WINPR_ASSERT(context);
701
702 RdpeiServerPrivate* priv = context->priv;
703 WINPR_ASSERT(priv);
704
705 wStream* s = priv->inputStream;
706 UINT error = CHANNEL_RC_OK;
707
708 if (!WTSVirtualChannelRead(priv->channelHandle, 0, Stream_Pointer(s), priv->expectedBytes,
709 &bytesReturned))
710 {
711 if (GetLastError() == ERROR_NO_DATA)
712 return ERROR_READ_FAULT;
713
714 WLog_DBG(TAG, "channel connection closed");
715 return CHANNEL_RC_OK;
716 }
717 priv->expectedBytes -= bytesReturned;
718 Stream_Seek(s, bytesReturned);
719
720 if (priv->expectedBytes)
721 return CHANNEL_RC_OK;
722
723 Stream_SealLength(s);
724 Stream_ResetPosition(s);
725
726 if (priv->waitingHeaders)
727 {
728 /* header case */
729 priv->currentMsgType = Stream_Get_UINT16(s);
730 const UINT32 pduLen = Stream_Get_UINT32(s);
731
732 if (pduLen < RDPINPUT_HEADER_LENGTH)
733 {
734 WLog_ERR(TAG, "invalid pduLength %" PRIu32 "", pduLen);
735 return ERROR_INVALID_DATA;
736 }
737
738 if (pduLen > RDPINPUT_MAX_PDU_LENGTH)
739 {
740 WLog_ERR(TAG, "invalid pduLength %" PRIu32 " > RDPINPUT_MAX_PDU_LENGTH(%llu)", pduLen,
741 RDPINPUT_MAX_PDU_LENGTH);
742 return ERROR_INVALID_DATA;
743 }
744
745 priv->expectedBytes = pduLen - RDPINPUT_HEADER_LENGTH;
746 priv->waitingHeaders = FALSE;
747 Stream_ResetPosition(s);
748 if (priv->expectedBytes)
749 {
750 if (!Stream_EnsureCapacity(s, priv->expectedBytes))
751 {
752 WLog_ERR(TAG, "Stream_EnsureCapacity failed!");
753 return CHANNEL_RC_NO_MEMORY;
754 }
755 return CHANNEL_RC_OK;
756 }
757 }
758
759 /* when here we have the header + the body */
760 switch (priv->currentMsgType)
761 {
762 case EVENTID_CS_READY:
763 if (priv->automataState != STATE_WAITING_CLIENT_READY)
764 {
765 WLog_ERR(TAG, "not expecting a CS_READY packet in this state(%u)",
766 priv->automataState);
767 return ERROR_INVALID_STATE;
768 }
769
770 if ((error = read_cs_ready_message(context, s)))
771 {
772 WLog_ERR(TAG, "read_cs_ready_message failed with error %" PRIu32 "", error);
773 return error;
774 }
775 priv->automataState = STATE_WAITING_FRAME;
776 break;
777
778 case EVENTID_TOUCH:
779 if ((error = read_touch_event(context, s)))
780 {
781 WLog_ERR(TAG, "read_touch_event failed with error %" PRIu32 "", error);
782 return error;
783 }
784 break;
785 case EVENTID_DISMISS_HOVERING_CONTACT:
786 if ((error = read_dismiss_hovering_contact(context, s)))
787 {
788 WLog_ERR(TAG, "read_dismiss_hovering_contact failed with error %" PRIu32 "", error);
789 return error;
790 }
791 break;
792 case EVENTID_PEN:
793 if ((error = read_pen_event(context, s)))
794 {
795 WLog_ERR(TAG, "read_pen_event failed with error %" PRIu32 "", error);
796 return error;
797 }
798 break;
799 default:
800 WLog_ERR(TAG, "unexpected message type 0x%" PRIx16 "", priv->currentMsgType);
801 }
802
803 Stream_ResetPosition(s);
804 priv->waitingHeaders = TRUE;
805 priv->expectedBytes = RDPINPUT_HEADER_LENGTH;
806 return error;
807}
808
814UINT rdpei_server_send_sc_ready(RdpeiServerContext* context, UINT32 version, UINT32 features)
815{
816 ULONG written = 0;
817 RdpeiServerPrivate* priv = context->priv;
818 UINT32 pduLen = 4;
819
820 if (priv->automataState != STATE_INITIAL)
821 {
822 WLog_ERR(TAG, "called from unexpected state %u", priv->automataState);
823 return ERROR_INVALID_STATE;
824 }
825
826 Stream_ResetPosition(priv->outputStream);
827
828 if (version >= RDPINPUT_PROTOCOL_V300)
829 pduLen += 4;
830
831 if (!Stream_EnsureCapacity(priv->outputStream, RDPINPUT_HEADER_LENGTH + pduLen))
832 {
833 WLog_ERR(TAG, "Stream_EnsureCapacity failed!");
834 return CHANNEL_RC_NO_MEMORY;
835 }
836
837 Stream_Write_UINT16(priv->outputStream, EVENTID_SC_READY);
838 Stream_Write_UINT32(priv->outputStream, RDPINPUT_HEADER_LENGTH + pduLen);
839 Stream_Write_UINT32(priv->outputStream, version);
840 if (version >= RDPINPUT_PROTOCOL_V300)
841 Stream_Write_UINT32(priv->outputStream, features);
842
843 const size_t pos = Stream_GetPosition(priv->outputStream);
844
845 WINPR_ASSERT(pos <= UINT32_MAX);
846 if (!WTSVirtualChannelWrite(priv->channelHandle, Stream_BufferAs(priv->outputStream, char),
847 (ULONG)pos, &written))
848 {
849 WLog_ERR(TAG, "WTSVirtualChannelWrite failed!");
850 return ERROR_INTERNAL_ERROR;
851 }
852
853 priv->automataState = STATE_WAITING_CLIENT_READY;
854 return CHANNEL_RC_OK;
855}
856
862UINT rdpei_server_suspend(RdpeiServerContext* context)
863{
864 ULONG written = 0;
865 RdpeiServerPrivate* priv = context->priv;
866
867 switch (priv->automataState)
868 {
869 case STATE_SUSPENDED:
870 WLog_ERR(TAG, "already suspended");
871 return CHANNEL_RC_OK;
872 case STATE_WAITING_FRAME:
873 break;
874 default:
875 WLog_ERR(TAG, "called from unexpected state %u", priv->automataState);
876 return ERROR_INVALID_STATE;
877 }
878
879 Stream_ResetPosition(priv->outputStream);
880 if (!Stream_EnsureCapacity(priv->outputStream, RDPINPUT_HEADER_LENGTH))
881 {
882 WLog_ERR(TAG, "Stream_EnsureCapacity failed!");
883 return CHANNEL_RC_NO_MEMORY;
884 }
885
886 Stream_Write_UINT16(priv->outputStream, EVENTID_SUSPEND_TOUCH);
887 Stream_Write_UINT32(priv->outputStream, RDPINPUT_HEADER_LENGTH);
888
889 const size_t pos = Stream_GetPosition(priv->outputStream);
890
891 WINPR_ASSERT(pos <= UINT32_MAX);
892 if (!WTSVirtualChannelWrite(priv->channelHandle, Stream_BufferAs(priv->outputStream, char),
893 (ULONG)pos, &written))
894 {
895 WLog_ERR(TAG, "WTSVirtualChannelWrite failed!");
896 return ERROR_INTERNAL_ERROR;
897 }
898
899 priv->automataState = STATE_SUSPENDED;
900 return CHANNEL_RC_OK;
901}
902
908UINT rdpei_server_resume(RdpeiServerContext* context)
909{
910 ULONG written = 0;
911 RdpeiServerPrivate* priv = context->priv;
912
913 switch (priv->automataState)
914 {
915 case STATE_WAITING_FRAME:
916 WLog_ERR(TAG, "not suspended");
917 return CHANNEL_RC_OK;
918 case STATE_SUSPENDED:
919 break;
920 default:
921 WLog_ERR(TAG, "called from unexpected state %u", priv->automataState);
922 return ERROR_INVALID_STATE;
923 }
924
925 Stream_ResetPosition(priv->outputStream);
926 if (!Stream_EnsureCapacity(priv->outputStream, RDPINPUT_HEADER_LENGTH))
927 {
928 WLog_ERR(TAG, "Stream_EnsureCapacity failed!");
929 return CHANNEL_RC_NO_MEMORY;
930 }
931
932 Stream_Write_UINT16(priv->outputStream, EVENTID_RESUME_TOUCH);
933 Stream_Write_UINT32(priv->outputStream, RDPINPUT_HEADER_LENGTH);
934
935 const size_t pos = Stream_GetPosition(priv->outputStream);
936
937 WINPR_ASSERT(pos <= UINT32_MAX);
938 if (!WTSVirtualChannelWrite(priv->channelHandle, Stream_BufferAs(priv->outputStream, char),
939 (ULONG)pos, &written))
940 {
941 WLog_ERR(TAG, "WTSVirtualChannelWrite failed!");
942 return ERROR_INTERNAL_ERROR;
943 }
944
945 priv->automataState = STATE_WAITING_FRAME;
946 return CHANNEL_RC_OK;
947}
a touch event with some frames
a touch event with some frames
a frame containing contact points