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.
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.
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.
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,
34 #include <rfb/rfbproto.h>
35 #include <rfb/keysym.h>
37 #define rfbClientSwap16IfLE(s) \
38 (*(char *)&client->endianTest ? ((((s) & 0xff) << 8) | (((s) >> 8) & 0xff)) : (s))
40 #define rfbClientSwap32IfLE(l) \
41 (*(char *)&client->endianTest ? ((((l) & 0xff000000) >> 24) | \
42 (((l) & 0x00ff0000) >> 8) | \
43 (((l) & 0x0000ff00) << 8) | \
44 (((l) & 0x000000ff) << 24)) : (l))
46 #define FLASH_PORT_OFFSET 5400
47 #define LISTEN_PORT_OFFSET 5500
48 #define TUNNEL_PORT_OFFSET 5500
49 #define SERVER_PORT_OFFSET 5900
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")
57 #if(defined __cplusplus)
67 rfbBool readTimestamp;
73 typedef struct rfbClientData {
76 struct rfbClientData* next;
79 /* app data (belongs into rfbClient?) */
85 const char* encodingsString;
90 rfbBool forceTrueColour;
96 rfbBool useRemoteCursor;
97 rfbBool palmVNC; /* use palmvnc specific SetScale (vs ultravnc) */
98 int scaleSetting; /* 0 means no scale set, else 1/scaleSetting */
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);
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);
118 typedef struct _rfbClient {
119 uint8_t* frameBuffer;
126 const char* programName;
128 int serverPort; /* if -1, then use file recorded by vncrec */
129 rfbBool listenSpecified;
130 int listenPort, flashPort;
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. */
141 #define RFB_BUFFER_SIZE (640*480)
142 char buffer[RFB_BUFFER_SIZE];
148 rfbBool canUseHextile;
150 rfbPixelFormat format;
154 #define RFB_BUF_SIZE 8192
155 char buf[RFB_BUF_SIZE];
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. */
165 /* Ultra Encoding uses this buffer too */
167 int ultra_buffer_size;
173 #ifdef LIBVNCSERVER_HAVE_LIBZ
174 z_stream decompStream;
175 rfbBool decompStreamInited;
179 #ifdef LIBVNCSERVER_HAVE_LIBZ
181 * Variables for the ``tight'' encoding implementation.
184 /* Separate buffer for compressed data. */
185 #define ZLIB_BUFFER_SIZE 30000
186 char zlib_buffer[ZLIB_BUFFER_SIZE];
188 /* Four independent compression streams for zlib library. */
189 z_stream zlibStream[4];
190 rfbBool zlibStreamActive[4];
192 /* Filter stuff. Should be initialized by filter initialization code. */
194 int rectWidth, rectColors;
195 char tightPalette[256*4];
196 uint8_t tightPrevRow[2048*3*sizeof(uint16_t)];
198 #ifdef LIBVNCSERVER_HAVE_LIBJPEG
199 /* JPEG decoder state. */
202 struct jpeg_source_mgr* jpegSrcManager;
204 size_t jpegBufferLen;
211 uint8_t *rcSource, *rcMask;
213 /* private data pointer */
214 rfbClientData* clientData;
218 /* Keyboard State support (is 'Caps Lock' set on the remote display???) */
219 int KeyboardLedStateEnabled;
220 int CurrentKeyboardLedState;
222 int canHandleNewFBSize;
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;
237 GotCursorShapeProc GotCursorShape;
238 GotCopyRectProc GotCopyRect;
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)
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.
248 rfbSupportedMessages supportedMessages;
250 /* negotiated protocol version */
256 extern rfbBool HandleCursorShape(rfbClient* client,int xhot, int yhot, int width, int height, uint32_t enc);
260 extern void listenForIncomingConnections(rfbClient* viewer);
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);
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);
286 extern void PrintPixelFormat(rfbPixelFormat *format);
290 void rfbClientSetClientData(rfbClient* client, void* tag, void* data);
291 void* rfbClientGetClientData(rfbClient* client, void* tag);
293 /* protocol extensions */
295 typedef struct _rfbClientProtocolExtension {
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;
306 void rfbClientRegisterExtension(rfbClientProtocolExtension* e);
310 extern rfbBool errorMessageOnReadFailure;
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);
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);
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);
330 #if(defined __cplusplus)