ExternalWindowTracker.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. #include "stdafx.h"
  2. #include "externalwindowtracker.h"
  3. #include "Misc.h"
  4. #include "SendKeys.h"
  5. #include "Options.h"
  6. #include "CP_Main.h"
  7. #include "UAC_Helper.h"
  8. ExternalWindowTracker::ExternalWindowTracker(void)
  9. {
  10. m_activeWnd = NULL;
  11. m_focusWnd = NULL;
  12. m_dittoHasFocus = false;
  13. }
  14. ExternalWindowTracker::~ExternalWindowTracker(void)
  15. {
  16. }
  17. bool ExternalWindowTracker::TrackActiveWnd(bool force)
  18. {
  19. if(force == false && IdleSeconds() < (CGetSetOptions::GetMinIdleTimeBeforeTrackFocus() / 1000.0))
  20. {
  21. Log(StrF(_T("Not Idle for long enough, IdleTime: %f, MinIdle %f"), IdleSeconds(), (CGetSetOptions::GetMinIdleTimeBeforeTrackFocus() / 1000.0)));
  22. return false;
  23. }
  24. BOOL fromHook = true;
  25. HWND newFocus = NULL;
  26. HWND newActive = ::GetForegroundWindow();
  27. if(CGetSetOptions::GetUseGuiThreadInfoForFocus())
  28. {
  29. GUITHREADINFO guiThreadInfo;
  30. guiThreadInfo.cbSize = sizeof(GUITHREADINFO);
  31. DWORD OtherThreadID = GetWindowThreadProcessId(newActive, NULL);
  32. if(GetGUIThreadInfo(OtherThreadID, &guiThreadInfo))
  33. {
  34. newFocus = guiThreadInfo.hwndFocus;
  35. }
  36. }
  37. else
  38. {
  39. if(AttachThreadInput(GetWindowThreadProcessId(newActive, NULL), GetCurrentThreadId(), TRUE))
  40. {
  41. newFocus = GetFocus();
  42. AttachThreadInput(GetWindowThreadProcessId(newActive, NULL), GetCurrentThreadId(), FALSE);
  43. }
  44. }
  45. if(newFocus == 0 && newActive != 0)
  46. {
  47. newFocus = newActive;
  48. }
  49. else if(newActive == 0 && newFocus != 0)
  50. {
  51. newActive = newFocus;
  52. }
  53. if(newFocus == 0 || !IsWindow(newFocus) || newActive == 0 || !IsWindow(newActive))
  54. {
  55. Log(_T("TargetActiveWindow values invalid"));
  56. return false;
  57. }
  58. if(NotifyTrayhWnd(newActive) || NotifyTrayhWnd(newFocus))
  59. {
  60. Log(_T("TargetActiveWindow shell tray icon has active"));
  61. return false;
  62. }
  63. if(IsAppWnd(newFocus) || IsAppWnd(newActive))
  64. {
  65. if(m_dittoHasFocus == false)
  66. {
  67. Log(StrF(_T("Ditto has focus - Active: %s (%d), Focus: %s (%d), FromHook %d"), WndName(m_activeWnd), m_activeWnd, WndName(m_focusWnd), m_focusWnd, fromHook));
  68. }
  69. m_dittoHasFocus = true;
  70. return false;
  71. }
  72. m_focusWnd = newFocus;
  73. m_activeWnd = newActive;
  74. m_dittoHasFocus = false;
  75. if(theApp.QPasteWnd())
  76. theApp.QPasteWnd()->UpdateStatus(true);
  77. Log(StrF(_T("TargetActiveWindow Active: %s (%d), Focus: %s (%d), FromHook %d, IdleTime: %f"), WndName(m_activeWnd), m_activeWnd, WndName(m_focusWnd), m_focusWnd, fromHook, IdleSeconds()));
  78. return true;
  79. }
  80. bool ExternalWindowTracker::WaitForActiveWnd(HWND activeWnd, int timeout)
  81. {
  82. DWORD start = GetTickCount();
  83. while(((int)(GetTickCount() - start)) < timeout)
  84. {
  85. if(::GetForegroundWindow() == activeWnd)
  86. {
  87. Log(StrF(_T("found focus wait %d"), GetTickCount()-start));
  88. return true;
  89. }
  90. Sleep(0);
  91. ActivateTarget();
  92. }
  93. Log(_T("Didn't find focus"));
  94. return false;
  95. }
  96. void ExternalWindowTracker::ActivateFocus(const HWND activeHwnd, const HWND focushWnd)
  97. {
  98. CString csApp = GetProcessName(m_activeWnd);
  99. Log(StrF(_T("SetFocus - AppName: %s, Active: %d, Focus: %d"), csApp, m_activeWnd, m_focusWnd));
  100. if (focushWnd != NULL)
  101. {
  102. AttachThreadInput(GetWindowThreadProcessId(activeHwnd, NULL), GetCurrentThreadId(), TRUE);
  103. if (GetFocus() != focushWnd)
  104. {
  105. SetFocus(focushWnd);
  106. }
  107. AttachThreadInput(GetWindowThreadProcessId(activeHwnd, NULL), GetCurrentThreadId(), FALSE);
  108. }
  109. }
  110. bool ExternalWindowTracker::NotifyTrayhWnd(HWND hWnd)
  111. {
  112. HWND hParent = hWnd;
  113. int nCount = 0;
  114. while(hParent != NULL)
  115. {
  116. TCHAR className[100];
  117. GetClassName(hParent, className, (sizeof(className) / sizeof(TCHAR)));
  118. if((STRCMP(className, _T("Shell_TrayWnd")) == 0) ||
  119. (STRCMP(className, _T("NotifyIconOverflowWindow")) == 0) ||
  120. (STRCMP(className, _T("TrayNotifyWnd")) == 0))
  121. {
  122. return true;
  123. }
  124. hParent = ::GetParent(hParent);
  125. if(hParent == NULL)
  126. break;
  127. nCount++;
  128. if(nCount > 100)
  129. {
  130. Log(_T("GetTargetName reached maximum search depth of 100"));
  131. break;
  132. }
  133. }
  134. return false;
  135. }
  136. bool ExternalWindowTracker::ActivateTarget()
  137. {
  138. Log(StrF(_T("Activate Target - Active: %d, Focus: %d"), m_activeWnd, m_focusWnd));
  139. if (IsIconic(m_activeWnd))
  140. {
  141. ShowWindow(m_activeWnd, SW_RESTORE);
  142. }
  143. // Save specified timeout period...
  144. DWORD timeoutMS = 0;
  145. SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, &timeoutMS, 0);
  146. // ... then set it to zero to disable it
  147. SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, (PVOID)0, 0);
  148. //If we are doing this and we are not the current foreground window then attach to the current bef
  149. //setting the focus window
  150. //this shouldn't happen that much, most of the time we are the foreground window
  151. bool detach = false;
  152. DWORD foreGroundProcessId = GetWindowThreadProcessId(::GetForegroundWindow(), NULL);
  153. if(foreGroundProcessId != GetCurrentThreadId())
  154. {
  155. Log(_T("Attach to process, calling set foreground from non forground window"));
  156. if(AttachThreadInput(foreGroundProcessId, GetCurrentThreadId(), TRUE))
  157. {
  158. detach = true;
  159. }
  160. }
  161. BringWindowToTop(m_activeWnd);
  162. SetForegroundWindow(m_activeWnd);
  163. if(detach)
  164. {
  165. AttachThreadInput(foreGroundProcessId, GetCurrentThreadId(), FALSE);
  166. }
  167. //check to see if this app should set focus
  168. //this is off by default
  169. CString csApp = GetProcessName(m_activeWnd);
  170. if(g_Opt.GetSetFocusToApp(csApp))
  171. {
  172. ActivateFocus(m_activeWnd, m_focusWnd);
  173. }
  174. SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, (PVOID)timeoutMS, 0);
  175. return true;
  176. }
  177. void ExternalWindowTracker::SendPaste(bool activateTarget)
  178. {
  179. HWND activeWnd = m_activeWnd;
  180. CSendKeys send;
  181. send.AllKeysUp();
  182. if(activateTarget == false)
  183. {
  184. activeWnd = ::GetForegroundWindow();
  185. }
  186. CString csPasteToApp = GetProcessName(activeWnd);
  187. CString csPasteString = g_Opt.GetPasteString(csPasteToApp);
  188. DWORD delay = g_Opt.SendKeysDelay();
  189. DWORD startTick = GetTickCount();
  190. if(activateTarget)
  191. {
  192. ActivateTarget();
  193. theApp.PumpMessageEx();
  194. WaitForActiveWnd(activeWnd, max(25, g_Opt.WaitForActiveWndTimeout()));
  195. }
  196. DWORD endTick = GetTickCount();
  197. if((endTick-startTick) > 150)
  198. Log(StrF(_T("Paste Timing Send Paste around activate Target: %d"), endTick-startTick));
  199. m_dittoHasFocus = false;
  200. Log(StrF(_T("Sending paste to app %s key stroke: %s, SeDelay: %d"), csPasteToApp, csPasteString, delay));
  201. bool pasteAsAdmin = false;
  202. if (g_Opt.GetPasteAsAdmin())
  203. {
  204. pasteAsAdmin = CUAC_Helper::PasteAsAdmin(activeWnd);
  205. }
  206. if(pasteAsAdmin &&
  207. theApp.UACThreadRunning() == false)
  208. {
  209. Log(StrF(_T("Passing paste off to uac aware app")));
  210. if (theApp.UACPaste() == false)
  211. {
  212. pasteAsAdmin = false;
  213. }
  214. }
  215. if (pasteAsAdmin == false)
  216. {
  217. if(activateTarget)
  218. {
  219. Sleep(delay);
  220. }
  221. send.SetKeyDownDelay(max(50, delay));
  222. send.SendKeys(csPasteString, true);
  223. }
  224. Log(_T("Post sending paste"));
  225. }
  226. void ExternalWindowTracker::SendCopy(CopyReasonEnum::CopyReason copyReason)
  227. {
  228. CSendKeys send;
  229. send.AllKeysUp();
  230. HWND activeWnd = GetForegroundWindow();
  231. CString csToApp = GetProcessName(activeWnd);
  232. CString csString = g_Opt.GetCopyString(csToApp);
  233. DWORD delay = g_Opt.SendKeysDelay();
  234. Sleep(delay);
  235. theApp.PumpMessageEx();
  236. Log(StrF(_T("Sending copy to app %s key stroke: %s, Delay: %d"), csToApp, csString, delay));
  237. bool pasteAsAdmin = false;
  238. if (g_Opt.GetPasteAsAdmin())
  239. {
  240. pasteAsAdmin = CUAC_Helper::PasteAsAdmin(activeWnd);
  241. }
  242. if(pasteAsAdmin &&
  243. theApp.UACThreadRunning() == false)
  244. {
  245. Log(StrF(_T("Passing copy off to uac aware app")));
  246. if (theApp.UACCopy() == false)
  247. {
  248. pasteAsAdmin = false;
  249. }
  250. }
  251. if (pasteAsAdmin == false)
  252. {
  253. //give the app some time to take focus before sending paste
  254. Sleep(delay);
  255. send.SetKeyDownDelay(max(50, delay));
  256. theApp.SetCopyReason(copyReason);
  257. send.SendKeys(csString, true);
  258. }
  259. Log(_T("Post sending copy"));
  260. }
  261. // sends Ctrl-X to the TargetWnd
  262. void ExternalWindowTracker::SendCut()
  263. {
  264. CSendKeys send;
  265. send.AllKeysUp();
  266. CString csToApp = GetProcessName(m_activeWnd);
  267. CString csString = g_Opt.GetCutString(csToApp);
  268. DWORD delay = g_Opt.SendKeysDelay();
  269. Sleep(delay);
  270. theApp.PumpMessageEx();
  271. Log(StrF(_T("Sending cut to app %s key stroke: %s, Delay: %d"), csToApp, csString, delay));
  272. bool pasteAsAdmin = false;
  273. if (g_Opt.GetPasteAsAdmin())
  274. {
  275. pasteAsAdmin = CUAC_Helper::PasteAsAdmin(m_activeWnd);
  276. }
  277. if(pasteAsAdmin &&
  278. theApp.UACThreadRunning() == false)
  279. {
  280. Log(StrF(_T("Passing copy off to uac aware app")));
  281. if (theApp.UACCut() == false)
  282. {
  283. pasteAsAdmin = false;
  284. }
  285. }
  286. if (pasteAsAdmin == false)
  287. {
  288. //give the app some time to take focus before sending paste
  289. Sleep(delay);
  290. send.SetKeyDownDelay(max(50, delay));
  291. send.SendKeys(csString, true);
  292. }
  293. Log(_T("Post sending cut"));
  294. }
  295. CString ExternalWindowTracker::ActiveWndName()
  296. {
  297. return WndName(m_activeWnd);
  298. }
  299. CString ExternalWindowTracker::WndName(HWND hWnd)
  300. {
  301. TCHAR cWindowText[200];
  302. HWND hParent = hWnd;
  303. ::GetWindowText(hParent, cWindowText, 100);
  304. int nCount = 0;
  305. while(STRLEN(cWindowText) <= 0)
  306. {
  307. hParent = ::GetParent(hParent);
  308. if(hParent == NULL)
  309. break;
  310. ::GetWindowText(hParent, cWindowText, 100);
  311. nCount++;
  312. if(nCount > 100)
  313. {
  314. Log(_T("GetTargetName reached maximum search depth of 100"));
  315. break;
  316. }
  317. }
  318. return cWindowText;
  319. }
  320. bool ExternalWindowTracker::ReleaseFocus()
  321. {
  322. if( IsAppWnd(::GetForegroundWindow()) )
  323. {
  324. return ActivateTarget();
  325. }
  326. return false;
  327. }
  328. CPoint ExternalWindowTracker::FocusCaret()
  329. {
  330. CPoint pt(-1, -1);
  331. if(m_activeWnd)
  332. {
  333. GUITHREADINFO guiThreadInfo;
  334. guiThreadInfo.cbSize = sizeof(GUITHREADINFO);
  335. DWORD OtherThreadID = GetWindowThreadProcessId(m_activeWnd, NULL);
  336. if(GetGUIThreadInfo(OtherThreadID, &guiThreadInfo))
  337. {
  338. CRect rc(guiThreadInfo.rcCaret);
  339. if(rc.IsRectEmpty() == FALSE)
  340. {
  341. pt = rc.BottomRight();
  342. ::ClientToScreen(m_focusWnd, &pt);
  343. }
  344. }
  345. if(pt.x < 0 || pt.y < 0)
  346. {
  347. if(m_focusWnd != NULL &&
  348. m_activeWnd != NULL &&
  349. AttachThreadInput(GetWindowThreadProcessId(m_activeWnd, NULL), GetCurrentThreadId(), TRUE))
  350. {
  351. BOOL ret = GetCaretPos(&pt);
  352. if(ret && (pt.x > 0 || pt.y > 0))
  353. {
  354. ClientToScreen(m_focusWnd, &pt);
  355. pt.y += 20;
  356. }
  357. AttachThreadInput(GetWindowThreadProcessId(m_activeWnd, NULL), GetCurrentThreadId(), FALSE);
  358. }
  359. }
  360. }
  361. return pt;
  362. }