client/common: parse and use remote assistance file
[platform/upstream/freerdp.git] / client / common / client.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Implementation
3  * FreeRDP Client Common
4  *
5  * Copyright 2012 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 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <freerdp/client.h>
25
26 #include <freerdp/addin.h>
27 #include <freerdp/client/file.h>
28 #include <freerdp/client/cmdline.h>
29 #include <freerdp/client/channels.h>
30 #include <freerdp/client/assistance.h>
31
32 int freerdp_client_common_new(freerdp* instance, rdpContext* context)
33 {
34         RDP_CLIENT_ENTRY_POINTS* pEntryPoints = instance->pClientEntryPoints;
35         return pEntryPoints->ClientNew(instance, context);
36 }
37
38 void freerdp_client_common_free(freerdp* instance, rdpContext* context)
39 {
40         RDP_CLIENT_ENTRY_POINTS* pEntryPoints = instance->pClientEntryPoints;
41         pEntryPoints->ClientFree(instance, context);
42 }
43
44 /* Common API */
45
46 rdpContext* freerdp_client_context_new(RDP_CLIENT_ENTRY_POINTS* pEntryPoints)
47 {
48         freerdp* instance;
49         rdpContext* context;
50
51         pEntryPoints->GlobalInit();
52
53         instance = freerdp_new();
54         instance->settings = pEntryPoints->settings;
55         instance->ContextSize = pEntryPoints->ContextSize;
56         instance->ContextNew = freerdp_client_common_new;
57         instance->ContextFree = freerdp_client_common_free;
58         instance->pClientEntryPoints = (RDP_CLIENT_ENTRY_POINTS*) malloc(pEntryPoints->Size);
59         CopyMemory(instance->pClientEntryPoints, pEntryPoints, pEntryPoints->Size);
60         freerdp_context_new(instance);
61
62         context = instance->context;
63         context->instance = instance;
64         context->settings = instance->settings;
65
66         freerdp_register_addin_provider(freerdp_channels_load_static_addin_entry, 0);
67
68         return context;
69 }
70
71 void freerdp_client_context_free(rdpContext* context)
72 {
73         freerdp* instance = context->instance;
74
75         if (instance)
76         {
77                 freerdp_context_free(instance);
78                 free(instance->pClientEntryPoints);
79                 freerdp_free(instance);
80         }
81 }
82
83 int freerdp_client_start(rdpContext* context)
84 {
85         RDP_CLIENT_ENTRY_POINTS* pEntryPoints = context->instance->pClientEntryPoints;
86         return pEntryPoints->ClientStart(context);
87 }
88
89 int freerdp_client_stop(rdpContext* context)
90 {
91         RDP_CLIENT_ENTRY_POINTS* pEntryPoints = context->instance->pClientEntryPoints;
92         return pEntryPoints->ClientStop(context);
93 }
94
95 freerdp* freerdp_client_get_instance(rdpContext* context)
96 {
97         return context->instance;
98 }
99
100 HANDLE freerdp_client_get_thread(rdpContext* context)
101 {
102         return ((rdpClientContext*) context)->thread;
103 }
104
105 int freerdp_client_settings_parse_command_line(rdpSettings* settings, int argc, char** argv)
106 {
107         int status;
108
109         if (argc < 1)
110                 return 0;
111
112         if (!argv)
113                 return -1;
114
115         status = freerdp_client_settings_parse_command_line_arguments(settings, argc, argv);
116
117         if (settings->ConnectionFile)
118         {
119                 status = freerdp_client_settings_parse_connection_file(settings, settings->ConnectionFile);
120         }
121
122         if (settings->AssistanceFile)
123         {
124                 status = freerdp_client_settings_parse_assistance_file(settings, settings->AssistanceFile);
125         }
126
127         return status;
128 }
129
130 int freerdp_client_settings_parse_connection_file(rdpSettings* settings, const char* filename)
131 {
132         rdpFile* file;
133
134         file = freerdp_client_rdp_file_new();
135         freerdp_client_parse_rdp_file(file, filename);
136         freerdp_client_populate_settings_from_rdp_file(file, settings);
137         freerdp_client_rdp_file_free(file);
138
139         return 0;
140 }
141
142 int freerdp_client_settings_parse_connection_file_buffer(rdpSettings* settings, const BYTE* buffer, size_t size)
143 {
144         rdpFile* file;
145         int status = -1;
146
147         file = freerdp_client_rdp_file_new();
148
149         if (freerdp_client_parse_rdp_file_buffer(file, buffer, size)
150                         && freerdp_client_populate_settings_from_rdp_file(file, settings))
151         {
152                 status = 0;
153         }
154
155         freerdp_client_rdp_file_free(file);
156
157         return status;
158 }
159
160 int freerdp_client_settings_write_connection_file(const rdpSettings* settings, const char* filename, BOOL unicode)
161 {
162         rdpFile* file;
163
164         file = freerdp_client_rdp_file_new();
165
166         if (!freerdp_client_populate_rdp_file_from_settings(file, settings))
167                 return -1;
168
169         if (!freerdp_client_write_rdp_file(file, filename, unicode))
170                 return -1;
171
172         freerdp_client_rdp_file_free(file);
173
174         return 0;
175 }
176
177 int freerdp_client_settings_parse_assistance_file(rdpSettings* settings, const char* filename)
178 {
179         int status;
180         rdpAssistanceFile* file;
181
182         file = freerdp_client_assistance_file_new();
183
184         if (!file)
185                 return -1;
186
187         status = freerdp_client_assistance_parse_file(file, filename);
188
189         if (status < 0)
190                 return -1;
191
192         status = freerdp_client_populate_settings_from_assistance_file(file, settings);
193
194         if (status < 0)
195                 return -1;
196
197         freerdp_client_assistance_file_free(file);
198
199         return 0;
200 }