added timeout to select waiting on FreeRDP
[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  *
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/channels/channels.h>
46 #include <freerdp/input.h>
47
48 #include <guacamole/socket.h>
49 #include <guacamole/protocol.h>
50 #include <guacamole/client.h>
51
52 #include "client.h"
53 #include "rdp_keymap.h"
54 #include "guac_handlers.h"
55
56 int rdp_guac_client_free_handler(guac_client* client) {
57
58     /* STUB */
59
60     /* FIXME: Clean up RDP client + disconnect */
61
62     return 0;
63
64 }
65
66 int rdp_guac_client_handle_messages(guac_client* client) {
67
68     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
69     freerdp* rdp_inst = guac_client_data->rdp_inst;
70     rdpChannels* channels = rdp_inst->context->channels;
71
72     int index;
73     int max_fd, fd;
74     void* read_fds[32];
75     void* write_fds[32];
76     int read_count = 0;
77     int write_count = 0;
78
79     fd_set rfds, wfds;
80
81     /* get rdp fds */
82     if (!freerdp_get_fds(rdp_inst, read_fds, &read_count, write_fds, &write_count)) {
83         guac_client_log_error(client, "Unable to read RDP file descriptors.");
84         return 1;
85     }
86
87     /* get channel fds */
88     if (!freerdp_channels_get_fds(channels, rdp_inst, read_fds, &read_count, write_fds, &write_count)) {
89         guac_client_log_error(client, "Unable to read RDP channel file descriptors.");
90         return 1;
91     }
92
93     /* Construct read fd_set */
94     max_fd = 0;
95     FD_ZERO(&rfds);
96     for (index = 0; index < read_count; index++) {
97         fd = (int)(long) (read_fds[index]);
98         if (fd > max_fd)
99             max_fd = fd;
100         FD_SET(fd, &rfds);
101     }
102
103     /* Construct write fd_set */
104     FD_ZERO(&wfds);
105     for (index = 0; index < write_count; index++) {
106         fd = (int)(long) (write_fds[index]);
107         if (fd > max_fd)
108             max_fd = fd;
109         FD_SET(fd, &wfds);
110     }
111
112     /* If no file descriptors, error */
113     if (max_fd == 0) {
114         guac_client_log_error(client, "No file descriptors");
115         return 1;
116     }
117
118     /* Otherwise, wait for file descriptors given */
119         struct timeval timeout;
120         timeout.tv_sec = 0;
121         timeout.tv_usec = 250000;
122     if (select(max_fd + 1, &rfds, &wfds, NULL, &timeout) == -1) {
123         /* these are not really errors */
124         if (!((errno == EAGAIN) ||
125             (errno == EWOULDBLOCK) ||
126             (errno == EINPROGRESS) ||
127             (errno == EINTR))) /* signal occurred */
128         {
129             guac_client_log_error(client, "Error waiting for file descriptor.");
130             return 1;
131         }
132     }
133
134     /* Check the libfreerdp fds */
135     if (!freerdp_check_fds(rdp_inst)) {
136         guac_client_log_error(client, "Error handling RDP file descriptors.");
137         return 1;
138     }
139
140     /* Check channel fds */
141     if (!freerdp_channels_check_fds(channels, rdp_inst)) {
142         guac_client_log_error(client, "Error handling RDP channel file descriptors.");
143         return 1;
144     }
145
146     /* Success */
147     return 0;
148
149 }
150
151 int rdp_guac_client_mouse_handler(guac_client* client, int x, int y, int mask) {
152
153     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
154     freerdp* rdp_inst = guac_client_data->rdp_inst;
155
156     /* If button mask unchanged, just send move event */
157     if (mask == guac_client_data->mouse_button_mask)
158         rdp_inst->input->MouseEvent(rdp_inst->input, PTR_FLAGS_MOVE, x, y);
159
160     /* Otherwise, send events describing button change */
161     else {
162
163         /* Mouse buttons which have JUST become released */
164         int released_mask =  guac_client_data->mouse_button_mask & ~mask;
165
166         /* Mouse buttons which have JUST become pressed */
167         int pressed_mask  = ~guac_client_data->mouse_button_mask &  mask;
168
169         /* Release event */
170         if (released_mask & 0x07) {
171
172             /* Calculate flags */
173             int flags = 0;
174             if (released_mask & 0x01) flags |= PTR_FLAGS_BUTTON1;
175             if (released_mask & 0x02) flags |= PTR_FLAGS_BUTTON3;
176             if (released_mask & 0x04) flags |= PTR_FLAGS_BUTTON2;
177
178             rdp_inst->input->MouseEvent(rdp_inst->input, flags, x, y);
179
180         }
181
182         /* Press event */
183         if (pressed_mask & 0x07) {
184
185             /* Calculate flags */
186             int flags = PTR_FLAGS_DOWN;
187             if (pressed_mask & 0x01) flags |= PTR_FLAGS_BUTTON1;
188             if (pressed_mask & 0x02) flags |= PTR_FLAGS_BUTTON3;
189             if (pressed_mask & 0x04) flags |= PTR_FLAGS_BUTTON2;
190             if (pressed_mask & 0x08) flags |= PTR_FLAGS_WHEEL | 0x78;
191             if (pressed_mask & 0x10) flags |= PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | 0x88;
192
193             /* Send event */
194             rdp_inst->input->MouseEvent(rdp_inst->input, flags, x, y);
195
196         }
197
198         /* Scroll event */
199         if (pressed_mask & 0x18) {
200
201             /* Down */
202             if (pressed_mask & 0x08)
203                 rdp_inst->input->MouseEvent(
204                         rdp_inst->input,
205                         PTR_FLAGS_WHEEL | 0x78,
206                         x, y);
207
208             /* Up */
209             if (pressed_mask & 0x10)
210                 rdp_inst->input->MouseEvent(
211                         rdp_inst->input,
212                         PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | 0x88,
213                         x, y);
214
215         }
216
217
218         guac_client_data->mouse_button_mask = mask;
219     }
220
221     return 0;
222 }
223
224 int rdp_guac_client_key_handler(guac_client* client, int keysym, int pressed) {
225
226     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
227     freerdp* rdp_inst = guac_client_data->rdp_inst;
228
229     /* If keysym can be in lookup table */
230     if (keysym <= 0xFFFF) {
231
232         /* Look up scancode */
233         const guac_rdp_keymap* keymap = 
234             &guac_rdp_keysym_scancode[(keysym & 0xFF00) >> 8][keysym & 0xFF];
235
236         /* If defined, send event */
237         if (keymap->scancode != 0)
238             rdp_inst->input->KeyboardEvent(
239                     rdp_inst->input,
240                     keymap->flags
241                         | (pressed ? KBD_FLAGS_DOWN : KBD_FLAGS_RELEASE),
242                     keymap->scancode);
243         else
244             guac_client_log_info(client, "unmapped keysym: 0x%x", keysym);
245
246     }
247
248     return 0;
249 }
250