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