blob: 4cd2e935a09e494f86af2721cec08dba8a593674 [file] [log] [blame]
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +00001//
2// Copyright (C) 2004 Horizon Wimba. All Rights Reserved.
3// Copyright (C) 2001-2003 HorizonLive.com, Inc. All Rights Reserved.
4// Copyright (C) 2001,2002 Constantin Kaplinsky. All Rights Reserved.
5// Copyright (C) 2000 Tridia Corporation. All Rights Reserved.
6// Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
7//
8// This is free software; you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation; either version 2 of the License, or
11// (at your option) any later version.
12//
13// This software is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17//
18// You should have received a copy of the GNU General Public License
19// along with this software; if not, write to the Free Software
20// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21// USA.
22//
23
Constantin Kaplinsky90d8a502008-04-14 09:45:50 +000024package com.tightvnc.vncviewer;
25
enikeyc41ba1d2008-12-19 09:07:22 +000026import com.tightvnc.decoder.CoRREDecoder;
27import com.tightvnc.decoder.HextileDecoder;
28import com.tightvnc.decoder.RREDecoder;
29import com.tightvnc.decoder.RawDecoder;
30import com.tightvnc.decoder.TightDecoder;
31import com.tightvnc.decoder.ZRLEDecoder;
32import com.tightvnc.decoder.ZlibDecoder;
enikey0dbc1532008-12-19 08:51:47 +000033import com.tightvnc.decoder.common.Repaintable;
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +000034import java.awt.*;
35import java.awt.event.*;
36import java.awt.image.*;
37import java.io.*;
38import java.lang.*;
39import java.util.zip.*;
40
41
42//
43// VncCanvas is a subclass of Canvas which draws a VNC desktop on it.
44//
45
46class VncCanvas extends Canvas
enikey0dbc1532008-12-19 08:51:47 +000047 implements KeyListener, MouseListener, MouseMotionListener, RecordInterface,
48 Repaintable {
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +000049
50 VncViewer viewer;
51 RfbProto rfb;
52 ColorModel cm8, cm24;
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +000053 int bytesPixel;
54
55 int maxWidth = 0, maxHeight = 0;
56 int scalingFactor;
57 int scaledWidth, scaledHeight;
58
59 Image memImage;
60 Graphics memGraphics;
61
enikeyc41ba1d2008-12-19 09:07:22 +000062 //
63 // Decoders
64 //
65
66 RawDecoder rawDecoder;
67 RREDecoder rreDecoder;
68 CoRREDecoder correDecoder;
69 ZlibDecoder zlibDecoder;
70 HextileDecoder hextileDecoder;
71 ZRLEDecoder zrleDecoder;
72 TightDecoder tightDecoder;
73
74 // Base decoder decoders array
75 RawDecoder []decoders = null;
76
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +000077 // Update statistics.
78 long statStartTime; // time on first framebufferUpdateRequest
79 int statNumUpdates; // counter for FramebufferUpdate messages
80 int statNumTotalRects; // rectangles in FramebufferUpdate messages
81 int statNumPixelRects; // the same, but excluding pseudo-rectangles
82 int statNumRectsTight; // Tight-encoded rectangles (including JPEG)
83 int statNumRectsTightJPEG; // JPEG-compressed Tight-encoded rectangles
84 int statNumRectsZRLE; // ZRLE-encoded rectangles
85 int statNumRectsHextile; // Hextile-encoded rectangles
86 int statNumRectsRaw; // Raw-encoded rectangles
87 int statNumRectsCopy; // CopyRect rectangles
88 int statNumBytesEncoded; // number of bytes in updates, as received
89 int statNumBytesDecoded; // number of bytes, as if Raw encoding was used
90
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +000091 // True if we process keyboard and mouse events.
92 boolean inputEnabled;
93
enikeyc92c1d12008-12-19 09:58:31 +000094 // True if was no one auto resize of canvas
95 boolean isFirstSizeAutoUpdate = true;
96
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +000097 //
98 // The constructors.
99 //
100
101 public VncCanvas(VncViewer v, int maxWidth_, int maxHeight_)
102 throws IOException {
103
104 viewer = v;
105 maxWidth = maxWidth_;
106 maxHeight = maxHeight_;
107
108 rfb = viewer.rfb;
109 scalingFactor = viewer.options.scalingFactor;
110
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000111 cm8 = new DirectColorModel(8, 7, (7 << 3), (3 << 6));
112 cm24 = new DirectColorModel(24, 0xFF0000, 0x00FF00, 0x0000FF);
113
enikeyc41ba1d2008-12-19 09:07:22 +0000114 //
115 // Create decoders
116 //
117
118 // Input stream for decoders
119 RfbInputStream rfbis = new RfbInputStream(rfb);
enikey1622a3c2008-12-24 03:58:29 +0000120 // Create output stream for session recording
121 RecordOutputStream ros = new RecordOutputStream(this);
enikeyc41ba1d2008-12-19 09:07:22 +0000122
123 rawDecoder = new RawDecoder(memGraphics, rfbis);
124 rreDecoder = new RREDecoder(memGraphics, rfbis);
125 correDecoder = new CoRREDecoder(memGraphics, rfbis);
126 hextileDecoder = new HextileDecoder(memGraphics, rfbis);
127 tightDecoder = new TightDecoder(memGraphics, rfbis);
enikey4582bab2008-12-19 09:19:59 +0000128 zlibDecoder = new ZlibDecoder(memGraphics, rfbis);
enikeyc41ba1d2008-12-19 09:07:22 +0000129 zrleDecoder = new ZRLEDecoder(memGraphics, rfbis);
130
131 //
132 // Set data for decoders that needs extra parameters
133 //
134
135 hextileDecoder.setRepainableControl(this);
136 tightDecoder.setRepainableControl(this);
137
138 //
139 // Create array that contains our decoders
140 //
141
142 decoders = new RawDecoder[7];
143 decoders[0] = rawDecoder;
144 decoders[1] = rreDecoder;
145 decoders[2] = correDecoder;
146 decoders[3] = hextileDecoder;
147 decoders[4] = zlibDecoder;
148 decoders[5] = tightDecoder;
149 decoders[6] = zrleDecoder;
150
151 //
152 // Set session recorder for decoders
153 //
154
155 for (int i = 0; i < decoders.length; i++) {
156 decoders[i].setSessionRecorder(this);
enikey1622a3c2008-12-24 03:58:29 +0000157 decoders[i].setDataOutputStream(ros);
enikeyc41ba1d2008-12-19 09:07:22 +0000158 }
enikeyc92c1d12008-12-19 09:58:31 +0000159
enikey2f7f46e2008-12-19 09:32:35 +0000160 setPixelFormat();
161
162 resetSelection();
163
164 inputEnabled = false;
165 if (!viewer.options.viewOnly)
166 enableInput(true);
enikeyc41ba1d2008-12-19 09:07:22 +0000167
Constantin Kaplinskyf7cb2bf2008-05-27 08:38:28 +0000168 // Enable mouse and keyboard event listeners.
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000169 addKeyListener(this);
Constantin Kaplinskyf7cb2bf2008-05-27 08:38:28 +0000170 addMouseListener(this);
171 addMouseMotionListener(this);
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000172 }
173
174 public VncCanvas(VncViewer v) throws IOException {
175 this(v, 0, 0);
176 }
177
178 //
179 // Callback methods to determine geometry of our Component.
180 //
181
182 public Dimension getPreferredSize() {
183 return new Dimension(scaledWidth, scaledHeight);
184 }
185
186 public Dimension getMinimumSize() {
187 return new Dimension(scaledWidth, scaledHeight);
188 }
189
190 public Dimension getMaximumSize() {
191 return new Dimension(scaledWidth, scaledHeight);
192 }
193
194 //
195 // All painting is performed here.
196 //
197
198 public void update(Graphics g) {
199 paint(g);
200 }
201
202 public void paint(Graphics g) {
203 synchronized(memImage) {
204 if (rfb.framebufferWidth == scaledWidth) {
205 g.drawImage(memImage, 0, 0, null);
206 } else {
207 paintScaledFrameBuffer(g);
208 }
209 }
210 if (showSoftCursor) {
211 int x0 = cursorX - hotX, y0 = cursorY - hotY;
212 Rectangle r = new Rectangle(x0, y0, cursorWidth, cursorHeight);
213 if (r.intersects(g.getClipBounds())) {
214 g.drawImage(softCursor, x0, y0, null);
215 }
216 }
Constantin Kaplinskyf7cb2bf2008-05-27 08:38:28 +0000217 if (isInSelectionMode()) {
218 Rectangle r = getSelection(true);
219 if (r.width > 0 && r.height > 0) {
220 // Don't forget to correct the coordinates for the right and bottom
221 // borders, so that the borders are the part of the selection.
222 r.width -= 1;
223 r.height -= 1;
224 g.setXORMode(Color.yellow);
225 g.drawRect(r.x, r.y, r.width, r.height);
226 }
227 }
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000228 }
229
230 public void paintScaledFrameBuffer(Graphics g) {
231 g.drawImage(memImage, 0, 0, scaledWidth, scaledHeight, null);
232 }
233
234 //
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000235 // Start/stop receiving mouse events. Keyboard events are received
236 // even in view-only mode, because we want to map the 'r' key to the
237 // screen refreshing function.
238 //
239
240 public synchronized void enableInput(boolean enable) {
241 if (enable && !inputEnabled) {
242 inputEnabled = true;
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000243 if (viewer.showControls) {
244 viewer.buttonPanel.enableRemoteAccessControls(true);
245 }
246 createSoftCursor(); // scaled cursor
247 } else if (!enable && inputEnabled) {
248 inputEnabled = false;
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000249 if (viewer.showControls) {
250 viewer.buttonPanel.enableRemoteAccessControls(false);
251 }
252 createSoftCursor(); // non-scaled cursor
253 }
254 }
255
256 public void setPixelFormat() throws IOException {
257 if (viewer.options.eightBitColors) {
258 rfb.writeSetPixelFormat(8, 8, false, true, 7, 7, 3, 0, 3, 6);
259 bytesPixel = 1;
260 } else {
261 rfb.writeSetPixelFormat(32, 24, false, true, 255, 255, 255, 16, 8, 0);
262 bytesPixel = 4;
263 }
264 updateFramebufferSize();
265 }
266
enikey45cfaa52008-12-03 06:52:09 +0000267 void setScalingFactor(int sf) {
268 scalingFactor = sf;
269 updateFramebufferSize();
270 invalidate();
271 }
272
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000273 void updateFramebufferSize() {
274
275 // Useful shortcuts.
276 int fbWidth = rfb.framebufferWidth;
277 int fbHeight = rfb.framebufferHeight;
278
enikey87647e92008-12-04 08:42:34 +0000279 // FIXME: This part of code must be in VncViewer i think
enikey73683202008-12-03 09:17:25 +0000280 if (viewer.options.autoScale) {
enikey87647e92008-12-04 08:42:34 +0000281 if (viewer.inAnApplet) {
282 maxWidth = viewer.getWidth();
283 maxHeight = viewer.getHeight();
284 } else {
285 if (viewer.vncFrame != null) {
286 if (isFirstSizeAutoUpdate) {
287 isFirstSizeAutoUpdate = false;
288 Dimension screenSize = viewer.vncFrame.getToolkit().getScreenSize();
289 maxWidth = (int)screenSize.getWidth() - 100;
enikeyc41ba1d2008-12-19 09:07:22 +0000290 maxHeight = (int)screenSize.getHeight() - 100;
enikey87647e92008-12-04 08:42:34 +0000291 viewer.vncFrame.setSize(maxWidth, maxHeight);
292 } else {
293 viewer.desktopScrollPane.doLayout();
294 maxWidth = viewer.desktopScrollPane.getWidth();
295 maxHeight = viewer.desktopScrollPane.getHeight();
296 }
297 } else {
298 maxWidth = fbWidth;
299 maxHeight = fbHeight;
300 }
enikey73683202008-12-03 09:17:25 +0000301 }
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000302 int f1 = maxWidth * 100 / fbWidth;
303 int f2 = maxHeight * 100 / fbHeight;
304 scalingFactor = Math.min(f1, f2);
305 if (scalingFactor > 100)
306 scalingFactor = 100;
307 System.out.println("Scaling desktop at " + scalingFactor + "%");
308 }
309
310 // Update scaled framebuffer geometry.
311 scaledWidth = (fbWidth * scalingFactor + 50) / 100;
312 scaledHeight = (fbHeight * scalingFactor + 50) / 100;
313
314 // Create new off-screen image either if it does not exist, or if
315 // its geometry should be changed. It's not necessary to replace
316 // existing image if only pixel format should be changed.
317 if (memImage == null) {
318 memImage = viewer.vncContainer.createImage(fbWidth, fbHeight);
319 memGraphics = memImage.getGraphics();
320 } else if (memImage.getWidth(null) != fbWidth ||
321 memImage.getHeight(null) != fbHeight) {
322 synchronized(memImage) {
323 memImage = viewer.vncContainer.createImage(fbWidth, fbHeight);
324 memGraphics = memImage.getGraphics();
325 }
326 }
327
enikey4582bab2008-12-19 09:19:59 +0000328 //
329 // Update decoders
330 //
331
332 //
333 // FIXME: Why decoders can be null here?
334 //
335
336 if (decoders != null) {
337 for (int i = 0; i < decoders.length; i++) {
338 //
339 // Set changes to every decoder that we can use
340 //
341
342 decoders[i].setBPP(bytesPixel);
343 decoders[i].setFrameBufferSize(fbWidth, fbHeight);
344 decoders[i].setGraphics(memGraphics);
345
346 //
347 // Update decoder
348 //
349
350 decoders[i].update();
351 }
352 }
353
enikey87647e92008-12-04 08:42:34 +0000354 // FIXME: This part of code must be in VncViewer i think
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000355 // Update the size of desktop containers.
356 if (viewer.inSeparateFrame) {
enikey87647e92008-12-04 08:42:34 +0000357 if (viewer.desktopScrollPane != null) {
358 if (!viewer.options.autoScale) {
359 resizeDesktopFrame();
360 } else {
361 setSize(scaledWidth, scaledHeight);
362 viewer.desktopScrollPane.setSize(maxWidth + 200,
363 maxHeight + 200);
364 }
365 }
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000366 } else {
367 setSize(scaledWidth, scaledHeight);
368 }
369 viewer.moveFocusToDesktop();
370 }
371
372 void resizeDesktopFrame() {
373 setSize(scaledWidth, scaledHeight);
374
375 // FIXME: Find a better way to determine correct size of a
376 // ScrollPane. -- const
377 Insets insets = viewer.desktopScrollPane.getInsets();
378 viewer.desktopScrollPane.setSize(scaledWidth +
379 2 * Math.min(insets.left, insets.right),
380 scaledHeight +
381 2 * Math.min(insets.top, insets.bottom));
382
383 viewer.vncFrame.pack();
384
385 // Try to limit the frame size to the screen size.
386
387 Dimension screenSize = viewer.vncFrame.getToolkit().getScreenSize();
388 Dimension frameSize = viewer.vncFrame.getSize();
389 Dimension newSize = frameSize;
390
391 // Reduce Screen Size by 30 pixels in each direction;
392 // This is a (poor) attempt to account for
393 // 1) Menu bar on Macintosh (should really also account for
394 // Dock on OSX). Usually 22px on top of screen.
395 // 2) Taxkbar on Windows (usually about 28 px on bottom)
396 // 3) Other obstructions.
397
398 screenSize.height -= 30;
399 screenSize.width -= 30;
400
401 boolean needToResizeFrame = false;
402 if (frameSize.height > screenSize.height) {
403 newSize.height = screenSize.height;
404 needToResizeFrame = true;
405 }
406 if (frameSize.width > screenSize.width) {
407 newSize.width = screenSize.width;
408 needToResizeFrame = true;
409 }
410 if (needToResizeFrame) {
411 viewer.vncFrame.setSize(newSize);
412 }
413
414 viewer.desktopScrollPane.doLayout();
415 }
416
417 //
418 // processNormalProtocol() - executed by the rfbThread to deal with the
419 // RFB socket.
420 //
421
422 public void processNormalProtocol() throws Exception {
423
424 // Start/stop session recording if necessary.
425 viewer.checkRecordingStatus();
426
427 rfb.writeFramebufferUpdateRequest(0, 0, rfb.framebufferWidth,
428 rfb.framebufferHeight, false);
429
430 if (viewer.options.continuousUpdates) {
431 rfb.tryEnableContinuousUpdates(0, 0, rfb.framebufferWidth,
432 rfb.framebufferHeight);
433 }
434
435 resetStats();
436 boolean statsRestarted = false;
437
438 //
439 // main dispatch loop
440 //
441
442 while (true) {
443
444 // Read message type from the server.
445 int msgType = rfb.readServerMessageType();
446
447 // Process the message depending on its type.
448 switch (msgType) {
449 case RfbProto.FramebufferUpdate:
450
451 if (statNumUpdates == viewer.debugStatsExcludeUpdates &&
452 !statsRestarted) {
453 resetStats();
454 statsRestarted = true;
455 } else if (statNumUpdates == viewer.debugStatsMeasureUpdates &&
456 statsRestarted) {
457 viewer.disconnect();
458 }
459
460 rfb.readFramebufferUpdate();
461 statNumUpdates++;
462
463 boolean cursorPosReceived = false;
464
465 for (int i = 0; i < rfb.updateNRects; i++) {
466
467 rfb.readFramebufferUpdateRectHdr();
468 statNumTotalRects++;
469 int rx = rfb.updateRectX, ry = rfb.updateRectY;
470 int rw = rfb.updateRectW, rh = rfb.updateRectH;
471
472 if (rfb.updateRectEncoding == rfb.EncodingLastRect)
473 break;
474
475 if (rfb.updateRectEncoding == rfb.EncodingNewFBSize) {
476 rfb.setFramebufferSize(rw, rh);
477 updateFramebufferSize();
478 break;
479 }
480
481 if (rfb.updateRectEncoding == rfb.EncodingXCursor ||
482 rfb.updateRectEncoding == rfb.EncodingRichCursor) {
483 handleCursorShapeUpdate(rfb.updateRectEncoding, rx, ry, rw, rh);
484 continue;
485 }
486
487 if (rfb.updateRectEncoding == rfb.EncodingPointerPos) {
488 softCursorMove(rx, ry);
489 cursorPosReceived = true;
490 continue;
491 }
492
493 long numBytesReadBefore = rfb.getNumBytesRead();
494
495 rfb.startTiming();
496
497 switch (rfb.updateRectEncoding) {
498 case RfbProto.EncodingRaw:
499 statNumRectsRaw++;
500 handleRawRect(rx, ry, rw, rh);
501 break;
502 case RfbProto.EncodingCopyRect:
503 statNumRectsCopy++;
504 handleCopyRect(rx, ry, rw, rh);
505 break;
506 case RfbProto.EncodingRRE:
507 handleRRERect(rx, ry, rw, rh);
508 break;
509 case RfbProto.EncodingCoRRE:
510 handleCoRRERect(rx, ry, rw, rh);
511 break;
512 case RfbProto.EncodingHextile:
513 statNumRectsHextile++;
514 handleHextileRect(rx, ry, rw, rh);
515 break;
516 case RfbProto.EncodingZRLE:
517 statNumRectsZRLE++;
518 handleZRLERect(rx, ry, rw, rh);
519 break;
520 case RfbProto.EncodingZlib:
521 handleZlibRect(rx, ry, rw, rh);
522 break;
523 case RfbProto.EncodingTight:
enikey2f7f46e2008-12-19 09:32:35 +0000524 if (tightDecoder != null) {
enikey479b18c2008-12-19 09:39:40 +0000525 statNumRectsTightJPEG = tightDecoder.getNumJPEGRects();
enikey2f7f46e2008-12-19 09:32:35 +0000526 }
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000527 handleTightRect(rx, ry, rw, rh);
528 break;
529 default:
530 throw new Exception("Unknown RFB rectangle encoding " +
531 rfb.updateRectEncoding);
532 }
533
534 rfb.stopTiming();
535
536 statNumPixelRects++;
537 statNumBytesDecoded += rw * rh * bytesPixel;
538 statNumBytesEncoded +=
539 (int)(rfb.getNumBytesRead() - numBytesReadBefore);
540 }
541
542 boolean fullUpdateNeeded = false;
543
544 // Start/stop session recording if necessary. Request full
545 // update if a new session file was opened.
546 if (viewer.checkRecordingStatus())
547 fullUpdateNeeded = true;
548
549 // Defer framebuffer update request if necessary. But wake up
550 // immediately on keyboard or mouse event. Also, don't sleep
551 // if there is some data to receive, or if the last update
552 // included a PointerPos message.
553 if (viewer.deferUpdateRequests > 0 &&
554 rfb.available() == 0 && !cursorPosReceived) {
555 synchronized(rfb) {
556 try {
557 rfb.wait(viewer.deferUpdateRequests);
558 } catch (InterruptedException e) {
559 }
560 }
561 }
562
563 viewer.autoSelectEncodings();
564
565 // Before requesting framebuffer update, check if the pixel
566 // format should be changed.
567 if (viewer.options.eightBitColors != (bytesPixel == 1)) {
568 // Pixel format should be changed.
569 if (!rfb.continuousUpdatesAreActive()) {
570 // Continuous updates are not used. In this case, we just
571 // set new pixel format and request full update.
572 setPixelFormat();
573 fullUpdateNeeded = true;
574 } else {
575 // Otherwise, disable continuous updates first. Pixel
576 // format will be set later when we are sure that there
577 // will be no unsolicited framebuffer updates.
578 rfb.tryDisableContinuousUpdates();
579 break; // skip the code below
580 }
581 }
582
583 // Enable/disable continuous updates to reflect the GUI setting.
584 boolean enable = viewer.options.continuousUpdates;
585 if (enable != rfb.continuousUpdatesAreActive()) {
586 if (enable) {
587 rfb.tryEnableContinuousUpdates(0, 0, rfb.framebufferWidth,
588 rfb.framebufferHeight);
589 } else {
590 rfb.tryDisableContinuousUpdates();
591 }
592 }
593
594 // Finally, request framebuffer update if needed.
595 if (fullUpdateNeeded) {
596 rfb.writeFramebufferUpdateRequest(0, 0, rfb.framebufferWidth,
597 rfb.framebufferHeight, false);
598 } else if (!rfb.continuousUpdatesAreActive()) {
599 rfb.writeFramebufferUpdateRequest(0, 0, rfb.framebufferWidth,
600 rfb.framebufferHeight, true);
601 }
602
603 break;
604
605 case RfbProto.SetColourMapEntries:
606 throw new Exception("Can't handle SetColourMapEntries message");
607
608 case RfbProto.Bell:
609 Toolkit.getDefaultToolkit().beep();
610 break;
611
612 case RfbProto.ServerCutText:
613 String s = rfb.readServerCutText();
614 viewer.clipboard.setCutText(s);
615 break;
616
617 case RfbProto.EndOfContinuousUpdates:
618 if (rfb.continuousUpdatesAreActive()) {
619 rfb.endOfContinuousUpdates();
620
621 // Change pixel format if such change was pending. Note that we
622 // could not change pixel format while continuous updates were
623 // in effect.
624 boolean incremental = true;
625 if (viewer.options.eightBitColors != (bytesPixel == 1)) {
626 setPixelFormat();
627 incremental = false;
628 }
629 // From this point, we ask for updates explicitly.
630 rfb.writeFramebufferUpdateRequest(0, 0, rfb.framebufferWidth,
631 rfb.framebufferHeight,
632 incremental);
633 }
634 break;
635
636 default:
637 throw new Exception("Unknown RFB message type " + msgType);
638 }
639 }
640 }
641
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000642 //
643 // Handle a raw rectangle. The second form with paint==false is used
644 // by the Hextile decoder for raw-encoded tiles.
645 //
646
enikey6c72ce12008-12-19 09:47:14 +0000647 void handleRawRect(int x, int y, int w, int h) throws IOException, Exception {
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000648 handleRawRect(x, y, w, h, true);
649 }
650
651 void handleRawRect(int x, int y, int w, int h, boolean paint)
enikey6c72ce12008-12-19 09:47:14 +0000652 throws IOException , Exception{
653 rawDecoder.handleRect(x, y, w, h);
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000654 if (paint)
655 scheduleRepaint(x, y, w, h);
656 }
657
658 //
659 // Handle a CopyRect rectangle.
660 //
661
662 void handleCopyRect(int x, int y, int w, int h) throws IOException {
663
664 rfb.readCopyRect();
665 memGraphics.copyArea(rfb.copyRectSrcX, rfb.copyRectSrcY, w, h,
666 x - rfb.copyRectSrcX, y - rfb.copyRectSrcY);
667
668 scheduleRepaint(x, y, w, h);
669 }
670
671 //
672 // Handle an RRE-encoded rectangle.
673 //
674
675 void handleRRERect(int x, int y, int w, int h) throws IOException {
enikey6c72ce12008-12-19 09:47:14 +0000676 rreDecoder.handleRect(x, y, w, h);
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000677 scheduleRepaint(x, y, w, h);
678 }
679
680 //
681 // Handle a CoRRE-encoded rectangle.
682 //
683
684 void handleCoRRERect(int x, int y, int w, int h) throws IOException {
enikey6c72ce12008-12-19 09:47:14 +0000685 correDecoder.handleRect(x, y, w, h);
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000686 scheduleRepaint(x, y, w, h);
687 }
688
689 //
690 // Handle a Hextile-encoded rectangle.
691 //
692
enikey6c72ce12008-12-19 09:47:14 +0000693 void handleHextileRect(int x, int y, int w, int h) throws IOException,
694 Exception {
695 hextileDecoder.handleRect(x, y, w, h);
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000696 }
697
698 //
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000699 // Handle a ZRLE-encoded rectangle.
700 //
701 // FIXME: Currently, session recording is not fully supported for ZRLE.
702 //
703
704 void handleZRLERect(int x, int y, int w, int h) throws Exception {
enikeyc92c1d12008-12-19 09:58:31 +0000705 zrleDecoder.handleRect(x, y, w, h);
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000706 scheduleRepaint(x, y, w, h);
707 }
708
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000709 //
710 // Handle a Zlib-encoded rectangle.
711 //
712
713 void handleZlibRect(int x, int y, int w, int h) throws Exception {
enikeyc92c1d12008-12-19 09:58:31 +0000714 zlibDecoder.handleRect(x, y, w, h);
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000715 scheduleRepaint(x, y, w, h);
716 }
717
718 //
719 // Handle a Tight-encoded rectangle.
720 //
721
722 void handleTightRect(int x, int y, int w, int h) throws Exception {
enikey2f7f46e2008-12-19 09:32:35 +0000723 tightDecoder.handleRect(x, y, w, h);
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000724 scheduleRepaint(x, y, w, h);
725 }
726
727 //
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000728 // Tell JVM to repaint specified desktop area.
729 //
730
enikey0dbc1532008-12-19 08:51:47 +0000731 public void scheduleRepaint(int x, int y, int w, int h) {
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000732 // Request repaint, deferred if necessary.
733 if (rfb.framebufferWidth == scaledWidth) {
734 repaint(viewer.deferScreenUpdates, x, y, w, h);
735 } else {
736 int sx = x * scalingFactor / 100;
737 int sy = y * scalingFactor / 100;
738 int sw = ((x + w) * scalingFactor + 49) / 100 - sx + 1;
739 int sh = ((y + h) * scalingFactor + 49) / 100 - sy + 1;
740 repaint(viewer.deferScreenUpdates, sx, sy, sw, sh);
741 }
742 }
743
744 //
745 // Handle events.
746 //
747
748 public void keyPressed(KeyEvent evt) {
749 processLocalKeyEvent(evt);
750 }
751 public void keyReleased(KeyEvent evt) {
752 processLocalKeyEvent(evt);
753 }
754 public void keyTyped(KeyEvent evt) {
755 evt.consume();
756 }
757
758 public void mousePressed(MouseEvent evt) {
759 processLocalMouseEvent(evt, false);
760 }
761 public void mouseReleased(MouseEvent evt) {
762 processLocalMouseEvent(evt, false);
763 }
764 public void mouseMoved(MouseEvent evt) {
765 processLocalMouseEvent(evt, true);
766 }
767 public void mouseDragged(MouseEvent evt) {
768 processLocalMouseEvent(evt, true);
769 }
770
Constantin Kaplinskyf7cb2bf2008-05-27 08:38:28 +0000771 //
772 // Ignored events.
773 //
774
775 public void mouseClicked(MouseEvent evt) {}
776 public void mouseEntered(MouseEvent evt) {}
777 public void mouseExited(MouseEvent evt) {}
778
779 //
780 // Actual event processing.
781 //
782
783 private void processLocalKeyEvent(KeyEvent evt) {
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000784 if (viewer.rfb != null && rfb.inNormalProtocol) {
785 if (!inputEnabled) {
786 if ((evt.getKeyChar() == 'r' || evt.getKeyChar() == 'R') &&
787 evt.getID() == KeyEvent.KEY_PRESSED ) {
788 // Request screen update.
789 try {
790 rfb.writeFramebufferUpdateRequest(0, 0, rfb.framebufferWidth,
791 rfb.framebufferHeight, false);
792 } catch (IOException e) {
793 e.printStackTrace();
794 }
795 }
796 } else {
797 // Input enabled.
798 synchronized(rfb) {
799 try {
800 rfb.writeKeyEvent(evt);
801 } catch (Exception e) {
802 e.printStackTrace();
803 }
804 rfb.notify();
805 }
806 }
807 }
enikeyc41ba1d2008-12-19 09:07:22 +0000808 // Don't ever pass keyboard events to AWT for default processing.
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000809 // Otherwise, pressing Tab would switch focus to ButtonPanel etc.
810 evt.consume();
811 }
812
Constantin Kaplinskyf7cb2bf2008-05-27 08:38:28 +0000813 private void processLocalMouseEvent(MouseEvent evt, boolean moved) {
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000814 if (viewer.rfb != null && rfb.inNormalProtocol) {
Constantin Kaplinskyf7cb2bf2008-05-27 08:38:28 +0000815 if (!inSelectionMode) {
816 if (inputEnabled) {
817 sendMouseEvent(evt, moved);
818 }
819 } else {
820 handleSelectionMouseEvent(evt);
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000821 }
822 }
823 }
824
Constantin Kaplinskyf7cb2bf2008-05-27 08:38:28 +0000825 private void sendMouseEvent(MouseEvent evt, boolean moved) {
826 if (moved) {
827 softCursorMove(evt.getX(), evt.getY());
828 }
829 if (rfb.framebufferWidth != scaledWidth) {
830 int sx = (evt.getX() * 100 + scalingFactor/2) / scalingFactor;
831 int sy = (evt.getY() * 100 + scalingFactor/2) / scalingFactor;
832 evt.translatePoint(sx - evt.getX(), sy - evt.getY());
833 }
834 synchronized(rfb) {
835 try {
836 rfb.writePointerEvent(evt);
837 } catch (Exception e) {
838 e.printStackTrace();
839 }
840 rfb.notify();
841 }
842 }
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000843
844 //
845 // Reset update statistics.
846 //
847
848 void resetStats() {
849 statStartTime = System.currentTimeMillis();
850 statNumUpdates = 0;
851 statNumTotalRects = 0;
852 statNumPixelRects = 0;
853 statNumRectsTight = 0;
854 statNumRectsTightJPEG = 0;
855 statNumRectsZRLE = 0;
856 statNumRectsHextile = 0;
857 statNumRectsRaw = 0;
858 statNumRectsCopy = 0;
859 statNumBytesEncoded = 0;
860 statNumBytesDecoded = 0;
enikey2f7f46e2008-12-19 09:32:35 +0000861 if (tightDecoder != null)
862 tightDecoder.setNumJPEGRects(0);
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +0000863 }
864
865 //////////////////////////////////////////////////////////////////
866 //
867 // Handle cursor shape updates (XCursor and RichCursor encodings).
868 //
869
870 boolean showSoftCursor = false;
871
872 MemoryImageSource softCursorSource;
873 Image softCursor;
874
875 int cursorX = 0, cursorY = 0;
876 int cursorWidth, cursorHeight;
877 int origCursorWidth, origCursorHeight;
878 int hotX, hotY;
879 int origHotX, origHotY;
880
881 //
882 // Handle cursor shape update (XCursor and RichCursor encodings).
883 //
884
885 synchronized void
886 handleCursorShapeUpdate(int encodingType,
887 int xhot, int yhot, int width, int height)
888 throws IOException {
889
890 softCursorFree();
891
892 if (width * height == 0)
893 return;
894
895 // Ignore cursor shape data if requested by user.
896 if (viewer.options.ignoreCursorUpdates) {
897 int bytesPerRow = (width + 7) / 8;
898 int bytesMaskData = bytesPerRow * height;
899
900 if (encodingType == rfb.EncodingXCursor) {
901 rfb.skipBytes(6 + bytesMaskData * 2);
902 } else {
903 // rfb.EncodingRichCursor
904 rfb.skipBytes(width * height + bytesMaskData);
905 }
906 return;
907 }
908
909 // Decode cursor pixel data.
910 softCursorSource = decodeCursorShape(encodingType, width, height);
911
912 // Set original (non-scaled) cursor dimensions.
913 origCursorWidth = width;
914 origCursorHeight = height;
915 origHotX = xhot;
916 origHotY = yhot;
917
918 // Create off-screen cursor image.
919 createSoftCursor();
920
921 // Show the cursor.
922 showSoftCursor = true;
923 repaint(viewer.deferCursorUpdates,
924 cursorX - hotX, cursorY - hotY, cursorWidth, cursorHeight);
925 }
926
927 //
928 // decodeCursorShape(). Decode cursor pixel data and return
929 // corresponding MemoryImageSource instance.
930 //
931
932 synchronized MemoryImageSource
933 decodeCursorShape(int encodingType, int width, int height)
934 throws IOException {
935
936 int bytesPerRow = (width + 7) / 8;
937 int bytesMaskData = bytesPerRow * height;
938
939 int[] softCursorPixels = new int[width * height];
940
941 if (encodingType == rfb.EncodingXCursor) {
942
943 // Read foreground and background colors of the cursor.
944 byte[] rgb = new byte[6];
945 rfb.readFully(rgb);
946 int[] colors = { (0xFF000000 | (rgb[3] & 0xFF) << 16 |
947 (rgb[4] & 0xFF) << 8 | (rgb[5] & 0xFF)),
948 (0xFF000000 | (rgb[0] & 0xFF) << 16 |
949 (rgb[1] & 0xFF) << 8 | (rgb[2] & 0xFF)) };
950
951 // Read pixel and mask data.
952 byte[] pixBuf = new byte[bytesMaskData];
953 rfb.readFully(pixBuf);
954 byte[] maskBuf = new byte[bytesMaskData];
955 rfb.readFully(maskBuf);
956
957 // Decode pixel data into softCursorPixels[].
958 byte pixByte, maskByte;
959 int x, y, n, result;
960 int i = 0;
961 for (y = 0; y < height; y++) {
962 for (x = 0; x < width / 8; x++) {
963 pixByte = pixBuf[y * bytesPerRow + x];
964 maskByte = maskBuf[y * bytesPerRow + x];
965 for (n = 7; n >= 0; n--) {
966 if ((maskByte >> n & 1) != 0) {
967 result = colors[pixByte >> n & 1];
968 } else {
969 result = 0; // Transparent pixel
970 }
971 softCursorPixels[i++] = result;
972 }
973 }
974 for (n = 7; n >= 8 - width % 8; n--) {
975 if ((maskBuf[y * bytesPerRow + x] >> n & 1) != 0) {
976 result = colors[pixBuf[y * bytesPerRow + x] >> n & 1];
977 } else {
978 result = 0; // Transparent pixel
979 }
980 softCursorPixels[i++] = result;
981 }
982 }
983
984 } else {
985 // encodingType == rfb.EncodingRichCursor
986
987 // Read pixel and mask data.
988 byte[] pixBuf = new byte[width * height * bytesPixel];
989 rfb.readFully(pixBuf);
990 byte[] maskBuf = new byte[bytesMaskData];
991 rfb.readFully(maskBuf);
992
993 // Decode pixel data into softCursorPixels[].
994 byte pixByte, maskByte;
995 int x, y, n, result;
996 int i = 0;
997 for (y = 0; y < height; y++) {
998 for (x = 0; x < width / 8; x++) {
999 maskByte = maskBuf[y * bytesPerRow + x];
1000 for (n = 7; n >= 0; n--) {
1001 if ((maskByte >> n & 1) != 0) {
1002 if (bytesPixel == 1) {
1003 result = cm8.getRGB(pixBuf[i]);
1004 } else {
1005 result = 0xFF000000 |
1006 (pixBuf[i * 4 + 2] & 0xFF) << 16 |
1007 (pixBuf[i * 4 + 1] & 0xFF) << 8 |
1008 (pixBuf[i * 4] & 0xFF);
1009 }
1010 } else {
1011 result = 0; // Transparent pixel
1012 }
1013 softCursorPixels[i++] = result;
1014 }
1015 }
1016 for (n = 7; n >= 8 - width % 8; n--) {
1017 if ((maskBuf[y * bytesPerRow + x] >> n & 1) != 0) {
1018 if (bytesPixel == 1) {
1019 result = cm8.getRGB(pixBuf[i]);
1020 } else {
1021 result = 0xFF000000 |
1022 (pixBuf[i * 4 + 2] & 0xFF) << 16 |
1023 (pixBuf[i * 4 + 1] & 0xFF) << 8 |
1024 (pixBuf[i * 4] & 0xFF);
1025 }
1026 } else {
1027 result = 0; // Transparent pixel
1028 }
1029 softCursorPixels[i++] = result;
1030 }
1031 }
1032
1033 }
1034
1035 return new MemoryImageSource(width, height, softCursorPixels, 0, width);
1036 }
1037
1038 //
1039 // createSoftCursor(). Assign softCursor new Image (scaled if necessary).
1040 // Uses softCursorSource as a source for new cursor image.
1041 //
1042
1043 synchronized void
1044 createSoftCursor() {
1045
1046 if (softCursorSource == null)
1047 return;
1048
1049 int scaleCursor = viewer.options.scaleCursor;
1050 if (scaleCursor == 0 || !inputEnabled)
1051 scaleCursor = 100;
1052
1053 // Save original cursor coordinates.
1054 int x = cursorX - hotX;
1055 int y = cursorY - hotY;
1056 int w = cursorWidth;
1057 int h = cursorHeight;
1058
1059 cursorWidth = (origCursorWidth * scaleCursor + 50) / 100;
1060 cursorHeight = (origCursorHeight * scaleCursor + 50) / 100;
1061 hotX = (origHotX * scaleCursor + 50) / 100;
1062 hotY = (origHotY * scaleCursor + 50) / 100;
1063 softCursor = Toolkit.getDefaultToolkit().createImage(softCursorSource);
1064
1065 if (scaleCursor != 100) {
1066 softCursor = softCursor.getScaledInstance(cursorWidth, cursorHeight,
1067 Image.SCALE_SMOOTH);
1068 }
1069
1070 if (showSoftCursor) {
1071 // Compute screen area to update.
1072 x = Math.min(x, cursorX - hotX);
1073 y = Math.min(y, cursorY - hotY);
1074 w = Math.max(w, cursorWidth);
1075 h = Math.max(h, cursorHeight);
1076
1077 repaint(viewer.deferCursorUpdates, x, y, w, h);
1078 }
1079 }
1080
1081 //
1082 // softCursorMove(). Moves soft cursor into a particular location.
1083 //
1084
1085 synchronized void softCursorMove(int x, int y) {
1086 int oldX = cursorX;
1087 int oldY = cursorY;
1088 cursorX = x;
1089 cursorY = y;
1090 if (showSoftCursor) {
1091 repaint(viewer.deferCursorUpdates,
1092 oldX - hotX, oldY - hotY, cursorWidth, cursorHeight);
1093 repaint(viewer.deferCursorUpdates,
1094 cursorX - hotX, cursorY - hotY, cursorWidth, cursorHeight);
1095 }
1096 }
1097
1098 //
1099 // softCursorFree(). Remove soft cursor, dispose resources.
1100 //
1101
1102 synchronized void softCursorFree() {
1103 if (showSoftCursor) {
1104 showSoftCursor = false;
1105 softCursor = null;
1106 softCursorSource = null;
1107
1108 repaint(viewer.deferCursorUpdates,
1109 cursorX - hotX, cursorY - hotY, cursorWidth, cursorHeight);
1110 }
1111 }
Constantin Kaplinskyf7cb2bf2008-05-27 08:38:28 +00001112
1113 //////////////////////////////////////////////////////////////////
1114 //
1115 // Support for selecting a rectangular video area.
1116 //
1117
1118 /** This flag is false in normal operation, and true in the selection mode. */
1119 private boolean inSelectionMode;
1120
1121 /** The point where the selection was started. */
1122 private Point selectionStart;
1123
1124 /** The second point of the selection. */
1125 private Point selectionEnd;
1126
1127 /**
1128 * We change cursor when enabling the selection mode. In this variable, we
1129 * save the original cursor so we can restore it on returning to the normal
1130 * mode.
1131 */
1132 private Cursor savedCursor;
1133
1134 /**
1135 * Initialize selection-related varibles.
1136 */
1137 private synchronized void resetSelection() {
1138 inSelectionMode = false;
1139 selectionStart = new Point(0, 0);
1140 selectionEnd = new Point(0, 0);
1141
1142 savedCursor = getCursor();
1143 }
1144
1145 /**
1146 * Check current state of the selection mode.
1147 * @return true in the selection mode, false otherwise.
1148 */
1149 public boolean isInSelectionMode() {
1150 return inSelectionMode;
1151 }
1152
1153 /**
1154 * Get current selection.
1155 * @param useScreenCoords use screen coordinates if true, or framebuffer
1156 * coordinates if false. This makes difference when scaling factor is not 100.
1157 * @return The selection as a {@link Rectangle}.
1158 */
1159 private synchronized Rectangle getSelection(boolean useScreenCoords) {
Constantin Kaplinsky4f374ff2008-09-03 04:51:28 +00001160 int x0 = selectionStart.x;
1161 int x1 = selectionEnd.x;
1162 int y0 = selectionStart.y;
1163 int y1 = selectionEnd.y;
Constantin Kaplinskyf7cb2bf2008-05-27 08:38:28 +00001164 // Make x and y point to the upper left corner of the selection.
Constantin Kaplinsky4f374ff2008-09-03 04:51:28 +00001165 if (x1 < x0) {
1166 int t = x0; x0 = x1; x1 = t;
Constantin Kaplinskyf7cb2bf2008-05-27 08:38:28 +00001167 }
Constantin Kaplinsky4f374ff2008-09-03 04:51:28 +00001168 if (y1 < y0) {
1169 int t = y0; y0 = y1; y1 = t;
Constantin Kaplinskyf7cb2bf2008-05-27 08:38:28 +00001170 }
Constantin Kaplinsky4f374ff2008-09-03 04:51:28 +00001171 // Include the borders in the selection (unless it's empty).
1172 if (x0 != x1 && y0 != y1) {
1173 x1 += 1;
1174 y1 += 1;
Constantin Kaplinskyf7cb2bf2008-05-27 08:38:28 +00001175 }
1176 // Translate from screen coordinates to framebuffer coordinates.
1177 if (rfb.framebufferWidth != scaledWidth) {
Constantin Kaplinsky4f374ff2008-09-03 04:51:28 +00001178 x0 = (x0 * 100 + scalingFactor/2) / scalingFactor;
1179 y0 = (y0 * 100 + scalingFactor/2) / scalingFactor;
1180 x1 = (x1 * 100 + scalingFactor/2) / scalingFactor;
1181 y1 = (y1 * 100 + scalingFactor/2) / scalingFactor;
Constantin Kaplinskyf7cb2bf2008-05-27 08:38:28 +00001182 }
Constantin Kaplinsky10da44d2008-09-03 03:12:18 +00001183 // Clip the selection to framebuffer.
Constantin Kaplinsky4f374ff2008-09-03 04:51:28 +00001184 if (x0 < 0)
1185 x0 = 0;
1186 if (y0 < 0)
1187 y0 = 0;
1188 if (x1 > rfb.framebufferWidth)
1189 x1 = rfb.framebufferWidth;
1190 if (y1 > rfb.framebufferHeight)
1191 y1 = rfb.framebufferHeight;
Constantin Kaplinskyf7cb2bf2008-05-27 08:38:28 +00001192 // Make width a multiple of 16.
Constantin Kaplinsky4f374ff2008-09-03 04:51:28 +00001193 int widthBlocks = (x1 - x0 + 8) / 16;
1194 if (selectionStart.x <= selectionEnd.x) {
1195 x1 = x0 + widthBlocks * 16;
1196 if (x1 > rfb.framebufferWidth) {
1197 x1 -= 16;
1198 }
1199 } else {
1200 x0 = x1 - widthBlocks * 16;
1201 if (x0 < 0) {
1202 x0 += 16;
1203 }
Constantin Kaplinskyf7cb2bf2008-05-27 08:38:28 +00001204 }
1205 // Make height a multiple of 8.
Constantin Kaplinsky4f374ff2008-09-03 04:51:28 +00001206 int heightBlocks = (y1 - y0 + 4) / 8;
1207 if (selectionStart.y <= selectionEnd.y) {
1208 y1 = y0 + heightBlocks * 8;
1209 if (y1 > rfb.framebufferHeight) {
1210 y1 -= 8;
1211 }
1212 } else {
1213 y0 = y1 - heightBlocks * 8;
1214 if (y0 < 0) {
1215 y0 += 8;
1216 }
Constantin Kaplinskyf7cb2bf2008-05-27 08:38:28 +00001217 }
1218 // Translate the selection back to screen coordinates if requested.
Constantin Kaplinskyf7cb2bf2008-05-27 08:38:28 +00001219 if (useScreenCoords && rfb.framebufferWidth != scaledWidth) {
Constantin Kaplinsky4f374ff2008-09-03 04:51:28 +00001220 x0 = (x0 * scalingFactor + 50) / 100;
1221 y0 = (y0 * scalingFactor + 50) / 100;
1222 x1 = (x1 * scalingFactor + 50) / 100;
1223 y1 = (y1 * scalingFactor + 50) / 100;
Constantin Kaplinskyf7cb2bf2008-05-27 08:38:28 +00001224 }
Constantin Kaplinsky4f374ff2008-09-03 04:51:28 +00001225 // Construct and return the result.
1226 return new Rectangle(x0, y0, x1 - x0, y1 - y0);
Constantin Kaplinskyf7cb2bf2008-05-27 08:38:28 +00001227 }
1228
1229 /**
1230 * Enable or disable the selection mode.
1231 * @param enable enables the selection mode if true, disables if fasle.
1232 */
1233 public synchronized void enableSelection(boolean enable) {
1234 if (enable && !inSelectionMode) {
1235 // Enter the selection mode.
1236 inSelectionMode = true;
1237 savedCursor = getCursor();
1238 setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
1239 repaint();
1240 } else if (!enable && inSelectionMode) {
1241 // Leave the selection mode.
1242 inSelectionMode = false;
1243 setCursor(savedCursor);
1244 repaint();
1245 }
1246 }
1247
1248 /**
1249 * Process mouse events in the selection mode.
enikeyc41ba1d2008-12-19 09:07:22 +00001250 *
Constantin Kaplinskyf7cb2bf2008-05-27 08:38:28 +00001251 * @param evt mouse event that was originally passed to
1252 * {@link MouseListener} or {@link MouseMotionListener}.
1253 */
1254 private synchronized void handleSelectionMouseEvent(MouseEvent evt) {
1255 int id = evt.getID();
1256 boolean button1 = (evt.getModifiers() & InputEvent.BUTTON1_MASK) != 0;
1257
1258 if (id == MouseEvent.MOUSE_PRESSED && button1) {
1259 selectionStart = selectionEnd = evt.getPoint();
1260 repaint();
1261 }
1262 if (id == MouseEvent.MOUSE_DRAGGED && button1) {
1263 selectionEnd = evt.getPoint();
1264 repaint();
1265 }
1266 if (id == MouseEvent.MOUSE_RELEASED && button1) {
1267 try {
1268 rfb.trySendVideoSelection(getSelection(false));
1269 } catch (IOException e) {
1270 e.printStackTrace();
1271 }
1272 }
1273 }
1274
enikey418611f2008-12-19 04:37:09 +00001275 //
1276 // Override RecordInterface methods
1277 //
1278
1279 public boolean isRecordFromBeginning() {
1280 return rfb.recordFromBeginning;
1281 }
1282
1283 public boolean canWrite() {
1284 // We can record if rec is not null
1285 return rfb.rec != null;
1286 }
1287
1288 public void write(byte b[]) throws IOException {
1289 rfb.rec.write(b);
1290 }
1291
1292 public void write(byte b[], int off, int len) throws IOException {
1293 rfb.rec.write(b, off, len);
1294 }
1295
1296 public void writeByte(byte b) throws IOException {
1297 rfb.rec.writeByte(b);
1298 }
1299
1300 public void writeByte(int i) throws IOException {
1301 rfb.rec.writeByte(i);
1302 }
1303
1304 public void writeIntBE(int v) throws IOException {
1305 rfb.rec.writeIntBE(v);
1306 }
1307
1308 public void recordCompactLen(int len) throws IOException {
1309 rfb.recordCompactLen(len);
1310 }
1311
1312 public void recordCompressedData(byte[] data) throws IOException {
1313 rfb.recordCompressedData(data);
1314 }
Constantin Kaplinsky2844fd52008-04-14 08:02:25 +00001315}