00001
00005 #ifdef __STRICT_ANSI__
00006 #define _BSD_SOURCE
00007 #endif
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include <sys/types.h>
00011 #ifndef __MINGW32__
00012 #include <sys/socket.h>
00013 #endif
00014 #include <rfb/rfb.h>
00015 #include <rfb/default8x16.h>
00016
00017 #define width 100
00018 #define height 100
00019 static char f[width*height];
00020 static char* keys[0x400];
00021
00022 static int hex2number(unsigned char c)
00023 {
00024 if(c>'f') return(-1);
00025 else if(c>'F')
00026 return(10+c-'a');
00027 else if(c>'9')
00028 return(10+c-'A');
00029 else
00030 return(c-'0');
00031 }
00032
00033 static void read_keys(void)
00034 {
00035 int i,j,k;
00036 char buffer[1024];
00037 FILE* keysyms=fopen("keysym.h","r");
00038
00039 memset(keys,0,0x400*sizeof(char*));
00040
00041 if(!keysyms)
00042 return;
00043
00044 while(!feof(keysyms)) {
00045 fgets(buffer,1024,keysyms);
00046 if(!strncmp(buffer,"#define XK_",strlen("#define XK_"))) {
00047 for(i=strlen("#define XK_");buffer[i] && buffer[i]!=' '
00048 && buffer[i]!='\t';i++);
00049 if(buffer[i]==0)
00050 continue;
00051 buffer[i]=0;
00052 for(i++;buffer[i] && buffer[i]!='0';i++);
00053 if(buffer[i]==0 || buffer[i+1]!='x') continue;
00054 for(j=0,i+=2;(k=hex2number(buffer[i]))>=0;i++)
00055 j=j*16+k;
00056 if(keys[j&0x3ff]) {
00057 char* x=(char*)malloc(1+strlen(keys[j&0x3ff])+1+strlen(buffer+strlen("#define ")));
00058 strcpy(x,keys[j&0x3ff]);
00059 strcat(x,",");
00060 strcat(x,buffer+strlen("#define "));
00061 free(keys[j&0x3ff]);
00062 keys[j&0x3ff]=x;
00063 } else
00064 keys[j&0x3ff] = strdup(buffer+strlen("#define "));
00065 }
00066
00067 }
00068 fclose(keysyms);
00069 }
00070
00071 static int lineHeight=16,lineY=height-16;
00072 static void output(rfbScreenInfoPtr s,char* line)
00073 {
00074 rfbDoCopyRect(s,0,0,width,height-lineHeight,0,-lineHeight);
00075 rfbDrawString(s,&default8x16Font,10,lineY,line,0x01);
00076 rfbLog("%s\n",line);
00077 }
00078
00079 static void dokey(rfbBool down,rfbKeySym k,rfbClientPtr cl)
00080 {
00081 char buffer[1024+32];
00082
00083 sprintf(buffer,"%s: %s (0x%x)",
00084 down?"down":"up",keys[k&0x3ff]?keys[k&0x3ff]:"",(unsigned int)k);
00085 output(cl->screen,buffer);
00086 }
00087
00088 static void doptr(int buttonMask,int x,int y,rfbClientPtr cl)
00089 {
00090 char buffer[1024];
00091 if(buttonMask) {
00092 sprintf(buffer,"Ptr: mouse button mask 0x%x at %d,%d",buttonMask,x,y);
00093 output(cl->screen,buffer);
00094 }
00095
00096 }
00097
00098 static enum rfbNewClientAction newclient(rfbClientPtr cl)
00099 {
00100 char buffer[1024];
00101 struct sockaddr_in addr;
00102 socklen_t len=sizeof(addr);
00103 unsigned int ip;
00104
00105 getpeername(cl->sock,(struct sockaddr*)&addr,&len);
00106 ip=ntohl(addr.sin_addr.s_addr);
00107 sprintf(buffer,"Client connected from ip %d.%d.%d.%d",
00108 (ip>>24)&0xff,(ip>>16)&0xff,(ip>>8)&0xff,ip&0xff);
00109 output(cl->screen,buffer);
00110 return RFB_CLIENT_ACCEPT;
00111 }
00112
00113 int main(int argc,char** argv)
00114 {
00115 rfbScreenInfoPtr s=rfbGetScreen(&argc,argv,width,height,8,1,1);
00116 if(!s)
00117 return 0;
00118 s->colourMap.is16=FALSE;
00119 s->colourMap.count=2;
00120 s->colourMap.data.bytes=(unsigned char*)"\xd0\xd0\xd0\x30\x01\xe0";
00121 s->serverFormat.trueColour=FALSE;
00122 s->frameBuffer=f;
00123 s->kbdAddEvent=dokey;
00124 s->ptrAddEvent=doptr;
00125 s->newClientHook=newclient;
00126
00127 memset(f,0,width*height);
00128 read_keys();
00129 rfbInitServer(s);
00130
00131 while(1) {
00132 rfbProcessEvents(s,999999);
00133 }
00134 }