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