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 void rdp_freerdp_context_new(freerdp* instance, rdpContext* context) {
70 void rdp_freerdp_context_free(freerdp* instance, rdpContext* context) {
74 int guac_client_init(guac_client* client, int argc, char** argv) {
76 rdp_guac_client_data* guac_client_data;
79 rdpChannels* channels;
80 rdpSettings* settings;
84 int port = RDP_DEFAULT_PORT;
87 guac_protocol_send_error(client->socket, "Wrong argument count received.");
88 guac_socket_flush(client->socket);
92 /* If port specified, use it */
93 if (argv[1][0] != '\0')
98 /* Allocate client data */
99 guac_client_data = malloc(sizeof(rdp_guac_client_data));
101 /* Get channel manager */
102 channels = freerdp_channels_new();
103 guac_client_data->channels = channels;
106 settings = malloc(sizeof(rdpSettings));
107 memset(settings, 0, sizeof(rdpSettings));
108 guac_client_data->settings = settings;
111 strncpy(settings->hostname, hostname, sizeof(settings->hostname) - 1);
114 settings->width = 1024;
115 settings->height = 768;
117 strncpy(settings->window_title, hostname, sizeof(settings->window_title));
118 strcpy(settings->username, "guest");
120 /* FIXME: Set RDP settings->* */
123 rdp_inst = freerdp_new(settings);
124 if (rdp_inst == NULL) {
125 guac_protocol_send_error(client->socket, "Error initializing RDP client");
126 guac_socket_flush(client->socket);
129 guac_client_data->rdp_inst = rdp_inst;
130 guac_client_data->mouse_button_mask = 0;
131 guac_client_data->current_surface = GUAC_DEFAULT_LAYER;
133 /* Allocate FreeRDP context */
134 rdp_inst->context_size = sizeof(rdp_freerdp_context);
135 rdp_inst->ContextNew = (pContextNew) rdp_freerdp_context_new;
136 rdp_inst->ContextFree = (pContextFree) rdp_freerdp_context_free;
137 freerdp_context_new(rdp_inst);
139 /* Store client data */
140 ((rdp_freerdp_context*) rdp_inst->context)->client = client;
141 client->data = guac_client_data;
143 /* Set up bitmap handling */
144 bitmap = xnew(rdpBitmap);
145 bitmap->size = sizeof(guac_rdp_bitmap);
146 bitmap->New = guac_rdp_bitmap_new;
147 /* bitmap->Free = guac_rdp_bitmap_free; */
148 /* bitmap->Paint = guac_rdp_bitmap_paint; */
149 /* bitmap->Decompress = guac_rdp_bitmap_decompress; */
150 /* bitmap->SetSurface = guac_rdp_bitmap_setsurface; */
151 graphics_register_bitmap(rdp_inst->context->graphics, bitmap);
153 /* Init channels (pre-connect) */
154 if (freerdp_channels_pre_connect(channels, rdp_inst)) {
155 guac_protocol_send_error(client->socket, "Error initializing RDP client channel manager");
156 guac_socket_flush(client->socket);
160 /* Connect to RDP server */
161 if (!freerdp_connect(rdp_inst)) {
162 guac_protocol_send_error(client->socket, "Error connecting to RDP server");
163 guac_socket_flush(client->socket);
167 /* Init channels (post-connect) */
168 if (freerdp_channels_post_connect(channels, rdp_inst)) {
169 guac_protocol_send_error(client->socket, "Error initializing RDP client channel manager");
170 guac_socket_flush(client->socket);
174 /* Client handlers */
175 client->free_handler = rdp_guac_client_free_handler;
176 client->handle_messages = rdp_guac_client_handle_messages;
177 client->mouse_handler = rdp_guac_client_mouse_handler;
178 client->key_handler = rdp_guac_client_key_handler;