Fix memory leaks.
[libguac-client-rdp.git] / src / guac_handlers.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  * Matt Hortman
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include <sys/select.h>
43 #include <errno.h>
44
45 #include <freerdp/freerdp.h>
46 #include <freerdp/channels/channels.h>
47 #include <freerdp/input.h>
48 #include <freerdp/codec/color.h>
49 #include <freerdp/cache/cache.h>
50
51 #include <guacamole/socket.h>
52 #include <guacamole/protocol.h>
53 #include <guacamole/client.h>
54
55 #include "client.h"
56 #include "rdp_keymap.h"
57 #include "guac_handlers.h"
58
59 void __guac_rdp_update_keysyms(guac_client* client, const int* keysym_string, int from, int to);
60 int __guac_rdp_send_keysym(guac_client* client, int keysym, int pressed);
61 void __guac_rdp_send_altcode(guac_client* client, int altcode);
62
63
64 int rdp_guac_client_free_handler(guac_client* client) {
65
66     rdp_guac_client_data* guac_client_data =
67         (rdp_guac_client_data*) client->data;
68
69     freerdp* rdp_inst = guac_client_data->rdp_inst;
70     rdpChannels* channels = rdp_inst->context->channels;
71
72     /* Clean up RDP client */
73         freerdp_channels_close(channels, rdp_inst);
74         freerdp_channels_free(channels);
75         freerdp_disconnect(rdp_inst);
76     freerdp_clrconv_free(((rdp_freerdp_context*) rdp_inst->context)->clrconv);
77     cache_free(rdp_inst->context->cache);
78     freerdp_free(rdp_inst);
79
80     /* Free client data */
81     free(guac_client_data);
82
83     return 0;
84
85 }
86
87 int rdp_guac_client_handle_messages(guac_client* client) {
88
89     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
90     freerdp* rdp_inst = guac_client_data->rdp_inst;
91     rdpChannels* channels = rdp_inst->context->channels;
92
93     int index;
94     int max_fd, fd;
95     void* read_fds[32];
96     void* write_fds[32];
97     int read_count = 0;
98     int write_count = 0;
99     fd_set rfds, wfds;
100
101     struct timeval timeout = {
102         .tv_sec = 0,
103         .tv_usec = 250000
104     };
105
106     /* get rdp fds */
107     if (!freerdp_get_fds(rdp_inst, read_fds, &read_count, write_fds, &write_count)) {
108         guac_client_log_error(client, "Unable to read RDP file descriptors.");
109         return 1;
110     }
111
112     /* get channel fds */
113     if (!freerdp_channels_get_fds(channels, rdp_inst, read_fds, &read_count, write_fds, &write_count)) {
114         guac_client_log_error(client, "Unable to read RDP channel file descriptors.");
115         return 1;
116     }
117
118     /* Construct read fd_set */
119     max_fd = 0;
120     FD_ZERO(&rfds);
121     for (index = 0; index < read_count; index++) {
122         fd = (int)(long) (read_fds[index]);
123         if (fd > max_fd)
124             max_fd = fd;
125         FD_SET(fd, &rfds);
126     }
127
128     /* Construct write fd_set */
129     FD_ZERO(&wfds);
130     for (index = 0; index < write_count; index++) {
131         fd = (int)(long) (write_fds[index]);
132         if (fd > max_fd)
133             max_fd = fd;
134         FD_SET(fd, &wfds);
135     }
136
137     /* If no file descriptors, error */
138     if (max_fd == 0) {
139         guac_client_log_error(client, "No file descriptors");
140         return 1;
141     }
142
143     /* Otherwise, wait for file descriptors given */
144     if (select(max_fd + 1, &rfds, &wfds, NULL, &timeout) == -1) {
145         /* these are not really errors */
146         if (!((errno == EAGAIN) ||
147             (errno == EWOULDBLOCK) ||
148             (errno == EINPROGRESS) ||
149             (errno == EINTR))) /* signal occurred */
150         {
151             guac_client_log_error(client, "Error waiting for file descriptor.");
152             return 1;
153         }
154     }
155
156     /* Check the libfreerdp fds */
157     if (!freerdp_check_fds(rdp_inst)) {
158         guac_client_log_error(client, "Error handling RDP file descriptors.");
159         return 1;
160     }
161
162     /* Check channel fds */
163     if (!freerdp_channels_check_fds(channels, rdp_inst)) {
164         guac_client_log_error(client, "Error handling RDP channel file descriptors.");
165         return 1;
166     }
167
168     /* Success */
169     return 0;
170
171 }
172
173 int rdp_guac_client_mouse_handler(guac_client* client, int x, int y, int mask) {
174
175     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
176     freerdp* rdp_inst = guac_client_data->rdp_inst;
177
178     /* If button mask unchanged, just send move event */
179     if (mask == guac_client_data->mouse_button_mask)
180         rdp_inst->input->MouseEvent(rdp_inst->input, PTR_FLAGS_MOVE, x, y);
181
182     /* Otherwise, send events describing button change */
183     else {
184
185         /* Mouse buttons which have JUST become released */
186         int released_mask =  guac_client_data->mouse_button_mask & ~mask;
187
188         /* Mouse buttons which have JUST become pressed */
189         int pressed_mask  = ~guac_client_data->mouse_button_mask &  mask;
190
191         /* Release event */
192         if (released_mask & 0x07) {
193
194             /* Calculate flags */
195             int flags = 0;
196             if (released_mask & 0x01) flags |= PTR_FLAGS_BUTTON1;
197             if (released_mask & 0x02) flags |= PTR_FLAGS_BUTTON3;
198             if (released_mask & 0x04) flags |= PTR_FLAGS_BUTTON2;
199
200             rdp_inst->input->MouseEvent(rdp_inst->input, flags, x, y);
201
202         }
203
204         /* Press event */
205         if (pressed_mask & 0x07) {
206
207             /* Calculate flags */
208             int flags = PTR_FLAGS_DOWN;
209             if (pressed_mask & 0x01) flags |= PTR_FLAGS_BUTTON1;
210             if (pressed_mask & 0x02) flags |= PTR_FLAGS_BUTTON3;
211             if (pressed_mask & 0x04) flags |= PTR_FLAGS_BUTTON2;
212             if (pressed_mask & 0x08) flags |= PTR_FLAGS_WHEEL | 0x78;
213             if (pressed_mask & 0x10) flags |= PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | 0x88;
214
215             /* Send event */
216             rdp_inst->input->MouseEvent(rdp_inst->input, flags, x, y);
217
218         }
219
220         /* Scroll event */
221         if (pressed_mask & 0x18) {
222
223             /* Down */
224             if (pressed_mask & 0x08)
225                 rdp_inst->input->MouseEvent(
226                         rdp_inst->input,
227                         PTR_FLAGS_WHEEL | 0x78,
228                         x, y);
229
230             /* Up */
231             if (pressed_mask & 0x10)
232                 rdp_inst->input->MouseEvent(
233                         rdp_inst->input,
234                         PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | 0x88,
235                         x, y);
236
237         }
238
239
240         guac_client_data->mouse_button_mask = mask;
241     }
242
243     return 0;
244 }
245
246 void __guac_rdp_send_altcode(guac_client* client, int altcode) {
247
248     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
249     freerdp* rdp_inst = guac_client_data->rdp_inst;
250     int i;
251
252     /* Lookup scancode for Alt */
253     int alt = GUAC_RDP_KEYSYM_LOOKUP(
254             guac_client_data->keymap,
255             0xFFE9 /* Alt_L */).scancode;
256
257     /* Release all pressed modifiers */
258     __guac_rdp_update_keysyms(client, GUAC_KEYSYMS_ALL_MODIFIERS, 1, 0);
259
260     /* Press Alt */
261     rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_DOWN, alt);
262
263     /* For each character in four-digit Alt-code ... */
264     for (i=0; i<4; i++) {
265
266         /* Get scancode of keypad digit */
267         int scancode = GUAC_RDP_KEYSYM_LOOKUP(
268                 guac_client_data->keymap,
269                 0xFFB0 + (altcode / 1000)
270         ).scancode;
271
272         /* Press and release digit */
273         rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_DOWN, scancode);
274         rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_RELEASE, scancode);
275
276         /* Shift digits left by one place */
277         altcode = (altcode * 10) % 10000;
278
279     }
280
281     /* Release Alt */
282     rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_RELEASE, alt);
283
284     /* Press all originally pressed modifiers */
285     __guac_rdp_update_keysyms(client, GUAC_KEYSYMS_ALL_MODIFIERS, 1, 1);
286
287 }
288
289 int __guac_rdp_send_keysym(guac_client* client, int keysym, int pressed) {
290
291     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
292     freerdp* rdp_inst = guac_client_data->rdp_inst;
293
294     /* If keysym can be in lookup table */
295     if (keysym <= 0xFFFF) {
296
297         /* Look up scancode mapping */
298         const guac_rdp_keysym_desc* keysym_desc =
299             &GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keymap, keysym);
300
301         /* If defined, send event */
302         if (keysym_desc->scancode != 0) {
303
304             /* If defined, send any prerequesite keys that must be set */
305             if (keysym_desc->set_keysyms != NULL)
306                 __guac_rdp_update_keysyms(client, keysym_desc->set_keysyms, 0, 1);
307
308             /* If defined, release any keys that must be cleared */
309             if (keysym_desc->clear_keysyms != NULL)
310                 __guac_rdp_update_keysyms(client, keysym_desc->clear_keysyms, 1, 0);
311
312             /* Send actual key */
313             rdp_inst->input->KeyboardEvent(
314                     rdp_inst->input,
315                     keysym_desc->flags
316                         | (pressed ? KBD_FLAGS_DOWN : KBD_FLAGS_RELEASE),
317                     keysym_desc->scancode);
318
319             /* If defined, release any keys that were originally released */
320             if (keysym_desc->set_keysyms != NULL)
321                 __guac_rdp_update_keysyms(client, keysym_desc->set_keysyms, 0, 0);
322
323             /* If defined, send any keys that were originally set */
324             if (keysym_desc->clear_keysyms != NULL)
325                 __guac_rdp_update_keysyms(client, keysym_desc->clear_keysyms, 1, 1);
326
327         }
328
329         /* If undefined but has Alt-code, use Alt-Code */
330         else if (keysym <= 0xFF) {
331
332             /* NOTE: The Alt-codes are conveniently identical to keysyms. */
333
334             /* Only send Alt-code on press */
335             if (pressed)
336                 __guac_rdp_send_altcode(client, keysym);
337
338         }
339
340         /* If no defined Alt-code, log warning */
341         else
342             guac_client_log_info(client, "unmapped keysym: 0x%x", keysym);
343
344     }
345
346     return 0;
347 }
348
349 void __guac_rdp_update_keysyms(guac_client* client, const int* keysym_string, int from, int to) {
350
351     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
352     int keysym;
353
354     /* Send all keysyms in string, NULL terminated */
355     while ((keysym = *keysym_string) != 0) {
356
357         /* Get current keysym state */
358         int current_state = GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keysym_state, keysym);
359
360         /* If key is currently in given state, send event for changing it to specified "to" state */
361         if (current_state == from)
362             __guac_rdp_send_keysym(client, *keysym_string, to);
363
364         /* Next keysym */
365         keysym_string++;
366
367     }
368
369 }
370
371 int rdp_guac_client_key_handler(guac_client* client, int keysym, int pressed) {
372
373     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
374
375     /* Update keysym state */
376     GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keysym_state, keysym) = pressed;
377
378     return __guac_rdp_send_keysym(client, keysym, pressed);
379
380 }
381