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