Go to the documentation of this file.00001
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008 #include <time.h>
00009 #include <errno.h>
00010 #include <rfb/rfbclient.h>
00011
00012 static void PrintRect(rfbClient* client, int x, int y, int w, int h) {
00013 rfbClientLog("Received an update for %d,%d,%d,%d.\n",x,y,w,h);
00014 }
00015
00016 static void SaveFramebufferAsPPM(rfbClient* client, int x, int y, int w, int h) {
00017 static time_t t=0,t1;
00018 FILE* f;
00019 int i,j;
00020 rfbPixelFormat* pf=&client->format;
00021 int bpp=pf->bitsPerPixel/8;
00022 int row_stride=client->width*bpp;
00023
00024
00025 t1=time(NULL);
00026 if(t1-t>2)
00027 t=t1;
00028 else
00029 return;
00030
00031
00032 if(bpp!=4 && bpp!=2) {
00033 rfbClientLog("bpp = %d (!=4)\n",bpp);
00034 return;
00035 }
00036
00037 f=fopen("framebuffer.ppm","wb");
00038 if(!f) {
00039 rfbClientErr("Could not open framebuffer.ppm\n");
00040 return;
00041 }
00042
00043 fprintf(f,"P6\n# %s\n%d %d\n255\n",client->desktopName,client->width,client->height);
00044 for(j=0;j<client->height*row_stride;j+=row_stride)
00045 for(i=0;i<client->width*bpp;i+=bpp) {
00046 unsigned char* p=client->frameBuffer+j+i;
00047 unsigned int v;
00048 if(bpp==4)
00049 v=*(unsigned int*)p;
00050 else if(bpp==2)
00051 v=*(unsigned short*)p;
00052 else
00053 v=*(unsigned char*)p;
00054 fputc((v>>pf->redShift)*256/(pf->redMax+1),f);
00055 fputc((v>>pf->greenShift)*256/(pf->greenMax+1),f);
00056 fputc((v>>pf->blueShift)*256/(pf->blueMax+1),f);
00057 }
00058 fclose(f);
00059 }
00060
00061 int
00062 main(int argc, char **argv)
00063 {
00064 rfbClient* client = rfbGetClient(8,3,4);
00065 time_t t=time(NULL);
00066
00067 if(argc>1 && !strcmp("-print",argv[1])) {
00068 client->GotFrameBufferUpdate = PrintRect;
00069 argv[1]=argv[0]; argv++; argc--;
00070 } else
00071 client->GotFrameBufferUpdate = SaveFramebufferAsPPM;
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 if (!rfbInitClient(client,&argc,argv))
00084 return 1;
00085
00086
00087 while (time(NULL)-t<5) {
00088 static int i=0;
00089 fprintf(stderr,"\r%d",i++);
00090 if(WaitForMessage(client,50)<0)
00091 break;
00092 if(!HandleRFBServerMessage(client))
00093 break;
00094 }
00095
00096 rfbClientCleanup(client);
00097
00098 return 0;
00099 }
00100