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 = NULL;
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 = NULL;
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 = NULL;
99 rdpSettings* settings = NULL;
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 = NULL;
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 PubSub_SubscribeChannelConnected(instance->context->pubSub, tf_OnChannelConnectedEventHandler);
171 PubSub_SubscribeChannelDisconnected(instance->context->pubSub,
172 tf_OnChannelDisconnectedEventHandler);
173
174 /* TODO: Any code your client requires */
175 return TRUE;
176}
177
178/* Called after a RDP connection was successfully established.
179 * Settings might have changed during negotiation of client / server feature
180 * support.
181 *
182 * Set up local framebuffers and paing callbacks.
183 * If required, register pointer callbacks to change the local mouse cursor
184 * when hovering over the RDP window
185 */
186static BOOL tf_post_connect(freerdp* instance)
187{
188 rdpContext* context = NULL;
189
190 if (!gdi_init(instance, PIXEL_FORMAT_XRGB32))
191 return FALSE;
192
193 context = instance->context;
194 WINPR_ASSERT(context);
195 WINPR_ASSERT(context->update);
196
197 /* With this setting we disable all graphics processing in the library.
198 *
199 * This allows low resource (client) protocol parsing.
200 */
201 if (!freerdp_settings_set_bool(context->settings, FreeRDP_DeactivateClientDecoding, TRUE))
202 return FALSE;
203
204 context->update->BeginPaint = tf_begin_paint;
205 context->update->EndPaint = tf_end_paint;
206 context->update->PlaySound = tf_play_sound;
207 context->update->DesktopResize = tf_desktop_resize;
208 context->update->SetKeyboardIndicators = tf_keyboard_set_indicators;
209 context->update->SetKeyboardImeStatus = tf_keyboard_set_ime_status;
210 return TRUE;
211}
212
213/* This function is called whether a session ends by failure or success.
214 * Clean up everything allocated by pre_connect and post_connect.
215 */
216static void tf_post_disconnect(freerdp* instance)
217{
218 tfContext* context = NULL;
219
220 if (!instance)
221 return;
222
223 if (!instance->context)
224 return;
225
226 context = (tfContext*)instance->context;
227 PubSub_UnsubscribeChannelConnected(instance->context->pubSub,
228 tf_OnChannelConnectedEventHandler);
229 PubSub_UnsubscribeChannelDisconnected(instance->context->pubSub,
230 tf_OnChannelDisconnectedEventHandler);
231 gdi_free(instance);
232 /* TODO : Clean up custom stuff */
233 WINPR_UNUSED(context);
234}
235
236/* RDP main loop.
237 * Connects RDP, loops while running and handles event and dispatch, cleans up
238 * after the connection ends. */
239static DWORD WINAPI tf_client_thread_proc(LPVOID arg)
240{
241 freerdp* instance = (freerdp*)arg;
242 DWORD nCount = 0;
243 DWORD status = 0;
244 DWORD result = 0;
245 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = { 0 };
246 BOOL rc = freerdp_connect(instance);
247
248 WINPR_ASSERT(instance->context);
249 WINPR_ASSERT(instance->context->settings);
250 if (freerdp_settings_get_bool(instance->context->settings, FreeRDP_AuthenticationOnly))
251 {
252 result = freerdp_get_last_error(instance->context);
253 freerdp_abort_connect_context(instance->context);
254 WLog_ERR(TAG, "Authentication only, exit status 0x%08" PRIx32 "", result);
255 goto disconnect;
256 }
257
258 if (!rc)
259 {
260 result = freerdp_get_last_error(instance->context);
261 WLog_ERR(TAG, "connection failure 0x%08" PRIx32, result);
262 return result;
263 }
264
265 while (!freerdp_shall_disconnect_context(instance->context))
266 {
267 nCount = freerdp_get_event_handles(instance->context, handles, ARRAYSIZE(handles));
268
269 if (nCount == 0)
270 {
271 WLog_ERR(TAG, "freerdp_get_event_handles failed");
272 break;
273 }
274
275 status = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE);
276
277 if (status == WAIT_FAILED)
278 {
279 WLog_ERR(TAG, "WaitForMultipleObjects failed with %" PRIu32 "", status);
280 break;
281 }
282
283 if (!freerdp_check_event_handles(instance->context))
284 {
285 if (freerdp_get_last_error(instance->context) == FREERDP_ERROR_SUCCESS)
286 WLog_ERR(TAG, "Failed to check FreeRDP event handles");
287
288 break;
289 }
290 }
291
292disconnect:
293 freerdp_disconnect(instance);
294 return result;
295}
296
297/* Optional global initializer.
298 * Here we just register a signal handler to print out stack traces
299 * if available. */
300static BOOL tf_client_global_init(void)
301{
302 if (freerdp_handle_signals() != 0)
303 return FALSE;
304
305 return TRUE;
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 = NULL;
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 = { 0 };
391
392 RdpClientEntry(&clientEntryPoints);
393 rdpContext* context = freerdp_client_context_new(&clientEntryPoints);
394
395 if (!context)
396 goto fail;
397
398 const int status =
399 freerdp_client_settings_parse_command_line(context->settings, argc, argv, FALSE);
400 if (status)
401 {
402 rc = freerdp_client_settings_command_line_status_print(context->settings, status, argc,
403 argv);
404 goto fail;
405 }
406
407 if (!stream_dump_register_handlers(context, CONNECTION_STATE_MCS_CREATE_REQUEST, FALSE))
408 goto fail;
409
410 if (freerdp_client_start(context) != 0)
411 goto fail;
412
413 const DWORD res = tf_client_thread_proc(context->instance);
414 rc = (int)res;
415
416 if (freerdp_client_stop(context) != 0)
417 rc = -1;
418
419fail:
420 freerdp_client_context_free(context);
421 return rc;
422}
FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 param)
Sets a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL param)
Sets a BOOL settings value.