add libvncserver
[presencevnc] / libvnc / rfb / rfbclient.h
1 #ifndef RFBCLIENT_H
2 #define RFBCLIENT_H
3
4 /*
5  *  Copyright (C) 2000, 2001 Const Kaplinsky.  All Rights Reserved.
6  *  Copyright (C) 2000 Tridia Corporation.  All Rights Reserved.
7  *  Copyright (C) 1999 AT&T Laboratories Cambridge.  All Rights Reserved.
8  *
9  *  This is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This software is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this software; if not, write to the Free Software
21  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
22  *  USA.
23  */
24
25 /*
26  * vncviewer.h
27  */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/time.h>
33 #include <unistd.h>
34 #include <rfb/rfbproto.h>
35 #include <rfb/keysym.h>
36
37 #define rfbClientSwap16IfLE(s) \
38     (*(char *)&client->endianTest ? ((((s) & 0xff) << 8) | (((s) >> 8) & 0xff)) : (s))
39
40 #define rfbClientSwap32IfLE(l) \
41     (*(char *)&client->endianTest ? ((((l) & 0xff000000) >> 24) | \
42                              (((l) & 0x00ff0000) >> 8)  | \
43                              (((l) & 0x0000ff00) << 8)  | \
44                              (((l) & 0x000000ff) << 24))  : (l))
45
46 #define FLASH_PORT_OFFSET 5400
47 #define LISTEN_PORT_OFFSET 5500
48 #define TUNNEL_PORT_OFFSET 5500
49 #define SERVER_PORT_OFFSET 5900
50
51 #define DEFAULT_SSH_CMD "/usr/bin/ssh"
52 #define DEFAULT_TUNNEL_CMD  \
53   (DEFAULT_SSH_CMD " -f -L %L:localhost:%R %H sleep 20")
54 #define DEFAULT_VIA_CMD     \
55   (DEFAULT_SSH_CMD " -f -L %L:%H:%R %G sleep 20")
56
57 #if(defined __cplusplus)
58 extern "C"
59 {
60 #endif
61
62 /* vncrec */
63
64 typedef struct {
65   FILE* file;
66   struct timeval tv;
67   rfbBool readTimestamp;
68   rfbBool doNotSleep;
69 } rfbVNCRec;
70
71 /* client data */
72
73 typedef struct rfbClientData {
74         void* tag;
75         void* data;
76         struct rfbClientData* next;
77 } rfbClientData;
78
79 /* app data (belongs into rfbClient?) */
80
81 typedef struct {
82   rfbBool shareDesktop;
83   rfbBool viewOnly;
84
85   const char* encodingsString;
86
87   rfbBool useBGR233;
88   int nColours;
89   rfbBool forceOwnCmap;
90   rfbBool forceTrueColour;
91   int requestedDepth;
92
93   int compressLevel;
94   int qualityLevel;
95   rfbBool enableJPEG;
96   rfbBool useRemoteCursor;
97   rfbBool palmVNC;  /* use palmvnc specific SetScale (vs ultravnc) */
98   int scaleSetting; /* 0 means no scale set, else 1/scaleSetting */
99 } AppData;
100
101
102 struct _rfbClient;
103
104 typedef void (*HandleTextChatProc)(struct _rfbClient* client, int value, char *text);
105 typedef void (*HandleKeyboardLedStateProc)(struct _rfbClient* client, int value, int pad);
106 typedef rfbBool (*HandleCursorPosProc)(struct _rfbClient* client, int x, int y);
107 typedef void (*SoftCursorLockAreaProc)(struct _rfbClient* client, int x, int y, int w, int h);
108 typedef void (*SoftCursorUnlockScreenProc)(struct _rfbClient* client);
109 typedef void (*GotFrameBufferUpdateProc)(struct _rfbClient* client, int x, int y, int w, int h);
110 typedef char* (*GetPasswordProc)(struct _rfbClient* client);
111 typedef rfbBool (*MallocFrameBufferProc)(struct _rfbClient* client);
112 typedef void (*GotXCutTextProc)(struct _rfbClient* client, const char *text, int textlen);
113 typedef void (*BellProc)(struct _rfbClient* client);
114
115 typedef void (*GotCursorShapeProc)(struct _rfbClient* client, int xhot, int yhot, int width, int height, int bytesPerPixel);
116 typedef void (*GotCopyRectProc)(struct _rfbClient* client, int src_x, int src_y, int w, int h, int dest_x, int dest_y);
117
118 typedef struct _rfbClient {
119         uint8_t* frameBuffer;
120         int width, height;
121
122         int endianTest;
123
124         AppData appData;
125
126         const char* programName;
127         char* serverHost;
128         int serverPort; /* if -1, then use file recorded by vncrec */
129         rfbBool listenSpecified;
130         int listenPort, flashPort;
131
132         struct {
133                 int x, y, w, h;
134         } updateRect;
135
136         /* Note that the CoRRE encoding uses this buffer and assumes it is big enough
137            to hold 255 * 255 * 32 bits -> 260100 bytes.  640*480 = 307200 bytes.
138            Hextile also assumes it is big enough to hold 16 * 16 * 32 bits.
139            Tight encoding assumes BUFFER_SIZE is at least 16384 bytes. */
140
141 #define RFB_BUFFER_SIZE (640*480)
142         char buffer[RFB_BUFFER_SIZE];
143
144         /* rfbproto.c */
145
146         int sock;
147         rfbBool canUseCoRRE;
148         rfbBool canUseHextile;
149         char *desktopName;
150         rfbPixelFormat format;
151         rfbServerInitMsg si;
152
153         /* sockets.c */
154 #define RFB_BUF_SIZE 8192
155         char buf[RFB_BUF_SIZE];
156         char *bufoutptr;
157         int buffered;
158
159         /* The zlib encoding requires expansion/decompression/deflation of the
160            compressed data in the "buffer" above into another, result buffer.
161            However, the size of the result buffer can be determined precisely
162            based on the bitsPerPixel, height and width of the rectangle.  We
163            allocate this buffer one time to be the full size of the buffer. */
164
165         /* Ultra Encoding uses this buffer too */
166         
167         int ultra_buffer_size;
168         char *ultra_buffer;
169
170         int raw_buffer_size;
171         char *raw_buffer;
172
173 #ifdef LIBVNCSERVER_HAVE_LIBZ
174         z_stream decompStream;
175         rfbBool decompStreamInited;
176 #endif
177
178
179 #ifdef LIBVNCSERVER_HAVE_LIBZ
180         /*
181          * Variables for the ``tight'' encoding implementation.
182          */
183
184         /* Separate buffer for compressed data. */
185 #define ZLIB_BUFFER_SIZE 30000
186         char zlib_buffer[ZLIB_BUFFER_SIZE];
187
188         /* Four independent compression streams for zlib library. */
189         z_stream zlibStream[4];
190         rfbBool zlibStreamActive[4];
191
192         /* Filter stuff. Should be initialized by filter initialization code. */
193         rfbBool cutZeros;
194         int rectWidth, rectColors;
195         char tightPalette[256*4];
196         uint8_t tightPrevRow[2048*3*sizeof(uint16_t)];
197
198 #ifdef LIBVNCSERVER_HAVE_LIBJPEG
199         /* JPEG decoder state. */
200         rfbBool jpegError;
201
202         struct jpeg_source_mgr* jpegSrcManager;
203         void* jpegBufferPtr;
204         size_t jpegBufferLen;
205
206 #endif
207 #endif
208
209
210         /* cursor.c */
211         uint8_t *rcSource, *rcMask;
212
213         /* private data pointer */
214         rfbClientData* clientData;
215
216         rfbVNCRec* vncRec;
217
218         /* Keyboard State support (is 'Caps Lock' set on the remote display???) */
219         int KeyboardLedStateEnabled;
220         int CurrentKeyboardLedState;
221
222         int canHandleNewFBSize;
223
224         /* hooks */
225         HandleTextChatProc         HandleTextChat;
226         HandleKeyboardLedStateProc HandleKeyboardLedState;
227         HandleCursorPosProc HandleCursorPos;
228         SoftCursorLockAreaProc SoftCursorLockArea;
229         SoftCursorUnlockScreenProc SoftCursorUnlockScreen;
230         GotFrameBufferUpdateProc GotFrameBufferUpdate;
231         /* the pointer returned by GetPassword will be freed after use! */
232         GetPasswordProc GetPassword;
233         MallocFrameBufferProc MallocFrameBuffer;
234         GotXCutTextProc GotXCutText;
235         BellProc Bell;
236
237         GotCursorShapeProc GotCursorShape;
238         GotCopyRectProc GotCopyRect;
239
240         /* Which messages are supported by the server
241          * This is a *guess* for most servers.
242          * (If we can even detect the type of server)
243          *
244          * If the server supports the "rfbEncodingSupportedMessages"
245          * then this will be updated when the encoding is received to
246          * accurately reflect the servers capabilities.
247          */
248         rfbSupportedMessages supportedMessages;
249
250         /* negotiated protocol version */
251         int major, minor;
252 } rfbClient;
253
254 /* cursor.c */
255
256 extern rfbBool HandleCursorShape(rfbClient* client,int xhot, int yhot, int width, int height, uint32_t enc);
257
258 /* listen.c */
259
260 extern void listenForIncomingConnections(rfbClient* viewer);
261
262 /* rfbproto.c */
263
264 extern rfbBool rfbEnableClientLogging;
265 typedef void (*rfbClientLogProc)(const char *format, ...);
266 extern rfbClientLogProc rfbClientLog,rfbClientErr;
267 extern rfbBool ConnectToRFBServer(rfbClient* client,const char *hostname, int port);
268 extern rfbBool InitialiseRFBConnection(rfbClient* client);
269 extern rfbBool SetFormatAndEncodings(rfbClient* client);
270 extern rfbBool SendIncrementalFramebufferUpdateRequest(rfbClient* client);
271 extern rfbBool SendFramebufferUpdateRequest(rfbClient* client,
272                                          int x, int y, int w, int h,
273                                          rfbBool incremental);
274 extern rfbBool SendScaleSetting(rfbClient* client,int scaleSetting);
275 extern rfbBool SendPointerEvent(rfbClient* client,int x, int y, int buttonMask);
276 extern rfbBool SendKeyEvent(rfbClient* client,uint32_t key, rfbBool down);
277 extern rfbBool SendClientCutText(rfbClient* client,char *str, int len);
278 extern rfbBool HandleRFBServerMessage(rfbClient* client);
279
280 extern rfbBool TextChatSend(rfbClient* client, char *text);
281 extern rfbBool TextChatOpen(rfbClient* client);
282 extern rfbBool TextChatClose(rfbClient* client);
283 extern rfbBool TextChatFinish(rfbClient* client);
284 extern rfbBool PermitServerInput(rfbClient* client, int enabled);
285
286 extern void PrintPixelFormat(rfbPixelFormat *format);
287
288 /* client data */
289
290 void rfbClientSetClientData(rfbClient* client, void* tag, void* data);
291 void* rfbClientGetClientData(rfbClient* client, void* tag);
292
293 /* protocol extensions */
294
295 typedef struct _rfbClientProtocolExtension {
296         int* encodings;
297         /* returns TRUE if the encoding was handled */
298         rfbBool (*handleEncoding)(rfbClient* cl,
299                 rfbFramebufferUpdateRectHeader* rect);
300         /* returns TRUE if it handled the message */
301         rfbBool (*handleMessage)(rfbClient* cl,
302                  rfbServerToClientMsg* message);
303         struct _rfbClientProtocolExtension* next;
304 } rfbClientProtocolExtension;
305
306 void rfbClientRegisterExtension(rfbClientProtocolExtension* e);
307
308 /* sockets.c */
309
310 extern rfbBool errorMessageOnReadFailure;
311
312 extern rfbBool ReadFromRFBServer(rfbClient* client, char *out, unsigned int n);
313 extern rfbBool WriteToRFBServer(rfbClient* client, char *buf, int n);
314 extern int FindFreeTcpPort(void);
315 extern int ListenAtTcpPort(int port);
316 extern int ConnectClientToTcpAddr(unsigned int host, int port);
317 extern int AcceptTcpConnection(int listenSock);
318 extern rfbBool SetNonBlocking(int sock);
319
320 extern rfbBool StringToIPAddr(const char *str, unsigned int *addr);
321 extern rfbBool SameMachine(int sock);
322 extern int WaitForMessage(rfbClient* client,unsigned int usecs);
323
324 /* vncviewer.c */
325 rfbClient* rfbGetClient(int bitsPerSample,int samplesPerPixel,int bytesPerPixel);
326 rfbBool rfbInitClient(rfbClient* client,int* argc,char** argv);
327 /* rfbClientCleanup() does not touch client->frameBuffer */
328 void rfbClientCleanup(rfbClient* client);
329
330 #if(defined __cplusplus)
331 }
332 #endif
333
334 #endif
335