correctly leave fullscreen mode when connection is lost
[presencevnc] / libvnc / libvncclient / vncviewer.c
1 /*
2  *  Copyright (C) 1999 AT&T Laboratories Cambridge.  All Rights Reserved.
3  *
4  *  This is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This software is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this software; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
17  *  USA.
18  */
19
20 /*
21  * vncviewer.c - the Xt-based VNC viewer.
22  */
23
24 #ifdef __STRICT_ANSI__
25 #define _BSD_SOURCE
26 #define _POSIX_SOURCE
27 #endif
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <time.h>
32 #include <rfb/rfbclient.h>
33
34 static void Dummy(rfbClient* client) {
35 }
36 static rfbBool DummyPoint(rfbClient* client, int x, int y) {
37   return TRUE;
38 }
39 static void DummyRect(rfbClient* client, int x, int y, int w, int h) {
40 }
41
42 #ifdef __MINGW32__
43 static char* NoPassword(rfbClient* client) {
44   return strdup("");
45 }
46 #else
47 #include <stdio.h>
48 #include <termios.h>
49 #endif
50
51 static char* ReadPassword(rfbClient* client) {
52 #ifdef __MINGW32__
53         /* FIXME */
54         rfbClientErr("ReadPassword on MinGW32 NOT IMPLEMENTED\n");
55         return NoPassword(client);
56 #else
57         int i;
58         char* p=malloc(9);
59         struct termios save,noecho;
60         p[0]=0;
61         if(tcgetattr(fileno(stdin),&save)!=0) return p;
62         noecho=save; noecho.c_lflag &= ~ECHO;
63         if(tcsetattr(fileno(stdin),TCSAFLUSH,&noecho)!=0) return p;
64         fprintf(stderr,"Password: ");
65         i=0;
66         while(1) {
67                 int c=fgetc(stdin);
68                 if(c=='\n')
69                         break;
70                 if(i<8) {
71                         p[i]=c;
72                         i++;
73                         p[i]=0;
74                 }
75         }
76         tcsetattr(fileno(stdin),TCSAFLUSH,&save);
77         return p;
78 #endif
79 }
80 static rfbBool MallocFrameBuffer(rfbClient* client) {
81   if(client->frameBuffer)
82     free(client->frameBuffer);
83   client->frameBuffer=malloc(client->width*client->height*client->format.bitsPerPixel/8);
84   return client->frameBuffer?TRUE:FALSE;
85 }
86
87 static void initAppData(AppData* data) {
88         data->shareDesktop=TRUE;
89         data->viewOnly=FALSE;
90         data->encodingsString="tight zrle ultra copyrect hextile zlib corre rre raw";
91         data->useBGR233=FALSE;
92         data->nColours=0;
93         data->forceOwnCmap=FALSE;
94         data->forceTrueColour=FALSE;
95         data->requestedDepth=0;
96         data->compressLevel=3;
97         data->qualityLevel=5;
98 #ifdef LIBVNCSERVER_HAVE_LIBJPEG
99         data->enableJPEG=TRUE;
100 #else
101         data->enableJPEG=FALSE;
102 #endif
103         data->useRemoteCursor=FALSE;
104 }
105
106 rfbClient* rfbGetClient(int bitsPerSample,int samplesPerPixel,
107                         int bytesPerPixel) {
108   rfbClient* client=(rfbClient*)calloc(sizeof(rfbClient),1);
109   if(!client) {
110     rfbClientErr("Couldn't allocate client structure!\n");
111     return NULL;
112   }
113   initAppData(&client->appData);
114   client->programName = NULL;
115   client->endianTest = 1;
116   client->programName="";
117   client->serverHost="";
118   client->serverPort=5900;
119   
120   client->CurrentKeyboardLedState = 0;
121   client->HandleKeyboardLedState = (HandleKeyboardLedStateProc)DummyPoint;
122
123   /* default: use complete frame buffer */ 
124   client->updateRect.x = -1;
125  
126   client->format.bitsPerPixel = bytesPerPixel*8;
127   client->format.depth = bitsPerSample*samplesPerPixel;
128   client->appData.requestedDepth=client->format.depth;
129   client->format.bigEndian = *(char *)&client->endianTest?FALSE:TRUE;
130   client->format.trueColour = TRUE;
131
132   if (client->format.bitsPerPixel == 8) {
133     client->format.redMax = 7;
134     client->format.greenMax = 7;
135     client->format.blueMax = 3;
136     client->format.redShift = 0;
137     client->format.greenShift = 3;
138     client->format.blueShift = 6;
139   } else {
140     client->format.redMax = (1 << bitsPerSample) - 1;
141     client->format.greenMax = (1 << bitsPerSample) - 1;
142     client->format.blueMax = (1 << bitsPerSample) - 1;
143     if(!client->format.bigEndian) {
144       client->format.redShift = 0;
145       client->format.greenShift = bitsPerSample;
146       client->format.blueShift = bitsPerSample * 2;
147     } else {
148       if(client->format.bitsPerPixel==8*3) {
149         client->format.redShift = bitsPerSample*2;
150         client->format.greenShift = bitsPerSample*1;
151         client->format.blueShift = 0;
152       } else {
153         client->format.redShift = bitsPerSample*3;
154         client->format.greenShift = bitsPerSample*2;
155         client->format.blueShift = bitsPerSample;
156       }
157     }
158   }
159
160   client->bufoutptr=client->buf;
161   client->buffered=0;
162
163 #ifdef LIBVNCSERVER_HAVE_LIBZ
164   client->raw_buffer_size = -1;
165   client->decompStreamInited = FALSE;
166
167 #ifdef LIBVNCSERVER_HAVE_LIBJPEG
168   memset(client->zlibStreamActive,0,sizeof(rfbBool)*4);
169   client->jpegSrcManager = NULL;
170 #endif
171 #endif
172
173   client->HandleCursorPos = DummyPoint;
174   client->SoftCursorLockArea = DummyRect;
175   client->SoftCursorUnlockScreen = Dummy;
176   client->GotFrameBufferUpdate = DummyRect;
177   client->GetPassword = ReadPassword;
178   client->MallocFrameBuffer = MallocFrameBuffer;
179   client->Bell = Dummy;
180   client->CurrentKeyboardLedState = 0;
181   client->HandleKeyboardLedState = (HandleKeyboardLedStateProc)DummyPoint;
182
183   return client;
184 }
185
186 static rfbBool rfbInitConnection(rfbClient* client)
187 {
188   /* Unless we accepted an incoming connection, make a TCP connection to the
189      given VNC server */
190
191   if (!client->listenSpecified) {
192     if (!client->serverHost || !ConnectToRFBServer(client,client->serverHost,client->serverPort))
193       return FALSE;
194   }
195
196   /* Initialise the VNC connection, including reading the password */
197
198   if (!InitialiseRFBConnection(client))
199     return FALSE;
200
201   if (!SetFormatAndEncodings(client))
202     return FALSE;
203
204   client->width=client->si.framebufferWidth;
205   client->height=client->si.framebufferHeight;
206   client->MallocFrameBuffer(client);
207
208   if (client->updateRect.x < 0) {
209     client->updateRect.x = client->updateRect.y = 0;
210     client->updateRect.w = client->width;
211     client->updateRect.h = client->height;
212   }
213
214   if (client->appData.scaleSetting>1)
215   {
216       if (!SendScaleSetting(client, client->appData.scaleSetting))
217           return FALSE;
218       if (!SendFramebufferUpdateRequest(client,
219                               client->updateRect.x / client->appData.scaleSetting,
220                               client->updateRect.y / client->appData.scaleSetting,
221                               client->updateRect.w / client->appData.scaleSetting,
222                               client->updateRect.h / client->appData.scaleSetting,
223                               FALSE))
224               return FALSE;
225   }
226   else
227   {
228       if (!SendFramebufferUpdateRequest(client,
229                               client->updateRect.x, client->updateRect.y,
230                               client->updateRect.w, client->updateRect.h,
231                               FALSE))
232       return FALSE;
233   }
234
235   return TRUE;
236 }
237
238 rfbBool rfbInitClient(rfbClient* client,int* argc,char** argv) {
239   int i,j;
240
241   if(argv && argc && *argc) {
242     if(client->programName==0)
243       client->programName=argv[0];
244
245     for (i = 1; i < *argc; i++) {
246       j = i;
247       if (strcmp(argv[i], "-listen") == 0) {
248         listenForIncomingConnections(client);
249         break;
250       } else if (strcmp(argv[i], "-play") == 0) {
251         client->serverPort = -1;
252         j++;
253       } else if (i+1<*argc && strcmp(argv[i], "-encodings") == 0) {
254         client->appData.encodingsString = argv[i+1];
255         j+=2;
256       } else if (i+1<*argc && strcmp(argv[i], "-compress") == 0) {
257         client->appData.compressLevel = atoi(argv[i+1]);
258         j+=2;
259       } else if (i+1<*argc && strcmp(argv[i], "-quality") == 0) {
260         client->appData.qualityLevel = atoi(argv[i+1]);
261         j+=2;
262       } else if (i+1<*argc && strcmp(argv[i], "-scale") == 0) {
263         client->appData.scaleSetting = atoi(argv[i+1]);
264         j+=2;
265       } else {
266         char* colon=strchr(argv[i],':');
267
268         if(colon) {
269           client->serverHost=strdup(argv[i]);
270           client->serverHost[(int)(colon-argv[i])]='\0';
271           client->serverPort=atoi(colon+1);
272         } else {
273           client->serverHost=strdup(argv[i]);
274         }
275         if(client->serverPort>=0 && client->serverPort<5900)
276           client->serverPort+=5900;
277       }
278       /* purge arguments */
279       if (j>i) {
280         *argc-=j-i;
281         memmove(argv+i,argv+j,(*argc-i)*sizeof(char*));
282         i--;
283       }
284     }
285   } else if(client->listenSpecified) {
286         listenForIncomingConnections(client);
287   }
288
289   if(!rfbInitConnection(client)) {
290     rfbClientCleanup(client);
291     return FALSE;
292   }
293
294   return TRUE;
295 }
296
297 void rfbClientCleanup(rfbClient* client) {
298 #ifdef LIBVNCSERVER_HAVE_LIBZ
299 #ifdef LIBVNCSERVER_HAVE_LIBJPEG
300   int i;
301
302   for ( i = 0; i < 4; i++ ) {
303     if (client->zlibStreamActive[i] == TRUE ) {
304       if (inflateEnd (&client->zlibStream[i]) != Z_OK &&
305           client->zlibStream[i].msg != NULL)
306         rfbClientLog("inflateEnd: %s\n", client->zlibStream[i].msg);
307     }
308   }
309
310   if ( client->decompStreamInited == TRUE ) {
311     if (inflateEnd (&client->decompStream) != Z_OK &&
312         client->decompStream.msg != NULL)
313       rfbClientLog("inflateEnd: %s\n", client->decompStream.msg );
314   }
315
316   if (client->jpegSrcManager)
317     free(client->jpegSrcManager);
318 #endif
319 #endif
320
321  
322   if (client->sock >= 0)
323     close(client->sock);
324
325   free(client->desktopName);
326   free(client->serverHost);
327   free(client);
328 }