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