Commit 9f07e222 authored by dscho's avatar dscho

rfbSelectBox, consoleFonts, too many changes

parent ac263ce9
0.2
added rfbLoadConsoleFont
fixed font colour handling.
added rfbSelectBox
added rfbDrawCharWithClip to allow for clipping and a background colour.
fixed font colours
added rfbFillRect
added IO function to check password.
rfbNewClient now sets the socket in the fd_set (for the select() call)
when compiling the library with HAVE_PTHREADS and an application
which includes "rfb.h" without, the structures got mixed up.
......
......@@ -2,8 +2,8 @@ INCLUDES=-I.
VNCSERVERLIB=-L. -lvncserver -L/usr/local/lib -lz -ljpeg
# Uncomment these two lines to enable use of PThreads
PTHREADDEF = -DHAVE_PTHREADS
PTHREADLIB = -lpthread
#PTHREADDEF = -DHAVE_PTHREADS
#PTHREADLIB = -lpthread
# Comment the following line to disable the use of 3 Bytes/Pixel.
# The code for 3 Bytes/Pixel is not very efficient!
......@@ -23,11 +23,11 @@ OSX_LIBS = -framework ApplicationServices -framework Carbon
SOURCES=main.c rfbserver.c sraRegion.c auth.c sockets.c \
stats.c corre.c hextile.c rre.c translate.c cutpaste.c \
zlib.c tight.c httpd.c cursor.c font.c \
d3des.c vncauth.c
draw.c selbox.c d3des.c vncauth.c
OBJS=main.o rfbserver.o sraRegion.o auth.o sockets.o \
stats.o corre.o hextile.o rre.o translate.o cutpaste.o \
zlib.o tight.o httpd.o cursor.o font.o \
d3des.o vncauth.o
draw.o selbox.o d3des.o vncauth.o
INSTALLHEADER=rfb.h rfbproto.h sraRegion.h keysym.h
all: example pnmshow storepasswd
......@@ -73,6 +73,9 @@ blooptest.o: example.c rfb.h
pnmshow24: pnmshow24.o libvncserver.a
$(CC) -o pnmshow24 pnmshow24.o $(LIBS)
fontsel: fontsel.o libvncserver.a
$(CC) -o fontsel fontsel.o -L. -lvncserver -lz -ljpeg
clean:
rm -f $(OBJS) *~ core "#"* *.bak *.orig storepasswd.o \
mac.o example.o pnmshow.o pnmshow24.o sratest.o \
......
......@@ -322,6 +322,33 @@ http://www.gimp.org/win32/extralibs-dev-20001007.zip
Thanks go to all the GIMP team!
What are those other targets in the Makefile?
---------------------------------------------
OSXvnc-server is the original OSXvnc adapted to use the library, which was in
turn adapted from OSXvnc. As you easily can see, the OSX dependend part is
minimal.
storepasswd is the original program to save a vnc style password in a file.
Unfortunately, authentication as every vncviewer speaks it means the server
has to know the plain password. You really should tunnel via ssh or use
your own PasswordCheck to build a PIN/TAN system.
sratest is a test unit. Run it to assert correct behaviour of sraRegion. I
wrote this to test my iterator implementation.
blooptest is a test of pthreads. It is just the example, but with a background
loop to hunt down thread lockups.
pnmshow24 is like pnmshow, but it uses 3 bytes/pixel internally, which is not
as efficient as 4 bytes/pixel for translation, because there is no native data
type of that size, so you have to memcpy pixels and be real cautious with
endianness. Anyway, it works.
fontsel is a test for rfbSelectBox and rfbLoadConsoleFont. If you have Linux
console fonts, you can browse them via VNC. Directory browsing not implemented
yet :-(
Why I don't feel bad about GPL
------------------------------
......
immediate:
----------
authentification schemes (secure vnc)
udp (need an rfbClientPtr udpClient in rfbScreen)
documentation
hint that to mark very tiny regions as
modified is possibly inefficient for the encodings.
......@@ -11,14 +9,23 @@ documentation
later:
------
authentification schemes (secure vnc)
IO function ptr exists; now explain how to tunnel and implement a
client address restriction scheme.
autoconf? at least Sun Solaris and Windows compilation
(maybe Michael makes that)
(maybe Michael makes a small autconf)
using Hermes library for fast colour translations.
CORBA
internal HTTP tunnelling feature (needs a special GET target and a few
changes to java applet).
done:
-----
.empty cursor sending doesn't work.
.udp (need an rfbClientPtr udpClient in rfbScreen)
input only; nearly untested (don't have the clients).
.font handling: bpp>1
.test copyRect and pthreads.
.optionally dont draw rich cursors as xcursors
.cursor smears on IRIX with pthreads, then has bus error. has to be a mutex
......
......@@ -73,8 +73,7 @@ void
rfbAuthProcessClientMessage(cl)
rfbClientPtr cl;
{
char passwd[1024];
int i, n;
int n;
CARD8 response[CHALLENGESIZE];
CARD32 authResult;
......@@ -85,8 +84,8 @@ rfbAuthProcessClientMessage(cl)
return;
}
if(!cl->screen->getPassword(cl,passwd,MAXPWLEN)) {
rfbLog("rfbAuthProcessClientMessage: could not get password\n");
if(!cl->screen->passwordCheck(cl,response,CHALLENGESIZE)) {
rfbLog("rfbAuthProcessClientMessage: password check failed\n");
authResult = Swap32IfLE(rfbVncAuthFailed);
if (WriteExact(cl, (char *)&authResult, 4) < 0) {
rfbLogPerror("rfbAuthProcessClientMessage: write");
......@@ -95,28 +94,6 @@ rfbAuthProcessClientMessage(cl)
return;
}
vncEncryptBytes(cl->authChallenge, passwd);
/* Lose the password from memory */
for (i = strlen(passwd); i >= 0; i--) {
passwd[i] = '\0';
}
free((char *)passwd);
if (memcmp(cl->authChallenge, response, CHALLENGESIZE) != 0) {
rfbLog("rfbAuthProcessClientMessage: authentication failed from %s\n",
cl->host);
authResult = Swap32IfLE(rfbVncAuthFailed);
if (WriteExact(cl, (char *)&authResult, 4) < 0) {
rfbLogPerror("rfbAuthProcessClientMessage: write");
}
rfbCloseClient(cl);
return;
}
authResult = Swap32IfLE(rfbVncAuthOK);
if (WriteExact(cl, (char *)&authResult, 4) < 0) {
......
......@@ -20,9 +20,17 @@ while(<>) {
} elsif(/^ENCODING (.*)$/) {
$glyphindex=$1;
$searchfor="BBX";
$dwidth=0;
} elsif(/^DWIDTH (.*) (.*)/) {
$dwidth=$1;
} elsif(/^BBX (.*) (.*) (.*) (.*)$/) {
($width,$height,$x,$y)=($1,$2,$3,$4);
@encodings[$glyphindex*5..($glyphindex*5+4)]=($counter,$width,$height,$x,$y);
if($dwidth != 0) {
$encodings[$glyphindex*5+1]=$dwidth;
} else {
$dwidth=$width;
}
$searchfor="BITMAP";
} elsif(/^BITMAP/) {
$i=1;
......@@ -31,8 +39,11 @@ while(<>) {
$i=0;
$out.=" /* $glyphindex */\n";
} else {
$_=substr($_,0,length($_)-1);
$counter+=length($_)/2;
if(int(($dwidth+7)/8) > int(($width+7)/8)) {
$_ .= "00"x(int(($dwidth+7)/8)-int(($width+7)/8));
}
$_=substr($_,0,(int(($dwidth+7)/8)*2));
$counter+=(int(($dwidth+7)/8));
s/(..)/$nullx$1,/g;
$out.=$_;
$i++;
......
#!/usr/bin/perl
# convert a linux console font (8x16 format) to a font definition
# suitable for processing with LibVNCServer
#if($#ARGV == 0) { exit; }
foreach $i (@ARGV) {
$fontname = $i;
$fontname =~ s/\.fnt$//;
$fontname =~ s/^.*\///g;
$fontname =~ y/+/_/;
print STDERR "$i -> $fontname\n";
open IN, "<$i";
print STDERR read(IN,$x,4096);
close(IN);
open OUT, ">$fontname.h";
print OUT "unsigned char ".$fontname."FontData[4096+1]={";
for($i=0;$i<4096;$i++) {
if(($i%16)==0) {
print OUT "\n";
}
printf OUT "0x%02x,", ord(substr($x,$i));
}
print OUT "\n};\nint ".$fontname."FontMetaData[256*5+1]={\n";
for($i=0;$i<256;$i++) {
print OUT ($i*16).",8,16,0,0,";
}
print OUT "};\nrfbFontData ".$fontname."Font = { ".$fontname."FontData, "
.$fontname."FontMetaData };\n";
close OUT;
}
......@@ -42,21 +42,21 @@ rfbSendCursorShape(cl)
CARD8 bitmapByte;
pCursor = cl->screen->getCursorPtr(cl);
if(!pCursor) return TRUE;
//if(!pCursor) return TRUE;
if (cl->useRichCursorEncoding) {
if(!pCursor->richSource)
MakeRichCursorFromXCursor(cl->screen,pCursor);
rect.encoding = Swap32IfLE(rfbEncodingRichCursor);
if(pCursor && !pCursor->richSource)
MakeRichCursorFromXCursor(cl->screen,pCursor);
rect.encoding = Swap32IfLE(rfbEncodingRichCursor);
} else {
if(!pCursor->source)
if(pCursor && !pCursor->source)
MakeXCursorFromRichCursor(cl->screen,pCursor);
rect.encoding = Swap32IfLE(rfbEncodingXCursor);
}
/* If there is no cursor, send update with empty cursor data. */
if ( pCursor->width == 1 &&
if ( pCursor && pCursor->width == 1 &&
pCursor->height == 1 &&
pCursor->mask[0] == 0 ) {
pCursor = NULL;
......
This diff is collapsed.
#include "rfb.h"
void rfbFillRect(rfbScreenInfoPtr s,int x1,int y1,int x2,int y2,Pixel col)
{
int rowstride = s->paddedWidthInBytes, bpp = s->bitsPerPixel>>3;
int i,j;
char* colour=(char*)&col;
if(!rfbEndianTest)
colour += 4-bpp;
for(j=y1;j<y2;j++)
for(i=x1;i<x2;i++)
memcpy(s->frameBuffer+j*rowstride+i*bpp,colour,bpp);
rfbMarkRectAsModified(s,x1,y1,x2,y2);
}
#include "rfb.h"
int rfbDrawChar(rfbScreenInfoPtr rfbScreen,rfbFontDataPtr font,
int x,int y,unsigned char c,CARD32 colour)
int x,int y,unsigned char c,Pixel col)
{
int i,j,k,width,height;
int i,j,width,height;
unsigned char d;
unsigned char* data=font->data+font->metaData[c*5];
int rowstride=rfbScreen->paddedWidthInBytes;
int bpp=rfbScreen->rfbServerFormat.bitsPerPixel/8;
char *colour=(char*)&col;
if(!rfbEndianTest)
colour += 4-bpp;
width=font->metaData[c*5+1];
height=font->metaData[c*5+2];
......@@ -20,21 +24,17 @@ int rfbDrawChar(rfbScreenInfoPtr rfbScreen,rfbFontDataPtr font,
d=*data;
data++;
}
if(d&0x80) {
for(k=0;k<bpp;k++)
rfbScreen->frameBuffer[(y+j)*rowstride+(x+i)*bpp+k]=
((colour>>(8*k))&0xff);
}
if(d&0x80)
memcpy(rfbScreen->frameBuffer+(y+j)*rowstride+(x+i)*bpp,colour,bpp);
d<<=1;
}
if((i&7)==0)
data++;
if((i&7)!=0) data++;
}
return(width);
}
void rfbDrawString(rfbScreenInfoPtr rfbScreen,rfbFontDataPtr font,
int x,int y,unsigned char* string,CARD32 colour)
int x,int y,const unsigned char* string,Pixel colour)
{
while(*string) {
x+=rfbDrawChar(rfbScreen,font,x,y,*string,colour);
......@@ -42,7 +42,73 @@ void rfbDrawString(rfbScreenInfoPtr rfbScreen,rfbFontDataPtr font,
}
}
int rfbWidth(rfbFontDataPtr font,unsigned char* string)
/* TODO: these two functions need to be more efficient */
int rfbDrawCharWithClip(rfbScreenInfoPtr rfbScreen,rfbFontDataPtr font,
int x,int y,unsigned char c,
int x1,int y1,int x2,int y2,
Pixel col,Pixel bcol)
{
int i,j,width,height;
unsigned char d;
unsigned char* data=font->data+font->metaData[c*5];
int rowstride=rfbScreen->paddedWidthInBytes;
int bpp=rfbScreen->rfbServerFormat.bitsPerPixel/8,extra_bytes=0;
char* colour=(char*)&col;
char* bcolour=(char*)&bcol;
if(!rfbEndianTest) {
colour+=4-bpp;
bcolour+=4-bpp;
}
width=font->metaData[c*5+1];
height=font->metaData[c*5+2];
x+=font->metaData[c*5+3];
y+=-font->metaData[c*5+4]-height+1;
/* after clipping, x2 will be count of bytes between rows,
* x1 start of i, y1 start of j, width and height will be adjusted. */
if(y1>y) { y1-=y; d+=(width+7)/8; height-=y1; y+=y1; } else y1=0;
if(x1>x) { x1-=x; d+=x1; width-=x1; x+=x1; extra_bytes+=x1/8; } else x1=0;
if(y2<y+height) height-=y+height-y2;
if(x2<x+width) { extra_bytes+=(x1+width)/8-(x+width-x2+7)/8; width-=x+width-x2; }
for(j=y1;j<height;j++) {
for(i=x1;i<width;i++) {
if((i&7)==0) {
d=*data;
data++;
}
/* if(x+i>=x1 && x+i<x2 && y+j>=y1 && y+j<y2) */ {
if(d&0x80) {
memcpy(rfbScreen->frameBuffer+(y+j)*rowstride+(x+i)*bpp,
colour,bpp);
} else if(bcol!=col) {
memcpy(rfbScreen->frameBuffer+(y+j)*rowstride+(x+i)*bpp,
bcolour,bpp);
}
}
d<<=1;
}
//if((i&7)==0) data++;
data += extra_bytes;
}
return(width);
}
void rfbDrawStringWithClip(rfbScreenInfoPtr rfbScreen,rfbFontDataPtr font,
int x,int y,const unsigned char* string,
int x1,int y1,int x2,int y2,
Pixel colour,Pixel backColour)
{
while(*string) {
x+=rfbDrawCharWithClip(rfbScreen,font,x,y,*string,x1,y1,x2,y2,
colour,backColour);
string++;
}
}
int rfbWidthOfString(rfbFontDataPtr font,const unsigned char* string)
{
int i=0;
while(*string) {
......@@ -65,3 +131,55 @@ void rfbFontBBox(rfbFontDataPtr font,unsigned char c,int* x1,int* y1,int* x2,int
*y2=*y1+font->metaData[c*5+2];
}
void rfbWholeFontBBox(rfbFontDataPtr font,
int *x1, int *y1, int *x2, int *y2)
{
int i;
int* m=font->metaData;
(*x1)=(*y1)=INT_MAX; (*x2)=(*y2)=-INT_MAX+1;
for(i=0;i<256;i++) {
if(m[i*5+1]-m[i*5+3]>(*x2))
(*x2)=m[i*5+1]-m[i*5+3];
if(-m[i*5+2]+m[i*5+4]<(*y1))
(*y1)=-m[i*5+2]+m[i*5+4];
if(m[i*5+3]<(*x1))
(*x1)=m[i*5+3];
if(-m[i*5+4]>(*y2))
(*y2)=-m[i*5+4];
}
}
rfbFontDataPtr rfbLoadConsoleFont(char *filename)
{
FILE *f=fopen(filename,"rb");
rfbFontDataPtr p;
int i;
if(!f) return(0);
p=(rfbFontDataPtr)malloc(sizeof(rfbFontData));
p->data=(char*)malloc(4096);
if(1!=fread(p->data,4096,1,f)) {
free(p->data);
free(p);
return(0);
}
fclose(f);
p->metaData=(int*)malloc(256*5*sizeof(int));
for(i=0;i<256;i++) {
p->metaData[i*5+0]=i*16; /* offset */
p->metaData[i*5+1]=8; /* width */
p->metaData[i*5+2]=16; /* height */
p->metaData[i*5+3]=0; /* xhot */
p->metaData[i*5+4]=0; /* yhot */
}
return(p);
}
void rfbFreeFont(rfbFontDataPtr f)
{
free(f->data);
free(f->metaData);
free(f);
}
#include "rfb.h"
#define FONTDIR "/usr/lib/kbd/consolefonts/"
#define DEFAULTFONT FONTDIR "default8x16"
char *fontlist[50]={
"8x16alt", "b.fnt", "c.fnt", "default8x16", "m.fnt", "ml.fnt", "mod_d.fnt",
"mod_s.fnt", "mr.fnt", "mu.fnt", "r.fnt", "rl.fnt", "ro.fnt", "s.fnt",
"sc.fnt", "scrawl_s.fnt", "scrawl_w.fnt", "sd.fnt", "t.fnt",
0
};
rfbScreenInfoPtr rfbScreen = 0;
rfbFontDataPtr curFont = 0;
void showFont(int index)
{
char buffer[1024];
if(!rfbScreen) return;
if(curFont)
rfbFreeFont(curFont);
strcpy(buffer,FONTDIR);
strcat(buffer,fontlist[index]);
curFont = rfbLoadConsoleFont(buffer);
rfbFillRect(rfbScreen,210,30-20,210+10*16,30-20+256*20/16,0xb77797);
if(curFont) {
int i,j;
for(j=0;j<256;j+=16)
for(i=0;i<16;i++)
rfbDrawCharWithClip(rfbScreen,curFont,210+10*i,30+j*20/16,j+i,
0,0,640,480,0xffffff,0x000000);
}
}
int main(int argc,char** argv)
{
rfbFontDataPtr font;
rfbScreenInfoPtr s=rfbGetScreen(argc,argv,640,480,8,3,3);
int i,j;
s->frameBuffer=(char*)malloc(640*480*3);
rfbInitServer(s);
for(j=0;j<480;j++)
for(i=0;i<640;i++) {
s->frameBuffer[(j*640+i)*3+0]=j*256/480;
s->frameBuffer[(j*640+i)*3+1]=i*256/640;
s->frameBuffer[(j*640+i)*3+2]=(i+j)*256/(480+640);
}
rfbScreen = s;
font=rfbLoadConsoleFont(DEFAULTFONT);
if(!font) {
fprintf(stderr,"Couldn't find %s\n",DEFAULTFONT);
exit(1);
}
for(j=0;j<0;j++)
rfbProcessEvents(s,900000);
i = rfbSelectBox(s,font,fontlist,10,20,200,300,0xffdfdf,0x602040,2,showFont);
fprintf(stderr,"Selection: %d: %s\n",i,(i>=0)?fontlist[i]:"cancelled");
rfbFreeFont(font);
return(0);
}
......@@ -1629,3 +1629,6 @@ SOFTWARE.
#define XK_Korean_Won 0xeff
#endif /* XK_KOREAN */
/* Euro currency symbol */
#define XK_EuroSign 0x20ac
......@@ -176,14 +176,14 @@ void rfbMarkRectAsModified(rfbScreenInfoPtr rfbScreen,int x1,int y1,int x2,int y
int i;
if(x1>x2) { i=x1; x1=x2; x2=i; }
x2++;
if(x1<0) { x1=0; if(x2==x1) x2++; }
if(x2>=rfbScreen->width) { x2=rfbScreen->width-1; if(x1==x2) x1--; }
if(x1<0) x1=0;
if(x2>=rfbScreen->width) x2=rfbScreen->width-1;
if(x1==x2) return;
if(y1>y2) { i=y1; y1=y2; y2=i; }
y2++;
if(y1<0) { y1=0; if(y2==y1) y2++; }
if(y2>=rfbScreen->height) { y2=rfbScreen->height-1; if(y1==y2) y1--; }
if(y1<0) y1=0;
if(y2>=rfbScreen->height) y2=rfbScreen->height-1;
if(y1==y2) return;
region = sraRgnCreateRect(x1,y1,x2,y2);
rfbMarkRegionAsModified(rfbScreen,region);
......@@ -400,12 +400,32 @@ rfbCursorPtr defaultGetCursorPtr(rfbClientPtr cl)
return(cl->screen->cursor);
}
Bool defaultGetPassword(rfbClientPtr cl,char* passwd,int len)
/* response is cl->authChallenge vncEncrypted with passwd */
Bool defaultPasswordCheck(rfbClientPtr cl,char* response,int len)
{
char *pwd=vncDecryptPasswdFromFile(cl->screen->rfbAuthPasswdData);
if(!pwd)
int i;
char *passwd=vncDecryptPasswdFromFile(cl->screen->rfbAuthPasswdData);
if(!passwd) {
rfbLog("Couldn't read password file: %s\n",cl->screen->rfbAuthPasswdData);
return(FALSE);
}
vncEncryptBytes(cl->authChallenge, passwd);
/* Lose the password from memory */
for (i = strlen(passwd); i >= 0; i--) {
passwd[i] = '\0';
}
free(passwd);
if (memcmp(cl->authChallenge, response, len) != 0) {
rfbLog("rfbAuthProcessClientMessage: authentication failed from %s\n",
cl->host);
return(FALSE);
strncpy(passwd,pwd,MAXPWLEN);
}
return(TRUE);
}
......@@ -520,7 +540,7 @@ rfbScreenInfoPtr rfbGetScreen(int argc,char** argv,
rfbScreen->setXCutText = defaultSetXCutText;
rfbScreen->getCursorPtr = defaultGetCursorPtr;
rfbScreen->setTranslateFunction = rfbSetTranslateFunction;
rfbScreen->getPassword = defaultGetPassword;
rfbScreen->passwordCheck = defaultPasswordCheck;
rfbScreen->newClientHook = doNothingWithClient;
rfbScreen->displayHook = 0;
......
This diff is collapsed.
......@@ -155,7 +155,7 @@ typedef void (*PtrAddEventProcPtr) (int buttonMask, int x, int y, struct rfbClie
typedef void (*SetXCutTextProcPtr) (char* str,int len, struct rfbClientRec* cl);
typedef struct rfbCursor* (*GetCursorProcPtr) (struct rfbClientRec* pScreen);
typedef Bool (*SetTranslateFunctionProcPtr)(struct rfbClientRec* cl);
typedef Bool (*GetPasswordProcPtr)(struct rfbClientRec* cl,char* passWord,int len);
typedef Bool (*PasswordCheckProcPtr)(struct rfbClientRec* cl,char* encryptedPassWord,int len);
typedef void (*NewClientHookPtr)(struct rfbClientRec* cl);
typedef void (*DisplayHookPtr)(struct rfbClientRec* cl);
......@@ -266,7 +266,7 @@ typedef struct
SOCKET httpSock;
FILE* httpFP;
GetPasswordProcPtr getPassword;
PasswordCheckProcPtr passwordCheck;
char* rfbAuthPasswdData;
int rfbDeferUpdateTime;
......@@ -680,11 +680,38 @@ typedef struct rfbFontData {
int* metaData;
} rfbFontData,* rfbFontDataPtr;
int rfbDrawChar(rfbScreenInfoPtr rfbScreen,rfbFontDataPtr font,int x,int y,unsigned char c,CARD32 colour);
void rfbDrawString(rfbScreenInfoPtr rfbScreen,rfbFontDataPtr font,int x,int y,unsigned char* string,CARD32 colour);
int rfbWidth(rfbFontDataPtr font,unsigned char* string);
int rfbDrawChar(rfbScreenInfoPtr rfbScreen,rfbFontDataPtr font,int x,int y,unsigned char c,Pixel colour);
void rfbDrawString(rfbScreenInfoPtr rfbScreen,rfbFontDataPtr font,int x,int y,const unsigned char* string,Pixel colour);
/* if colour==backColour, background is transparent */
int rfbDrawCharWithClip(rfbScreenInfoPtr rfbScreen,rfbFontDataPtr font,int x,int y,unsigned char c,int x1,int y1,int x2,int y2,Pixel colour,Pixel backColour);
void rfbDrawStringWithClip(rfbScreenInfoPtr rfbScreen,rfbFontDataPtr font,int x,int y,const unsigned char* string,int x1,int y1,int x2,int y2,Pixel colour,Pixel backColour);
int rfbWidthOfString(rfbFontDataPtr font,const unsigned char* string);
int rfbWidthOfChar(rfbFontDataPtr font,unsigned char c);
void rfbFontBBox(rfbFontDataPtr font,unsigned char c,int* x1,int* y1,int* x2,int* y2);
/* this returns the smallest box enclosing any character of font. */
void rfbWholeFontBBox(rfbFontDataPtr font,int *x1, int *y1, int *x2, int *y2);
/* dynamically load a linux console font (4096 bytes, 256 glyphs a 8x16 */
rfbFontDataPtr rfbLoadConsoleFont(char *filename);
/* free a dynamically loaded font */
void rfbFreeFont(rfbFontDataPtr font);
/* draw.c */
void rfbFillRect(rfbScreenInfoPtr s,int x1,int y1,int x2,int y2,Pixel col);
/* selbox.c */
/* this opens a modal select box. list is an array of strings, the end marked
with a NULL.
It returns the index in the list or -1 if cancelled or something else
wasn't kosher. */
typedef void (*SelectionChangedHookPtr)(int index);
extern int rfbSelectBox(rfbScreenInfoPtr rfbScreen,
rfbFontDataPtr font, char** list,
int x1, int y1, int x2, int y2,
Pixel foreColour, Pixel backColour,
int border,SelectionChangedHookPtr selChangedHook);
/* main.c */
......
#include <ctype.h>
#include "rfb.h"
#include "keysym.h"
typedef struct {
rfbScreenInfoPtr screen;
rfbFontDataPtr font;
char** list;
int listSize;
int selected;
int displayStart;
int x1,y1,x2,y2,textH,pageH;
int xhot,yhot;
int buttonWidth,okBX,cancelBX,okX,cancelX,okY;
Bool okInverted,cancelInverted;
int lastButtons;
Pixel colour,backColour;
SelectionChangedHookPtr selChangedHook;
enum { SELECTING, OK, CANCEL } state;
} rfbSelectData;
static const char* okStr="OK";
static const char* cancelStr="Cancel";
static void selPaintButtons(rfbSelectData* m,Bool invertOk,Bool invertCancel)
{
rfbScreenInfoPtr s = m->screen;
Pixel bcolour = m->backColour;
Pixel colour = m->colour;
rfbFillRect(s,m->x1,m->okY-m->textH,m->x2,m->okY,bcolour);
if(invertOk) {
rfbFillRect(s,m->okBX,m->okY-m->textH,m->okBX+m->buttonWidth,m->okY,colour);
rfbDrawStringWithClip(s,m->font,m->okX+m->xhot,m->okY-1+m->yhot,okStr,
m->x1,m->okY-m->textH,m->x2,m->okY,
bcolour,colour);
} else
rfbDrawString(s,m->font,m->okX+m->xhot,m->okY-1+m->yhot,okStr,colour);
if(invertCancel) {
rfbFillRect(s,m->cancelBX,m->okY-m->textH,
m->cancelBX+m->buttonWidth,m->okY,colour);
rfbDrawStringWithClip(s,m->font,m->cancelX+m->xhot,m->okY-1+m->yhot,
cancelStr,m->x1,m->okY-m->textH,m->x2,m->okY,
bcolour,colour);
} else
rfbDrawString(s,m->font,m->cancelX+m->xhot,m->okY-1+m->yhot,cancelStr,colour);
m->okInverted = invertOk;
m->cancelInverted = invertCancel;
}
/* line is relative to displayStart */
static void selPaintLine(rfbSelectData* m,int line,Bool invert)
{
int y1 = m->y1+line*m->textH, y2 = y1+m->textH;
if(y2>m->y2)
y2=m->y2;
rfbFillRect(m->screen,m->x1,y1,m->x2,y2,invert?m->colour:m->backColour);
if(m->displayStart+line<m->listSize)
rfbDrawStringWithClip(m->screen,m->font,m->x1+m->xhot,y2-1+m->yhot,
m->list[m->displayStart+line],
m->x1,y1,m->x2,y2,
invert?m->backColour:m->colour,
invert?m->backColour:m->colour);
}
static void selSelect(rfbSelectData* m,int index)
{
int delta;
if(index==m->selected || index<0 || index>=m->listSize)
return;
if(m->selected>=0)
selPaintLine(m,m->selected-m->displayStart,FALSE);
if(index<m->displayStart || index>=m->displayStart+m->pageH) {
/* targetLine is the screen line in which the selected line will
be displayed.
targetLine = m->pageH/2 doesn't look so nice */
int targetLine = m->selected-m->displayStart;
int lineStart,lineEnd;
/* scroll */
if(index<targetLine)
targetLine = index;
else if(index+m->pageH-targetLine>=m->listSize)
targetLine = index+m->pageH-m->listSize;
delta = index-(m->displayStart+targetLine);
if(delta>-m->pageH && delta<m->pageH) {
if(delta>0) {
lineStart = m->pageH-delta;
lineEnd = m->pageH;
rfbDoCopyRect(m->screen,m->x1,m->y1,m->x2,m->y1+lineStart*m->textH,
0,-delta*m->textH);
} else {
lineStart = 0;
lineEnd = -delta;
rfbDoCopyRect(m->screen,
m->x1,m->y1+lineEnd*m->textH,m->x2,m->y2,
0,-delta*m->textH);
}
} else {
lineStart = 0;
lineEnd = m->pageH;
}
m->displayStart += delta;
for(delta=lineStart;delta<lineEnd;delta++)
if(delta!=index)
selPaintLine(m,delta,FALSE);
}
m->selected = index;
selPaintLine(m,m->selected-m->displayStart,TRUE);
if(m->selChangedHook)
m->selChangedHook(index);
/* todo: scrollbars */
}
static void selKbdAddEvent(Bool down,KeySym keySym,rfbClientPtr cl)
{
if(down) {
if(keySym>' ' && keySym<0xff) {
int i;
rfbSelectData* m = (rfbSelectData*)cl->screen->screenData;
char c = tolower(keySym);
for(i=m->selected+1;m->list[i] && tolower(m->list[i][0])!=c;i++);
if(!m->list[i])
for(i=0;i<m->selected && tolower(m->list[i][0])!=c;i++);
selSelect(m,i);
} else if(keySym==XK_Escape) {
rfbSelectData* m = (rfbSelectData*)cl->screen->screenData;
m->state = CANCEL;
} else if(keySym==XK_Return) {
rfbSelectData* m = (rfbSelectData*)cl->screen->screenData;
m->state = OK;
} else {
rfbSelectData* m = (rfbSelectData*)cl->screen->screenData;
int curSel=m->selected;
if(keySym==XK_Up) {
if(curSel>0)
selSelect(m,curSel-1);
} else if(keySym==XK_Down) {
if(curSel+1<m->listSize)
selSelect(m,curSel+1);
} else {
if(keySym==XK_Page_Down) {
if(curSel+m->pageH<m->listSize)
selSelect(m,curSel+m->pageH);
else
selSelect(m,m->listSize-1);
} else if(keySym==XK_Page_Up) {
if(curSel-m->pageH>=0)
selSelect(m,curSel-m->pageH);
else
selSelect(m,0);
}
}
}
}
}
static void selPtrAddEvent(int buttonMask,int x,int y,rfbClientPtr cl)
{
rfbSelectData* m = (rfbSelectData*)cl->screen->screenData;
if(y<m->okY && y>=m->okY-m->textH) {
if(x>=m->okBX && x<m->okBX+m->buttonWidth) {
if(!m->okInverted)
selPaintButtons(m,TRUE,FALSE);
if(buttonMask)
m->state = OK;
} else if(x>=m->cancelBX && x<m->cancelBX+m->buttonWidth) {
if(!m->cancelInverted)
selPaintButtons(m,FALSE,TRUE);
if(buttonMask)
m->state = CANCEL;
} else if(m->okInverted || m->cancelInverted)
selPaintButtons(m,FALSE,FALSE);
} else {
if(m->okInverted || m->cancelInverted)
selPaintButtons(m,FALSE,FALSE);
if(!m->lastButtons && buttonMask) {
if(x>=m->x1 && x<m->x2 && y>=m->y1 && y<m->y2)
selSelect(m,m->displayStart+(y-m->y1)/m->textH);
}
}
m->lastButtons = buttonMask;
/* todo: scrollbars */
}
static rfbCursorPtr selGetCursorPtr(rfbClientPtr cl)
{
return(0);
}
int rfbSelectBox(rfbScreenInfoPtr rfbScreen,rfbFontDataPtr font,
char** list,
int x1,int y1,int x2,int y2,
Pixel colour,Pixel backColour,
int border,SelectionChangedHookPtr selChangedHook)
{
int bpp = rfbScreen->bitsPerPixel/8;
char* frameBufferBackup;
void* screenDataBackup = rfbScreen->screenData;
KbdAddEventProcPtr kbdAddEventBackup = rfbScreen->kbdAddEvent;
PtrAddEventProcPtr ptrAddEventBackup = rfbScreen->ptrAddEvent;
GetCursorProcPtr getCursorPtrBackup = rfbScreen->getCursorPtr;
DisplayHookPtr displayHookBackup = rfbScreen->displayHook;
rfbSelectData selData;
int i,j,k;
int fx1,fy1,fx2,fy2; /* for font bbox */
if(list==0 || *list==0)
return(-1);
rfbWholeFontBBox(font, &fx1, &fy1, &fx2, &fy2);
selData.textH = fy2-fy1;
/* I need at least one line for the choice and one for the buttons */
if(y2-y1<selData.textH*2+3*border)
return(-1);
selData.xhot = -fx1;
selData.yhot = -fy2;
selData.x1 = x1+border;
selData.y1 = y1+border;
selData.y2 = y2-selData.textH-3*border;
selData.x2 = x2-2*border;
selData.pageH = (selData.y2-selData.y1)/selData.textH;
i = rfbWidthOfString(font,okStr);
j = rfbWidthOfString(font,cancelStr);
selData.buttonWidth= k = 4*border+(i<j)?j:i;
selData.okBX = x1+(x2-x1-2*k)/3;
if(selData.okBX<x1+border) /* too narrow! */
return(-1);
selData.cancelBX = x1+k+(x2-x1-2*k)*2/3;
selData.okX = selData.okBX+(k-i)/2;
selData.cancelX = selData.cancelBX+(k-j)/2;
selData.okY = y2-border;
rfbUndrawCursor(rfbScreen);
frameBufferBackup = (char*)malloc(bpp*(x2-x1)*(y2-y1));
selData.state = SELECTING;
selData.screen = rfbScreen;
selData.font = font;
selData.list = list;
selData.colour = colour;
selData.backColour = backColour;
for(i=0;list[i];i++);
selData.selected = i;
selData.listSize = i;
selData.displayStart = i;
selData.lastButtons = 0;
selData.selChangedHook = selChangedHook;
rfbScreen->screenData = &selData;
rfbScreen->kbdAddEvent = selKbdAddEvent;
rfbScreen->ptrAddEvent = selPtrAddEvent;
rfbScreen->getCursorPtr = selGetCursorPtr;
rfbScreen->displayHook = 0;
/* backup screen */
for(j=0;j<y2-y1;j++)
memcpy(frameBufferBackup+j*(x2-x1)*bpp,
rfbScreen->frameBuffer+j*rfbScreen->paddedWidthInBytes+x1*bpp,
(x2-x1)*bpp);
/* paint list and buttons */
rfbFillRect(rfbScreen,x1,y1,x2,y2,colour);
selPaintButtons(&selData,FALSE,FALSE);
selSelect(&selData,0);
/* modal loop */
while(selData.state == SELECTING)
rfbProcessEvents(rfbScreen,20000);
/* copy back screen data */
for(j=0;j<y2-y1;j++)
memcpy(rfbScreen->frameBuffer+j*rfbScreen->paddedWidthInBytes+x1*bpp,
frameBufferBackup+j*(x2-x1)*bpp,
(x2-x1)*bpp);
free(frameBufferBackup);
rfbMarkRectAsModified(rfbScreen,x1,y1,x2,y2);
rfbScreen->screenData = screenDataBackup;
rfbScreen->kbdAddEvent = kbdAddEventBackup;
rfbScreen->ptrAddEvent = ptrAddEventBackup;
rfbScreen->getCursorPtr = getCursorPtrBackup;
rfbScreen->displayHook = displayHookBackup;
if(selData.state==CANCEL)
selData.selected=-1;
return(selData.selected);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment