Add SetNull and SetDefault functions to pointer managment to avoyd segfault with...
[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 #include "default_pointer.h"
70
71 /* Client plugin arguments */
72 const char* GUAC_CLIENT_ARGS[] = {
73     "hostname",
74     "port",
75     "domain",
76     "username",
77     "password",
78     "width",
79     "height",
80     "initial-program",
81     "color-depth",
82     NULL
83 };
84
85 enum ARGS_IDX {
86     IDX_HOSTNAME,
87     IDX_PORT,
88     IDX_DOMAIN,
89     IDX_USERNAME,
90     IDX_PASSWORD,
91     IDX_WIDTH,
92     IDX_HEIGHT,
93     IDX_INITIAL_PROGRAM,
94     IDX_COLOR_DEPTH
95 };
96
97 int __guac_receive_channel_data(freerdp* rdp_inst, int channelId, uint8* data, int size, int flags, int total_size) {
98     return freerdp_channels_data(rdp_inst, channelId, data, size, flags, total_size);
99 }
100
101 boolean rdp_freerdp_pre_connect(freerdp* instance) {
102
103     rdpContext* context = instance->context;
104     guac_client* client = ((rdp_freerdp_context*) context)->client;
105     rdpChannels* channels = context->channels;
106     rdpBitmap* bitmap;
107     rdpGlyph* glyph;
108     rdpPointer* pointer;
109     rdpPrimaryUpdate* primary;
110     CLRCONV* clrconv;
111
112     /* Load clipboard plugin */
113     freerdp_channels_load_plugin(channels, instance->settings, "cliprdr", NULL);
114
115     /* Init color conversion structure */
116     clrconv = xnew(CLRCONV);
117     clrconv->alpha = 1;
118     clrconv->invert = 0;
119     clrconv->rgb555 = 0;
120     clrconv->palette = xnew(rdpPalette);
121     ((rdp_freerdp_context*) context)->clrconv = clrconv;
122
123     /* Init FreeRDP cache */
124     instance->context->cache = cache_new(instance->settings);
125
126     /* Set up bitmap handling */
127     bitmap = xnew(rdpBitmap);
128     bitmap->size = sizeof(guac_rdp_bitmap);
129     bitmap->New = guac_rdp_bitmap_new;
130     bitmap->Free = guac_rdp_bitmap_free;
131     bitmap->Paint = guac_rdp_bitmap_paint;
132     bitmap->Decompress = guac_rdp_bitmap_decompress;
133     bitmap->SetSurface = guac_rdp_bitmap_setsurface;
134     graphics_register_bitmap(context->graphics, bitmap);
135     xfree(bitmap);
136
137     /* Set up glyph handling */
138     glyph = xnew(rdpGlyph);
139     glyph->size = sizeof(guac_rdp_glyph);
140     glyph->New = guac_rdp_glyph_new;
141     glyph->Free = guac_rdp_glyph_free;
142     glyph->Draw = guac_rdp_glyph_draw;
143     glyph->BeginDraw = guac_rdp_glyph_begindraw;
144     glyph->EndDraw = guac_rdp_glyph_enddraw;
145     graphics_register_glyph(context->graphics, glyph);
146     xfree(glyph);
147
148     /* Set up pointer handling */
149     pointer = xnew(rdpPointer);
150     pointer->size = sizeof(guac_rdp_pointer);
151     pointer->New = guac_rdp_pointer_new;
152     pointer->Free = guac_rdp_pointer_free;
153     pointer->Set = guac_rdp_pointer_set;
154     pointer->SetNull = guac_rdp_pointer_set_null;
155     pointer->SetDefault = guac_rdp_pointer_set_default;
156     graphics_register_pointer(context->graphics, pointer);
157     xfree(pointer);
158
159     /* Set up GDI */
160     instance->update->EndPaint = guac_rdp_gdi_end_paint;
161     instance->update->Palette = guac_rdp_gdi_palette_update;
162     instance->update->SetBounds = guac_rdp_gdi_set_bounds;
163
164     primary = instance->update->primary;
165     primary->DstBlt = guac_rdp_gdi_dstblt;
166     primary->PatBlt = guac_rdp_gdi_patblt;
167     primary->ScrBlt = guac_rdp_gdi_scrblt;
168     primary->MemBlt = guac_rdp_gdi_memblt;
169     primary->OpaqueRect = guac_rdp_gdi_opaquerect;
170
171     pointer_cache_register_callbacks(instance->update);
172     glyph_cache_register_callbacks(instance->update);
173     brush_cache_register_callbacks(instance->update);
174     bitmap_cache_register_callbacks(instance->update);
175     offscreen_cache_register_callbacks(instance->update);
176     palette_cache_register_callbacks(instance->update);
177
178     /* Init channels (pre-connect) */
179     if (freerdp_channels_pre_connect(channels, instance)) {
180         guac_protocol_send_error(client->socket, "Error initializing RDP client channel manager");
181         guac_socket_flush(client->socket);
182         return false;
183     }
184
185     return true;
186
187 }
188
189 boolean rdp_freerdp_post_connect(freerdp* instance) {
190
191     rdpContext* context = instance->context;
192     guac_client* client = ((rdp_freerdp_context*) context)->client;
193     rdpChannels* channels = instance->context->channels;
194
195     /* Init channels (post-connect) */
196     if (freerdp_channels_post_connect(channels, instance)) {
197         guac_protocol_send_error(client->socket, "Error initializing RDP client channel manager");
198         guac_socket_flush(client->socket);
199         return false;
200     }
201
202     /* Client handlers */
203     client->free_handler = rdp_guac_client_free_handler;
204     client->handle_messages = rdp_guac_client_handle_messages;
205     client->mouse_handler = rdp_guac_client_mouse_handler;
206     client->key_handler = rdp_guac_client_key_handler;
207     client->clipboard_handler = rdp_guac_client_clipboard_handler;
208
209     return true;
210
211 }
212
213 void rdp_freerdp_context_new(freerdp* instance, rdpContext* context) {
214     context->channels = freerdp_channels_new();
215 }
216
217 void rdp_freerdp_context_free(freerdp* instance, rdpContext* context) {
218     /* EMPTY */
219 }
220
221 void __guac_rdp_client_load_keymap(guac_client* client,
222         const guac_rdp_keymap* keymap) {
223
224     rdp_guac_client_data* guac_client_data =
225         (rdp_guac_client_data*) client->data;
226
227     /* Get mapping */
228     const guac_rdp_keysym_desc* mapping = keymap->mapping;
229
230     /* If parent exists, load parent first */
231     if (keymap->parent != NULL)
232         __guac_rdp_client_load_keymap(client, keymap->parent);
233
234     /* Log load */
235     guac_client_log_info(client, "Loading keymap \"%s\"", keymap->name);
236
237     /* Load mapping into keymap */
238     while (mapping->keysym != 0) {
239
240         /* Copy mapping */
241         GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keymap, mapping->keysym) =
242             *mapping;
243
244         /* Next keysym */
245         mapping++;
246
247     }
248
249 }
250
251 int guac_client_init(guac_client* client, int argc, char** argv) {
252
253     rdp_guac_client_data* guac_client_data;
254
255     freerdp* rdp_inst;
256     rdpSettings* settings;
257
258     char* hostname;
259     int port = RDP_DEFAULT_PORT;
260     boolean bitmap_cache;
261
262     /**
263      * Selected server-side keymap. Client will be assumed to also use this
264      * keymap. Keys will be sent to server based on client input on a
265      * best-effort basis.
266      *
267      * Currently hard-coded to en-us-qwerty.
268      */
269     const guac_rdp_keymap* chosen_keymap = &guac_rdp_keymap_en_us;
270
271     if (argc < 9) {
272
273         guac_protocol_send_error(client->socket,
274                 "Wrong argument count received.");
275         guac_socket_flush(client->socket);
276
277         guac_error = GUAC_STATUS_BAD_ARGUMENT;
278         guac_error_message = "Wrong argument count received";
279
280         return 1;
281     }
282
283     /* If port specified, use it */
284     if (argv[IDX_PORT][0] != '\0')
285         port = atoi(argv[IDX_PORT]);
286
287     hostname = argv[IDX_HOSTNAME];
288
289     /* Allocate client data */
290     guac_client_data = malloc(sizeof(rdp_guac_client_data));
291
292     /* Init client */
293     freerdp_channels_global_init();
294     rdp_inst = freerdp_new();
295     rdp_inst->PreConnect = rdp_freerdp_pre_connect;
296     rdp_inst->PostConnect = rdp_freerdp_post_connect;
297     rdp_inst->ReceiveChannelData = __guac_receive_channel_data;
298
299     /* Allocate FreeRDP context */
300     rdp_inst->context_size = sizeof(rdp_freerdp_context);
301     rdp_inst->ContextNew  = (pContextNew) rdp_freerdp_context_new;
302     rdp_inst->ContextFree = (pContextFree) rdp_freerdp_context_free;
303     freerdp_context_new(rdp_inst);
304
305     /* Set settings */
306     settings = rdp_inst->settings;
307
308     /* --no-auth */
309     settings->authentication = false;
310
311     /* --sec rdp */
312     settings->rdp_security = true;
313     settings->tls_security = false;
314     settings->nla_security = false;
315     settings->encryption = true;
316     settings->encryption_method = ENCRYPTION_METHOD_40BIT | ENCRYPTION_METHOD_128BIT | ENCRYPTION_METHOD_FIPS;
317     settings->encryption_level = ENCRYPTION_LEVEL_CLIENT_COMPATIBLE;
318
319     /* session width */
320     settings->width = 1024;
321     if (argv[IDX_WIDTH][0] != '\0')
322         settings->width = atoi(argv[IDX_WIDTH]);
323     if (settings->width == 0)
324         settings->width = 1024;
325
326     /* session height */
327     settings->height = 768;
328     if (argv[IDX_HEIGHT][0] != '\0')
329         settings->height = atoi(argv[IDX_HEIGHT]);
330     if (settings->height == 0)
331         settings->height = 768;
332
333     /* Set hostname */
334     settings->hostname = strdup(hostname);
335     settings->port = port;
336     settings->window_title = strdup(hostname);
337
338     /* Domain */
339     if (argv[IDX_DOMAIN][0] != '\0')
340         settings->domain = strdup(argv[IDX_DOMAIN]);
341
342     /* Username */
343     if (argv[IDX_USERNAME][0] != '\0')
344         settings->username = strdup(argv[IDX_USERNAME]);
345
346     /* Password */
347     if (argv[IDX_PASSWORD][0] != '\0') {
348         settings->password = strdup(argv[IDX_PASSWORD]);
349         settings->autologon = 1;
350     }
351
352     /* Initial program */
353     if (argv[IDX_INITIAL_PROGRAM][0] != '\0')
354         settings->shell = strdup(argv[IDX_INITIAL_PROGRAM]);
355
356     /* Order support */
357     bitmap_cache = settings->bitmap_cache;
358     settings->os_major_type = OSMAJORTYPE_UNSPECIFIED;
359     settings->os_minor_type = OSMINORTYPE_UNSPECIFIED;
360     settings->order_support[NEG_DSTBLT_INDEX] = true;
361     settings->order_support[NEG_PATBLT_INDEX] = false; /* PATBLT not yet supported */
362     settings->order_support[NEG_SCRBLT_INDEX] = true;
363     settings->order_support[NEG_OPAQUE_RECT_INDEX] = true;
364     settings->order_support[NEG_DRAWNINEGRID_INDEX] = false;
365     settings->order_support[NEG_MULTIDSTBLT_INDEX] = false;
366     settings->order_support[NEG_MULTIPATBLT_INDEX] = false;
367     settings->order_support[NEG_MULTISCRBLT_INDEX] = false;
368     settings->order_support[NEG_MULTIOPAQUERECT_INDEX] = false;
369     settings->order_support[NEG_MULTI_DRAWNINEGRID_INDEX] = false;
370     settings->order_support[NEG_LINETO_INDEX] = false;
371     settings->order_support[NEG_POLYLINE_INDEX] = false;
372     settings->order_support[NEG_MEMBLT_INDEX] = bitmap_cache;
373     settings->order_support[NEG_MEM3BLT_INDEX] = false;
374     settings->order_support[NEG_MEMBLT_V2_INDEX] = bitmap_cache;
375     settings->order_support[NEG_MEM3BLT_V2_INDEX] = false;
376     settings->order_support[NEG_SAVEBITMAP_INDEX] = false;
377     settings->order_support[NEG_GLYPH_INDEX_INDEX] = true;
378     settings->order_support[NEG_FAST_INDEX_INDEX] = true;
379     settings->order_support[NEG_FAST_GLYPH_INDEX] = true;
380     settings->order_support[NEG_POLYGON_SC_INDEX] = false;
381     settings->order_support[NEG_POLYGON_CB_INDEX] = false;
382     settings->order_support[NEG_ELLIPSE_SC_INDEX] = false;
383     settings->order_support[NEG_ELLIPSE_CB_INDEX] = false;
384
385     /* Store client data */
386     guac_client_data->rdp_inst = rdp_inst;
387     guac_client_data->mouse_button_mask = 0;
388     guac_client_data->current_surface = GUAC_DEFAULT_LAYER;
389     guac_client_data->clipboard = NULL;
390
391     /* Clear keysym state mapping and keymap */
392     memset(guac_client_data->keysym_state, 0,
393             sizeof(guac_rdp_keysym_state_map));
394
395     memset(guac_client_data->keymap, 0,
396             sizeof(guac_rdp_static_keymap));
397
398     client->data = guac_client_data;
399     ((rdp_freerdp_context*) rdp_inst->context)->client = client;
400
401     /* Load keymap into client */
402     __guac_rdp_client_load_keymap(client, chosen_keymap);
403
404     /* Set server-side keymap */
405     settings->kbd_layout = chosen_keymap->freerdp_keyboard_layout; 
406
407     /* Connect to RDP server */
408     if (!freerdp_connect(rdp_inst)) {
409
410         guac_protocol_send_error(client->socket,
411                 "Error connecting to RDP server");
412         guac_socket_flush(client->socket);
413
414         guac_error = GUAC_STATUS_BAD_STATE;
415         guac_error_message = "Error connecting to RDP server";
416
417         return 1;
418     }
419
420     /* Send connection name */
421     guac_protocol_send_name(client->socket, settings->window_title);
422
423     /* Send size */
424     guac_protocol_send_size(client->socket, GUAC_DEFAULT_LAYER,
425             settings->width, settings->height);
426
427     /* Create glyph surfaces */
428     guac_client_data->opaque_glyph_surface = cairo_image_surface_create(
429             CAIRO_FORMAT_RGB24, settings->width, settings->height);
430
431     guac_client_data->trans_glyph_surface = cairo_image_surface_create(
432             CAIRO_FORMAT_ARGB32, settings->width, settings->height);
433
434     /* Set default pointer */
435     guac_rdp_pointer_set_default(client);
436
437     /* Success */
438     return 0;
439
440 }
441