1
0

CopyThread.cpp 4.8 KB

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