guacamole-spice-protocol/protocols/vnc/src/vnc_client.c

428 lines
12 KiB
C
Raw Normal View History

2010-12-08 21:14:04 +00:00
2011-02-16 02:47:51 +00:00
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
2010-12-08 21:14:04 +00:00
*
2011-02-16 02:47:51 +00:00
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
2010-12-08 21:14:04 +00:00
*
2011-02-16 02:47:51 +00:00
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
2010-12-08 21:14:04 +00:00
*
2011-02-16 02:47:51 +00:00
* The Original Code is libguac-client-vnc.
*
* The Initial Developer of the Original Code is
* Michael Jumper.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* 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
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
2010-12-08 21:14:04 +00:00
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <cairo/cairo.h>
2010-12-08 21:14:04 +00:00
#include <rfb/rfbclient.h>
#include <guacamole/log.h>
2010-12-08 21:14:04 +00:00
#include <guacamole/guacio.h>
#include <guacamole/protocol.h>
#include <guacamole/client.h>
/* Client plugin arguments */
const char* GUAC_CLIENT_ARGS[] = {
"hostname",
"port",
2011-02-11 06:56:47 +00:00
"read-only",
2011-03-14 01:58:31 +00:00
"encodings",
"password",
NULL
};
2010-12-08 21:14:04 +00:00
static char* __GUAC_CLIENT = "GUAC_CLIENT";
typedef struct vnc_guac_client_data {
rfbClient* rfb_client;
MallocFrameBufferProc rfb_MallocFrameBuffer;
2010-12-08 21:14:04 +00:00
int copy_rect_used;
char* password;
2011-03-14 01:58:31 +00:00
char* encodings;
2010-12-08 21:14:04 +00:00
} vnc_guac_client_data;
void guac_vnc_cursor(rfbClient* client, int x, int y, int w, int h, int bpp) {
guac_client* gc = rfbClientGetClientData(client, __GUAC_CLIENT);
GUACIO* io = gc->io;
/* Cairo image buffer */
int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, w);
unsigned char* buffer = malloc(h*stride);
unsigned char* buffer_row_current = buffer;
cairo_surface_t* surface;
2010-12-08 21:14:04 +00:00
/* VNC image buffer */
unsigned int fb_stride = bpp * w;
2010-12-08 21:14:04 +00:00
unsigned char* fb_row_current = client->rcSource;
unsigned char* fb_mask = client->rcMask;
int dx, dy;
/* Copy image data from VNC client to RGBA buffer */
2010-12-08 21:14:04 +00:00
for (dy = 0; dy<h; dy++) {
unsigned char* buffer_current = buffer_row_current;
buffer_row_current += stride;
2010-12-08 21:14:04 +00:00
unsigned char* fb_current = fb_row_current;
fb_row_current += fb_stride;
2010-12-08 21:14:04 +00:00
for (dx = 0; dx<w; dx++) {
unsigned char alpha;
unsigned int v;
/* Read current pixel value */
2010-12-08 21:14:04 +00:00
switch (bpp) {
case 4:
v = *((unsigned int*) fb_current);
2010-12-08 21:14:04 +00:00
break;
case 2:
v = *((unsigned short*) fb_current);
2010-12-08 21:14:04 +00:00
break;
default:
v = *((unsigned char*) fb_current);
2010-12-08 21:14:04 +00:00
}
/* Translate mask to alpha */
if (*(fb_mask++)) alpha = 0xFF;
else alpha = 0x00;
2010-12-08 21:14:04 +00:00
/* Output ARGB */
*(buffer_current++) = alpha;
*(buffer_current++) = (v >> client->format.redShift) * 0x100 / (client->format.redMax + 1);
*(buffer_current++) = (v >> client->format.greenShift) * 0x100 / (client->format.greenMax+ 1);
*(buffer_current++) = (v >> client->format.blueShift) * 0x100 / (client->format.blueMax + 1);
2010-12-08 21:14:04 +00:00
/* Next VNC pixel */
fb_current += bpp;
2010-12-08 21:14:04 +00:00
}
}
/* SEND CURSOR */
surface = cairo_image_surface_create_for_data(buffer, CAIRO_FORMAT_ARGB32, w, h, stride);
guac_send_cursor(io, x, y, surface);
2010-12-08 21:14:04 +00:00
/* libvncclient does not free rcMask as it does rcSource */
free(client->rcMask);
2010-12-08 21:14:04 +00:00
}
void guac_vnc_update(rfbClient* client, int x, int y, int w, int h) {
int dx, dy;
guac_client* gc = rfbClientGetClientData(client, __GUAC_CLIENT);
GUACIO* io = gc->io;
png_byte** png_buffer = ((vnc_guac_client_data*) gc->data)->png_buffer;
png_byte* row;
png_byte** png_row_current = png_buffer;
unsigned int bpp = client->format.bitsPerPixel/8;
unsigned int bytesPerRow = bpp * client->width;
unsigned char* fb_row_current = client->frameBuffer + (y * bytesPerRow) + (x * bpp);
unsigned char* fb_row;
unsigned int v;
/* Ignore extra update if already handled by copyrect */
if (((vnc_guac_client_data*) gc->data)->copy_rect_used) {
((vnc_guac_client_data*) gc->data)->copy_rect_used = 0;
return;
}
/* Copy image data from VNC client to PNG */
for (dy = y; dy<y+h; dy++) {
row = *(png_row_current++);
fb_row = fb_row_current;
fb_row_current += bytesPerRow;
for (dx = x; dx<x+w; dx++) {
switch (bpp) {
case 4:
v = *((unsigned int*) fb_row);
break;
case 2:
v = *((unsigned short*) fb_row);
break;
default:
v = *((unsigned char*) fb_row);
}
*(row++) = (v >> client->format.redShift) * 256 / (client->format.redMax+1);
*(row++) = (v >> client->format.greenShift) * 256 / (client->format.greenMax+1);
*(row++) = (v >> client->format.blueShift) * 256 / (client->format.blueMax+1);
fb_row += bpp;
}
}
/* For now, only use layer 0 */
guac_send_png(io, 0, x, y, png_buffer, w, h);
2010-12-08 21:14:04 +00:00
}
void guac_vnc_copyrect(rfbClient* client, int src_x, int src_y, int w, int h, int dest_x, int dest_y) {
guac_client* gc = rfbClientGetClientData(client, __GUAC_CLIENT);
GUACIO* io = gc->io;
/* For now, only use layer 0 */
guac_send_copy(io, 0, src_x, src_y, w, h, 0, dest_x, dest_y);
2010-12-08 21:14:04 +00:00
((vnc_guac_client_data*) gc->data)->copy_rect_used = 1;
}
char* guac_vnc_get_password(rfbClient* client) {
guac_client* gc = rfbClientGetClientData(client, __GUAC_CLIENT);
return ((vnc_guac_client_data*) gc->data)->password;
}
rfbBool guac_vnc_malloc_framebuffer(rfbClient* rfb_client) {
guac_client* gc = rfbClientGetClientData(rfb_client, __GUAC_CLIENT);
vnc_guac_client_data* guac_client_data = (vnc_guac_client_data*) gc->data;
/* Send new size */
guac_send_size(gc->io, rfb_client->width, rfb_client->height);
/* Use original, wrapped proc */
return guac_client_data->rfb_MallocFrameBuffer(rfb_client);
}
2010-12-08 21:14:04 +00:00
void guac_vnc_cut_text(rfbClient* client, const char* text, int textlen) {
guac_client* gc = rfbClientGetClientData(client, __GUAC_CLIENT);
GUACIO* io = gc->io;
guac_send_clipboard(io, text);
}
int vnc_guac_client_handle_messages(guac_client* client) {
int wait_result;
rfbClient* rfb_client = ((vnc_guac_client_data*) client->data)->rfb_client;
wait_result = WaitForMessage(rfb_client, 1000000);
2010-12-08 21:14:04 +00:00
if (wait_result < 0) {
2011-03-20 00:13:24 +00:00
guac_log_error("Error waiting for VNC server message\n");
2010-12-08 21:14:04 +00:00
return 1;
}
if (wait_result > 0) {
if (!HandleRFBServerMessage(rfb_client)) {
2011-03-20 00:13:24 +00:00
guac_log_error("Error handling VNC server message\n");
2010-12-08 21:14:04 +00:00
return 1;
}
}
return 0;
}
int vnc_guac_client_mouse_handler(guac_client* client, int x, int y, int mask) {
rfbClient* rfb_client = ((vnc_guac_client_data*) client->data)->rfb_client;
SendPointerEvent(rfb_client, x, y, mask);
return 0;
}
int vnc_guac_client_key_handler(guac_client* client, int keysym, int pressed) {
rfbClient* rfb_client = ((vnc_guac_client_data*) client->data)->rfb_client;
SendKeyEvent(rfb_client, keysym, pressed);
return 0;
}
int vnc_guac_client_clipboard_handler(guac_client* client, char* data) {
rfbClient* rfb_client = ((vnc_guac_client_data*) client->data)->rfb_client;
SendClientCutText(rfb_client, data, strlen(data));
return 0;
}
int vnc_guac_client_free_handler(guac_client* client) {
vnc_guac_client_data* guac_client_data = (vnc_guac_client_data*) client->data;
rfbClient* rfb_client = guac_client_data->rfb_client;
2010-12-08 21:14:04 +00:00
2011-03-14 01:58:31 +00:00
/* Free encodings string, if used */
if (guac_client_data->encodings != NULL)
free(guac_client_data->encodings);
/* Free generic data struct */
free(client->data);
/* Free memory not free'd by libvncclient's rfbClientCleanup() */
if (rfb_client->frameBuffer != NULL) free(rfb_client->frameBuffer);
if (rfb_client->raw_buffer != NULL) free(rfb_client->raw_buffer);
if (rfb_client->rcSource != NULL) free(rfb_client->rcSource);
/* Free VNC rfbClientData linked list (not free'd by rfbClientCleanup()) */
while (rfb_client->clientData != NULL) {
rfbClientData* next = rfb_client->clientData->next;
free(rfb_client->clientData);
rfb_client->clientData = next;
}
2010-12-08 21:14:04 +00:00
/* Clean up VNC client*/
rfbClientCleanup(rfb_client);
return 0;
}
int guac_client_init(guac_client* client, int argc, char** argv) {
rfbClient* rfb_client;
vnc_guac_client_data* guac_client_data;
2011-02-11 06:56:47 +00:00
int read_only = 0;
2010-12-08 21:14:04 +00:00
2011-03-20 07:22:22 +00:00
/* Set up libvncclient logging */
rfbClientLog = guac_log_info;
rfbClientErr = guac_log_error;
2011-02-11 06:56:47 +00:00
/*** PARSE ARGUMENTS ***/
2010-12-08 21:14:04 +00:00
2011-03-14 01:58:31 +00:00
if (argc < 5) {
guac_send_error(client->io, "Wrong argument count received.");
2010-12-08 21:14:04 +00:00
guac_flush(client->io);
return 1;
}
/* Alloc client data */
guac_client_data = malloc(sizeof(vnc_guac_client_data));
client->data = guac_client_data;
2011-02-11 06:56:47 +00:00
/* If read-only specified, set flag */
2011-03-14 01:58:31 +00:00
if (strcmp(argv[2], "true") == 0)
read_only = 1;
2010-12-08 21:14:04 +00:00
2011-03-14 01:58:31 +00:00
/* Freed after use by libvncclient */
guac_client_data->password = strdup(argv[4]);
2010-12-08 21:14:04 +00:00
2011-02-11 06:56:47 +00:00
/*** INIT RFB CLIENT ***/
rfb_client = rfbGetClient(8, 3, 4); /* 32-bpp client */
/* Store Guac client in rfb client */
rfbClientSetClientData(rfb_client, __GUAC_CLIENT, client);
2011-02-11 06:56:47 +00:00
/* Framebuffer update handler */
rfb_client->GotFrameBufferUpdate = guac_vnc_update;
rfb_client->GotCopyRect = guac_vnc_copyrect;
/* Do not handle clipboard and local cursor if read-only */
if (read_only == 0) {
/* Enable client-side cursor */
rfb_client->GotCursorShape = guac_vnc_cursor;
rfb_client->appData.useRemoteCursor = TRUE;
/* Clipboard */
rfb_client->GotXCutText = guac_vnc_cut_text;
}
/* Password */
rfb_client->GetPassword = guac_vnc_get_password;
/* Hook into allocation so we can handle resize. */
guac_client_data->rfb_MallocFrameBuffer = rfb_client->MallocFrameBuffer;
rfb_client->MallocFrameBuffer = guac_vnc_malloc_framebuffer;
rfb_client->canHandleNewFBSize = 1;
2011-02-11 06:56:47 +00:00
2010-12-08 21:14:04 +00:00
/* Set hostname and port */
rfb_client->serverHost = strdup(argv[0]);
rfb_client->serverPort = atoi(argv[1]);
2010-12-08 21:14:04 +00:00
2011-03-14 01:58:31 +00:00
/* Set encodings if specified */
if (argv[3][0] != '\0')
rfb_client->appData.encodingsString = guac_client_data->encodings = strdup(argv[3]);
else
guac_client_data->encodings = NULL;
2010-12-08 21:14:04 +00:00
/* Connect */
if (!rfbInitClient(rfb_client, NULL, NULL)) {
guac_send_error(client->io, "Error initializing VNC client");
guac_flush(client->io);
return 1;
}
/* Set remaining client data */
guac_client_data->rfb_client = rfb_client;
guac_client_data->copy_rect_used = 0;
/* Set handlers */
client->handle_messages = vnc_guac_client_handle_messages;
client->free_handler = vnc_guac_client_free_handler;
2011-02-11 06:56:47 +00:00
if (read_only == 0) {
/* Do not handle mouse/keyboard/clipboard if read-only */
client->mouse_handler = vnc_guac_client_mouse_handler;
client->key_handler = vnc_guac_client_key_handler;
client->clipboard_handler = vnc_guac_client_clipboard_handler;
}
2010-12-08 21:14:04 +00:00
/* Send name */
guac_send_name(client->io, rfb_client->desktopName);
/* Send size */
guac_send_size(client->io, rfb_client->width, rfb_client->height);
return 0;
}