Fix changelog email address
[freerdp-ubuntu-pcb-backport.git] / libfreerdp-core / freerdp.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Client
3  * FreeRDP Core
4  *
5  * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  *
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 #include "rdp.h"
21 #include "input.h"
22 #include "update.h"
23 #include "surface.h"
24 #include "transport.h"
25 #include "connection.h"
26 #include "extension.h"
27
28 #include <freerdp/freerdp.h>
29 #include <freerdp/utils/memory.h>
30
31 boolean freerdp_connect(freerdp* instance)
32 {
33         rdpRdp* rdp;
34         boolean status = false;
35
36         rdp = instance->context->rdp;
37
38         extension_pre_connect(rdp->extension);
39
40         IFCALLRET(instance->PreConnect, status, instance);
41
42         if (status != true)
43         {
44                 printf("freerdp_pre_connect failed\n");
45                 return false;
46         }
47
48         status = rdp_client_connect(rdp);
49
50         if (status)
51         {
52                 if (instance->settings->dump_rfx)
53                 {
54                         instance->update->pcap_rfx = pcap_open(instance->settings->dump_rfx_file, true);
55                         if (instance->update->pcap_rfx)
56                                 instance->update->dump_rfx = true;
57                 }
58
59                 extension_post_connect(rdp->extension);
60
61                 IFCALLRET(instance->PostConnect, status, instance);
62
63                 if (status != true)
64                 {
65                         printf("freerdp_post_connect failed\n");
66                         return false;
67                 }
68
69                 if (instance->settings->play_rfx)
70                 {
71                         STREAM* s;
72                         rdpUpdate* update;
73                         pcap_record record;
74
75                         s = stream_new(1024);
76                         instance->update->pcap_rfx = pcap_open(instance->settings->play_rfx_file, false);
77                         if (instance->update->pcap_rfx)
78                                 instance->update->play_rfx = true;
79                         update = instance->update;
80
81                         while (instance->update->play_rfx && pcap_has_next_record(update->pcap_rfx))
82                         {
83                                 pcap_get_next_record_header(update->pcap_rfx, &record);
84
85                                 s->data = xrealloc(s->data, record.length);
86                                 record.data = s->data;
87                                 s->size = record.length;
88
89                                 pcap_get_next_record_content(update->pcap_rfx, &record);
90                                 stream_set_pos(s, 0);
91
92                                 update->BeginPaint(update->context);
93                                 update_recv_surfcmds(update, s->size, s);
94                                 update->EndPaint(update->context);
95                         }
96
97                         xfree(s->data);
98                         return true;
99                 }
100         }
101
102         return status;
103 }
104
105 boolean freerdp_get_fds(freerdp* instance, void** rfds, int* rcount, void** wfds, int* wcount)
106 {
107         rdpRdp* rdp;
108
109         rdp = instance->context->rdp;
110         transport_get_fds(rdp->transport, rfds, rcount);
111
112         return true;
113 }
114
115 boolean freerdp_check_fds(freerdp* instance)
116 {
117         int status;
118         rdpRdp* rdp;
119
120         rdp = instance->context->rdp;
121
122         status = rdp_check_fds(rdp);
123
124         if (status < 0)
125                 return false;
126
127         return true;
128 }
129
130 void freerdp_send_keep_alive(freerdp* instance)
131 {
132         input_send_synchronize_event(instance->context->rdp->input, 0);
133 }
134
135 static int freerdp_send_channel_data(freerdp* instance, int channel_id, uint8* data, int size)
136 {
137         return rdp_send_channel_data(instance->context->rdp, channel_id, data, size);
138 }
139
140 boolean freerdp_disconnect(freerdp* instance)
141 {
142         rdpRdp* rdp;
143
144         rdp = instance->context->rdp;
145         transport_disconnect(rdp->transport);
146
147         return true;
148 }
149
150 boolean freerdp_shall_disconnect(freerdp* instance)
151 {
152
153         return instance->context->rdp->disconnect;
154 }
155
156 void freerdp_get_version(int* major, int* minor, int* revision)
157 {
158         if (major != NULL)
159                 *major = FREERDP_VERSION_MAJOR;
160
161         if (minor != NULL)
162                 *minor = FREERDP_VERSION_MINOR;
163
164         if (revision != NULL)
165                 *revision = FREERDP_VERSION_REVISION;
166 }
167
168 void freerdp_context_new(freerdp* instance)
169 {
170         rdpRdp* rdp;
171
172         rdp = rdp_new(instance);
173         instance->input = rdp->input;
174         instance->update = rdp->update;
175         instance->settings = rdp->settings;
176
177         instance->context = (rdpContext*) xzalloc(instance->context_size);
178         instance->context->graphics = graphics_new(instance->context);
179         instance->context->instance = instance;
180         instance->context->rdp = rdp;
181
182         instance->update->context = instance->context;
183         instance->update->pointer->context = instance->context;
184         instance->update->primary->context = instance->context;
185         instance->update->secondary->context = instance->context;
186         instance->update->altsec->context = instance->context;
187
188         instance->input->context = instance->context;
189
190         IFCALL(instance->ContextNew, instance, instance->context);
191 }
192
193 void freerdp_context_free(freerdp* instance)
194 {
195         IFCALL(instance->ContextFree, instance, instance->context);
196
197         rdp_free(instance->context->rdp);
198         graphics_free(instance->context->graphics);
199         xfree(instance->context);
200 }
201
202 uint32 freerdp_error_info(freerdp* instance)
203 {
204         return instance->context->rdp->errorInfo;
205 }
206
207 freerdp* freerdp_new()
208 {
209         freerdp* instance;
210
211         instance = xzalloc(sizeof(freerdp));
212
213         if (instance != NULL)
214         {
215                 instance->context_size = sizeof(rdpContext);
216                 instance->SendChannelData = freerdp_send_channel_data;
217         }
218
219         return instance;
220 }
221
222 void freerdp_free(freerdp* freerdp)
223 {
224         if (freerdp)
225         {
226                 freerdp_context_free(freerdp);
227                 xfree(freerdp);
228         }
229 }