blob: 3c6da95a2264463445aa9b4b4fcf2a6cbd02d841 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18//
19// SMsgWriter - class for writing RFB messages on the server side.
20//
21
22#ifndef __RFB_SMSGWRITER_H__
23#define __RFB_SMSGWRITER_H__
24
25#include <rdr/types.h>
26#include <rfb/encodings.h>
Pierre Ossmanc5e25602009-03-20 12:59:05 +000027#include <rfb/screenTypes.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000028#include <rfb/Encoder.h>
Constantin Kaplinsky46ba45a2007-08-31 21:31:34 +000029#include <rfb/PixelBuffer.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000030
31namespace rdr { class OutStream; }
32
33namespace rfb {
34
35 class PixelFormat;
36 class ConnParams;
37 class ImageGetter;
38 class ColourMap;
39 class Region;
40 class UpdateInfo;
41
42 class WriteSetCursorCallback {
43 public:
44 virtual void writeSetCursorCallback() = 0;
45 };
46
47 class SMsgWriter {
48 public:
49 virtual ~SMsgWriter();
50
51 // writeServerInit() must only be called at the appropriate time in the
52 // protocol initialisation.
53 virtual void writeServerInit()=0;
54
55 // Methods to write normal protocol messages
56
57 // writeSetColourMapEntries() writes a setColourMapEntries message, using
58 // the given ColourMap object to lookup the RGB values of the given range
59 // of colours.
60 virtual void writeSetColourMapEntries(int firstColour, int nColours,
61 ColourMap* cm);
62
63 // writeBell() and writeServerCutText() do the obvious thing.
64 virtual void writeBell();
65 virtual void writeServerCutText(const char* str, int len);
66
67 // setupCurrentEncoder() should be called before each framebuffer update,
68 // prior to calling getNumRects() or writeFramebufferUpdateStart().
69 void setupCurrentEncoder();
70
71 // getNumRects() computes the number of sub-rectangles that will compose a
72 // given rectangle, for current encoder.
73 int getNumRects(const Rect &r);
74
75 // writeSetDesktopSize() on a V3 writer won't actually write immediately,
76 // but will write the relevant pseudo-rectangle as part of the next update.
77 virtual bool writeSetDesktopSize()=0;
Pierre Ossmanc5e25602009-03-20 12:59:05 +000078 // Same thing for the extended version
79 virtual bool writeExtendedDesktopSize(rdr::U16 error = resultUnsolicited)=0;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000080
Peter Åstrandc39e0782009-01-15 12:21:42 +000081 virtual bool writeSetDesktopName()=0;
82
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000083 // Like setDesktopSize, we can't just write out a setCursor message
84 // immediately on a V3 writer. Instead of calling writeSetCursor()
85 // directly, you must call cursorChange(), and then invoke writeSetCursor()
86 // in response to the writeSetCursorCallback() callback. For a V3 writer
87 // this will happen when the next update is sent.
88 virtual void cursorChange(WriteSetCursorCallback* cb)=0;
89 virtual void writeSetCursor(int width, int height, const Point& hotspot,
90 void* data, void* mask)=0;
91 virtual void writeSetXCursor(int width, int height, int hotspotX,
92 int hotspotY, void* data, void* mask)=0;
93
94 // needFakeUpdate() returns true when an immediate update is needed in
95 // order to flush out setDesktopSize or setCursor pseudo-rectangles to the
96 // client.
97 virtual bool needFakeUpdate();
98
99 // writeFramebufferUpdate() writes a framebuffer update using the given
100 // UpdateInfo and ImageGetter. On a V3 writer this may have
101 // pseudo-rectangles for setDesktopSize and setCursor added to it, and so
102 // may invoke writeSetCursorCallback().
Constantin Kaplinskyfe6b5692007-08-31 15:44:12 +0000103 //
Constantin Kaplinskyfe0db842007-08-31 21:14:45 +0000104 // FIXME: This function is not used because it incorrectly computes
Constantin Kaplinskyfe6b5692007-08-31 15:44:12 +0000105 // the number of rectangles if the Tight encoder is used.
106 /*
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000107 virtual void writeFramebufferUpdate(const UpdateInfo& ui, ImageGetter* ig,
108 Region* updatedRegion);
Constantin Kaplinskyfe6b5692007-08-31 15:44:12 +0000109 */
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000110
111 // writeRects() accepts an UpdateInfo (changed & copied regions) and an
112 // ImageGetter to fetch pixels from. It then calls writeCopyRect() and
113 // writeRect() as appropriate. writeFramebufferUpdateStart() must be used
114 // before the first writeRects() call and writeFrameBufferUpdateEnd() after
115 // the last one. It returns the actual region sent to the client, which
116 // may be smaller than the update passed in.
117 virtual void writeRects(const UpdateInfo& update, ImageGetter* ig,
118 Region* updatedRegion);
119
120 // To construct a framebuffer update you can call
121 // writeFramebufferUpdateStart(), followed by a number of writeCopyRect()s
122 // and writeRect()s, finishing with writeFramebufferUpdateEnd(). If you
123 // know the exact number of rectangles ahead of time you can specify it to
124 // writeFramebufferUpdateStart() which can be more efficient.
125 virtual void writeFramebufferUpdateStart(int nRects)=0;
126 virtual void writeFramebufferUpdateStart()=0;
127 virtual void writeFramebufferUpdateEnd()=0;
128
129 // writeRect() tries to write the given rectangle. If it is unable to
130 // write the whole rectangle it returns false and sets actual to the actual
131 // rectangle which was updated.
132 virtual bool writeRect(const Rect& r, ImageGetter* ig, Rect* actual);
133 virtual bool writeRect(const Rect& r, unsigned int encoding,
134 ImageGetter* ig, Rect* actual);
135
136 virtual void writeCopyRect(const Rect& r, int srcX, int srcY);
137
138 virtual void startRect(const Rect& r, unsigned int enc)=0;
139 virtual void endRect()=0;
140
141 ConnParams* getConnParams() { return cp; }
142 rdr::OutStream* getOutStream() { return os; }
143 rdr::U8* getImageBuf(int required, int requested=0, int* nPixels=0);
144 int bpp();
145
146 int getUpdatesSent() { return updatesSent; }
147 int getRectsSent(int encoding) { return rectsSent[encoding]; }
148 int getBytesSent(int encoding) { return bytesSent[encoding]; }
149 int getRawBytesEquivalent() { return rawBytesEquivalent; }
150
151 int imageBufIdealSize;
152
153 protected:
154 SMsgWriter(ConnParams* cp, rdr::OutStream* os);
155
156 virtual void startMsg(int type)=0;
157 virtual void endMsg()=0;
158
159 ConnParams* cp;
160 rdr::OutStream* os;
161
162 Encoder* encoders[encodingMax+1];
163 int lenBeforeRect;
164 unsigned int currentEncoding;
165 int updatesSent;
166 int bytesSent[encodingMax+1];
167 int rectsSent[encodingMax+1];
168 int rawBytesEquivalent;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000169
170 rdr::U8* imageBuf;
171 int imageBufSize;
172 };
173}
174#endif