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>
54 #include <guacamole/error.h>
57 #include "rdp_keymap.h"
58 #include "guac_handlers.h"
60 void __guac_rdp_update_keysyms(guac_client* client, const int* keysym_string, int from, int to);
61 int __guac_rdp_send_keysym(guac_client* client, int keysym, int pressed);
62 void __guac_rdp_send_altcode(guac_client* client, int altcode);
65 int rdp_guac_client_free_handler(guac_client* client) {
67 rdp_guac_client_data* guac_client_data =
68 (rdp_guac_client_data*) client->data;
70 freerdp* rdp_inst = guac_client_data->rdp_inst;
71 rdpChannels* channels = rdp_inst->context->channels;
73 /* Clean up RDP client */
74 freerdp_channels_close(channels, rdp_inst);
75 freerdp_channels_free(channels);
76 freerdp_disconnect(rdp_inst);
77 freerdp_clrconv_free(((rdp_freerdp_context*) rdp_inst->context)->clrconv);
78 cache_free(rdp_inst->context->cache);
79 freerdp_free(rdp_inst);
81 /* Free client data */
82 free(guac_client_data);
88 int rdp_guac_client_handle_messages(guac_client* client) {
90 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
91 freerdp* rdp_inst = guac_client_data->rdp_inst;
92 rdpChannels* channels = rdp_inst->context->channels;
102 struct timeval timeout = {
108 if (!freerdp_get_fds(rdp_inst, read_fds, &read_count, write_fds, &write_count)) {
109 guac_error = GUAC_STATUS_BAD_STATE;
110 guac_error_message = "Unable to read RDP file descriptors";
114 /* get channel fds */
115 if (!freerdp_channels_get_fds(channels, rdp_inst, read_fds, &read_count, write_fds, &write_count)) {
116 guac_error = GUAC_STATUS_BAD_STATE;
117 guac_error_message = "Unable to read RDP channel file descriptors";
121 /* Construct read fd_set */
124 for (index = 0; index < read_count; index++) {
125 fd = (int)(long) (read_fds[index]);
131 /* Construct write fd_set */
133 for (index = 0; index < write_count; index++) {
134 fd = (int)(long) (write_fds[index]);
140 /* If no file descriptors, error */
142 guac_error = GUAC_STATUS_BAD_STATE;
143 guac_error_message = "No file descriptors";
147 /* Otherwise, wait for file descriptors given */
148 if (select(max_fd + 1, &rfds, &wfds, NULL, &timeout) == -1) {
149 /* these are not really errors */
150 if (!((errno == EAGAIN) ||
151 (errno == EWOULDBLOCK) ||
152 (errno == EINPROGRESS) ||
153 (errno == EINTR))) /* signal occurred */
155 guac_error = GUAC_STATUS_SEE_ERRNO;
156 guac_error_message = "Error waiting for file descriptor";
161 /* Check the libfreerdp fds */
162 if (!freerdp_check_fds(rdp_inst)) {
163 guac_error = GUAC_STATUS_BAD_STATE;
164 guac_error_message = "Error handling RDP file descriptors";
168 /* Check channel fds */
169 if (!freerdp_channels_check_fds(channels, rdp_inst)) {
170 guac_error = GUAC_STATUS_BAD_STATE;
171 guac_error_message = "Error handling RDP channel file descriptors";
180 int rdp_guac_client_mouse_handler(guac_client* client, int x, int y, int mask) {
182 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
183 freerdp* rdp_inst = guac_client_data->rdp_inst;
185 /* If button mask unchanged, just send move event */
186 if (mask == guac_client_data->mouse_button_mask)
187 rdp_inst->input->MouseEvent(rdp_inst->input, PTR_FLAGS_MOVE, x, y);
189 /* Otherwise, send events describing button change */
192 /* Mouse buttons which have JUST become released */
193 int released_mask = guac_client_data->mouse_button_mask & ~mask;
195 /* Mouse buttons which have JUST become pressed */
196 int pressed_mask = ~guac_client_data->mouse_button_mask & mask;
199 if (released_mask & 0x07) {
201 /* Calculate flags */
203 if (released_mask & 0x01) flags |= PTR_FLAGS_BUTTON1;
204 if (released_mask & 0x02) flags |= PTR_FLAGS_BUTTON3;
205 if (released_mask & 0x04) flags |= PTR_FLAGS_BUTTON2;
207 rdp_inst->input->MouseEvent(rdp_inst->input, flags, x, y);
212 if (pressed_mask & 0x07) {
214 /* Calculate flags */
215 int flags = PTR_FLAGS_DOWN;
216 if (pressed_mask & 0x01) flags |= PTR_FLAGS_BUTTON1;
217 if (pressed_mask & 0x02) flags |= PTR_FLAGS_BUTTON3;
218 if (pressed_mask & 0x04) flags |= PTR_FLAGS_BUTTON2;
219 if (pressed_mask & 0x08) flags |= PTR_FLAGS_WHEEL | 0x78;
220 if (pressed_mask & 0x10) flags |= PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | 0x88;
223 rdp_inst->input->MouseEvent(rdp_inst->input, flags, x, y);
228 if (pressed_mask & 0x18) {
231 if (pressed_mask & 0x08)
232 rdp_inst->input->MouseEvent(
234 PTR_FLAGS_WHEEL | 0x78,
238 if (pressed_mask & 0x10)
239 rdp_inst->input->MouseEvent(
241 PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | 0x88,
247 guac_client_data->mouse_button_mask = mask;
253 void __guac_rdp_send_altcode(guac_client* client, int altcode) {
255 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
256 freerdp* rdp_inst = guac_client_data->rdp_inst;
259 /* Lookup scancode for Alt */
260 int alt = GUAC_RDP_KEYSYM_LOOKUP(
261 guac_client_data->keymap,
262 0xFFE9 /* Alt_L */).scancode;
264 /* Release all pressed modifiers */
265 __guac_rdp_update_keysyms(client, GUAC_KEYSYMS_ALL_MODIFIERS, 1, 0);
268 rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_DOWN, alt);
270 /* For each character in four-digit Alt-code ... */
271 for (i=0; i<4; i++) {
273 /* Get scancode of keypad digit */
274 int scancode = GUAC_RDP_KEYSYM_LOOKUP(
275 guac_client_data->keymap,
276 0xFFB0 + (altcode / 1000)
279 /* Press and release digit */
280 rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_DOWN, scancode);
281 rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_RELEASE, scancode);
283 /* Shift digits left by one place */
284 altcode = (altcode * 10) % 10000;
289 rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_RELEASE, alt);
291 /* Press all originally pressed modifiers */
292 __guac_rdp_update_keysyms(client, GUAC_KEYSYMS_ALL_MODIFIERS, 1, 1);
296 int __guac_rdp_send_keysym(guac_client* client, int keysym, int pressed) {
298 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
299 freerdp* rdp_inst = guac_client_data->rdp_inst;
301 /* If keysym can be in lookup table */
302 if (keysym <= 0xFFFF) {
304 /* Look up scancode mapping */
305 const guac_rdp_keysym_desc* keysym_desc =
306 &GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keymap, keysym);
308 /* If defined, send event */
309 if (keysym_desc->scancode != 0) {
311 /* If defined, send any prerequesite keys that must be set */
312 if (keysym_desc->set_keysyms != NULL)
313 __guac_rdp_update_keysyms(client, keysym_desc->set_keysyms, 0, 1);
315 /* If defined, release any keys that must be cleared */
316 if (keysym_desc->clear_keysyms != NULL)
317 __guac_rdp_update_keysyms(client, keysym_desc->clear_keysyms, 1, 0);
319 /* Send actual key */
320 rdp_inst->input->KeyboardEvent(
323 | (pressed ? KBD_FLAGS_DOWN : KBD_FLAGS_RELEASE),
324 keysym_desc->scancode);
326 /* If defined, release any keys that were originally released */
327 if (keysym_desc->set_keysyms != NULL)
328 __guac_rdp_update_keysyms(client, keysym_desc->set_keysyms, 0, 0);
330 /* If defined, send any keys that were originally set */
331 if (keysym_desc->clear_keysyms != NULL)
332 __guac_rdp_update_keysyms(client, keysym_desc->clear_keysyms, 1, 1);
336 /* If undefined but has Alt-code, use Alt-Code */
337 else if (keysym <= 0xFF) {
339 /* NOTE: The Alt-codes are conveniently identical to keysyms. */
341 /* Only send Alt-code on press */
343 __guac_rdp_send_altcode(client, keysym);
347 /* If no defined Alt-code, log warning */
349 guac_client_log_info(client, "unmapped keysym: 0x%x", keysym);
356 void __guac_rdp_update_keysyms(guac_client* client, const int* keysym_string, int from, int to) {
358 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
361 /* Send all keysyms in string, NULL terminated */
362 while ((keysym = *keysym_string) != 0) {
364 /* Get current keysym state */
365 int current_state = GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keysym_state, keysym);
367 /* If key is currently in given state, send event for changing it to specified "to" state */
368 if (current_state == from)
369 __guac_rdp_send_keysym(client, *keysym_string, to);
378 int rdp_guac_client_key_handler(guac_client* client, int keysym, int pressed) {
380 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
382 /* Update keysym state */
383 GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keysym_state, keysym) = pressed;
385 return __guac_rdp_send_keysym(client, keysym, pressed);