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