ExternalWindowTracker.cpp 9.3 KB

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