Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 2 | * Copyright 2009-2014 Pierre Ossman for Cendio AB |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 3 | * |
| 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 Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 28 | #include <rfb/ScreenSet.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 29 | |
| 30 | namespace rdr { class OutStream; } |
| 31 | |
| 32 | namespace rfb { |
| 33 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 34 | class ConnParams; |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 35 | class TransImageGetter; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 36 | class ColourMap; |
| 37 | class Region; |
| 38 | class UpdateInfo; |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 39 | class Encoder; |
| 40 | class ScreenSet; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 41 | |
| 42 | class WriteSetCursorCallback { |
| 43 | public: |
| 44 | virtual void writeSetCursorCallback() = 0; |
| 45 | }; |
| 46 | |
| 47 | class SMsgWriter { |
| 48 | public: |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 49 | SMsgWriter(ConnParams* cp, rdr::OutStream* os); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 50 | virtual ~SMsgWriter(); |
| 51 | |
| 52 | // writeServerInit() must only be called at the appropriate time in the |
| 53 | // protocol initialisation. |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 54 | void writeServerInit(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 55 | |
| 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 Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 61 | void writeSetColourMapEntries(int firstColour, int nColours, |
| 62 | ColourMap* cm); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 63 | |
| 64 | // writeBell() and writeServerCutText() do the obvious thing. |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 65 | void writeBell(); |
| 66 | void writeServerCutText(const char* str, int len); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 67 | |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 68 | // writeFence() sends a new fence request or response to the client. |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 69 | void writeFence(rdr::U32 flags, unsigned len, const char data[]); |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 70 | |
Pierre Ossman | c898d9a | 2011-11-14 16:22:23 +0000 | [diff] [blame] | 71 | // writeEndOfContinuousUpdates() indicates that we have left continuous |
| 72 | // updates mode. |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 73 | void writeEndOfContinuousUpdates(); |
Pierre Ossman | c898d9a | 2011-11-14 16:22:23 +0000 | [diff] [blame] | 74 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 75 | // 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 Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 83 | // writeSetDesktopSize() won't actually write immediately, but will |
| 84 | // write the relevant pseudo-rectangle as part of the next update. |
| 85 | bool writeSetDesktopSize(); |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 86 | // 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 Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 89 | bool writeExtendedDesktopSize(); |
| 90 | bool writeExtendedDesktopSize(rdr::U16 reason, rdr::U16 result, |
| 91 | int fb_width, int fb_height, |
| 92 | const ScreenSet& layout); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 93 | |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 94 | bool writeSetDesktopName(); |
Peter Åstrand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 95 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 96 | // Like setDesktopSize, we can't just write out a setCursor message |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 97 | // 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 Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 106 | |
| 107 | // needFakeUpdate() returns true when an immediate update is needed in |
Pierre Ossman | e9962f7 | 2009-04-23 12:31:42 +0000 | [diff] [blame] | 108 | // order to flush out pseudo-rectangles to the client. |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 109 | bool needFakeUpdate(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 110 | |
Pierre Ossman | e9962f7 | 2009-04-23 12:31:42 +0000 | [diff] [blame] | 111 | // 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 Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 115 | bool needNoDataUpdate(); |
Pierre Ossman | e9962f7 | 2009-04-23 12:31:42 +0000 | [diff] [blame] | 116 | |
| 117 | // writeNoDataUpdate() write a framebuffer update containing only |
| 118 | // pseudo-rectangles. |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 119 | void writeNoDataUpdate(); |
Pierre Ossman | e9962f7 | 2009-04-23 12:31:42 +0000 | [diff] [blame] | 120 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 121 | // 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 Ossman | 717c07b | 2014-01-21 14:45:10 +0100 | [diff] [blame^] | 125 | // the last one. |
| 126 | void writeRects(const UpdateInfo& update, TransImageGetter* ig); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 127 | |
| 128 | // To construct a framebuffer update you can call |
| 129 | // writeFramebufferUpdateStart(), followed by a number of writeCopyRect()s |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 130 | // and writeRect()s, finishing with writeFramebufferUpdateEnd(). |
| 131 | void writeFramebufferUpdateStart(int nRects); |
| 132 | void writeFramebufferUpdateEnd(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 133 | |
Pierre Ossman | 717c07b | 2014-01-21 14:45:10 +0100 | [diff] [blame^] | 134 | // 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 Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 138 | |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 139 | void writeCopyRect(const Rect& r, int srcX, int srcY); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 140 | |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 141 | void startRect(const Rect& r, int enc); |
| 142 | void endRect(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 143 | |
| 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]; } |
DRC | 887c5fd | 2011-08-19 03:13:47 +0000 | [diff] [blame] | 152 | rdr::U64 getRawBytesEquivalent() { return rawBytesEquivalent; } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 153 | |
| 154 | int imageBufIdealSize; |
| 155 | |
| 156 | protected: |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 157 | void startMsg(int type); |
| 158 | void endMsg(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 159 | |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 160 | void writePseudoRects(); |
| 161 | void writeNoDataRects(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 162 | |
Pierre Ossman | c0b1aa0 | 2014-01-16 13:39:05 +0100 | [diff] [blame] | 163 | 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 Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 169 | ConnParams* cp; |
| 170 | rdr::OutStream* os; |
| 171 | |
| 172 | Encoder* encoders[encodingMax+1]; |
Peter Åstrand | 98fe98c | 2010-02-10 07:43:02 +0000 | [diff] [blame] | 173 | int currentEncoding; |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 174 | |
| 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 Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 186 | int updatesSent; |
| 187 | int bytesSent[encodingMax+1]; |
| 188 | int rectsSent[encodingMax+1]; |
DRC | 887c5fd | 2011-08-19 03:13:47 +0000 | [diff] [blame] | 189 | rdr::U64 rawBytesEquivalent; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 190 | |
| 191 | rdr::U8* imageBuf; |
| 192 | int imageBufSize; |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 193 | |
| 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 Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 201 | }; |
| 202 | } |
| 203 | #endif |