blob: aaa98e441d520e89f5aeec878264cc6601adaf04 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossmanc754cce2011-11-14 15:44:11 +00002 * Copyright 2009-2011 Pierre Ossman for Cendio AB
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00003 *
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// SMsgWriter - class for writing RFB messages on the server side.
21//
22
23#ifndef __RFB_SMSGWRITER_H__
24#define __RFB_SMSGWRITER_H__
25
26#include <rdr/types.h>
27#include <rfb/encodings.h>
Pierre Ossmanc5e25602009-03-20 12:59:05 +000028#include <rfb/screenTypes.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000029#include <rfb/Encoder.h>
Constantin Kaplinsky46ba45a2007-08-31 21:31:34 +000030#include <rfb/PixelBuffer.h>
Pierre Ossman04e62db2009-03-23 16:57:07 +000031#include <rfb/ScreenSet.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000032
33namespace rdr { class OutStream; }
34
35namespace rfb {
36
37 class PixelFormat;
38 class ConnParams;
39 class ImageGetter;
40 class ColourMap;
41 class Region;
42 class UpdateInfo;
43
44 class WriteSetCursorCallback {
45 public:
46 virtual void writeSetCursorCallback() = 0;
47 };
48
49 class SMsgWriter {
50 public:
51 virtual ~SMsgWriter();
52
53 // writeServerInit() must only be called at the appropriate time in the
54 // protocol initialisation.
55 virtual void writeServerInit()=0;
56
57 // Methods to write normal protocol messages
58
59 // writeSetColourMapEntries() writes a setColourMapEntries message, using
60 // the given ColourMap object to lookup the RGB values of the given range
61 // of colours.
62 virtual void writeSetColourMapEntries(int firstColour, int nColours,
63 ColourMap* cm);
64
65 // writeBell() and writeServerCutText() do the obvious thing.
66 virtual void writeBell();
67 virtual void writeServerCutText(const char* str, int len);
68
Pierre Ossmanc754cce2011-11-14 15:44:11 +000069 // writeFence() sends a new fence request or response to the client.
70 virtual void writeFence(rdr::U32 flags, unsigned len, const char data[])=0;
71
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000072 // setupCurrentEncoder() should be called before each framebuffer update,
73 // prior to calling getNumRects() or writeFramebufferUpdateStart().
74 void setupCurrentEncoder();
75
76 // getNumRects() computes the number of sub-rectangles that will compose a
77 // given rectangle, for current encoder.
78 int getNumRects(const Rect &r);
79
80 // writeSetDesktopSize() on a V3 writer won't actually write immediately,
81 // but will write the relevant pseudo-rectangle as part of the next update.
82 virtual bool writeSetDesktopSize()=0;
Pierre Ossman04e62db2009-03-23 16:57:07 +000083 // Same thing for the extended version. The first version queues up a
84 // generic update of the current server state, but the second queues a
85 // specific message.
86 virtual bool writeExtendedDesktopSize()=0;
87 virtual bool writeExtendedDesktopSize(rdr::U16 reason, rdr::U16 result,
88 int fb_width, int fb_height,
89 const ScreenSet& layout)=0;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000090
Peter Åstrandc39e0782009-01-15 12:21:42 +000091 virtual bool writeSetDesktopName()=0;
92
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000093 // Like setDesktopSize, we can't just write out a setCursor message
94 // immediately on a V3 writer. Instead of calling writeSetCursor()
95 // directly, you must call cursorChange(), and then invoke writeSetCursor()
96 // in response to the writeSetCursorCallback() callback. For a V3 writer
97 // this will happen when the next update is sent.
98 virtual void cursorChange(WriteSetCursorCallback* cb)=0;
99 virtual void writeSetCursor(int width, int height, const Point& hotspot,
100 void* data, void* mask)=0;
101 virtual void writeSetXCursor(int width, int height, int hotspotX,
102 int hotspotY, void* data, void* mask)=0;
103
104 // needFakeUpdate() returns true when an immediate update is needed in
Pierre Ossmane9962f72009-04-23 12:31:42 +0000105 // order to flush out pseudo-rectangles to the client.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000106 virtual bool needFakeUpdate();
107
108 // writeFramebufferUpdate() writes a framebuffer update using the given
109 // UpdateInfo and ImageGetter. On a V3 writer this may have
110 // pseudo-rectangles for setDesktopSize and setCursor added to it, and so
111 // may invoke writeSetCursorCallback().
Constantin Kaplinskyfe6b5692007-08-31 15:44:12 +0000112 //
Constantin Kaplinskyfe0db842007-08-31 21:14:45 +0000113 // FIXME: This function is not used because it incorrectly computes
Constantin Kaplinskyfe6b5692007-08-31 15:44:12 +0000114 // the number of rectangles if the Tight encoder is used.
115 /*
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000116 virtual void writeFramebufferUpdate(const UpdateInfo& ui, ImageGetter* ig,
117 Region* updatedRegion);
Constantin Kaplinskyfe6b5692007-08-31 15:44:12 +0000118 */
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000119
Pierre Ossmane9962f72009-04-23 12:31:42 +0000120 // needNoDataUpdate() returns true when an update without any
121 // framebuffer changes need to be sent (using writeNoDataUpdate()).
122 // Commonly this is an update that modifies the size of the framebuffer
123 // or the screen layout.
124 virtual bool needNoDataUpdate();
125
126 // writeNoDataUpdate() write a framebuffer update containing only
127 // pseudo-rectangles.
128 virtual void writeNoDataUpdate();
129
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000130 // writeRects() accepts an UpdateInfo (changed & copied regions) and an
131 // ImageGetter to fetch pixels from. It then calls writeCopyRect() and
132 // writeRect() as appropriate. writeFramebufferUpdateStart() must be used
133 // before the first writeRects() call and writeFrameBufferUpdateEnd() after
134 // the last one. It returns the actual region sent to the client, which
135 // may be smaller than the update passed in.
DRCffe09d62011-08-17 02:27:59 +0000136 virtual void writeRects(const UpdateInfo& update, TransImageGetter* ig,
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000137 Region* updatedRegion);
138
139 // To construct a framebuffer update you can call
140 // writeFramebufferUpdateStart(), followed by a number of writeCopyRect()s
141 // and writeRect()s, finishing with writeFramebufferUpdateEnd(). If you
142 // know the exact number of rectangles ahead of time you can specify it to
143 // writeFramebufferUpdateStart() which can be more efficient.
144 virtual void writeFramebufferUpdateStart(int nRects)=0;
145 virtual void writeFramebufferUpdateStart()=0;
146 virtual void writeFramebufferUpdateEnd()=0;
147
148 // writeRect() tries to write the given rectangle. If it is unable to
149 // write the whole rectangle it returns false and sets actual to the actual
150 // rectangle which was updated.
DRCffe09d62011-08-17 02:27:59 +0000151 virtual bool writeRect(const Rect& r, TransImageGetter* ig, Rect* actual);
Peter Åstrand98fe98c2010-02-10 07:43:02 +0000152 virtual bool writeRect(const Rect& r, int encoding,
DRCffe09d62011-08-17 02:27:59 +0000153 TransImageGetter* ig, Rect* actual);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000154
155 virtual void writeCopyRect(const Rect& r, int srcX, int srcY);
156
Peter Åstrand98fe98c2010-02-10 07:43:02 +0000157 virtual void startRect(const Rect& r, int enc)=0;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000158 virtual void endRect()=0;
159
160 ConnParams* getConnParams() { return cp; }
161 rdr::OutStream* getOutStream() { return os; }
162 rdr::U8* getImageBuf(int required, int requested=0, int* nPixels=0);
163 int bpp();
164
165 int getUpdatesSent() { return updatesSent; }
166 int getRectsSent(int encoding) { return rectsSent[encoding]; }
167 int getBytesSent(int encoding) { return bytesSent[encoding]; }
DRC887c5fd2011-08-19 03:13:47 +0000168 rdr::U64 getRawBytesEquivalent() { return rawBytesEquivalent; }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000169
170 int imageBufIdealSize;
171
172 protected:
173 SMsgWriter(ConnParams* cp, rdr::OutStream* os);
174
175 virtual void startMsg(int type)=0;
176 virtual void endMsg()=0;
177
178 ConnParams* cp;
179 rdr::OutStream* os;
180
181 Encoder* encoders[encodingMax+1];
182 int lenBeforeRect;
Peter Åstrand98fe98c2010-02-10 07:43:02 +0000183 int currentEncoding;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000184 int updatesSent;
185 int bytesSent[encodingMax+1];
186 int rectsSent[encodingMax+1];
DRC887c5fd2011-08-19 03:13:47 +0000187 rdr::U64 rawBytesEquivalent;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000188
189 rdr::U8* imageBuf;
190 int imageBufSize;
191 };
192}
193#endif