ExternalWindowTracker.cpp 11 KB

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