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/utils/memory.h>
46 #include <freerdp/cache/bitmap.h>
47 #include <freerdp/channels/channels.h>
48 #include <freerdp/input.h>
50 #include <guacamole/socket.h>
51 #include <guacamole/protocol.h>
52 #include <guacamole/client.h>
55 #include "guac_handlers.h"
56 #include "rdp_keymap.h"
57 #include "rdp_bitmap.h"
59 /* Client plugin arguments */
60 const char* GUAC_CLIENT_ARGS[] = {
66 boolean rdp_freerdp_pre_connect(freerdp* instance) {
68 rdpContext* context = instance->context;
69 guac_client* client = ((rdp_freerdp_context*) context)->client;
70 rdpChannels* channels = context->channels;
73 /* Set up bitmap handling */
74 bitmap = xnew(rdpBitmap);
75 bitmap->size = sizeof(guac_rdp_bitmap);
76 bitmap->New = guac_rdp_bitmap_new;
77 /* bitmap->Free = guac_rdp_bitmap_free; */
78 /* bitmap->Paint = guac_rdp_bitmap_paint; */
79 /* bitmap->Decompress = guac_rdp_bitmap_decompress; */
80 /* bitmap->SetSurface = guac_rdp_bitmap_setsurface; */
81 graphics_register_bitmap(context->graphics, bitmap);
83 /* Init channels (pre-connect) */
84 if (freerdp_channels_pre_connect(channels, instance)) {
85 guac_protocol_send_error(client->socket, "Error initializing RDP client channel manager");
86 guac_socket_flush(client->socket);
94 boolean rdp_freerdp_post_connect(freerdp* instance) {
96 rdpContext* context = instance->context;
97 guac_client* client = ((rdp_freerdp_context*) context)->client;
98 rdpChannels* channels = instance->context->channels;
100 /* Init channels (post-connect) */
101 if (freerdp_channels_post_connect(channels, instance)) {
102 guac_protocol_send_error(client->socket, "Error initializing RDP client channel manager");
103 guac_socket_flush(client->socket);
107 /* Client handlers */
108 client->free_handler = rdp_guac_client_free_handler;
109 client->handle_messages = rdp_guac_client_handle_messages;
110 client->mouse_handler = rdp_guac_client_mouse_handler;
111 client->key_handler = rdp_guac_client_key_handler;
117 void rdp_freerdp_context_new(freerdp* instance, rdpContext* context) {
118 context->channels = freerdp_channels_new();
121 void rdp_freerdp_context_free(freerdp* instance, rdpContext* context) {
125 int guac_client_init(guac_client* client, int argc, char** argv) {
127 rdp_guac_client_data* guac_client_data;
130 rdpSettings* settings;
133 int port = RDP_DEFAULT_PORT;
136 guac_protocol_send_error(client->socket, "Wrong argument count received.");
137 guac_socket_flush(client->socket);
141 /* If port specified, use it */
142 if (argv[1][0] != '\0')
143 port = atoi(argv[1]);
147 /* Allocate client data */
148 guac_client_data = malloc(sizeof(rdp_guac_client_data));
151 freerdp_channels_global_init();
152 rdp_inst = freerdp_new();
153 rdp_inst->PreConnect = rdp_freerdp_pre_connect;
154 rdp_inst->PostConnect = rdp_freerdp_post_connect;
156 /* Allocate FreeRDP context */
157 rdp_inst->context_size = sizeof(rdp_freerdp_context);
158 rdp_inst->ContextNew = (pContextNew) rdp_freerdp_context_new;
159 rdp_inst->ContextFree = (pContextFree) rdp_freerdp_context_free;
160 freerdp_context_new(rdp_inst);
163 settings = rdp_inst->settings;
166 settings->authentication = false;
169 settings->rdp_security = true;
170 settings->tls_security = false;
171 settings->nla_security = false;
172 settings->encryption = true;
173 settings->encryption_method = ENCRYPTION_METHOD_40BIT | ENCRYPTION_METHOD_128BIT | ENCRYPTION_METHOD_FIPS;
174 settings->encryption_level = ENCRYPTION_LEVEL_CLIENT_COMPATIBLE;
177 settings->width = 1024;
178 settings->height = 768;
181 settings->hostname = strdup(hostname);
182 settings->window_title = strdup(hostname);
183 settings->username = "guest";
185 /* Store client data */
186 guac_client_data->rdp_inst = rdp_inst;
187 guac_client_data->mouse_button_mask = 0;
188 guac_client_data->current_surface = GUAC_DEFAULT_LAYER;
190 ((rdp_freerdp_context*) rdp_inst->context)->client = client;
191 client->data = guac_client_data;
193 /* Connect to RDP server */
194 if (!freerdp_connect(rdp_inst)) {
195 guac_protocol_send_error(client->socket, "Error connecting to RDP server");
196 guac_socket_flush(client->socket);