LibVNCServer/LibVNCClient
ppmtest.c

A simple example of an RFB client

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <rfb/rfbclient.h>
static void PrintRect(rfbClient* client, int x, int y, int w, int h) {
rfbClientLog("Received an update for %d,%d,%d,%d.\n",x,y,w,h);
}
static void SaveFramebufferAsPPM(rfbClient* client, int x, int y, int w, int h) {
static time_t t=0,t1;
FILE* f;
int i,j;
int bpp=pf->bitsPerPixel/8;
int row_stride=client->width*bpp;
/* save one picture only if the last is older than 2 seconds */
t1=time(NULL);
if(t1-t>2)
t=t1;
else
return;
/* assert bpp=4 */
if(bpp!=4 && bpp!=2) {
rfbClientLog("bpp = %d (!=4)\n",bpp);
return;
}
f=fopen("framebuffer.ppm","wb");
if(!f) {
rfbClientErr("Could not open framebuffer.ppm\n");
return;
}
fprintf(f,"P6\n# %s\n%d %d\n255\n",client->desktopName,client->width,client->height);
for(j=0;j<client->height*row_stride;j+=row_stride)
for(i=0;i<client->width*bpp;i+=bpp) {
unsigned char* p=client->frameBuffer+j+i;
unsigned int v;
if(bpp==4)
v=*(unsigned int*)p;
else if(bpp==2)
v=*(unsigned short*)p;
else
v=*(unsigned char*)p;
fputc((v>>pf->redShift)*256/(pf->redMax+1),f);
fputc((v>>pf->greenShift)*256/(pf->greenMax+1),f);
fputc((v>>pf->blueShift)*256/(pf->blueMax+1),f);
}
fclose(f);
}
{
return strdup("testuser@test");
}
{
return strdup("Password");
}
int
main(int argc, char **argv)
{
time_t t=time(NULL);
#ifdef LIBVNCSERVER_HAVE_SASL
client->GetUser = getuser;
#endif
if(argc>1 && !strcmp("-print",argv[1])) {
argv[1]=argv[0]; argv++; argc--;
} else
client->GotFrameBufferUpdate = SaveFramebufferAsPPM;
/* The -listen option is used to make us a daemon process which listens for
incoming connections from servers, rather than actively connecting to a
given server. The -tunnel and -via options are useful to create
connections tunneled via SSH port forwarding. We must test for the
-listen option before invoking any Xt functions - this is because we use
forking, and Xt doesn't seem to cope with forking very well. For -listen
option, when a successful incoming connection has been accepted,
listenForIncomingConnections() returns, setting the listenSpecified
flag. */
if (!rfbInitClient(client,&argc,argv))
return 1;
/* TODO: better wait for update completion */
while (time(NULL)-t<5) {
static int i=0;
fprintf(stderr,"\r%d",i++);
int n = WaitForMessage(client,50);
if(n < 0)
break;
if(n)
break;
}
return 0;
}
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