FreeRDP
Loading...
Searching...
No Matches
tf_freerdp.c
1
22#include <freerdp/config.h>
23
24#include <errno.h>
25#include <stdio.h>
26#include <string.h>
27
28#include <freerdp/freerdp.h>
29#include <freerdp/constants.h>
30#include <freerdp/gdi/gdi.h>
31#include <freerdp/streamdump.h>
32#include <freerdp/utils/signal.h>
33
34#include <freerdp/client/file.h>
35#include <freerdp/client/cmdline.h>
36#include <freerdp/client/cliprdr.h>
37#include <freerdp/client/channels.h>
38#include <freerdp/channels/channels.h>
39
40#include <winpr/crt.h>
41#include <winpr/assert.h>
42#include <winpr/synch.h>
43#include <freerdp/log.h>
44
45#include "tf_channels.h"
46#include "tf_freerdp.h"
47
48#define TAG CLIENT_TAG("sample")
49
50/* This function is called whenever a new frame starts.
51 * It can be used to reset invalidated areas. */
52static BOOL tf_begin_paint(rdpContext* context)
53{
54 rdpGdi* gdi = nullptr;
55
56 WINPR_ASSERT(context);
57
58 gdi = context->gdi;
59 WINPR_ASSERT(gdi);
60 WINPR_ASSERT(gdi->primary);
61 WINPR_ASSERT(gdi->primary->hdc);
62 WINPR_ASSERT(gdi->primary->hdc->hwnd);
63 WINPR_ASSERT(gdi->primary->hdc->hwnd->invalid);
64 gdi->primary->hdc->hwnd->invalid->null = TRUE;
65 return TRUE;
66}
67
68/* This function is called when the library completed composing a new
69 * frame. Read out the changed areas and blit them to your output device.
70 * The image buffer will have the format specified by gdi_init
71 */
72static BOOL tf_end_paint(rdpContext* context)
73{
74 rdpGdi* gdi = nullptr;
75
76 WINPR_ASSERT(context);
77
78 gdi = context->gdi;
79 WINPR_ASSERT(gdi);
80 WINPR_ASSERT(gdi->primary);
81
82 HGDI_DC hdc = gdi->primary->hdc;
83 WINPR_ASSERT(hdc);
84 if (!hdc->hwnd)
85 return TRUE;
86
87 HGDI_WND hwnd = hdc->hwnd;
88 WINPR_ASSERT(hwnd->invalid || (hwnd->ninvalid == 0));
89
90 if (hwnd->invalid->null)
91 return TRUE;
92
93 return TRUE;
94}
95
96static BOOL tf_desktop_resize(rdpContext* context)
97{
98 rdpGdi* gdi = nullptr;
99 rdpSettings* settings = nullptr;
100
101 WINPR_ASSERT(context);
102
103 settings = context->settings;
104 WINPR_ASSERT(settings);
105
106 gdi = context->gdi;
107 return gdi_resize(gdi, freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth),
108 freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight));
109}
110
111/* This function is called to output a System BEEP */
112static BOOL tf_play_sound(rdpContext* context, const PLAY_SOUND_UPDATE* play_sound)
113{
114 /* TODO: Implement */
115 WINPR_UNUSED(context);
116 WINPR_UNUSED(play_sound);
117 return TRUE;
118}
119
120/* This function is called to update the keyboard indocator LED */
121static BOOL tf_keyboard_set_indicators(rdpContext* context, UINT16 led_flags)
122{
123 /* TODO: Set local keyboard indicator LED status */
124 WINPR_UNUSED(context);
125 WINPR_UNUSED(led_flags);
126 return TRUE;
127}
128
129/* This function is called to set the IME state */
130static BOOL tf_keyboard_set_ime_status(rdpContext* context, UINT16 imeId, UINT32 imeState,
131 UINT32 imeConvMode)
132{
133 if (!context)
134 return FALSE;
135
136 WLog_WARN(TAG,
137 "KeyboardSetImeStatus(unitId=%04" PRIx16 ", imeState=%08" PRIx32
138 ", imeConvMode=%08" PRIx32 ") ignored",
139 imeId, imeState, imeConvMode);
140 return TRUE;
141}
142
143/* Called before a connection is established.
144 * Set all configuration options to support and load channels here. */
145static BOOL tf_pre_connect(freerdp* instance)
146{
147 rdpSettings* settings = nullptr;
148
149 WINPR_ASSERT(instance);
150 WINPR_ASSERT(instance->context);
151
152 settings = instance->context->settings;
153 WINPR_ASSERT(settings);
154
155 /* If the callbacks provide the PEM all certificate options can be extracted, otherwise
156 * only the certificate fingerprint is available. */
157 if (!freerdp_settings_set_bool(settings, FreeRDP_CertificateCallbackPreferPEM, TRUE))
158 return FALSE;
159
160 /* Optional OS identifier sent to server */
161 if (!freerdp_settings_set_uint32(settings, FreeRDP_OsMajorType, OSMAJORTYPE_UNIX))
162 return FALSE;
163 if (!freerdp_settings_set_uint32(settings, FreeRDP_OsMinorType, OSMINORTYPE_NATIVE_XSERVER))
164 return FALSE;
165 /* OrderSupport is initialized at this point.
166 * Only override it if you plan to implement custom order
167 * callbacks or deactivate certain features. */
168 /* Register the channel listeners.
169 * They are required to set up / tear down channels if they are loaded. */
170 if (PubSub_SubscribeChannelConnected(instance->context->pubSub,
171 tf_OnChannelConnectedEventHandler) < 0)
172 return FALSE;
173 if (PubSub_SubscribeChannelDisconnected(instance->context->pubSub,
174 tf_OnChannelDisconnectedEventHandler) < 0)
175 return FALSE;
176
177 /* TODO: Any code your client requires */
178 return TRUE;
179}
180
181/* Called after a RDP connection was successfully established.
182 * Settings might have changed during negotiation of client / server feature
183 * support.
184 *
185 * Set up local framebuffers and paing callbacks.
186 * If required, register pointer callbacks to change the local mouse cursor
187 * when hovering over the RDP window
188 */
189static BOOL tf_post_connect(freerdp* instance)
190{
191 rdpContext* context = nullptr;
192
193 if (!gdi_init(instance, PIXEL_FORMAT_XRGB32))
194 return FALSE;
195
196 context = instance->context;
197 WINPR_ASSERT(context);
198 WINPR_ASSERT(context->update);
199
200 /* With this setting we disable all graphics processing in the library.
201 *
202 * This allows low resource (client) protocol parsing.
203 */
204 if (!freerdp_settings_set_bool(context->settings, FreeRDP_DeactivateClientDecoding, TRUE))
205 return FALSE;
206
207 context->update->BeginPaint = tf_begin_paint;
208 context->update->EndPaint = tf_end_paint;
209 context->update->PlaySound = tf_play_sound;
210 context->update->DesktopResize = tf_desktop_resize;
211 context->update->SetKeyboardIndicators = tf_keyboard_set_indicators;
212 context->update->SetKeyboardImeStatus = tf_keyboard_set_ime_status;
213 return TRUE;
214}
215
216/* This function is called whether a session ends by failure or success.
217 * Clean up everything allocated by pre_connect and post_connect.
218 */
219static void tf_post_disconnect(freerdp* instance)
220{
221 tfContext* context = nullptr;
222
223 if (!instance)
224 return;
225
226 if (!instance->context)
227 return;
228
229 context = (tfContext*)instance->context;
230 PubSub_UnsubscribeChannelConnected(instance->context->pubSub,
231 tf_OnChannelConnectedEventHandler);
232 PubSub_UnsubscribeChannelDisconnected(instance->context->pubSub,
233 tf_OnChannelDisconnectedEventHandler);
234 gdi_free(instance);
235 /* TODO : Clean up custom stuff */
236 WINPR_UNUSED(context);
237}
238
239/* RDP main loop.
240 * Connects RDP, loops while running and handles event and dispatch, cleans up
241 * after the connection ends. */
242static DWORD WINAPI tf_client_thread_proc(LPVOID arg)
243{
244 freerdp* instance = (freerdp*)arg;
245 DWORD nCount = 0;
246 DWORD status = 0;
247 DWORD result = 0;
248 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = WINPR_C_ARRAY_INIT;
249 BOOL rc = freerdp_connect(instance);
250
251 WINPR_ASSERT(instance->context);
252 WINPR_ASSERT(instance->context->settings);
253 if (freerdp_settings_get_bool(instance->context->settings, FreeRDP_AuthenticationOnly))
254 {
255 result = freerdp_get_last_error(instance->context);
256 freerdp_abort_connect_context(instance->context);
257 WLog_ERR(TAG, "Authentication only, exit status 0x%08" PRIx32 "", result);
258 goto disconnect;
259 }
260
261 if (!rc)
262 {
263 result = freerdp_get_last_error(instance->context);
264 WLog_ERR(TAG, "connection failure 0x%08" PRIx32, result);
265 return result;
266 }
267
268 while (!freerdp_shall_disconnect_context(instance->context))
269 {
270 nCount = freerdp_get_event_handles(instance->context, handles, ARRAYSIZE(handles));
271
272 if (nCount == 0)
273 {
274 WLog_ERR(TAG, "freerdp_get_event_handles failed");
275 break;
276 }
277
278 status = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE);
279
280 if (status == WAIT_FAILED)
281 {
282 WLog_ERR(TAG, "WaitForMultipleObjects failed with %" PRIu32 "", status);
283 break;
284 }
285
286 if (!freerdp_check_event_handles(instance->context))
287 {
288 if (freerdp_get_last_error(instance->context) == FREERDP_ERROR_SUCCESS)
289 WLog_ERR(TAG, "Failed to check FreeRDP event handles");
290
291 break;
292 }
293 }
294
295disconnect:
296 freerdp_disconnect(instance);
297 return result;
298}
299
300/* Optional global initializer.
301 * Here we just register a signal handler to print out stack traces
302 * if available. */
303static BOOL tf_client_global_init(void)
304{
305 return freerdp_handle_signals() == 0;
306}
307
308/* Optional global tear down */
309static void tf_client_global_uninit(void)
310{
311}
312
313static int tf_logon_error_info(freerdp* instance, UINT32 data, UINT32 type)
314{
315 tfContext* tf = nullptr;
316 const char* str_data = freerdp_get_logon_error_info_data(data);
317 const char* str_type = freerdp_get_logon_error_info_type(type);
318
319 if (!instance || !instance->context)
320 return -1;
321
322 tf = (tfContext*)instance->context;
323 WLog_INFO(TAG, "Logon Error Info %s [%s]", str_data, str_type);
324 WINPR_UNUSED(tf);
325
326 return 1;
327}
328
329static BOOL tf_client_new(freerdp* instance, rdpContext* context)
330{
331 tfContext* tf = (tfContext*)context;
332
333 if (!instance || !context)
334 return FALSE;
335
336 instance->PreConnect = tf_pre_connect;
337 instance->PostConnect = tf_post_connect;
338 instance->PostDisconnect = tf_post_disconnect;
339 instance->LogonErrorInfo = tf_logon_error_info;
340 /* TODO: Client display set up */
341 WINPR_UNUSED(tf);
342 return TRUE;
343}
344
345static void tf_client_free(freerdp* instance, rdpContext* context)
346{
347 tfContext* tf = (tfContext*)instance->context;
348
349 if (!context)
350 return;
351
352 /* TODO: Client display tear down */
353 WINPR_UNUSED(tf);
354}
355
356static int tf_client_start(rdpContext* context)
357{
358 /* TODO: Start client related stuff */
359 WINPR_UNUSED(context);
360 return 0;
361}
362
363static int tf_client_stop(rdpContext* context)
364{
365 /* TODO: Stop client related stuff */
366 WINPR_UNUSED(context);
367 return 0;
368}
369
370static int RdpClientEntry(RDP_CLIENT_ENTRY_POINTS* pEntryPoints)
371{
372 WINPR_ASSERT(pEntryPoints);
373
374 ZeroMemory(pEntryPoints, sizeof(RDP_CLIENT_ENTRY_POINTS));
375 pEntryPoints->Version = RDP_CLIENT_INTERFACE_VERSION;
376 pEntryPoints->Size = sizeof(RDP_CLIENT_ENTRY_POINTS_V1);
377 pEntryPoints->GlobalInit = tf_client_global_init;
378 pEntryPoints->GlobalUninit = tf_client_global_uninit;
379 pEntryPoints->ContextSize = sizeof(tfContext);
380 pEntryPoints->ClientNew = tf_client_new;
381 pEntryPoints->ClientFree = tf_client_free;
382 pEntryPoints->ClientStart = tf_client_start;
383 pEntryPoints->ClientStop = tf_client_stop;
384 return 0;
385}
386
387int main(int argc, char* argv[])
388{
389 int rc = -1;
390 RDP_CLIENT_ENTRY_POINTS clientEntryPoints = WINPR_C_ARRAY_INIT;
391
392 RdpClientEntry(&clientEntryPoints);
393 rdpContext* context = freerdp_client_context_new(&clientEntryPoints);
394
395 if (!context)
396 goto fail;
397
398 {
399 const int status =
400 freerdp_client_settings_parse_command_line(context->settings, argc, argv, FALSE);
401 if (status)
402 {
403 rc = freerdp_client_settings_command_line_status_print(context->settings, status, argc,
404 argv);
405 goto fail;
406 }
407 }
408
409 if (!stream_dump_register_handlers(context, CONNECTION_STATE_MCS_CREATE_REQUEST, FALSE))
410 goto fail;
411
412 if (freerdp_client_start(context) != 0)
413 goto fail;
414
415 {
416 const DWORD res = tf_client_thread_proc(context->instance);
417 rc = (int)res;
418 }
419
420 if (freerdp_client_stop(context) != 0)
421 rc = -1;
422
423fail:
424 freerdp_client_context_free(context);
425 return rc;
426}
FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 val)
Sets a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL val)
Sets a BOOL settings value.
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.