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