Init bitmap handlers, use xzalloc (part of libfreerdp-utils).
[libguac-client-rdp.git] / src / client.c
1
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
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/
9  *
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
13  * License.
14  *
15  * The Original Code is libguac-client-rdp.
16  *
17  * The Initial Developer of the Original Code is
18  * Michael Jumper.
19  * Portions created by the Initial Developer are Copyright (C) 2011
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *
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.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include <sys/select.h>
42 #include <errno.h>
43
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>
49
50 #include <guacamole/socket.h>
51 #include <guacamole/protocol.h>
52 #include <guacamole/client.h>
53
54 #include "client.h"
55 #include "guac_handlers.h"
56 #include "rdp_keymap.h"
57 #include "rdp_bitmap.h"
58
59 /* Client plugin arguments */
60 const char* GUAC_CLIENT_ARGS[] = {
61     "hostname",
62     "port",
63     NULL
64 };
65
66 void rdp_freerdp_context_new(freerdp* instance, rdpContext* context) {
67     /* EMPTY */
68 }
69
70 void rdp_freerdp_context_free(freerdp* instance, rdpContext* context) {
71     /* EMPTY */
72 }
73
74 int guac_client_init(guac_client* client, int argc, char** argv) {
75
76     rdp_guac_client_data* guac_client_data;
77
78     freerdp* rdp_inst;
79     rdpChannels* channels;
80         rdpSettings* settings;
81     rdpBitmap* bitmap;
82
83     char* hostname;
84     int port = RDP_DEFAULT_PORT;
85
86     if (argc < 2) {
87         guac_protocol_send_error(client->socket, "Wrong argument count received.");
88         guac_socket_flush(client->socket);
89         return 1;
90     }
91
92     /* If port specified, use it */
93     if (argv[1][0] != '\0')
94         port = atoi(argv[1]);
95
96     hostname = argv[0];
97
98     /* Allocate client data */
99     guac_client_data = malloc(sizeof(rdp_guac_client_data));
100
101     /* Get channel manager */
102     channels = freerdp_channels_new();
103     guac_client_data->channels = channels;
104
105     /* INIT SETTINGS */
106     settings = malloc(sizeof(rdpSettings));
107         memset(settings, 0, sizeof(rdpSettings));
108     guac_client_data->settings = settings;
109
110     /* Set hostname */
111     strncpy(settings->hostname, hostname, sizeof(settings->hostname) - 1);
112
113     /* Default size */
114         settings->width = 1024;
115         settings->height = 768;
116
117         strncpy(settings->window_title, hostname, sizeof(settings->window_title));
118         strcpy(settings->username, "guest");
119
120     /* FIXME: Set RDP settings->* */
121
122     /* Init client */
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);
127         return 1;
128     }
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;
132
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);
138
139     /* Store client data */
140     ((rdp_freerdp_context*) rdp_inst->context)->client = client;
141     client->data = guac_client_data;
142
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);
152
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);
157         return 1;
158     }
159
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);
164         return 1;
165     }
166
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);
171         return 1;
172     }
173
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;
179
180     /* Success */
181     return 0;
182
183 }
184