33d575a4125e41566354f706fc36a7b600dcfafe
[libguac-client-rdp.git] / src / rdp_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/chanman.h>
46 #include <freerdp/constants/core.h>
47
48 #include <guacamole/socket.h>
49 #include <guacamole/protocol.h>
50 #include <guacamole/client.h>
51
52 #include "rdp_handlers.h"
53 #include "rdp_client.h"
54 #include "rdp_keymap.h"
55
56 /* Client plugin arguments */
57 const char* GUAC_CLIENT_ARGS[] = {
58     "hostname",
59     "port",
60     NULL
61 };
62
63 int rdp_guac_client_free_handler(guac_client* client) {
64
65     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
66
67     /* Disconnect client */
68     guac_client_data->rdp_inst->rdp_disconnect(guac_client_data->rdp_inst);
69
70     /* Free RDP client */
71     freerdp_free(guac_client_data->rdp_inst);
72     freerdp_chanman_free(guac_client_data->chanman);
73     free(guac_client_data->settings);
74
75     /* Free guac client data */
76     free(guac_client_data);
77
78     return 0;
79
80 }
81
82 int rdp_guac_client_handle_messages(guac_client* client) {
83
84     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
85     rdpInst* rdp_inst = guac_client_data->rdp_inst;
86     rdpChanMan* chanman = guac_client_data->chanman;
87
88     int index;
89     int max_fd, fd;
90     void* read_fds[32];
91     void* write_fds[32];
92     int read_count = 0;
93     int write_count = 0;
94
95     fd_set rfds, wfds;
96
97     /* get rdp fds */
98     if (rdp_inst->rdp_get_fds(rdp_inst, read_fds, &read_count, write_fds, &write_count) != 0) {
99         guac_client_log_error(client, "Unable to read RDP file descriptors.");
100         return 1;
101     }
102
103     /* get channel fds */
104     if (freerdp_chanman_get_fds(chanman, rdp_inst, read_fds, &read_count, write_fds, &write_count) != 0) {
105         guac_client_log_error(client, "Unable to read RDP channel file descriptors.");
106         return 1;
107     }
108
109     /* Construct read fd_set */
110     max_fd = 0;
111     FD_ZERO(&rfds);
112     for (index = 0; index < read_count; index++) {
113         fd = (int)(long) (read_fds[index]);
114         if (fd > max_fd)
115             max_fd = fd;
116         FD_SET(fd, &rfds);
117     }
118
119     /* Construct write fd_set */
120     FD_ZERO(&wfds);
121     for (index = 0; index < write_count; index++) {
122         fd = (int)(long) (write_fds[index]);
123         if (fd > max_fd)
124             max_fd = fd;
125         FD_SET(fd, &wfds);
126     }
127
128     /* If no file descriptors, error */
129     if (max_fd == 0) {
130         guac_client_log_error(client, "No file descriptors");
131         return 1;
132     }
133
134     /* Otherwise, wait for file descriptors given */
135     if (select(max_fd + 1, &rfds, &wfds, NULL, NULL) == -1) {
136         /* these are not really errors */
137         if (!((errno == EAGAIN) ||
138             (errno == EWOULDBLOCK) ||
139             (errno == EINPROGRESS) ||
140             (errno == EINTR))) /* signal occurred */
141         {
142             guac_client_log_error(client, "Error waiting for file descriptor.");
143             return 1;
144         }
145     }
146
147     /* check the libfreerdp fds */
148     if (rdp_inst->rdp_check_fds(rdp_inst) != 0) {
149         guac_client_log_error(client, "Error handling RDP file descriptors.");
150         return 1;
151     }
152
153     /* check channel fds */
154     if (freerdp_chanman_check_fds(chanman, rdp_inst) != 0) {
155         guac_client_log_error(client, "Error handling RDP channel file descriptors.");
156         return 1;
157     }
158
159     /* Success */
160     return 0;
161
162 }
163
164 int guac_client_init(guac_client* client, int argc, char** argv) {
165
166     rdp_guac_client_data* guac_client_data;
167
168     rdpInst* rdp_inst;
169     rdpChanMan* chanman;
170         rdpSet* settings;
171
172     char* hostname;
173     int port = RDP_DEFAULT_PORT;
174
175     if (argc < 2) {
176         guac_protocol_send_error(client->socket, "Wrong argument count received.");
177         guac_socket_flush(client->socket);
178         return 1;
179     }
180
181     /* If port specified, use it */
182     if (argv[1][0] != '\0')
183         port = atoi(argv[1]);
184
185     hostname = argv[0];
186
187     /* Allocate client data */
188     guac_client_data = malloc(sizeof(rdp_guac_client_data));
189
190     /* Get channel manager */
191     chanman = freerdp_chanman_new();
192     guac_client_data->chanman = chanman;
193
194     /* INIT SETTINGS */
195     settings = malloc(sizeof(rdpSet));
196         memset(settings, 0, sizeof(rdpSet));
197     guac_client_data->settings = settings;
198
199     /* Set hostname */
200     strncpy(settings->hostname, hostname, sizeof(settings->hostname) - 1);
201
202     /* Default size */
203         settings->width = 1024;
204         settings->height = 768;
205
206         strncpy(settings->server, hostname, sizeof(settings->server));
207         strcpy(settings->username, "guest");
208
209         settings->tcp_port_rdp = port;
210         settings->encryption = 1;
211         settings->server_depth = 16;
212         settings->bitmap_cache = 1;
213         settings->bitmap_compression = 1;
214         settings->desktop_save = 0;
215         settings->performanceflags =
216                 PERF_DISABLE_WALLPAPER
217         | PERF_DISABLE_FULLWINDOWDRAG
218         | PERF_DISABLE_MENUANIMATIONS;
219         settings->mouse_motion = 1;
220         settings->off_screen_bitmaps = 1;
221         settings->triblt = 0;
222         settings->software_gdi = 0;
223         settings->new_cursors = 1;
224         settings->rdp_version = 5;
225         settings->rdp_security = 1;
226
227     /* Init client */
228     rdp_inst = freerdp_new(settings);
229     if (rdp_inst == NULL) {
230         guac_protocol_send_error(client->socket, "Error initializing RDP client");
231         guac_socket_flush(client->socket);
232         return 1;
233     }
234     guac_client_data->rdp_inst = rdp_inst;
235     guac_client_data->mouse_button_mask = 0;
236     guac_client_data->current_surface = GUAC_DEFAULT_LAYER;
237
238
239     /* Store client data */
240     rdp_inst->param1 = client;
241     client->data = guac_client_data;
242
243
244     /* RDP handlers */
245     rdp_inst->ui_error = guac_rdp_ui_error;
246         rdp_inst->ui_warning = guac_rdp_ui_warning;
247         rdp_inst->ui_unimpl = guac_rdp_ui_unimpl;
248         rdp_inst->ui_begin_update = guac_rdp_ui_begin_update;
249         rdp_inst->ui_end_update = guac_rdp_ui_end_update;
250         rdp_inst->ui_desktop_save = guac_rdp_ui_desktop_save;
251         rdp_inst->ui_desktop_restore = guac_rdp_ui_desktop_restore;
252         rdp_inst->ui_create_bitmap = guac_rdp_ui_create_bitmap;
253         rdp_inst->ui_paint_bitmap = guac_rdp_ui_paint_bitmap;
254         rdp_inst->ui_destroy_bitmap = guac_rdp_ui_destroy_bitmap;
255         rdp_inst->ui_line = guac_rdp_ui_line;
256         rdp_inst->ui_rect = guac_rdp_ui_rect;
257         rdp_inst->ui_polygon = guac_rdp_ui_polygon;
258         rdp_inst->ui_polyline = guac_rdp_ui_polyline;
259         rdp_inst->ui_ellipse = guac_rdp_ui_ellipse;
260         rdp_inst->ui_start_draw_glyphs = guac_rdp_ui_start_draw_glyphs;
261         rdp_inst->ui_draw_glyph = guac_rdp_ui_draw_glyph;
262         rdp_inst->ui_end_draw_glyphs = guac_rdp_ui_end_draw_glyphs;
263         rdp_inst->ui_get_toggle_keys_state = guac_rdp_ui_get_toggle_keys_state;
264         rdp_inst->ui_bell = guac_rdp_ui_bell;
265         rdp_inst->ui_destblt = guac_rdp_ui_destblt;
266         rdp_inst->ui_patblt = guac_rdp_ui_patblt;
267         rdp_inst->ui_screenblt = guac_rdp_ui_screenblt;
268         rdp_inst->ui_memblt = guac_rdp_ui_memblt;
269         rdp_inst->ui_triblt = guac_rdp_ui_triblt;
270         rdp_inst->ui_create_glyph = guac_rdp_ui_create_glyph;
271         rdp_inst->ui_destroy_glyph = guac_rdp_ui_destroy_glyph;
272     rdp_inst->ui_select = guac_rdp_ui_select;
273         rdp_inst->ui_set_clip = guac_rdp_ui_set_clip;
274         rdp_inst->ui_reset_clip = guac_rdp_ui_reset_clip;
275         rdp_inst->ui_resize_window = guac_rdp_ui_resize_window;
276         rdp_inst->ui_set_cursor = guac_rdp_ui_set_cursor;
277         rdp_inst->ui_destroy_cursor = guac_rdp_ui_destroy_cursor;
278         rdp_inst->ui_create_cursor = guac_rdp_ui_create_cursor;
279         rdp_inst->ui_set_null_cursor = guac_rdp_ui_set_null_cursor;
280         rdp_inst->ui_set_default_cursor = guac_rdp_ui_set_default_cursor;
281         rdp_inst->ui_create_palette = guac_rdp_ui_create_palette;
282         rdp_inst->ui_move_pointer = guac_rdp_ui_move_pointer;
283         rdp_inst->ui_set_palette = guac_rdp_ui_set_palette;
284         rdp_inst->ui_create_surface = guac_rdp_ui_create_surface;
285         rdp_inst->ui_set_surface = guac_rdp_ui_set_surface;
286         rdp_inst->ui_destroy_surface = guac_rdp_ui_destroy_surface;
287         rdp_inst->ui_channel_data = guac_rdp_ui_channel_data;
288
289     /* Init chanman (pre-connect) */
290     if (freerdp_chanman_pre_connect(chanman, rdp_inst)) {
291         guac_protocol_send_error(client->socket, "Error initializing RDP client channel manager");
292         guac_socket_flush(client->socket);
293         return 1;
294     }
295
296     /* Connect to RDP server */
297     if (rdp_inst->rdp_connect(rdp_inst)) {
298         guac_protocol_send_error(client->socket, "Error connecting to RDP server");
299         guac_socket_flush(client->socket);
300         return 1;
301     }
302
303     /* Init chanman (post-connect) */
304     if (freerdp_chanman_post_connect(chanman, rdp_inst)) {
305         guac_protocol_send_error(client->socket, "Error initializing RDP client channel manager");
306         guac_socket_flush(client->socket);
307         return 1;
308     }
309
310     /* Client handlers */
311     client->free_handler = rdp_guac_client_free_handler;
312     client->handle_messages = rdp_guac_client_handle_messages;
313     client->mouse_handler = rdp_guac_client_mouse_handler;
314     client->key_handler = rdp_guac_client_key_handler;
315
316     /* Success */
317     return 0;
318
319 }
320
321 int rdp_guac_client_mouse_handler(guac_client* client, int x, int y, int mask) {
322
323     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
324     rdpInst* rdp_inst = guac_client_data->rdp_inst;
325
326     /* If button mask unchanged, just send move event */
327     if (mask == guac_client_data->mouse_button_mask)
328         rdp_inst->rdp_send_input_mouse(rdp_inst, PTRFLAGS_MOVE, x, y);
329
330     /* Otherwise, send events describing button change */
331     else {
332
333         /* Mouse buttons which have JUST become released */
334         int released_mask =  guac_client_data->mouse_button_mask & ~mask;
335
336         /* Mouse buttons which have JUST become pressed */
337         int pressed_mask  = ~guac_client_data->mouse_button_mask &  mask;
338
339         /* Release event */
340         if (released_mask & 0x07) {
341
342             /* Calculate flags */
343             int flags = 0;
344             if (released_mask & 0x01) flags |= PTRFLAGS_BUTTON1;
345             if (released_mask & 0x02) flags |= PTRFLAGS_BUTTON3;
346             if (released_mask & 0x04) flags |= PTRFLAGS_BUTTON2;
347
348             rdp_inst->rdp_send_input_mouse(rdp_inst, flags, x, y);
349
350         }
351
352         /* Press event */
353         if (pressed_mask & 0x07) {
354
355             /* Calculate flags */
356             int flags = PTRFLAGS_DOWN;
357             if (pressed_mask & 0x01) flags |= PTRFLAGS_BUTTON1;
358             if (pressed_mask & 0x02) flags |= PTRFLAGS_BUTTON3;
359             if (pressed_mask & 0x04) flags |= PTRFLAGS_BUTTON2;
360             if (pressed_mask & 0x08) flags |= PTRFLAGS_WHEEL | 0x78;
361             if (pressed_mask & 0x10) flags |= PTRFLAGS_WHEEL | PTRFLAGS_WHEEL_NEGATIVE | 0x88;
362
363             /* Send event */
364             rdp_inst->rdp_send_input_mouse(rdp_inst, flags, x, y);
365
366         }
367
368         /* Scroll event */
369         if (pressed_mask & 0x18) {
370
371             /* Down */
372             if (pressed_mask & 0x08)
373                 rdp_inst->rdp_send_input_mouse(
374                         rdp_inst,
375                         PTRFLAGS_WHEEL | 0x78,
376                         x, y);
377
378             /* Up */
379             if (pressed_mask & 0x10)
380                 rdp_inst->rdp_send_input_mouse(
381                         rdp_inst,
382                         PTRFLAGS_WHEEL | PTRFLAGS_WHEEL_NEGATIVE | 0x88,
383                         x, y);
384
385         }
386
387
388         guac_client_data->mouse_button_mask = mask;
389     }
390
391     return 0;
392 }
393
394 int rdp_guac_client_key_handler(guac_client* client, int keysym, int pressed) {
395
396     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
397     rdpInst* rdp_inst = guac_client_data->rdp_inst;
398
399     /* If keysym can be in lookup table */
400     if (keysym <= 0xFFFF) {
401
402         /* Look up scancode */
403         const guac_rdp_keymap* keymap = 
404             &guac_rdp_keysym_scancode[(keysym & 0xFF00) >> 8][keysym & 0xFF];
405
406         /* If defined, send event */
407         if (keymap->scancode != 0)
408             rdp_inst->rdp_send_input_scancode(
409                     rdp_inst,
410                     !pressed,
411                     keymap->flags & KBDFLAGS_EXTENDED,
412                     keymap->scancode);
413         else
414             guac_client_log_info(client, "unmapped keysym: 0x%x", keysym);
415
416     }
417
418     return 0;
419 }
420