blob: 3933b38cea39428cf52527178614a818edf0eb1a [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
Pierre Ossmanc898d9a2011-11-14 16:22:23 +000072 // writeEndOfContinuousUpdates() indicates that we have left continuous
73 // updates mode.
74 virtual void writeEndOfContinuousUpdates()=0;
75
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000076 // setupCurrentEncoder() should be called before each framebuffer update,
77 // prior to calling getNumRects() or writeFramebufferUpdateStart().
78 void setupCurrentEncoder();
79
80 // getNumRects() computes the number of sub-rectangles that will compose a
81 // given rectangle, for current encoder.
82 int getNumRects(const Rect &r);
83
84 // writeSetDesktopSize() on a V3 writer won't actually write immediately,
85 // but will write the relevant pseudo-rectangle as part of the next update.
86 virtual bool writeSetDesktopSize()=0;
Pierre Ossman04e62db2009-03-23 16:57:07 +000087 // Same thing for the extended version. The first version queues up a
88 // generic update of the current server state, but the second queues a
89 // specific message.
90 virtual bool writeExtendedDesktopSize()=0;
91 virtual bool writeExtendedDesktopSize(rdr::U16 reason, rdr::U16 result,
92 int fb_width, int fb_height,
93 const ScreenSet& layout)=0;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000094
Peter Åstrandc39e0782009-01-15 12:21:42 +000095 virtual bool writeSetDesktopName()=0;
96
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000097 // Like setDesktopSize, we can't just write out a setCursor message
98 // immediately on a V3 writer. Instead of calling writeSetCursor()
99 // directly, you must call cursorChange(), and then invoke writeSetCursor()
100 // in response to the writeSetCursorCallback() callback. For a V3 writer
101 // this will happen when the next update is sent.
102 virtual void cursorChange(WriteSetCursorCallback* cb)=0;
103 virtual void writeSetCursor(int width, int height, const Point& hotspot,
104 void* data, void* mask)=0;
105 virtual void writeSetXCursor(int width, int height, int hotspotX,
106 int hotspotY, void* data, void* mask)=0;
107
108 // needFakeUpdate() returns true when an immediate update is needed in
Pierre Ossmane9962f72009-04-23 12:31:42 +0000109 // order to flush out pseudo-rectangles to the client.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000110 virtual bool needFakeUpdate();
111
112 // writeFramebufferUpdate() writes a framebuffer update using the given
113 // UpdateInfo and ImageGetter. On a V3 writer this may have
114 // pseudo-rectangles for setDesktopSize and setCursor added to it, and so
115 // may invoke writeSetCursorCallback().
Constantin Kaplinskyfe6b5692007-08-31 15:44:12 +0000116 //
Constantin Kaplinskyfe0db842007-08-31 21:14:45 +0000117 // FIXME: This function is not used because it incorrectly computes
Constantin Kaplinskyfe6b5692007-08-31 15:44:12 +0000118 // the number of rectangles if the Tight encoder is used.
119 /*
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000120 virtual void writeFramebufferUpdate(const UpdateInfo& ui, ImageGetter* ig,
121 Region* updatedRegion);
Constantin Kaplinskyfe6b5692007-08-31 15:44:12 +0000122 */
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000123
Pierre Ossmane9962f72009-04-23 12:31:42 +0000124 // needNoDataUpdate() returns true when an update without any
125 // framebuffer changes need to be sent (using writeNoDataUpdate()).
126 // Commonly this is an update that modifies the size of the framebuffer
127 // or the screen layout.
128 virtual bool needNoDataUpdate();
129
130 // writeNoDataUpdate() write a framebuffer update containing only
131 // pseudo-rectangles.
132 virtual void writeNoDataUpdate();
133
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000134 // writeRects() accepts an UpdateInfo (changed & copied regions) and an
135 // ImageGetter to fetch pixels from. It then calls writeCopyRect() and
136 // writeRect() as appropriate. writeFramebufferUpdateStart() must be used
137 // before the first writeRects() call and writeFrameBufferUpdateEnd() after
138 // the last one. It returns the actual region sent to the client, which
139 // may be smaller than the update passed in.
DRCffe09d62011-08-17 02:27:59 +0000140 virtual void writeRects(const UpdateInfo& update, TransImageGetter* ig,
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000141 Region* updatedRegion);
142
143 // To construct a framebuffer update you can call
144 // writeFramebufferUpdateStart(), followed by a number of writeCopyRect()s
145 // and writeRect()s, finishing with writeFramebufferUpdateEnd(). If you
146 // know the exact number of rectangles ahead of time you can specify it to
147 // writeFramebufferUpdateStart() which can be more efficient.
148 virtual void writeFramebufferUpdateStart(int nRects)=0;
149 virtual void writeFramebufferUpdateStart()=0;
150 virtual void writeFramebufferUpdateEnd()=0;
151
152 // writeRect() tries to write the given rectangle. If it is unable to
153 // write the whole rectangle it returns false and sets actual to the actual
154 // rectangle which was updated.
DRCffe09d62011-08-17 02:27:59 +0000155 virtual bool writeRect(const Rect& r, TransImageGetter* ig, Rect* actual);
Peter Åstrand98fe98c2010-02-10 07:43:02 +0000156 virtual bool writeRect(const Rect& r, int encoding,
DRCffe09d62011-08-17 02:27:59 +0000157 TransImageGetter* ig, Rect* actual);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000158
159 virtual void writeCopyRect(const Rect& r, int srcX, int srcY);
160
Peter Åstrand98fe98c2010-02-10 07:43:02 +0000161 virtual void startRect(const Rect& r, int enc)=0;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000162 virtual void endRect()=0;
163
164 ConnParams* getConnParams() { return cp; }
165 rdr::OutStream* getOutStream() { return os; }
166 rdr::U8* getImageBuf(int required, int requested=0, int* nPixels=0);
167 int bpp();
168
169 int getUpdatesSent() { return updatesSent; }
170 int getRectsSent(int encoding) { return rectsSent[encoding]; }
171 int getBytesSent(int encoding) { return bytesSent[encoding]; }
DRC887c5fd2011-08-19 03:13:47 +0000172 rdr::U64 getRawBytesEquivalent() { return rawBytesEquivalent; }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000173
174 int imageBufIdealSize;
175
176 protected:
177 SMsgWriter(ConnParams* cp, rdr::OutStream* os);
178
179 virtual void startMsg(int type)=0;
180 virtual void endMsg()=0;
181
182 ConnParams* cp;
183 rdr::OutStream* os;
184
185 Encoder* encoders[encodingMax+1];
186 int lenBeforeRect;
Peter Åstrand98fe98c2010-02-10 07:43:02 +0000187 int currentEncoding;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000188 int updatesSent;
189 int bytesSent[encodingMax+1];
190 int rectsSent[encodingMax+1];
DRC887c5fd2011-08-19 03:13:47 +0000191 rdr::U64 rawBytesEquivalent;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000192
193 rdr::U8* imageBuf;
194 int imageBufSize;
195 };
196}
197#endif