Forgot to restore the increment of current keysym.
[libguac-client-rdp.git] / src / guac_handlers.c
index 1f986cb..bfb7bda 100644 (file)
@@ -54,7 +54,7 @@
 #include "rdp_keymap.h"
 #include "guac_handlers.h"
 
-void __guac_rdp_send_keysym_string(guac_client* client, const int* keysym_string, int pressed);
+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, const char* altcode);
 
@@ -234,7 +234,7 @@ void __guac_rdp_send_altcode(guac_client* client, const char* altcode) {
     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;
+    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);
@@ -243,7 +243,7 @@ void __guac_rdp_send_altcode(guac_client* client, const char* altcode) {
     while (*altcode != '\0') {
 
         /* Get scancode of keypad digit */
-        int scancode = GUAC_RDP_KEYSYM_LOOKUP(*keysym_scancodes, 0xFFB0 + (*altcode) - '0')->scancode;
+        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);
@@ -268,14 +268,18 @@ int __guac_rdp_send_keysym(guac_client* client, int keysym, int pressed) {
     if (keysym <= 0xFFFF) {
 
         /* Look up scancode mapping */
-        const guac_rdp_scancode_map* scancode_map = GUAC_RDP_KEYSYM_LOOKUP(*keysym_scancodes, keysym);
+        const guac_rdp_scancode_map* scancode_map = &GUAC_RDP_KEYSYM_LOOKUP(*keysym_scancodes, keysym);
 
         /* If defined, send event */
         if (scancode_map->scancode != 0) {
 
-            /* If defined, send any prerequesite keys */
+            /* If defined, send any prerequesite keys that must be set */
             if (scancode_map->set_keysyms != NULL)
-                __guac_rdp_send_keysym_string(client, scancode_map->set_keysyms, 1);
+                __guac_rdp_update_keysyms(client, scancode_map->set_keysyms, 0, 1);
+
+            /* If defined, release any keys that must be cleared */
+            if (scancode_map->clear_keysyms != NULL)
+                __guac_rdp_update_keysyms(client, scancode_map->clear_keysyms, 1, 0);
 
             /* Send actual key */
             rdp_inst->input->KeyboardEvent(
@@ -284,16 +288,20 @@ int __guac_rdp_send_keysym(guac_client* client, int keysym, int pressed) {
                         | (pressed ? KBD_FLAGS_DOWN : KBD_FLAGS_RELEASE),
                     scancode_map->scancode);
 
-            /* If defined, release any prerequesite keys */
+            /* If defined, release any keys that were originally released */
             if (scancode_map->set_keysyms != NULL)
-                __guac_rdp_send_keysym_string(client, scancode_map->set_keysyms, 0);
+                __guac_rdp_update_keysyms(client, scancode_map->set_keysyms, 0, 0);
+
+            /* If defined, send any keys that were originally set */
+            if (scancode_map->clear_keysyms != NULL)
+                __guac_rdp_update_keysyms(client, scancode_map->clear_keysyms, 1, 1);
 
         }
 
         /* 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);
+            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 */
@@ -313,17 +321,36 @@ int __guac_rdp_send_keysym(guac_client* client, int keysym, int pressed) {
     return 0;
 }
 
-void __guac_rdp_send_keysym_string(guac_client* client, const int* keysym_string, int pressed) {
+void __guac_rdp_update_keysyms(guac_client* client, const int* keysym_string, int from, int to) {
+
+    rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
+    int keysym;
 
     /* Send all keysyms in string, NULL terminated */
-    while (*keysym_string != 0)
-        __guac_rdp_send_keysym(client, *(keysym_string++), pressed);
+    while ((keysym = *keysym_string) != 0) {
+
+        /* Get current keysym state */
+        int current_state = GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keysym_state, 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) {
 
-    return __guac_rdp_send_keysym(client, keysym, 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);
 
 }