Free clipboard data properly.
[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  * Matt Hortman
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include <sys/select.h>
43 #include <errno.h>
44
45 #include <freerdp/freerdp.h>
46 #include <freerdp/utils/memory.h>
47 #include <freerdp/cache/bitmap.h>
48 #include <freerdp/cache/brush.h>
49 #include <freerdp/cache/glyph.h>
50 #include <freerdp/cache/palette.h>
51 #include <freerdp/cache/pointer.h>
52 #include <freerdp/cache/offscreen.h>
53 #include <freerdp/channels/channels.h>
54 #include <freerdp/input.h>
55 #include <freerdp/constants.h>
56
57 #include <guacamole/socket.h>
58 #include <guacamole/protocol.h>
59 #include <guacamole/client.h>
60 #include <guacamole/error.h>
61
62 #include "client.h"
63 #include "guac_handlers.h"
64 #include "rdp_keymap.h"
65 #include "rdp_bitmap.h"
66 #include "rdp_glyph.h"
67 #include "rdp_pointer.h"
68 #include "rdp_gdi.h"
69
70 /* Client plugin arguments */
71 const char* GUAC_CLIENT_ARGS[] = {
72     "hostname",
73     "port",
74     "username",
75     "password",
76     "width",
77     "height",
78     "initial_program",
79     "color_depth",
80     NULL
81 };
82
83 enum ARGS_IDX {
84     IDX_HOSTNAME,
85     IDX_PORT,
86     IDX_USERNAME,
87     IDX_PASSWORD,
88     IDX_WIDTH,
89     IDX_HEIGHT,
90     IDX_INITIAL_PROGRAM,
91     IDX_COLOR_DEPTH
92 };
93
94 int __guac_receive_channel_data(freerdp* rdp_inst, int channelId, uint8* data, int size, int flags, int total_size) {
95     return freerdp_channels_data(rdp_inst, channelId, data, size, flags, total_size);
96 }
97
98 boolean rdp_freerdp_pre_connect(freerdp* instance) {
99
100     rdpContext* context = instance->context;
101     guac_client* client = ((rdp_freerdp_context*) context)->client;
102     rdpChannels* channels = context->channels;
103     rdpBitmap* bitmap;
104     rdpGlyph* glyph;
105     rdpPointer* pointer;
106     rdpPrimaryUpdate* primary;
107     CLRCONV* clrconv;
108
109     /* Load clipboard plugin */
110     freerdp_channels_load_plugin(channels, instance->settings, "cliprdr", NULL);
111
112     /* Init color conversion structure */
113     clrconv = xnew(CLRCONV);
114     clrconv->alpha = 1;
115     clrconv->invert = 0;
116     clrconv->rgb555 = 0;
117     clrconv->palette = xnew(rdpPalette);
118     ((rdp_freerdp_context*) context)->clrconv = clrconv;
119
120     /* Init FreeRDP cache */
121     instance->context->cache = cache_new(instance->settings);
122
123     /* Set up bitmap handling */
124     bitmap = xnew(rdpBitmap);
125     bitmap->size = sizeof(guac_rdp_bitmap);
126     bitmap->New = guac_rdp_bitmap_new;
127     bitmap->Free = guac_rdp_bitmap_free;
128     bitmap->Paint = guac_rdp_bitmap_paint;
129     bitmap->Decompress = guac_rdp_bitmap_decompress;
130     bitmap->SetSurface = guac_rdp_bitmap_setsurface;
131     graphics_register_bitmap(context->graphics, bitmap);
132     xfree(bitmap);
133
134     /* Set up glyph handling */
135     glyph = xnew(rdpGlyph);
136     glyph->size = sizeof(guac_rdp_glyph);
137     glyph->New = guac_rdp_glyph_new;
138     glyph->Free = guac_rdp_glyph_free;
139     glyph->Draw = guac_rdp_glyph_draw;
140     glyph->BeginDraw = guac_rdp_glyph_begindraw;
141     glyph->EndDraw = guac_rdp_glyph_enddraw;
142     graphics_register_glyph(context->graphics, glyph);
143     xfree(glyph);
144
145     /* Set up pointer handling */
146     pointer = xnew(rdpPointer);
147     pointer->size = sizeof(guac_rdp_pointer);
148     pointer->New = guac_rdp_pointer_new;
149     pointer->Free = guac_rdp_pointer_free;
150     pointer->Set = guac_rdp_pointer_set;
151     graphics_register_pointer(context->graphics, pointer);
152     xfree(pointer);
153
154     /* Set up GDI */
155     instance->update->EndPaint = guac_rdp_gdi_end_paint;
156     instance->update->Palette = guac_rdp_gdi_palette_update;
157     instance->update->SetBounds = guac_rdp_gdi_set_bounds;
158
159     primary = instance->update->primary;
160     primary->DstBlt = guac_rdp_gdi_dstblt;
161     primary->PatBlt = guac_rdp_gdi_patblt;
162     primary->ScrBlt = guac_rdp_gdi_scrblt;
163     primary->MemBlt = guac_rdp_gdi_memblt;
164     primary->OpaqueRect = guac_rdp_gdi_opaquerect;
165
166     pointer_cache_register_callbacks(instance->update);
167     glyph_cache_register_callbacks(instance->update);
168     brush_cache_register_callbacks(instance->update);
169     bitmap_cache_register_callbacks(instance->update);
170     offscreen_cache_register_callbacks(instance->update);
171     palette_cache_register_callbacks(instance->update);
172
173     /* Init channels (pre-connect) */
174     if (freerdp_channels_pre_connect(channels, instance)) {
175         guac_protocol_send_error(client->socket, "Error initializing RDP client channel manager");
176         guac_socket_flush(client->socket);
177         return false;
178     }
179
180     return true;
181
182 }
183
184 boolean rdp_freerdp_post_connect(freerdp* instance) {
185
186     rdpContext* context = instance->context;
187     guac_client* client = ((rdp_freerdp_context*) context)->client;
188     rdpChannels* channels = instance->context->channels;
189
190     /* Init channels (post-connect) */
191     if (freerdp_channels_post_connect(channels, instance)) {
192         guac_protocol_send_error(client->socket, "Error initializing RDP client channel manager");
193         guac_socket_flush(client->socket);
194         return false;
195     }
196
197     /* Client handlers */
198     client->free_handler = rdp_guac_client_free_handler;
199     client->handle_messages = rdp_guac_client_handle_messages;
200     client->mouse_handler = rdp_guac_client_mouse_handler;
201     client->key_handler = rdp_guac_client_key_handler;
202     client->clipboard_handler = rdp_guac_client_clipboard_handler;
203
204     return true;
205
206 }
207
208 void rdp_freerdp_context_new(freerdp* instance, rdpContext* context) {
209     context->channels = freerdp_channels_new();
210 }
211
212 void rdp_freerdp_context_free(freerdp* instance, rdpContext* context) {
213     /* EMPTY */
214 }
215
216 void __guac_rdp_client_load_keymap(guac_client* client,
217         const guac_rdp_keymap* keymap) {
218
219     rdp_guac_client_data* guac_client_data =
220         (rdp_guac_client_data*) client->data;
221     /* Get mapping */
222     const guac_rdp_keysym_desc* mapping = keymap->mapping;
223
224     /* If parent exists, load parent first */
225     if (keymap->parent != NULL)
226         __guac_rdp_client_load_keymap(client, keymap->parent);
227
228     /* Log load */
229     guac_client_log_info(client, "Loading keymap \"%s\"", keymap->name);
230
231     /* Load mapping into keymap */
232     while (mapping->keysym != 0) {
233
234         /* Copy mapping */
235         GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keymap, mapping->keysym) =
236             *mapping;
237
238         /* Next keysym */
239         mapping++;
240
241     }
242
243 }
244
245 int guac_client_init(guac_client* client, int argc, char** argv) {
246
247     rdp_guac_client_data* guac_client_data;
248
249     freerdp* rdp_inst;
250     rdpSettings* settings;
251
252     char* hostname;
253     int port = RDP_DEFAULT_PORT;
254     boolean bitmap_cache;
255
256     if (argc < 8) {
257
258         guac_protocol_send_error(client->socket,
259                 "Wrong argument count received.");
260         guac_socket_flush(client->socket);
261
262         guac_error = GUAC_STATUS_BAD_ARGUMENT;
263         guac_error_message = "Wrong argument count received";
264
265         return 1;
266     }
267
268     /* If port specified, use it */
269     if (argv[IDX_PORT][0] != '\0')
270         port = atoi(argv[IDX_PORT]);
271
272     hostname = argv[IDX_HOSTNAME];
273
274     /* Allocate client data */
275     guac_client_data = malloc(sizeof(rdp_guac_client_data));
276
277     /* Init client */
278     freerdp_channels_global_init();
279     rdp_inst = freerdp_new();
280     rdp_inst->PreConnect = rdp_freerdp_pre_connect;
281     rdp_inst->PostConnect = rdp_freerdp_post_connect;
282     rdp_inst->ReceiveChannelData = __guac_receive_channel_data;
283
284     /* Allocate FreeRDP context */
285     rdp_inst->context_size = sizeof(rdp_freerdp_context);
286     rdp_inst->ContextNew  = (pContextNew) rdp_freerdp_context_new;
287     rdp_inst->ContextFree = (pContextFree) rdp_freerdp_context_free;
288     freerdp_context_new(rdp_inst);
289
290     /* Set settings */
291     settings = rdp_inst->settings;
292
293     /* --no-auth */
294     settings->authentication = false;
295
296     /* --sec rdp */
297     settings->rdp_security = true;
298     settings->tls_security = false;
299     settings->nla_security = false;
300     settings->encryption = true;
301     settings->encryption_method = ENCRYPTION_METHOD_40BIT | ENCRYPTION_METHOD_128BIT | ENCRYPTION_METHOD_FIPS;
302     settings->encryption_level = ENCRYPTION_LEVEL_CLIENT_COMPATIBLE;
303
304     /* session width */
305     settings->width = 1024;
306     if (argv[IDX_WIDTH][0] != '\0')
307         settings->width = atoi(argv[IDX_WIDTH]);
308     if (settings->width == 0)
309         settings->width = 1024;
310
311     /* session height */
312     settings->height = 768;
313     if (argv[IDX_HEIGHT][0] != '\0')
314         settings->height = atoi(argv[IDX_HEIGHT]);
315     if (settings->height == 0)
316         settings->height = 768;
317
318     /* Set hostname */
319     settings->hostname = strdup(hostname);
320     settings->port = port;
321     settings->window_title = strdup(hostname);
322
323     /* username */
324     settings->username = "guest";
325     if (argv[IDX_USERNAME][0] != '\0')
326         settings->username = strdup (argv[IDX_USERNAME]);
327
328     /* password */
329     if (argv[IDX_PASSWORD][0] != '\0') {
330         settings->password = strdup (argv[IDX_PASSWORD]);
331         settings->autologon = 1;
332     }
333
334     /* initial program */
335     if (argv[IDX_INITIAL_PROGRAM][0] != '\0')
336         settings->shell = strdup (argv[IDX_INITIAL_PROGRAM]);
337
338     /* Order support */
339     bitmap_cache = settings->bitmap_cache;
340     settings->os_major_type = OSMAJORTYPE_UNSPECIFIED;
341     settings->os_minor_type = OSMINORTYPE_UNSPECIFIED;
342     settings->order_support[NEG_DSTBLT_INDEX] = true;
343     settings->order_support[NEG_PATBLT_INDEX] = false; /* PATBLT not yet supported */
344     settings->order_support[NEG_SCRBLT_INDEX] = true;
345     settings->order_support[NEG_OPAQUE_RECT_INDEX] = true;
346     settings->order_support[NEG_DRAWNINEGRID_INDEX] = false;
347     settings->order_support[NEG_MULTIDSTBLT_INDEX] = false;
348     settings->order_support[NEG_MULTIPATBLT_INDEX] = false;
349     settings->order_support[NEG_MULTISCRBLT_INDEX] = false;
350     settings->order_support[NEG_MULTIOPAQUERECT_INDEX] = false;
351     settings->order_support[NEG_MULTI_DRAWNINEGRID_INDEX] = false;
352     settings->order_support[NEG_LINETO_INDEX] = false;
353     settings->order_support[NEG_POLYLINE_INDEX] = false;
354     settings->order_support[NEG_MEMBLT_INDEX] = bitmap_cache;
355     settings->order_support[NEG_MEM3BLT_INDEX] = false;
356     settings->order_support[NEG_MEMBLT_V2_INDEX] = bitmap_cache;
357     settings->order_support[NEG_MEM3BLT_V2_INDEX] = false;
358     settings->order_support[NEG_SAVEBITMAP_INDEX] = false;
359     settings->order_support[NEG_GLYPH_INDEX_INDEX] = true;
360     settings->order_support[NEG_FAST_INDEX_INDEX] = true;
361     settings->order_support[NEG_FAST_GLYPH_INDEX] = true;
362     settings->order_support[NEG_POLYGON_SC_INDEX] = false;
363     settings->order_support[NEG_POLYGON_CB_INDEX] = false;
364     settings->order_support[NEG_ELLIPSE_SC_INDEX] = false;
365     settings->order_support[NEG_ELLIPSE_CB_INDEX] = false;
366
367     /* Store client data */
368     guac_client_data->rdp_inst = rdp_inst;
369     guac_client_data->mouse_button_mask = 0;
370     guac_client_data->current_surface = GUAC_DEFAULT_LAYER;
371     guac_client_data->clipboard = NULL;
372
373     /* Clear keysym state mapping and keymap */
374     memset(guac_client_data->keysym_state, 0,
375             sizeof(guac_rdp_keysym_state_map));
376
377     memset(guac_client_data->keymap, 0,
378             sizeof(guac_rdp_static_keymap));
379
380     client->data = guac_client_data;
381     ((rdp_freerdp_context*) rdp_inst->context)->client = client;
382
383     /* Load keymap into client */
384     __guac_rdp_client_load_keymap(client, &guac_rdp_keymap_en_us);
385
386     /* Connect to RDP server */
387     if (!freerdp_connect(rdp_inst)) {
388
389         guac_protocol_send_error(client->socket,
390                 "Error connecting to RDP server");
391         guac_socket_flush(client->socket);
392
393         guac_error = GUAC_STATUS_BAD_STATE;
394         guac_error_message = "Error connecting to RDP server";
395
396         return 1;
397     }
398
399     /* Send connection name */
400     guac_protocol_send_name(client->socket, settings->window_title);
401
402     /* Send size */
403     guac_protocol_send_size(client->socket, GUAC_DEFAULT_LAYER,
404             settings->width, settings->height);
405
406     /* Create glyph surfaces */
407     guac_client_data->opaque_glyph_surface = cairo_image_surface_create(
408             CAIRO_FORMAT_RGB24, settings->width, settings->height);
409
410     guac_client_data->trans_glyph_surface = cairo_image_surface_create(
411             CAIRO_FORMAT_ARGB32, settings->width, settings->height);
412
413     /* Success */
414     return 0;
415
416 }
417