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/chanman.h>
46 #include <freerdp/constants/core.h>
48 #include <guacamole/socket.h>
49 #include <guacamole/protocol.h>
50 #include <guacamole/client.h>
52 #include "rdp_handlers.h"
53 #include "rdp_client.h"
54 #include "rdp_keymap.h"
56 /* Client plugin arguments */
57 const char* GUAC_CLIENT_ARGS[] = {
63 int rdp_guac_client_free_handler(guac_client* client) {
65 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
67 /* Disconnect client */
68 guac_client_data->rdp_inst->rdp_disconnect(guac_client_data->rdp_inst);
71 freerdp_free(guac_client_data->rdp_inst);
72 freerdp_chanman_free(guac_client_data->chanman);
73 free(guac_client_data->settings);
75 /* Free guac client data */
76 free(guac_client_data);
82 int rdp_guac_client_handle_messages(guac_client* client) {
84 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
85 rdpInst* rdp_inst = guac_client_data->rdp_inst;
86 rdpChanMan* chanman = guac_client_data->chanman;
98 if (rdp_inst->rdp_get_fds(rdp_inst, read_fds, &read_count, write_fds, &write_count) != 0) {
99 guac_client_log_error(client, "Unable to read RDP file descriptors.");
103 /* get channel fds */
104 if (freerdp_chanman_get_fds(chanman, rdp_inst, read_fds, &read_count, write_fds, &write_count) != 0) {
105 guac_client_log_error(client, "Unable to read RDP channel file descriptors.");
109 /* Construct read fd_set */
112 for (index = 0; index < read_count; index++) {
113 fd = (int)(long) (read_fds[index]);
119 /* Construct write fd_set */
121 for (index = 0; index < write_count; index++) {
122 fd = (int)(long) (write_fds[index]);
128 /* If no file descriptors, error */
130 guac_client_log_error(client, "No file descriptors");
134 /* Otherwise, wait for file descriptors given */
135 if (select(max_fd + 1, &rfds, &wfds, NULL, NULL) == -1) {
136 /* these are not really errors */
137 if (!((errno == EAGAIN) ||
138 (errno == EWOULDBLOCK) ||
139 (errno == EINPROGRESS) ||
140 (errno == EINTR))) /* signal occurred */
142 guac_client_log_error(client, "Error waiting for file descriptor.");
147 /* check the libfreerdp fds */
148 if (rdp_inst->rdp_check_fds(rdp_inst) != 0) {
149 guac_client_log_error(client, "Error handling RDP file descriptors.");
153 /* check channel fds */
154 if (freerdp_chanman_check_fds(chanman, rdp_inst) != 0) {
155 guac_client_log_error(client, "Error handling RDP channel file descriptors.");
164 int guac_client_init(guac_client* client, int argc, char** argv) {
166 rdp_guac_client_data* guac_client_data;
173 int port = RDP_DEFAULT_PORT;
176 guac_protocol_send_error(client->socket, "Wrong argument count received.");
177 guac_socket_flush(client->socket);
181 /* If port specified, use it */
182 if (argv[1][0] != '\0')
183 port = atoi(argv[1]);
187 /* Allocate client data */
188 guac_client_data = malloc(sizeof(rdp_guac_client_data));
190 /* Get channel manager */
191 chanman = freerdp_chanman_new();
192 guac_client_data->chanman = chanman;
195 settings = malloc(sizeof(rdpSet));
196 memset(settings, 0, sizeof(rdpSet));
197 guac_client_data->settings = settings;
200 strncpy(settings->hostname, hostname, sizeof(settings->hostname) - 1);
203 settings->width = 1024;
204 settings->height = 768;
206 strncpy(settings->server, hostname, sizeof(settings->server));
207 strcpy(settings->username, "guest");
209 settings->tcp_port_rdp = port;
210 settings->encryption = 1;
211 settings->server_depth = 16;
212 settings->bitmap_cache = 1;
213 settings->bitmap_compression = 1;
214 settings->desktop_save = 0;
215 settings->performanceflags =
216 PERF_DISABLE_WALLPAPER
217 | PERF_DISABLE_FULLWINDOWDRAG
218 | PERF_DISABLE_MENUANIMATIONS;
219 settings->mouse_motion = 1;
220 settings->off_screen_bitmaps = 1;
221 settings->triblt = 0;
222 settings->software_gdi = 0;
223 settings->new_cursors = 1;
224 settings->rdp_version = 5;
225 settings->rdp_security = 1;
228 rdp_inst = freerdp_new(settings);
229 if (rdp_inst == NULL) {
230 guac_protocol_send_error(client->socket, "Error initializing RDP client");
231 guac_socket_flush(client->socket);
234 guac_client_data->rdp_inst = rdp_inst;
235 guac_client_data->mouse_button_mask = 0;
236 guac_client_data->current_surface = GUAC_DEFAULT_LAYER;
239 /* Store client data */
240 rdp_inst->param1 = client;
241 client->data = guac_client_data;
245 rdp_inst->ui_error = guac_rdp_ui_error;
246 rdp_inst->ui_warning = guac_rdp_ui_warning;
247 rdp_inst->ui_unimpl = guac_rdp_ui_unimpl;
248 rdp_inst->ui_begin_update = guac_rdp_ui_begin_update;
249 rdp_inst->ui_end_update = guac_rdp_ui_end_update;
250 rdp_inst->ui_desktop_save = guac_rdp_ui_desktop_save;
251 rdp_inst->ui_desktop_restore = guac_rdp_ui_desktop_restore;
252 rdp_inst->ui_create_bitmap = guac_rdp_ui_create_bitmap;
253 rdp_inst->ui_paint_bitmap = guac_rdp_ui_paint_bitmap;
254 rdp_inst->ui_destroy_bitmap = guac_rdp_ui_destroy_bitmap;
255 rdp_inst->ui_line = guac_rdp_ui_line;
256 rdp_inst->ui_rect = guac_rdp_ui_rect;
257 rdp_inst->ui_polygon = guac_rdp_ui_polygon;
258 rdp_inst->ui_polyline = guac_rdp_ui_polyline;
259 rdp_inst->ui_ellipse = guac_rdp_ui_ellipse;
260 rdp_inst->ui_start_draw_glyphs = guac_rdp_ui_start_draw_glyphs;
261 rdp_inst->ui_draw_glyph = guac_rdp_ui_draw_glyph;
262 rdp_inst->ui_end_draw_glyphs = guac_rdp_ui_end_draw_glyphs;
263 rdp_inst->ui_get_toggle_keys_state = guac_rdp_ui_get_toggle_keys_state;
264 rdp_inst->ui_bell = guac_rdp_ui_bell;
265 rdp_inst->ui_destblt = guac_rdp_ui_destblt;
266 rdp_inst->ui_patblt = guac_rdp_ui_patblt;
267 rdp_inst->ui_screenblt = guac_rdp_ui_screenblt;
268 rdp_inst->ui_memblt = guac_rdp_ui_memblt;
269 rdp_inst->ui_triblt = guac_rdp_ui_triblt;
270 rdp_inst->ui_create_glyph = guac_rdp_ui_create_glyph;
271 rdp_inst->ui_destroy_glyph = guac_rdp_ui_destroy_glyph;
272 rdp_inst->ui_select = guac_rdp_ui_select;
273 rdp_inst->ui_set_clip = guac_rdp_ui_set_clip;
274 rdp_inst->ui_reset_clip = guac_rdp_ui_reset_clip;
275 rdp_inst->ui_resize_window = guac_rdp_ui_resize_window;
276 rdp_inst->ui_set_cursor = guac_rdp_ui_set_cursor;
277 rdp_inst->ui_destroy_cursor = guac_rdp_ui_destroy_cursor;
278 rdp_inst->ui_create_cursor = guac_rdp_ui_create_cursor;
279 rdp_inst->ui_set_null_cursor = guac_rdp_ui_set_null_cursor;
280 rdp_inst->ui_set_default_cursor = guac_rdp_ui_set_default_cursor;
281 rdp_inst->ui_create_palette = guac_rdp_ui_create_palette;
282 rdp_inst->ui_move_pointer = guac_rdp_ui_move_pointer;
283 rdp_inst->ui_set_palette = guac_rdp_ui_set_palette;
284 rdp_inst->ui_create_surface = guac_rdp_ui_create_surface;
285 rdp_inst->ui_set_surface = guac_rdp_ui_set_surface;
286 rdp_inst->ui_destroy_surface = guac_rdp_ui_destroy_surface;
287 rdp_inst->ui_channel_data = guac_rdp_ui_channel_data;
289 /* Init chanman (pre-connect) */
290 if (freerdp_chanman_pre_connect(chanman, rdp_inst)) {
291 guac_protocol_send_error(client->socket, "Error initializing RDP client channel manager");
292 guac_socket_flush(client->socket);
296 /* Connect to RDP server */
297 if (rdp_inst->rdp_connect(rdp_inst)) {
298 guac_protocol_send_error(client->socket, "Error connecting to RDP server");
299 guac_socket_flush(client->socket);
303 /* Init chanman (post-connect) */
304 if (freerdp_chanman_post_connect(chanman, rdp_inst)) {
305 guac_protocol_send_error(client->socket, "Error initializing RDP client channel manager");
306 guac_socket_flush(client->socket);
310 /* Client handlers */
311 client->free_handler = rdp_guac_client_free_handler;
312 client->handle_messages = rdp_guac_client_handle_messages;
313 client->mouse_handler = rdp_guac_client_mouse_handler;
314 client->key_handler = rdp_guac_client_key_handler;
321 int rdp_guac_client_mouse_handler(guac_client* client, int x, int y, int mask) {
323 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
324 rdpInst* rdp_inst = guac_client_data->rdp_inst;
326 /* If button mask unchanged, just send move event */
327 if (mask == guac_client_data->mouse_button_mask)
328 rdp_inst->rdp_send_input_mouse(rdp_inst, PTRFLAGS_MOVE, x, y);
330 /* Otherwise, send events describing button change */
333 /* Mouse buttons which have JUST become released */
334 int released_mask = guac_client_data->mouse_button_mask & ~mask;
336 /* Mouse buttons which have JUST become pressed */
337 int pressed_mask = ~guac_client_data->mouse_button_mask & mask;
340 if (released_mask & 0x07) {
342 /* Calculate flags */
344 if (released_mask & 0x01) flags |= PTRFLAGS_BUTTON1;
345 if (released_mask & 0x02) flags |= PTRFLAGS_BUTTON3;
346 if (released_mask & 0x04) flags |= PTRFLAGS_BUTTON2;
348 rdp_inst->rdp_send_input_mouse(rdp_inst, flags, x, y);
353 if (pressed_mask & 0x07) {
355 /* Calculate flags */
356 int flags = PTRFLAGS_DOWN;
357 if (pressed_mask & 0x01) flags |= PTRFLAGS_BUTTON1;
358 if (pressed_mask & 0x02) flags |= PTRFLAGS_BUTTON3;
359 if (pressed_mask & 0x04) flags |= PTRFLAGS_BUTTON2;
360 if (pressed_mask & 0x08) flags |= PTRFLAGS_WHEEL | 0x78;
361 if (pressed_mask & 0x10) flags |= PTRFLAGS_WHEEL | PTRFLAGS_WHEEL_NEGATIVE | 0x88;
364 rdp_inst->rdp_send_input_mouse(rdp_inst, flags, x, y);
369 if (pressed_mask & 0x18) {
372 if (pressed_mask & 0x08)
373 rdp_inst->rdp_send_input_mouse(
375 PTRFLAGS_WHEEL | 0x78,
379 if (pressed_mask & 0x10)
380 rdp_inst->rdp_send_input_mouse(
382 PTRFLAGS_WHEEL | PTRFLAGS_WHEEL_NEGATIVE | 0x88,
388 guac_client_data->mouse_button_mask = mask;
394 int rdp_guac_client_key_handler(guac_client* client, int keysym, int pressed) {
396 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
397 rdpInst* rdp_inst = guac_client_data->rdp_inst;
399 /* If keysym can be in lookup table */
400 if (keysym <= 0xFFFF) {
402 /* Look up scancode */
403 const guac_rdp_keymap* keymap =
404 &guac_rdp_keysym_scancode[(keysym & 0xFF00) >> 8][keysym & 0xFF];
406 /* If defined, send event */
407 if (keymap->scancode != 0)
408 rdp_inst->rdp_send_input_scancode(
411 keymap->flags & KBDFLAGS_EXTENDED,
414 guac_client_log_info(client, "unmapped keysym: 0x%x", keysym);