LibVNCServer/LibVNCClient
ppmtest.c
Go to the documentation of this file.
1 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <time.h>
9 #include <errno.h>
10 #include <rfb/rfbclient.h>
11 
12 static void PrintRect(rfbClient* client, int x, int y, int w, int h) {
13  rfbClientLog("Received an update for %d,%d,%d,%d.\n",x,y,w,h);
14 }
15 
16 static void SaveFramebufferAsPPM(rfbClient* client, int x, int y, int w, int h) {
17  static time_t t=0,t1;
18  FILE* f;
19  int i,j;
21  int bpp=pf->bitsPerPixel/8;
22  int row_stride=client->width*bpp;
23 
24  /* save one picture only if the last is older than 2 seconds */
25  t1=time(NULL);
26  if(t1-t>2)
27  t=t1;
28  else
29  return;
30 
31  /* assert bpp=4 */
32  if(bpp!=4 && bpp!=2) {
33  rfbClientLog("bpp = %d (!=4)\n",bpp);
34  return;
35  }
36 
37  f=fopen("framebuffer.ppm","wb");
38  if(!f) {
39  rfbClientErr("Could not open framebuffer.ppm\n");
40  return;
41  }
42 
43  fprintf(f,"P6\n# %s\n%d %d\n255\n",client->desktopName,client->width,client->height);
44  for(j=0;j<client->height*row_stride;j+=row_stride)
45  for(i=0;i<client->width*bpp;i+=bpp) {
46  unsigned char* p=client->frameBuffer+j+i;
47  unsigned int v;
48  if(bpp==4)
49  v=*(unsigned int*)p;
50  else if(bpp==2)
51  v=*(unsigned short*)p;
52  else
53  v=*(unsigned char*)p;
54  fputc((v>>pf->redShift)*256/(pf->redMax+1),f);
55  fputc((v>>pf->greenShift)*256/(pf->greenMax+1),f);
56  fputc((v>>pf->blueShift)*256/(pf->blueMax+1),f);
57  }
58  fclose(f);
59 }
60 
62 {
63 return strdup("testuser@test");
64 }
65 
67 {
68 return strdup("Password");
69 }
70 
71 int
72 main(int argc, char **argv)
73 {
74  rfbClient* client = rfbGetClient(8,3,4);
75  time_t t=time(NULL);
76 
77 #ifdef LIBVNCSERVER_HAVE_SASL
78  client->GetUser = getuser;
80 #endif
81 
82  if(argc>1 && !strcmp("-print",argv[1])) {
83  client->GotFrameBufferUpdate = PrintRect;
84  argv[1]=argv[0]; argv++; argc--;
85  } else
86  client->GotFrameBufferUpdate = SaveFramebufferAsPPM;
87 
88  /* The -listen option is used to make us a daemon process which listens for
89  incoming connections from servers, rather than actively connecting to a
90  given server. The -tunnel and -via options are useful to create
91  connections tunneled via SSH port forwarding. We must test for the
92  -listen option before invoking any Xt functions - this is because we use
93  forking, and Xt doesn't seem to cope with forking very well. For -listen
94  option, when a successful incoming connection has been accepted,
95  listenForIncomingConnections() returns, setting the listenSpecified
96  flag. */
97 
98  if (!rfbInitClient(client,&argc,argv))
99  return 1;
100 
101  /* TODO: better wait for update completion */
102  while (time(NULL)-t<5) {
103  static int i=0;
104  fprintf(stderr,"\r%d",i++);
105  int n = WaitForMessage(client,50);
106  if(n < 0)
107  break;
108  if(n)
110  break;
111  }
112 
114 
115  return 0;
116 }
117 
int y
Definition: SDLvncviewer.c:34
int x
Definition: SDLvncviewer.c:34
rfbClient * rfbGetClient(int bitsPerSample, int samplesPerPixel, int bytesPerPixel)
Allocates and returns a pointer to an rfbClient structure.
int WaitForMessage(rfbClient *client, unsigned int usecs)
Waits for an RFB message to arrive from the server.
rfbClientLogProc rfbClientLog
void rfbClientCleanup(rfbClient *client)
Cleans up the client structure and releases the memory allocated for it.
rfbBool HandleRFBServerMessage(rfbClient *client)
Handles messages from the RFB server.
rfbBool rfbInitClient(rfbClient *client, int *argc, char **argv)
Initializes the client.
rfbClientLogProc rfbClientErr
Definition: rfbclient.h:500
int main(int argc, char **argv)
Definition: ppmtest.c:72
char * getpassword(rfbClient *client)
Definition: ppmtest.c:66
char * getuser(rfbClient *client)
Definition: ppmtest.c:61
char * desktopName
Definition: rfbclient.h:272
int width
Definition: rfbclient.h:243
GotFrameBufferUpdateProc GotFrameBufferUpdate
Definition: rfbclient.h:354
uint8_t * frameBuffer
Definition: rfbclient.h:242
GetPasswordProc GetPassword
the pointer returned by GetPassword will be freed after use!
Definition: rfbclient.h:356
int height
Definition: rfbclient.h:243
rfbPixelFormat format
Definition: rfbclient.h:273
uint16_t redMax
Definition: rfbproto.h:180
uint16_t greenMax
Definition: rfbproto.h:184
uint16_t blueMax
Definition: rfbproto.h:186
uint8_t greenShift
Definition: rfbproto.h:200
uint8_t bitsPerPixel
Definition: rfbproto.h:163
uint8_t blueShift
Definition: rfbproto.h:202
uint8_t redShift
Definition: rfbproto.h:188
rfbClient * client
Definition: vnc2mpg.c:358