a78f83aea8c25366be18211962164375acdced64
[platform/upstream/freerdp.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 void freerdp_get_version(int* major, int* minor, int* revision)
151 {
152         if (major != NULL)
153                 *major = FREERDP_VERSION_MAJOR;
154
155         if (minor != NULL)
156                 *minor = FREERDP_VERSION_MINOR;
157
158         if (revision != NULL)
159                 *revision = FREERDP_VERSION_REVISION;
160 }
161
162 void freerdp_context_new(freerdp* instance)
163 {
164         rdpRdp* rdp;
165
166         rdp = rdp_new(instance);
167         instance->input = rdp->input;
168         instance->update = rdp->update;
169         instance->settings = rdp->settings;
170
171         instance->context = (rdpContext*) xzalloc(instance->context_size);
172         instance->context->graphics = graphics_new(instance->context);
173         instance->context->instance = instance;
174         instance->context->rdp = rdp;
175
176         instance->update->context = instance->context;
177         instance->update->pointer->context = instance->context;
178         instance->update->primary->context = instance->context;
179         instance->update->secondary->context = instance->context;
180         instance->update->altsec->context = instance->context;
181
182         instance->input->context = instance->context;
183
184         IFCALL(instance->ContextNew, instance, instance->context);
185 }
186
187 void freerdp_context_free(freerdp* instance)
188 {
189         IFCALL(instance->ContextFree, instance, instance->context);
190 }
191
192 uint32 freerdp_error_info(freerdp* instance)
193 {
194         return instance->context->rdp->errorInfo;
195 }
196
197 freerdp* freerdp_new()
198 {
199         freerdp* instance;
200
201         instance = xzalloc(sizeof(freerdp));
202
203         if (instance != NULL)
204         {
205                 instance->context_size = sizeof(rdpContext);
206                 instance->SendChannelData = freerdp_send_channel_data;
207         }
208
209         return instance;
210 }
211
212 void freerdp_free(freerdp* freerdp)
213 {
214         if (freerdp)
215         {
216                 rdp_free(freerdp->context->rdp);
217                 xfree(freerdp);
218         }
219 }