blob: bab9a1a2495f28c4b9d39ac689d20a3a17dfcb79 [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>
27#include <rfb/Encoder.h>
Constantin Kaplinsky46ba45a2007-08-31 21:31:34 +000028#include <rfb/PixelBuffer.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000029
30namespace rdr { class OutStream; }
31
32namespace rfb {
33
34 class PixelFormat;
35 class ConnParams;
36 class ImageGetter;
37 class ColourMap;
38 class Region;
39 class UpdateInfo;
40
41 class WriteSetCursorCallback {
42 public:
43 virtual void writeSetCursorCallback() = 0;
44 };
45
46 class SMsgWriter {
47 public:
48 virtual ~SMsgWriter();
49
50 // writeServerInit() must only be called at the appropriate time in the
51 // protocol initialisation.
52 virtual void writeServerInit()=0;
53
54 // Methods to write normal protocol messages
55
56 // writeSetColourMapEntries() writes a setColourMapEntries message, using
57 // the given ColourMap object to lookup the RGB values of the given range
58 // of colours.
59 virtual void writeSetColourMapEntries(int firstColour, int nColours,
60 ColourMap* cm);
61
62 // writeBell() and writeServerCutText() do the obvious thing.
63 virtual void writeBell();
64 virtual void writeServerCutText(const char* str, int len);
65
66 // setupCurrentEncoder() should be called before each framebuffer update,
67 // prior to calling getNumRects() or writeFramebufferUpdateStart().
68 void setupCurrentEncoder();
69
70 // getNumRects() computes the number of sub-rectangles that will compose a
71 // given rectangle, for current encoder.
72 int getNumRects(const Rect &r);
73
74 // writeSetDesktopSize() on a V3 writer won't actually write immediately,
75 // but will write the relevant pseudo-rectangle as part of the next update.
76 virtual bool writeSetDesktopSize()=0;
77
Peter Åstrandc39e0782009-01-15 12:21:42 +000078 virtual bool writeSetDesktopName()=0;
79
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000080 // Like setDesktopSize, we can't just write out a setCursor message
81 // immediately on a V3 writer. Instead of calling writeSetCursor()
82 // directly, you must call cursorChange(), and then invoke writeSetCursor()
83 // in response to the writeSetCursorCallback() callback. For a V3 writer
84 // this will happen when the next update is sent.
85 virtual void cursorChange(WriteSetCursorCallback* cb)=0;
86 virtual void writeSetCursor(int width, int height, const Point& hotspot,
87 void* data, void* mask)=0;
88 virtual void writeSetXCursor(int width, int height, int hotspotX,
89 int hotspotY, void* data, void* mask)=0;
90
91 // needFakeUpdate() returns true when an immediate update is needed in
92 // order to flush out setDesktopSize or setCursor pseudo-rectangles to the
93 // client.
94 virtual bool needFakeUpdate();
95
96 // writeFramebufferUpdate() writes a framebuffer update using the given
97 // UpdateInfo and ImageGetter. On a V3 writer this may have
98 // pseudo-rectangles for setDesktopSize and setCursor added to it, and so
99 // may invoke writeSetCursorCallback().
Constantin Kaplinskyfe6b5692007-08-31 15:44:12 +0000100 //
Constantin Kaplinskyfe0db842007-08-31 21:14:45 +0000101 // FIXME: This function is not used because it incorrectly computes
Constantin Kaplinskyfe6b5692007-08-31 15:44:12 +0000102 // the number of rectangles if the Tight encoder is used.
103 /*
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000104 virtual void writeFramebufferUpdate(const UpdateInfo& ui, ImageGetter* ig,
105 Region* updatedRegion);
Constantin Kaplinskyfe6b5692007-08-31 15:44:12 +0000106 */
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000107
108 // writeRects() accepts an UpdateInfo (changed & copied regions) and an
109 // ImageGetter to fetch pixels from. It then calls writeCopyRect() and
110 // writeRect() as appropriate. writeFramebufferUpdateStart() must be used
111 // before the first writeRects() call and writeFrameBufferUpdateEnd() after
112 // the last one. It returns the actual region sent to the client, which
113 // may be smaller than the update passed in.
114 virtual void writeRects(const UpdateInfo& update, ImageGetter* ig,
115 Region* updatedRegion);
116
117 // To construct a framebuffer update you can call
118 // writeFramebufferUpdateStart(), followed by a number of writeCopyRect()s
119 // and writeRect()s, finishing with writeFramebufferUpdateEnd(). If you
120 // know the exact number of rectangles ahead of time you can specify it to
121 // writeFramebufferUpdateStart() which can be more efficient.
122 virtual void writeFramebufferUpdateStart(int nRects)=0;
123 virtual void writeFramebufferUpdateStart()=0;
124 virtual void writeFramebufferUpdateEnd()=0;
125
126 // writeRect() tries to write the given rectangle. If it is unable to
127 // write the whole rectangle it returns false and sets actual to the actual
128 // rectangle which was updated.
129 virtual bool writeRect(const Rect& r, ImageGetter* ig, Rect* actual);
130 virtual bool writeRect(const Rect& r, unsigned int encoding,
131 ImageGetter* ig, Rect* actual);
132
133 virtual void writeCopyRect(const Rect& r, int srcX, int srcY);
134
135 virtual void startRect(const Rect& r, unsigned int enc)=0;
136 virtual void endRect()=0;
137
138 ConnParams* getConnParams() { return cp; }
139 rdr::OutStream* getOutStream() { return os; }
140 rdr::U8* getImageBuf(int required, int requested=0, int* nPixels=0);
141 int bpp();
142
143 int getUpdatesSent() { return updatesSent; }
144 int getRectsSent(int encoding) { return rectsSent[encoding]; }
145 int getBytesSent(int encoding) { return bytesSent[encoding]; }
146 int getRawBytesEquivalent() { return rawBytesEquivalent; }
147
148 int imageBufIdealSize;
149
150 protected:
151 SMsgWriter(ConnParams* cp, rdr::OutStream* os);
152
153 virtual void startMsg(int type)=0;
154 virtual void endMsg()=0;
155
156 ConnParams* cp;
157 rdr::OutStream* os;
158
159 Encoder* encoders[encodingMax+1];
160 int lenBeforeRect;
161 unsigned int currentEncoding;
162 int updatesSent;
163 int bytesSent[encodingMax+1];
164 int rectsSent[encodingMax+1];
165 int rawBytesEquivalent;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000166
167 rdr::U8* imageBuf;
168 int imageBufSize;
169 };
170}
171#endif