guacamole-server/src/protocols/vnc/display.c

395 lines
13 KiB
C

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include "config.h"
#include "client.h"
#include "display.h"
#include "common/iconv.h"
#include "common/surface.h"
#include "vnc.h"
#include <cairo/cairo.h>
#include <guacamole/client.h>
#include <guacamole/layer.h>
#include <guacamole/mem.h>
#include <guacamole/protocol.h>
#include <guacamole/socket.h>
#include <guacamole/timestamp.h>
#include <rfb/rfbclient.h>
#include <rfb/rfbproto.h>
/* Define cairo_format_stride_for_width() if missing */
#ifndef HAVE_CAIRO_FORMAT_STRIDE_FOR_WIDTH
#define cairo_format_stride_for_width(format, width) (width*4)
#endif
#include <stdarg.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <syslog.h>
void guac_vnc_update(rfbClient* client, int x, int y, int w, int h) {
guac_client* gc = rfbClientGetClientData(client, GUAC_VNC_CLIENT_KEY);
guac_vnc_client* vnc_client = (guac_vnc_client*) gc->data;
#ifdef LIBVNC_CLIENT_HAS_SCREEN
int new_height = rfbClientSwap16IfLE(client->screen.height);
int new_width = rfbClientSwap16IfLE(client->screen.width);
#else
int new_height = rfbClientSwap16IfLE(client->height);
int new_width = rfbClientSwap16IfLE(client->width);
#endif
/* Resize the surface if VNC screen size has changed */
int old_height = vnc_client->display->default_surface->height;
int old_width = vnc_client->display->default_surface->width;
if (
new_height > 0 && new_width > 0
&& (new_height != old_height || new_width != old_width)
) {
guac_common_surface_resize(vnc_client->display->default_surface,
new_width, new_height);
}
int dx, dy;
/* Cairo image buffer */
int stride;
unsigned char* buffer;
unsigned char* buffer_row_current;
cairo_surface_t* surface;
/* VNC framebuffer */
unsigned int bpp;
unsigned int fb_stride;
unsigned char* fb_row_current;
/* Ignore extra update if already handled by copyrect */
if (vnc_client->copy_rect_used) {
vnc_client->copy_rect_used = 0;
return;
}
/* Init Cairo buffer */
stride = cairo_format_stride_for_width(CAIRO_FORMAT_RGB24, w);
buffer = guac_mem_alloc(h, stride);
buffer_row_current = buffer;
bpp = client->format.bitsPerPixel/8;
fb_stride = bpp * client->width;
fb_row_current = client->frameBuffer + (y * fb_stride) + (x * bpp);
/* Copy image data from VNC client to PNG */
for (dy = y; dy<y+h; dy++) {
unsigned int* buffer_current;
unsigned char* fb_current;
/* Get current buffer row, advance to next */
buffer_current = (unsigned int*) buffer_row_current;
buffer_row_current += stride;
/* Get current framebuffer row, advance to next */
fb_current = fb_row_current;
fb_row_current += fb_stride;
for (dx = x; dx<x+w; dx++) {
unsigned char red, green, blue;
unsigned int v;
switch (bpp) {
case 4:
v = *((uint32_t*) fb_current);
break;
case 2:
v = *((uint16_t*) fb_current);
break;
default:
v = *((uint8_t*) fb_current);
}
/* Translate value to RGB */
red = (v >> client->format.redShift) * 0x100 / (client->format.redMax + 1);
green = (v >> client->format.greenShift) * 0x100 / (client->format.greenMax+ 1);
blue = (v >> client->format.blueShift) * 0x100 / (client->format.blueMax + 1);
/* Output RGB */
if (vnc_client->settings->swap_red_blue)
*(buffer_current++) = (blue << 16) | (green << 8) | red;
else
*(buffer_current++) = (red << 16) | (green << 8) | blue;
fb_current += bpp;
}
}
/* Create surface from decoded buffer */
surface = cairo_image_surface_create_for_data(buffer, CAIRO_FORMAT_RGB24,
w, h, stride);
/* Draw directly to default layer */
guac_common_surface_draw(vnc_client->display->default_surface,
x, y, surface);
/* Free surface */
cairo_surface_destroy(surface);
guac_mem_free(buffer);
}
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_VNC_CLIENT_KEY);
guac_vnc_client* vnc_client = (guac_vnc_client*) gc->data;
/* Copy specified rectangle within default layer */
guac_common_surface_copy(vnc_client->display->default_surface,
src_x, src_y, w, h,
vnc_client->display->default_surface, dest_x, dest_y);
vnc_client->copy_rect_used = 1;
}
#ifdef LIBVNC_HAS_SIZE_MSG
/**
* This function does the actual work of sending the message to the RFB/VNC
* server to request the resize, and then makes sure that the client frame
* buffer is updated, as well.
*
* @param client
* The remote frame buffer client that is triggering the resize
* request.
*
* @param width
* The updated width of the screen.
*
* @param height
* The updated height of the screen.
*
* @return
* TRUE if the screen update was sent to the server, otherwise false. Note
* that a successful send of the resize message to the server does NOT mean
* that the server has any obligation to resize the display - it only
* indicates that the VNC library has successfully sent the request.
*/
static rfbBool guac_vnc_send_desktop_size(rfbClient* client, int width, int height) {
/* Get the Guacamole client data */
guac_client* gc = rfbClientGetClientData(client, GUAC_VNC_CLIENT_KEY);
guac_client_log(gc, GUAC_LOG_TRACE,
"Current screen size is %ix%i; setting new size %ix%i\n",
rfbClientSwap16IfLE(client->screen.width),
rfbClientSwap16IfLE(client->screen.height),
width, height);
#ifdef LIBVNC_CLIENT_HAS_SCREEN
/* Don't send an update if the requested dimensions are identical to current dimensions. */
if (client->screen.width == rfbClientSwap16IfLE(width) && client->screen.height == rfbClientSwap16IfLE(height)) {
guac_client_log(gc, GUAC_LOG_WARNING, "Screen size has not changed, not sending update.");
return FALSE;
}
#else
/* Don't send an update if the screen appears to be uninitialized. */
if (client->width == 0 || client->height == 0) {
guac_client_log(gc, GUAC_LOG_WARNING, "Framebuffer has not been initialized, cannot send resize.");
return FALSE;
}
/* Don't send an update if the requested dimensions are identical to current dimensions. */
if (client->width == rfbClientSwap16IfLE(width) && client->height == rfbClientSwap16IfLE(height)) {
guac_client_log(gc, GUAC_LOG_WARNING, "Screen size has not changed, not sending update.");
return FALSE;
}
#endif
/**
* Note: The RFB protocol requires two message types to be sent during a
* resize request - the first for the desktop size (total size of all
* monitors), and then a message for each screen that is attached to the
* remote server. Both libvncclient and Guacamole only support a single
* screen, so we send the desktop resize and screen resize with (nearly)
* identical data, but if one or both of these components is updated in the
* future to support multiple screens, this will need to be re-worked.
*/
/* Set up the messages. */
rfbSetDesktopSizeMsg size_msg = { 0 };
rfbExtDesktopScreen new_screen = { 0 };
/* Configure the desktop size update message. */
size_msg.type = rfbSetDesktopSize;
size_msg.width = rfbClientSwap16IfLE(width);
size_msg.height = rfbClientSwap16IfLE(height);
size_msg.numberOfScreens = 1;
#ifdef LIBVNC_CLIENT_HAS_SCREEN
/* Configure the screen update message. */
new_screen.id = GUAC_VNC_SCREEN_ID;
new_screen.x = client->screen.x;
new_screen.y = client->screen.y;
new_screen.flags = client->screen.flags;
#else
/* Assume screen starts at the origin. */
new_screen.id = GUAC_VNC_SCREEN_ID;
new_screen.x = 0;
new_screen.y = 0;
new_screen.flags = 0;
#endif // LIBVNC_CLIENT_HAS_SCREEN
new_screen.width = rfbClientSwap16IfLE(width);
new_screen.height = rfbClientSwap16IfLE(height);
/* Send the resize messages to the remote server. */
if (!WriteToRFBServer(client, (char *)&size_msg, sz_rfbSetDesktopSizeMsg)
|| !WriteToRFBServer(client, (char *)&new_screen, sz_rfbExtDesktopScreen)) {
guac_client_log(gc, GUAC_LOG_WARNING,
"Failed to send new desktop and screen size to the VNC server.");
return FALSE;
}
#ifdef LIBVNC_CLIENT_HAS_SCREEN
/* Update the client frame buffer with the requested size. */
client->screen.width = rfbClientSwap16IfLE(width);
client->screen.height = rfbClientSwap16IfLE(height);
#endif // LIBVNC_CLIENT_HAS_SCREEN
#ifdef LIBVNC_CLIENT_HAS_REQUESTED_RESIZE
client->requestedResize = FALSE;
#endif // LIBVNC_HAS_REQUESTED_RESIZE
if (!SendFramebufferUpdateRequest(client, 0, 0, width, height, FALSE)) {
guac_client_log(gc, GUAC_LOG_WARNING, "Failed to request a full screen update.");
}
#ifdef LIBVNC_CLIENT_HAS_REQUESTED_RESIZE
client->requestedResize = TRUE;
#endif // LIBVNC_HAS_REQUESTED_RESIZE
/* Update should be successful. */
return TRUE;
}
void* guac_vnc_display_set_owner_size(guac_user* owner, void* data) {
/* Pull RFB clients from provided data. */
rfbClient* rfb_client = (rfbClient*) data;
guac_user_log(owner, GUAC_LOG_DEBUG, "Sending VNC display size for owner's display.");
/* Set the display size. */
guac_vnc_display_set_size(rfb_client, owner->info.optimal_width, owner->info.optimal_height);
/* Always return NULL. */
return NULL;
}
void guac_vnc_display_set_size(rfbClient* client, int width, int height) {
/* Get the VNC client */
guac_client* gc = rfbClientGetClientData(client, GUAC_VNC_CLIENT_KEY);
guac_vnc_client* vnc_client = (guac_vnc_client*) gc->data;
/* Fit width within bounds, adjusting height to maintain aspect ratio */
guac_common_display_fit(&width, &height);
/* Fit height within bounds, adjusting width to maintain aspect ratio */
guac_common_display_fit(&height, &width);
/* Acquire the lock for sending messages to server. */
pthread_mutex_lock(&(vnc_client->message_lock));
/* Send the display size update. */
guac_client_log(gc, GUAC_LOG_TRACE, "Setting VNC display size.");
if (guac_vnc_send_desktop_size(client, width, height))
guac_client_log(gc, GUAC_LOG_TRACE, "Successfully sent desktop size message.");
else
guac_client_log(gc, GUAC_LOG_TRACE, "Failed to send desktop size message.");
/* Release the lock. */
pthread_mutex_unlock(&(vnc_client->message_lock));
}
#endif // LIBVNC_HAS_SIZE_MSG
void guac_vnc_set_pixel_format(rfbClient* client, int color_depth) {
client->format.trueColour = 1;
switch(color_depth) {
case 8:
client->format.depth = 8;
client->format.bitsPerPixel = 8;
client->format.blueShift = 6;
client->format.redShift = 0;
client->format.greenShift = 3;
client->format.blueMax = 3;
client->format.redMax = 7;
client->format.greenMax = 7;
break;
case 16:
client->format.depth = 16;
client->format.bitsPerPixel = 16;
client->format.blueShift = 0;
client->format.redShift = 11;
client->format.greenShift = 5;
client->format.blueMax = 0x1f;
client->format.redMax = 0x1f;
client->format.greenMax = 0x3f;
break;
case 24:
case 32:
default:
client->format.depth = 24;
client->format.bitsPerPixel = 32;
client->format.blueShift = 0;
client->format.redShift = 16;
client->format.greenShift = 8;
client->format.blueMax = 0xff;
client->format.redMax = 0xff;
client->format.greenMax = 0xff;
}
}
rfbBool guac_vnc_malloc_framebuffer(rfbClient* rfb_client) {
guac_client* gc = rfbClientGetClientData(rfb_client, GUAC_VNC_CLIENT_KEY);
guac_vnc_client* vnc_client = (guac_vnc_client*) gc->data;
/* Resize surface */
if (vnc_client->display != NULL)
guac_common_surface_resize(vnc_client->display->default_surface,
rfb_client->width, rfb_client->height);
/* Use original, wrapped proc */
return vnc_client->rfb_MallocFrameBuffer(rfb_client);
}