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);
70 int rdp_guac_client_free_handler(guac_client* client) {
72 rdp_guac_client_data* guac_client_data =
73 (rdp_guac_client_data*) client->data;
75 freerdp* rdp_inst = guac_client_data->rdp_inst;
76 rdpChannels* channels = rdp_inst->context->channels;
78 /* Clean up RDP client */
79 freerdp_channels_close(channels, rdp_inst);
80 freerdp_channels_free(channels);
81 freerdp_disconnect(rdp_inst);
82 freerdp_clrconv_free(((rdp_freerdp_context*) rdp_inst->context)->clrconv);
83 cache_free(rdp_inst->context->cache);
84 freerdp_free(rdp_inst);
86 /* Free client data */
87 cairo_surface_destroy(guac_client_data->opaque_glyph_surface);
88 cairo_surface_destroy(guac_client_data->trans_glyph_surface);
89 free(guac_client_data->clipboard);
90 free(guac_client_data);
96 int rdp_guac_client_handle_messages(guac_client* client) {
98 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
99 freerdp* rdp_inst = guac_client_data->rdp_inst;
100 rdpChannels* channels = rdp_inst->context->channels;
111 struct timeval timeout = {
117 if (!freerdp_get_fds(rdp_inst, read_fds, &read_count, write_fds, &write_count)) {
118 guac_error = GUAC_STATUS_BAD_STATE;
119 guac_error_message = "Unable to read RDP file descriptors";
123 /* get channel fds */
124 if (!freerdp_channels_get_fds(channels, rdp_inst, read_fds, &read_count, write_fds, &write_count)) {
125 guac_error = GUAC_STATUS_BAD_STATE;
126 guac_error_message = "Unable to read RDP channel file descriptors";
130 /* Construct read fd_set */
133 for (index = 0; index < read_count; index++) {
134 fd = (int)(long) (read_fds[index]);
140 /* Construct write fd_set */
142 for (index = 0; index < write_count; index++) {
143 fd = (int)(long) (write_fds[index]);
149 /* If no file descriptors, error */
151 guac_error = GUAC_STATUS_BAD_STATE;
152 guac_error_message = "No file descriptors";
156 /* Otherwise, wait for file descriptors given */
157 if (select(max_fd + 1, &rfds, &wfds, NULL, &timeout) == -1) {
158 /* these are not really errors */
159 if (!((errno == EAGAIN) ||
160 (errno == EWOULDBLOCK) ||
161 (errno == EINPROGRESS) ||
162 (errno == EINTR))) /* signal occurred */
164 guac_error = GUAC_STATUS_SEE_ERRNO;
165 guac_error_message = "Error waiting for file descriptor";
170 /* Check the libfreerdp fds */
171 if (!freerdp_check_fds(rdp_inst)) {
172 guac_error = GUAC_STATUS_BAD_STATE;
173 guac_error_message = "Error handling RDP file descriptors";
177 /* Check channel fds */
178 if (!freerdp_channels_check_fds(channels, rdp_inst)) {
179 guac_error = GUAC_STATUS_BAD_STATE;
180 guac_error_message = "Error handling RDP channel file descriptors";
184 /* Check for channel events */
185 event = freerdp_channels_pop_event(channels);
188 /* Handle clipboard events */
189 if (event->event_class == RDP_EVENT_CLASS_CLIPRDR)
190 guac_rdp_process_cliprdr_event(client, event);
192 freerdp_event_free(event);
196 /* Handle RDP disconnect */
197 if (freerdp_shall_disconnect(rdp_inst)) {
198 guac_error = GUAC_STATUS_NO_INPUT;
199 guac_error_message = "RDP server closed connection";
208 int rdp_guac_client_mouse_handler(guac_client* client, int x, int y, int mask) {
210 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
211 freerdp* rdp_inst = guac_client_data->rdp_inst;
213 /* If button mask unchanged, just send move event */
214 if (mask == guac_client_data->mouse_button_mask)
215 rdp_inst->input->MouseEvent(rdp_inst->input, PTR_FLAGS_MOVE, x, y);
217 /* Otherwise, send events describing button change */
220 /* Mouse buttons which have JUST become released */
221 int released_mask = guac_client_data->mouse_button_mask & ~mask;
223 /* Mouse buttons which have JUST become pressed */
224 int pressed_mask = ~guac_client_data->mouse_button_mask & mask;
227 if (released_mask & 0x07) {
229 /* Calculate flags */
231 if (released_mask & 0x01) flags |= PTR_FLAGS_BUTTON1;
232 if (released_mask & 0x02) flags |= PTR_FLAGS_BUTTON3;
233 if (released_mask & 0x04) flags |= PTR_FLAGS_BUTTON2;
235 rdp_inst->input->MouseEvent(rdp_inst->input, flags, x, y);
240 if (pressed_mask & 0x07) {
242 /* Calculate flags */
243 int flags = PTR_FLAGS_DOWN;
244 if (pressed_mask & 0x01) flags |= PTR_FLAGS_BUTTON1;
245 if (pressed_mask & 0x02) flags |= PTR_FLAGS_BUTTON3;
246 if (pressed_mask & 0x04) flags |= PTR_FLAGS_BUTTON2;
247 if (pressed_mask & 0x08) flags |= PTR_FLAGS_WHEEL | 0x78;
248 if (pressed_mask & 0x10) flags |= PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | 0x88;
251 rdp_inst->input->MouseEvent(rdp_inst->input, flags, x, y);
256 if (pressed_mask & 0x18) {
259 if (pressed_mask & 0x08)
260 rdp_inst->input->MouseEvent(
262 PTR_FLAGS_WHEEL | 0x78,
266 if (pressed_mask & 0x10)
267 rdp_inst->input->MouseEvent(
269 PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | 0x88,
275 guac_client_data->mouse_button_mask = mask;
282 int __guac_rdp_send_keysym(guac_client* client, int keysym, int pressed) {
284 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
285 freerdp* rdp_inst = guac_client_data->rdp_inst;
287 /* If keysym can be in lookup table */
288 if (keysym <= 0xFFFF) {
290 /* Look up scancode mapping */
291 const guac_rdp_keysym_desc* keysym_desc =
292 &GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keymap, keysym);
294 /* If defined, send event */
295 if (keysym_desc->scancode != 0) {
297 /* If defined, send any prerequesite keys that must be set */
298 if (keysym_desc->set_keysyms != NULL)
299 __guac_rdp_update_keysyms(client, keysym_desc->set_keysyms, 0, 1);
301 /* If defined, release any keys that must be cleared */
302 if (keysym_desc->clear_keysyms != NULL)
303 __guac_rdp_update_keysyms(client, keysym_desc->clear_keysyms, 1, 0);
305 /* Send actual key */
306 rdp_inst->input->KeyboardEvent(
309 | (pressed ? KBD_FLAGS_DOWN : KBD_FLAGS_RELEASE),
310 keysym_desc->scancode);
312 /* If defined, release any keys that were originally released */
313 if (keysym_desc->set_keysyms != NULL)
314 __guac_rdp_update_keysyms(client, keysym_desc->set_keysyms, 0, 0);
316 /* If defined, send any keys that were originally set */
317 if (keysym_desc->clear_keysyms != NULL)
318 __guac_rdp_update_keysyms(client, keysym_desc->clear_keysyms, 1, 1);
325 /* Fall back to unicode events if undefined inside current keymap */
327 /* Only send when key pressed - Unicode events do not have
328 * DOWN/RELEASE flags */
331 /* Translate keysym into codepoint */
335 else if (keysym >= 0x1000000)
336 codepoint = keysym & 0xFFFFFF;
338 guac_client_log_info(client,
339 "Unmapped keysym has no equivalent unicode "
340 "value: 0x%x", keysym);
344 guac_client_log_info(client, "Translated keysym 0x%x to U+%04X",
347 /* Send Unicode event */
348 rdp_inst->input->UnicodeKeyboardEvent(
354 guac_client_log_info(client, "Ignoring key release (Unicode event)");
359 void __guac_rdp_update_keysyms(guac_client* client, const int* keysym_string, int from, int to) {
361 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
364 /* Send all keysyms in string, NULL terminated */
365 while ((keysym = *keysym_string) != 0) {
367 /* Get current keysym state */
368 int current_state = GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keysym_state, keysym);
370 /* If key is currently in given state, send event for changing it to specified "to" state */
371 if (current_state == from)
372 __guac_rdp_send_keysym(client, *keysym_string, to);
381 int rdp_guac_client_key_handler(guac_client* client, int keysym, int pressed) {
383 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
385 /* Update keysym state */
386 GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keysym_state, keysym) = pressed;
388 return __guac_rdp_send_keysym(client, keysym, pressed);
392 int rdp_guac_client_clipboard_handler(guac_client* client, char* data) {
394 rdpChannels* channels =
395 ((rdp_guac_client_data*) client->data)->rdp_inst->context->channels;
397 RDP_CB_FORMAT_LIST_EVENT* format_list =
398 (RDP_CB_FORMAT_LIST_EVENT*) freerdp_event_new(
399 RDP_EVENT_CLASS_CLIPRDR,
400 RDP_EVENT_TYPE_CB_FORMAT_LIST,
403 /* Free existing data */
404 free(((rdp_guac_client_data*) client->data)->clipboard);
406 /* Store data in client */
407 ((rdp_guac_client_data*) client->data)->clipboard = strdup(data);
409 /* Notify server that text data is now available */
410 format_list->formats = (uint32*) malloc(sizeof(uint32));
411 format_list->formats[0] = CB_FORMAT_TEXT;
412 format_list->num_formats = 1;
414 freerdp_channels_send_event(channels, (RDP_EVENT*) format_list);