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