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.
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.
37 * ***** END LICENSE BLOCK ***** */
42 #include <sys/select.h>
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>
51 #include <guacamole/socket.h>
52 #include <guacamole/protocol.h>
53 #include <guacamole/client.h>
56 #include "rdp_keymap.h"
57 #include "guac_handlers.h"
59 void __guac_rdp_update_keysyms(guac_client* client, const int* keysym_string, int from, int to);
60 int __guac_rdp_send_keysym(guac_client* client, int keysym, int pressed);
61 void __guac_rdp_send_altcode(guac_client* client, int altcode);
64 int rdp_guac_client_free_handler(guac_client* client) {
66 rdp_guac_client_data* guac_client_data =
67 (rdp_guac_client_data*) client->data;
69 freerdp* rdp_inst = guac_client_data->rdp_inst;
70 rdpChannels* channels = rdp_inst->context->channels;
72 /* Clean up RDP client */
73 freerdp_channels_close(channels, rdp_inst);
74 freerdp_channels_free(channels);
75 freerdp_disconnect(rdp_inst);
76 freerdp_clrconv_free(((rdp_freerdp_context*) rdp_inst->context)->clrconv);
77 cache_free(rdp_inst->context->cache);
78 freerdp_free(rdp_inst);
80 /* Free client data */
81 free(guac_client_data);
87 int rdp_guac_client_handle_messages(guac_client* client) {
89 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
90 freerdp* rdp_inst = guac_client_data->rdp_inst;
91 rdpChannels* channels = rdp_inst->context->channels;
101 struct timeval timeout = {
107 if (!freerdp_get_fds(rdp_inst, read_fds, &read_count, write_fds, &write_count)) {
108 guac_client_log_error(client, "Unable to read RDP file descriptors.");
112 /* get channel fds */
113 if (!freerdp_channels_get_fds(channels, rdp_inst, read_fds, &read_count, write_fds, &write_count)) {
114 guac_client_log_error(client, "Unable to read RDP channel file descriptors.");
118 /* Construct read fd_set */
121 for (index = 0; index < read_count; index++) {
122 fd = (int)(long) (read_fds[index]);
128 /* Construct write fd_set */
130 for (index = 0; index < write_count; index++) {
131 fd = (int)(long) (write_fds[index]);
137 /* If no file descriptors, error */
139 guac_client_log_error(client, "No file descriptors");
143 /* Otherwise, wait for file descriptors given */
144 if (select(max_fd + 1, &rfds, &wfds, NULL, &timeout) == -1) {
145 /* these are not really errors */
146 if (!((errno == EAGAIN) ||
147 (errno == EWOULDBLOCK) ||
148 (errno == EINPROGRESS) ||
149 (errno == EINTR))) /* signal occurred */
151 guac_client_log_error(client, "Error waiting for file descriptor.");
156 /* Check the libfreerdp fds */
157 if (!freerdp_check_fds(rdp_inst)) {
158 guac_client_log_error(client, "Error handling RDP file descriptors.");
162 /* Check channel fds */
163 if (!freerdp_channels_check_fds(channels, rdp_inst)) {
164 guac_client_log_error(client, "Error handling RDP channel file descriptors.");
173 int rdp_guac_client_mouse_handler(guac_client* client, int x, int y, int mask) {
175 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
176 freerdp* rdp_inst = guac_client_data->rdp_inst;
178 /* If button mask unchanged, just send move event */
179 if (mask == guac_client_data->mouse_button_mask)
180 rdp_inst->input->MouseEvent(rdp_inst->input, PTR_FLAGS_MOVE, x, y);
182 /* Otherwise, send events describing button change */
185 /* Mouse buttons which have JUST become released */
186 int released_mask = guac_client_data->mouse_button_mask & ~mask;
188 /* Mouse buttons which have JUST become pressed */
189 int pressed_mask = ~guac_client_data->mouse_button_mask & mask;
192 if (released_mask & 0x07) {
194 /* Calculate flags */
196 if (released_mask & 0x01) flags |= PTR_FLAGS_BUTTON1;
197 if (released_mask & 0x02) flags |= PTR_FLAGS_BUTTON3;
198 if (released_mask & 0x04) flags |= PTR_FLAGS_BUTTON2;
200 rdp_inst->input->MouseEvent(rdp_inst->input, flags, x, y);
205 if (pressed_mask & 0x07) {
207 /* Calculate flags */
208 int flags = PTR_FLAGS_DOWN;
209 if (pressed_mask & 0x01) flags |= PTR_FLAGS_BUTTON1;
210 if (pressed_mask & 0x02) flags |= PTR_FLAGS_BUTTON3;
211 if (pressed_mask & 0x04) flags |= PTR_FLAGS_BUTTON2;
212 if (pressed_mask & 0x08) flags |= PTR_FLAGS_WHEEL | 0x78;
213 if (pressed_mask & 0x10) flags |= PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | 0x88;
216 rdp_inst->input->MouseEvent(rdp_inst->input, flags, x, y);
221 if (pressed_mask & 0x18) {
224 if (pressed_mask & 0x08)
225 rdp_inst->input->MouseEvent(
227 PTR_FLAGS_WHEEL | 0x78,
231 if (pressed_mask & 0x10)
232 rdp_inst->input->MouseEvent(
234 PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | 0x88,
240 guac_client_data->mouse_button_mask = mask;
246 void __guac_rdp_send_altcode(guac_client* client, int altcode) {
248 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
249 freerdp* rdp_inst = guac_client_data->rdp_inst;
252 /* Lookup scancode for Alt */
253 int alt = GUAC_RDP_KEYSYM_LOOKUP(
254 guac_client_data->keymap,
255 0xFFE9 /* Alt_L */).scancode;
257 /* Release all pressed modifiers */
258 __guac_rdp_update_keysyms(client, GUAC_KEYSYMS_ALL_MODIFIERS, 1, 0);
261 rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_DOWN, alt);
263 /* For each character in four-digit Alt-code ... */
264 for (i=0; i<4; i++) {
266 /* Get scancode of keypad digit */
267 int scancode = GUAC_RDP_KEYSYM_LOOKUP(
268 guac_client_data->keymap,
269 0xFFB0 + (altcode / 1000)
272 /* Press and release digit */
273 rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_DOWN, scancode);
274 rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_RELEASE, scancode);
276 /* Shift digits left by one place */
277 altcode = (altcode * 10) % 10000;
282 rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_RELEASE, alt);
284 /* Press all originally pressed modifiers */
285 __guac_rdp_update_keysyms(client, GUAC_KEYSYMS_ALL_MODIFIERS, 1, 1);
289 int __guac_rdp_send_keysym(guac_client* client, int keysym, int pressed) {
291 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
292 freerdp* rdp_inst = guac_client_data->rdp_inst;
294 /* If keysym can be in lookup table */
295 if (keysym <= 0xFFFF) {
297 /* Look up scancode mapping */
298 const guac_rdp_keysym_desc* keysym_desc =
299 &GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keymap, keysym);
301 /* If defined, send event */
302 if (keysym_desc->scancode != 0) {
304 /* If defined, send any prerequesite keys that must be set */
305 if (keysym_desc->set_keysyms != NULL)
306 __guac_rdp_update_keysyms(client, keysym_desc->set_keysyms, 0, 1);
308 /* If defined, release any keys that must be cleared */
309 if (keysym_desc->clear_keysyms != NULL)
310 __guac_rdp_update_keysyms(client, keysym_desc->clear_keysyms, 1, 0);
312 /* Send actual key */
313 rdp_inst->input->KeyboardEvent(
316 | (pressed ? KBD_FLAGS_DOWN : KBD_FLAGS_RELEASE),
317 keysym_desc->scancode);
319 /* If defined, release any keys that were originally released */
320 if (keysym_desc->set_keysyms != NULL)
321 __guac_rdp_update_keysyms(client, keysym_desc->set_keysyms, 0, 0);
323 /* If defined, send any keys that were originally set */
324 if (keysym_desc->clear_keysyms != NULL)
325 __guac_rdp_update_keysyms(client, keysym_desc->clear_keysyms, 1, 1);
329 /* If undefined but has Alt-code, use Alt-Code */
330 else if (keysym <= 0xFF) {
332 /* NOTE: The Alt-codes are conveniently identical to keysyms. */
334 /* Only send Alt-code on press */
336 __guac_rdp_send_altcode(client, keysym);
340 /* If no defined Alt-code, log warning */
342 guac_client_log_info(client, "unmapped keysym: 0x%x", keysym);
349 void __guac_rdp_update_keysyms(guac_client* client, const int* keysym_string, int from, int to) {
351 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
354 /* Send all keysyms in string, NULL terminated */
355 while ((keysym = *keysym_string) != 0) {
357 /* Get current keysym state */
358 int current_state = GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keysym_state, keysym);
360 /* If key is currently in given state, send event for changing it to specified "to" state */
361 if (current_state == from)
362 __guac_rdp_send_keysym(client, *keysym_string, to);
371 int rdp_guac_client_key_handler(guac_client* client, int keysym, int pressed) {
373 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
375 /* Update keysym state */
376 GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keysym_state, keysym) = pressed;
378 return __guac_rdp_send_keysym(client, keysym, pressed);