[Refactoring, cleanup] Write encoding ID of real decoders in decoders classes. Pseudo encoding ID still writes in RfbProto class. ZlibDecoder recoding enabled. ZRLEDecoder recoding still not working.

git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/trunk@3455 3789f03b-4d11-0410-bbf8-ca57d06f2519
diff --git a/java/src/com/tightvnc/decoder/CoRREDecoder.java b/java/src/com/tightvnc/decoder/CoRREDecoder.java
index 3947d15..bc08668 100644
--- a/java/src/com/tightvnc/decoder/CoRREDecoder.java
+++ b/java/src/com/tightvnc/decoder/CoRREDecoder.java
@@ -28,6 +28,15 @@
   //
 
   public void handleRect(int x, int y, int w, int h) throws IOException {
+
+    //
+    // Write encoding ID to record output stream
+    //
+
+    if (dos != null) {
+      dos.writeInt(CoRREDecoder.EncodingCoRRE);
+    }
+
     int nSubrects = rfbis.readU32();
 
     byte[] bg_buf = new byte[bytesPerPixel];
diff --git a/java/src/com/tightvnc/decoder/HextileDecoder.java b/java/src/com/tightvnc/decoder/HextileDecoder.java
index 9c05309..da7e778 100644
--- a/java/src/com/tightvnc/decoder/HextileDecoder.java
+++ b/java/src/com/tightvnc/decoder/HextileDecoder.java
@@ -46,6 +46,15 @@
 
   public void handleRect(int x, int y, int w, int h) throws IOException,
                                                             Exception {
+
+    //
+    // Write encoding ID to record output stream
+    //
+
+    if (dos != null) {
+      dos.writeInt(HextileDecoder.EncodingHextile);
+    }
+
     hextile_bg = new Color(0);
     hextile_fg = new Color(0);
 
@@ -87,9 +96,16 @@
 
     // Is it a raw-encoded sub-rectangle?
     if ((subencoding & HextileRaw) != 0) {
-      //handleRawRect(tx, ty, tw, th, false);
+      //
+      // Disable encoding id writting to record stream
+      // in super (RawDecoder) class, cause we write subencoding ID
+      // in this class (see code above).
+      //
+
+      super.enableEncodingRecordWritting(false);
       super.handleRect(tx, ty, tw, th);
       super.handleUpdatedPixels(tx, ty, tw, th);
+      super.enableEncodingRecordWritting(true);
       return;
     }
 
diff --git a/java/src/com/tightvnc/decoder/RREDecoder.java b/java/src/com/tightvnc/decoder/RREDecoder.java
index e2b5feb..02eb513 100644
--- a/java/src/com/tightvnc/decoder/RREDecoder.java
+++ b/java/src/com/tightvnc/decoder/RREDecoder.java
@@ -30,6 +30,15 @@
   //
 
   public void handleRect(int x, int y, int w, int h) throws IOException {
+
+    //
+    // Write encoding ID to record output stream
+    //
+
+    if (dos != null) {
+      dos.writeInt(RREDecoder.EncodingRRE);
+    }
+
     int nSubrects = rfbis.readU32();
     byte[] bg_buf = new byte[bytesPerPixel];
     rfbis.readFully(bg_buf);
diff --git a/java/src/com/tightvnc/decoder/RawDecoder.java b/java/src/com/tightvnc/decoder/RawDecoder.java
index 9025e9e..affdc67 100644
--- a/java/src/com/tightvnc/decoder/RawDecoder.java
+++ b/java/src/com/tightvnc/decoder/RawDecoder.java
@@ -85,6 +85,15 @@
   //
 
   public void handleRect(int x, int y, int w, int h) throws IOException, Exception {
+
+    //
+    // Write encoding ID to record output stream
+    //
+
+    if ((dos != null) && (enableEncodingRecordWritting)) {
+      dos.writeInt(RawDecoder.EncodingRaw);
+    }
+
     if (bytesPerPixel == 1) {
       for (int dy = y; dy < y + h; dy++) {
         if (pixels8 != null) {
@@ -189,6 +198,17 @@
   }
 
   //
+  // This method will be used by HextileDecoder to disable
+  // double writting encoding id to record stream.
+  //
+  // FIXME: Try to find better solution than this.
+  //
+
+  protected void enableEncodingRecordWritting(boolean enable) {
+    enableEncodingRecordWritting = enable;
+  }
+
+  //
   // Unique data for every decoder (? maybe not ?)
   //
 
@@ -199,6 +219,7 @@
   protected Graphics graphics = null;
   protected RecordInterface rec = null;
   protected DataOutput dos = null;
+  protected boolean enableEncodingRecordWritting = true;
 
   //
   // This data must be shared between decoders
diff --git a/java/src/com/tightvnc/decoder/TightDecoder.java b/java/src/com/tightvnc/decoder/TightDecoder.java
index c4ce226..a1d28d7 100644
--- a/java/src/com/tightvnc/decoder/TightDecoder.java
+++ b/java/src/com/tightvnc/decoder/TightDecoder.java
@@ -71,6 +71,14 @@
 
   public void handleRect(int x, int y, int w, int h) throws Exception {
 
+    //
+    // Write encoding ID to record output stream
+    //
+
+    if (dos != null) {
+      dos.writeInt(TightDecoder.EncodingTight);
+    }
+
     int comp_ctl = rfbis.readU8();
     if (rec.canWrite()) {
       if (rec.isRecordFromBeginning() ||
diff --git a/java/src/com/tightvnc/decoder/ZRLEDecoder.java b/java/src/com/tightvnc/decoder/ZRLEDecoder.java
index 91f7c16..fbbb3a6 100644
--- a/java/src/com/tightvnc/decoder/ZRLEDecoder.java
+++ b/java/src/com/tightvnc/decoder/ZRLEDecoder.java
@@ -34,6 +34,15 @@
   //
 
   public void handleRect(int x, int y, int w, int h) throws IOException, Exception {
+
+    //
+    // Write encoding ID to record output stream
+    //
+
+    if (dos != null) {
+      dos.writeInt(ZRLEDecoder.EncodingZRLE);
+    }
+
     if (zrleInStream == null)
       zrleInStream = new ZlibInStream();
 
diff --git a/java/src/com/tightvnc/decoder/ZlibDecoder.java b/java/src/com/tightvnc/decoder/ZlibDecoder.java
index 9a167b1..1370da1 100644
--- a/java/src/com/tightvnc/decoder/ZlibDecoder.java
+++ b/java/src/com/tightvnc/decoder/ZlibDecoder.java
@@ -29,6 +29,17 @@
   //
 
   public void handleRect(int x, int y, int w, int h) throws IOException  {
+
+    //
+    // Write encoding ID to record output stream.
+    // Remark: we forced changed encoding from zlib to raw
+    // cause at this moment we cannot save data in zlib encoding.
+    //
+
+    if (dos != null) {
+      dos.writeInt(RawDecoder.EncodingRaw);
+    }
+
     int nBytes = rfbis.readU32();
 
     if (zlibBuf == null || zlibBufLen < nBytes) {
@@ -38,17 +49,6 @@
 
     rfbis.readFully(zlibBuf, 0, nBytes);
 
-    //
-    // FIXME: Now we think that isRecordFromBeggining 
-    // always returns false and this part of code will be never
-    // executed.
-    //
-
-    //if (rec.canWrite() && rec.isRecordFromBeginning()) {
-    // rec.writeIntBE(nBytes);
-    // rec.write(zlibBuf, 0, nBytes);
-    //}
-
     if (zlibInflater == null) {
       zlibInflater = new Inflater();
     }
@@ -60,12 +60,11 @@
           zlibInflater.inflate(pixels8, dy * framebufferWidth + x, w);
 
           //
-          // Save decoded data to data output stream
+          // Save decoded raw data to data output stream
           //
 
-          //if (rec.canWrite() && !rec.isRecordFromBeginning())
-          //if (dos != null)
-          //  dos.write(pixels8, dy * framebufferWidth + x, w);
+          if (dos != null)
+            dos.write(pixels8, dy * framebufferWidth + x, w);
         }
       } else {
         byte[] buf = new byte[w * 4];
@@ -81,12 +80,11 @@
           }
 
           //
-          // Save decoded data to data output stream
+          // Save decoded raw data to data output stream
           //
 
-          //if (rec.canWrite() && !rec.isRecordFromBeginning())
-          //if (dos != null)
-          //  dos.write(buf);
+          if (dos != null)
+            dos.write(buf);
         }
       }
     } catch (DataFormatException ex) {
diff --git a/java/src/com/tightvnc/vncviewer/RfbProto.java b/java/src/com/tightvnc/vncviewer/RfbProto.java
index fcdbbfa..65cf909 100644
--- a/java/src/com/tightvnc/vncviewer/RfbProto.java
+++ b/java/src/com/tightvnc/vncviewer/RfbProto.java
@@ -177,7 +177,7 @@
   // Java on UNIX does not call keyPressed() on some keys, for example
   // swedish keys To prevent our workaround to produce duplicate
   // keypresses on JVMs that actually works, keep track of if
-  // keyPressed() for a "broken" key was called or not. 
+  // keyPressed() for a "broken" key was called or not.
   boolean brokenKeyPressed = false;
 
   // This will be set to true on the first framebuffer update
@@ -196,7 +196,7 @@
 
   // Before starting to record each saved session, we set this field
   // to 0, and increment on each framebuffer update. We don't flush
-  // the SessionRecorder data into the file before the second update. 
+  // the SessionRecorder data into the file before the second update.
   // This allows us to write initial framebuffer update with zero
   // timestamp, to let the player show initial desktop before
   // playback.
@@ -758,6 +758,19 @@
     numUpdatesInSession++;
   }
 
+  //
+  // Returns true if encoding is not pseudo
+  //
+  // FIXME: Find better way to differ pseudo and real encodings
+  //
+
+  boolean isRealDecoderEncoding(int encoding) {
+    if ((encoding >= 1) && (encoding <= 16)) {
+      return true;
+    }
+    return false;
+  }
+
   // Read a FramebufferUpdate rectangle header
 
   int updateRectX, updateRectY, updateRectW, updateRectH, updateRectEncoding;
@@ -782,7 +795,25 @@
       rec.writeShortBE(updateRectY);
       rec.writeShortBE(updateRectW);
       rec.writeShortBE(updateRectH);
-      if (updateRectEncoding == EncodingZlib && !recordFromBeginning) {
+
+      //
+      // If this is pseudo encoding or CopyRect that write encoding ID
+      // in this place. All real encoding ID will be written to record stream
+      // in decoder classes.
+      //
+      // TODO: Make CopyRect decoder class.
+      //
+
+      if (((updateRectEncoding == EncodingCopyRect)
+          || (!isRealDecoderEncoding(updateRectEncoding))) && (rec != null)) {
+        rec.writeIntBE(updateRectEncoding);
+      }
+
+      //
+      // Old code
+      //
+
+      /*if (updateRectEncoding == EncodingZlib && !recordFromBeginning) {
 	// Here we cannot write Zlib-encoded rectangles because the
 	// decoder won't be able to reproduce zlib stream state.
 	if (!zlibWarningShown) {
@@ -799,7 +830,7 @@
 			     "updates for session recording.");
 	  tightWarningShown = true;
 	}
-      }
+      }*/
     }
 
     if (updateRectEncoding < 0 || updateRectEncoding > MaxNormalEncoding)
@@ -954,7 +985,7 @@
       b[6 + i * 6 + 4] = (byte) ((blue[i] >> 8) & 0xff);
       b[6 + i * 6 + 5] = (byte) (blue[i] & 0xff);
     }
- 
+
     os.write(b);
   }
 
@@ -1002,7 +1033,7 @@
 
   //
   // A buffer for putting pointer and keyboard events before being sent.  This
-  // is to ensure that multiple RFB events generated from a single Java Event 
+  // is to ensure that multiple RFB events generated from a single Java Event
   // will all be sent in a single network packet.  The maximum possible
   // length is 4 modifier down events, a single key event followed by 4
   // modifier up events i.e. 9 key events or 72 bytes.
@@ -1197,13 +1228,13 @@
 	(key == 0xa3)) {                  // XK_sterling
       // Make sure we do not send keypress events twice on platforms
       // with correct JVMs (those that actually report KeyPress for all
-      // keys)	
+      // keys)
       if (down)
 	brokenKeyPressed = true;
 
       if (!down && !brokenKeyPressed) {
 	// We've got a release event for this key, but haven't received
-        // a press. Fake it. 
+        // a press. Fake it.
 	eventBufLen = 0;
 	writeModifierKeyEvents(evt.getModifiers());
 	writeKeyEvent(key, true);
@@ -1211,7 +1242,7 @@
       }
 
       if (!down)
-	brokenKeyPressed = false;  
+	brokenKeyPressed = false;
     }
 
     eventBufLen = 0;
@@ -1383,7 +1414,7 @@
     int y = rect.y;
     int w = rect.width;
     int h = rect.height;
- 
+
     byte[] b = new byte[10];
 
     b[0] = (byte) VideoRectangleSelection;
@@ -1456,7 +1487,7 @@
   }
 
   public void stopTiming() {
-    timing = false; 
+    timing = false;
     if (timeWaitedIn100us < timedKbits/2)
       timeWaitedIn100us = timedKbits/2; // upper limit 20Mbit/s
   }
@@ -1534,4 +1565,3 @@
     return r;
   }
 }
-