Home | History | Annotate | Download | only in libvncclient
      1 /*
      2  *  Copyright (C) 2001,2002 Constantin Kaplinsky.  All Rights Reserved.
      3  *
      4  *  This is free software; you can redistribute it and/or modify
      5  *  it under the terms of the GNU General Public License as published by
      6  *  the Free Software Foundation; either version 2 of the License, or
      7  *  (at your option) any later version.
      8  *
      9  *  This software is distributed in the hope that it will be useful,
     10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12  *  GNU General Public License for more details.
     13  *
     14  *  You should have received a copy of the GNU General Public License
     15  *  along with this software; if not, write to the Free Software
     16  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
     17  *  USA.
     18  */
     19 
     20 /*
     21  * cursor.c - code to support cursor shape updates (XCursor and
     22  * RichCursor preudo-encodings).
     23  */
     24 
     25 #include <rfb/rfbclient.h>
     26 
     27 
     28 #define OPER_SAVE     0
     29 #define OPER_RESTORE  1
     30 
     31 #define RGB24_TO_PIXEL(bpp,r,g,b)                                       \
     32    ((((uint##bpp##_t)(r) & 0xFF) * client->format.redMax + 127) / 255             \
     33     << client->format.redShift |                                              \
     34     (((uint##bpp##_t)(g) & 0xFF) * client->format.greenMax + 127) / 255           \
     35     << client->format.greenShift |                                            \
     36     (((uint##bpp##_t)(b) & 0xFF) * client->format.blueMax + 127) / 255            \
     37     << client->format.blueShift)
     38 
     39 
     40 /*********************************************************************
     41  * HandleCursorShape(). Support for XCursor and RichCursor shape
     42  * updates. We emulate cursor operating on the frame buffer (that is
     43  * why we call it "software cursor").
     44  ********************************************************************/
     45 
     46 rfbBool HandleCursorShape(rfbClient* client,int xhot, int yhot, int width, int height, uint32_t enc)
     47 {
     48   int bytesPerPixel;
     49   size_t bytesPerRow, bytesMaskData;
     50   rfbXCursorColors rgb;
     51   uint32_t colors[2];
     52   char *buf;
     53   uint8_t *ptr;
     54   int x, y, b;
     55 
     56   bytesPerPixel = client->format.bitsPerPixel / 8;
     57   bytesPerRow = (width + 7) / 8;
     58   bytesMaskData = bytesPerRow * height;
     59 
     60   if (width * height == 0)
     61     return TRUE;
     62 
     63   /* Allocate memory for pixel data and temporary mask data. */
     64   if(client->rcSource)
     65     free(client->rcSource);
     66 
     67   client->rcSource = malloc(width * height * bytesPerPixel);
     68   if (client->rcSource == NULL)
     69     return FALSE;
     70 
     71   buf = malloc(bytesMaskData);
     72   if (buf == NULL) {
     73     free(client->rcSource);
     74     client->rcSource = NULL;
     75     return FALSE;
     76   }
     77 
     78   /* Read and decode cursor pixel data, depending on the encoding type. */
     79 
     80   if (enc == rfbEncodingXCursor) {
     81     /* Read and convert background and foreground colors. */
     82     if (!ReadFromRFBServer(client, (char *)&rgb, sz_rfbXCursorColors)) {
     83       free(client->rcSource);
     84       client->rcSource = NULL;
     85       free(buf);
     86       return FALSE;
     87     }
     88     colors[0] = RGB24_TO_PIXEL(32, rgb.backRed, rgb.backGreen, rgb.backBlue);
     89     colors[1] = RGB24_TO_PIXEL(32, rgb.foreRed, rgb.foreGreen, rgb.foreBlue);
     90 
     91     /* Read 1bpp pixel data into a temporary buffer. */
     92     if (!ReadFromRFBServer(client, buf, bytesMaskData)) {
     93       free(client->rcSource);
     94       client->rcSource = NULL;
     95       free(buf);
     96       return FALSE;
     97     }
     98 
     99     /* Convert 1bpp data to byte-wide color indices. */
    100     ptr = client->rcSource;
    101     for (y = 0; y < height; y++) {
    102       for (x = 0; x < width / 8; x++) {
    103 	for (b = 7; b >= 0; b--) {
    104 	  *ptr = buf[y * bytesPerRow + x] >> b & 1;
    105 	  ptr += bytesPerPixel;
    106 	}
    107       }
    108       for (b = 7; b > 7 - width % 8; b--) {
    109 	*ptr = buf[y * bytesPerRow + x] >> b & 1;
    110 	ptr += bytesPerPixel;
    111       }
    112     }
    113 
    114     /* Convert indices into the actual pixel values. */
    115     switch (bytesPerPixel) {
    116     case 1:
    117       for (x = 0; x < width * height; x++)
    118 	client->rcSource[x] = (uint8_t)colors[client->rcSource[x]];
    119       break;
    120     case 2:
    121       for (x = 0; x < width * height; x++)
    122 	((uint16_t *)client->rcSource)[x] = (uint16_t)colors[client->rcSource[x * 2]];
    123       break;
    124     case 4:
    125       for (x = 0; x < width * height; x++)
    126 	((uint32_t *)client->rcSource)[x] = colors[client->rcSource[x * 4]];
    127       break;
    128     }
    129 
    130   } else {			/* enc == rfbEncodingRichCursor */
    131 
    132     if (!ReadFromRFBServer(client, (char *)client->rcSource, width * height * bytesPerPixel)) {
    133       free(client->rcSource);
    134       client->rcSource = NULL;
    135       free(buf);
    136       return FALSE;
    137     }
    138 
    139   }
    140 
    141   /* Read and decode mask data. */
    142 
    143   if (!ReadFromRFBServer(client, buf, bytesMaskData)) {
    144     free(client->rcSource);
    145     client->rcSource = NULL;
    146     free(buf);
    147     return FALSE;
    148   }
    149 
    150   client->rcMask = malloc(width * height);
    151   if (client->rcMask == NULL) {
    152     free(client->rcSource);
    153     client->rcSource = NULL;
    154     free(buf);
    155     return FALSE;
    156   }
    157 
    158   ptr = client->rcMask;
    159   for (y = 0; y < height; y++) {
    160     for (x = 0; x < width / 8; x++) {
    161       for (b = 7; b >= 0; b--) {
    162 	*ptr++ = buf[y * bytesPerRow + x] >> b & 1;
    163       }
    164     }
    165     for (b = 7; b > 7 - width % 8; b--) {
    166       *ptr++ = buf[y * bytesPerRow + x] >> b & 1;
    167     }
    168   }
    169 
    170   if (client->GotCursorShape != NULL) {
    171      client->GotCursorShape(client, xhot, yhot, width, height, bytesPerPixel);
    172   }
    173 
    174   free(buf);
    175 
    176   return TRUE;
    177 }
    178 
    179 
    180