FreeRDP
Loading...
Searching...
No Matches
client/ainput_main.c
1
21#include <freerdp/config.h>
22
23#include <stdio.h>
24#include <stdlib.h>
25
26#include <winpr/crt.h>
27#include <winpr/assert.h>
28#include <winpr/stream.h>
29#include <winpr/sysinfo.h>
30
31#include "ainput_main.h"
32#include <freerdp/channels/log.h>
33#include <freerdp/client/channels.h>
34#include <freerdp/client/ainput.h>
35#include <freerdp/channels/ainput.h>
36
37#include "../common/ainput_common.h"
38
39#define TAG CHANNELS_TAG("ainput.client")
40
41typedef struct AINPUT_PLUGIN_ AINPUT_PLUGIN;
42struct AINPUT_PLUGIN_
43{
45 AInputClientContext* context;
46 UINT32 MajorVersion;
47 UINT32 MinorVersion;
49};
50
56static UINT ainput_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* data)
57{
58 UINT16 type = 0;
59 AINPUT_PLUGIN* ainput = nullptr;
60 GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
61
62 WINPR_ASSERT(callback);
63 WINPR_ASSERT(data);
64
65 ainput = (AINPUT_PLUGIN*)callback->plugin;
66 WINPR_ASSERT(ainput);
67
68 if (!Stream_CheckAndLogRequiredLength(TAG, data, 2))
69 return ERROR_NO_DATA;
70 Stream_Read_UINT16(data, type);
71 switch (type)
72 {
73 case MSG_AINPUT_VERSION:
74 if (!Stream_CheckAndLogRequiredLength(TAG, data, 8))
75 return ERROR_NO_DATA;
76 Stream_Read_UINT32(data, ainput->MajorVersion);
77 Stream_Read_UINT32(data, ainput->MinorVersion);
78 break;
79 default:
80 WLog_WARN(TAG, "Received unsupported message type 0x%04" PRIx16, type);
81 break;
82 }
83
84 return CHANNEL_RC_OK;
85}
86
87static UINT ainput_send_input_event(AInputClientContext* context, UINT64 flags, INT32 x, INT32 y)
88{
89 BYTE buffer[32] = WINPR_C_ARRAY_INIT;
90 wStream sbuffer = WINPR_C_ARRAY_INIT;
91 wStream* s = Stream_StaticInit(&sbuffer, buffer, sizeof(buffer));
92
93 WINPR_ASSERT(s);
94 WINPR_ASSERT(context);
95
96 const UINT64 time = GetTickCount64();
97 AINPUT_PLUGIN* ainput = (AINPUT_PLUGIN*)context->handle;
98 WINPR_ASSERT(ainput);
99
100 if (ainput->MajorVersion != AINPUT_VERSION_MAJOR)
101 {
102 WLog_WARN(TAG, "Unsupported channel version %" PRIu32 ".%" PRIu32 ", aborting.",
103 ainput->MajorVersion, ainput->MinorVersion);
104 return CHANNEL_RC_UNSUPPORTED_VERSION;
105 }
106
107 {
108 char ebuffer[128] = WINPR_C_ARRAY_INIT;
109 WLog_VRB(TAG, "sending timestamp=0x%08" PRIx64 ", flags=%s, %" PRId32 "x%" PRId32, time,
110 ainput_flags_to_string(flags, ebuffer, sizeof(ebuffer)), x, y);
111 }
112
113 /* Message type */
114 Stream_Write_UINT16(s, MSG_AINPUT_MOUSE);
115
116 /* Event data */
117 Stream_Write_UINT64(s, time);
118 Stream_Write_UINT64(s, flags);
119 Stream_Write_INT32(s, x);
120 Stream_Write_INT32(s, y);
121 Stream_SealLength(s);
122
123 /* ainput back what we have received. AINPUT does not have any message IDs. */
124 UINT rc = CHANNEL_RC_NOT_OPEN;
125 EnterCriticalSection(&ainput->lock);
126 GENERIC_CHANNEL_CALLBACK* callback = ainput->base.listener_callback->channel_callback;
127 /* The channel might have been closed by the server in the meantime, ainput_on_close
128 * resets the callback pointer in that case. Let the caller fall back to the default
129 * input channel. */
130 if (callback)
131 {
132 WINPR_ASSERT(callback->channel);
133 WINPR_ASSERT(callback->channel->Write);
134 rc = callback->channel->Write(callback->channel, (ULONG)Stream_Length(s), Stream_Buffer(s),
135 nullptr);
136 }
137 LeaveCriticalSection(&ainput->lock);
138 return rc;
139}
140
146static UINT ainput_on_close(IWTSVirtualChannelCallback* pChannelCallback)
147{
148 GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
149
150 if (callback)
151 {
152 AINPUT_PLUGIN* ainput = (AINPUT_PLUGIN*)callback->plugin;
153 WINPR_ASSERT(ainput);
154
155 /* Lock here to ensure that no ainput_send_input_event is in progress and
156 * reset the listener callback pointer so that no later one uses the freed callback. */
157 EnterCriticalSection(&ainput->lock);
158 if (ainput->base.listener_callback->channel_callback == callback)
159 ainput->base.listener_callback->channel_callback = nullptr;
160 free(callback);
161 LeaveCriticalSection(&ainput->lock);
162 }
163 return CHANNEL_RC_OK;
164}
165
166static UINT init_plugin_cb(GENERIC_DYNVC_PLUGIN* base, WINPR_ATTR_UNUSED rdpContext* rcontext,
167 WINPR_ATTR_UNUSED rdpSettings* settings)
168{
169 AINPUT_PLUGIN* ainput = (AINPUT_PLUGIN*)base;
170 AInputClientContext* context = (AInputClientContext*)calloc(1, sizeof(AInputClientContext));
171 if (!context)
172 return CHANNEL_RC_NO_MEMORY;
173
174 context->handle = (void*)base;
175 context->AInputSendInputEvent = ainput_send_input_event;
176
177 InitializeCriticalSection(&ainput->lock);
178
179 EnterCriticalSection(&ainput->lock);
180 ainput->context = context;
181 ainput->base.iface.pInterface = context;
182 LeaveCriticalSection(&ainput->lock);
183 return CHANNEL_RC_OK;
184}
185
186static void terminate_plugin_cb(GENERIC_DYNVC_PLUGIN* base)
187{
188 AINPUT_PLUGIN* ainput = (AINPUT_PLUGIN*)base;
189 WINPR_ASSERT(ainput);
190
191 DeleteCriticalSection(&ainput->lock);
192 free(ainput->context);
193}
194
195static const IWTSVirtualChannelCallback ainput_functions = { ainput_on_data_received,
196 nullptr, /* Open */
197 ainput_on_close, nullptr };
198
204FREERDP_ENTRY_POINT(UINT VCAPITYPE ainput_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
205{
206 return freerdp_generic_DVCPluginEntry(pEntryPoints, TAG, AINPUT_DVC_CHANNEL_NAME,
207 sizeof(AINPUT_PLUGIN), sizeof(GENERIC_CHANNEL_CALLBACK),
208 &ainput_functions, init_plugin_cb, terminate_plugin_cb);
209}