Fixed whitespace, moved timeout declaration.
[libguac-client-rdp.git] / src / rdp_bitmap.c
1
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is libguac-client-rdp.
16  *
17  * The Initial Developer of the Original Code is
18  * Michael Jumper.
19  * Portions created by the Initial Developer are Copyright (C) 2011
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 #include <stdio.h>
39 #include <stdlib.h>
40
41 #include <cairo/cairo.h>
42
43 #include <guacamole/socket.h>
44 #include <guacamole/client.h>
45 #include <guacamole/protocol.h>
46
47 #include <freerdp/freerdp.h>
48 #include <freerdp/utils/memory.h>
49 #include <freerdp/codec/color.h>
50 #include <freerdp/codec/bitmap.h>
51
52 #include "client.h"
53 #include "rdp_bitmap.h"
54
55 void guac_rdp_bitmap_new(rdpContext* context, rdpBitmap* bitmap) {
56
57     guac_client* client = ((rdp_freerdp_context*) context)->client;
58     guac_socket* socket = client->socket; 
59
60     /* Allocate buffer */
61     guac_layer* buffer = guac_client_alloc_buffer(client);
62
63     /* Store buffer reference in bitmap */
64     ((guac_rdp_bitmap*) bitmap)->layer = buffer;
65
66     /* Convert image data if present */
67     if (bitmap->data != NULL) {
68
69         /* Convert image data to 32-bit RGB */
70         unsigned char* image_buffer = freerdp_image_convert(bitmap->data, NULL,
71                 bitmap->width, bitmap->height,
72                 context->instance->settings->color_depth,
73                 32, ((rdp_freerdp_context*) context)->clrconv);
74
75         /* Create surface from image data */
76         cairo_surface_t* surface = cairo_image_surface_create_for_data(
77             image_buffer, CAIRO_FORMAT_RGB24,
78             bitmap->width, bitmap->height, 4*bitmap->width);
79
80         /* Send surface to buffer */
81         guac_protocol_send_png(socket, GUAC_COMP_SRC, buffer, 0, 0, surface);
82
83         /* Free surface */
84         cairo_surface_destroy(surface);
85
86         /* If ephemeral, just free image data */
87         if (!bitmap->ephemeral) {
88
89             /* Free image data if actually alloated */
90             if (image_buffer != bitmap->data)
91                 free(image_buffer);
92
93         }
94
95         /* Otherwise, store converted image in bitmap, free any existing */
96         else {
97
98             /* Free existing image, if any */
99             if (image_buffer != bitmap->data)
100                 free(bitmap->data);
101
102             /* Store converted image in bitmap */
103             bitmap->data = image_buffer;
104
105         }
106
107     }
108
109 }
110
111 void guac_rdp_bitmap_paint(rdpContext* context, rdpBitmap* bitmap) {
112
113     guac_client* client = ((rdp_freerdp_context*) context)->client;
114     guac_socket* socket = client->socket;
115
116     int width = bitmap->right - bitmap->left + 1;
117     int height = bitmap->bottom - bitmap->top + 1;
118
119     guac_protocol_send_copy(socket,
120             ((guac_rdp_bitmap*) bitmap)->layer,
121             0, 0, width, height,
122             GUAC_COMP_OVER,
123             GUAC_DEFAULT_LAYER, bitmap->left, bitmap->top);
124
125 }
126
127 void guac_rdp_bitmap_free(rdpContext* context, rdpBitmap* bitmap) {
128     guac_client* client = ((rdp_freerdp_context*) context)->client;
129
130     /* Free layer, if any */
131     if (((guac_rdp_bitmap*) bitmap)->layer != NULL)
132         guac_client_free_buffer(client, ((guac_rdp_bitmap*) bitmap)->layer);
133 }
134
135 void guac_rdp_bitmap_setsurface(rdpContext* context, rdpBitmap* bitmap, boolean primary) {
136     guac_client* client = ((rdp_freerdp_context*) context)->client;
137
138     if (primary)
139         ((rdp_guac_client_data*) client->data)->current_surface
140             = GUAC_DEFAULT_LAYER;
141     else
142         ((rdp_guac_client_data*) client->data)->current_surface 
143             = ((guac_rdp_bitmap*) bitmap)->layer;
144
145 }
146
147 void guac_rdp_bitmap_decompress(rdpContext* context, rdpBitmap* bitmap, uint8* data, int width, int height, int bpp, int length, boolean compressed) {
148
149     int size = width * height * (bpp + 7) / 8;
150
151     if (bitmap->data == NULL)
152         bitmap->data = (uint8*) xmalloc(size);
153     else
154         bitmap->data = (uint8*) xrealloc(bitmap->data, size);
155
156     if (compressed)
157         bitmap_decompress(data, bitmap->data, width, height, length, bpp, bpp);
158     else
159         freerdp_image_flip(data, bitmap->data, width, height, bpp);
160
161     bitmap->compressed = false;
162     bitmap->length = size;
163     bitmap->bpp = bpp;
164
165 }
166