2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
15 * The Original Code is libguac-client-rdp.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2011
20 * the Initial Developer. All Rights Reserved.
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.
36 * ***** END LICENSE BLOCK ***** */
41 #include <cairo/cairo.h>
43 #include <guacamole/log.h>
44 #include <guacamole/guacio.h>
45 #include <guacamole/client.h>
46 #include <guacamole/protocol.h>
48 #include <freerdp/freerdp.h>
50 #include "rdp_handlers.h"
51 #include "rdp_client.h"
54 void guac_rdp_convert_color(int depth, int color, guac_rdp_color* comp) {
59 comp->red = (color >> 16) & 0xFF;
60 comp->green = (color >> 8) & 0xFF;
61 comp->blue = (color ) & 0xFF;
65 comp->red = ((color >> 8) & 0xF8) | ((color >> 13) & 0x07);
66 comp->green = ((color >> 3) & 0xFC) | ((color >> 9) & 0x03);
67 comp->blue = ((color << 3) & 0xF8) | ((color >> 2) & 0x07);
70 default: /* The Magenta of Failure */
78 void guac_rdp_ui_error(rdpInst* inst, const char* text) {
80 guac_client* client = (guac_client*) inst->param1;
81 GUACIO* io = client->io;
83 guac_send_error(io, text);
88 void guac_rdp_ui_warning(rdpInst* inst, const char* text) {
89 guac_log_info("guac_rdp_ui_warning: %s\n", text);
92 void guac_rdp_ui_unimpl(rdpInst* inst, const char* text) {
93 guac_log_info("guac_rdp_ui_unimpl: %s\n", text);
96 void guac_rdp_ui_begin_update(rdpInst* inst) {
100 void guac_rdp_ui_end_update(rdpInst* inst) {
101 guac_client* client = (guac_client*) inst->param1;
102 GUACIO* io = client->io;
106 void guac_rdp_ui_desktop_save(rdpInst* inst, int offset, int x, int y, int cx, int cy) {
107 guac_log_info("guac_rdp_ui_desktop_save: STUB\n");
110 void guac_rdp_ui_desktop_restore(rdpInst* inst, int offset, int x, int y, int cx, int cy) {
111 guac_log_info("guac_rdp_ui_desktop_restore: STUB\n");
115 RD_HBITMAP guac_rdp_ui_create_bitmap(rdpInst* inst, int width, int height, uint8* data) {
117 /* Allocate buffer */
118 guac_client* client = (guac_client*) inst->param1;
119 GUACIO* io = client->io;
120 guac_layer* buffer = guac_client_alloc_buffer(client);
124 int bpp = inst->settings->server_depth / 8;
125 unsigned char* image_buffer;
126 unsigned char* image_buffer_row;
128 cairo_surface_t* surface;
130 /* Init Cairo buffer */
131 stride = cairo_format_stride_for_width(CAIRO_FORMAT_RGB24, width);
132 image_buffer = malloc(height*stride);
133 image_buffer_row = image_buffer;
135 /* Copy image data from image data to buffer */
136 for (y = 0; y<height; y++) {
138 unsigned int* image_buffer_current;
140 /* Get current buffer row, advance to next */
141 image_buffer_current = (unsigned int*) image_buffer_row;
142 image_buffer_row += stride;
144 for (x = 0; x<width; x++) {
146 unsigned char red, green, blue;
151 blue = *((unsigned char*) data++);
152 green = *((unsigned char*) data++);
153 red = *((unsigned char*) data++);
157 v = *((unsigned char*) data++);
158 v |= *((unsigned char*) data++) << 8;
160 red = ((v >> 8) & 0xF8) | ((v >> 13) & 0x07);
161 green = ((v >> 3) & 0xFC) | ((v >> 9) & 0x03);
162 blue = ((v << 3) & 0xF8) | ((v >> 2) & 0x07);
165 default: /* The Magenta of Failure */
172 *(image_buffer_current++) = (red << 16) | (green << 8) | blue;
177 surface = cairo_image_surface_create_for_data(image_buffer, CAIRO_FORMAT_RGB24, width, height, stride);
178 guac_send_png(io, GUAC_COMP_OVER, buffer, 0, 0, surface);
181 cairo_surface_destroy(surface);
184 return (RD_HBITMAP) buffer;
188 void guac_rdp_ui_paint_bitmap(rdpInst* inst, int x, int y, int cx, int cy, int width, int height, uint8* data) {
189 guac_log_info("guac_rdp_ui_paint_bitmap: STUB\n");
192 void guac_rdp_ui_destroy_bitmap(rdpInst* inst, RD_HBITMAP bmp) {
195 guac_client* client = (guac_client*) inst->param1;
196 guac_client_free_buffer(client, (guac_layer*) bmp);
200 void guac_rdp_ui_line(rdpInst* inst, uint8 opcode, int startx, int starty, int endx, int endy, RD_PEN* pen) {
201 guac_log_info("guac_rdp_ui_line: STUB\n");
204 void guac_rdp_ui_rect(rdpInst* inst, int x, int y, int cx, int cy, int color) {
206 guac_client* client = (guac_client*) inst->param1;
207 GUACIO* io = client->io;
209 unsigned char red, green, blue;
211 switch (inst->settings->server_depth) {
213 red = (color >> 16) & 0xFF;
214 green = (color >> 8) & 0xFF;
215 blue = (color ) & 0xFF;
219 red = ((color >> 8) & 0xF8) | ((color >> 13) & 0x07);
220 green = ((color >> 3) & 0xFC) | ((color >> 9) & 0x03);
221 blue = ((color << 3) & 0xF8) | ((color >> 2) & 0x07);
224 default: /* The Magenta of Failure */
231 guac_send_rect(io, GUAC_COMP_OVER, GUAC_DEFAULT_LAYER,
233 red, green, blue, 0xFF);
237 void guac_rdp_ui_polygon(rdpInst* inst, uint8 opcode, uint8 fillmode, RD_POINT* point, int npoints, RD_BRUSH* brush, int bgcolor, int fgcolor) {
238 guac_log_info("guac_rdp_ui_polygon: STUB\n");
241 void guac_rdp_ui_polyline(rdpInst* inst, uint8 opcode, RD_POINT* points, int npoints, RD_PEN* pen) {
242 guac_log_info("guac_rdp_ui_polyline: STUB\n");
245 void guac_rdp_ui_ellipse(rdpInst* inst, uint8 opcode, uint8 fillmode, int x, int y, int cx, int cy, RD_BRUSH* brush, int bgcolor, int fgcolor) {
246 guac_log_info("guac_rdp_ui_ellipse: STUB\n");
249 void guac_rdp_ui_start_draw_glyphs(rdpInst* inst, int bgcolor, int fgcolor) {
251 guac_client* client = (guac_client*) inst->param1;
252 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
254 guac_rdp_convert_color(
255 inst->settings->server_depth,
257 &(guac_client_data->background));
259 guac_rdp_convert_color(
260 inst->settings->server_depth,
262 &(guac_client_data->foreground));
266 void guac_rdp_ui_draw_glyph(rdpInst* inst, int x, int y, int width, int height, RD_HGLYPH glyph) {
268 guac_client* client = (guac_client*) inst->param1;
269 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
270 GUACIO* io = client->io;
272 /* NOTE: Originally: Stencil=SRC, FG=ATOP, BG=RATOP */
273 /* Temporarily removed BG drawing... */
277 (guac_layer*) glyph, 0, 0, width, height,
278 GUAC_COMP_ROUT, GUAC_DEFAULT_LAYER, x, y);
281 guac_send_rect(io, GUAC_COMP_RATOP, GUAC_DEFAULT_LAYER,
283 guac_client_data->foreground.red,
284 guac_client_data->foreground.green,
285 guac_client_data->foreground.blue,
289 /*guac_send_rect(io, GUAC_COMP_RATOP, GUAC_DEFAULT_LAYER,
291 guac_client_data->background.red,
292 guac_client_data->background.green,
293 guac_client_data->background.blue,
298 void guac_rdp_ui_end_draw_glyphs(rdpInst* inst, int x, int y, int cx, int cy) {
302 uint32 guac_rdp_ui_get_toggle_keys_state(rdpInst* inst) {
303 guac_log_info("guac_rdp_ui_get_toggle_keys_state: STUB\n");
307 void guac_rdp_ui_bell(rdpInst* inst) {
308 guac_log_info("guac_rdp_ui_bell: STUB\n");
311 void guac_rdp_ui_destblt(rdpInst* inst, uint8 opcode, int x, int y, int cx, int cy) {
312 guac_log_info("guac_rdp_ui_destblt: STUB\n");
315 void guac_rdp_ui_patblt(rdpInst* inst, uint8 opcode, int x, int y, int cx, int cy, RD_BRUSH* brush, int bgcolor, int fgcolor) {
316 guac_log_info("guac_rdp_ui_patblt: STUB\n");
319 void guac_rdp_ui_screenblt(rdpInst* inst, uint8 opcode, int x, int y, int cx, int cy, int srcx, int srcy) {
320 guac_log_info("guac_rdp_ui_screenblt: STUB\n");
323 void guac_rdp_ui_memblt(rdpInst* inst, uint8 opcode, int x, int y, int width, int height, RD_HBITMAP src, int srcx, int srcy) {
325 guac_client* client = (guac_client*) inst->param1;
326 GUACIO* io = client->io;
329 guac_log_info("guac_rdp_ui_memblt: opcode=%i, index=%i\n", opcode,
330 ((guac_layer*) src)->index);
333 (guac_layer*) src, srcx, srcy, width, height,
334 GUAC_COMP_OVER, GUAC_DEFAULT_LAYER, x, y);
338 void guac_rdp_ui_triblt(rdpInst* inst, uint8 opcode, int x, int y, int cx, int cy, RD_HBITMAP src, int srcx, int srcy, RD_BRUSH* brush, int bgcolor, int fgcolor) {
339 guac_log_info("guac_rdp_ui_triblt: STUB\n");
342 RD_HGLYPH guac_rdp_ui_create_glyph(rdpInst* inst, int width, int height, uint8* data) {
344 /* Allocate buffer */
345 guac_client* client = (guac_client*) inst->param1;
346 GUACIO* io = client->io;
347 guac_layer* glyph = guac_client_alloc_buffer(client);
351 unsigned char* image_buffer;
352 unsigned char* image_buffer_row;
354 cairo_surface_t* surface;
356 /* Init Cairo buffer */
357 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
358 image_buffer = malloc(height*stride);
359 image_buffer_row = image_buffer;
361 /* Copy image data from image data to buffer */
362 for (y = 0; y<height; y++) {
364 unsigned int* image_buffer_current;
366 /* Get current buffer row, advance to next */
367 image_buffer_current = (unsigned int*) image_buffer_row;
368 image_buffer_row += stride;
370 for (x = 0; x<width;) {
372 /* Get byte from image data */
373 unsigned int v = *(data++);
375 /* Read bits, write pixels */
376 for (i = 0; i<8 && x<width; i++, x++) {
380 *(image_buffer_current++) = 0xFF000000;
382 *(image_buffer_current++) = 0x00000000;
392 surface = cairo_image_surface_create_for_data(image_buffer, CAIRO_FORMAT_ARGB32, width, height, stride);
393 guac_send_png(io, GUAC_COMP_SRC, glyph, 0, 0, surface);
396 cairo_surface_destroy(surface);
400 return (RD_HGLYPH) glyph;
404 void guac_rdp_ui_destroy_glyph(rdpInst* inst, RD_HGLYPH glyph) {
407 guac_client* client = (guac_client*) inst->param1;
408 guac_client_free_buffer(client, (guac_layer*) glyph);
412 int guac_rdp_ui_select(rdpInst* inst, int rdp_socket) {
416 void guac_rdp_ui_set_clip(rdpInst* inst, int x, int y, int cx, int cy) {
418 guac_client* client = (guac_client*) inst->param1;
419 GUACIO* io = client->io;
421 guac_send_clip(io, GUAC_DEFAULT_LAYER, x, y, cx, cy);
425 void guac_rdp_ui_reset_clip(rdpInst* inst) {
427 guac_client* client = (guac_client*) inst->param1;
428 GUACIO* io = client->io;
430 guac_send_clip(io, GUAC_DEFAULT_LAYER, 0, 0, inst->settings->width, inst->settings->height);
434 void guac_rdp_ui_resize_window(rdpInst* inst) {
435 guac_log_info("guac_rdp_ui_resize_window: %ix%i\n", inst->settings->width, inst->settings->height);
438 void guac_rdp_ui_set_cursor(rdpInst* inst, RD_HCURSOR cursor) {
439 guac_log_info("guac_rdp_ui_set_cursor: STUB\n");
442 void guac_rdp_ui_destroy_cursor(rdpInst* inst, RD_HCURSOR cursor) {
443 guac_log_info("guac_rdp_ui_destroy_cursor: STUB\n");
446 RD_HCURSOR guac_rdp_ui_create_cursor(rdpInst* inst, unsigned int x, unsigned int y, int width, int height, uint8* andmask, uint8* xormask, int bpp) {
448 guac_client* client = (guac_client*) inst->param1;
449 guac_log_info("guac_rdp_ui_create_cursor: STUB\n");
450 return guac_client_alloc_buffer(client);
454 void guac_rdp_ui_set_null_cursor(rdpInst* inst) {
455 guac_log_info("guac_rdp_ui_set_null_cursor: STUB\n");
458 void guac_rdp_ui_set_default_cursor(rdpInst* inst) {
459 guac_log_info("guac_rdp_ui_set_default_cursor: STUB\n");
462 RD_HPALETTE guac_rdp_ui_create_colormap(rdpInst* inst, RD_PALETTE* colors) {
463 guac_log_info("guac_rdp_ui_create_colormap: STUB\n");
467 void guac_rdp_ui_move_pointer(rdpInst* inst, int x, int y) {
468 guac_log_info("guac_rdp_ui_move_pointer: STUB\n");
471 void guac_rdp_ui_set_colormap(rdpInst* inst, RD_HPALETTE map) {
472 guac_log_info("guac_rdp_ui_set_colormap: STUB\n");
475 RD_HBITMAP guac_rdp_ui_create_surface(rdpInst* inst, int width, int height, RD_HBITMAP old) {
477 /* Allocate and return buffer */
478 guac_client* client = (guac_client*) inst->param1;
479 return (RD_HBITMAP) guac_client_alloc_buffer(client);
483 void guac_rdp_ui_set_surface(rdpInst* inst, RD_HBITMAP surface) {
485 guac_client* client = (guac_client*) inst->param1;
486 GUACIO* io = client->io;
489 if (surface == NULL) {
491 guac_send_name(io, inst->settings->server);
492 guac_send_size(io, inst->settings->width, inst->settings->height);
497 guac_log_info("guac_rdp_ui_set_surface: STUB (surface=%p) ... %ix%i\n", surface, inst->settings->width, inst->settings->height);
501 void guac_rdp_ui_destroy_surface(rdpInst* inst, RD_HBITMAP surface) {
504 guac_client* client = (guac_client*) inst->param1;
505 guac_client_free_buffer(client, (guac_layer*) surface);
509 void guac_rdp_ui_channel_data(rdpInst* inst, int chan_id, char* data, int data_size, int flags, int total_size) {
510 guac_log_info("guac_rdp_ui_channel_data: STUB\n");