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