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 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
41 #include <sys/select.h>
44 #include <freerdp/freerdp.h>
45 #include <freerdp/channels/channels.h>
46 #include <freerdp/input.h>
48 #include <guacamole/socket.h>
49 #include <guacamole/protocol.h>
50 #include <guacamole/client.h>
53 #include "rdp_keymap.h"
54 #include "guac_handlers.h"
56 int rdp_guac_client_free_handler(guac_client* client) {
60 /* FIXME: Clean up RDP client + disconnect */
66 int rdp_guac_client_handle_messages(guac_client* client) {
68 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
69 freerdp* rdp_inst = guac_client_data->rdp_inst;
70 rdpChannels* channels = rdp_inst->context->channels;
82 if (!freerdp_get_fds(rdp_inst, read_fds, &read_count, write_fds, &write_count)) {
83 guac_client_log_error(client, "Unable to read RDP file descriptors.");
88 if (!freerdp_channels_get_fds(channels, rdp_inst, read_fds, &read_count, write_fds, &write_count)) {
89 guac_client_log_error(client, "Unable to read RDP channel file descriptors.");
93 /* Construct read fd_set */
96 for (index = 0; index < read_count; index++) {
97 fd = (int)(long) (read_fds[index]);
103 /* Construct write fd_set */
105 for (index = 0; index < write_count; index++) {
106 fd = (int)(long) (write_fds[index]);
112 /* If no file descriptors, error */
114 guac_client_log_error(client, "No file descriptors");
118 /* Otherwise, wait for file descriptors given */
119 if (select(max_fd + 1, &rfds, &wfds, NULL, NULL) == -1) {
120 /* these are not really errors */
121 if (!((errno == EAGAIN) ||
122 (errno == EWOULDBLOCK) ||
123 (errno == EINPROGRESS) ||
124 (errno == EINTR))) /* signal occurred */
126 guac_client_log_error(client, "Error waiting for file descriptor.");
131 /* Check the libfreerdp fds */
132 if (!freerdp_check_fds(rdp_inst)) {
133 guac_client_log_error(client, "Error handling RDP file descriptors.");
137 /* Check channel fds */
138 if (!freerdp_channels_check_fds(channels, rdp_inst)) {
139 guac_client_log_error(client, "Error handling RDP channel file descriptors.");
148 int rdp_guac_client_mouse_handler(guac_client* client, int x, int y, int mask) {
150 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
151 freerdp* rdp_inst = guac_client_data->rdp_inst;
153 /* If button mask unchanged, just send move event */
154 if (mask == guac_client_data->mouse_button_mask)
155 rdp_inst->input->MouseEvent(rdp_inst->input, PTR_FLAGS_MOVE, x, y);
157 /* Otherwise, send events describing button change */
160 /* Mouse buttons which have JUST become released */
161 int released_mask = guac_client_data->mouse_button_mask & ~mask;
163 /* Mouse buttons which have JUST become pressed */
164 int pressed_mask = ~guac_client_data->mouse_button_mask & mask;
167 if (released_mask & 0x07) {
169 /* Calculate flags */
171 if (released_mask & 0x01) flags |= PTR_FLAGS_BUTTON1;
172 if (released_mask & 0x02) flags |= PTR_FLAGS_BUTTON3;
173 if (released_mask & 0x04) flags |= PTR_FLAGS_BUTTON2;
175 rdp_inst->input->MouseEvent(rdp_inst->input, flags, x, y);
180 if (pressed_mask & 0x07) {
182 /* Calculate flags */
183 int flags = PTR_FLAGS_DOWN;
184 if (pressed_mask & 0x01) flags |= PTR_FLAGS_BUTTON1;
185 if (pressed_mask & 0x02) flags |= PTR_FLAGS_BUTTON3;
186 if (pressed_mask & 0x04) flags |= PTR_FLAGS_BUTTON2;
187 if (pressed_mask & 0x08) flags |= PTR_FLAGS_WHEEL | 0x78;
188 if (pressed_mask & 0x10) flags |= PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | 0x88;
191 rdp_inst->input->MouseEvent(rdp_inst->input, flags, x, y);
196 if (pressed_mask & 0x18) {
199 if (pressed_mask & 0x08)
200 rdp_inst->input->MouseEvent(
202 PTR_FLAGS_WHEEL | 0x78,
206 if (pressed_mask & 0x10)
207 rdp_inst->input->MouseEvent(
209 PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | 0x88,
215 guac_client_data->mouse_button_mask = mask;
221 int rdp_guac_client_key_handler(guac_client* client, int keysym, int pressed) {
223 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
224 freerdp* rdp_inst = guac_client_data->rdp_inst;
226 /* If keysym can be in lookup table */
227 if (keysym <= 0xFFFF) {
229 /* Look up scancode */
230 const guac_rdp_keymap* keymap =
231 &guac_rdp_keysym_scancode[(keysym & 0xFF00) >> 8][keysym & 0xFF];
233 /* If defined, send event */
234 if (keymap->scancode != 0)
235 rdp_inst->input->KeyboardEvent(
238 | (pressed ? KBD_FLAGS_DOWN : KBD_FLAGS_RELEASE),
241 guac_client_log_info(client, "unmapped keysym: 0x%x", keysym);