CopyThread.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // CopyThread.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "cp_main.h"
  5. #include "CopyThread.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CCopyThread
  13. IMPLEMENT_DYNCREATE(CCopyThread, CWinThread)
  14. CCopyThread::CCopyThread():
  15. m_bQuit(false),
  16. m_bConfigChanged(false),
  17. m_pClips(NULL),
  18. m_pClipboardViewer(NULL)
  19. {
  20. m_bAutoDelete = false,
  21. ::InitializeCriticalSection(&m_CS);
  22. }
  23. CCopyThread::~CCopyThread()
  24. {
  25. m_LocalConfig.DeleteTypes();
  26. m_SharedConfig.DeleteTypes();
  27. DELETE_PTR(m_pClipboardViewer);
  28. DELETE_PTR(m_pClips);
  29. ::DeleteCriticalSection(&m_CS);
  30. }
  31. BOOL CCopyThread::InitInstance()
  32. {
  33. m_pClipboardViewer = new CClipboardViewer(this);
  34. // the window is created within this thread and therefore uses its message queue
  35. m_pClipboardViewer->Create();
  36. return TRUE;
  37. }
  38. int CCopyThread::ExitInstance()
  39. {
  40. m_pClipboardViewer->Disconnect(false);
  41. return CWinThread::ExitInstance();
  42. }
  43. BEGIN_MESSAGE_MAP(CCopyThread, CWinThread)
  44. //{{AFX_MSG_MAP(CCopyThread)
  45. // NOTE - the ClassWizard will add and remove mapping macros here.
  46. //}}AFX_MSG_MAP
  47. END_MESSAGE_MAP()
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CCopyThread message handlers
  50. // Called within Copy Thread:
  51. void CCopyThread::OnClipboardChange()
  52. {
  53. SyncConfig(); // synchronize with the main thread's copy configuration
  54. // if we are told not to copy on change, then we have nothing to do.
  55. if(!m_LocalConfig.m_bCopyOnChange)
  56. return;
  57. CClip* pClip = new CClip;
  58. bool bResult = pClip->LoadFromClipboard(m_LocalConfig.m_pSupportedTypes);
  59. if(!bResult)
  60. {
  61. delete pClip;
  62. return; // error
  63. }
  64. AddToClips(pClip);
  65. if(m_LocalConfig.m_bAsyncCopy)
  66. ::PostMessage(m_LocalConfig.m_hClipHandler, WM_CLIPBOARD_COPIED, (WPARAM)pClip, 0);
  67. else
  68. ::SendMessage(m_LocalConfig.m_hClipHandler, WM_CLIPBOARD_COPIED, (WPARAM)pClip, 0);
  69. }
  70. void CCopyThread::SyncConfig()
  71. {
  72. // atomic read
  73. if(m_bConfigChanged)
  74. {
  75. CClipTypes* pTypes = NULL;
  76. Hold();
  77. pTypes = m_LocalConfig.m_pSupportedTypes;
  78. m_LocalConfig = m_SharedConfig;
  79. // NULL means that it shouldn't have been sync'ed
  80. if( m_SharedConfig.m_pSupportedTypes == NULL )
  81. { // let m_LocalConfig keep its types
  82. m_LocalConfig.m_pSupportedTypes = pTypes; // undo sync
  83. pTypes = NULL; // nothing to delete
  84. }
  85. else
  86. m_SharedConfig.m_pSupportedTypes = NULL; // now owned by LocalConfig
  87. Release();
  88. // delete old types
  89. if( pTypes )
  90. delete pTypes;
  91. }
  92. }
  93. void CCopyThread::AddToClips(CClip* pClip)
  94. {
  95. Hold();
  96. if(!m_pClips)
  97. m_pClips = new CClipList;
  98. m_pClips->AddTail(pClip); // m_pClips now owns pClip
  99. Release();
  100. }
  101. bool CCopyThread::IsClipboardViewerConnected()
  102. {
  103. return m_pClipboardViewer->m_bIsConnected;
  104. }
  105. bool CCopyThread::GetConnectCV()
  106. {
  107. return m_pClipboardViewer->GetConnect();
  108. }
  109. void CCopyThread::SetConnectCV(bool bConnect)
  110. {
  111. ASSERT( m_pClipboardViewer && m_pClipboardViewer->m_hWnd );
  112. ::SendMessage( m_pClipboardViewer->m_hWnd, WM_SETCONNECT, bConnect, 0 );
  113. }
  114. CClipList* CCopyThread::GetClips()
  115. {
  116. Hold();
  117. CClipList* pRet = m_pClips;
  118. m_pClips = NULL;
  119. Release();
  120. return pRet;
  121. }
  122. void CCopyThread::SetSupportedTypes( CClipTypes* pTypes )
  123. {
  124. Hold();
  125. if(m_SharedConfig.m_pSupportedTypes)
  126. {
  127. DELETE_PTR(m_SharedConfig.m_pSupportedTypes);
  128. }
  129. m_SharedConfig.m_pSupportedTypes = pTypes;
  130. m_bConfigChanged = true;
  131. Release();
  132. }
  133. HWND CCopyThread::SetClipHandler(HWND hWnd)
  134. {
  135. Hold();
  136. HWND hRet = m_SharedConfig.m_hClipHandler;
  137. m_SharedConfig.m_hClipHandler = hWnd;
  138. m_bConfigChanged = (hRet != hWnd);
  139. Release();
  140. return hRet;
  141. }
  142. HWND CCopyThread::GetClipHandler()
  143. {
  144. Hold();
  145. HWND hRet = m_SharedConfig.m_hClipHandler;
  146. Release();
  147. return hRet;
  148. }
  149. bool CCopyThread::SetCopyOnChange(bool bVal)
  150. {
  151. Hold();
  152. bool bRet = m_SharedConfig.m_bCopyOnChange;
  153. m_SharedConfig.m_bCopyOnChange = bVal;
  154. m_bConfigChanged = (bRet != bVal);
  155. Release();
  156. return bRet;
  157. }
  158. bool CCopyThread::GetCopyOnChange()
  159. {
  160. Hold();
  161. bool bRet = m_SharedConfig.m_bCopyOnChange;
  162. Release();
  163. return bRet;
  164. }
  165. bool CCopyThread::SetAsyncCopy(bool bVal)
  166. {
  167. Hold();
  168. bool bRet = m_SharedConfig.m_bAsyncCopy;
  169. m_SharedConfig.m_bAsyncCopy = bVal;
  170. m_bConfigChanged = (bRet != bVal);
  171. Release();
  172. return bRet;
  173. }
  174. bool CCopyThread::GetAsyncCopy()
  175. {
  176. Hold();
  177. bool bRet = m_SharedConfig.m_bAsyncCopy;
  178. Release();
  179. return bRet;
  180. }
  181. void CCopyThread::Init(CCopyConfig cfg)
  182. {
  183. ASSERT(m_LocalConfig.m_pSupportedTypes == NULL);
  184. m_LocalConfig = m_SharedConfig = cfg;
  185. // let m_LocalConfig own the m_pSupportedTypes
  186. m_SharedConfig.m_pSupportedTypes = NULL;
  187. }
  188. bool CCopyThread::Quit()
  189. {
  190. m_bQuit = true;
  191. m_pClipboardViewer->PostMessage( WM_QUIT );
  192. return CWinThread::PostThreadMessage( WM_QUIT, NULL, NULL ) != FALSE;
  193. }