2
* FreeRDP: A Remote Desktop Protocol Implementation
5
* Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
7
* Licensed under the Apache License, Version 2.0 (the "License");
8
* you may not use this file except in compliance with the License.
9
* You may obtain a copy of the License at
11
* http://www.apache.org/licenses/LICENSE-2.0
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an "AS IS" BASIS,
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
* See the License for the specific language governing permissions and
17
* limitations under the License.
28
#include "transport.h"
29
#include "connection.h"
30
#include "extension.h"
33
#include <winpr/crt.h>
34
#include <winpr/stream.h>
36
#include <freerdp/freerdp.h>
37
#include <freerdp/error.h>
38
#include <freerdp/event.h>
39
#include <freerdp/locale/keyboard.h>
40
#include <freerdp/version.h>
42
/* connectErrorCode is 'extern' in error.h. See comment there.*/
44
/** Creates a new connection based on the settings found in the "instance" parameter
45
* It will use the callbacks registered on the structure to process the pre/post connect operations
46
* that the caller requires.
47
* @see struct rdp_freerdp in freerdp.h
49
* @param instance - pointer to a rdp_freerdp structure that contains base information to establish the connection.
50
* On return, this function will be initialized with the new connection's settings.
52
* @return TRUE if successful. FALSE otherwise.
55
BOOL freerdp_connect(freerdp* instance)
58
rdpSettings* settings;
60
ConnectionResultEventArgs e;
62
/* We always set the return code to 0 before we start the connect sequence*/
65
rdp = instance->context->rdp;
66
settings = instance->settings;
68
IFCALLRET(instance->PreConnect, status, instance);
70
if (settings->KeyboardLayout == KBD_JAPANESE_INPUT_SYSTEM_MS_IME2002)
72
settings->KeyboardType = 7;
73
settings->KeyboardSubType = 2;
74
settings->KeyboardFunctionKey = 12;
77
extension_load_and_init_plugins(rdp->extension);
78
extension_pre_connect(rdp->extension);
82
if (!connectErrorCode)
84
connectErrorCode = PREECONNECTERROR;
86
fprintf(stderr, "%s:%d: freerdp_pre_connect failed\n", __FILE__, __LINE__);
90
status = rdp_client_connect(rdp);
92
/* --authonly tests the connection without a UI */
93
if (instance->settings->AuthenticationOnly)
95
fprintf(stderr, "%s:%d: Authentication only, exit status %d\n", __FILE__, __LINE__, !status);
101
if (instance->settings->DumpRemoteFx)
103
instance->update->pcap_rfx = pcap_open(instance->settings->DumpRemoteFxFile, TRUE);
104
if (instance->update->pcap_rfx)
105
instance->update->dump_rfx = TRUE;
108
extension_post_connect(rdp->extension);
110
IFCALLRET(instance->PostConnect, status, instance);
111
update_post_connect(instance->update);
115
fprintf(stderr, "freerdp_post_connect failed\n");
117
if (!connectErrorCode)
119
connectErrorCode = POSTCONNECTERROR;
125
if (instance->settings->PlayRemoteFx)
131
update = instance->update;
133
update->pcap_rfx = pcap_open(settings->PlayRemoteFxFile, FALSE);
135
if (!update->pcap_rfx)
138
update->play_rfx = TRUE;
140
while (pcap_has_next_record(update->pcap_rfx))
143
pcap_get_next_record_header(update->pcap_rfx, &record);
145
s = StreamPool_Take(rdp->transport->ReceivePool, record.length);
146
record.data = Stream_Buffer(s);
148
pcap_get_next_record_content(update->pcap_rfx, &record);
149
Stream_SetLength(s,record.length);
150
Stream_SetPosition(s, 0);
152
update->BeginPaint(update->context);
153
update_recv_surfcmds(update, Stream_Length(s) , s);
154
update->EndPaint(update->context);
162
if (rdp->errorInfo == ERRINFO_SERVER_INSUFFICIENT_PRIVILEGES)
164
connectErrorCode = INSUFFICIENTPRIVILEGESERROR;
167
if (!connectErrorCode)
169
connectErrorCode = UNDEFINEDCONNECTERROR;
172
SetEvent(rdp->transport->connectedEvent);
174
EventArgsInit(&e, "freerdp");
175
e.result = status ? 0 : -1;
176
PubSub_OnConnectionResult(instance->context->pubSub, instance->context, &e);
181
BOOL freerdp_get_fds(freerdp* instance, void** rfds, int* rcount, void** wfds, int* wcount)
185
rdp = instance->context->rdp;
186
transport_get_fds(rdp->transport, rfds, rcount);
191
BOOL freerdp_check_fds(freerdp* instance)
196
rdp = instance->context->rdp;
198
status = rdp_check_fds(rdp);
202
TerminateEventArgs e;
203
rdpContext* context = instance->context;
205
EventArgsInit(&e, "freerdp");
207
PubSub_OnTerminate(context->pubSub, context, &e);
215
wMessageQueue* freerdp_get_message_queue(freerdp* instance, DWORD id)
217
wMessageQueue* queue = NULL;
221
case FREERDP_UPDATE_MESSAGE_QUEUE:
222
queue = instance->update->queue;
225
case FREERDP_INPUT_MESSAGE_QUEUE:
226
queue = instance->input->queue;
233
HANDLE freerdp_get_message_queue_event_handle(freerdp* instance, DWORD id)
236
wMessageQueue* queue = NULL;
238
queue = freerdp_get_message_queue(instance, id);
241
event = MessageQueue_Event(queue);
246
int freerdp_message_queue_process_message(freerdp* instance, DWORD id, wMessage* message)
252
case FREERDP_UPDATE_MESSAGE_QUEUE:
253
status = update_message_queue_process_message(instance->update, message);
256
case FREERDP_INPUT_MESSAGE_QUEUE:
257
status = input_message_queue_process_message(instance->input, message);
264
int freerdp_message_queue_process_pending_messages(freerdp* instance, DWORD id)
270
case FREERDP_UPDATE_MESSAGE_QUEUE:
271
status = update_message_queue_process_pending_messages(instance->update);
274
case FREERDP_INPUT_MESSAGE_QUEUE:
275
status = input_message_queue_process_pending_messages(instance->input);
282
static int freerdp_send_channel_data(freerdp* instance, int channel_id, BYTE* data, int size)
284
return rdp_send_channel_data(instance->context->rdp, channel_id, data, size);
287
BOOL freerdp_disconnect(freerdp* instance)
291
rdp = instance->context->rdp;
292
transport_disconnect(rdp->transport);
297
BOOL freerdp_shall_disconnect(freerdp* instance)
299
return instance->context->rdp->disconnect;
302
FREERDP_API BOOL freerdp_focus_required(freerdp* instance)
305
BOOL bRetCode = FALSE;
307
rdp = instance->context->rdp;
309
if (rdp->resendFocus)
312
rdp->resendFocus = FALSE;
318
void freerdp_get_version(int* major, int* minor, int* revision)
321
*major = FREERDP_VERSION_MAJOR;
324
*minor = FREERDP_VERSION_MINOR;
326
if (revision != NULL)
327
*revision = FREERDP_VERSION_REVISION;
330
static wEventType FreeRDP_Events[] =
332
DEFINE_EVENT_ENTRY(WindowStateChange)
333
DEFINE_EVENT_ENTRY(ResizeWindow)
334
DEFINE_EVENT_ENTRY(LocalResizeWindow)
335
DEFINE_EVENT_ENTRY(EmbedWindow)
336
DEFINE_EVENT_ENTRY(PanningChange)
337
DEFINE_EVENT_ENTRY(ScalingFactorChange)
338
DEFINE_EVENT_ENTRY(ErrorInfo)
339
DEFINE_EVENT_ENTRY(ParamChange)
340
DEFINE_EVENT_ENTRY(Terminate)
341
DEFINE_EVENT_ENTRY(ConnectionResult)
342
DEFINE_EVENT_ENTRY(ChannelConnected)
343
DEFINE_EVENT_ENTRY(ChannelDisconnected)
346
/** Allocator function for a rdp context.
347
* The function will allocate a rdpRdp structure using rdp_new(), then copy
348
* its contents to the appropriate fields in the rdp_freerdp structure given in parameters.
349
* It will also initialize the 'context' field in the rdp_freerdp structure as needed.
350
* If the caller has set the ContextNew callback in the 'instance' parameter, it will be called at the end of the function.
352
* @param instance - Pointer to the rdp_freerdp structure that will be initialized with the new context.
354
int freerdp_context_new(freerdp* instance)
359
instance->context = (rdpContext*) malloc(instance->ContextSize);
360
ZeroMemory(instance->context, instance->ContextSize);
361
context = instance->context;
363
context->pubSub = PubSub_New(TRUE);
364
PubSub_AddEventTypes(context->pubSub, FreeRDP_Events, sizeof(FreeRDP_Events) / sizeof(wEventType));
366
rdp = rdp_new(instance);
367
instance->input = rdp->input;
368
instance->update = rdp->update;
369
instance->settings = rdp->settings;
371
context->graphics = graphics_new(context);
372
context->instance = instance;
375
context->input = instance->input;
376
context->update = instance->update;
377
context->settings = instance->settings;
379
instance->update->context = instance->context;
380
instance->update->pointer->context = instance->context;
381
instance->update->primary->context = instance->context;
382
instance->update->secondary->context = instance->context;
383
instance->update->altsec->context = instance->context;
385
instance->input->context = context;
387
update_register_client_callbacks(rdp->update);
389
IFCALL(instance->ContextNew, instance, instance->context);
394
/** Deallocator function for a rdp context.
395
* The function will deallocate the resources from the 'instance' parameter that were allocated from a call
396
* to freerdp_context_new().
397
* If the ContextFree callback is set in the 'instance' parameter, it will be called before deallocation occurs.
399
* @param instance - Pointer to the rdp_freerdp structure that was initialized by a call to freerdp_context_new().
400
* On return, the fields associated to the context are invalid.
402
void freerdp_context_free(freerdp* instance)
404
if (!instance->context)
407
IFCALL(instance->ContextFree, instance, instance->context);
409
rdp_free(instance->context->rdp);
410
instance->context->rdp = NULL;
412
graphics_free(instance->context->graphics);
413
instance->context->graphics = NULL;
415
PubSub_Free(instance->context->pubSub);
417
free(instance->context);
418
instance->context = NULL;
421
UINT32 freerdp_error_info(freerdp* instance)
423
return instance->context->rdp->errorInfo;
426
/** Allocator function for the rdp_freerdp structure.
427
* @return an allocated structure filled with 0s. Need to be deallocated using freerdp_free()
429
freerdp* freerdp_new()
433
instance = (freerdp*) malloc(sizeof(freerdp));
437
ZeroMemory(instance, sizeof(freerdp));
438
instance->ContextSize = sizeof(rdpContext);
439
instance->SendChannelData = freerdp_send_channel_data;
445
/** Deallocator function for the rdp_freerdp structure.
446
* @param instance - pointer to the rdp_freerdp structure to deallocate.
447
* On return, this pointer is not valid anymore.
449
void freerdp_free(freerdp* instance)