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