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"
66 void __guac_rdp_update_keysyms(guac_client* client, const int* keysym_string, int from, int to);
67 int __guac_rdp_send_keysym(guac_client* client, int keysym, int pressed);
68 void __guac_rdp_send_altcode(guac_client* client, int altcode);
71 int rdp_guac_client_free_handler(guac_client* client) {
73 rdp_guac_client_data* guac_client_data =
74 (rdp_guac_client_data*) client->data;
76 freerdp* rdp_inst = guac_client_data->rdp_inst;
77 rdpChannels* channels = rdp_inst->context->channels;
79 /* Clean up RDP client */
80 freerdp_channels_close(channels, rdp_inst);
81 freerdp_channels_free(channels);
82 freerdp_disconnect(rdp_inst);
83 freerdp_clrconv_free(((rdp_freerdp_context*) rdp_inst->context)->clrconv);
84 cache_free(rdp_inst->context->cache);
85 freerdp_free(rdp_inst);
87 /* Free client data */
88 cairo_surface_destroy(guac_client_data->opaque_glyph_surface);
89 cairo_surface_destroy(guac_client_data->trans_glyph_surface);
90 free(guac_client_data->clipboard);
91 free(guac_client_data);
97 int rdp_guac_client_handle_messages(guac_client* client) {
99 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
100 freerdp* rdp_inst = guac_client_data->rdp_inst;
101 rdpChannels* channels = rdp_inst->context->channels;
112 struct timeval timeout = {
118 if (!freerdp_get_fds(rdp_inst, read_fds, &read_count, write_fds, &write_count)) {
119 guac_error = GUAC_STATUS_BAD_STATE;
120 guac_error_message = "Unable to read RDP file descriptors";
124 /* get channel fds */
125 if (!freerdp_channels_get_fds(channels, rdp_inst, read_fds, &read_count, write_fds, &write_count)) {
126 guac_error = GUAC_STATUS_BAD_STATE;
127 guac_error_message = "Unable to read RDP channel file descriptors";
131 /* Construct read fd_set */
134 for (index = 0; index < read_count; index++) {
135 fd = (int)(long) (read_fds[index]);
141 /* Construct write fd_set */
143 for (index = 0; index < write_count; index++) {
144 fd = (int)(long) (write_fds[index]);
150 /* If no file descriptors, error */
152 guac_error = GUAC_STATUS_BAD_STATE;
153 guac_error_message = "No file descriptors";
157 /* Otherwise, wait for file descriptors given */
158 if (select(max_fd + 1, &rfds, &wfds, NULL, &timeout) == -1) {
159 /* these are not really errors */
160 if (!((errno == EAGAIN) ||
161 (errno == EWOULDBLOCK) ||
162 (errno == EINPROGRESS) ||
163 (errno == EINTR))) /* signal occurred */
165 guac_error = GUAC_STATUS_SEE_ERRNO;
166 guac_error_message = "Error waiting for file descriptor";
171 /* Check the libfreerdp fds */
172 if (!freerdp_check_fds(rdp_inst)) {
173 guac_error = GUAC_STATUS_BAD_STATE;
174 guac_error_message = "Error handling RDP file descriptors";
178 /* Check channel fds */
179 if (!freerdp_channels_check_fds(channels, rdp_inst)) {
180 guac_error = GUAC_STATUS_BAD_STATE;
181 guac_error_message = "Error handling RDP channel file descriptors";
185 /* Check for channel events */
186 event = freerdp_channels_pop_event(channels);
189 /* Handle clipboard events */
190 if (event->event_class == RDP_EVENT_CLASS_CLIPRDR)
191 guac_rdp_process_cliprdr_event(client, event);
193 freerdp_event_free(event);
197 /* Handle RDP disconnect */
198 if (freerdp_shall_disconnect(rdp_inst)) {
199 guac_error = GUAC_STATUS_NO_INPUT;
200 guac_error_message = "RDP server closed connection";
209 int rdp_guac_client_mouse_handler(guac_client* client, int x, int y, int mask) {
211 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
212 freerdp* rdp_inst = guac_client_data->rdp_inst;
214 /* If button mask unchanged, just send move event */
215 if (mask == guac_client_data->mouse_button_mask)
216 rdp_inst->input->MouseEvent(rdp_inst->input, PTR_FLAGS_MOVE, x, y);
218 /* Otherwise, send events describing button change */
221 /* Mouse buttons which have JUST become released */
222 int released_mask = guac_client_data->mouse_button_mask & ~mask;
224 /* Mouse buttons which have JUST become pressed */
225 int pressed_mask = ~guac_client_data->mouse_button_mask & mask;
228 if (released_mask & 0x07) {
230 /* Calculate flags */
232 if (released_mask & 0x01) flags |= PTR_FLAGS_BUTTON1;
233 if (released_mask & 0x02) flags |= PTR_FLAGS_BUTTON3;
234 if (released_mask & 0x04) flags |= PTR_FLAGS_BUTTON2;
236 rdp_inst->input->MouseEvent(rdp_inst->input, flags, x, y);
241 if (pressed_mask & 0x07) {
243 /* Calculate flags */
244 int flags = PTR_FLAGS_DOWN;
245 if (pressed_mask & 0x01) flags |= PTR_FLAGS_BUTTON1;
246 if (pressed_mask & 0x02) flags |= PTR_FLAGS_BUTTON3;
247 if (pressed_mask & 0x04) flags |= PTR_FLAGS_BUTTON2;
248 if (pressed_mask & 0x08) flags |= PTR_FLAGS_WHEEL | 0x78;
249 if (pressed_mask & 0x10) flags |= PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | 0x88;
252 rdp_inst->input->MouseEvent(rdp_inst->input, flags, x, y);
257 if (pressed_mask & 0x18) {
260 if (pressed_mask & 0x08)
261 rdp_inst->input->MouseEvent(
263 PTR_FLAGS_WHEEL | 0x78,
267 if (pressed_mask & 0x10)
268 rdp_inst->input->MouseEvent(
270 PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | 0x88,
276 guac_client_data->mouse_button_mask = mask;
282 void __guac_rdp_send_altcode(guac_client* client, int altcode) {
284 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
285 freerdp* rdp_inst = guac_client_data->rdp_inst;
288 /* Lookup scancode for Alt */
289 int alt = GUAC_RDP_KEYSYM_LOOKUP(
290 guac_client_data->keymap,
291 0xFFE9 /* Alt_L */).scancode;
293 /* Release all pressed modifiers */
294 __guac_rdp_update_keysyms(client, GUAC_KEYSYMS_ALL_MODIFIERS, 1, 0);
297 rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_DOWN, alt);
299 /* For each character in four-digit Alt-code ... */
300 for (i=0; i<4; i++) {
302 /* Get scancode of keypad digit */
303 int scancode = GUAC_RDP_KEYSYM_LOOKUP(
304 guac_client_data->keymap,
305 0xFFB0 + (altcode / 1000)
308 /* Press and release digit */
309 rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_DOWN, scancode);
310 rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_RELEASE, scancode);
312 /* Shift digits left by one place */
313 altcode = (altcode * 10) % 10000;
318 rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_RELEASE, alt);
320 /* Press all originally pressed modifiers */
321 __guac_rdp_update_keysyms(client, GUAC_KEYSYMS_ALL_MODIFIERS, 1, 1);
325 int __guac_rdp_send_keysym(guac_client* client, int keysym, int pressed) {
327 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
328 freerdp* rdp_inst = guac_client_data->rdp_inst;
330 /* If keysym can be in lookup table */
331 if (keysym <= 0xFFFF) {
333 /* Look up scancode mapping */
334 const guac_rdp_keysym_desc* keysym_desc =
335 &GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keymap, keysym);
337 /* If defined, send event */
338 if (keysym_desc->scancode != 0) {
340 /* If defined, send any prerequesite keys that must be set */
341 if (keysym_desc->set_keysyms != NULL)
342 __guac_rdp_update_keysyms(client, keysym_desc->set_keysyms, 0, 1);
344 /* If defined, release any keys that must be cleared */
345 if (keysym_desc->clear_keysyms != NULL)
346 __guac_rdp_update_keysyms(client, keysym_desc->clear_keysyms, 1, 0);
348 /* Send actual key */
349 rdp_inst->input->KeyboardEvent(
352 | (pressed ? KBD_FLAGS_DOWN : KBD_FLAGS_RELEASE),
353 keysym_desc->scancode);
355 guac_client_log_info(client, "Base flags are %d", keysym_desc->flags);
357 /* If defined, release any keys that were originally released */
358 if (keysym_desc->set_keysyms != NULL)
359 __guac_rdp_update_keysyms(client, keysym_desc->set_keysyms, 0, 0);
361 /* If defined, send any keys that were originally set */
362 if (keysym_desc->clear_keysyms != NULL)
363 __guac_rdp_update_keysyms(client, keysym_desc->clear_keysyms, 1, 1);
370 /* Fall back to unicode events if undefined inside current keymap */
372 /* Only send when key pressed - Unicode events do not have DOWN/RELEASE flags */
375 /* Translate keysym into codepoint */
380 codepoint = keysym & 0xFFFFFF;
382 guac_client_log_info(client, "Translated keysym 0x%x to U+%04X", keysym, codepoint);
384 /* Send Unicode event */
385 rdp_inst->input->UnicodeKeyboardEvent(
391 guac_client_log_info(client, "Ignoring key release (Unicode event)");
396 void __guac_rdp_update_keysyms(guac_client* client, const int* keysym_string, int from, int to) {
398 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
401 /* Send all keysyms in string, NULL terminated */
402 while ((keysym = *keysym_string) != 0) {
404 /* Get current keysym state */
405 int current_state = GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keysym_state, keysym);
407 /* If key is currently in given state, send event for changing it to specified "to" state */
408 if (current_state == from)
409 __guac_rdp_send_keysym(client, *keysym_string, to);
418 int rdp_guac_client_key_handler(guac_client* client, int keysym, int pressed) {
420 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
422 /* Update keysym state */
423 GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keysym_state, keysym) = pressed;
425 return __guac_rdp_send_keysym(client, keysym, pressed);
429 int rdp_guac_client_clipboard_handler(guac_client* client, char* data) {
431 rdpChannels* channels =
432 ((rdp_guac_client_data*) client->data)->rdp_inst->context->channels;
434 RDP_CB_FORMAT_LIST_EVENT* format_list =
435 (RDP_CB_FORMAT_LIST_EVENT*) freerdp_event_new(
436 RDP_EVENT_CLASS_CLIPRDR,
437 RDP_EVENT_TYPE_CB_FORMAT_LIST,
440 /* Free existing data */
441 free(((rdp_guac_client_data*) client->data)->clipboard);
443 /* Store data in client */
444 ((rdp_guac_client_data*) client->data)->clipboard = strdup(data);
446 /* Notify server that text data is now available */
447 format_list->formats = (uint32*) malloc(sizeof(uint32));
448 format_list->formats[0] = CB_FORMAT_TEXT;
449 format_list->num_formats = 1;
451 freerdp_channels_send_event(channels, (RDP_EVENT*) format_list);