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