Bläddra i källkod

added timer delay to process draw clipboard messages
[SAB]


git-svn-id: svn://svn.code.sf.net/p/ditto-cp/code/trunk@199 595ec19a-5cb4-439b-94a8-42fb3063c22c

sabrogden 20 år sedan
förälder
incheckning
bc85027a74
6 ändrade filer med 150 tillägg och 94 borttagningar
  1. 2 2
      CP_Main.cpp
  2. 17 2
      Clip.cpp
  3. 37 33
      ClipboardViewer.cpp
  4. 1 0
      ClipboardViewer.h
  5. 81 47
      Misc.cpp
  6. 12 10
      Misc.h

+ 2 - 2
CP_Main.cpp

@@ -85,7 +85,7 @@ CCP_MainApp::~CCP_MainApp()
 
 BOOL CCP_MainApp::InitInstance()
 {
-	LOG("InitInstance");
+	Log("InitInstance");
 
 	AfxEnableControlContainer();
 
@@ -681,7 +681,7 @@ void CCP_MainApp::Delayed_RemoveOldEntries( UINT delay )
 
 int CCP_MainApp::ExitInstance() 
 {
-	LOG("ExitInstance");
+	Log("ExitInstance");
 
 	CloseDB();
 

+ 17 - 2
Clip.cpp

@@ -27,7 +27,7 @@ HGLOBAL COleDataObjectEx::GetGlobalData(CLIPFORMAT cfFormat, LPFORMATETC lpForma
 	{
 		if(!::IsValid(hGlobal))
 		{
-			LOG( StrF(
+			Log( StrF(
 				"COleDataObjectEx::GetGlobalData(\"%s\"): ERROR: Invalid (NULL) data returned.",
 				GetFormatName(cfFormat) ) );
 			::GlobalFree( hGlobal );
@@ -78,7 +78,7 @@ HGLOBAL COleDataObjectEx::GetGlobalData(CLIPFORMAT cfFormat, LPFORMATETC lpForma
 	
 	if(hGlobal && !::IsValid(hGlobal))
 	{
-		LOG( StrF(
+		Log( StrF(
 			"COleDataObjectEx::GetGlobalData(\"%s\"): ERROR: Invalid (NULL) data returned.",
 			GetFormatName(cfFormat)));
 		::GlobalFree(hGlobal);
@@ -240,12 +240,19 @@ bool CClip::AddFormat(CLIPFORMAT cfType, void* pData, UINT nLen)
 	return true;
 }
 
+bool g_bCopyingClipboard = false; // for debugging reentrance
+
 // Fills this CClip with the contents of the clipboard.
 bool CClip::LoadFromClipboard(CClipTypes* pClipTypes)
 {
 	COleDataObjectEx oleData;
 	CClipTypes defaultTypes;
 	CClipTypes* pTypes = pClipTypes;
+
+	if( g_bCopyingClipboard )
+		return false;
+	
+	g_bCopyingClipboard = true;
 	
 	// m_Formats should be empty when this is called.
 	ASSERT(m_Formats.GetSize() == 0);
@@ -253,12 +260,17 @@ bool CClip::LoadFromClipboard(CClipTypes* pClipTypes)
 	// If the data is supposed to be private, then return
 	if(::IsClipboardFormatAvailable(theApp.m_cfIgnoreClipboard))
 	{
+		g_bCopyingClipboard = false;
+
 		return false;
 	}
+
+	Sleep(2000);
 	
 	//Attach to the clipboard
 	if(!oleData.AttachClipboard())
 	{
+		g_bCopyingClipboard = false;
 		ASSERT(0); // does this ever happen?
 		return false;
 	}
@@ -330,6 +342,7 @@ bool CClip::LoadFromClipboard(CClipTypes* pClipTypes)
 					Log(cs);
 
 					oleData.Release();
+					g_bCopyingClipboard = false;
 					return false;
 				}
 
@@ -365,9 +378,11 @@ bool CClip::LoadFromClipboard(CClipTypes* pClipTypes)
 	
 	if(m_Formats.GetSize() == 0)
 	{
+		g_bCopyingClipboard = false;
 		return false;
 	}
 	
+	g_bCopyingClipboard = false;
 	return true;
 }
 

+ 37 - 33
ClipboardViewer.cpp

@@ -181,47 +181,30 @@ void CClipboardViewer::OnDrawClipboard()
 		return;
 	}
 
-	Log("Start of OnDrawClipboard");
-	bool bPassOn = false;
-	
-	if((GetTickCount() - m_lLastCopy) > g_Opt.m_lSaveClipDelay)
+	// don't process the event when we first attach
+	if(m_pHandler && !m_bCalling_SetClipboardViewer)
 	{
-		// don't process the event when we first attach
-		if(m_pHandler && !m_bCalling_SetClipboardViewer)
+		if(!::IsClipboardFormatAvailable(theApp.m_cfIgnoreClipboard))
 		{
-			if(!::IsClipboardFormatAvailable(theApp.m_cfIgnoreClipboard))
-			{
-				m_pHandler->OnClipboardChange();
-
-				bPassOn = true;
-				m_lLastCopy = GetTickCount();
-			}
+			Log(StrF("OnDrawClipboard::SetTimer %d", GetTickCount()));
+			
+			KillTimer(TIMER_DRAW_CLIPBOARD);
+			SetTimer(TIMER_DRAW_CLIPBOARD, g_Opt.m_lProcessDrawClipboardDelay, NULL);		
 		}
 	}
-	else
-	{
-		Log(StrF("Clip copy to fast difference from last copy = %d", (GetTickCount() - m_lLastCopy)));
-	}
-	
-	if(bPassOn)
+
+	// pass the event to the next Clipboard viewer in the chain
+	if(m_hNextClipboardViewer != NULL)
 	{
-		// pass the event to the next Clipboard viewer in the chain
-		if(m_hNextClipboardViewer != NULL)
+		if(m_hNextClipboardViewer != m_hWnd)
 		{
-			if(m_hNextClipboardViewer != m_hWnd)
-			{
-				Log(StrF("OnDrawClipboard Sending WM_DRAWCLIPBOARD to %d", m_hNextClipboardViewer));
-
-				::SendMessage(m_hNextClipboardViewer, WM_DRAWCLIPBOARD, 0, 0);	
-			}
-			else
-			{
-				m_hNextClipboardViewer = NULL;
-			}
+			::SendMessage(m_hNextClipboardViewer, WM_DRAWCLIPBOARD, 0, 0);	
+		}
+		else
+		{
+			m_hNextClipboardViewer = NULL;
 		}
 	}
-
-	Log("End of OnDrawClipboard");
 }
 
 void CClipboardViewer::OnTimer(UINT nIDEvent) 
@@ -231,6 +214,27 @@ void CClipboardViewer::OnTimer(UINT nIDEvent)
 	case TIMER_ENSURE_VIEWER_IN_CHAIN:
 		EnsureConnected();
 		break;
+
+	case TIMER_DRAW_CLIPBOARD:
+		KillTimer(nIDEvent);
+
+		if((GetTickCount() - m_lLastCopy) > g_Opt.m_lSaveClipDelay)
+		{
+			if(!::IsClipboardFormatAvailable(theApp.m_cfIgnoreClipboard))
+			{
+				Log(StrF("OnDrawClipboard::OnTimer %d", GetTickCount()));
+
+				m_pHandler->OnClipboardChange();
+
+				m_lLastCopy = GetTickCount();
+			}
+		}
+		else
+		{
+			Log(StrF("Clip copy to fast difference from last copy = %d", (GetTickCount() - m_lLastCopy)));
+		}
+
+		break;
 	}
 	
 	CWnd::OnTimer(nIDEvent);

+ 1 - 0
ClipboardViewer.h

@@ -7,6 +7,7 @@
 
 
 #define TIMER_ENSURE_VIEWER_IN_CHAIN	6
+#define TIMER_DRAW_CLIPBOARD			7
 
 class CClipboardViewer : public CWnd
 {

+ 81 - 47
Misc.cpp

@@ -55,17 +55,24 @@ void AppendToFile( const char* fn, const char* msg )
 	fclose(file);
 }
 
-void Log( const char* msg )
+void Log(const char* msg, bool bFromSendRecieve)
 {
-	ASSERT( AfxIsValidString(msg) );
+	ASSERT(AfxIsValidString(msg));
 	CTime	time = CTime::GetCurrentTime();
 	CString	csText = time.Format("[%Y/%m/%d %I:%M:%S %p]  ");
-	//CString	csTemp;
-	//	csTemp.Format( "%04x  ", AfxGetInstanceHandle() );
+	
 	csText += msg;
 	csText += "\n";
 	TRACE(csText);
 
+#ifndef _DEBUG
+	if(!bFromSendRecieve)
+	{
+		if(!g_Opt.m_bEnableDebugLogging)
+			return;
+	}
+#endif
+	
 	CString csFile = CGetSetOptions::GetExeFileName();
 	csFile = GetFilePath(csFile);
 	csFile += "Ditto.log";
@@ -76,7 +83,7 @@ void Log( const char* msg )
 void LogSendRecieveInfo(CString cs)
 {
 	if(g_Opt.m_bLogSendReceiveErrors)
-		Log(cs);
+		Log(cs, true);
 }
 
 CString GetErrorString( int err )
@@ -494,6 +501,8 @@ BOOL CGetSetOptions::m_bEnsureEntireWindowCanBeSeen;
 BOOL CGetSetOptions::m_bShowAllClipsInMainList;
 long CGetSetOptions::m_lMaxClipSizeInBytes;
 long CGetSetOptions::m_lSaveClipDelay;
+long CGetSetOptions::m_lProcessDrawClipboardDelay;
+BOOL CGetSetOptions::m_bEnableDebugLogging;
 
 CGetSetOptions g_Opt;
 
@@ -528,6 +537,8 @@ CGetSetOptions::CGetSetOptions()
 	m_bShowAllClipsInMainList = GetShowAllClipsInMainList();
 	m_lMaxClipSizeInBytes = GetMaxClipSizeInBytes();
 	m_lSaveClipDelay = GetSaveClipDelay();
+	m_lProcessDrawClipboardDelay = GetProcessDrawClipboardDelay();
+	m_bEnableDebugLogging = GetEnableDebugLogging();
 
 	GetExtraNetworkPassword(true);
 	
@@ -1373,6 +1384,28 @@ void CGetSetOptions::SetSaveClipDelay(long lDelay)
 	SetProfileLong("SaveClipDelay", lDelay);
 }
 
+long CGetSetOptions::GetProcessDrawClipboardDelay()
+{
+	return GetProfileLong("ProcessDrawClipboardDelay", 50);
+}
+
+void CGetSetOptions::SetProcessDrawClipboardDelay(long lDelay)
+{
+	m_lProcessDrawClipboardDelay = lDelay;
+	SetProfileLong("ProcessDrawClipboardDelay", lDelay);
+}
+
+BOOL CGetSetOptions::GetEnableDebugLogging()
+{
+	return GetProfileLong("EnableDebugLogging", FALSE);
+}
+
+void CGetSetOptions::SetEnableDebugLogging(BOOL bEnable)
+{
+	m_bEnableDebugLogging = bEnable;
+	SetProfileLong("EnableDebugLogging", bEnable);
+}
+
 /*------------------------------------------------------------------*\
 CHotKey - a single system-wide hotkey
 \*------------------------------------------------------------------*/
@@ -1470,15 +1503,15 @@ UINT CHotKey::GetModifier(DWORD dwHotKey)
 
 bool CHotKey::Register()
 {
-	if( m_Key )
+	if(m_Key)
 	{
 		if(m_bIsRegistered == false)
 		{
-			ASSERT( g_HotKeys.m_hWnd );
-			m_bIsRegistered = ::RegisterHotKey(	g_HotKeys.m_hWnd,
-				m_Atom,
-				GetModifier(),
-				LOBYTE(m_Key) ) == TRUE;
+			ASSERT(g_HotKeys.m_hWnd);
+			m_bIsRegistered = ::RegisterHotKey(g_HotKeys.m_hWnd,
+												m_Atom,
+												GetModifier(),
+												LOBYTE(m_Key) ) == TRUE;
 		}
 	}
 	else
@@ -1488,7 +1521,7 @@ bool CHotKey::Register()
 }
 bool CHotKey::Unregister(bool bOnShowingDitto)
 {
-	if( !m_bIsRegistered )
+	if(!m_bIsRegistered)
 		return true;
 	
 	if(bOnShowingDitto)
@@ -1500,14 +1533,14 @@ bool CHotKey::Unregister(bool bOnShowingDitto)
 	if(m_Key)
 	{
 		ASSERT(g_HotKeys.m_hWnd);
-		if( ::UnregisterHotKey( g_HotKeys.m_hWnd, m_Atom ) )
+		if(::UnregisterHotKey( g_HotKeys.m_hWnd, m_Atom))
 		{
 			m_bIsRegistered = false;
 			return true;
 		}
 		else
 		{
-			LOG("Unregister" "FAILED!");
+			Log("Unregister" "FAILED!");
 			ASSERT(0);
 		}
 	}
@@ -1532,10 +1565,10 @@ CHotKeys::~CHotKeys()
 {
 	CHotKey* pHotKey;
 	int count = GetSize();
-	for( int i=0; i < count; i++ )
+	for(int i=0; i < count; i++)
 	{
 		pHotKey = ElementAt(i);
-		if( pHotKey )
+		if(pHotKey)
 			delete pHotKey;
 	}
 }
@@ -1543,7 +1576,7 @@ CHotKeys::~CHotKeys()
 int CHotKeys::Find( CHotKey* pHotKey )
 {
 	int count = GetSize();
-	for( int i=0; i < count; i++ )
+	for(int i=0; i < count; i++)
 	{
 		if( pHotKey == ElementAt(i) )
 			return i;
@@ -1554,7 +1587,7 @@ int CHotKeys::Find( CHotKey* pHotKey )
 bool CHotKeys::Remove( CHotKey* pHotKey )
 {
 	int i = Find(pHotKey);
-	if( i >= 0 )
+	if(i >= 0)
 	{
 		RemoveAt(i);
 		return true;
@@ -1565,31 +1598,31 @@ bool CHotKeys::Remove( CHotKey* pHotKey )
 void CHotKeys::LoadAllKeys()
 {
 	int count = GetSize();
-	for( int i=0; i < count; i++ )
+	for(int i=0; i < count; i++)
 		ElementAt(i)->LoadKey();
 }
 
 void CHotKeys::SaveAllKeys()
 {
 	int count = GetSize();
-	for( int i=0; i < count; i++ )
+	for(int i=0; i < count; i++)
 		ElementAt(i)->SaveKey();
 }
 
-void CHotKeys::RegisterAll( bool bMsgOnError )
+void CHotKeys::RegisterAll(bool bMsgOnError)
 {
 	CString str;
 	CHotKey* pHotKey;
 	int count = GetSize();
-	for( int i=0; i < count; i++ )
+	for(int i = 0; i < count; i++)
 	{
 		pHotKey = ElementAt(i);
-		if( !pHotKey->Register() )
+		if(!pHotKey->Register())
 		{
 			str =  "Error Registering ";
 			str += pHotKey->GetName();
-			LOG( str );
-			if( bMsgOnError )
+			Log(str);
+			if(bMsgOnError)
 				AfxMessageBox(str);
 		}
 	}
@@ -1600,53 +1633,54 @@ void CHotKeys::UnregisterAll(bool bMsgOnError, bool bOnShowDitto)
 	CString str;
 	CHotKey* pHotKey;
 	int count = GetSize();
-	for( int i=0; i < count; i++ )
+	for(int i = 0; i < count; i++)
 	{
 		pHotKey = ElementAt(i);
 		if(!pHotKey->Unregister(bOnShowDitto))
 		{
-			str =  "Error Unregistering ";
+			str = "Error Unregistering ";
 			str += pHotKey->GetName();
-			LOG( str );
-			if( bMsgOnError )
+			Log(str);
+			if(bMsgOnError)
 				AfxMessageBox(str);
 		}
 	}
 }
 
-void CHotKeys::GetKeys( ARRAY& keys )
+void CHotKeys::GetKeys(ARRAY& keys)
 {
 	int count = GetSize();
-	keys.SetSize( count );
-	for( int i=0; i < count; i++ )
+	keys.SetSize(count);
+	for(int i=0; i < count; i++)
 		keys[i] = ElementAt(i)->GetKey();
 }
 
 // caution! this alters hotkeys based upon corresponding indexes
-void CHotKeys::SetKeys( ARRAY& keys, bool bSave )
+void CHotKeys::SetKeys(ARRAY& keys, bool bSave)
 {
 	int count = GetSize();
-	ASSERT( count == keys.GetSize() );
-	for( int i=0; i < count; i++ )
-		ElementAt(i)->SetKey( keys[i], bSave );
+	ASSERT(count == keys.GetSize());
+	for(int i=0; i < count; i++)
+		ElementAt(i)->SetKey(keys[i], bSave);
 }
 
-bool CHotKeys::FindFirstConflict( ARRAY& keys, int* pX, int* pY )
+bool CHotKeys::FindFirstConflict(ARRAY& keys, int* pX, int* pY)
 {
 	bool bConflict = false;
 	int i, j;
 	int count = keys.GetSize();
 	DWORD key;
-	for( i=0; i < count && !bConflict; i++ )
+	for(i = 0; i < count && !bConflict; i++)
 	{
 		key = keys.ElementAt(i);
 		// only check valid keys
-		if( key == 0 )
+		if(key == 0)
 			continue;
+
 		// scan the array for a duplicate
-		for( j=i+1; j < count; j++ )
+		for(j = i+1; j < count; j++ )
 		{
-			if( keys.ElementAt(j) == key )
+			if(keys.ElementAt(j) == key)
 			{
 				bConflict = true;
 				break;
@@ -1654,11 +1688,11 @@ bool CHotKeys::FindFirstConflict( ARRAY& keys, int* pX, int* pY )
 		}
 	}
 	
-	if( bConflict )
+	if(bConflict)
 	{
-		if( pX )
+		if(pX)
 			*pX = i-1;
-		if( pY )
+		if(pY)
 			*pY = j;
 	}
 	
@@ -1666,11 +1700,11 @@ bool CHotKeys::FindFirstConflict( ARRAY& keys, int* pX, int* pY )
 }
 
 // if true, pX and pY (if valid) are set to the indexes of the conflicting hotkeys.
-bool CHotKeys::FindFirstConflict( int* pX, int* pY )
+bool CHotKeys::FindFirstConflict(int* pX, int* pY)
 {
 	ARRAY keys;
-	GetKeys( keys );
-	return FindFirstConflict( keys, pX, pY );
+	GetKeys(keys);
+	return FindFirstConflict(keys, pX, pY);
 }
 
 /****************************************************************************************************

+ 12 - 10
Misc.h

@@ -12,21 +12,14 @@
 
 #include "DatabaseUtilities.h"
 
-// Debugging
-#ifdef _DEBUG
-#define LOG(x)		Log(x)
-#else
-#define LOG(x)
-#endif
-
 CString GetIPAddress();
 CString GetComputerName();
 
 #define FUNC		__FUNCTION__
 #define FUNCSIG		__FUNCSIG__
-void AppendToFile( const char* fn, const char *msg );
-void Log( const char* msg );
-CString GetErrorString( int err );
+void AppendToFile(const char* fn, const char *msg);
+void Log(const char* msg, bool bFromSendRecieve = false);
+CString GetErrorString(int err);
 
 #define MS_VC_EXCEPTION 0x406d1388
 typedef struct tagTHREADNAME_INFO
@@ -356,6 +349,15 @@ public:
 	static long		GetSaveClipDelay();
 	static void		SetSaveClipDelay(long lDelay);
 
+	static long		m_lProcessDrawClipboardDelay;
+	static long		GetProcessDrawClipboardDelay();
+	static void		SetProcessDrawClipboardDelay(long lDelay);
+
+	static BOOL		m_bEnableDebugLogging;
+	static BOOL		GetEnableDebugLogging();
+	static void		SetEnableDebugLogging(BOOL bEnable);
+
+
 	static CStringArray m_csNetworkPasswordArray;
 
 	/*