2d1bc20c56eceb2711389bfca569bed47d7dfac7
[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     if (select(max_fd + 1, &rfds, &wfds, NULL, NULL) == -1) {
120         /* these are not really errors */
121         if (!((errno == EAGAIN) ||
122             (errno == EWOULDBLOCK) ||
123             (errno == EINPROGRESS) ||
124             (errno == EINTR))) /* signal occurred */
125         {
126             guac_client_log_error(client, "Error waiting for file descriptor.");
127             return 1;
128         }
129     }
130
131     /* Check the libfreerdp fds */
132     if (!freerdp_check_fds(rdp_inst)) {
133         guac_client_log_error(client, "Error handling RDP file descriptors.");
134         return 1;
135     }
136
137     /* Check channel fds */
138     if (!freerdp_channels_check_fds(channels, rdp_inst)) {
139         guac_client_log_error(client, "Error handling RDP channel file descriptors.");
140         return 1;
141     }
142
143     /* Success */
144     return 0;
145
146 }
147
148 int rdp_guac_client_mouse_handler(guac_client* client, int x, int y, int mask) {
149
150     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
151     freerdp* rdp_inst = guac_client_data->rdp_inst;
152
153     /* If button mask unchanged, just send move event */
154     if (mask == guac_client_data->mouse_button_mask)
155         rdp_inst->input->MouseEvent(rdp_inst->input, PTR_FLAGS_MOVE, x, y);
156
157     /* Otherwise, send events describing button change */
158     else {
159
160         /* Mouse buttons which have JUST become released */
161         int released_mask =  guac_client_data->mouse_button_mask & ~mask;
162
163         /* Mouse buttons which have JUST become pressed */
164         int pressed_mask  = ~guac_client_data->mouse_button_mask &  mask;
165
166         /* Release event */
167         if (released_mask & 0x07) {
168
169             /* Calculate flags */
170             int flags = 0;
171             if (released_mask & 0x01) flags |= PTR_FLAGS_BUTTON1;
172             if (released_mask & 0x02) flags |= PTR_FLAGS_BUTTON3;
173             if (released_mask & 0x04) flags |= PTR_FLAGS_BUTTON2;
174
175             rdp_inst->input->MouseEvent(rdp_inst->input, flags, x, y);
176
177         }
178
179         /* Press event */
180         if (pressed_mask & 0x07) {
181
182             /* Calculate flags */
183             int flags = PTR_FLAGS_DOWN;
184             if (pressed_mask & 0x01) flags |= PTR_FLAGS_BUTTON1;
185             if (pressed_mask & 0x02) flags |= PTR_FLAGS_BUTTON3;
186             if (pressed_mask & 0x04) flags |= PTR_FLAGS_BUTTON2;
187             if (pressed_mask & 0x08) flags |= PTR_FLAGS_WHEEL | 0x78;
188             if (pressed_mask & 0x10) flags |= PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | 0x88;
189
190             /* Send event */
191             rdp_inst->input->MouseEvent(rdp_inst->input, flags, x, y);
192
193         }
194
195         /* Scroll event */
196         if (pressed_mask & 0x18) {
197
198             /* Down */
199             if (pressed_mask & 0x08)
200                 rdp_inst->input->MouseEvent(
201                         rdp_inst->input,
202                         PTR_FLAGS_WHEEL | 0x78,
203                         x, y);
204
205             /* Up */
206             if (pressed_mask & 0x10)
207                 rdp_inst->input->MouseEvent(
208                         rdp_inst->input,
209                         PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | 0x88,
210                         x, y);
211
212         }
213
214
215         guac_client_data->mouse_button_mask = mask;
216     }
217
218     return 0;
219 }
220
221 int rdp_guac_client_key_handler(guac_client* client, int keysym, int pressed) {
222
223     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
224     freerdp* rdp_inst = guac_client_data->rdp_inst;
225
226     /* If keysym can be in lookup table */
227     if (keysym <= 0xFFFF) {
228
229         /* Look up scancode */
230         const guac_rdp_keymap* keymap = 
231             &guac_rdp_keysym_scancode[(keysym & 0xFF00) >> 8][keysym & 0xFF];
232
233         /* If defined, send event */
234         if (keymap->scancode != 0)
235             rdp_inst->input->KeyboardEvent(
236                     rdp_inst->input,
237                     keymap->flags
238                         | (pressed ? KBD_FLAGS_DOWN : KBD_FLAGS_RELEASE),
239                     keymap->scancode);
240         else
241             guac_client_log_info(client, "unmapped keysym: 0x%x", keysym);
242
243     }
244
245     return 0;
246 }
247