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 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2009-2011 Pierre Ossman for Cendio AB
  3. * Copyright (C) 2011 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.Iterator;
  25. public class CMsgWriter {
  26. protected CMsgWriter(ConnParams cp_, OutStream os_)
  27. {
  28. cp = cp_;
  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(int nEncodings, int[] encodings)
  43. {
  44. startMsg(MsgTypes.msgTypeSetEncodings);
  45. os.skip(1);
  46. os.writeU16(nEncodings);
  47. for (int i = 0; i < nEncodings; i++)
  48. os.writeU32(encodings[i]);
  49. endMsg();
  50. }
  51. // Ask for encodings based on which decoders are supported. Assumes higher
  52. // encoding numbers are more desirable.
  53. synchronized public void writeSetEncodings(int preferredEncoding, boolean useCopyRect)
  54. {
  55. int nEncodings = 0;
  56. int[] encodings = new int[Encodings.encodingMax+3];
  57. if (cp.supportsLocalCursor) {
  58. if (cp.supportsLocalCursorWithAlpha)
  59. encodings[nEncodings++] = Encodings.pseudoEncodingCursorWithAlpha;
  60. encodings[nEncodings++] = Encodings.pseudoEncodingCursor;
  61. encodings[nEncodings++] = Encodings.pseudoEncodingXCursor;
  62. }
  63. if (cp.supportsDesktopResize)
  64. encodings[nEncodings++] = Encodings.pseudoEncodingDesktopSize;
  65. if (cp.supportsExtendedDesktopSize)
  66. encodings[nEncodings++] = Encodings.pseudoEncodingExtendedDesktopSize;
  67. if (cp.supportsDesktopRename)
  68. encodings[nEncodings++] = Encodings.pseudoEncodingDesktopName;
  69. if (cp.supportsClientRedirect)
  70. encodings[nEncodings++] = Encodings.pseudoEncodingClientRedirect;
  71. encodings[nEncodings++] = Encodings.pseudoEncodingLastRect;
  72. encodings[nEncodings++] = Encodings.pseudoEncodingContinuousUpdates;
  73. encodings[nEncodings++] = Encodings.pseudoEncodingFence;
  74. if (Decoder.supported(preferredEncoding)) {
  75. encodings[nEncodings++] = preferredEncoding;
  76. }
  77. if (useCopyRect) {
  78. encodings[nEncodings++] = Encodings.encodingCopyRect;
  79. }
  80. /*
  81. * Prefer encodings in this order:
  82. *
  83. * Tight, ZRLE, Hextile, *
  84. */
  85. if ((preferredEncoding != Encodings.encodingTight) &&
  86. Decoder.supported(Encodings.encodingTight))
  87. encodings[nEncodings++] = Encodings.encodingTight;
  88. if ((preferredEncoding != Encodings.encodingZRLE) &&
  89. Decoder.supported(Encodings.encodingZRLE))
  90. encodings[nEncodings++] = Encodings.encodingZRLE;
  91. if ((preferredEncoding != Encodings.encodingHextile) &&
  92. Decoder.supported(Encodings.encodingHextile))
  93. encodings[nEncodings++] = Encodings.encodingHextile;
  94. // Remaining encodings
  95. for (int i = Encodings.encodingMax; i >= 0; i--) {
  96. switch (i) {
  97. case Encodings.encodingCopyRect:
  98. case Encodings.encodingTight:
  99. case Encodings.encodingZRLE:
  100. case Encodings.encodingHextile:
  101. /* These have already been sent earlier */
  102. break;
  103. default:
  104. if ((i != preferredEncoding) && Decoder.supported(i))
  105. encodings[nEncodings++] = i;
  106. }
  107. }
  108. if (cp.compressLevel >= 0 && cp.compressLevel <= 9)
  109. encodings[nEncodings++] =
  110. Encodings.pseudoEncodingCompressLevel0 + cp.compressLevel;
  111. if (cp.qualityLevel >= 0 && cp.qualityLevel <= 9)
  112. encodings[nEncodings++] =
  113. Encodings.pseudoEncodingQualityLevel0 + cp.qualityLevel;
  114. writeSetEncodings(nEncodings, encodings);
  115. }
  116. synchronized public void writeSetDesktopSize(int width, int height,
  117. ScreenSet layout)
  118. {
  119. if (!cp.supportsSetDesktopSize)
  120. throw new Exception("Server does not support SetDesktopSize");
  121. startMsg(MsgTypes.msgTypeSetDesktopSize);
  122. os.pad(1);
  123. os.writeU16(width);
  124. os.writeU16(height);
  125. os.writeU8(layout.num_screens());
  126. os.pad(1);
  127. for (Iterator<Screen> iter = layout.screens.iterator(); iter.hasNext(); ) {
  128. Screen refScreen = (Screen)iter.next();
  129. os.writeU32(refScreen.id);
  130. os.writeU16(refScreen.dimensions.tl.x);
  131. os.writeU16(refScreen.dimensions.tl.y);
  132. os.writeU16(refScreen.dimensions.width());
  133. os.writeU16(refScreen.dimensions.height());
  134. os.writeU32(refScreen.flags);
  135. }
  136. endMsg();
  137. }
  138. synchronized public void writeFramebufferUpdateRequest(Rect r, boolean incremental)
  139. {
  140. startMsg(MsgTypes.msgTypeFramebufferUpdateRequest);
  141. os.writeU8(incremental?1:0);
  142. os.writeU16(r.tl.x);
  143. os.writeU16(r.tl.y);
  144. os.writeU16(r.width());
  145. os.writeU16(r.height());
  146. endMsg();
  147. }
  148. synchronized public void writeEnableContinuousUpdates(boolean enable,
  149. int x, int y, int w, int h)
  150. {
  151. if (!cp.supportsContinuousUpdates)
  152. throw new Exception("Server does not support continuous updates");
  153. startMsg(MsgTypes.msgTypeEnableContinuousUpdates);
  154. os.writeU8((enable?1:0));
  155. os.writeU16(x);
  156. os.writeU16(y);
  157. os.writeU16(w);
  158. os.writeU16(h);
  159. endMsg();
  160. }
  161. synchronized public void writeFence(int flags, int len, byte[] data)
  162. {
  163. if (!cp.supportsFence)
  164. throw new Exception("Server does not support fences");
  165. if (len > 64)
  166. throw new Exception("Too large fence payload");
  167. if ((flags & ~fenceTypes.fenceFlagsSupported) != 0)
  168. throw new Exception("Unknown fence flags");
  169. startMsg(MsgTypes.msgTypeClientFence);
  170. os.pad(3);
  171. os.writeU32(flags);
  172. os.writeU8(len);
  173. os.writeBytes(data, 0, len);
  174. endMsg();
  175. }
  176. synchronized public void keyEvent(int keysym, boolean down)
  177. {
  178. startMsg(MsgTypes.msgTypeKeyEvent);
  179. os.writeU8(down?1:0);
  180. os.pad(2);
  181. os.writeU32(keysym);
  182. endMsg();
  183. }
  184. synchronized public void pointerEvent(Point pos, int buttonMask)
  185. {
  186. Point p = new Point(pos.x,pos.y);
  187. if (p.x < 0) p.x = 0;
  188. if (p.y < 0) p.y = 0;
  189. if (p.x >= cp.width) p.x = cp.width - 1;
  190. if (p.y >= cp.height) p.y = cp.height - 1;
  191. startMsg(MsgTypes.msgTypePointerEvent);
  192. os.writeU8(buttonMask);
  193. os.writeU16(p.x);
  194. os.writeU16(p.y);
  195. endMsg();
  196. }
  197. synchronized public void writeClientCutText(String str, int len)
  198. {
  199. startMsg(MsgTypes.msgTypeClientCutText);
  200. os.pad(3);
  201. os.writeU32(len);
  202. Charset latin1 = Charset.forName("ISO-8859-1");
  203. ByteBuffer bytes = latin1.encode(str);
  204. os.writeBytes(bytes.array(), 0, len);
  205. endMsg();
  206. }
  207. synchronized public void startMsg(int type) {
  208. os.writeU8(type);
  209. }
  210. synchronized public void endMsg() {
  211. os.flush();
  212. }
  213. ConnParams cp;
  214. OutStream os;
  215. }