Do not use addressof in lookup macro. Update keysym state in event handler.
[libguac-client-rdp.git] / src / guac_handlers.c
index 357b572..488af41 100644 (file)
@@ -20,6 +20,7 @@
  * the Initial Developer. All Rights Reserved.
  *
  * Contributor(s):
+ * Matt Hortman
  *
  * Alternatively, the contents of this file may be used under the terms of
  * either the GNU General Public License Version 2 or later (the "GPL"), or
 #include <guacamole/client.h>
 
 #include "client.h"
-#include "rdp_handlers.h"
 #include "rdp_keymap.h"
+#include "guac_handlers.h"
 
-int rdp_guac_client_free_handler(guac_client* client) {
-
-    rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
+void __guac_rdp_send_keysym_string(guac_client* client, const int* keysym_string, int pressed);
+int __guac_rdp_send_keysym(guac_client* client, int keysym, int pressed);
+void __guac_rdp_send_altcode(guac_client* client, const char* altcode);
 
-    /* Disconnect client */
-    guac_client_data->rdp_inst->rdp_disconnect(guac_client_data->rdp_inst);
+int rdp_guac_client_free_handler(guac_client* client) {
 
-    /* Free RDP client */
-    freerdp_free(guac_client_data->rdp_inst);
-    freerdp_channels_free(guac_client_data->channels);
-    free(guac_client_data->settings);
+    /* STUB */
 
-    /* Free guac client data */
-    free(guac_client_data);
+    /* FIXME: Clean up RDP client + disconnect */
 
     return 0;
 
@@ -75,8 +71,8 @@ int rdp_guac_client_free_handler(guac_client* client) {
 int rdp_guac_client_handle_messages(guac_client* client) {
 
     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
-    rdpInst* rdp_inst = guac_client_data->rdp_inst;
-    rdpChannels* channels = guac_client_data->channels;
+    freerdp* rdp_inst = guac_client_data->rdp_inst;
+    rdpChannels* channels = rdp_inst->context->channels;
 
     int index;
     int max_fd, fd;
@@ -84,17 +80,21 @@ int rdp_guac_client_handle_messages(guac_client* client) {
     void* write_fds[32];
     int read_count = 0;
     int write_count = 0;
-
     fd_set rfds, wfds;
 
+    struct timeval timeout = {
+        .tv_sec = 0,
+        .tv_usec = 250000
+    };
+
     /* get rdp fds */
-    if (rdp_inst->rdp_get_fds(rdp_inst, read_fds, &read_count, write_fds, &write_count) != 0) {
+    if (!freerdp_get_fds(rdp_inst, read_fds, &read_count, write_fds, &write_count)) {
         guac_client_log_error(client, "Unable to read RDP file descriptors.");
         return 1;
     }
 
     /* get channel fds */
-    if (freerdp_channels_get_fds(channels, rdp_inst, read_fds, &read_count, write_fds, &write_count) != 0) {
+    if (!freerdp_channels_get_fds(channels, rdp_inst, read_fds, &read_count, write_fds, &write_count)) {
         guac_client_log_error(client, "Unable to read RDP channel file descriptors.");
         return 1;
     }
@@ -125,7 +125,7 @@ int rdp_guac_client_handle_messages(guac_client* client) {
     }
 
     /* Otherwise, wait for file descriptors given */
-    if (select(max_fd + 1, &rfds, &wfds, NULL, NULL) == -1) {
+    if (select(max_fd + 1, &rfds, &wfds, NULL, &timeout) == -1) {
         /* these are not really errors */
         if (!((errno == EAGAIN) ||
             (errno == EWOULDBLOCK) ||
@@ -137,14 +137,14 @@ int rdp_guac_client_handle_messages(guac_client* client) {
         }
     }
 
-    /* check the libfreerdp fds */
-    if (rdp_inst->rdp_check_fds(rdp_inst) != 0) {
+    /* Check the libfreerdp fds */
+    if (!freerdp_check_fds(rdp_inst)) {
         guac_client_log_error(client, "Error handling RDP file descriptors.");
         return 1;
     }
 
-    /* check channel fds */
-    if (freerdp_channels_check_fds(channels, rdp_inst) != 0) {
+    /* Check channel fds */
+    if (!freerdp_channels_check_fds(channels, rdp_inst)) {
         guac_client_log_error(client, "Error handling RDP channel file descriptors.");
         return 1;
     }
@@ -157,11 +157,11 @@ int rdp_guac_client_handle_messages(guac_client* client) {
 int rdp_guac_client_mouse_handler(guac_client* client, int x, int y, int mask) {
 
     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
-    rdpInst* rdp_inst = guac_client_data->rdp_inst;
+    freerdp* rdp_inst = guac_client_data->rdp_inst;
 
     /* If button mask unchanged, just send move event */
     if (mask == guac_client_data->mouse_button_mask)
-        rdp_inst->rdp_send_input_mouse(rdp_inst, PTR_FLAGS_MOVE, x, y);
+        rdp_inst->input->MouseEvent(rdp_inst->input, PTR_FLAGS_MOVE, x, y);
 
     /* Otherwise, send events describing button change */
     else {
@@ -181,7 +181,7 @@ int rdp_guac_client_mouse_handler(guac_client* client, int x, int y, int mask) {
             if (released_mask & 0x02) flags |= PTR_FLAGS_BUTTON3;
             if (released_mask & 0x04) flags |= PTR_FLAGS_BUTTON2;
 
-            rdp_inst->rdp_send_input_mouse(rdp_inst, flags, x, y);
+            rdp_inst->input->MouseEvent(rdp_inst->input, flags, x, y);
 
         }
 
@@ -197,7 +197,7 @@ int rdp_guac_client_mouse_handler(guac_client* client, int x, int y, int mask) {
             if (pressed_mask & 0x10) flags |= PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | 0x88;
 
             /* Send event */
-            rdp_inst->rdp_send_input_mouse(rdp_inst, flags, x, y);
+            rdp_inst->input->MouseEvent(rdp_inst->input, flags, x, y);
 
         }
 
@@ -206,15 +206,15 @@ int rdp_guac_client_mouse_handler(guac_client* client, int x, int y, int mask) {
 
             /* Down */
             if (pressed_mask & 0x08)
-                rdp_inst->rdp_send_input_mouse(
-                        rdp_inst,
+                rdp_inst->input->MouseEvent(
+                        rdp_inst->input,
                         PTR_FLAGS_WHEEL | 0x78,
                         x, y);
 
             /* Up */
             if (pressed_mask & 0x10)
-                rdp_inst->rdp_send_input_mouse(
-                        rdp_inst,
+                rdp_inst->input->MouseEvent(
+                        rdp_inst->input,
                         PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | 0x88,
                         x, y);
 
@@ -227,30 +227,108 @@ int rdp_guac_client_mouse_handler(guac_client* client, int x, int y, int mask) {
     return 0;
 }
 
-int rdp_guac_client_key_handler(guac_client* client, int keysym, int pressed) {
+void __guac_rdp_send_altcode(guac_client* client, const char* altcode) {
+
+    rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
+    freerdp* rdp_inst = guac_client_data->rdp_inst;
+    const guac_rdp_keysym_scancode_map* keysym_scancodes = guac_client_data->keysym_scancodes;
+
+    /* Lookup scancode for Alt */
+    int alt = GUAC_RDP_KEYSYM_LOOKUP(*keysym_scancodes, 0xFFE9 /* Alt_L */).scancode;
+
+    /* Press Alt */
+    rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_DOWN, alt);
+
+    /* For each character in Alt-code ... */
+    while (*altcode != '\0') {
+
+        /* Get scancode of keypad digit */
+        int scancode = GUAC_RDP_KEYSYM_LOOKUP(*keysym_scancodes, 0xFFB0 + (*altcode) - '0').scancode;
+
+        /* Press and release digit */
+        rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_DOWN, scancode);
+        rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_RELEASE, scancode);
+
+        /* Next character */
+        altcode++;
+    }
+
+    /* Release Alt */
+    rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_RELEASE, alt);
+
+}
+
+int __guac_rdp_send_keysym(guac_client* client, int keysym, int pressed) {
 
     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
-    rdpInst* rdp_inst = guac_client_data->rdp_inst;
+    freerdp* rdp_inst = guac_client_data->rdp_inst;
+    const guac_rdp_keysym_scancode_map* keysym_scancodes = guac_client_data->keysym_scancodes;
 
     /* If keysym can be in lookup table */
     if (keysym <= 0xFFFF) {
 
-        /* Look up scancode */
-        const guac_rdp_keymap* keymap = 
-            &guac_rdp_keysym_scancode[(keysym & 0xFF00) >> 8][keysym & 0xFF];
+        /* Look up scancode mapping */
+        const guac_rdp_scancode_map* scancode_map = &GUAC_RDP_KEYSYM_LOOKUP(*keysym_scancodes, keysym);
 
         /* If defined, send event */
-        if (keymap->scancode != 0)
-            rdp_inst->rdp_send_input_scancode(
-                    rdp_inst,
-                    !pressed,
-                    keymap->flags & KBD_FLAGS_EXTENDED,
-                    keymap->scancode);
-        else
-            guac_client_log_info(client, "unmapped keysym: 0x%x", keysym);
+        if (scancode_map->scancode != 0) {
+
+            /* If defined, send any prerequesite keys */
+            if (scancode_map->set_keysyms != NULL)
+                __guac_rdp_send_keysym_string(client, scancode_map->set_keysyms, 1);
+
+            /* Send actual key */
+            rdp_inst->input->KeyboardEvent(
+                    rdp_inst->input,
+                    scancode_map->flags
+                        | (pressed ? KBD_FLAGS_DOWN : KBD_FLAGS_RELEASE),
+                    scancode_map->scancode);
+
+            /* If defined, release any prerequesite keys */
+            if (scancode_map->set_keysyms != NULL)
+                __guac_rdp_send_keysym_string(client, scancode_map->set_keysyms, 0);
+
+        }
+
+        /* If undefined, try to type using Alt-code */
+        else {
+
+            const guac_rdp_altcode_map* altcode_map = &GUAC_RDP_KEYSYM_LOOKUP(guac_rdp_keysym_altcode, keysym);
+            if (altcode_map->altcode != NULL) {
+
+                /* Only send Alt-code on press */
+                if (pressed)
+                    __guac_rdp_send_altcode(client, altcode_map->altcode);
+
+            }
+
+            /* If no defined Alt-code, log warning */
+            else
+                guac_client_log_info(client, "unmapped keysym: 0x%x", keysym);
+
+        }
 
     }
 
     return 0;
 }
 
+void __guac_rdp_send_keysym_string(guac_client* client, const int* keysym_string, int pressed) {
+
+    /* Send all keysyms in string, NULL terminated */
+    while (*keysym_string != 0)
+        __guac_rdp_send_keysym(client, *(keysym_string++), pressed);
+
+}
+
+int rdp_guac_client_key_handler(guac_client* client, int keysym, int pressed) {
+
+    rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
+
+    /* Update keysym state */
+    GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keysym_state, keysym) = pressed;
+
+    return __guac_rdp_send_keysym(client, keysym, pressed);
+
+}
+