2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
15 * The Original Code is libguac-client-rdp.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2011
20 * the Initial Developer. All Rights Reserved.
24 * Jocelyn DELALANDE <j.delalande@ulteo.com> Ulteo SAS - http://www.ulteo.com
26 * Portions created by Ulteo SAS employees are Copyright (C) 2012 Ulteo SAS
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.
40 * ***** END LICENSE BLOCK ***** */
45 #include <sys/select.h>
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>
56 #include <guacamole/socket.h>
57 #include <guacamole/protocol.h>
58 #include <guacamole/client.h>
59 #include <guacamole/error.h>
62 #include "rdp_keymap.h"
63 #include "rdp_cliprdr.h"
64 #include "guac_handlers.h"
65 #include "unicode_convtable.h"
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);
72 int rdp_guac_client_free_handler(guac_client* client) {
74 rdp_guac_client_data* guac_client_data =
75 (rdp_guac_client_data*) client->data;
77 freerdp* rdp_inst = guac_client_data->rdp_inst;
78 rdpChannels* channels = rdp_inst->context->channels;
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);
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);
98 int rdp_guac_client_handle_messages(guac_client* client) {
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;
113 struct timeval timeout = {
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";
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";
132 /* Construct read fd_set */
135 for (index = 0; index < read_count; index++) {
136 fd = (int)(long) (read_fds[index]);
142 /* Construct write fd_set */
144 for (index = 0; index < write_count; index++) {
145 fd = (int)(long) (write_fds[index]);
151 /* If no file descriptors, error */
153 guac_error = GUAC_STATUS_BAD_STATE;
154 guac_error_message = "No file descriptors";
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 */
166 guac_error = GUAC_STATUS_SEE_ERRNO;
167 guac_error_message = "Error waiting for file descriptor";
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";
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";
186 /* Check for channel events */
187 event = freerdp_channels_pop_event(channels);
190 /* Handle clipboard events */
191 if (event->event_class == RDP_EVENT_CLASS_CLIPRDR)
192 guac_rdp_process_cliprdr_event(client, event);
194 freerdp_event_free(event);
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";
210 int rdp_guac_client_mouse_handler(guac_client* client, int x, int y, int mask) {
212 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
213 freerdp* rdp_inst = guac_client_data->rdp_inst;
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);
219 /* Otherwise, send events describing button change */
222 /* Mouse buttons which have JUST become released */
223 int released_mask = guac_client_data->mouse_button_mask & ~mask;
225 /* Mouse buttons which have JUST become pressed */
226 int pressed_mask = ~guac_client_data->mouse_button_mask & mask;
229 if (released_mask & 0x07) {
231 /* Calculate flags */
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;
237 rdp_inst->input->MouseEvent(rdp_inst->input, flags, x, y);
242 if (pressed_mask & 0x07) {
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;
253 rdp_inst->input->MouseEvent(rdp_inst->input, flags, x, y);
258 if (pressed_mask & 0x18) {
261 if (pressed_mask & 0x08)
262 rdp_inst->input->MouseEvent(
264 PTR_FLAGS_WHEEL | 0x78,
268 if (pressed_mask & 0x10)
269 rdp_inst->input->MouseEvent(
271 PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | 0x88,
277 guac_client_data->mouse_button_mask = mask;
283 void __guac_rdp_send_altcode(guac_client* client, int altcode) {
285 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
286 freerdp* rdp_inst = guac_client_data->rdp_inst;
289 /* Lookup scancode for Alt */
290 int alt = GUAC_RDP_KEYSYM_LOOKUP(
291 guac_client_data->keymap,
292 0xFFE9 /* Alt_L */).scancode;
294 /* Release all pressed modifiers */
295 __guac_rdp_update_keysyms(client, GUAC_KEYSYMS_ALL_MODIFIERS, 1, 0);
298 rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_DOWN, alt);
300 /* For each character in four-digit Alt-code ... */
301 for (i=0; i<4; i++) {
303 /* Get scancode of keypad digit */
304 int scancode = GUAC_RDP_KEYSYM_LOOKUP(
305 guac_client_data->keymap,
306 0xFFB0 + (altcode / 1000)
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);
313 /* Shift digits left by one place */
314 altcode = (altcode * 10) % 10000;
319 rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_RELEASE, alt);
321 /* Press all originally pressed modifiers */
322 __guac_rdp_update_keysyms(client, GUAC_KEYSYMS_ALL_MODIFIERS, 1, 1);
326 int __guac_rdp_send_keysym(guac_client* client, int keysym, int pressed) {
328 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
329 freerdp* rdp_inst = guac_client_data->rdp_inst;
331 /* If keysym can be in lookup table */
332 //if (keysym <= 0xFFFF) {
334 /* Look up scancode mapping */
335 const guac_rdp_keysym_desc* keysym_desc =
336 &GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keymap, keysym);
338 /* If defined, send event */
339 if (keysym_desc->scancode != 0) {
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);
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);
349 /* Send actual key */
350 rdp_inst->input->KeyboardEvent(
353 | (pressed ? KBD_FLAGS_DOWN : KBD_FLAGS_RELEASE),
354 keysym_desc->scancode);
356 guac_client_log_info(client, "Base flags are %d", keysym_desc->flags);
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);
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);
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);
373 /* LibfreeRDP seems not to take into account the DOWN/RELEASE flags.
374 * So we send only on of the two key events.
377 rdp_inst->input->UnicodeKeyboardEvent(
379 //pressed ? KBD_FLAGS_DOW : KBD_FLAGS_RELEASE, <- not
380 // taken into account
385 guac_client_log_info(client, "Ignoring release");
393 void __guac_rdp_update_keysyms(guac_client* client, const int* keysym_string, int from, int to) {
395 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
398 /* Send all keysyms in string, NULL terminated */
399 while ((keysym = *keysym_string) != 0) {
401 /* Get current keysym state */
402 int current_state = GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keysym_state, keysym);
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);
415 int rdp_guac_client_key_handler(guac_client* client, int keysym, int pressed) {
417 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
419 /* Update keysym state */
420 GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keysym_state, keysym) = pressed;
422 return __guac_rdp_send_keysym(client, keysym, pressed);
426 int rdp_guac_client_clipboard_handler(guac_client* client, char* data) {
428 rdpChannels* channels =
429 ((rdp_guac_client_data*) client->data)->rdp_inst->context->channels;
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,
437 /* Free existing data */
438 free(((rdp_guac_client_data*) client->data)->clipboard);
440 /* Store data in client */
441 ((rdp_guac_client_data*) client->data)->clipboard = strdup(data);
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;
448 freerdp_channels_send_event(channels, (RDP_EVENT*) format_list);