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