ClipboardViewer.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. // ClipboardViewer.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "cp_main.h"
  5. #include "ClipboardViewer.h"
  6. #include "Misc.h"
  7. #include "shared/Tokenizer.h"
  8. #include "WildCardMatch.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CClipboardViewer
  16. CClipboardViewer::CClipboardViewer(CCopyThread* pHandler) :
  17. m_hNextClipboardViewer(0),
  18. m_bCalling_SetClipboardViewer(false),
  19. m_pHandler(pHandler),
  20. m_bPinging(false),
  21. m_bIsConnected(false),
  22. m_bConnect(false),
  23. m_dwLastCopy(0),
  24. m_connectOnStartup(true)
  25. {
  26. m_activeWindowTitle = _T("");
  27. m_activeWindow = _T("");
  28. }
  29. CClipboardViewer::~CClipboardViewer()
  30. {
  31. }
  32. BEGIN_MESSAGE_MAP(CClipboardViewer, CWnd)
  33. //{{AFX_MSG_MAP(CClipboardViewer)
  34. ON_WM_CREATE()
  35. ON_WM_CHANGECBCHAIN()
  36. ON_WM_DRAWCLIPBOARD()
  37. ON_WM_TIMER()
  38. ON_WM_DESTROY()
  39. //}}AFX_MSG_MAP
  40. ON_MESSAGE(WM_SETCONNECT, OnSetConnect)
  41. ON_MESSAGE(WM_CLIPBOARDUPDATE, OnClipboardChange)
  42. END_MESSAGE_MAP()
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CClipboardViewer message handlers
  45. void CClipboardViewer::Create()
  46. {
  47. CString strParentClass = AfxRegisterWndClass(0);
  48. CWnd::CreateEx(0, strParentClass, _T("Ditto Clipboard Viewer"), 0, -1, -1, 0, 0, 0, 0);
  49. if(m_connectOnStartup)
  50. {
  51. SetConnect(true);
  52. }
  53. }
  54. // connects as a clipboard viewer
  55. void CClipboardViewer::Connect()
  56. {
  57. Log(_T("Connect to Clipboard"));
  58. m_bCalling_SetClipboardViewer = true;
  59. bool useSetClipboardWnd = true;
  60. if(IsVista())
  61. {
  62. HMODULE hUser32 = LoadLibrary(_T("USER32.dll"));
  63. if (hUser32)
  64. {
  65. typedef BOOL (__stdcall *AddClipFormatListener)( HWND hwnd );
  66. AddClipFormatListener addListener = (AddClipFormatListener) GetProcAddress(hUser32, "AddClipboardFormatListener");
  67. if(addListener)
  68. {
  69. Log(_T("Connecting to clipboard with function AddClipboardFormatListener"));
  70. useSetClipboardWnd = false;
  71. addListener(m_hWnd);
  72. }
  73. }
  74. }
  75. if(useSetClipboardWnd)
  76. {
  77. Log(_T("Connecting to clipboard with function SetClipboardViewer"));
  78. m_hNextClipboardViewer = CWnd::SetClipboardViewer();
  79. }
  80. m_bCalling_SetClipboardViewer = false;
  81. m_bIsConnected = true;
  82. m_bConnect = true;
  83. SetEnsureConnectedTimer();
  84. }
  85. void CClipboardViewer::SetEnsureConnectedTimer()
  86. {
  87. SetTimer(TIMER_ENSURE_VIEWER_IN_CHAIN, ONE_MINUTE*5, NULL);
  88. }
  89. // disconnects as a clipboard viewer
  90. void CClipboardViewer::Disconnect(bool bSendPing)
  91. {
  92. Log(_T("Disconnect From Clipboard"));
  93. KillTimer(TIMER_ENSURE_VIEWER_IN_CHAIN);
  94. bool removeOldWay = true;
  95. if(IsVista())
  96. {
  97. HMODULE hUser32 = LoadLibrary(_T("USER32.dll"));
  98. if (hUser32)
  99. {
  100. typedef BOOL (__stdcall *RemoveClipFormatListener)( HWND hwnd );
  101. RemoveClipFormatListener removeListener = (RemoveClipFormatListener) GetProcAddress(hUser32, "RemoveClipboardFormatListener");
  102. if(removeListener)
  103. {
  104. Log(_T("Disconnecting from clipboard with function RemoveClipboardFormatListener"));
  105. removeOldWay = false;
  106. removeListener(m_hWnd);
  107. }
  108. }
  109. }
  110. if(removeOldWay)
  111. {
  112. Log(_T("Disconnecting from clipboard with function ChangeClipboardChain"));
  113. BOOL bRet = CWnd::ChangeClipboardChain(m_hNextClipboardViewer);
  114. if(!bRet)
  115. {
  116. Log(_T("Error disconnecting from clipboard"));
  117. bRet = CWnd::ChangeClipboardChain(m_hNextClipboardViewer);
  118. if(!bRet)
  119. {
  120. Log(_T("Error disconnecting from clipboard2"));
  121. }
  122. }
  123. }
  124. m_hNextClipboardViewer = 0;
  125. m_bConnect = false;
  126. m_bIsConnected = false;
  127. if(bSendPing)
  128. SendPing();
  129. }
  130. void CClipboardViewer::SendPing()
  131. {
  132. if(g_Opt.m_bEnsureConnectToClipboard)
  133. {
  134. if(OpenClipboard())
  135. {
  136. m_bPinging = true;
  137. SetClipboardData(theApp.m_PingFormat, NewGlobalP("Ditto Ping", sizeof("Ditto Ping")));
  138. SetClipboardData(theApp.m_cfIgnoreClipboard , NewGlobalP("Ignore", sizeof("Ignore")));
  139. SetTimer(TIMER_PING, 2000, NULL);
  140. CloseClipboard();
  141. }
  142. }
  143. }
  144. void CClipboardViewer::SetConnect(bool bConnect)
  145. {
  146. m_bConnect = bConnect;
  147. if(bConnect)
  148. {
  149. if(m_bIsConnected == false)
  150. {
  151. Connect();
  152. }
  153. else
  154. {
  155. SendPing();
  156. }
  157. }
  158. else
  159. {
  160. Disconnect();
  161. }
  162. }
  163. /////////////////////////////////////////////////////////////////////////////
  164. // CClipboardViewer message handlers
  165. int CClipboardViewer::OnCreate(LPCREATESTRUCT lpCreateStruct)
  166. {
  167. if(CWnd::OnCreate(lpCreateStruct) == -1)
  168. return -1;
  169. //Set up the clip board viewer
  170. if(m_connectOnStartup)
  171. {
  172. Connect();
  173. }
  174. return 0;
  175. }
  176. void CClipboardViewer::OnDestroy()
  177. {
  178. Disconnect();
  179. CWnd::OnDestroy();
  180. }
  181. void CClipboardViewer::OnChangeCbChain(HWND hWndRemove, HWND hWndAfter)
  182. {
  183. Log(_T("OnChangeCbChain"));
  184. // If the next window is closing, repair the chain.
  185. if(m_hNextClipboardViewer == hWndRemove)
  186. {
  187. m_hNextClipboardViewer = hWndAfter;
  188. }
  189. // Otherwise, pass the message to the next link.
  190. else if (m_hNextClipboardViewer != NULL)
  191. {
  192. if(m_hNextClipboardViewer != m_hWnd)
  193. {
  194. ::SendMessage(m_hNextClipboardViewer, WM_CHANGECBCHAIN, (WPARAM) hWndRemove, (LPARAM) hWndAfter);
  195. }
  196. else
  197. {
  198. m_hNextClipboardViewer = NULL;
  199. }
  200. }
  201. }
  202. LRESULT CClipboardViewer::OnClipboardChange(WPARAM wParam, LPARAM lPara)
  203. {
  204. Log(StrF(_T("OnClipboardChange - Start")));
  205. OnDrawClipboard();
  206. Log(StrF(_T("OnClipboardChange - End")));
  207. return TRUE;
  208. }
  209. //Message that the clipboard data has changed
  210. void CClipboardViewer::OnDrawClipboard()
  211. {
  212. if(::IsClipboardFormatAvailable(theApp.m_PingFormat))
  213. {
  214. m_bPinging = false;
  215. return;
  216. }
  217. // don't process the event when we first attach
  218. if(m_pHandler && !m_bCalling_SetClipboardViewer)
  219. {
  220. if(m_bIsConnected)
  221. {
  222. if(!::IsClipboardFormatAvailable(theApp.m_cfIgnoreClipboard))
  223. {
  224. if(ValidActiveWnd())
  225. {
  226. Log(StrF(_T("OnDrawClipboard:: *** SetTimer *** %d"), GetTickCount()));
  227. KillTimer(TIMER_DRAW_CLIPBOARD);
  228. SetTimer(TIMER_DRAW_CLIPBOARD, g_Opt.m_lProcessDrawClipboardDelay, NULL);
  229. }
  230. }
  231. }
  232. else
  233. {
  234. Log(_T("Not connected, ignore clipboard change"));
  235. }
  236. }
  237. // pass the event to the next Clipboard viewer in the chain
  238. if(m_hNextClipboardViewer != NULL)
  239. {
  240. if(m_hNextClipboardViewer != m_hWnd)
  241. {
  242. ::SendMessage(m_hNextClipboardViewer, WM_DRAWCLIPBOARD, 0, 0);
  243. }
  244. else
  245. {
  246. m_hNextClipboardViewer = NULL;
  247. }
  248. }
  249. }
  250. bool CClipboardViewer::ValidActiveWnd()
  251. {
  252. m_activeWindow = _T("");
  253. m_activeWindowTitle = _T("";)
  254. HWND owner = ::GetClipboardOwner();
  255. if (owner != NULL)
  256. {
  257. DWORD PID = 0;
  258. ::GetWindowThreadProcessId(owner, &PID);
  259. if (PID != 0)
  260. {
  261. m_activeWindow = GetProcessName(NULL, PID);
  262. m_activeWindowTitle = TopLevelWindowText(PID);
  263. }
  264. }
  265. //L"RuntimeBroker.exe" is what all modern apps report as
  266. if (m_activeWindow == _T(""))
  267. {
  268. HWND active = ::GetForegroundWindow();
  269. m_activeWindow = GetProcessName(active, 0);
  270. m_activeWindowTitle = GetWndText(active);
  271. }
  272. m_activeWindow = m_activeWindow.MakeLower();
  273. CString includeApps = CGetSetOptions::GetCopyAppInclude().MakeLower();
  274. Log(StrF(_T("INCLUDE app names: %s, Active App: %s"), includeApps, m_activeWindow));
  275. bool tokenMatch = false;
  276. CTokenizer token(includeApps, CGetSetOptions::GetCopyAppSeparator());
  277. CString line;
  278. while(token.Next(line))
  279. {
  280. if(line != "")
  281. {
  282. if(CWildCardMatch::WildMatch(line.Trim(), m_activeWindow, ""))
  283. {
  284. Log(StrF(_T("Inlclude app names Found Match %s - %s"), line, m_activeWindow));
  285. tokenMatch = true;
  286. break;
  287. }
  288. }
  289. }
  290. if(tokenMatch)
  291. {
  292. CString excludeApps = CGetSetOptions::GetCopyAppExclude().MakeLower();
  293. if(excludeApps != "")
  294. {
  295. Log(StrF(_T("EXCLUDE app names %s, Active App: %s"), excludeApps, m_activeWindow));
  296. CTokenizer token2(excludeApps, CGetSetOptions::GetCopyAppSeparator());
  297. CString line2;
  298. while(token2.Next(line2))
  299. {
  300. if(line2 != "")
  301. {
  302. if(CWildCardMatch::WildMatch(line2.Trim(), m_activeWindow, ""))
  303. {
  304. Log(StrF(_T("Exclude app names Found Match %s - %s - NOT SAVING COPY"), line2, m_activeWindow));
  305. return false;
  306. }
  307. }
  308. }
  309. }
  310. }
  311. else
  312. {
  313. Log(StrF(_T("Didn't find a match to INCLUDE match %s, NOT SAVING COPY"), includeApps));
  314. return false;
  315. }
  316. return true;
  317. }
  318. void CClipboardViewer::OnTimer(UINT_PTR nIDEvent)
  319. {
  320. switch(nIDEvent)
  321. {
  322. case TIMER_ENSURE_VIEWER_IN_CHAIN:
  323. SendPing();
  324. break;
  325. case TIMER_DRAW_CLIPBOARD:
  326. {
  327. KillTimer(nIDEvent);
  328. DWORD dwNow = GetTickCount();
  329. if(dwNow - m_dwLastCopy > g_Opt.m_dwSaveClipDelay || m_dwLastCopy > dwNow)
  330. {
  331. if(!::IsClipboardFormatAvailable(theApp.m_cfIgnoreClipboard))
  332. {
  333. Log(StrF(_T("OnDrawClipboard::OnTimer %d"), dwNow));
  334. m_pHandler->OnClipboardChange(m_activeWindow, m_activeWindowTitle);
  335. m_dwLastCopy = dwNow;
  336. }
  337. }
  338. else
  339. {
  340. Log(StrF(_T("Clip copy to fast difference from last copy = %d"), (dwNow - m_dwLastCopy)));
  341. }
  342. m_activeWindow = _T("");
  343. m_activeWindowTitle = _T("");
  344. }
  345. break;
  346. case TIMER_PING:
  347. KillTimer(TIMER_PING);
  348. //If we haven't received the change clipboard message then we are disconnected
  349. //if so reconnect
  350. if(m_bPinging)
  351. {
  352. if(m_bConnect)
  353. {
  354. Log(_T("Ping Failed Reconnecting to clipboard"));
  355. Disconnect(false);
  356. Connect();
  357. }
  358. else
  359. {
  360. Log(_T("Ping Failed but Connected set to FALSE so this is ok"));
  361. }
  362. }
  363. else
  364. {
  365. if(m_bConnect)
  366. {
  367. m_bIsConnected = true;
  368. }
  369. }
  370. break;
  371. }
  372. CWnd::OnTimer(nIDEvent);
  373. }
  374. LRESULT CClipboardViewer::OnSetConnect(WPARAM wParam, LPARAM lParam)
  375. {
  376. bool bConnect = wParam == TRUE;
  377. SetConnect(bConnect);
  378. return TRUE;
  379. }