blob: 5d45411dd27dc5b3ac5d7ff7a404ef9236707a7a [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossman7638e9c2014-01-16 13:12:40 +01002 * Copyright 2009-2014 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 Ossman04e62db2009-03-23 16:57:07 +000028#include <rfb/ScreenSet.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000029
30namespace rdr { class OutStream; }
31
32namespace rfb {
33
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000034 class ConnParams;
Pierre Ossman7638e9c2014-01-16 13:12:40 +010035 class TransImageGetter;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000036 class ColourMap;
37 class Region;
38 class UpdateInfo;
Pierre Ossman7638e9c2014-01-16 13:12:40 +010039 class Encoder;
40 class ScreenSet;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000041
42 class WriteSetCursorCallback {
43 public:
44 virtual void writeSetCursorCallback() = 0;
45 };
46
47 class SMsgWriter {
48 public:
Pierre Ossman7638e9c2014-01-16 13:12:40 +010049 SMsgWriter(ConnParams* cp, rdr::OutStream* os);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000050 virtual ~SMsgWriter();
51
52 // writeServerInit() must only be called at the appropriate time in the
53 // protocol initialisation.
Pierre Ossman7638e9c2014-01-16 13:12:40 +010054 void writeServerInit();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000055
56 // Methods to write normal protocol messages
57
58 // writeSetColourMapEntries() writes a setColourMapEntries message, using
59 // the given ColourMap object to lookup the RGB values of the given range
60 // of colours.
Pierre Ossman7638e9c2014-01-16 13:12:40 +010061 void writeSetColourMapEntries(int firstColour, int nColours,
62 ColourMap* cm);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000063
64 // writeBell() and writeServerCutText() do the obvious thing.
Pierre Ossman7638e9c2014-01-16 13:12:40 +010065 void writeBell();
66 void writeServerCutText(const char* str, int len);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000067
Pierre Ossmanc754cce2011-11-14 15:44:11 +000068 // writeFence() sends a new fence request or response to the client.
Pierre Ossman7638e9c2014-01-16 13:12:40 +010069 void writeFence(rdr::U32 flags, unsigned len, const char data[]);
Pierre Ossmanc754cce2011-11-14 15:44:11 +000070
Pierre Ossmanc898d9a2011-11-14 16:22:23 +000071 // writeEndOfContinuousUpdates() indicates that we have left continuous
72 // updates mode.
Pierre Ossman7638e9c2014-01-16 13:12:40 +010073 void writeEndOfContinuousUpdates();
Pierre Ossmanc898d9a2011-11-14 16:22:23 +000074
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000075 // setupCurrentEncoder() should be called before each framebuffer update,
76 // prior to calling getNumRects() or writeFramebufferUpdateStart().
77 void setupCurrentEncoder();
78
79 // getNumRects() computes the number of sub-rectangles that will compose a
80 // given rectangle, for current encoder.
81 int getNumRects(const Rect &r);
82
Pierre Ossman7638e9c2014-01-16 13:12:40 +010083 // writeSetDesktopSize() won't actually write immediately, but will
84 // write the relevant pseudo-rectangle as part of the next update.
85 bool writeSetDesktopSize();
Pierre Ossman04e62db2009-03-23 16:57:07 +000086 // Same thing for the extended version. The first version queues up a
87 // generic update of the current server state, but the second queues a
88 // specific message.
Pierre Ossman7638e9c2014-01-16 13:12:40 +010089 bool writeExtendedDesktopSize();
90 bool writeExtendedDesktopSize(rdr::U16 reason, rdr::U16 result,
91 int fb_width, int fb_height,
92 const ScreenSet& layout);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000093
Pierre Ossman7638e9c2014-01-16 13:12:40 +010094 bool writeSetDesktopName();
Peter Åstrandc39e0782009-01-15 12:21:42 +000095
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000096 // Like setDesktopSize, we can't just write out a setCursor message
Pierre Ossman7638e9c2014-01-16 13:12:40 +010097 // immediately. Instead of calling writeSetCursor() directly,
98 // you must call cursorChange(), and then invoke writeSetCursor()
99 // in response to the writeSetCursorCallback() callback. This will
100 // happen when the next update is sent.
101 void cursorChange(WriteSetCursorCallback* cb);
102 void writeSetCursor(int width, int height, const Point& hotspot,
103 void* data, void* mask);
104 void writeSetXCursor(int width, int height, int hotspotX, int hotspotY,
105 void* data, void* mask);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000106
107 // needFakeUpdate() returns true when an immediate update is needed in
Pierre Ossmane9962f72009-04-23 12:31:42 +0000108 // order to flush out pseudo-rectangles to the client.
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100109 bool needFakeUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000110
Pierre Ossmane9962f72009-04-23 12:31:42 +0000111 // needNoDataUpdate() returns true when an update without any
112 // framebuffer changes need to be sent (using writeNoDataUpdate()).
113 // Commonly this is an update that modifies the size of the framebuffer
114 // or the screen layout.
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100115 bool needNoDataUpdate();
Pierre Ossmane9962f72009-04-23 12:31:42 +0000116
117 // writeNoDataUpdate() write a framebuffer update containing only
118 // pseudo-rectangles.
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100119 void writeNoDataUpdate();
Pierre Ossmane9962f72009-04-23 12:31:42 +0000120
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000121 // writeRects() accepts an UpdateInfo (changed & copied regions) and an
122 // ImageGetter to fetch pixels from. It then calls writeCopyRect() and
123 // writeRect() as appropriate. writeFramebufferUpdateStart() must be used
124 // before the first writeRects() call and writeFrameBufferUpdateEnd() after
Pierre Ossman717c07b2014-01-21 14:45:10 +0100125 // the last one.
126 void writeRects(const UpdateInfo& update, TransImageGetter* ig);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000127
128 // To construct a framebuffer update you can call
129 // writeFramebufferUpdateStart(), followed by a number of writeCopyRect()s
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100130 // and writeRect()s, finishing with writeFramebufferUpdateEnd().
131 void writeFramebufferUpdateStart(int nRects);
132 void writeFramebufferUpdateEnd();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000133
Pierre Ossman717c07b2014-01-21 14:45:10 +0100134 // writeRect() writers the given rectangle using either the preferred
135 // encoder, or the one explicitly given.
136 void writeRect(const Rect& r, TransImageGetter* ig);
137 void writeRect(const Rect& r, int encoding, TransImageGetter* ig);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000138
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100139 void writeCopyRect(const Rect& r, int srcX, int srcY);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000140
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100141 void startRect(const Rect& r, int enc);
142 void endRect();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000143
144 ConnParams* getConnParams() { return cp; }
145 rdr::OutStream* getOutStream() { return os; }
146 rdr::U8* getImageBuf(int required, int requested=0, int* nPixels=0);
147 int bpp();
148
149 int getUpdatesSent() { return updatesSent; }
150 int getRectsSent(int encoding) { return rectsSent[encoding]; }
151 int getBytesSent(int encoding) { return bytesSent[encoding]; }
DRC887c5fd2011-08-19 03:13:47 +0000152 rdr::U64 getRawBytesEquivalent() { return rawBytesEquivalent; }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000153
154 int imageBufIdealSize;
155
156 protected:
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100157 void startMsg(int type);
158 void endMsg();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000159
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100160 void writePseudoRects();
161 void writeNoDataRects();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000162
Pierre Ossmanc0b1aa02014-01-16 13:39:05 +0100163 void writeSetDesktopSizeRect(int width, int height);
164 void writeExtendedDesktopSizeRect(rdr::U16 reason, rdr::U16 result,
165 int fb_width, int fb_height,
166 const ScreenSet& layout);
167 void writeSetDesktopNameRect(const char *name);
168
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000169 ConnParams* cp;
170 rdr::OutStream* os;
171
172 Encoder* encoders[encodingMax+1];
Peter Åstrand98fe98c2010-02-10 07:43:02 +0000173 int currentEncoding;
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100174
175 int nRectsInUpdate;
176 int nRectsInHeader;
177
178 WriteSetCursorCallback* wsccb;
179
180 bool needSetDesktopSize;
181 bool needExtendedDesktopSize;
182 bool needSetDesktopName;
183 bool needLastRect;
184
185 int lenBeforeRect;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000186 int updatesSent;
187 int bytesSent[encodingMax+1];
188 int rectsSent[encodingMax+1];
DRC887c5fd2011-08-19 03:13:47 +0000189 rdr::U64 rawBytesEquivalent;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000190
191 rdr::U8* imageBuf;
192 int imageBufSize;
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100193
194 typedef struct {
195 rdr::U16 reason, result;
196 int fb_width, fb_height;
197 ScreenSet layout;
198 } ExtendedDesktopSizeMsg;
199
200 std::list<ExtendedDesktopSizeMsg> extendedDesktopSizeMsgs;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000201 };
202}
203#endif