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