Home | History | Annotate | Download | only in examples
      1 /**
      2  * @example vncev.c
      3  * This program is a simple server to show events coming from the client
      4 */
      5 #ifdef __STRICT_ANSI__
      6 #define _BSD_SOURCE
      7 #endif
      8 #include <stdio.h>
      9 #include <stdlib.h>
     10 #include <sys/types.h>
     11 #ifndef __MINGW32__
     12 #include <sys/socket.h>
     13 #endif
     14 #include <rfb/rfb.h>
     15 #include <rfb/default8x16.h>
     16 
     17 #define width 100
     18 #define height 100
     19 static char f[width*height];
     20 static char* keys[0x400];
     21 
     22 static int hex2number(unsigned char c)
     23 {
     24    if(c>'f') return(-1);
     25    else if(c>'F')
     26      return(10+c-'a');
     27    else if(c>'9')
     28      return(10+c-'A');
     29    else
     30      return(c-'0');
     31 }
     32 
     33 static void read_keys(void)
     34 {
     35    int i,j,k;
     36    char buffer[1024];
     37    FILE* keysyms=fopen("keysym.h","r");
     38 
     39    memset(keys,0,0x400*sizeof(char*));
     40 
     41    if(!keysyms)
     42      return;
     43 
     44    while(!feof(keysyms)) {
     45       fgets(buffer,1024,keysyms);
     46       if(!strncmp(buffer,"#define XK_",strlen("#define XK_"))) {
     47 	 for(i=strlen("#define XK_");buffer[i] && buffer[i]!=' '
     48 	     && buffer[i]!='\t';i++);
     49 	 if(buffer[i]==0) /* don't support wrapped lines */
     50 	   continue;
     51 	 buffer[i]=0;
     52 	 for(i++;buffer[i] && buffer[i]!='0';i++);
     53 	 if(buffer[i]==0 || buffer[i+1]!='x') continue;
     54 	 for(j=0,i+=2;(k=hex2number(buffer[i]))>=0;i++)
     55 	   j=j*16+k;
     56 	 if(keys[j&0x3ff]) {
     57 	    char* x=(char*)malloc(1+strlen(keys[j&0x3ff])+1+strlen(buffer+strlen("#define ")));
     58 	    strcpy(x,keys[j&0x3ff]);
     59 	    strcat(x,",");
     60 	    strcat(x,buffer+strlen("#define "));
     61 	    free(keys[j&0x3ff]);
     62 	    keys[j&0x3ff]=x;
     63 	 } else
     64 	   keys[j&0x3ff] = strdup(buffer+strlen("#define "));
     65       }
     66 
     67    }
     68    fclose(keysyms);
     69 }
     70 
     71 static int lineHeight=16,lineY=height-16;
     72 static void output(rfbScreenInfoPtr s,char* line)
     73 {
     74    rfbDoCopyRect(s,0,0,width,height-lineHeight,0,-lineHeight);
     75    rfbDrawString(s,&default8x16Font,10,lineY,line,0x01);
     76    rfbLog("%s\n",line);
     77 }
     78 
     79 static void dokey(rfbBool down,rfbKeySym k,rfbClientPtr cl)
     80 {
     81    char buffer[1024+32];
     82 
     83    sprintf(buffer,"%s: %s (0x%x)",
     84 	   down?"down":"up",keys[k&0x3ff]?keys[k&0x3ff]:"",(unsigned int)k);
     85    output(cl->screen,buffer);
     86 }
     87 
     88 static void doptr(int buttonMask,int x,int y,rfbClientPtr cl)
     89 {
     90    char buffer[1024];
     91    if(buttonMask) {
     92       sprintf(buffer,"Ptr: mouse button mask 0x%x at %d,%d",buttonMask,x,y);
     93       output(cl->screen,buffer);
     94    }
     95 
     96 }
     97 
     98 static enum rfbNewClientAction newclient(rfbClientPtr cl)
     99 {
    100    char buffer[1024];
    101    struct sockaddr_in addr;
    102    socklen_t len=sizeof(addr);
    103    unsigned int ip;
    104 
    105    getpeername(cl->sock,(struct sockaddr*)&addr,&len);
    106    ip=ntohl(addr.sin_addr.s_addr);
    107    sprintf(buffer,"Client connected from ip %d.%d.%d.%d",
    108 	   (ip>>24)&0xff,(ip>>16)&0xff,(ip>>8)&0xff,ip&0xff);
    109    output(cl->screen,buffer);
    110    return RFB_CLIENT_ACCEPT;
    111 }
    112 
    113 int main(int argc,char** argv)
    114 {
    115    rfbScreenInfoPtr s=rfbGetScreen(&argc,argv,width,height,8,1,1);
    116    if(!s)
    117      return 0;
    118    s->colourMap.is16=FALSE;
    119    s->colourMap.count=2;
    120    s->colourMap.data.bytes=(unsigned char*)"\xd0\xd0\xd0\x30\x01\xe0";
    121    s->serverFormat.trueColour=FALSE;
    122    s->frameBuffer=f;
    123    s->kbdAddEvent=dokey;
    124    s->ptrAddEvent=doptr;
    125    s->newClientHook=newclient;
    126 
    127    memset(f,0,width*height);
    128    read_keys();
    129    rfbInitServer(s);
    130 
    131    while(1) {
    132       rfbProcessEvents(s,999999);
    133    }
    134 }
    135