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 = NULL;
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 = NULL;
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 = NULL;
147 void* buffer = NULL;
148 DWORD bytesReturned = 0;
149 HANDLE channelEvent = NULL;
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 = NULL;
171 HANDLE events[2] = { 0 };
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 = NULL;
218
219 ExitThread(error);
220 return error;
221}
222
223static UINT rdpei_server_open(RdpeiServerContext* context)
224{
225 RdpeiServerPrivate* priv = NULL;
226
227 priv = context->priv;
228 WINPR_ASSERT(priv);
229
230 if (!priv->thread)
231 {
232 priv->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
233 if (!priv->stopEvent)
234 {
235 WLog_ERR(TAG, "CreateEvent failed!");
236 return ERROR_INTERNAL_ERROR;
237 }
238
239 priv->thread = CreateThread(NULL, 0, rdpei_server_thread_func, context, 0, NULL);
240 if (!priv->thread)
241 {
242 WLog_ERR(TAG, "CreateThread failed!");
243 (void)CloseHandle(priv->stopEvent);
244 priv->stopEvent = NULL;
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 = NULL;
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 = NULL;
274 priv->stopEvent = NULL;
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 NULL;
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(NULL, 256);
295 if (!ret->priv->inputStream)
296 goto fail;
297
298 ret->priv->outputStream = Stream_New(NULL, 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 NULL;
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 return CHANNEL_RC_INITIALIZATION_ERROR;
338}
339
340void rdpei_server_context_reset(RdpeiServerContext* context)
341{
342 RdpeiServerPrivate* priv = context->priv;
343
344 priv->channelHandle = INVALID_HANDLE_VALUE;
345 priv->expectedBytes = RDPINPUT_HEADER_LENGTH;
346 priv->waitingHeaders = TRUE;
347 priv->automataState = STATE_INITIAL;
348 Stream_SetPosition(priv->inputStream, 0);
349}
350
351void rdpei_server_context_free(RdpeiServerContext* context)
352{
353 if (!context)
354 return;
355 RdpeiServerPrivate* priv = context->priv;
356 if (priv)
357 {
358 if (priv->channelHandle && priv->channelHandle != INVALID_HANDLE_VALUE)
359 (void)WTSVirtualChannelClose(priv->channelHandle);
360 Stream_Free(priv->inputStream, TRUE);
361 Stream_Free(priv->outputStream, TRUE);
362 }
363 free(priv);
364 free(context);
365}
366
367HANDLE rdpei_server_get_event_handle(RdpeiServerContext* context)
368{
369 return context->priv->eventHandle;
370}
371
377static UINT read_cs_ready_message(RdpeiServerContext* context, wStream* s)
378{
379 UINT error = CHANNEL_RC_OK;
380 if (!Stream_CheckAndLogRequiredLength(TAG, s, 10))
381 return ERROR_INVALID_DATA;
382
383 Stream_Read_UINT32(s, context->protocolFlags);
384 Stream_Read_UINT32(s, context->clientVersion);
385 Stream_Read_UINT16(s, context->maxTouchPoints);
386
387 switch (context->clientVersion)
388 {
389 case RDPINPUT_PROTOCOL_V10:
390 case RDPINPUT_PROTOCOL_V101:
391 case RDPINPUT_PROTOCOL_V200:
392 case RDPINPUT_PROTOCOL_V300:
393 break;
394 default:
395 WLog_ERR(TAG, "unhandled RPDEI protocol version 0x%" PRIx32 "", context->clientVersion);
396 break;
397 }
398
399 IFCALLRET(context->onClientReady, error, context);
400 if (error)
401 WLog_ERR(TAG, "context->onClientReady failed with error %" PRIu32 "", error);
402
403 return error;
404}
405
411static UINT read_touch_contact_data(RdpeiServerContext* context, wStream* s,
412 RDPINPUT_CONTACT_DATA* contactData)
413{
414 WINPR_UNUSED(context);
415 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
416 return ERROR_INVALID_DATA;
417
418 Stream_Read_UINT8(s, contactData->contactId);
419 if (!rdpei_read_2byte_unsigned(s, &contactData->fieldsPresent) ||
420 !rdpei_read_4byte_signed(s, &contactData->x) ||
421 !rdpei_read_4byte_signed(s, &contactData->y) ||
422 !rdpei_read_4byte_unsigned(s, &contactData->contactFlags))
423 {
424 WLog_ERR(TAG, "rdpei_read_ failed!");
425 return ERROR_INTERNAL_ERROR;
426 }
427
428 if (contactData->fieldsPresent & CONTACT_DATA_CONTACTRECT_PRESENT)
429 {
430 if (!rdpei_read_2byte_signed(s, &contactData->contactRectLeft) ||
431 !rdpei_read_2byte_signed(s, &contactData->contactRectTop) ||
432 !rdpei_read_2byte_signed(s, &contactData->contactRectRight) ||
433 !rdpei_read_2byte_signed(s, &contactData->contactRectBottom))
434 {
435 WLog_ERR(TAG, "rdpei_read_ failed!");
436 return ERROR_INTERNAL_ERROR;
437 }
438 }
439
440 if ((contactData->fieldsPresent & CONTACT_DATA_ORIENTATION_PRESENT) &&
441 !rdpei_read_4byte_unsigned(s, &contactData->orientation))
442 {
443 WLog_ERR(TAG, "rdpei_read_ failed!");
444 return ERROR_INTERNAL_ERROR;
445 }
446
447 if ((contactData->fieldsPresent & CONTACT_DATA_PRESSURE_PRESENT) &&
448 !rdpei_read_4byte_unsigned(s, &contactData->pressure))
449 {
450 WLog_ERR(TAG, "rdpei_read_ failed!");
451 return ERROR_INTERNAL_ERROR;
452 }
453
454 return CHANNEL_RC_OK;
455}
456
457static UINT read_pen_contact(RdpeiServerContext* context, wStream* s,
458 RDPINPUT_PEN_CONTACT* contactData)
459{
460 WINPR_UNUSED(context);
461 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
462 return ERROR_INVALID_DATA;
463
464 Stream_Read_UINT8(s, contactData->deviceId);
465 if (!rdpei_read_2byte_unsigned(s, &contactData->fieldsPresent) ||
466 !rdpei_read_4byte_signed(s, &contactData->x) ||
467 !rdpei_read_4byte_signed(s, &contactData->y) ||
468 !rdpei_read_4byte_unsigned(s, &contactData->contactFlags))
469 {
470 WLog_ERR(TAG, "rdpei_read_ failed!");
471 return ERROR_INTERNAL_ERROR;
472 }
473
474 if (contactData->fieldsPresent & RDPINPUT_PEN_CONTACT_PENFLAGS_PRESENT)
475 {
476 if (!rdpei_read_4byte_unsigned(s, &contactData->penFlags))
477 return ERROR_INVALID_DATA;
478 }
479 if (contactData->fieldsPresent & RDPINPUT_PEN_CONTACT_PRESSURE_PRESENT)
480 {
481 if (!rdpei_read_4byte_unsigned(s, &contactData->pressure))
482 return ERROR_INVALID_DATA;
483 }
484 if (contactData->fieldsPresent & RDPINPUT_PEN_CONTACT_ROTATION_PRESENT)
485 {
486 if (!rdpei_read_2byte_unsigned(s, &contactData->rotation))
487 return ERROR_INVALID_DATA;
488 }
489 if (contactData->fieldsPresent & RDPINPUT_PEN_CONTACT_TILTX_PRESENT)
490 {
491 if (!rdpei_read_2byte_signed(s, &contactData->tiltX))
492 return ERROR_INVALID_DATA;
493 }
494 if (contactData->fieldsPresent & RDPINPUT_PEN_CONTACT_TILTY_PRESENT)
495 {
496 if (!rdpei_read_2byte_signed(s, &contactData->tiltY))
497 return ERROR_INVALID_DATA;
498 }
499
500 return CHANNEL_RC_OK;
501}
502
508static UINT read_touch_frame(RdpeiServerContext* context, wStream* s, RDPINPUT_TOUCH_FRAME* frame)
509{
510 RDPINPUT_CONTACT_DATA* contact = NULL;
511 UINT error = 0;
512
513 if (!rdpei_read_2byte_unsigned(s, &frame->contactCount) ||
514 !rdpei_read_8byte_unsigned(s, &frame->frameOffset))
515 {
516 WLog_ERR(TAG, "rdpei_read_ failed!");
517 return ERROR_INTERNAL_ERROR;
518 }
519
520 frame->contacts = contact = calloc(frame->contactCount, sizeof(RDPINPUT_CONTACT_DATA));
521 if (!frame->contacts)
522 {
523 WLog_ERR(TAG, "calloc failed!");
524 return CHANNEL_RC_NO_MEMORY;
525 }
526
527 for (UINT32 i = 0; i < frame->contactCount; i++, contact++)
528 {
529 if ((error = read_touch_contact_data(context, s, contact)))
530 {
531 WLog_ERR(TAG, "read_touch_contact_data failed with error %" PRIu32 "!", error);
532 frame->contactCount = WINPR_ASSERTING_INT_CAST(UINT16, i);
533 touch_frame_reset(frame);
534 return error;
535 }
536 }
537 return CHANNEL_RC_OK;
538}
539
540static UINT read_pen_frame(RdpeiServerContext* context, wStream* s, RDPINPUT_PEN_FRAME* frame)
541{
542 RDPINPUT_PEN_CONTACT* contact = NULL;
543 UINT error = 0;
544
545 if (!rdpei_read_2byte_unsigned(s, &frame->contactCount) ||
546 !rdpei_read_8byte_unsigned(s, &frame->frameOffset))
547 {
548 WLog_ERR(TAG, "rdpei_read_ failed!");
549 return ERROR_INTERNAL_ERROR;
550 }
551
552 frame->contacts = contact = calloc(frame->contactCount, sizeof(RDPINPUT_PEN_CONTACT));
553 if (!frame->contacts)
554 {
555 WLog_ERR(TAG, "calloc failed!");
556 return CHANNEL_RC_NO_MEMORY;
557 }
558
559 for (UINT32 i = 0; i < frame->contactCount; i++, contact++)
560 {
561 if ((error = read_pen_contact(context, s, contact)))
562 {
563 WLog_ERR(TAG, "read_touch_contact_data failed with error %" PRIu32 "!", error);
564 frame->contactCount = WINPR_ASSERTING_INT_CAST(UINT16, i);
565
566 pen_frame_reset(frame);
567 return error;
568 }
569 }
570 return CHANNEL_RC_OK;
571}
572
578static UINT read_touch_event(RdpeiServerContext* context, wStream* s)
579{
580 UINT16 frameCount = 0;
581 RDPINPUT_TOUCH_EVENT* event = &context->priv->touchEvent;
582 RDPINPUT_TOUCH_FRAME* frame = NULL;
583 UINT error = CHANNEL_RC_OK;
584
585 if (!rdpei_read_4byte_unsigned(s, &event->encodeTime) ||
586 !rdpei_read_2byte_unsigned(s, &frameCount))
587 {
588 WLog_ERR(TAG, "rdpei_read_ failed!");
589 return ERROR_INTERNAL_ERROR;
590 }
591
592 event->frameCount = frameCount;
593 event->frames = frame = calloc(event->frameCount, sizeof(RDPINPUT_TOUCH_FRAME));
594 if (!event->frames)
595 {
596 WLog_ERR(TAG, "calloc failed!");
597 return CHANNEL_RC_NO_MEMORY;
598 }
599
600 for (UINT32 i = 0; i < frameCount; i++, frame++)
601 {
602 if ((error = read_touch_frame(context, s, frame)))
603 {
604 WLog_ERR(TAG, "read_touch_contact_data failed with error %" PRIu32 "!", error);
605 event->frameCount = WINPR_ASSERTING_INT_CAST(UINT16, i);
606
607 goto out_cleanup;
608 }
609 }
610
611 IFCALLRET(context->onTouchEvent, error, context, event);
612 if (error)
613 WLog_ERR(TAG, "context->onTouchEvent failed with error %" PRIu32 "", error);
614
615out_cleanup:
616 touch_event_reset(event);
617 return error;
618}
619
620static UINT read_pen_event(RdpeiServerContext* context, wStream* s)
621{
622 UINT16 frameCount = 0;
623 RDPINPUT_PEN_EVENT* event = &context->priv->penEvent;
624 RDPINPUT_PEN_FRAME* frame = NULL;
625 UINT error = CHANNEL_RC_OK;
626
627 if (!rdpei_read_4byte_unsigned(s, &event->encodeTime) ||
628 !rdpei_read_2byte_unsigned(s, &frameCount))
629 {
630 WLog_ERR(TAG, "rdpei_read_ failed!");
631 return ERROR_INTERNAL_ERROR;
632 }
633
634 event->frameCount = frameCount;
635 event->frames = frame = calloc(event->frameCount, sizeof(RDPINPUT_PEN_FRAME));
636 if (!event->frames)
637 {
638 WLog_ERR(TAG, "calloc failed!");
639 return CHANNEL_RC_NO_MEMORY;
640 }
641
642 for (UINT32 i = 0; i < frameCount; i++, frame++)
643 {
644 if ((error = read_pen_frame(context, s, frame)))
645 {
646 WLog_ERR(TAG, "read_pen_frame failed with error %" PRIu32 "!", error);
647 event->frameCount = WINPR_ASSERTING_INT_CAST(UINT16, i);
648
649 goto out_cleanup;
650 }
651 }
652
653 IFCALLRET(context->onPenEvent, error, context, event);
654 if (error)
655 WLog_ERR(TAG, "context->onPenEvent failed with error %" PRIu32 "", error);
656
657out_cleanup:
658 pen_event_reset(event);
659 return error;
660}
661
667static UINT read_dismiss_hovering_contact(RdpeiServerContext* context, wStream* s)
668{
669 BYTE contactId = 0;
670 UINT error = CHANNEL_RC_OK;
671
672 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
673 return ERROR_INVALID_DATA;
674
675 Stream_Read_UINT8(s, contactId);
676
677 IFCALLRET(context->onTouchReleased, error, context, contactId);
678 if (error)
679 WLog_ERR(TAG, "context->onTouchReleased failed with error %" PRIu32 "", error);
680
681 return error;
682}
683
689UINT rdpei_server_handle_messages(RdpeiServerContext* context)
690{
691 DWORD bytesReturned = 0;
692 RdpeiServerPrivate* priv = context->priv;
693 wStream* s = priv->inputStream;
694 UINT error = CHANNEL_RC_OK;
695
696 if (!WTSVirtualChannelRead(priv->channelHandle, 0, Stream_Pointer(s), priv->expectedBytes,
697 &bytesReturned))
698 {
699 if (GetLastError() == ERROR_NO_DATA)
700 return ERROR_READ_FAULT;
701
702 WLog_DBG(TAG, "channel connection closed");
703 return CHANNEL_RC_OK;
704 }
705 priv->expectedBytes -= bytesReturned;
706 Stream_Seek(s, bytesReturned);
707
708 if (priv->expectedBytes)
709 return CHANNEL_RC_OK;
710
711 Stream_SealLength(s);
712 Stream_SetPosition(s, 0);
713
714 if (priv->waitingHeaders)
715 {
716 UINT32 pduLen = 0;
717
718 /* header case */
719 Stream_Read_UINT16(s, priv->currentMsgType);
720 Stream_Read_UINT32(s, pduLen);
721
722 if (pduLen < RDPINPUT_HEADER_LENGTH)
723 {
724 WLog_ERR(TAG, "invalid pduLength %" PRIu32 "", pduLen);
725 return ERROR_INVALID_DATA;
726 }
727 priv->expectedBytes = pduLen - RDPINPUT_HEADER_LENGTH;
728 priv->waitingHeaders = FALSE;
729 Stream_SetPosition(s, 0);
730 if (priv->expectedBytes)
731 {
732 if (!Stream_EnsureCapacity(s, priv->expectedBytes))
733 {
734 WLog_ERR(TAG, "Stream_EnsureCapacity failed!");
735 return CHANNEL_RC_NO_MEMORY;
736 }
737 return CHANNEL_RC_OK;
738 }
739 }
740
741 /* when here we have the header + the body */
742 switch (priv->currentMsgType)
743 {
744 case EVENTID_CS_READY:
745 if (priv->automataState != STATE_WAITING_CLIENT_READY)
746 {
747 WLog_ERR(TAG, "not expecting a CS_READY packet in this state(%d)",
748 priv->automataState);
749 return ERROR_INVALID_STATE;
750 }
751
752 if ((error = read_cs_ready_message(context, s)))
753 {
754 WLog_ERR(TAG, "read_cs_ready_message failed with error %" PRIu32 "", error);
755 return error;
756 }
757 break;
758
759 case EVENTID_TOUCH:
760 if ((error = read_touch_event(context, s)))
761 {
762 WLog_ERR(TAG, "read_touch_event failed with error %" PRIu32 "", error);
763 return error;
764 }
765 break;
766 case EVENTID_DISMISS_HOVERING_CONTACT:
767 if ((error = read_dismiss_hovering_contact(context, s)))
768 {
769 WLog_ERR(TAG, "read_dismiss_hovering_contact failed with error %" PRIu32 "", error);
770 return error;
771 }
772 break;
773 case EVENTID_PEN:
774 if ((error = read_pen_event(context, s)))
775 {
776 WLog_ERR(TAG, "read_pen_event failed with error %" PRIu32 "", error);
777 return error;
778 }
779 break;
780 default:
781 WLog_ERR(TAG, "unexpected message type 0x%" PRIx16 "", priv->currentMsgType);
782 }
783
784 Stream_SetPosition(s, 0);
785 priv->waitingHeaders = TRUE;
786 priv->expectedBytes = RDPINPUT_HEADER_LENGTH;
787 return error;
788}
789
795UINT rdpei_server_send_sc_ready(RdpeiServerContext* context, UINT32 version, UINT32 features)
796{
797 ULONG written = 0;
798 RdpeiServerPrivate* priv = context->priv;
799 UINT32 pduLen = 4;
800
801 if (priv->automataState != STATE_INITIAL)
802 {
803 WLog_ERR(TAG, "called from unexpected state %d", priv->automataState);
804 return ERROR_INVALID_STATE;
805 }
806
807 Stream_SetPosition(priv->outputStream, 0);
808
809 if (version >= RDPINPUT_PROTOCOL_V300)
810 pduLen += 4;
811
812 if (!Stream_EnsureCapacity(priv->outputStream, RDPINPUT_HEADER_LENGTH + pduLen))
813 {
814 WLog_ERR(TAG, "Stream_EnsureCapacity failed!");
815 return CHANNEL_RC_NO_MEMORY;
816 }
817
818 Stream_Write_UINT16(priv->outputStream, EVENTID_SC_READY);
819 Stream_Write_UINT32(priv->outputStream, RDPINPUT_HEADER_LENGTH + pduLen);
820 Stream_Write_UINT32(priv->outputStream, version);
821 if (version >= RDPINPUT_PROTOCOL_V300)
822 Stream_Write_UINT32(priv->outputStream, features);
823
824 const size_t pos = Stream_GetPosition(priv->outputStream);
825
826 WINPR_ASSERT(pos <= UINT32_MAX);
827 if (!WTSVirtualChannelWrite(priv->channelHandle, Stream_BufferAs(priv->outputStream, char),
828 (ULONG)pos, &written))
829 {
830 WLog_ERR(TAG, "WTSVirtualChannelWrite failed!");
831 return ERROR_INTERNAL_ERROR;
832 }
833
834 priv->automataState = STATE_WAITING_CLIENT_READY;
835 return CHANNEL_RC_OK;
836}
837
843UINT rdpei_server_suspend(RdpeiServerContext* context)
844{
845 ULONG written = 0;
846 RdpeiServerPrivate* priv = context->priv;
847
848 switch (priv->automataState)
849 {
850 case STATE_SUSPENDED:
851 WLog_ERR(TAG, "already suspended");
852 return CHANNEL_RC_OK;
853 case STATE_WAITING_FRAME:
854 break;
855 default:
856 WLog_ERR(TAG, "called from unexpected state %d", priv->automataState);
857 return ERROR_INVALID_STATE;
858 }
859
860 Stream_SetPosition(priv->outputStream, 0);
861 if (!Stream_EnsureCapacity(priv->outputStream, RDPINPUT_HEADER_LENGTH))
862 {
863 WLog_ERR(TAG, "Stream_EnsureCapacity failed!");
864 return CHANNEL_RC_NO_MEMORY;
865 }
866
867 Stream_Write_UINT16(priv->outputStream, EVENTID_SUSPEND_TOUCH);
868 Stream_Write_UINT32(priv->outputStream, RDPINPUT_HEADER_LENGTH);
869
870 const size_t pos = Stream_GetPosition(priv->outputStream);
871
872 WINPR_ASSERT(pos <= UINT32_MAX);
873 if (!WTSVirtualChannelWrite(priv->channelHandle, Stream_BufferAs(priv->outputStream, char),
874 (ULONG)pos, &written))
875 {
876 WLog_ERR(TAG, "WTSVirtualChannelWrite failed!");
877 return ERROR_INTERNAL_ERROR;
878 }
879
880 priv->automataState = STATE_SUSPENDED;
881 return CHANNEL_RC_OK;
882}
883
889UINT rdpei_server_resume(RdpeiServerContext* context)
890{
891 ULONG written = 0;
892 RdpeiServerPrivate* priv = context->priv;
893
894 switch (priv->automataState)
895 {
896 case STATE_WAITING_FRAME:
897 WLog_ERR(TAG, "not suspended");
898 return CHANNEL_RC_OK;
899 case STATE_SUSPENDED:
900 break;
901 default:
902 WLog_ERR(TAG, "called from unexpected state %d", priv->automataState);
903 return ERROR_INVALID_STATE;
904 }
905
906 Stream_SetPosition(priv->outputStream, 0);
907 if (!Stream_EnsureCapacity(priv->outputStream, RDPINPUT_HEADER_LENGTH))
908 {
909 WLog_ERR(TAG, "Stream_EnsureCapacity failed!");
910 return CHANNEL_RC_NO_MEMORY;
911 }
912
913 Stream_Write_UINT16(priv->outputStream, EVENTID_RESUME_TOUCH);
914 Stream_Write_UINT32(priv->outputStream, RDPINPUT_HEADER_LENGTH);
915
916 const size_t pos = Stream_GetPosition(priv->outputStream);
917
918 WINPR_ASSERT(pos <= UINT32_MAX);
919 if (!WTSVirtualChannelWrite(priv->channelHandle, Stream_BufferAs(priv->outputStream, char),
920 (ULONG)pos, &written))
921 {
922 WLog_ERR(TAG, "WTSVirtualChannelWrite failed!");
923 return ERROR_INTERNAL_ERROR;
924 }
925
926 priv->automataState = STATE_WAITING_FRAME;
927 return CHANNEL_RC_OK;
928}
a touch event with some frames
a touch event with some frames
a frame containing contact points