aboutsummaryrefslogtreecommitdiffstats
path: root/common/rfb/SMsgWriter.cxx
blob: 0c03b51dc18751530f6c471363752d69d52c92a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
/* Copyright (C) 2002-2005 RealVNC Ltd.  All Rights Reserved.
 * Copyright (C) 2011 D. R. Commander.  All Rights Reserved.
 * Copyright 2009-2019 Pierre Ossman for Cendio AB
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>

#include <rdr/OutStream.h>
#include <rdr/MemOutStream.h>
#include <rdr/ZlibOutStream.h>

#include <rfb/msgTypes.h>
#include <rfb/fenceTypes.h>
#include <rfb/clipboardTypes.h>
#include <rfb/Exception.h>
#include <rfb/ClientParams.h>
#include <rfb/UpdateTracker.h>
#include <rfb/Encoder.h>
#include <rfb/SMsgWriter.h>
#include <rfb/LogWriter.h>
#include <rfb/ledStates.h>
#include <rfb/util.h>

using namespace rfb;

static LogWriter vlog("SMsgWriter");

SMsgWriter::SMsgWriter(ClientParams* client_, rdr::OutStream* os_)
  : client(client_), os(os_),
    nRectsInUpdate(0), nRectsInHeader(0),
    needSetDesktopName(false), needCursor(false),
    needCursorPos(false), needLEDState(false),
    needQEMUKeyEvent(false)
{
}

SMsgWriter::~SMsgWriter()
{
}

void SMsgWriter::writeServerInit(uint16_t width, uint16_t height,
                                 const PixelFormat& pf, const char* name)
{
  os->writeU16(width);
  os->writeU16(height);
  pf.write(os);
  os->writeU32(strlen(name));
  os->writeBytes((const uint8_t*)name, strlen(name));
  endMsg();
}

void SMsgWriter::writeSetColourMapEntries(int firstColour, int nColours,
                                          const uint16_t red[],
                                          const uint16_t green[],
                                          const uint16_t blue[])
{
  startMsg(msgTypeSetColourMapEntries);
  os->pad(1);
  os->writeU16(firstColour);
  os->writeU16(nColours);
  for (int i = firstColour; i < firstColour+nColours; i++) {
    os->writeU16(red[i]);
    os->writeU16(green[i]);
    os->writeU16(blue[i]);
  }
  endMsg();
}

void SMsgWriter::writeBell()
{
  startMsg(msgTypeBell);
  endMsg();
}

void SMsgWriter::writeServerCutText(const char* str)
{
  if (strchr(str, '\r') != nullptr)
    throw Exception("Invalid carriage return in clipboard data");

  std::string latin1(utf8ToLatin1(str));

  startMsg(msgTypeServerCutText);
  os->pad(3);
  os->writeU32(latin1.size());
  os->writeBytes((const uint8_t*)latin1.data(), latin1.size());
  endMsg();
}

void SMsgWriter::writeClipboardCaps(uint32_t caps,
                                    const uint32_t* lengths)
{
  size_t i, count;

  if (!client->supportsEncoding(pseudoEncodingExtendedClipboard))
    throw Exception("Client does not support extended clipboard");

  count = 0;
  for (i = 0;i < 16;i++) {
    if (caps & (1 << i))
      count++;
  }

  startMsg(msgTypeServerCutText);
  os->pad(3);
  os->writeS32(-(4 + 4 * count));

  os->writeU32(caps | clipboardCaps);

  count = 0;
  for (i = 0;i < 16;i++) {
    if (caps & (1 << i))
      os->writeU32(lengths[count++]);
  }

  endMsg();
}

void SMsgWriter::writeClipboardRequest(uint32_t flags)
{
  if (!client->supportsEncoding(pseudoEncodingExtendedClipboard))
    throw Exception("Client does not support extended clipboard");
  if (!(client->clipboardFlags() & clipboardRequest))
    throw Exception("Client does not support clipboard \"request\" action");

  startMsg(msgTypeServerCutText);
  os->pad(3);
  os->writeS32(-4);
  os->writeU32(flags | clipboardRequest);
  endMsg();
}

void SMsgWriter::writeClipboardPeek(uint32_t flags)
{
  if (!client->supportsEncoding(pseudoEncodingExtendedClipboard))
    throw Exception("Client does not support extended clipboard");
  if (!(client->clipboardFlags() & clipboardPeek))
    throw Exception("Client does not support clipboard \"peek\" action");

  startMsg(msgTypeServerCutText);
  os->pad(3);
  os->writeS32(-4);
  os->writeU32(flags | clipboardPeek);
  endMsg();
}

void SMsgWriter::writeClipboardNotify(uint32_t flags)
{
  if (!client->supportsEncoding(pseudoEncodingExtendedClipboard))
    throw Exception("Client does not support extended clipboard");
  if (!(client->clipboardFlags() & clipboardNotify))
    throw Exception("Client does not support clipboard \"notify\" action");

  startMsg(msgTypeServerCutText);
  os->pad(3);
  os->writeS32(-4);
  os->writeU32(flags | clipboardNotify);
  endMsg();
}

void SMsgWriter::writeClipboardProvide(uint32_t flags,
                                      const size_t* lengths,
                                      const uint8_t* const* data)
{
  rdr::MemOutStream mos;
  rdr::ZlibOutStream zos;

  int i, count;

  if (!client->supportsEncoding(pseudoEncodingExtendedClipboard))
    throw Exception("Client does not support extended clipboard");
  if (!(client->clipboardFlags() & clipboardProvide))
    throw Exception("Client does not support clipboard \"provide\" action");

  zos.setUnderlying(&mos);

  count = 0;
  for (i = 0;i < 16;i++) {
    if (!(flags & (1 << i)))
      continue;
    zos.writeU32(lengths[count]);
    zos.writeBytes(data[count], lengths[count]);
    count++;
  }

  zos.flush();

  startMsg(msgTypeServerCutText);
  os->pad(3);
  os->writeS32(-(4 + mos.length()));
  os->writeU32(flags | clipboardProvide);
  os->writeBytes(mos.data(), mos.length());
  endMsg();
}

void SMsgWriter::writeFence(uint32_t flags, unsigned len,
                            const uint8_t data[])
{
  if (!client->supportsEncoding(pseudoEncodingFence))
    throw Exception("Client does not support fences");
  if (len > 64)
    throw Exception("Too large fence payload");
  if ((flags & ~fenceFlagsSupported) != 0)
    throw Exception("Unknown fence flags");

  startMsg(msgTypeServerFence);
  os->pad(3);

  os->writeU32(flags);

  os->writeU8(len);

  if (len > 0)
    os->writeBytes(data, len);

  endMsg();
}

void SMsgWriter::writeEndOfContinuousUpdates()
{
  if (!client->supportsEncoding(pseudoEncodingContinuousUpdates))
    throw Exception("Client does not support continuous updates");

  startMsg(msgTypeEndOfContinuousUpdates);
  endMsg();
}

void SMsgWriter::writeDesktopSize(uint16_t reason, uint16_t result)
{
  ExtendedDesktopSizeMsg msg;

  if (!client->supportsEncoding(pseudoEncodingDesktopSize) &&
      !client->supportsEncoding(pseudoEncodingExtendedDesktopSize))
    throw Exception("Client does not support desktop size changes");

  msg.reason = reason;
  msg.result = result;

  extendedDesktopSizeMsgs.push_back(msg);
}

void SMsgWriter::writeSetDesktopName()
{
  if (!client->supportsEncoding(pseudoEncodingDesktopName))
    throw Exception("Client does not support desktop name changes");

  needSetDesktopName = true;
}

void SMsgWriter::writeCursor()
{
  if (!client->supportsEncoding(pseudoEncodingCursor) &&
      !client->supportsEncoding(pseudoEncodingXCursor) &&
      !client->supportsEncoding(pseudoEncodingCursorWithAlpha) &&
      !client->supportsEncoding(pseudoEncodingVMwareCursor))
    throw Exception("Client does not support local cursor");

  needCursor = true;
}

void SMsgWriter::writeCursorPos()
{
  if (!client->supportsEncoding(pseudoEncodingVMwareCursorPosition))
    throw Exception("Client does not support cursor position");

  needCursorPos = true;
}

void SMsgWriter::writeLEDState()
{
  if (!client->supportsEncoding(pseudoEncodingLEDState) &&
      !client->supportsEncoding(pseudoEncodingVMwareLEDState))
    throw Exception("Client does not support LED state");
  if (client->ledState() == ledUnknown)
    throw Exception("Server has not specified LED state");

  needLEDState = true;
}

void SMsgWriter::writeQEMUKeyEvent()
{
  if (!client->supportsEncoding(pseudoEncodingQEMUKeyEvent))
    throw Exception("Client does not support QEMU key events");

  needQEMUKeyEvent = true;
}

bool SMsgWriter::needFakeUpdate()
{
  if (needSetDesktopName)
    return true;
  if (needCursor)
    return true;
  if (needCursorPos)
    return true;
  if (needLEDState)
    return true;
  if (needQEMUKeyEvent)
    return true;
  if (needNoDataUpdate())
    return true;

  return false;
}

bool SMsgWriter::needNoDataUpdate()
{
  if (!extendedDesktopSizeMsgs.empty())
    return true;

  return false;
}

void SMsgWriter::writeNoDataUpdate()
{
  int nRects;

  nRects = 0;

  if (!extendedDesktopSizeMsgs.empty()) {
    if (client->supportsEncoding(pseudoEncodingExtendedDesktopSize))
      nRects += extendedDesktopSizeMsgs.size();
    else
      nRects++;
  }

  writeFramebufferUpdateStart(nRects);
  writeNoDataRects();
  writeFramebufferUpdateEnd();
}

void SMsgWriter::writeFramebufferUpdateStart(int nRects)
{
  startMsg(msgTypeFramebufferUpdate);
  os->pad(1);

  if (nRects != 0xFFFF) {
    if (needSetDesktopName)
      nRects++;
    if (needCursor)
      nRects++;
    if (needCursorPos)
      nRects++;
    if (needLEDState)
      nRects++;
    if (needQEMUKeyEvent)
      nRects++;
  }

  os->writeU16(nRects);

  nRectsInUpdate = 0;
  if (nRects == 0xFFFF)
    nRectsInHeader = 0;
  else
    nRectsInHeader = nRects;

  writePseudoRects();
}

void SMsgWriter::writeFramebufferUpdateEnd()
{
  if (nRectsInUpdate != nRectsInHeader && nRectsInHeader)
    throw Exception("SMsgWriter::writeFramebufferUpdateEnd: "
                    "nRects out of sync");

  if (nRectsInHeader == 0) {
    // Send last rect. marker
    os->writeS16(0);
    os->writeS16(0);
    os->writeU16(0);
    os->writeU16(0);
    os->writeU32(pseudoEncodingLastRect);
  }

  endMsg();
}

void SMsgWriter::writeCopyRect(const Rect& r, int srcX, int srcY)
{
  startRect(r,encodingCopyRect);
  os->writeU16(srcX);
  os->writeU16(srcY);
  endRect();
}

void SMsgWriter::startRect(const Rect& r, int encoding)
{
  if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
    throw Exception("SMsgWriter::startRect: nRects out of sync");

  os->writeS16(r.tl.x);
  os->writeS16(r.tl.y);
  os->writeU16(r.width());
  os->writeU16(r.height());
  os->writeU32(encoding);
}

void SMsgWriter::endRect()
{
  os->flush();
}

void SMsgWriter::startMsg(int type)
{
  os->writeU8(type);
}

void SMsgWriter::endMsg()
{
  os->flush();
}

void SMsgWriter::writePseudoRects()
{
  if (needCursor) {
    const Cursor& cursor = client->cursor();

    if (client->supportsEncoding(pseudoEncodingCursorWithAlpha)) {
      writeSetCursorWithAlphaRect(cursor.width(), cursor.height(),
                                  cursor.hotspot().x, cursor.hotspot().y,
                                  cursor.getBuffer());
    } else if (client->supportsEncoding(pseudoEncodingVMwareCursor)) {
      writeSetVMwareCursorRect(cursor.width(), cursor.height(),
                               cursor.hotspot().x, cursor.hotspot().y,
                               cursor.getBuffer());
    } else if (client->supportsEncoding(pseudoEncodingCursor)) {
      size_t data_len = cursor.width()*cursor.height() *
                        (client->pf().bpp/8);
      std::vector<uint8_t> data(data_len);
      std::vector<uint8_t> mask(cursor.getMask());

      const uint8_t* in;
      uint8_t* out;

      in = cursor.getBuffer();
      out = data.data();
      for (int i = 0;i < cursor.width()*cursor.height();i++) {
        client->pf().bufferFromRGB(out, in, 1);
        in += 4;
        out += client->pf().bpp/8;
      }

      writeSetCursorRect(cursor.width(), cursor.height(),
                         cursor.hotspot().x, cursor.hotspot().y,
                         data.data(), mask.data());
    } else if (client->supportsEncoding(pseudoEncodingXCursor)) {
      std::vector<uint8_t> bitmap(cursor.getBitmap());
      std::vector<uint8_t> mask(cursor.getMask());

      writeSetXCursorRect(cursor.width(), cursor.height(),
                          cursor.hotspot().x, cursor.hotspot().y,
                          bitmap.data(), mask.data());
    } else {
      throw Exception("Client does not support local cursor");
    }

    needCursor = false;
  }

  if (needCursorPos) {
    const Point& cursorPos = client->cursorPos();

    if (client->supportsEncoding(pseudoEncodingVMwareCursorPosition)) {
      writeSetVMwareCursorPositionRect(cursorPos.x, cursorPos.y);
    } else {
      throw Exception("Client does not support cursor position");
    }

    needCursorPos = false;
  }

  if (needSetDesktopName) {
    writeSetDesktopNameRect(client->name());
    needSetDesktopName = false;
  }

  if (needLEDState) {
    writeLEDStateRect(client->ledState());
    needLEDState = false;
  }

  if (needQEMUKeyEvent) {
    writeQEMUKeyEventRect();
    needQEMUKeyEvent = false;
  }
}

void SMsgWriter::writeNoDataRects()
{
  if (!extendedDesktopSizeMsgs.empty()) {
    if (client->supportsEncoding(pseudoEncodingExtendedDesktopSize)) {
      for (ExtendedDesktopSizeMsg msg : extendedDesktopSizeMsgs) {
        // FIXME: We can probably skip multiple reasonServer entries
        writeExtendedDesktopSizeRect(msg.reason, msg.result,
                                     client->width(), client->height(),
                                     client->screenLayout());
      }
    } else if (client->supportsEncoding(pseudoEncodingDesktopSize)) {
      // Some clients assume this is the last rectangle so don't send anything
      // more after this
      writeSetDesktopSizeRect(client->width(), client->height());
    } else {
      throw Exception("Client does not support desktop size changes");
    }

    extendedDesktopSizeMsgs.clear();
  }
}

void SMsgWriter::writeSetDesktopSizeRect(int width, int height)
{
  if (!client->supportsEncoding(pseudoEncodingDesktopSize))
    throw Exception("Client does not support desktop resize");
  if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
    throw Exception("SMsgWriter::writeSetDesktopSizeRect: nRects out of sync");

  os->writeS16(0);
  os->writeS16(0);
  os->writeU16(width);
  os->writeU16(height);
  os->writeU32(pseudoEncodingDesktopSize);
}

void SMsgWriter::writeExtendedDesktopSizeRect(uint16_t reason,
                                              uint16_t result,
                                              int fb_width,
                                              int fb_height,
                                              const ScreenSet& layout)
{
  ScreenSet::const_iterator si;

  if (!client->supportsEncoding(pseudoEncodingExtendedDesktopSize))
    throw Exception("Client does not support extended desktop resize");
  if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
    throw Exception("SMsgWriter::writeExtendedDesktopSizeRect: nRects out of sync");

  os->writeU16(reason);
  os->writeU16(result);
  os->writeU16(fb_width);
  os->writeU16(fb_height);
  os->writeU32(pseudoEncodingExtendedDesktopSize);

  os->writeU8(layout.num_screens());
  os->pad(3);

  for (si = layout.begin();si != layout.end();++si) {
    os->writeU32(si->id);
    os->writeU16(si->dimensions.tl.x);
    os->writeU16(si->dimensions.tl.y);
    os->writeU16(si->dimensions.width());
    os->writeU16(si->dimensions.height());
    os->writeU32(si->flags);
  }
}

void SMsgWriter::writeSetDesktopNameRect(const char *name)
{
  if (!client->supportsEncoding(pseudoEncodingDesktopName))
    throw Exception("Client does not support desktop rename");
  if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
    throw Exception("SMsgWriter::writeSetDesktopNameRect: nRects out of sync");

  os->writeS16(0);
  os->writeS16(0);
  os->writeU16(0);
  os->writeU16(0);
  os->writeU32(pseudoEncodingDesktopName);
  os->writeU32(strlen(name));
  os->writeBytes((const uint8_t*)name, strlen(name));
}

void SMsgWriter::writeSetCursorRect(int width, int height,
                                    int hotspotX, int hotspotY,
                                    const uint8_t* data,
                                    const uint8_t* mask)
{
  if (!client->supportsEncoding(pseudoEncodingCursor))
    throw Exception("Client does not support local cursors");
  if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
    throw Exception("SMsgWriter::writeSetCursorRect: nRects out of sync");

  os->writeS16(hotspotX);
  os->writeS16(hotspotY);
  os->writeU16(width);
  os->writeU16(height);
  os->writeU32(pseudoEncodingCursor);
  os->writeBytes(data, width * height * (client->pf().bpp/8));
  os->writeBytes(mask, (width+7)/8 * height);
}

void SMsgWriter::writeSetXCursorRect(int width, int height,
                                     int hotspotX, int hotspotY,
                                     const uint8_t* data,
                                     const uint8_t* mask)
{
  if (!client->supportsEncoding(pseudoEncodingXCursor))
    throw Exception("Client does not support local cursors");
  if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
    throw Exception("SMsgWriter::writeSetXCursorRect: nRects out of sync");

  os->writeS16(hotspotX);
  os->writeS16(hotspotY);
  os->writeU16(width);
  os->writeU16(height);
  os->writeU32(pseudoEncodingXCursor);
  if (width * height > 0) {
    os->writeU8(255);
    os->writeU8(255);
    os->writeU8(255);
    os->writeU8(0);
    os->writeU8(0);
    os->writeU8(0);
    os->writeBytes(data, (width+7)/8 * height);
    os->writeBytes(mask, (width+7)/8 * height);
  }
}

void SMsgWriter::writeSetCursorWithAlphaRect(int width, int height,
                                             int hotspotX, int hotspotY,
                                             const uint8_t* data)
{
  if (!client->supportsEncoding(pseudoEncodingCursorWithAlpha))
    throw Exception("Client does not support local cursors");
  if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
    throw Exception("SMsgWriter::writeSetCursorWithAlphaRect: nRects out of sync");

  os->writeS16(hotspotX);
  os->writeS16(hotspotY);
  os->writeU16(width);
  os->writeU16(height);
  os->writeU32(pseudoEncodingCursorWithAlpha);

  // FIXME: Use an encoder with compression?
  os->writeU32(encodingRaw);

  // Alpha needs to be pre-multiplied
  for (int i = 0;i < width*height;i++) {
    os->writeU8((unsigned)data[0] * data[3] / 255);
    os->writeU8((unsigned)data[1] * data[3] / 255);
    os->writeU8((unsigned)data[2] * data[3] / 255);
    os->writeU8(data[3]);
    data += 4;
  }
}

void SMsgWriter::writeSetVMwareCursorRect(int width, int height,
                                          int hotspotX, int hotspotY,
                                          const uint8_t* data)
{
  if (!client->supportsEncoding(pseudoEncodingVMwareCursor))
    throw Exception("Client does not support local cursors");
  if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
    throw Exception("SMsgWriter::writeSetVMwareCursorRect: nRects out of sync");

  os->writeS16(hotspotX);
  os->writeS16(hotspotY);
  os->writeU16(width);
  os->writeU16(height);
  os->writeU32(pseudoEncodingVMwareCursor);

  os->writeU8(1); // Alpha cursor
  os->pad(1);

  // FIXME: Should alpha be premultiplied?
  os->writeBytes(data, width*height*4);
}

void SMsgWriter::writeSetVMwareCursorPositionRect(int hotspotX, int hotspotY)
{
  if (!client->supportsEncoding(pseudoEncodingVMwareCursorPosition))
    throw Exception("Client does not support cursor position");
  if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
    throw Exception("SMsgWriter::writeSetVMwareCursorRect: nRects out of sync");

  os->writeS16(hotspotX);
  os->writeS16(hotspotY);
  os->writeU16(0);
  os->writeU16(0);
  os->writeU32(pseudoEncodingVMwareCursorPosition);
}

void SMsgWriter::writeLEDStateRect(uint8_t state)
{
  if (!client->supportsEncoding(pseudoEncodingLEDState) &&
      !client->supportsEncoding(pseudoEncodingVMwareLEDState))
    throw Exception("Client does not support LED state updates");
  if (client->ledState() == ledUnknown)
    throw Exception("Server does not support LED state updates");
  if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
    throw Exception("SMsgWriter::writeLEDStateRect: nRects out of sync");

  os->writeS16(0);
  os->writeS16(0);
  os->writeU16(0);
  os->writeU16(0);
  if (client->supportsEncoding(pseudoEncodingLEDState)) {
    os->writeU32(pseudoEncodingLEDState);
    os->writeU8(state);
  } else {
    os->writeU32(pseudoEncodingVMwareLEDState);
    os->writeU32(state);
  }
}

void SMsgWriter::writeQEMUKeyEventRect()
{
  if (!client->supportsEncoding(pseudoEncodingQEMUKeyEvent))
    throw Exception("Client does not support QEMU extended key events");
  if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
    throw Exception("SMsgWriter::writeQEMUKeyEventRect: nRects out of sync");

  os->writeS16(0);
  os->writeS16(0);
  os->writeU16(0);
  os->writeU16(0);
  os->writeU32(pseudoEncodingQEMUKeyEvent);
}