Free clipboard data properly.
[libguac-client-rdp.git] / src / client.c
index f979147..1dcd309 100644 (file)
@@ -57,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"
@@ -90,6 +91,10 @@ enum ARGS_IDX {
     IDX_COLOR_DEPTH
 };
 
+int __guac_receive_channel_data(freerdp* rdp_inst, int channelId, uint8* data, int size, int flags, int total_size) {
+    return freerdp_channels_data(rdp_inst, channelId, data, size, flags, total_size);
+}
+
 boolean rdp_freerdp_pre_connect(freerdp* instance) {
 
     rdpContext* context = instance->context;
@@ -101,6 +106,9 @@ boolean rdp_freerdp_pre_connect(freerdp* instance) {
     rdpPrimaryUpdate* primary;
     CLRCONV* clrconv;
 
+    /* Load clipboard plugin */
+    freerdp_channels_load_plugin(channels, instance->settings, "cliprdr", NULL);
+
     /* Init color conversion structure */
     clrconv = xnew(CLRCONV);
     clrconv->alpha = 1;
@@ -121,6 +129,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);
@@ -131,6 +140,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);
@@ -139,8 +149,10 @@ 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->EndPaint = guac_rdp_gdi_end_paint;
     instance->update->Palette = guac_rdp_gdi_palette_update;
     instance->update->SetBounds = guac_rdp_gdi_set_bounds;
 
@@ -187,10 +199,7 @@ boolean rdp_freerdp_post_connect(freerdp* instance) {
     client->handle_messages = rdp_guac_client_handle_messages;
     client->mouse_handler = rdp_guac_client_mouse_handler;
     client->key_handler = rdp_guac_client_key_handler;
-
-    /* Send size */
-    guac_protocol_send_size(client->socket, GUAC_DEFAULT_LAYER,
-            instance->settings->width, instance->settings->height);
+    client->clipboard_handler = rdp_guac_client_clipboard_handler;
 
     return true;
 
@@ -245,8 +254,14 @@ int guac_client_init(guac_client* client, int argc, char** argv) {
     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;
     }
 
@@ -264,6 +279,7 @@ int guac_client_init(guac_client* client, int argc, char** argv) {
     rdp_inst = freerdp_new();
     rdp_inst->PreConnect = rdp_freerdp_pre_connect;
     rdp_inst->PostConnect = rdp_freerdp_post_connect;
+    rdp_inst->ReceiveChannelData = __guac_receive_channel_data;
 
     /* Allocate FreeRDP context */
     rdp_inst->context_size = sizeof(rdp_freerdp_context);
@@ -352,6 +368,7 @@ int guac_client_init(guac_client* client, int argc, char** argv) {
     guac_client_data->rdp_inst = rdp_inst;
     guac_client_data->mouse_button_mask = 0;
     guac_client_data->current_surface = GUAC_DEFAULT_LAYER;
+    guac_client_data->clipboard = NULL;
 
     /* Clear keysym state mapping and keymap */
     memset(guac_client_data->keysym_state, 0,
@@ -368,11 +385,31 @@ int guac_client_init(guac_client* client, int argc, char** argv) {
 
     /* 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);
+
+    /* Send size */
+    guac_protocol_send_size(client->socket, GUAC_DEFAULT_LAYER,
+            settings->width, settings->height);
+
+    /* Create glyph surfaces */
+    guac_client_data->opaque_glyph_surface = cairo_image_surface_create(
+            CAIRO_FORMAT_RGB24, settings->width, settings->height);
+
+    guac_client_data->trans_glyph_surface = cairo_image_surface_create(
+            CAIRO_FORMAT_ARGB32, settings->width, settings->height);
+
     /* Success */
     return 0;