First working implementation with special characters handled by unicodekeyboardevents.
[libguac-client-rdp.git] / src / guac_handlers.c
index dab2b27..30aeae2 100644 (file)
@@ -47,6 +47,8 @@
 #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>
@@ -55,7 +57,9 @@
 
 #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);
@@ -79,6 +83,9 @@ int rdp_guac_client_free_handler(guac_client* client) {
     freerdp_free(rdp_inst);
 
     /* 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;
@@ -98,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,
@@ -172,6 +180,18 @@ int rdp_guac_client_handle_messages(guac_client* client) {
         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;
@@ -325,10 +345,12 @@ int __guac_rdp_send_keysym(guac_client* client, int keysym, int pressed) {
 
             /* Send actual key */
             rdp_inst->input->KeyboardEvent(
-                    rdp_inst->input,
+                                       rdp_inst->input,
                     keysym_desc->flags
-                        | (pressed ? KBD_FLAGS_DOWN : KBD_FLAGS_RELEASE),
-                    keysym_desc->scancode);
+                                           | (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)
@@ -338,23 +360,39 @@ int __guac_rdp_send_keysym(guac_client* client, int keysym, int pressed) {
             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. */
+        /* /\* If undefined but has Alt-code, use Alt-Code *\/ */
+        /* else if (keysym <= 0xFF) { */
 
-            /* Only send Alt-code on press */
-            if (pressed)
-                __guac_rdp_send_altcode(client, keysym);
+        /*     /\* 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);
+        /* } */
 
+        /* /\* 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,
+                                       0,//pressed ? KBD_FLAGS_DOW : KBD_FLAGS_RELEASE,
+                    unicode_code);
+                       }
+               }
     }
 
     return 0;
@@ -393,3 +431,31 @@ int rdp_guac_client_key_handler(guac_client* client, int keysym, int 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;
+
+}
+