ExternalWindowTracker.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 sendKeysDelay = g_Opt.RealSendKeysDelay();
  199. DWORD startTick = GetTickCount();
  200. if(activateTarget)
  201. {
  202. ActivateTarget();
  203. theApp.PumpMessageEx();
  204. WaitForActiveWnd(activeWnd, max(25, g_Opt.WaitForActiveWndTimeout()));
  205. }
  206. DWORD endTick = GetTickCount();
  207. if((endTick-startTick) > 150)
  208. Log(StrF(_T("Paste Timing Send Paste around activate Target: %d"), endTick-startTick));
  209. m_dittoHasFocus = false;
  210. Log(StrF(_T("Sending paste to app %s key stroke: %s, SeDelay: %d"), csPasteToApp, csPasteString, delay));
  211. bool pasteAsAdmin = false;
  212. if (g_Opt.GetPasteAsAdmin())
  213. {
  214. pasteAsAdmin = CUAC_Helper::PasteAsAdmin(activeWnd);
  215. }
  216. //can't run an elevated app when running windows app
  217. if(CGetSetOptions::GetIsWindowsApp() == FALSE &&
  218. pasteAsAdmin &&
  219. theApp.UACThreadRunning() == false)
  220. {
  221. Log(StrF(_T("Passing paste off to uac aware app")));
  222. if (theApp.UACPaste() == false)
  223. {
  224. pasteAsAdmin = false;
  225. }
  226. }
  227. if (pasteAsAdmin == false)
  228. {
  229. if(activateTarget)
  230. {
  231. Sleep(delay);
  232. }
  233. send.SetKeyDownDelay(sendKeysDelay);
  234. send.SendKeys(csPasteString, true);
  235. }
  236. Log(_T("Post sending paste"));
  237. }
  238. void ExternalWindowTracker::SendCopy(CopyReasonEnum::CopyReason copyReason)
  239. {
  240. CSendKeys send;
  241. send.AllKeysUp();
  242. HWND activeWnd = GetForegroundWindow();
  243. CString csToApp = GetProcessName(activeWnd);
  244. CString csString = g_Opt.GetCopyString(csToApp);
  245. DWORD delay = g_Opt.SendKeysDelay();
  246. DWORD SendKeysDelay = g_Opt.RealSendKeysDelay();
  247. Sleep(delay);
  248. theApp.PumpMessageEx();
  249. Log(StrF(_T("Sending copy to app %s key stroke: %s, Delay: %d"), csToApp, csString, delay));
  250. bool pasteAsAdmin = false;
  251. if (g_Opt.GetPasteAsAdmin())
  252. {
  253. pasteAsAdmin = CUAC_Helper::PasteAsAdmin(activeWnd);
  254. }
  255. //can't run an elevated app when running windows app
  256. if(CGetSetOptions::GetIsWindowsApp() == FALSE &&
  257. pasteAsAdmin &&
  258. theApp.UACThreadRunning() == false)
  259. {
  260. Log(StrF(_T("Passing copy off to uac aware app")));
  261. if (theApp.UACCopy() == false)
  262. {
  263. pasteAsAdmin = false;
  264. }
  265. }
  266. if (pasteAsAdmin == false)
  267. {
  268. //give the app some time to take focus before sending paste
  269. Sleep(delay);
  270. send.SetKeyDownDelay(SendKeysDelay);
  271. theApp.SetCopyReason(copyReason);
  272. send.SendKeys(csString, true);
  273. }
  274. Log(_T("Post sending copy"));
  275. }
  276. // sends Ctrl-X to the TargetWnd
  277. void ExternalWindowTracker::SendCut()
  278. {
  279. CSendKeys send;
  280. send.AllKeysUp();
  281. CString csToApp = GetProcessName(m_activeWnd);
  282. CString csString = g_Opt.GetCutString(csToApp);
  283. DWORD delay = g_Opt.SendKeysDelay();
  284. DWORD sendKeysDelay = g_Opt.RealSendKeysDelay();
  285. Sleep(delay);
  286. theApp.PumpMessageEx();
  287. Log(StrF(_T("Sending cut to app %s key stroke: %s, Delay: %d"), csToApp, csString, delay));
  288. bool pasteAsAdmin = false;
  289. if (g_Opt.GetPasteAsAdmin())
  290. {
  291. pasteAsAdmin = CUAC_Helper::PasteAsAdmin(m_activeWnd);
  292. }
  293. //can't run an elevated app when running windows app
  294. if(CGetSetOptions::GetIsWindowsApp() == FALSE &&
  295. pasteAsAdmin &&
  296. theApp.UACThreadRunning() == false)
  297. {
  298. Log(StrF(_T("Passing copy off to uac aware app")));
  299. if (theApp.UACCut() == false)
  300. {
  301. pasteAsAdmin = false;
  302. }
  303. }
  304. if (pasteAsAdmin == false)
  305. {
  306. //give the app some time to take focus before sending paste
  307. Sleep(delay);
  308. send.SetKeyDownDelay(sendKeysDelay);
  309. send.SendKeys(csString, true);
  310. }
  311. Log(_T("Post sending cut"));
  312. }
  313. CString ExternalWindowTracker::ActiveWndName()
  314. {
  315. return WndName(m_activeWnd);
  316. }
  317. CString ExternalWindowTracker::WndName(HWND hWnd)
  318. {
  319. TCHAR cWindowText[200];
  320. HWND hParent = hWnd;
  321. ::GetWindowText(hParent, cWindowText, 100);
  322. int nCount = 0;
  323. while(STRLEN(cWindowText) <= 0)
  324. {
  325. hParent = ::GetParent(hParent);
  326. if(hParent == NULL)
  327. break;
  328. ::GetWindowText(hParent, cWindowText, 100);
  329. nCount++;
  330. if(nCount > 100)
  331. {
  332. Log(_T("GetTargetName reached maximum search depth of 100"));
  333. break;
  334. }
  335. }
  336. return cWindowText;
  337. }
  338. bool ExternalWindowTracker::ReleaseFocus()
  339. {
  340. if( IsAppWnd(::GetForegroundWindow()) )
  341. {
  342. return ActivateTarget();
  343. }
  344. return false;
  345. }
  346. CPoint ExternalWindowTracker::FocusCaret()
  347. {
  348. CPoint pt(-1, -1);
  349. if(m_activeWnd)
  350. {
  351. GUITHREADINFO guiThreadInfo;
  352. guiThreadInfo.cbSize = sizeof(GUITHREADINFO);
  353. DWORD OtherThreadID = GetWindowThreadProcessId(m_activeWnd, NULL);
  354. if(GetGUIThreadInfo(OtherThreadID, &guiThreadInfo))
  355. {
  356. CRect rc(guiThreadInfo.rcCaret);
  357. if(rc.IsRectEmpty() == FALSE)
  358. {
  359. pt = rc.BottomRight();
  360. ::ClientToScreen(m_focusWnd, &pt);
  361. }
  362. }
  363. if(pt.x == -1 || pt.y == -1)
  364. {
  365. if(m_focusWnd != NULL &&
  366. m_activeWnd != NULL &&
  367. AttachThreadInput(GetWindowThreadProcessId(m_activeWnd, NULL), GetCurrentThreadId(), TRUE))
  368. {
  369. BOOL ret = GetCaretPos(&pt);
  370. if(ret && (pt.x != 0 && pt.y != 0))
  371. {
  372. ::ClientToScreen(m_focusWnd, &pt);
  373. if (pt.y != 0 && pt.x != 0)
  374. {
  375. pt.y += 20;
  376. }
  377. else
  378. {
  379. pt.x = -1;
  380. pt.y = -1;
  381. }
  382. }
  383. else
  384. {
  385. pt.x = -1;
  386. pt.y = -1;
  387. }
  388. AttachThreadInput(GetWindowThreadProcessId(m_activeWnd, NULL), GetCurrentThreadId(), FALSE);
  389. }
  390. }
  391. }
  392. return pt;
  393. }