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 int rdp_guac_client_free_handler(guac_client* client) {
61 /* FIXME: Clean up RDP client + disconnect */
67 int rdp_guac_client_handle_messages(guac_client* client) {
69 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
70 freerdp* rdp_inst = guac_client_data->rdp_inst;
71 rdpChannels* channels = rdp_inst->context->channels;
81 struct timeval timeout = {
87 if (!freerdp_get_fds(rdp_inst, read_fds, &read_count, write_fds, &write_count)) {
88 guac_client_log_error(client, "Unable to read RDP file descriptors.");
93 if (!freerdp_channels_get_fds(channels, rdp_inst, read_fds, &read_count, write_fds, &write_count)) {
94 guac_client_log_error(client, "Unable to read RDP channel file descriptors.");
98 /* Construct read fd_set */
101 for (index = 0; index < read_count; index++) {
102 fd = (int)(long) (read_fds[index]);
108 /* Construct write fd_set */
110 for (index = 0; index < write_count; index++) {
111 fd = (int)(long) (write_fds[index]);
117 /* If no file descriptors, error */
119 guac_client_log_error(client, "No file descriptors");
123 /* Otherwise, wait for file descriptors given */
124 if (select(max_fd + 1, &rfds, &wfds, NULL, &timeout) == -1) {
125 /* these are not really errors */
126 if (!((errno == EAGAIN) ||
127 (errno == EWOULDBLOCK) ||
128 (errno == EINPROGRESS) ||
129 (errno == EINTR))) /* signal occurred */
131 guac_client_log_error(client, "Error waiting for file descriptor.");
136 /* Check the libfreerdp fds */
137 if (!freerdp_check_fds(rdp_inst)) {
138 guac_client_log_error(client, "Error handling RDP file descriptors.");
142 /* Check channel fds */
143 if (!freerdp_channels_check_fds(channels, rdp_inst)) {
144 guac_client_log_error(client, "Error handling RDP channel file descriptors.");
153 int rdp_guac_client_mouse_handler(guac_client* client, int x, int y, int mask) {
155 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
156 freerdp* rdp_inst = guac_client_data->rdp_inst;
158 /* If button mask unchanged, just send move event */
159 if (mask == guac_client_data->mouse_button_mask)
160 rdp_inst->input->MouseEvent(rdp_inst->input, PTR_FLAGS_MOVE, x, y);
162 /* Otherwise, send events describing button change */
165 /* Mouse buttons which have JUST become released */
166 int released_mask = guac_client_data->mouse_button_mask & ~mask;
168 /* Mouse buttons which have JUST become pressed */
169 int pressed_mask = ~guac_client_data->mouse_button_mask & mask;
172 if (released_mask & 0x07) {
174 /* Calculate flags */
176 if (released_mask & 0x01) flags |= PTR_FLAGS_BUTTON1;
177 if (released_mask & 0x02) flags |= PTR_FLAGS_BUTTON3;
178 if (released_mask & 0x04) flags |= PTR_FLAGS_BUTTON2;
180 rdp_inst->input->MouseEvent(rdp_inst->input, flags, x, y);
185 if (pressed_mask & 0x07) {
187 /* Calculate flags */
188 int flags = PTR_FLAGS_DOWN;
189 if (pressed_mask & 0x01) flags |= PTR_FLAGS_BUTTON1;
190 if (pressed_mask & 0x02) flags |= PTR_FLAGS_BUTTON3;
191 if (pressed_mask & 0x04) flags |= PTR_FLAGS_BUTTON2;
192 if (pressed_mask & 0x08) flags |= PTR_FLAGS_WHEEL | 0x78;
193 if (pressed_mask & 0x10) flags |= PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | 0x88;
196 rdp_inst->input->MouseEvent(rdp_inst->input, flags, x, y);
201 if (pressed_mask & 0x18) {
204 if (pressed_mask & 0x08)
205 rdp_inst->input->MouseEvent(
207 PTR_FLAGS_WHEEL | 0x78,
211 if (pressed_mask & 0x10)
212 rdp_inst->input->MouseEvent(
214 PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | 0x88,
220 guac_client_data->mouse_button_mask = mask;
226 void __guac_rdp_send_altcode(guac_client* client, const char* altcode) {
228 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
229 freerdp* rdp_inst = guac_client_data->rdp_inst;
230 const guac_rdp_keysym_scancode_map* keysym_scancodes = guac_client_data->keysym_scancodes;
232 /* Lookup scancode for Alt */
233 int alt = GUAC_RDP_KEYSYM_LOOKUP(*keysym_scancodes, 0xFFE9 /* Alt_L */)->scancode;
234 guac_client_log_info(client, "ALTCODE: alt=%i", alt);
237 rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_DOWN, alt);
239 /* For each character in Alt-code ... */
240 while (*altcode != '\0') {
242 /* Get scancode of keypad digit */
243 int scancode = GUAC_RDP_KEYSYM_LOOKUP(*keysym_scancodes, 0xFFB0 + (*altcode) - '0')->scancode;
245 /* Press and release digit */
246 rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_DOWN, scancode);
247 rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_RELEASE, scancode);
248 guac_client_log_info(client, "ALTCODE: scan=%i", scancode);
255 rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_RELEASE, alt);
256 guac_client_log_info(client, "ALTCODE: done");
260 int rdp_guac_client_key_handler(guac_client* client, int keysym, int pressed) {
262 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
263 freerdp* rdp_inst = guac_client_data->rdp_inst;
264 const guac_rdp_keysym_scancode_map* keysym_scancodes = guac_client_data->keysym_scancodes;
266 /* If keysym can be in lookup table */
267 if (keysym <= 0xFFFF) {
269 /* Look up scancode mapping */
270 const guac_rdp_scancode_map* scancode_map = GUAC_RDP_KEYSYM_LOOKUP(*keysym_scancodes, keysym);
272 /* If defined, send event */
273 if (scancode_map->scancode != 0)
274 rdp_inst->input->KeyboardEvent(
277 | (pressed ? KBD_FLAGS_DOWN : KBD_FLAGS_RELEASE),
278 scancode_map->scancode);
280 /* If undefined, try to type using Alt-code */
283 const guac_rdp_altcode_map* altcode_map = GUAC_RDP_KEYSYM_LOOKUP(guac_rdp_keysym_altcode, keysym);
284 if (altcode_map->altcode != NULL) {
286 /* Only send Alt-code on press */
288 __guac_rdp_send_altcode(client, altcode_map->altcode);
292 /* If no defined Alt-code, log warning */
294 guac_client_log_info(client, "unmapped keysym: 0x%x", keysym);