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>
49 #include <guacamole/socket.h>
50 #include <guacamole/protocol.h>
51 #include <guacamole/client.h>
54 #include "rdp_keymap.h"
55 #include "guac_handlers.h"
57 void __guac_rdp_update_keysyms(guac_client* client, const int* keysym_string, int from, int to);
58 int __guac_rdp_send_keysym(guac_client* client, int keysym, int pressed);
59 void __guac_rdp_send_altcode(guac_client* client, int altcode);
61 int rdp_guac_client_free_handler(guac_client* client) {
65 /* FIXME: Clean up RDP client + disconnect */
71 int rdp_guac_client_handle_messages(guac_client* client) {
73 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
74 freerdp* rdp_inst = guac_client_data->rdp_inst;
75 rdpChannels* channels = rdp_inst->context->channels;
85 struct timeval timeout = {
91 if (!freerdp_get_fds(rdp_inst, read_fds, &read_count, write_fds, &write_count)) {
92 guac_client_log_error(client, "Unable to read RDP file descriptors.");
97 if (!freerdp_channels_get_fds(channels, rdp_inst, read_fds, &read_count, write_fds, &write_count)) {
98 guac_client_log_error(client, "Unable to read RDP channel file descriptors.");
102 /* Construct read fd_set */
105 for (index = 0; index < read_count; index++) {
106 fd = (int)(long) (read_fds[index]);
112 /* Construct write fd_set */
114 for (index = 0; index < write_count; index++) {
115 fd = (int)(long) (write_fds[index]);
121 /* If no file descriptors, error */
123 guac_client_log_error(client, "No file descriptors");
127 /* Otherwise, wait for file descriptors given */
128 if (select(max_fd + 1, &rfds, &wfds, NULL, &timeout) == -1) {
129 /* these are not really errors */
130 if (!((errno == EAGAIN) ||
131 (errno == EWOULDBLOCK) ||
132 (errno == EINPROGRESS) ||
133 (errno == EINTR))) /* signal occurred */
135 guac_client_log_error(client, "Error waiting for file descriptor.");
140 /* Check the libfreerdp fds */
141 if (!freerdp_check_fds(rdp_inst)) {
142 guac_client_log_error(client, "Error handling RDP file descriptors.");
146 /* Check channel fds */
147 if (!freerdp_channels_check_fds(channels, rdp_inst)) {
148 guac_client_log_error(client, "Error handling RDP channel file descriptors.");
157 int rdp_guac_client_mouse_handler(guac_client* client, int x, int y, int mask) {
159 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
160 freerdp* rdp_inst = guac_client_data->rdp_inst;
162 /* If button mask unchanged, just send move event */
163 if (mask == guac_client_data->mouse_button_mask)
164 rdp_inst->input->MouseEvent(rdp_inst->input, PTR_FLAGS_MOVE, x, y);
166 /* Otherwise, send events describing button change */
169 /* Mouse buttons which have JUST become released */
170 int released_mask = guac_client_data->mouse_button_mask & ~mask;
172 /* Mouse buttons which have JUST become pressed */
173 int pressed_mask = ~guac_client_data->mouse_button_mask & mask;
176 if (released_mask & 0x07) {
178 /* Calculate flags */
180 if (released_mask & 0x01) flags |= PTR_FLAGS_BUTTON1;
181 if (released_mask & 0x02) flags |= PTR_FLAGS_BUTTON3;
182 if (released_mask & 0x04) flags |= PTR_FLAGS_BUTTON2;
184 rdp_inst->input->MouseEvent(rdp_inst->input, flags, x, y);
189 if (pressed_mask & 0x07) {
191 /* Calculate flags */
192 int flags = PTR_FLAGS_DOWN;
193 if (pressed_mask & 0x01) flags |= PTR_FLAGS_BUTTON1;
194 if (pressed_mask & 0x02) flags |= PTR_FLAGS_BUTTON3;
195 if (pressed_mask & 0x04) flags |= PTR_FLAGS_BUTTON2;
196 if (pressed_mask & 0x08) flags |= PTR_FLAGS_WHEEL | 0x78;
197 if (pressed_mask & 0x10) flags |= PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | 0x88;
200 rdp_inst->input->MouseEvent(rdp_inst->input, flags, x, y);
205 if (pressed_mask & 0x18) {
208 if (pressed_mask & 0x08)
209 rdp_inst->input->MouseEvent(
211 PTR_FLAGS_WHEEL | 0x78,
215 if (pressed_mask & 0x10)
216 rdp_inst->input->MouseEvent(
218 PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | 0x88,
224 guac_client_data->mouse_button_mask = mask;
230 void __guac_rdp_send_altcode(guac_client* client, int altcode) {
232 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
233 freerdp* rdp_inst = guac_client_data->rdp_inst;
236 /* Lookup scancode for Alt */
237 int alt = GUAC_RDP_KEYSYM_LOOKUP(
238 guac_client_data->keymap,
239 0xFFE9 /* Alt_L */).scancode;
241 /* Release all pressed modifiers */
242 __guac_rdp_update_keysyms(client, GUAC_KEYSYMS_ALL_MODIFIERS, 1, 0);
245 rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_DOWN, alt);
247 /* For each character in four-digit Alt-code ... */
248 for (i=0; i<4; i++) {
250 /* Get scancode of keypad digit */
251 int scancode = GUAC_RDP_KEYSYM_LOOKUP(
252 guac_client_data->keymap,
253 0xFFB0 + (altcode / 1000)
256 /* Press and release digit */
257 rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_DOWN, scancode);
258 rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_RELEASE, scancode);
260 /* Shift digits left by one place */
261 altcode = (altcode * 10) % 10000;
266 rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_RELEASE, alt);
268 /* Press all originally pressed modifiers */
269 __guac_rdp_update_keysyms(client, GUAC_KEYSYMS_ALL_MODIFIERS, 1, 1);
273 int __guac_rdp_send_keysym(guac_client* client, int keysym, int pressed) {
275 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
276 freerdp* rdp_inst = guac_client_data->rdp_inst;
278 /* If keysym can be in lookup table */
279 if (keysym <= 0xFFFF) {
281 /* Look up scancode mapping */
282 const guac_rdp_keysym_desc* keysym_desc =
283 &GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keymap, keysym);
285 /* If defined, send event */
286 if (keysym_desc->scancode != 0) {
288 /* If defined, send any prerequesite keys that must be set */
289 if (keysym_desc->set_keysyms != NULL)
290 __guac_rdp_update_keysyms(client, keysym_desc->set_keysyms, 0, 1);
292 /* If defined, release any keys that must be cleared */
293 if (keysym_desc->clear_keysyms != NULL)
294 __guac_rdp_update_keysyms(client, keysym_desc->clear_keysyms, 1, 0);
296 /* Send actual key */
297 rdp_inst->input->KeyboardEvent(
300 | (pressed ? KBD_FLAGS_DOWN : KBD_FLAGS_RELEASE),
301 keysym_desc->scancode);
303 /* If defined, release any keys that were originally released */
304 if (keysym_desc->set_keysyms != NULL)
305 __guac_rdp_update_keysyms(client, keysym_desc->set_keysyms, 0, 0);
307 /* If defined, send any keys that were originally set */
308 if (keysym_desc->clear_keysyms != NULL)
309 __guac_rdp_update_keysyms(client, keysym_desc->clear_keysyms, 1, 1);
313 /* If undefined but has Alt-code, use Alt-Code */
314 else if (keysym <= 0xFF) {
316 /* NOTE: The Alt-codes are conveniently identical to keysyms. */
318 /* Only send Alt-code on press */
320 __guac_rdp_send_altcode(client, keysym);
324 /* If no defined Alt-code, log warning */
326 guac_client_log_info(client, "unmapped keysym: 0x%x", keysym);
333 void __guac_rdp_update_keysyms(guac_client* client, const int* keysym_string, int from, int to) {
335 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
338 /* Send all keysyms in string, NULL terminated */
339 while ((keysym = *keysym_string) != 0) {
341 /* Get current keysym state */
342 int current_state = GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keysym_state, keysym);
344 /* If key is currently in given state, send event for changing it to specified "to" state */
345 if (current_state == from)
346 __guac_rdp_send_keysym(client, *keysym_string, to);
355 int rdp_guac_client_key_handler(guac_client* client, int keysym, int pressed) {
357 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
359 /* Update keysym state */
360 GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keysym_state, keysym) = pressed;
362 return __guac_rdp_send_keysym(client, keysym, pressed);