Delegate decoder object management to a separate class

Done in preparation for multi-core decoding. Keeps the complexity
out of the other classes. This also moves ownership of the
framebuffer in to CConnection. It's the CConnection object that is
aware of the threads and how to synchronise with them. Therefore
the ownership of the framebuffer must also be there to make sure
it isn't deleted whilst threads are working.
diff --git a/vncviewer/CConn.cxx b/vncviewer/CConn.cxx
index 9c12796..54716d3 100644
--- a/vncviewer/CConn.cxx
+++ b/vncviewer/CConn.cxx
@@ -29,8 +29,6 @@
 
 #include <rfb/CMsgWriter.h>
 #include <rfb/CSecurity.h>
-#include <rfb/encodings.h>
-#include <rfb/Decoder.h>
 #include <rfb/Hostname.h>
 #include <rfb/LogWriter.h>
 #include <rfb/Security.h>
@@ -83,8 +81,6 @@
   setShared(::shared);
   sock = socket;
 
-  memset(decoders, 0, sizeof(decoders));
-
   int encNum = encodingNum(preferredEncoding);
   if (encNum != -1)
     currentEncoding = encNum;
@@ -137,9 +133,6 @@
   OptionsDialog::removeCallback(handleOptions);
   Fl::remove_timeout(handleUpdateTimeout, this);
 
-  for (size_t i = 0; i < sizeof(decoders)/sizeof(decoders[0]); i++)
-    delete decoders[i];
-
   if (desktop)
     delete desktop;
 
@@ -441,20 +434,7 @@
   if (encoding != encodingCopyRect)
     lastServerEncoding = encoding;
 
-  if (!Decoder::supported(encoding)) {
-    // TRANSLATORS: Refers to a VNC protocol encoding type
-    vlog.error(_("Unknown encoding %d"), encoding);
-    throw Exception(_("Unknown encoding"));
-  }
-
-  if (!decoders[encoding]) {
-    decoders[encoding] = Decoder::createDecoder(encoding, this);
-    if (!decoders[encoding]) {
-      vlog.error(_("Unknown encoding %d"), encoding);
-      throw Exception(_("Unknown encoding"));
-    }
-  }
-  decoders[encoding]->readRect(r, desktop->getFramebuffer());
+  CConnection::dataRect(r, encoding);
 
   sock->inStream().stopTiming();
 }
diff --git a/vncviewer/CConn.h b/vncviewer/CConn.h
index 06e3040..c934f3d 100644
--- a/vncviewer/CConn.h
+++ b/vncviewer/CConn.h
@@ -23,10 +23,9 @@
 #include <FL/Fl.H>
 
 #include <rfb/CConnection.h>
-#include <rfb/encodings.h>
-#include <network/Socket.h>
+#include <rdr/FdInStream.h>
 
-namespace rfb { class Decoder; }
+namespace network { class Socket; }
 
 class DesktopWindow;
 
@@ -96,8 +95,6 @@
   bool pendingPFChange;
   rfb::PixelFormat pendingPF;
 
-  rfb::Decoder *decoders[rfb::encodingMax+1];
-
   int currentEncoding, lastServerEncoding;
 
   bool formatChange;
diff --git a/vncviewer/DesktopWindow.cxx b/vncviewer/DesktopWindow.cxx
index bba502b..2787bee 100644
--- a/vncviewer/DesktopWindow.cxx
+++ b/vncviewer/DesktopWindow.cxx
@@ -199,12 +199,6 @@
 }
 
 
-rfb::ModifiablePixelBuffer* DesktopWindow::getFramebuffer(void)
-{
-  return viewport->getFramebuffer();
-}
-
-
 // Copy the areas of the framebuffer that have been changed (damaged)
 // to the displayed window.
 
diff --git a/vncviewer/DesktopWindow.h b/vncviewer/DesktopWindow.h
index 8b91450..d755b6d 100644
--- a/vncviewer/DesktopWindow.h
+++ b/vncviewer/DesktopWindow.h
@@ -50,9 +50,6 @@
   // Updated session title
   void setName(const char *name);
 
-  // Return a pointer to the framebuffer for decoders to write into
-  rfb::ModifiablePixelBuffer* getFramebuffer(void);
-
   // Resize the current framebuffer, but retain the contents
   void resizeFramebuffer(int new_w, int new_h);
 
diff --git a/vncviewer/Viewport.cxx b/vncviewer/Viewport.cxx
index a7881bc..1bc0b82 100644
--- a/vncviewer/Viewport.cxx
+++ b/vncviewer/Viewport.cxx
@@ -113,6 +113,7 @@
 
   frameBuffer = createFramebuffer(w, h);
   assert(frameBuffer);
+  cc->setFramebuffer(frameBuffer);
 
   contextMenu = new Fl_Menu_Button(0, 0, 0, 0);
   // Setting box type to FL_NO_BOX prevents it from trying to draw the
@@ -149,8 +150,6 @@
 
   OptionsDialog::removeCallback(handleOptions);
 
-  delete frameBuffer;
-
   if (cursor) {
     if (!cursor->alloc_array)
       delete [] cursor->array;
@@ -179,11 +178,6 @@
   damage(FL_DAMAGE_USER1, r.tl.x + x(), r.tl.y + y(), r.width(), r.height());
 }
 
-rfb::ModifiablePixelBuffer* Viewport::getFramebuffer(void)
-{
-  return frameBuffer;
-}
-
 static const char * dotcursor_xpm[] = {
   "5 5 2 1",
   ".	c #000000",
@@ -274,51 +268,15 @@
 
 void Viewport::resize(int x, int y, int w, int h)
 {
-  PlatformPixelBuffer* newBuffer;
-  rfb::Rect rect;
+  if ((w != frameBuffer->width()) || (h != frameBuffer->height())) {
+    vlog.debug("Resizing framebuffer from %dx%d to %dx%d",
+               frameBuffer->width(), frameBuffer->height(), w, h);
 
-  const rdr::U8* data;
-  int stride;
-
-  const rdr::U8 black[4] = { 0, 0, 0, 0 };
-
-  // FIXME: Resize should probably be a feature of the pixel buffer itself
-
-  if ((w == frameBuffer->width()) && (h == frameBuffer->height()))
-    goto end;
-
-  vlog.debug("Resizing framebuffer from %dx%d to %dx%d",
-             frameBuffer->width(), frameBuffer->height(), w, h);
-
-  newBuffer = createFramebuffer(w, h);
-  assert(newBuffer);
-
-  rect.setXYWH(0, 0,
-               __rfbmin(newBuffer->width(), frameBuffer->width()),
-               __rfbmin(newBuffer->height(), frameBuffer->height()));
-  data = frameBuffer->getBuffer(frameBuffer->getRect(), &stride);
-  newBuffer->imageRect(rect, data, stride);
-
-  // Black out any new areas
-
-  if (newBuffer->width() > frameBuffer->width()) {
-    rect.setXYWH(frameBuffer->width(), 0,
-                 newBuffer->width() - frameBuffer->width(),
-                 newBuffer->height());
-    newBuffer->fillRect(rect, black);
+    frameBuffer = createFramebuffer(w, h);
+    assert(frameBuffer);
+    cc->setFramebuffer(frameBuffer);
   }
 
-  if (newBuffer->height() > frameBuffer->height()) {
-    rect.setXYWH(0, frameBuffer->height(),
-                 newBuffer->width(),
-                 newBuffer->height() - frameBuffer->height());
-    newBuffer->fillRect(rect, black);
-  }
-
-  delete frameBuffer;
-  frameBuffer = newBuffer;
-
-end:
   Fl_Widget::resize(x, y, w, h);
 }
 
diff --git a/vncviewer/Viewport.h b/vncviewer/Viewport.h
index 387ff03..f73a27d 100644
--- a/vncviewer/Viewport.h
+++ b/vncviewer/Viewport.h
@@ -46,9 +46,6 @@
   // Flush updates to screen
   void updateWindow();
 
-  // Return a pointer to the framebuffer for decoders to write into
-  rfb::ModifiablePixelBuffer* getFramebuffer(void);
-
   // New image for the locally rendered cursor
   void setCursor(int width, int height, const rfb::Point& hotspot,
                  void* data, void* mask);