Index: /trunk/synergy/Makefile.kmk
===================================================================
--- /trunk/synergy/Makefile.kmk	(revision 2773)
+++ /trunk/synergy/Makefile.kmk	(revision 2774)
@@ -243,5 +243,5 @@
 synrgyhk_ASTOOL.os2 = NASM
 synrgyhk_ASFLAGS.os2 = -f obj
-synrgyhk_CXXFLAGS.os2 = -fno-exceptions 
+synrgyhk_CXXFLAGS.os2 = -fno-exceptions
 synrgyhk_LDFLAGS.os2 = -nostdlib -los2 -v -lend
 synrgyhk_SOURCES.nt = \
@@ -252,3 +252,20 @@
 	lib/platform/CPMSynergyHook.def
 
+
+#
+# A debugging testcase.
+#
+PROGRAMS += syntest
+syntest_SOURCES = syntest.cpp
+syntest_LIBS = \
+	$(TARGET_platform) \
+	$(TARGET_synergy) \
+	$(TARGET_net) \
+	$(TARGET_base) \
+	$(TARGET_io) \
+	$(TARGET_mt) \
+	$(TARGET_arch) \
+	$(TARGET_common)
+
+
 include $(PATH_KBUILD)/footer.kmk
Index: /trunk/synergy/lib/platform/CPMClipboard.cpp
===================================================================
--- /trunk/synergy/lib/platform/CPMClipboard.cpp	(revision 2773)
+++ /trunk/synergy/lib/platform/CPMClipboard.cpp	(revision 2774)
@@ -43,21 +43,9 @@
 
 bool
-CPMClipboard::emptyUnowned()
+CPMClipboard::empty()
 {
     LOG((CLOG_DEBUG "empty clipboard"));
-
-    // empty the clipboard (and take ownership)
     if (!WinEmptyClipbrd(CPMUtil::getHAB())) {
-        LOG((CLOG_DEBUG "failed to grab clipboard"));
-        return false;
-    }
-
-    return true;
-}
-
-bool
-CPMClipboard::empty()
-{
-    if (!emptyUnowned()) {
+        LOG((CLOG_ERR "failed to empty the clipboard"));
         return false;
     }
@@ -70,5 +58,10 @@
         return false;
     }
-    return !!WinSetClipbrdData(CPMUtil::getHAB(), (ULONG)pv, getOwnershipFormat(), CFI_POINTER);
+    if (!WinSetClipbrdData(CPMUtil::getHAB(), (ULONG)pv, getOwnershipFormat(), CFI_POINTER)) {
+        LOG((CLOG_ERR "failed to set dummy clipboard data. %#x", WinGetLastError(CPMUtil::getHAB())));
+        DosFreeMem(pv);
+        return false;
+    }
+    return true;
 }
 
@@ -92,4 +85,6 @@
                 if (!WinSetClipbrdData(CPMUtil::getHAB(), ulPMData, ulPMFormat, ulPMFormatInfo)) {
                     // free converted data if we couldn't put it on the clipboard
+                    LOG((CLOG_ERR "failed to set clipboard data ulPMData=%#lx ulPMFormat=%#lx ulPMFormatInfo=%#lx. %#x",
+                         ulPMData, ulPMFormat, ulPMFormatInfo, WinGetLastError(CPMUtil::getHAB())));
                     converter->freePMData(ulPMData);
                 }
@@ -108,7 +103,5 @@
         return false;
     }
-
     m_time = time;
-
     return true;
 }
@@ -118,5 +111,7 @@
 {
     LOG((CLOG_DEBUG "close clipboard"));
-    WinCloseClipbrd(CPMUtil::getHAB());
+    if (!WinCloseClipbrd(CPMUtil::getHAB())) {
+        LOG((CLOG_WARN "failed to close the clipboard. %#x", WinGetLastError(CPMUtil::getHAB())));
+    }
 }
 
@@ -130,15 +125,19 @@
 CPMClipboard::has(EFormat format) const
 {
+    LOG((CLOG_DEBUG "has clipboard format=%d?", format));
     for (ConverterList::const_iterator index = m_converters.begin();
          index != m_converters.end();
          ++index) {
         IPMClipboardConverter* converter = *index;
+        LOG((CLOG_DEBUG2 "CPMClipboard::has: format=%d convert->getFormat()->%d (pmformat=%d)", format, converter->getFormat(), converter->getPMFormat()));
         if (converter->getFormat() == format) {
             ULONG fFmtInfo;
-            if (WinQueryClipbrdFmtInfo(CPMUtil::getHAB(), getOwnershipFormat(), &fFmtInfo)) {
+            if (WinQueryClipbrdFmtInfo(CPMUtil::getHAB(), converter->getPMFormat(), &fFmtInfo)) {
+                LOG((CLOG_DEBUG "yes, we have format %d!", format));
                 return true;
             }
         }
     }
+    LOG((CLOG_DEBUG "sorry, no we haven't."));
     return false;
 }
@@ -147,4 +146,6 @@
 CPMClipboard::get(EFormat format) const
 {
+    LOG((CLOG_DEBUG "get clipboard format=%d?", format));
+
     // find the converter for the first clipboard format we can handle
     IPMClipboardConverter* converter = NULL;
@@ -155,4 +156,5 @@
              ++index) {
             converter = *index;
+            LOG((CLOG_DEBUG2 "CPMClipboard::get: pmFormat=%d format=%d ?=? %d:%d", pmFormat, format, converter->getPMFormat(), converter->getFormat()));
             if (converter->getPMFormat() == pmFormat &&
                 converter->getFormat()   == format) {
@@ -172,9 +174,12 @@
     ULONG ulData = WinQueryClipbrdData(CPMUtil::getHAB(), converter->getPMFormat());
     if (ulData == 0) {
+        LOG((CLOG_DEBUG "CPMClipboard::get: ulData=%#x\n", ulData));
         return CString();
     }
 
     // convert
-    return converter->toIClipboard(ulData);
+    CString str = converter->toIClipboard(ulData);
+    LOG((CLOG_DEBUG2 "CPMClipboard::get: converted ulData=%lx to '%s'\n", ulData, str.c_str()));
+    return str;
 }
 
@@ -194,5 +199,10 @@
 {
     ULONG fFmtInfo;
-    return (!!WinQueryClipbrdFmtInfo(CPMUtil::getHAB(), getOwnershipFormat(), &fFmtInfo));
+    if (WinQueryClipbrdFmtInfo(CPMUtil::getHAB(), getOwnershipFormat(), &fFmtInfo)) {
+        LOG((CLOG_DEBUG "isOwnedBySynergy: yes"));
+        return true;
+    }
+    LOG((CLOG_DEBUG "isOwnedBySynergy: no"));
+    return false;
 }
 
Index: /trunk/synergy/lib/platform/CPMClipboard.h
===================================================================
--- /trunk/synergy/lib/platform/CPMClipboard.h	(revision 2773)
+++ /trunk/synergy/lib/platform/CPMClipboard.h	(revision 2774)
@@ -31,19 +31,4 @@
     CPMClipboard(HWND window);
     virtual ~CPMClipboard();
-
-    //! Empty clipboard without ownership
-    /*!
-    Take ownership of the clipboard and clear all data from it.
-    This must be called between a successful open() and close().
-    Return false if the clipboard ownership could not be taken;
-    the clipboard should not be emptied in this case.  Unlike
-    empty(), isOwnedBySynergy() will return false when emptied
-    this way.  This is useful when synergy wants to put data on
-    clipboard but pretend (to itself) that some other app did it.
-    When using empty(), synergy assumes the data came from the
-    server and doesn't need to be sent back.  emptyUnowned()
-    makes synergy send the data to the server.
-    */
-    bool                emptyUnowned();
 
     //! Test if clipboard is owned by synergy
Index: /trunk/synergy/lib/platform/CPMClipboardAnyTextConverter.cpp
===================================================================
--- /trunk/synergy/lib/platform/CPMClipboardAnyTextConverter.cpp	(revision 2773)
+++ /trunk/synergy/lib/platform/CPMClipboardAnyTextConverter.cpp	(revision 2774)
@@ -3,9 +3,9 @@
  * Copyright (C) 2002 Chris Schoeneman
  * Copyright (C) 2006 Knut St. Osmundsen
- * 
+ *
  * This package is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * found in the file COPYING that should have accompanied this file.
- * 
+ *
  * This package is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -15,4 +15,5 @@
 
 #include "CPMClipboardAnyTextConverter.h"
+#include "CLog.h"
 
 //
@@ -50,12 +51,14 @@
 	// copy to memory handle
     PVOID pv = NULL;
-    APIRET rc = DosAllocSharedMem(&pv, NULL, cb, PAG_READ | PAG_WRITE | PAG_COMMIT | OBJ_GETTABLE | OBJ_GIVEABLE);
+    APIRET rc = DosAllocSharedMem(&pv, NULL, cb + 1, PAG_READ | PAG_WRITE | PAG_COMMIT | OBJ_GETTABLE | OBJ_GIVEABLE);
     if (rc == NO_ERROR) {
-        return (ULONG)memcpy(pv, text.data(), cb);
+		const char *psz = text.c_str();
+LOG((CLOG_DEBUG "fromIClipboard: psz='%s' cb=%d strlen=%d\n", psz, cb, strlen(psz)));
+        return (ULONG)memcpy(pv, psz, cb);
     }
     return 0;
 }
 
-void 
+void
 CPMClipboardAnyTextConverter::freePMData(ULONG ulPMData) const
 {
@@ -68,5 +71,9 @@
 	// convert text
     const char *psz = (const char *)data;
-	CString text = doToIClipboard(CString(psz));
+LOG((CLOG_DEBUG "toIClipboard: psz='%s' strlen=%d\n", psz, strlen(psz)));
+	CString raw = CString(psz);
+LOG((CLOG_DEBUG "toIClipboard: raw='%s'\n", raw.c_str()));
+	CString text = doToIClipboard(raw);
+LOG((CLOG_DEBUG "toIClipboard: text='%s'\n", text.c_str()));
 
 	// convert newlines
@@ -77,5 +84,5 @@
 CPMClipboardAnyTextConverter::convertLinefeedToPM(const CString& src) const
 {
-	// note -- we assume src is a valid UTF-8 string
+	// note -- we assume src is a valid UTF-8 string    L
 
 	// count newlines in string
Index: /trunk/synergy/lib/platform/CPMScreen.cpp
===================================================================
--- /trunk/synergy/lib/platform/CPMScreen.cpp	(revision 2773)
+++ /trunk/synergy/lib/platform/CPMScreen.cpp	(revision 2774)
@@ -547,10 +547,9 @@
     qmsg.ptl.y = 0;
     qmsg.reserved = 0;
-LOG((CLOG_DEBUG "fakeMessage: msg=%#lx mp1=%#lx mp2=%#lx...", msg, mp1, mp2));
+    LOG((CLOG_DEBUG "fakeMessage: msg=%#lx mp1=%#lx mp2=%#lx...", msg, mp1, mp2));
     const char *pszError = m_fakeMsg(m_hab, &qmsg);
     if (pszError) {
         LOG((CLOG_CRIT "fakeMessage: msg=%#lx mp1=%#lx mp2=%#lx failed: %s", msg, mp1, mp2, pszError));
     }
-else LOG((CLOG_DEBUG "fakeMessage: done"));
 }
 
@@ -624,5 +623,5 @@
     POINTL ptl;
     if (WinQueryPointerPos(HWND_DESKTOP, &ptl)) {
-        LOG((CLOG_DEBUG "fakeMouseRelativeMove: %d,%d (%d,%d +/- %d,%d)", ptl.x + dx, ptl.y - dy, ptl.y, ptl.x, dx, dy));
+        LOG((CLOG_DEBUG1 "fakeMouseRelativeMove: %d,%d (%d,%d +/- %d,%d)", ptl.x + dx, ptl.y - dy, ptl.y, ptl.x, dx, dy));
         fakeMouseMove(ptl.x + dx, ptl.y - dy - m_cy);
         return;
@@ -762,5 +761,5 @@
         throw XScreenOpenFailure();
     }
-    LOG((CLOG_DEBUG1 "openHookLibrary: hmod=%#lx '%s'", hmod, name));
+    LOG((CLOG_DEBUG2 "openHookLibrary: hmod=%#lx '%s'", hmod, name));
     return hmod;
 }
@@ -769,5 +768,5 @@
 CPMScreen::closeHookLibrary(HMODULE hmod) const
 {
-    LOG((CLOG_DEBUG1 "closeHookLibrary: hmod=%#lx\n", hmod));
+    LOG((CLOG_DEBUG2 "closeHookLibrary: hmod=%#lx\n", hmod));
     if (hmod != NULL) {
         m_cleanup(m_hab);
@@ -911,5 +910,23 @@
 {
     switch (msg) {
-    case WM_DRAWCLIPBOARD:
+
+        //
+        // Just freak out and close the clipboard if we get any of these.
+        //
+    case WM_RENDERFMT:
+    case WM_RENDERALLFMTS:
+    case WM_PAINTCLIPBOARD:
+    case WM_SIZECLIPBOARD:
+    case WM_HSCROLLCLIPBOARD:
+    case WM_VSCROLLCLIPBOARD:
+        LOG((CLOG_DEBUG "msg=%#x -> closing clipboard.\n", msg));
+        WinCloseClipbrd(m_hab);
+        break;
+
+        //
+        // The content has changed.
+        //
+    //case WM_DESTROYCLIPBOARD:
+	case WM_DRAWCLIPBOARD:
         //// first pass on the message
         //if (m_nextClipboardWindow != NULL) {
Index: /trunk/synergy/syntest.cpp
===================================================================
--- /trunk/synergy/syntest.cpp	(revision 2774)
+++ /trunk/synergy/syntest.cpp	(revision 2774)
@@ -0,0 +1,52 @@
+/*
+ * Testcase used while debugging various bits of the code.
+ */
+#include "XBase.h"
+#include "XArch.h"
+#include "CArch.h"
+#include "CUnicode.h"
+#include "CLog.h"
+#include "Version.h"
+#include <stdio.h>
+
+
+
+int test1()
+{
+    CString text = CString("Ah! Megami-Sama!");
+    CString utf8Text = CUnicode::textToUTF8(text);
+    printf("utf8Text='%s'\n", utf8Text.c_str());
+    return 0;
+}
+
+
+int
+main(int argc, char** argv)
+{
+    try
+    {
+        CArch arch;
+        CLOG;
+
+        test1();
+
+        delete CLOG;
+        return 0;
+    }
+    catch (XBase& e)
+    {
+        printf("Uncaught exception: %s\n", e.what());
+        throw;
+    }
+    catch (XArch& e)
+    {
+        printf("Initialization failed: %s\n", e.what().c_str());
+        return kExitFailed;
+    }
+    catch (...)
+    {
+        printf("Uncaught exception: <unknown exception>\n");
+        throw;
+    }
+}
+
