1 /**
2  * @example pnmshow24.c
3  */
4 
5 #include <stdio.h>
6 #include <rfb/rfb.h>
7 #include <rfb/keysym.h>
8 
9 #ifndef LIBVNCSERVER_ALLOW24BPP
main()10 int main() {
11 	printf("I need the ALLOW24BPP LibVNCServer flag to work\n");
12 	exit(1);
13 }
14 #else
15 
HandleKey(rfbBool down,rfbKeySym key,rfbClientPtr cl)16 static void HandleKey(rfbBool down,rfbKeySym key,rfbClientPtr cl)
17 {
18   if(down && (key==XK_Escape || key=='q' || key=='Q'))
19     rfbCloseClient(cl);
20 }
21 
main(int argc,char ** argv)22 int main(int argc,char** argv)
23 {
24   FILE* in=stdin;
25   int j,width,height,paddedWidth;
26   char buffer[1024];
27   rfbScreenInfoPtr rfbScreen;
28 
29   if(argc>1) {
30     in=fopen(argv[1],"rb");
31     if(!in) {
32       printf("Couldn't find file %s.\n",argv[1]);
33       exit(1);
34     }
35   }
36 
37   fgets(buffer,1024,in);
38   if(strncmp(buffer,"P6",2)) {
39     printf("Not a ppm.\n");
40     exit(2);
41   }
42 
43   /* skip comments */
44   do {
45     fgets(buffer,1024,in);
46   } while(buffer[0]=='#');
47 
48   /* get width & height */
49   sscanf(buffer,"%d %d",&width,&height);
50   rfbLog("Got width %d and height %d.\n",width,height);
51   fgets(buffer,1024,in);
52 
53   /* vncviewers have problems with widths which are no multiple of 4. */
54   paddedWidth = width;
55 
56   /* if your vncviewer doesn't have problems with a width
57      which is not a multiple of 4, you can comment this. */
58   if(width&3)
59     paddedWidth+=4-(width&3);
60 
61   /* initialize data for vnc server */
62   rfbScreen = rfbGetScreen(&argc,argv,paddedWidth,height,8,3,3);
63   if(!rfbScreen)
64     return 0;
65   if(argc>1)
66     rfbScreen->desktopName = argv[1];
67   else
68     rfbScreen->desktopName = "Picture";
69   rfbScreen->alwaysShared = TRUE;
70   rfbScreen->kbdAddEvent = HandleKey;
71 
72   /* enable http */
73   rfbScreen->httpDir = "../webclients";
74 
75   /* allocate picture and read it */
76   rfbScreen->frameBuffer = (char*)malloc(paddedWidth*3*height);
77   fread(rfbScreen->frameBuffer,width*3,height,in);
78   fclose(in);
79 
80   /* pad to paddedWidth */
81   if(width != paddedWidth) {
82     int padCount = 3*(paddedWidth - width);
83     for(j=height-1;j>=0;j--) {
84       memmove(rfbScreen->frameBuffer+3*paddedWidth*j,
85 	      rfbScreen->frameBuffer+3*width*j,
86 	      3*width);
87       memset(rfbScreen->frameBuffer+3*paddedWidth*(j+1)-padCount,
88 	     0,padCount);
89     }
90   }
91 
92   /* initialize server */
93   rfbInitServer(rfbScreen);
94 
95   /* run event loop */
96   rfbRunEventLoop(rfbScreen,40000,FALSE);
97 
98   return(0);
99 }
100 #endif
101