Blame test/cursortest.c

Packit 9c64f8
/*
Packit 9c64f8
 * 
Packit 9c64f8
 * This is an example of how to use libvncserver.
Packit 9c64f8
 * 
Packit 9c64f8
 * libvncserver example
Packit 9c64f8
 * Copyright (C) 2005 Johannes E. Schindelin <Johannes.Schindelin@gmx.de>,
Packit 9c64f8
 * 		Karl Runge <runge@karlrunge.com>
Packit 9c64f8
 * 
Packit 9c64f8
 *  This is free software; you can redistribute it and/or modify
Packit 9c64f8
 *  it under the terms of the GNU General Public License as published by
Packit 9c64f8
 *  the Free Software Foundation; either version 2 of the License, or
Packit 9c64f8
 *  (at your option) any later version.
Packit 9c64f8
 *
Packit 9c64f8
 *  This software is distributed in the hope that it will be useful,
Packit 9c64f8
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 9c64f8
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 9c64f8
 *  GNU General Public License for more details.
Packit 9c64f8
 *
Packit 9c64f8
 *  You should have received a copy of the GNU General Public License
Packit 9c64f8
 *  along with this software; if not, write to the Free Software
Packit 9c64f8
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
Packit 9c64f8
 *  USA.
Packit 9c64f8
 */
Packit 9c64f8
Packit 9c64f8
#include <rfb/rfb.h>
Packit 9c64f8
Packit 9c64f8
static const int bpp=4;
Packit 9c64f8
static int maxx=800, maxy=600;
Packit 9c64f8
Packit 9c64f8
/* This initializes a nice (?) background */
Packit 9c64f8
Packit 9c64f8
static void initBuffer(unsigned char* buffer)
Packit 9c64f8
{
Packit 9c64f8
	int i,j;
Packit 9c64f8
	for(j=0;j
Packit 9c64f8
		for(i=0;i
Packit 9c64f8
			buffer[(j*maxx+i)*bpp+0]=(i+j)*128/(maxx+maxy); /* red */
Packit 9c64f8
			buffer[(j*maxx+i)*bpp+1]=i*128/maxx; /* green */
Packit 9c64f8
			buffer[(j*maxx+i)*bpp+2]=j*256/maxy; /* blue */
Packit 9c64f8
		}
Packit 9c64f8
	}
Packit 9c64f8
}
Packit 9c64f8
Packit 9c64f8
/* Example for an XCursor (foreground/background only) */
Packit 9c64f8
Packit 9c64f8
static void SetXCursor(rfbScreenInfoPtr rfbScreen)
Packit 9c64f8
{
Packit 9c64f8
	int width=13,height=11;
Packit 9c64f8
	char cursor[]=
Packit 9c64f8
		"             "
Packit 9c64f8
		" xx       xx "
Packit 9c64f8
		"  xx     xx  "
Packit 9c64f8
		"   xx   xx   "
Packit 9c64f8
		"    xx xx    "
Packit 9c64f8
		"     xxx     "
Packit 9c64f8
		"    xx xx    "
Packit 9c64f8
		"   xx   xx   "
Packit 9c64f8
		"  xx     xx  "
Packit 9c64f8
		" xx       xx "
Packit 9c64f8
		"             ",
Packit 9c64f8
	     mask[]=
Packit 9c64f8
		"xxxx     xxxx"
Packit 9c64f8
		"xxxx     xxxx"
Packit 9c64f8
		" xxxx   xxxx "
Packit 9c64f8
		"  xxxx xxxx  "
Packit 9c64f8
		"   xxxxxxx   "
Packit 9c64f8
		"    xxxxx    "
Packit 9c64f8
		"   xxxxxxx   "
Packit 9c64f8
		"  xxxx xxxx  "
Packit 9c64f8
		" xxxx   xxxx "
Packit 9c64f8
		"xxxx     xxxx"
Packit 9c64f8
		"xxxx     xxxx";
Packit 9c64f8
	rfbCursorPtr c;
Packit 9c64f8
	
Packit 9c64f8
	c=rfbMakeXCursor(width,height,cursor,mask);
Packit 9c64f8
	c->xhot=width/2;c->yhot=height/2;
Packit 9c64f8
Packit 9c64f8
	rfbSetCursor(rfbScreen, c);
Packit 9c64f8
}
Packit 9c64f8
Packit 9c64f8
static void SetXCursor2(rfbScreenInfoPtr rfbScreen)
Packit 9c64f8
{
Packit 9c64f8
	int width=13,height=22;
Packit 9c64f8
	char cursor[]=
Packit 9c64f8
		" xx          "
Packit 9c64f8
		" x x         "
Packit 9c64f8
		" x  x        "
Packit 9c64f8
		" x   x       "
Packit 9c64f8
		" x    x      "
Packit 9c64f8
		" x     x     "
Packit 9c64f8
		" x      x    "
Packit 9c64f8
		" x       x   "
Packit 9c64f8
		" x     xx x  "
Packit 9c64f8
		" x x   x xxx "
Packit 9c64f8
		" x xx  x   x "
Packit 9c64f8
		" xx x   x    "
Packit 9c64f8
		" xx  x  x    "
Packit 9c64f8
		" x    x  x   "
Packit 9c64f8
		" x    x  x   "
Packit 9c64f8
		"       x  x  "
Packit 9c64f8
		"        x  x "
Packit 9c64f8
		"        x  x "
Packit 9c64f8
		"         xx  "
Packit 9c64f8
		"             "
Packit 9c64f8
		"             ",
Packit 9c64f8
	     mask[]=
Packit 9c64f8
		"xxx          "
Packit 9c64f8
		"xxxx         "
Packit 9c64f8
		"xxxxx        "
Packit 9c64f8
		"xxxxxx       "
Packit 9c64f8
		"xxxxxxx      "
Packit 9c64f8
		"xxxxxxxx     "
Packit 9c64f8
		"xxxxxxxxx    "
Packit 9c64f8
		"xxxxxxxxxx   "
Packit 9c64f8
		"xxxxxxxxxxx  "
Packit 9c64f8
		"xxxxxxxxxxxx "
Packit 9c64f8
		"xxxxxxxxxxxxx"
Packit 9c64f8
		"xxxxxxxxxxxxx"
Packit 9c64f8
		"xxxxxxxxxx  x"
Packit 9c64f8
		"xxxxxxxxxx   "
Packit 9c64f8
		"xxx  xxxxxx  "
Packit 9c64f8
		"xxx  xxxxxx  "
Packit 9c64f8
		"xx    xxxxxx "
Packit 9c64f8
		"       xxxxx "
Packit 9c64f8
		"       xxxxxx"
Packit 9c64f8
		"        xxxxx"
Packit 9c64f8
		"         xxx "
Packit 9c64f8
		"             ";
Packit 9c64f8
	rfbCursorPtr c;
Packit 9c64f8
	
Packit 9c64f8
	c=rfbMakeXCursor(width,height,cursor,mask);
Packit 9c64f8
	c->xhot=0;c->yhot=0;
Packit 9c64f8
Packit 9c64f8
	rfbSetCursor(rfbScreen, c);
Packit 9c64f8
}
Packit 9c64f8
Packit 9c64f8
/* Example for a rich cursor (full-colour) */
Packit 9c64f8
Packit 9c64f8
static void SetRichCursor(rfbScreenInfoPtr rfbScreen)
Packit 9c64f8
{
Packit 9c64f8
	int i,j,w=32,h=32;
Packit 9c64f8
	/* runge */
Packit 9c64f8
	/*  rfbCursorPtr c = rfbScreen->cursor; */
Packit 9c64f8
	rfbCursorPtr c;
Packit 9c64f8
	char bitmap[]=
Packit 9c64f8
		"                                "
Packit 9c64f8
		"              xxxxxx            "
Packit 9c64f8
		"       xxxxxxxxxxxxxxxxx        "
Packit 9c64f8
		"      xxxxxxxxxxxxxxxxxxxxxx    "
Packit 9c64f8
		"    xxxxx  xxxxxxxx  xxxxxxxx   "
Packit 9c64f8
		"   xxxxxxxxxxxxxxxxxxxxxxxxxxx  "
Packit 9c64f8
		"  xxxxxxxxxxxxxxxxxxxxxxxxxxxxx "
Packit 9c64f8
		"  xxxxx   xxxxxxxxxxx   xxxxxxx "
Packit 9c64f8
		"  xxxx     xxxxxxxxx     xxxxxx "
Packit 9c64f8
		"  xxxxx   xxxxxxxxxxx   xxxxxxx "
Packit 9c64f8
		" xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx "
Packit 9c64f8
		" xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx "
Packit 9c64f8
		" xxxxxxxxxxxx  xxxxxxxxxxxxxxx  "
Packit 9c64f8
		" xxxxxxxxxxxxxxxxxxxxxxxxxxxx   "
Packit 9c64f8
		" xxxxxxxxxxxxxxxxxxxxxxxxxxxx   "
Packit 9c64f8
		" xxxxxxxxxxx   xxxxxxxxxxxxxx   "
Packit 9c64f8
		" xxxxxxxxxx     xxxxxxxxxxxx    "
Packit 9c64f8
		"  xxxxxxxxx      xxxxxxxxx      "
Packit 9c64f8
		"   xxxxxxxxxx   xxxxxxxxx       "
Packit 9c64f8
		"      xxxxxxxxxxxxxxxxxxx       "
Packit 9c64f8
		"       xxxxxxxxxxxxxxxxxxx      "
Packit 9c64f8
		"         xxxxxxxxxxxxxxxxxxx    "
Packit 9c64f8
		"             xxxxxxxxxxxxxxxxx  "
Packit 9c64f8
		"                xxxxxxxxxxxxxxx "
Packit 9c64f8
		"   xxxx           xxxxxxxxxxxxx "
Packit 9c64f8
		"  xx   x            xxxxxxxxxxx "
Packit 9c64f8
		"  xxx               xxxxxxxxxxx "
Packit 9c64f8
		"  xxxx             xxxxxxxxxxx  "
Packit 9c64f8
		"   xxxxxx       xxxxxxxxxxxx    "
Packit 9c64f8
		"    xxxxxxxxxxxxxxxxxxxxxx      "
Packit 9c64f8
		"      xxxxxxxxxxxxxxxx          "
Packit 9c64f8
		"                                ";
Packit 9c64f8
Packit 9c64f8
	c=rfbMakeXCursor(w,h,bitmap,bitmap);
Packit 9c64f8
	c->xhot = 16; c->yhot = 24;
Packit 9c64f8
Packit 9c64f8
	c->richSource = (char*)malloc(w*h*bpp);
Packit 9c64f8
	for(j=0;j
Packit 9c64f8
		for(i=0;i
Packit 9c64f8
			c->richSource[j*w*bpp+i*bpp+0]=i*0xff/w;
Packit 9c64f8
			c->richSource[j*w*bpp+i*bpp+1]=(i+j)*0xff/(w+h);
Packit 9c64f8
			c->richSource[j*w*bpp+i*bpp+2]=j*0xff/h;
Packit 9c64f8
			c->richSource[j*w*bpp+i*bpp+3]=0;
Packit 9c64f8
		}
Packit 9c64f8
	}
Packit 9c64f8
	rfbSetCursor(rfbScreen, c);
Packit 9c64f8
}
Packit 9c64f8
Packit 9c64f8
/* runge */
Packit 9c64f8
static void SetRichCursor2(rfbScreenInfoPtr rfbScreen)
Packit 9c64f8
{
Packit 9c64f8
	int i,j,w=17,h=16;
Packit 9c64f8
	/*  rfbCursorPtr c = rfbScreen->cursor; */
Packit 9c64f8
	rfbCursorPtr c;
Packit 9c64f8
	char bitmap[]=
Packit 9c64f8
		"                 "
Packit 9c64f8
		"xxxx             "
Packit 9c64f8
		"xxxxxxxx         "
Packit 9c64f8
		"xxxxxxxxxxxx    x"
Packit 9c64f8
		"xxx  xxxxxxxx   x"
Packit 9c64f8
		"xxxxxxxxxxxxxx  x"
Packit 9c64f8
		"xxxxxxxxxxxxxxx x"
Packit 9c64f8
		"xxxxx   xxxxxxx x"
Packit 9c64f8
		"xxxx     xxxxxx x"
Packit 9c64f8
		"xxxxx   xxxxxxx x"
Packit 9c64f8
		"xxxxxxxxxxxxxxx x"
Packit 9c64f8
		"xxxxxxxxxxxxxxx x"
Packit 9c64f8
		"xxxxxxxxxxxxxx  x"
Packit 9c64f8
		"xxxxxxxxxxxxx   x"
Packit 9c64f8
		"xxxxxxxxxxxxx   x"
Packit 9c64f8
		"xxxxxxxxxxxxx   x";
Packit 9c64f8
	/*  c=rfbScreen->cursor = rfbMakeXCursor(w,h,bitmap,bitmap); */
Packit 9c64f8
	c=rfbMakeXCursor(w,h,bitmap,bitmap);
Packit 9c64f8
	c->xhot = 5; c->yhot = 7;
Packit 9c64f8
Packit 9c64f8
	c->richSource = (char*)malloc(w*h*bpp);
Packit 9c64f8
	for(j=0;j
Packit 9c64f8
		for(i=0;i
Packit 9c64f8
			c->richSource[j*w*bpp+i*bpp+0]=0xff;
Packit 9c64f8
			c->richSource[j*w*bpp+i*bpp+1]=0x00;
Packit 9c64f8
			c->richSource[j*w*bpp+i*bpp+2]=0x7f;
Packit 9c64f8
			c->richSource[j*w*bpp+i*bpp+3]=0;
Packit 9c64f8
		}
Packit 9c64f8
	}
Packit 9c64f8
	rfbSetCursor(rfbScreen, c);
Packit 9c64f8
}
Packit 9c64f8
Packit 9c64f8
/* alpha channel */
Packit 9c64f8
Packit 9c64f8
static void SetAlphaCursor(rfbScreenInfoPtr screen,int mode)
Packit 9c64f8
{
Packit 9c64f8
	int i,j;
Packit 9c64f8
	rfbCursorPtr c = screen->cursor;
Packit 9c64f8
	int maskStride=(c->width+7)/8;
Packit 9c64f8
Packit 9c64f8
	if(!c)
Packit 9c64f8
		return;
Packit 9c64f8
Packit 9c64f8
	if(c->alphaSource) {
Packit 9c64f8
		free(c->alphaSource);
Packit 9c64f8
		c->alphaSource=NULL;
Packit 9c64f8
	}
Packit 9c64f8
Packit 9c64f8
	if(mode==0)
Packit 9c64f8
		return;
Packit 9c64f8
Packit 9c64f8
	c->alphaSource = (unsigned char*)malloc(c->width*c->height);
Packit 9c64f8
Packit 9c64f8
	for(j=0;j<c->height;j++)
Packit 9c64f8
		for(i=0;i<c->width;i++) {
Packit 9c64f8
			unsigned char value=0x100*i/c->width;
Packit 9c64f8
			rfbBool masked=(c->mask[(i/8)+maskStride*j]<<(i&7))&0x80;
Packit 9c64f8
			c->alphaSource[i+c->width*j]=(masked?(mode==1?value:0xff-value):0);
Packit 9c64f8
		}
Packit 9c64f8
	if(c->cleanupMask)
Packit 9c64f8
		free(c->mask);
Packit 9c64f8
	c->mask=rfbMakeMaskFromAlphaSource(c->width,c->height,c->alphaSource);
Packit 9c64f8
	c->cleanupMask=TRUE;
Packit 9c64f8
}
Packit 9c64f8
Packit 9c64f8
/* Here the pointer events are handled */
Packit 9c64f8
Packit 9c64f8
static void doptr(int buttonMask,int x,int y,rfbClientPtr cl)
Packit 9c64f8
{
Packit 9c64f8
	static int oldButtonMask=0;
Packit 9c64f8
	static int counter=0;
Packit 9c64f8
Packit 9c64f8
	if((oldButtonMask&1)==0 && (buttonMask&1)==1) {
Packit 9c64f8
		switch(++counter) {
Packit 9c64f8
		case 7:
Packit 9c64f8
			SetRichCursor(cl->screen);
Packit 9c64f8
			SetAlphaCursor(cl->screen,2);
Packit 9c64f8
			break;
Packit 9c64f8
		case 6:
Packit 9c64f8
			SetRichCursor(cl->screen);
Packit 9c64f8
			SetAlphaCursor(cl->screen,1);
Packit 9c64f8
			break;
Packit 9c64f8
		case 5:
Packit 9c64f8
			SetRichCursor2(cl->screen);
Packit 9c64f8
			SetAlphaCursor(cl->screen,0);
Packit 9c64f8
			break;
Packit 9c64f8
		case 4:
Packit 9c64f8
			SetXCursor(cl->screen);
Packit 9c64f8
			break;
Packit 9c64f8
		case 3:
Packit 9c64f8
			SetRichCursor2(cl->screen);
Packit 9c64f8
			SetAlphaCursor(cl->screen,2);
Packit 9c64f8
			break;
Packit 9c64f8
		case 2:
Packit 9c64f8
			SetXCursor(cl->screen);
Packit 9c64f8
			SetAlphaCursor(cl->screen,2);
Packit 9c64f8
			break;
Packit 9c64f8
		case 1:
Packit 9c64f8
			SetXCursor2(cl->screen);
Packit 9c64f8
			SetAlphaCursor(cl->screen,0);
Packit 9c64f8
			break;
Packit 9c64f8
		default:
Packit 9c64f8
			SetRichCursor(cl->screen);
Packit 9c64f8
			counter=0;
Packit 9c64f8
		}
Packit 9c64f8
	}
Packit 9c64f8
	if(buttonMask&2) {
Packit 9c64f8
		rfbScreenCleanup(cl->screen);
Packit 9c64f8
		exit(0);
Packit 9c64f8
	}
Packit 9c64f8
Packit 9c64f8
	if(buttonMask&4)
Packit 9c64f8
		rfbCloseClient(cl);
Packit 9c64f8
Packit 9c64f8
Packit 9c64f8
	oldButtonMask=buttonMask;
Packit 9c64f8
Packit 9c64f8
	rfbDefaultPtrAddEvent(buttonMask,x,y,cl);
Packit 9c64f8
}
Packit 9c64f8
Packit 9c64f8
/* Initialization */
Packit 9c64f8
Packit 9c64f8
int main(int argc,char** argv)
Packit 9c64f8
{
Packit 9c64f8
	rfbScreenInfoPtr rfbScreen = rfbGetScreen(&argc,argv,maxx,maxy,8,3,bpp);
Packit 9c64f8
        if(!rfbScreen)
Packit 9c64f8
          return 0;
Packit 9c64f8
Packit 9c64f8
	rfbScreen->desktopName = "Cursor Test";
Packit 9c64f8
	rfbScreen->frameBuffer = (char*)malloc(maxx*maxy*bpp);
Packit 9c64f8
	rfbScreen->ptrAddEvent = doptr;
Packit 9c64f8
Packit 9c64f8
	initBuffer((unsigned char*)rfbScreen->frameBuffer);
Packit 9c64f8
Packit 9c64f8
Packit 9c64f8
	SetRichCursor(rfbScreen);
Packit 9c64f8
Packit 9c64f8
	/* initialize the server */
Packit 9c64f8
	rfbInitServer(rfbScreen);
Packit 9c64f8
Packit 9c64f8
	rfbLog("Change cursor shape with left mouse button,\n\t"
Packit 9c64f8
			"quit with right one (middle button quits server).\n");
Packit 9c64f8
Packit 9c64f8
	/* this is the blocking event loop, i.e. it never returns */
Packit 9c64f8
	/* 40000 are the microseconds to wait on select(), i.e. 0.04 seconds */
Packit 9c64f8
	rfbRunEventLoop(rfbScreen,40000,FALSE);
Packit 9c64f8
Packit 9c64f8
	free(rfbScreen->frameBuffer);
Packit 9c64f8
	rfbScreenCleanup(rfbScreen);
Packit 9c64f8
Packit 9c64f8
	return(0);
Packit 9c64f8
}
Packit 9c64f8