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