First working implementation with special characters handled by unicodekeyboardevents.
[libguac-client-rdp.git] / src / guac_handlers.c
index b95ad7f..30aeae2 100644 (file)
 #include <freerdp/freerdp.h>
 #include <freerdp/channels/channels.h>
 #include <freerdp/input.h>
+#include <freerdp/codec/color.h>
+#include <freerdp/cache/cache.h>
+#include <freerdp/utils/event.h>
+#include <freerdp/plugins/cliprdr.h>
 
 #include <guacamole/socket.h>
 #include <guacamole/protocol.h>
 #include <guacamole/client.h>
+#include <guacamole/error.h>
 
 #include "client.h"
 #include "rdp_keymap.h"
+#include "rdp_cliprdr.h"
 #include "guac_handlers.h"
+#include "unicode_convtable.h"
+
+void __guac_rdp_update_keysyms(guac_client* client, const int* keysym_string, int from, int to);
+int __guac_rdp_send_keysym(guac_client* client, int keysym, int pressed);
+void __guac_rdp_send_altcode(guac_client* client, int altcode);
+
 
 int rdp_guac_client_free_handler(guac_client* client) {
 
-    /* STUB */
+    rdp_guac_client_data* guac_client_data =
+        (rdp_guac_client_data*) client->data;
+
+    freerdp* rdp_inst = guac_client_data->rdp_inst;
+    rdpChannels* channels = rdp_inst->context->channels;
+
+    /* Clean up RDP client */
+       freerdp_channels_close(channels, rdp_inst);
+       freerdp_channels_free(channels);
+       freerdp_disconnect(rdp_inst);
+    freerdp_clrconv_free(((rdp_freerdp_context*) rdp_inst->context)->clrconv);
+    cache_free(rdp_inst->context->cache);
+    freerdp_free(rdp_inst);
 
-    /* FIXME: Clean up RDP client + disconnect */
+    /* Free client data */
+    cairo_surface_destroy(guac_client_data->opaque_glyph_surface);
+    cairo_surface_destroy(guac_client_data->trans_glyph_surface);
+    free(guac_client_data->clipboard);
+    free(guac_client_data);
 
     return 0;
 
@@ -77,6 +105,7 @@ int rdp_guac_client_handle_messages(guac_client* client) {
     int read_count = 0;
     int write_count = 0;
     fd_set rfds, wfds;
+    RDP_EVENT* event;
 
     struct timeval timeout = {
         .tv_sec = 0,
@@ -85,13 +114,15 @@ int rdp_guac_client_handle_messages(guac_client* client) {
 
     /* get rdp fds */
     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.");
+        guac_error = GUAC_STATUS_BAD_STATE;
+        guac_error_message = "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)) {
-        guac_client_log_error(client, "Unable to read RDP channel file descriptors.");
+        guac_error = GUAC_STATUS_BAD_STATE;
+        guac_error_message = "Unable to read RDP channel file descriptors";
         return 1;
     }
 
@@ -116,7 +147,8 @@ int rdp_guac_client_handle_messages(guac_client* client) {
 
     /* If no file descriptors, error */
     if (max_fd == 0) {
-        guac_client_log_error(client, "No file descriptors");
+        guac_error = GUAC_STATUS_BAD_STATE;
+        guac_error_message = "No file descriptors";
         return 1;
     }
 
@@ -128,20 +160,42 @@ int rdp_guac_client_handle_messages(guac_client* client) {
             (errno == EINPROGRESS) ||
             (errno == EINTR))) /* signal occurred */
         {
-            guac_client_log_error(client, "Error waiting for file descriptor.");
+            guac_error = GUAC_STATUS_SEE_ERRNO;
+            guac_error_message = "Error waiting for file descriptor";
             return 1;
         }
     }
 
     /* Check the libfreerdp fds */
     if (!freerdp_check_fds(rdp_inst)) {
-        guac_client_log_error(client, "Error handling RDP file descriptors.");
+        guac_error = GUAC_STATUS_BAD_STATE;
+        guac_error_message = "Error handling RDP file descriptors";
         return 1;
     }
 
     /* Check channel fds */
     if (!freerdp_channels_check_fds(channels, rdp_inst)) {
-        guac_client_log_error(client, "Error handling RDP channel file descriptors.");
+        guac_error = GUAC_STATUS_BAD_STATE;
+        guac_error_message = "Error handling RDP channel file descriptors";
+        return 1;
+    }
+
+    /* Check for channel events */
+    event = freerdp_channels_pop_event(channels);
+    if (event) {
+
+        /* Handle clipboard events */
+        if (event->event_class == RDP_EVENT_CLASS_CLIPRDR)
+            guac_rdp_process_cliprdr_event(client, event);
+
+        freerdp_event_free(event);
+
+    }
+
+    /* Handle RDP disconnect */
+    if (freerdp_shall_disconnect(rdp_inst)) {
+        guac_error = GUAC_STATUS_NO_INPUT;
+        guac_error_message = "RDP server closed connection";
         return 1;
     }
 
@@ -223,80 +277,185 @@ int rdp_guac_client_mouse_handler(guac_client* client, int x, int y, int mask) {
     return 0;
 }
 
-void __guac_rdp_send_altcode(guac_client* client, const char* altcode) {
+void __guac_rdp_send_altcode(guac_client* client, int 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;
+    int i;
 
     /* Lookup scancode for Alt */
-    int alt = GUAC_RDP_KEYSYM_LOOKUP(*keysym_scancodes, 0xFFE9 /* Alt_L */)->scancode;
-    guac_client_log_info(client, "ALTCODE: alt=%i", alt);
+    int alt = GUAC_RDP_KEYSYM_LOOKUP(
+            guac_client_data->keymap,
+            0xFFE9 /* Alt_L */).scancode;
+
+    /* Release all pressed modifiers */
+    __guac_rdp_update_keysyms(client, GUAC_KEYSYMS_ALL_MODIFIERS, 1, 0);
 
     /* Press Alt */
     rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_DOWN, alt);
 
-    /* For each character in Alt-code ... */
-    while (*altcode != '\0') {
+    /* For each character in four-digit Alt-code ... */
+    for (i=0; i<4; i++) {
 
         /* Get scancode of keypad digit */
-        int scancode = GUAC_RDP_KEYSYM_LOOKUP(*keysym_scancodes, 0xFFB0 + (*altcode) - '0')->scancode;
+        int scancode = GUAC_RDP_KEYSYM_LOOKUP(
+                guac_client_data->keymap,
+                0xFFB0 + (altcode / 1000)
+        ).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);
-        guac_client_log_info(client, "ALTCODE: scan=%i", scancode);
 
-        /* Next character */
-        altcode++;
+        /* Shift digits left by one place */
+        altcode = (altcode * 10) % 10000;
+
     }
 
     /* Release Alt */
     rdp_inst->input->KeyboardEvent(rdp_inst->input, KBD_FLAGS_RELEASE, alt);
-    guac_client_log_info(client, "ALTCODE: done");
+
+    /* Press all originally pressed modifiers */
+    __guac_rdp_update_keysyms(client, GUAC_KEYSYMS_ALL_MODIFIERS, 1, 1);
 
 }
 
-int rdp_guac_client_key_handler(guac_client* client, int keysym, int pressed) {
+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;
     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 mapping */
-        const guac_rdp_scancode_map* scancode_map = GUAC_RDP_KEYSYM_LOOKUP(*keysym_scancodes, keysym);
+        const guac_rdp_keysym_desc* keysym_desc =
+            &GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keymap, keysym);
 
         /* If defined, send event */
-        if (scancode_map->scancode != 0)
+        if (keysym_desc->scancode != 0) {
+
+            /* If defined, send any prerequesite keys that must be set */
+            if (keysym_desc->set_keysyms != NULL)
+                __guac_rdp_update_keysyms(client, keysym_desc->set_keysyms, 0, 1);
+
+            /* If defined, release any keys that must be cleared */
+            if (keysym_desc->clear_keysyms != NULL)
+                __guac_rdp_update_keysyms(client, keysym_desc->clear_keysyms, 1, 0);
+
+            /* Send actual key */
             rdp_inst->input->KeyboardEvent(
+                                       rdp_inst->input,
+                    keysym_desc->flags
+                                           | (pressed ? KBD_FLAGS_DOWN : KBD_FLAGS_RELEASE),
+                                       keysym_desc->scancode);
+
+                       guac_client_log_info(client, "Base flags are %d", keysym_desc->flags);
+
+            /* If defined, release any keys that were originally released */
+            if (keysym_desc->set_keysyms != NULL)
+                __guac_rdp_update_keysyms(client, keysym_desc->set_keysyms, 0, 0);
+
+            /* If defined, send any keys that were originally set */
+            if (keysym_desc->clear_keysyms != NULL)
+                __guac_rdp_update_keysyms(client, keysym_desc->clear_keysyms, 1, 1);
+
+
+
+        /* /\* If undefined but has Alt-code, use Alt-Code *\/ */
+        /* else if (keysym <= 0xFF) { */
+
+        /*     /\* NOTE: The Alt-codes are conveniently identical to keysyms. *\/ */
+
+        /*     /\* Only send Alt-code on press *\/ */
+        /*     if (pressed) */
+        /*         __guac_rdp_send_altcode(client, keysym); */
+
+        /* } */
+
+        /* /\* If no defined Alt-code, log warning *\/ */
+        /* else */
+        /*     guac_client_log_info(client, "unmapped keysym: 0x%x", keysym); */
+
+        } else {
+                       /* Fall back to unicode events */
+                       int unicode_code = keysym2uni(keysym);
+                       guac_client_log_info(client, "Translated keysym:0x%x to unicode:0x%x (pressed=%d flag=%d)", 
+                                                                keysym, unicode_code, pressed, pressed ? KBD_FLAGS_DOWN : KBD_FLAGS_RELEASE);
+
+                       /* LibfreeRDP seems not to take into account the DOWN/RELEASE flags.
+                        *   So we send only the key once.
+                        */
+                       if (pressed) {
+            rdp_inst->input->UnicodeKeyboardEvent(
                     rdp_inst->input,
-                    scancode_map->flags
-                        | (pressed ? KBD_FLAGS_DOWN : KBD_FLAGS_RELEASE),
-                    scancode_map->scancode);
+                                       0,//pressed ? KBD_FLAGS_DOW : KBD_FLAGS_RELEASE,
+                    unicode_code);
+                       }
+               }
+    }
+
+    return 0;
+}
 
-        /* If undefined, try to type using Alt-code */
-        else {
+void __guac_rdp_update_keysyms(guac_client* client, const int* keysym_string, int from, int to) {
 
-            const guac_rdp_altcode_map* altcode_map = GUAC_RDP_KEYSYM_LOOKUP(guac_rdp_keysym_altcode, keysym);
-            if (altcode_map->altcode != NULL) {
+    rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
+    int keysym;
 
-                /* Only send Alt-code on press */
-                if (pressed)
-                    __guac_rdp_send_altcode(client, altcode_map->altcode);
+    /* Send all keysyms in string, NULL terminated */
+    while ((keysym = *keysym_string) != 0) {
 
-            }
+        /* Get current keysym state */
+        int current_state = GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keysym_state, keysym);
 
-            /* If no defined Alt-code, log warning */
-            else
-                guac_client_log_info(client, "unmapped keysym: 0x%x", keysym);
+        /* If key is currently in given state, send event for changing it to specified "to" state */
+        if (current_state == from)
+            __guac_rdp_send_keysym(client, *keysym_string, to);
 
-        }
+        /* Next keysym */
+        keysym_string++;
 
     }
 
+}
+
+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);
+
+}
+
+int rdp_guac_client_clipboard_handler(guac_client* client, char* data) {
+
+    rdpChannels* channels = 
+        ((rdp_guac_client_data*) client->data)->rdp_inst->context->channels;
+
+    RDP_CB_FORMAT_LIST_EVENT* format_list =
+        (RDP_CB_FORMAT_LIST_EVENT*) freerdp_event_new(
+            RDP_EVENT_CLASS_CLIPRDR,
+            RDP_EVENT_TYPE_CB_FORMAT_LIST,
+            NULL, NULL);
+
+    /* Free existing data */
+    free(((rdp_guac_client_data*) client->data)->clipboard);
+
+    /* Store data in client */
+    ((rdp_guac_client_data*) client->data)->clipboard = strdup(data);
+
+    /* Notify server that text data is now available */
+    format_list->formats = (uint32*) malloc(sizeof(uint32));
+    format_list->formats[0] = CB_FORMAT_TEXT;
+    format_list->num_formats = 1;
+
+    freerdp_channels_send_event(channels, (RDP_EVENT*) format_list);
+
     return 0;
+
 }