You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CMsgWriter.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2009-2011 Pierre Ossman for Cendio AB
  3. * Copyright (C) 2011-2019 Brian P. Hinz
  4. *
  5. * This is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This software is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this software; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  18. * USA.
  19. */
  20. package com.tigervnc.rfb;
  21. import com.tigervnc.rdr.*;
  22. import java.nio.ByteBuffer;
  23. import java.nio.charset.Charset;
  24. import java.util.*;
  25. public class CMsgWriter {
  26. protected CMsgWriter(ServerParams server_, OutStream os_)
  27. {
  28. server = server_;
  29. os = os_;
  30. }
  31. synchronized public void writeClientInit(boolean shared) {
  32. os.writeU8(shared?1:0);
  33. endMsg();
  34. }
  35. synchronized public void writeSetPixelFormat(PixelFormat pf)
  36. {
  37. startMsg(MsgTypes.msgTypeSetPixelFormat);
  38. os.pad(3);
  39. pf.write(os);
  40. endMsg();
  41. }
  42. synchronized public void writeSetEncodings(List<Integer> encodings)
  43. {
  44. startMsg(MsgTypes.msgTypeSetEncodings);
  45. os.skip(1);
  46. os.writeU16(encodings.size());
  47. for (Iterator<Integer> i = encodings.iterator(); i.hasNext();)
  48. os.writeU32(i.next());
  49. endMsg();
  50. }
  51. synchronized public void writeSetDesktopSize(int width, int height,
  52. ScreenSet layout)
  53. {
  54. if (!server.supportsSetDesktopSize)
  55. throw new Exception("Server does not support SetDesktopSize");
  56. startMsg(MsgTypes.msgTypeSetDesktopSize);
  57. os.pad(1);
  58. os.writeU16(width);
  59. os.writeU16(height);
  60. os.writeU8(layout.num_screens());
  61. os.pad(1);
  62. for (Iterator<Screen> iter = layout.screens.iterator(); iter.hasNext(); ) {
  63. Screen refScreen = (Screen)iter.next();
  64. os.writeU32(refScreen.id);
  65. os.writeU16(refScreen.dimensions.tl.x);
  66. os.writeU16(refScreen.dimensions.tl.y);
  67. os.writeU16(refScreen.dimensions.width());
  68. os.writeU16(refScreen.dimensions.height());
  69. os.writeU32(refScreen.flags);
  70. }
  71. endMsg();
  72. }
  73. synchronized public void writeFramebufferUpdateRequest(Rect r, boolean incremental)
  74. {
  75. startMsg(MsgTypes.msgTypeFramebufferUpdateRequest);
  76. os.writeU8(incremental?1:0);
  77. os.writeU16(r.tl.x);
  78. os.writeU16(r.tl.y);
  79. os.writeU16(r.width());
  80. os.writeU16(r.height());
  81. endMsg();
  82. }
  83. synchronized public void writeEnableContinuousUpdates(boolean enable,
  84. int x, int y, int w, int h)
  85. {
  86. if (!server.supportsContinuousUpdates)
  87. throw new Exception("Server does not support continuous updates");
  88. startMsg(MsgTypes.msgTypeEnableContinuousUpdates);
  89. os.writeU8((enable?1:0));
  90. os.writeU16(x);
  91. os.writeU16(y);
  92. os.writeU16(w);
  93. os.writeU16(h);
  94. endMsg();
  95. }
  96. synchronized public void writeFence(int flags, int len, byte[] data)
  97. {
  98. if (!server.supportsFence)
  99. throw new Exception("Server does not support fences");
  100. if (len > 64)
  101. throw new Exception("Too large fence payload");
  102. if ((flags & ~fenceTypes.fenceFlagsSupported) != 0)
  103. throw new Exception("Unknown fence flags");
  104. startMsg(MsgTypes.msgTypeClientFence);
  105. os.pad(3);
  106. os.writeU32(flags);
  107. os.writeU8(len);
  108. os.writeBytes(data, 0, len);
  109. endMsg();
  110. }
  111. synchronized public void writeKeyEvent(int keysym, boolean down)
  112. {
  113. startMsg(MsgTypes.msgTypeKeyEvent);
  114. os.writeU8(down?1:0);
  115. os.pad(2);
  116. os.writeU32(keysym);
  117. endMsg();
  118. }
  119. synchronized public void writePointerEvent(Point pos, int buttonMask)
  120. {
  121. Point p = new Point(pos.x,pos.y);
  122. if (p.x < 0) p.x = 0;
  123. if (p.y < 0) p.y = 0;
  124. if (p.x >= server.width()) p.x = server.width() - 1;
  125. if (p.y >= server.height()) p.y = server.height() - 1;
  126. startMsg(MsgTypes.msgTypePointerEvent);
  127. os.writeU8(buttonMask);
  128. os.writeU16(p.x);
  129. os.writeU16(p.y);
  130. endMsg();
  131. }
  132. synchronized public void writeClientCutText(String str, int len)
  133. {
  134. startMsg(MsgTypes.msgTypeClientCutText);
  135. os.pad(3);
  136. os.writeU32(len);
  137. Charset latin1 = Charset.forName("ISO-8859-1");
  138. ByteBuffer bytes = latin1.encode(str);
  139. os.writeBytes(bytes.array(), 0, len);
  140. endMsg();
  141. }
  142. synchronized protected void startMsg(int type) {
  143. os.writeU8(type);
  144. }
  145. synchronized protected void endMsg() {
  146. os.flush();
  147. }
  148. protected ServerParams server;
  149. protected OutStream os;
  150. }