Send name after connect.
[libguac-client-rdp.git] / src / client.c
old mode 100755 (executable)
new mode 100644 (file)
index 2aed214..e6a028b
@@ -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
@@ -56,6 +57,7 @@
 #include <guacamole/socket.h>
 #include <guacamole/protocol.h>
 #include <guacamole/client.h>
+#include <guacamole/error.h>
 
 #include "client.h"
 #include "guac_handlers.h"
 const char* GUAC_CLIENT_ARGS[] = {
     "hostname",
     "port",
-       "username",
-       "password",
-       "width",
-       "height",
-       "initial_program",
-       "color_depth",
+    "username",
+    "password",
+    "width",
+    "height",
+    "initial_program",
+    "color_depth",
     NULL
 };
 
 enum ARGS_IDX {
-       IDX_HOSTNAME,
-       IDX_PORT,
-       IDX_USERNAME,
-       IDX_PASSWORD,
-       IDX_WIDTH,
-       IDX_HEIGHT,
-       IDX_INITIAL_PROGRAM,
-       IDX_COLOR_DEPTH
+    IDX_HOSTNAME,
+    IDX_PORT,
+    IDX_USERNAME,
+    IDX_PASSWORD,
+    IDX_WIDTH,
+    IDX_HEIGHT,
+    IDX_INITIAL_PROGRAM,
+    IDX_COLOR_DEPTH
 };
 
 boolean rdp_freerdp_pre_connect(freerdp* instance) {
@@ -120,6 +122,7 @@ boolean rdp_freerdp_pre_connect(freerdp* instance) {
     bitmap->Decompress = guac_rdp_bitmap_decompress;
     bitmap->SetSurface = guac_rdp_bitmap_setsurface;
     graphics_register_bitmap(context->graphics, bitmap);
+    xfree(bitmap);
 
     /* Set up glyph handling */
     glyph = xnew(rdpGlyph);
@@ -130,6 +133,7 @@ boolean rdp_freerdp_pre_connect(freerdp* instance) {
     glyph->BeginDraw = guac_rdp_glyph_begindraw;
     glyph->EndDraw = guac_rdp_glyph_enddraw;
     graphics_register_glyph(context->graphics, glyph);
+    xfree(glyph);
 
     /* Set up pointer handling */
     pointer = xnew(rdpPointer);
@@ -138,6 +142,7 @@ boolean rdp_freerdp_pre_connect(freerdp* instance) {
     pointer->Free = guac_rdp_pointer_free;
     pointer->Set = guac_rdp_pointer_set;
     graphics_register_pointer(context->graphics, pointer);
+    xfree(pointer);
 
     /* Set up GDI */
     instance->update->Palette = guac_rdp_gdi_palette_update;
@@ -203,20 +208,55 @@ void rdp_freerdp_context_free(freerdp* instance, rdpContext* context) {
     /* EMPTY */
 }
 
+void __guac_rdp_client_load_keymap(guac_client* client,
+        const guac_rdp_keymap* keymap) {
+
+    rdp_guac_client_data* guac_client_data =
+        (rdp_guac_client_data*) client->data;
+    /* Get mapping */
+    const guac_rdp_keysym_desc* mapping = keymap->mapping;
+
+    /* If parent exists, load parent first */
+    if (keymap->parent != NULL)
+        __guac_rdp_client_load_keymap(client, keymap->parent);
+
+    /* Log load */
+    guac_client_log_info(client, "Loading keymap \"%s\"", keymap->name);
+
+    /* Load mapping into keymap */
+    while (mapping->keysym != 0) {
+
+        /* Copy mapping */
+        GUAC_RDP_KEYSYM_LOOKUP(guac_client_data->keymap, mapping->keysym) =
+            *mapping;
+
+        /* Next keysym */
+        mapping++;
+
+    }
+
+}
+
 int guac_client_init(guac_client* client, int argc, char** argv) {
 
     rdp_guac_client_data* guac_client_data;
 
     freerdp* rdp_inst;
-       rdpSettings* settings;
+    rdpSettings* settings;
 
     char* hostname;
     int port = RDP_DEFAULT_PORT;
     boolean bitmap_cache;
 
     if (argc < 8) {
-        guac_protocol_send_error(client->socket, "Wrong argument count received.");
+
+        guac_protocol_send_error(client->socket,
+                "Wrong argument count received.");
         guac_socket_flush(client->socket);
+
+        guac_error = GUAC_STATUS_BAD_ARGUMENT;
+        guac_error_message = "Wrong argument count received";
+
         return 1;
     }
 
@@ -256,44 +296,45 @@ int guac_client_init(guac_client* client, int argc, char** argv) {
     settings->encryption_level = ENCRYPTION_LEVEL_CLIENT_COMPATIBLE;
 
     /* session width */
-       settings->width = 1024;
-       if (argv[IDX_WIDTH][0] != '\0')
-               settings->width = atoi(argv[IDX_WIDTH]);
-       if (settings->width == 0)
-               settings->width = 1024;
-
-       /* session height */
-       settings->height = 768;
-       if (argv[IDX_HEIGHT][0] != '\0')
-               settings->height = atoi(argv[IDX_HEIGHT]);
-       if (settings->height == 0)
-               settings->height = 768;
+    settings->width = 1024;
+    if (argv[IDX_WIDTH][0] != '\0')
+        settings->width = atoi(argv[IDX_WIDTH]);
+    if (settings->width == 0)
+        settings->width = 1024;
+
+    /* session height */
+    settings->height = 768;
+    if (argv[IDX_HEIGHT][0] != '\0')
+        settings->height = atoi(argv[IDX_HEIGHT]);
+    if (settings->height == 0)
+        settings->height = 768;
 
     /* Set hostname */
     settings->hostname = strdup(hostname);
-       settings->window_title = strdup(hostname);
-
-       /* username */
-       settings->username = "guest";
-       if (argv[IDX_USERNAME][0] != '\0')
-               settings->username = strdup (argv[IDX_USERNAME]);
-
-       /* password */
-       if (argv[IDX_PASSWORD][0] != '\0') {
-               settings->password = strdup (argv[IDX_PASSWORD]);
-               settings->autologon = 1;
-       }
+    settings->port = port;
+    settings->window_title = strdup(hostname);
+
+    /* username */
+    settings->username = "guest";
+    if (argv[IDX_USERNAME][0] != '\0')
+        settings->username = strdup (argv[IDX_USERNAME]);
+
+    /* password */
+    if (argv[IDX_PASSWORD][0] != '\0') {
+        settings->password = strdup (argv[IDX_PASSWORD]);
+        settings->autologon = 1;
+    }
 
-       /* initial program */
-       if (argv[IDX_INITIAL_PROGRAM][0] != '\0')
-               settings->shell = strdup (argv[IDX_INITIAL_PROGRAM]);
+    /* initial program */
+    if (argv[IDX_INITIAL_PROGRAM][0] != '\0')
+        settings->shell = strdup (argv[IDX_INITIAL_PROGRAM]);
 
     /* Order support */
     bitmap_cache = settings->bitmap_cache;
     settings->os_major_type = OSMAJORTYPE_UNSPECIFIED;
     settings->os_minor_type = OSMINORTYPE_UNSPECIFIED;
     settings->order_support[NEG_DSTBLT_INDEX] = true;
-    settings->order_support[NEG_PATBLT_INDEX] = true;
+    settings->order_support[NEG_PATBLT_INDEX] = false; /* PATBLT not yet supported */
     settings->order_support[NEG_SCRBLT_INDEX] = true;
     settings->order_support[NEG_OPAQUE_RECT_INDEX] = true;
     settings->order_support[NEG_DRAWNINEGRID_INDEX] = false;
@@ -309,9 +350,9 @@ int guac_client_init(guac_client* client, int argc, char** argv) {
     settings->order_support[NEG_MEMBLT_V2_INDEX] = bitmap_cache;
     settings->order_support[NEG_MEM3BLT_V2_INDEX] = false;
     settings->order_support[NEG_SAVEBITMAP_INDEX] = false;
-    settings->order_support[NEG_GLYPH_INDEX_INDEX] = false;
-    settings->order_support[NEG_FAST_INDEX_INDEX] = false;
-    settings->order_support[NEG_FAST_GLYPH_INDEX] = false;
+    settings->order_support[NEG_GLYPH_INDEX_INDEX] = true;
+    settings->order_support[NEG_FAST_INDEX_INDEX] = true;
+    settings->order_support[NEG_FAST_GLYPH_INDEX] = true;
     settings->order_support[NEG_POLYGON_SC_INDEX] = false;
     settings->order_support[NEG_POLYGON_CB_INDEX] = false;
     settings->order_support[NEG_ELLIPSE_SC_INDEX] = false;
@@ -322,16 +363,35 @@ int guac_client_init(guac_client* client, int argc, char** argv) {
     guac_client_data->mouse_button_mask = 0;
     guac_client_data->current_surface = GUAC_DEFAULT_LAYER;
 
-    ((rdp_freerdp_context*) rdp_inst->context)->client = client;
+    /* Clear keysym state mapping and keymap */
+    memset(guac_client_data->keysym_state, 0,
+            sizeof(guac_rdp_keysym_state_map));
+
+    memset(guac_client_data->keymap, 0,
+            sizeof(guac_rdp_static_keymap));
+
     client->data = guac_client_data;
+    ((rdp_freerdp_context*) rdp_inst->context)->client = client;
+
+    /* Load keymap into client */
+    __guac_rdp_client_load_keymap(client, &guac_rdp_keymap_en_us);
 
     /* Connect to RDP server */
     if (!freerdp_connect(rdp_inst)) {
-        guac_protocol_send_error(client->socket, "Error connecting to RDP server");
+
+        guac_protocol_send_error(client->socket,
+                "Error connecting to RDP server");
         guac_socket_flush(client->socket);
+
+        guac_error = GUAC_STATUS_BAD_STATE;
+        guac_error_message = "Error connecting to RDP server";
+
         return 1;
     }
 
+    /* Send connection name */
+    guac_protocol_send_name(client->socket, settings->window_title);
+
     /* Success */
     return 0;