2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
15 * The Original Code is libguac-client-rdp.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2011
20 * the Initial Developer. All Rights Reserved.
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.
37 * ***** END LICENSE BLOCK ***** */
42 #include <sys/select.h>
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>
57 #include <guacamole/socket.h>
58 #include <guacamole/protocol.h>
59 #include <guacamole/client.h>
62 #include "guac_handlers.h"
63 #include "rdp_keymap.h"
64 #include "rdp_bitmap.h"
65 #include "rdp_glyph.h"
66 #include "rdp_pointer.h"
69 /* Client plugin arguments */
70 const char* GUAC_CLIENT_ARGS[] = {
93 boolean rdp_freerdp_pre_connect(freerdp* instance) {
95 rdpContext* context = instance->context;
96 guac_client* client = ((rdp_freerdp_context*) context)->client;
97 rdpChannels* channels = context->channels;
101 rdpPrimaryUpdate* primary;
104 /* Init color conversion structure */
105 clrconv = xnew(CLRCONV);
109 clrconv->palette = xnew(rdpPalette);
110 ((rdp_freerdp_context*) context)->clrconv = clrconv;
112 /* Init FreeRDP cache */
113 instance->context->cache = cache_new(instance->settings);
115 /* Set up bitmap handling */
116 bitmap = xnew(rdpBitmap);
117 bitmap->size = sizeof(guac_rdp_bitmap);
118 bitmap->New = guac_rdp_bitmap_new;
119 bitmap->Free = guac_rdp_bitmap_free;
120 bitmap->Paint = guac_rdp_bitmap_paint;
121 bitmap->Decompress = guac_rdp_bitmap_decompress;
122 bitmap->SetSurface = guac_rdp_bitmap_setsurface;
123 graphics_register_bitmap(context->graphics, bitmap);
125 /* Set up glyph handling */
126 glyph = xnew(rdpGlyph);
127 glyph->size = sizeof(guac_rdp_glyph);
128 glyph->New = guac_rdp_glyph_new;
129 glyph->Free = guac_rdp_glyph_free;
130 glyph->Draw = guac_rdp_glyph_draw;
131 glyph->BeginDraw = guac_rdp_glyph_begindraw;
132 glyph->EndDraw = guac_rdp_glyph_enddraw;
133 graphics_register_glyph(context->graphics, glyph);
135 /* Set up pointer handling */
136 pointer = xnew(rdpPointer);
137 pointer->size = sizeof(guac_rdp_pointer);
138 pointer->New = guac_rdp_pointer_new;
139 pointer->Free = guac_rdp_pointer_free;
140 pointer->Set = guac_rdp_pointer_set;
141 graphics_register_pointer(context->graphics, pointer);
144 instance->update->Palette = guac_rdp_gdi_palette_update;
145 instance->update->SetBounds = guac_rdp_gdi_set_bounds;
147 primary = instance->update->primary;
148 primary->DstBlt = guac_rdp_gdi_dstblt;
149 primary->PatBlt = guac_rdp_gdi_patblt;
150 primary->ScrBlt = guac_rdp_gdi_scrblt;
151 primary->MemBlt = guac_rdp_gdi_memblt;
152 primary->OpaqueRect = guac_rdp_gdi_opaquerect;
154 pointer_cache_register_callbacks(instance->update);
155 glyph_cache_register_callbacks(instance->update);
156 brush_cache_register_callbacks(instance->update);
157 bitmap_cache_register_callbacks(instance->update);
158 offscreen_cache_register_callbacks(instance->update);
159 palette_cache_register_callbacks(instance->update);
161 /* Init channels (pre-connect) */
162 if (freerdp_channels_pre_connect(channels, instance)) {
163 guac_protocol_send_error(client->socket, "Error initializing RDP client channel manager");
164 guac_socket_flush(client->socket);
172 boolean rdp_freerdp_post_connect(freerdp* instance) {
174 rdpContext* context = instance->context;
175 guac_client* client = ((rdp_freerdp_context*) context)->client;
176 rdpChannels* channels = instance->context->channels;
178 /* Init channels (post-connect) */
179 if (freerdp_channels_post_connect(channels, instance)) {
180 guac_protocol_send_error(client->socket, "Error initializing RDP client channel manager");
181 guac_socket_flush(client->socket);
185 /* Client handlers */
186 client->free_handler = rdp_guac_client_free_handler;
187 client->handle_messages = rdp_guac_client_handle_messages;
188 client->mouse_handler = rdp_guac_client_mouse_handler;
189 client->key_handler = rdp_guac_client_key_handler;
192 guac_protocol_send_size(client->socket, GUAC_DEFAULT_LAYER,
193 instance->settings->width, instance->settings->height);
199 void rdp_freerdp_context_new(freerdp* instance, rdpContext* context) {
200 context->channels = freerdp_channels_new();
203 void rdp_freerdp_context_free(freerdp* instance, rdpContext* context) {
207 int guac_client_init(guac_client* client, int argc, char** argv) {
209 rdp_guac_client_data* guac_client_data;
212 rdpSettings* settings;
215 int port = RDP_DEFAULT_PORT;
216 boolean bitmap_cache;
219 guac_protocol_send_error(client->socket, "Wrong argument count received.");
220 guac_socket_flush(client->socket);
224 /* If port specified, use it */
225 if (argv[IDX_PORT][0] != '\0')
226 port = atoi(argv[IDX_PORT]);
228 hostname = argv[IDX_HOSTNAME];
230 /* Allocate client data */
231 guac_client_data = malloc(sizeof(rdp_guac_client_data));
234 freerdp_channels_global_init();
235 rdp_inst = freerdp_new();
236 rdp_inst->PreConnect = rdp_freerdp_pre_connect;
237 rdp_inst->PostConnect = rdp_freerdp_post_connect;
239 /* Allocate FreeRDP context */
240 rdp_inst->context_size = sizeof(rdp_freerdp_context);
241 rdp_inst->ContextNew = (pContextNew) rdp_freerdp_context_new;
242 rdp_inst->ContextFree = (pContextFree) rdp_freerdp_context_free;
243 freerdp_context_new(rdp_inst);
246 settings = rdp_inst->settings;
249 settings->authentication = false;
252 settings->rdp_security = true;
253 settings->tls_security = false;
254 settings->nla_security = false;
255 settings->encryption = true;
256 settings->encryption_method = ENCRYPTION_METHOD_40BIT | ENCRYPTION_METHOD_128BIT | ENCRYPTION_METHOD_FIPS;
257 settings->encryption_level = ENCRYPTION_LEVEL_CLIENT_COMPATIBLE;
260 settings->width = 1024;
261 if (argv[IDX_WIDTH][0] != '\0')
262 settings->width = atoi(argv[IDX_WIDTH]);
263 if (settings->width == 0)
264 settings->width = 1024;
267 settings->height = 768;
268 if (argv[IDX_HEIGHT][0] != '\0')
269 settings->height = atoi(argv[IDX_HEIGHT]);
270 if (settings->height == 0)
271 settings->height = 768;
274 settings->hostname = strdup(hostname);
275 settings->port = port;
276 settings->window_title = strdup(hostname);
279 settings->username = "guest";
280 if (argv[IDX_USERNAME][0] != '\0')
281 settings->username = strdup (argv[IDX_USERNAME]);
284 if (argv[IDX_PASSWORD][0] != '\0') {
285 settings->password = strdup (argv[IDX_PASSWORD]);
286 settings->autologon = 1;
289 /* initial program */
290 if (argv[IDX_INITIAL_PROGRAM][0] != '\0')
291 settings->shell = strdup (argv[IDX_INITIAL_PROGRAM]);
294 bitmap_cache = settings->bitmap_cache;
295 settings->os_major_type = OSMAJORTYPE_UNSPECIFIED;
296 settings->os_minor_type = OSMINORTYPE_UNSPECIFIED;
297 settings->order_support[NEG_DSTBLT_INDEX] = true;
298 settings->order_support[NEG_PATBLT_INDEX] = false; /* PATBLT not yet supported */
299 settings->order_support[NEG_SCRBLT_INDEX] = true;
300 settings->order_support[NEG_OPAQUE_RECT_INDEX] = true;
301 settings->order_support[NEG_DRAWNINEGRID_INDEX] = false;
302 settings->order_support[NEG_MULTIDSTBLT_INDEX] = false;
303 settings->order_support[NEG_MULTIPATBLT_INDEX] = false;
304 settings->order_support[NEG_MULTISCRBLT_INDEX] = false;
305 settings->order_support[NEG_MULTIOPAQUERECT_INDEX] = false;
306 settings->order_support[NEG_MULTI_DRAWNINEGRID_INDEX] = false;
307 settings->order_support[NEG_LINETO_INDEX] = false;
308 settings->order_support[NEG_POLYLINE_INDEX] = false;
309 settings->order_support[NEG_MEMBLT_INDEX] = bitmap_cache;
310 settings->order_support[NEG_MEM3BLT_INDEX] = false;
311 settings->order_support[NEG_MEMBLT_V2_INDEX] = bitmap_cache;
312 settings->order_support[NEG_MEM3BLT_V2_INDEX] = false;
313 settings->order_support[NEG_SAVEBITMAP_INDEX] = false;
314 settings->order_support[NEG_GLYPH_INDEX_INDEX] = true;
315 settings->order_support[NEG_FAST_INDEX_INDEX] = true;
316 settings->order_support[NEG_FAST_GLYPH_INDEX] = true;
317 settings->order_support[NEG_POLYGON_SC_INDEX] = false;
318 settings->order_support[NEG_POLYGON_CB_INDEX] = false;
319 settings->order_support[NEG_ELLIPSE_SC_INDEX] = false;
320 settings->order_support[NEG_ELLIPSE_CB_INDEX] = false;
322 /* Store client data */
323 guac_client_data->rdp_inst = rdp_inst;
324 guac_client_data->mouse_button_mask = 0;
325 guac_client_data->current_surface = GUAC_DEFAULT_LAYER;
326 guac_client_data->keysym_scancodes = &guac_rdp_keysym_scancode_en_us;
328 /* Clear keysym state mapping */
329 memset(guac_client_data->keysym_state, 0, sizeof(guac_rdp_keysym_state_map));
331 ((rdp_freerdp_context*) rdp_inst->context)->client = client;
332 client->data = guac_client_data;
334 /* Connect to RDP server */
335 if (!freerdp_connect(rdp_inst)) {
336 guac_protocol_send_error(client->socket, "Error connecting to RDP server");
337 guac_socket_flush(client->socket);