Migrate to 0.6.0 libguac API.
[libguac-client-rdp.git] / src / client.c
1
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is libguac-client-rdp.
16  *
17  * The Initial Developer of the Original Code is
18  * Michael Jumper.
19  * Portions created by the Initial Developer are Copyright (C) 2011
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include <sys/select.h>
42 #include <errno.h>
43
44 #include <freerdp/freerdp.h>
45 #include <freerdp/utils/memory.h>
46 #include <freerdp/cache/bitmap.h>
47 #include <freerdp/cache/brush.h>
48 #include <freerdp/cache/glyph.h>
49 #include <freerdp/cache/palette.h>
50 #include <freerdp/cache/pointer.h>
51 #include <freerdp/cache/offscreen.h>
52 #include <freerdp/channels/channels.h>
53 #include <freerdp/input.h>
54 #include <freerdp/constants.h>
55
56 #include <guacamole/socket.h>
57 #include <guacamole/protocol.h>
58 #include <guacamole/client.h>
59
60 #include "client.h"
61 #include "guac_handlers.h"
62 #include "rdp_keymap.h"
63 #include "rdp_bitmap.h"
64 #include "rdp_glyph.h"
65 #include "rdp_pointer.h"
66 #include "rdp_gdi.h"
67
68 /* Client plugin arguments */
69 const char* GUAC_CLIENT_ARGS[] = {
70     "hostname",
71     "port",
72     NULL
73 };
74
75 boolean rdp_freerdp_pre_connect(freerdp* instance) {
76
77     rdpContext* context = instance->context;
78     guac_client* client = ((rdp_freerdp_context*) context)->client;
79     rdpChannels* channels = context->channels;
80     rdpBitmap* bitmap;
81     rdpGlyph* glyph;
82     rdpPointer* pointer;
83     rdpPrimaryUpdate* primary;
84     CLRCONV* clrconv;
85
86     /* Init color conversion structure */
87     clrconv = xnew(CLRCONV);
88     clrconv->alpha = 1;
89     clrconv->invert = 0;
90     clrconv->rgb555 = 0;
91     clrconv->palette = xnew(rdpPalette);
92     ((rdp_freerdp_context*) context)->clrconv = clrconv;
93
94     /* Init FreeRDP cache */
95     instance->context->cache = cache_new(instance->settings);
96
97     /* Set up bitmap handling */
98     bitmap = xnew(rdpBitmap);
99     bitmap->size = sizeof(guac_rdp_bitmap);
100     bitmap->New = guac_rdp_bitmap_new;
101     bitmap->Free = guac_rdp_bitmap_free;
102     bitmap->Paint = guac_rdp_bitmap_paint;
103     bitmap->Decompress = guac_rdp_bitmap_decompress;
104     bitmap->SetSurface = guac_rdp_bitmap_setsurface;
105     graphics_register_bitmap(context->graphics, bitmap);
106
107     /* Set up glyph handling */
108     glyph = xnew(rdpGlyph);
109     glyph->size = sizeof(guac_rdp_glyph);
110     glyph->New = guac_rdp_glyph_new;
111     glyph->Free = guac_rdp_glyph_free;
112     glyph->Draw = guac_rdp_glyph_draw;
113     glyph->BeginDraw = guac_rdp_glyph_begindraw;
114     glyph->EndDraw = guac_rdp_glyph_enddraw;
115     graphics_register_glyph(context->graphics, glyph);
116
117     /* Set up pointer handling */
118     pointer = xnew(rdpPointer);
119     pointer->size = sizeof(guac_rdp_pointer);
120     pointer->New = guac_rdp_pointer_new;
121     pointer->Free = guac_rdp_pointer_free;
122     pointer->Set = guac_rdp_pointer_set;
123     graphics_register_pointer(context->graphics, pointer);
124
125     /* Set up GDI */
126     instance->update->Palette = guac_rdp_gdi_palette_update;
127     instance->update->SetBounds = guac_rdp_gdi_set_bounds;
128
129     primary = instance->update->primary;
130     primary->DstBlt = guac_rdp_gdi_dstblt;
131     primary->PatBlt = guac_rdp_gdi_patblt;
132     primary->ScrBlt = guac_rdp_gdi_scrblt;
133     primary->MemBlt = guac_rdp_gdi_memblt;
134     primary->OpaqueRect = guac_rdp_gdi_opaquerect;
135
136     pointer_cache_register_callbacks(instance->update);
137     glyph_cache_register_callbacks(instance->update);
138     brush_cache_register_callbacks(instance->update);
139     bitmap_cache_register_callbacks(instance->update);
140     offscreen_cache_register_callbacks(instance->update);
141     palette_cache_register_callbacks(instance->update);
142
143     /* Init channels (pre-connect) */
144     if (freerdp_channels_pre_connect(channels, instance)) {
145         guac_protocol_send_error(client->socket, "Error initializing RDP client channel manager");
146         guac_socket_flush(client->socket);
147         return false;
148     }
149
150     return true;
151
152 }
153
154 boolean rdp_freerdp_post_connect(freerdp* instance) {
155
156     rdpContext* context = instance->context;
157     guac_client* client = ((rdp_freerdp_context*) context)->client;
158     rdpChannels* channels = instance->context->channels;
159
160     /* Init channels (post-connect) */
161     if (freerdp_channels_post_connect(channels, instance)) {
162         guac_protocol_send_error(client->socket, "Error initializing RDP client channel manager");
163         guac_socket_flush(client->socket);
164         return false;
165     }
166
167     /* Client handlers */
168     client->free_handler = rdp_guac_client_free_handler;
169     client->handle_messages = rdp_guac_client_handle_messages;
170     client->mouse_handler = rdp_guac_client_mouse_handler;
171     client->key_handler = rdp_guac_client_key_handler;
172
173     /* Send size */
174     guac_protocol_send_size(client->socket, GUAC_DEFAULT_LAYER,
175             instance->settings->width, instance->settings->height);
176
177     return true;
178
179 }
180
181 void rdp_freerdp_context_new(freerdp* instance, rdpContext* context) {
182     context->channels = freerdp_channels_new();
183 }
184
185 void rdp_freerdp_context_free(freerdp* instance, rdpContext* context) {
186     /* EMPTY */
187 }
188
189 int guac_client_init(guac_client* client, int argc, char** argv) {
190
191     rdp_guac_client_data* guac_client_data;
192
193     freerdp* rdp_inst;
194         rdpSettings* settings;
195
196     char* hostname;
197     int port = RDP_DEFAULT_PORT;
198     boolean bitmap_cache;
199
200     if (argc < 2) {
201         guac_protocol_send_error(client->socket, "Wrong argument count received.");
202         guac_socket_flush(client->socket);
203         return 1;
204     }
205
206     /* If port specified, use it */
207     if (argv[1][0] != '\0')
208         port = atoi(argv[1]);
209
210     hostname = argv[0];
211
212     /* Allocate client data */
213     guac_client_data = malloc(sizeof(rdp_guac_client_data));
214
215     /* Init client */
216     freerdp_channels_global_init();
217     rdp_inst = freerdp_new();
218     rdp_inst->PreConnect = rdp_freerdp_pre_connect;
219     rdp_inst->PostConnect = rdp_freerdp_post_connect;
220
221     /* Allocate FreeRDP context */
222     rdp_inst->context_size = sizeof(rdp_freerdp_context);
223     rdp_inst->ContextNew  = (pContextNew) rdp_freerdp_context_new;
224     rdp_inst->ContextFree = (pContextFree) rdp_freerdp_context_free;
225     freerdp_context_new(rdp_inst);
226
227     /* Set settings */
228     settings = rdp_inst->settings;
229
230     /* --no-auth */
231     settings->authentication = false;
232
233     /* --sec rdp */
234     settings->rdp_security = true;
235     settings->tls_security = false;
236     settings->nla_security = false;
237     settings->encryption = true;
238     settings->encryption_method = ENCRYPTION_METHOD_40BIT | ENCRYPTION_METHOD_128BIT | ENCRYPTION_METHOD_FIPS;
239     settings->encryption_level = ENCRYPTION_LEVEL_CLIENT_COMPATIBLE;
240
241     /* Default size */
242         settings->width = 1024;
243         settings->height = 768;
244
245     /* Set hostname */
246     settings->hostname = strdup(hostname);
247         settings->window_title = strdup(hostname);
248         settings->username = "guest";
249
250     /* Order support */
251     bitmap_cache = settings->bitmap_cache;
252     settings->os_major_type = OSMAJORTYPE_UNSPECIFIED;
253     settings->os_minor_type = OSMINORTYPE_UNSPECIFIED;
254     settings->order_support[NEG_DSTBLT_INDEX] = true;
255     settings->order_support[NEG_PATBLT_INDEX] = true;
256     settings->order_support[NEG_SCRBLT_INDEX] = true;
257     settings->order_support[NEG_OPAQUE_RECT_INDEX] = true;
258     settings->order_support[NEG_DRAWNINEGRID_INDEX] = false;
259     settings->order_support[NEG_MULTIDSTBLT_INDEX] = false;
260     settings->order_support[NEG_MULTIPATBLT_INDEX] = false;
261     settings->order_support[NEG_MULTISCRBLT_INDEX] = false;
262     settings->order_support[NEG_MULTIOPAQUERECT_INDEX] = false;
263     settings->order_support[NEG_MULTI_DRAWNINEGRID_INDEX] = false;
264     settings->order_support[NEG_LINETO_INDEX] = false;
265     settings->order_support[NEG_POLYLINE_INDEX] = false;
266     settings->order_support[NEG_MEMBLT_INDEX] = bitmap_cache;
267     settings->order_support[NEG_MEM3BLT_INDEX] = false;
268     settings->order_support[NEG_MEMBLT_V2_INDEX] = bitmap_cache;
269     settings->order_support[NEG_MEM3BLT_V2_INDEX] = false;
270     settings->order_support[NEG_SAVEBITMAP_INDEX] = false;
271     settings->order_support[NEG_GLYPH_INDEX_INDEX] = false;
272     settings->order_support[NEG_FAST_INDEX_INDEX] = false;
273     settings->order_support[NEG_FAST_GLYPH_INDEX] = false;
274     settings->order_support[NEG_POLYGON_SC_INDEX] = false;
275     settings->order_support[NEG_POLYGON_CB_INDEX] = false;
276     settings->order_support[NEG_ELLIPSE_SC_INDEX] = false;
277     settings->order_support[NEG_ELLIPSE_CB_INDEX] = false;
278
279     /* Store client data */
280     guac_client_data->rdp_inst = rdp_inst;
281     guac_client_data->mouse_button_mask = 0;
282     guac_client_data->current_surface = GUAC_DEFAULT_LAYER;
283
284     ((rdp_freerdp_context*) rdp_inst->context)->client = client;
285     client->data = guac_client_data;
286
287     /* Connect to RDP server */
288     if (!freerdp_connect(rdp_inst)) {
289         guac_protocol_send_error(client->socket, "Error connecting to RDP server");
290         guac_socket_flush(client->socket);
291         return 1;
292     }
293
294     /* Success */
295     return 0;
296
297 }
298