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