Fix leaks - free clipboard and glyph surfaces.
[libguac-client-rdp.git] / src / guac_handlers.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/channels/channels.h>
47 #include <freerdp/input.h>
48 #include <freerdp/codec/color.h>
49 #include <freerdp/cache/cache.h>
50 #include <freerdp/utils/event.h>
51 #include <freerdp/plugins/cliprdr.h>
52
53 #include <guacamole/socket.h>
54 #include <guacamole/protocol.h>
55 #include <guacamole/client.h>
56 #include <guacamole/error.h>
57
58 #include "client.h"
59 #include "rdp_keymap.h"
60 #include "rdp_cliprdr.h"
61 #include "guac_handlers.h"
62
63 void __guac_rdp_update_keysyms(guac_client* client, const int* keysym_string, int from, int to);
64 int __guac_rdp_send_keysym(guac_client* client, int keysym, int pressed);
65 void __guac_rdp_send_altcode(guac_client* client, int altcode);
66
67
68 int rdp_guac_client_free_handler(guac_client* client) {
69
70     rdp_guac_client_data* guac_client_data =
71         (rdp_guac_client_data*) client->data;
72
73     freerdp* rdp_inst = guac_client_data->rdp_inst;
74     rdpChannels* channels = rdp_inst->context->channels;
75
76     /* Clean up RDP client */
77         freerdp_channels_close(channels, rdp_inst);
78         freerdp_channels_free(channels);
79         freerdp_disconnect(rdp_inst);
80     freerdp_clrconv_free(((rdp_freerdp_context*) rdp_inst->context)->clrconv);
81     cache_free(rdp_inst->context->cache);
82     freerdp_free(rdp_inst);
83
84     /* Free client data */
85     cairo_surface_destroy(guac_client_data->opaque_glyph_surface);
86     cairo_surface_destroy(guac_client_data->trans_glyph_surface);
87     free(guac_client_data->clipboard);
88     free(guac_client_data);
89
90     return 0;
91
92 }
93
94 int rdp_guac_client_handle_messages(guac_client* client) {
95
96     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
97     freerdp* rdp_inst = guac_client_data->rdp_inst;
98     rdpChannels* channels = rdp_inst->context->channels;
99
100     int index;
101     int max_fd, fd;
102     void* read_fds[32];
103     void* write_fds[32];
104     int read_count = 0;
105     int write_count = 0;
106     fd_set rfds, wfds;
107     RDP_EVENT* event;
108
109     struct timeval timeout = {
110         .tv_sec = 0,
111         .tv_usec = 250000
112     };
113
114     /* get rdp fds */
115     if (!freerdp_get_fds(rdp_inst, read_fds, &read_count, write_fds, &write_count)) {
116         guac_error = GUAC_STATUS_BAD_STATE;
117         guac_error_message = "Unable to read RDP file descriptors";
118         return 1;
119     }
120
121     /* get channel fds */
122     if (!freerdp_channels_get_fds(channels, rdp_inst, read_fds, &read_count, write_fds, &write_count)) {
123         guac_error = GUAC_STATUS_BAD_STATE;
124         guac_error_message = "Unable to read RDP channel file descriptors";
125         return 1;
126     }
127
128     /* Construct read fd_set */
129     max_fd = 0;
130     FD_ZERO(&rfds);
131     for (index = 0; index < read_count; index++) {
132         fd = (int)(long) (read_fds[index]);
133         if (fd > max_fd)
134             max_fd = fd;
135         FD_SET(fd, &rfds);
136     }
137
138     /* Construct write fd_set */
139     FD_ZERO(&wfds);
140     for (index = 0; index < write_count; index++) {
141         fd = (int)(long) (write_fds[index]);
142         if (fd > max_fd)
143             max_fd = fd;
144         FD_SET(fd, &wfds);
145     }
146
147     /* If no file descriptors, error */
148     if (max_fd == 0) {
149         guac_error = GUAC_STATUS_BAD_STATE;
150         guac_error_message = "No file descriptors";
151         return 1;
152     }
153
154     /* Otherwise, wait for file descriptors given */
155     if (select(max_fd + 1, &rfds, &wfds, NULL, &timeout) == -1) {
156         /* these are not really errors */
157         if (!((errno == EAGAIN) ||
158             (errno == EWOULDBLOCK) ||
159             (errno == EINPROGRESS) ||
160             (errno == EINTR))) /* signal occurred */
161         {
162             guac_error = GUAC_STATUS_SEE_ERRNO;
163             guac_error_message = "Error waiting for file descriptor";
164             return 1;
165         }
166     }
167
168     /* Check the libfreerdp fds */
169     if (!freerdp_check_fds(rdp_inst)) {
170         guac_error = GUAC_STATUS_BAD_STATE;
171         guac_error_message = "Error handling RDP file descriptors";
172         return 1;
173     }
174
175     /* Check channel fds */
176     if (!freerdp_channels_check_fds(channels, rdp_inst)) {
177         guac_error = GUAC_STATUS_BAD_STATE;
178         guac_error_message = "Error handling RDP channel file descriptors";
179         return 1;
180     }
181
182     /* Check for channel events */
183     event = freerdp_channels_pop_event(channels);
184     if (event) {
185
186         /* Handle clipboard events */
187         if (event->event_class == RDP_EVENT_CLASS_CLIPRDR)
188             guac_rdp_process_cliprdr_event(client, event);
189
190         freerdp_event_free(event);
191
192     }
193
194     /* Handle RDP disconnect */
195     if (freerdp_shall_disconnect(rdp_inst)) {
196         guac_error = GUAC_STATUS_NO_INPUT;
197         guac_error_message = "RDP server closed connection";
198         return 1;
199     }
200
201     /* Success */
202     return 0;
203
204 }
205
206 int rdp_guac_client_mouse_handler(guac_client* client, int x, int y, int mask) {
207
208     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
209     freerdp* rdp_inst = guac_client_data->rdp_inst;
210
211     /* If button mask unchanged, just send move event */
212     if (mask == guac_client_data->mouse_button_mask)
213         rdp_inst->input->MouseEvent(rdp_inst->input, PTR_FLAGS_MOVE, x, y);
214
215     /* Otherwise, send events describing button change */
216     else {
217
218         /* Mouse buttons which have JUST become released */
219         int released_mask =  guac_client_data->mouse_button_mask & ~mask;
220
221         /* Mouse buttons which have JUST become pressed */
222         int pressed_mask  = ~guac_client_data->mouse_button_mask &  mask;
223
224         /* Release event */
225         if (released_mask & 0x07) {
226
227             /* Calculate flags */
228             int flags = 0;
229             if (released_mask & 0x01) flags |= PTR_FLAGS_BUTTON1;
230             if (released_mask & 0x02) flags |= PTR_FLAGS_BUTTON3;
231             if (released_mask & 0x04) flags |= PTR_FLAGS_BUTTON2;
232
233             rdp_inst->input->MouseEvent(rdp_inst->input, flags, x, y);
234
235         }
236
237         /* Press event */
238         if (pressed_mask & 0x07) {
239
240             /* Calculate flags */
241             int flags = PTR_FLAGS_DOWN;
242             if (pressed_mask & 0x01) flags |= PTR_FLAGS_BUTTON1;
243             if (pressed_mask & 0x02) flags |= PTR_FLAGS_BUTTON3;
244             if (pressed_mask & 0x04) flags |= PTR_FLAGS_BUTTON2;
245             if (pressed_mask & 0x08) flags |= PTR_FLAGS_WHEEL | 0x78;
246             if (pressed_mask & 0x10) flags |= PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | 0x88;
247
248             /* Send event */
249             rdp_inst->input->MouseEvent(rdp_inst->input, flags, x, y);
250
251         }
252
253         /* Scroll event */
254         if (pressed_mask & 0x18) {
255
256             /* Down */
257             if (pressed_mask & 0x08)
258                 rdp_inst->input->MouseEvent(
259                         rdp_inst->input,
260                         PTR_FLAGS_WHEEL | 0x78,
261                         x, y);
262
263             /* Up */
264             if (pressed_mask & 0x10)
265                 rdp_inst->input->MouseEvent(
266                         rdp_inst->input,
267                         PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | 0x88,
268                         x, y);
269
270         }
271
272
273         guac_client_data->mouse_button_mask = mask;
274     }
275
276     return 0;
277 }
278
279 void __guac_rdp_send_altcode(guac_client* client, int altcode) {
280
281     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
282     freerdp* rdp_inst = guac_client_data->rdp_inst;
283     int i;
284
285     /* Lookup scancode for Alt */
286     int alt = GUAC_RDP_KEYSYM_LOOKUP(
287             guac_client_data->keymap,
288             0xFFE9 /* Alt_L */).scancode;
289
290     /* Release all pressed modifiers */
291     __guac_rdp_update_keysyms(client, GUAC_KEYSYMS_ALL_MODIFIERS, 1, 0);
292
293     /* Press Alt */
294     rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_DOWN, alt);
295
296     /* For each character in four-digit Alt-code ... */
297     for (i=0; i<4; i++) {
298
299         /* Get scancode of keypad digit */
300         int scancode = GUAC_RDP_KEYSYM_LOOKUP(
301                 guac_client_data->keymap,
302                 0xFFB0 + (altcode / 1000)
303         ).scancode;
304
305         /* Press and release digit */
306         rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_DOWN, scancode);
307         rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_RELEASE, scancode);
308
309         /* Shift digits left by one place */
310         altcode = (altcode * 10) % 10000;
311
312     }
313
314     /* Release Alt */
315     rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_RELEASE, alt);
316
317     /* Press all originally pressed modifiers */
318     __guac_rdp_update_keysyms(client, GUAC_KEYSYMS_ALL_MODIFIERS, 1, 1);
319
320 }
321
322 int __guac_rdp_send_keysym(guac_client* client, int keysym, int pressed) {
323
324     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
325     freerdp* rdp_inst = guac_client_data->rdp_inst;
326
327     /* If keysym can be in lookup table */
328     if (keysym <= 0xFFFF) {
329
330         /* Look up scancode mapping */
331         const guac_rdp_keysym_desc* keysym_desc =
332             &GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keymap, keysym);
333
334         /* If defined, send event */
335         if (keysym_desc->scancode != 0) {
336
337             /* If defined, send any prerequesite keys that must be set */
338             if (keysym_desc->set_keysyms != NULL)
339                 __guac_rdp_update_keysyms(client, keysym_desc->set_keysyms, 0, 1);
340
341             /* If defined, release any keys that must be cleared */
342             if (keysym_desc->clear_keysyms != NULL)
343                 __guac_rdp_update_keysyms(client, keysym_desc->clear_keysyms, 1, 0);
344
345             /* Send actual key */
346             rdp_inst->input->KeyboardEvent(
347                     rdp_inst->input,
348                     keysym_desc->flags
349                         | (pressed ? KBD_FLAGS_DOWN : KBD_FLAGS_RELEASE),
350                     keysym_desc->scancode);
351
352             /* If defined, release any keys that were originally released */
353             if (keysym_desc->set_keysyms != NULL)
354                 __guac_rdp_update_keysyms(client, keysym_desc->set_keysyms, 0, 0);
355
356             /* If defined, send any keys that were originally set */
357             if (keysym_desc->clear_keysyms != NULL)
358                 __guac_rdp_update_keysyms(client, keysym_desc->clear_keysyms, 1, 1);
359
360         }
361
362         /* If undefined but has Alt-code, use Alt-Code */
363         else if (keysym <= 0xFF) {
364
365             /* NOTE: The Alt-codes are conveniently identical to keysyms. */
366
367             /* Only send Alt-code on press */
368             if (pressed)
369                 __guac_rdp_send_altcode(client, keysym);
370
371         }
372
373         /* If no defined Alt-code, log warning */
374         else
375             guac_client_log_info(client, "unmapped keysym: 0x%x", keysym);
376
377     }
378
379     return 0;
380 }
381
382 void __guac_rdp_update_keysyms(guac_client* client, const int* keysym_string, int from, int to) {
383
384     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
385     int keysym;
386
387     /* Send all keysyms in string, NULL terminated */
388     while ((keysym = *keysym_string) != 0) {
389
390         /* Get current keysym state */
391         int current_state = GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keysym_state, keysym);
392
393         /* If key is currently in given state, send event for changing it to specified "to" state */
394         if (current_state == from)
395             __guac_rdp_send_keysym(client, *keysym_string, to);
396
397         /* Next keysym */
398         keysym_string++;
399
400     }
401
402 }
403
404 int rdp_guac_client_key_handler(guac_client* client, int keysym, int pressed) {
405
406     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
407
408     /* Update keysym state */
409     GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keysym_state, keysym) = pressed;
410
411     return __guac_rdp_send_keysym(client, keysym, pressed);
412
413 }
414
415 int rdp_guac_client_clipboard_handler(guac_client* client, char* data) {
416
417     rdpChannels* channels = 
418         ((rdp_guac_client_data*) client->data)->rdp_inst->context->channels;
419
420     RDP_CB_FORMAT_LIST_EVENT* format_list =
421         (RDP_CB_FORMAT_LIST_EVENT*) freerdp_event_new(
422             RDP_EVENT_CLASS_CLIPRDR,
423             RDP_EVENT_TYPE_CB_FORMAT_LIST,
424             NULL, NULL);
425
426     /* Free existing data */
427     free(((rdp_guac_client_data*) client->data)->clipboard);
428
429     /* Store data in client */
430     ((rdp_guac_client_data*) client->data)->clipboard = strdup(data);
431
432     /* Notify server that text data is now available */
433     format_list->formats = (uint32*) malloc(sizeof(uint32));
434     format_list->formats[0] = CB_FORMAT_TEXT;
435     format_list->num_formats = 1;
436
437     freerdp_channels_send_event(channels, (RDP_EVENT*) format_list);
438
439     return 0;
440
441 }
442