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