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_ui.h>
48 #include <guacamole/log.h>
49 #include <guacamole/guacio.h>
50 #include <guacamole/protocol.h>
51 #include <guacamole/client.h>
53 #include "rdp_handlers.h"
54 #include "rdp_client.h"
55 #include "rdp_keymap.h"
57 /* Client plugin arguments */
58 const char* GUAC_CLIENT_ARGS[] = {
64 int rdp_guac_client_free_handler(guac_client* client) {
66 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
68 /* Disconnect client */
69 guac_client_data->rdp_inst->rdp_disconnect(guac_client_data->rdp_inst);
72 freerdp_free(guac_client_data->rdp_inst);
73 freerdp_chanman_free(guac_client_data->chanman);
74 free(guac_client_data->settings);
76 /* Free guac client data */
77 free(guac_client_data);
83 int rdp_guac_client_handle_messages(guac_client* client) {
85 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
86 rdpInst* rdp_inst = guac_client_data->rdp_inst;
87 rdpChanMan* chanman = guac_client_data->chanman;
99 if (rdp_inst->rdp_get_fds(rdp_inst, read_fds, &read_count, write_fds, &write_count) != 0) {
100 guac_log_error("Unable to read RDP file descriptors.");
104 /* get channel fds */
105 if (freerdp_chanman_get_fds(chanman, rdp_inst, read_fds, &read_count, write_fds, &write_count) != 0) {
106 guac_log_error("Unable to read RDP channel file descriptors.");
110 /* Construct read fd_set */
113 for (index = 0; index < read_count; index++) {
114 fd = (int)(long) (read_fds[index]);
120 /* Construct write fd_set */
122 for (index = 0; index < write_count; index++) {
123 fd = (int)(long) (write_fds[index]);
129 /* If no file descriptors, error */
131 guac_log_error("No file descriptors");
135 /* Otherwise, wait for file descriptors given */
136 if (select(max_fd + 1, &rfds, &wfds, NULL, NULL) == -1) {
137 /* these are not really errors */
138 if (!((errno == EAGAIN) ||
139 (errno == EWOULDBLOCK) ||
140 (errno == EINPROGRESS) ||
141 (errno == EINTR))) /* signal occurred */
143 guac_log_error("Error waiting for file descriptor.");
148 /* check the libfreerdp fds */
149 if (rdp_inst->rdp_check_fds(rdp_inst) != 0) {
150 guac_log_error("Error handling RDP file descriptors.");
154 /* check channel fds */
155 if (freerdp_chanman_check_fds(chanman, rdp_inst) != 0) {
156 guac_log_error("Error handling RDP channel file descriptors.");
165 int guac_client_init(guac_client* client, int argc, char** argv) {
167 rdp_guac_client_data* guac_client_data;
174 int port = RDP_DEFAULT_PORT;
177 guac_send_error(client->io, "Wrong argument count received.");
178 guac_flush(client->io);
182 /* If port specified, use it */
183 if (argv[1][0] != '\0')
184 port = atoi(argv[1]);
188 /* Allocate client data */
189 guac_client_data = malloc(sizeof(rdp_guac_client_data));
191 /* Get channel manager */
192 chanman = freerdp_chanman_new();
193 guac_client_data->chanman = chanman;
196 settings = malloc(sizeof(rdpSet));
197 memset(settings, 0, sizeof(rdpSet));
198 guac_client_data->settings = settings;
201 strncpy(settings->hostname, hostname, sizeof(settings->hostname) - 1);
204 settings->width = 1024;
205 settings->height = 768;
207 strncpy(settings->server, hostname, sizeof(settings->server));
208 strcpy(settings->username, "guest");
210 settings->tcp_port_rdp = port;
211 settings->encryption = 1;
212 settings->server_depth = 16;
213 settings->bitmap_cache = 1;
214 settings->bitmap_compression = 1;
215 settings->desktop_save = 0;
216 settings->performanceflags =
217 PERF_DISABLE_WALLPAPER
218 | PERF_DISABLE_FULLWINDOWDRAG
219 | PERF_DISABLE_MENUANIMATIONS;
220 settings->off_screen_bitmaps = 1;
221 settings->triblt = 0;
222 settings->new_cursors = 1;
223 settings->rdp_version = 5;
226 rdp_inst = freerdp_new(settings);
227 if (rdp_inst == NULL) {
228 guac_send_error(client->io, "Error initializing RDP client");
229 guac_flush(client->io);
232 guac_client_data->rdp_inst = rdp_inst;
233 guac_client_data->mouse_button_mask = 0;
234 guac_client_data->current_surface = GUAC_DEFAULT_LAYER;
236 /* Store client data */
237 rdp_inst->param1 = client;
238 client->data = guac_client_data;
241 rdp_inst->ui_error = guac_rdp_ui_error;
242 rdp_inst->ui_warning = guac_rdp_ui_warning;
243 rdp_inst->ui_unimpl = guac_rdp_ui_unimpl;
244 rdp_inst->ui_begin_update = guac_rdp_ui_begin_update;
245 rdp_inst->ui_end_update = guac_rdp_ui_end_update;
246 rdp_inst->ui_desktop_save = guac_rdp_ui_desktop_save;
247 rdp_inst->ui_desktop_restore = guac_rdp_ui_desktop_restore;
248 rdp_inst->ui_create_bitmap = guac_rdp_ui_create_bitmap;
249 rdp_inst->ui_paint_bitmap = guac_rdp_ui_paint_bitmap;
250 rdp_inst->ui_destroy_bitmap = guac_rdp_ui_destroy_bitmap;
251 rdp_inst->ui_line = guac_rdp_ui_line;
252 rdp_inst->ui_rect = guac_rdp_ui_rect;
253 rdp_inst->ui_polygon = guac_rdp_ui_polygon;
254 rdp_inst->ui_polyline = guac_rdp_ui_polyline;
255 rdp_inst->ui_ellipse = guac_rdp_ui_ellipse;
256 rdp_inst->ui_start_draw_glyphs = guac_rdp_ui_start_draw_glyphs;
257 rdp_inst->ui_draw_glyph = guac_rdp_ui_draw_glyph;
258 rdp_inst->ui_end_draw_glyphs = guac_rdp_ui_end_draw_glyphs;
259 rdp_inst->ui_get_toggle_keys_state = guac_rdp_ui_get_toggle_keys_state;
260 rdp_inst->ui_bell = guac_rdp_ui_bell;
261 rdp_inst->ui_destblt = guac_rdp_ui_destblt;
262 rdp_inst->ui_patblt = guac_rdp_ui_patblt;
263 rdp_inst->ui_screenblt = guac_rdp_ui_screenblt;
264 rdp_inst->ui_memblt = guac_rdp_ui_memblt;
265 rdp_inst->ui_triblt = guac_rdp_ui_triblt;
266 rdp_inst->ui_create_glyph = guac_rdp_ui_create_glyph;
267 rdp_inst->ui_destroy_glyph = guac_rdp_ui_destroy_glyph;
268 rdp_inst->ui_select = guac_rdp_ui_select;
269 rdp_inst->ui_set_clip = guac_rdp_ui_set_clip;
270 rdp_inst->ui_reset_clip = guac_rdp_ui_reset_clip;
271 rdp_inst->ui_resize_window = guac_rdp_ui_resize_window;
272 rdp_inst->ui_set_cursor = guac_rdp_ui_set_cursor;
273 rdp_inst->ui_destroy_cursor = guac_rdp_ui_destroy_cursor;
274 rdp_inst->ui_create_cursor = guac_rdp_ui_create_cursor;
275 rdp_inst->ui_set_null_cursor = guac_rdp_ui_set_null_cursor;
276 rdp_inst->ui_set_default_cursor = guac_rdp_ui_set_default_cursor;
277 rdp_inst->ui_create_colormap = guac_rdp_ui_create_colormap;
278 rdp_inst->ui_move_pointer = guac_rdp_ui_move_pointer;
279 rdp_inst->ui_set_colormap = guac_rdp_ui_set_colormap;
280 rdp_inst->ui_create_surface = guac_rdp_ui_create_surface;
281 rdp_inst->ui_set_surface = guac_rdp_ui_set_surface;
282 rdp_inst->ui_destroy_surface = guac_rdp_ui_destroy_surface;
283 rdp_inst->ui_channel_data = guac_rdp_ui_channel_data;
285 /* Init chanman (pre-connect) */
286 if (freerdp_chanman_pre_connect(chanman, rdp_inst)) {
287 guac_send_error(client->io, "Error initializing RDP client channel manager");
288 guac_flush(client->io);
292 /* Connect to RDP server */
293 if (rdp_inst->rdp_connect(rdp_inst)) {
294 guac_send_error(client->io, "Error connecting to RDP server");
295 guac_flush(client->io);
299 /* Init chanman (post-connect) */
300 if (freerdp_chanman_post_connect(chanman, rdp_inst)) {
301 guac_send_error(client->io, "Error initializing RDP client channel manager");
302 guac_flush(client->io);
306 /* Client handlers */
307 client->free_handler = rdp_guac_client_free_handler;
308 client->handle_messages = rdp_guac_client_handle_messages;
309 client->mouse_handler = rdp_guac_client_mouse_handler;
310 client->key_handler = rdp_guac_client_key_handler;
317 int rdp_guac_client_mouse_handler(guac_client* client, int x, int y, int mask) {
319 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
320 rdpInst* rdp_inst = guac_client_data->rdp_inst;
322 /* If no buttons pressed */
325 /* Send mouse move event if no buttons pressed before */
326 if (guac_client_data->mouse_button_mask == 0)
327 rdp_inst->rdp_send_input(rdp_inst, RDP_INPUT_MOUSE, PTRFLAGS_MOVE, x, y);
329 /* Otherwise, send mouse up event, releasing any old buttons */
331 rdp_inst->rdp_send_input(rdp_inst, RDP_INPUT_MOUSE, PTRFLAGS_BUTTON1, x, y);
334 /* Otherwise, send mouse down event */
336 rdp_inst->rdp_send_input(rdp_inst, RDP_INPUT_MOUSE, PTRFLAGS_DOWN | PTRFLAGS_BUTTON1, x, y);
338 guac_client_data->mouse_button_mask = mask;
342 int rdp_guac_client_key_handler(guac_client* client, int keysym, int pressed) {
344 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
345 rdpInst* rdp_inst = guac_client_data->rdp_inst;
347 /* If keysym can be in lookup table */
348 if (keysym <= 0xFFFF) {
350 /* Look up scancode */
351 const guac_rdp_keymap* keymap =
352 &guac_rdp_keysym_scancode[(keysym & 0xFF00) >> 8][keysym & 0xFF];
354 /* If defined, send event */
355 if (keymap->scancode != 0)
356 rdp_inst->rdp_send_input(
357 rdp_inst, RDP_INPUT_SCANCODE,
358 pressed ? RDP_KEYPRESS : RDP_KEYRELEASE,