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

131 lines
4.3 KiB
C
Raw Normal View History

/*
* Copyright (C) 2013 Glyptodon LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "config.h"
#include "client.h"
#include "guac_cursor.h"
#include "guac_display.h"
#include "guac_surface.h"
#include "vnc.h"
#include <cairo/cairo.h>
#include <guacamole/client.h>
#include <guacamole/layer.h>
#include <guacamole/protocol.h>
#include <guacamole/socket.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 <stdlib.h>
#include <syslog.h>
void guac_vnc_cursor(rfbClient* client, int x, int y, int w, int h, int bpp) {
guac_client* gc = rfbClientGetClientData(client, GUAC_VNC_CLIENT_KEY);
guac_vnc_client* vnc_client = (guac_vnc_client*) gc->data;
/* 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;
/* VNC image buffer */
unsigned int fb_stride = bpp * w;
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 */
for (dy = 0; dy<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 = 0; dx<w; dx++) {
unsigned char alpha, red, green, blue;
unsigned int v;
/* Read current pixel value */
switch (bpp) {
case 4:
v = *((unsigned int*) fb_current);
break;
case 2:
v = *((unsigned short*) fb_current);
break;
default:
v = *((unsigned char*) fb_current);
}
/* Translate mask to alpha */
if (*(fb_mask++)) alpha = 0xFF;
else alpha = 0x00;
/* 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 ARGB */
if (vnc_client->settings->swap_red_blue)
*(buffer_current++) = (alpha << 24) | (blue << 16) | (green << 8) | red;
else
*(buffer_current++) = (alpha << 24) | (red << 16) | (green << 8) | blue;
/* Next VNC pixel */
fb_current += bpp;
}
}
/* Update stored cursor information */
guac_common_cursor_set_argb(vnc_client->display->cursor, x, y,
buffer, w, h, stride);
/* Free surface */
free(buffer);
/* libvncclient does not free rcMask as it does rcSource */
free(client->rcMask);
}