ExternalWindowTracker.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. Sleep(delay);
  205. send.SetKeyDownDelay(max(50, delay));
  206. send.SendKeys(csPasteString, true);
  207. Log(_T("Post sending paste"));
  208. }
  209. void ExternalWindowTracker::SendCopy()
  210. {
  211. CSendKeys send;
  212. send.AllKeysUp();
  213. CString csToApp = GetProcessName(m_activeWnd);
  214. CString csString = g_Opt.GetCopyString(csToApp);
  215. DWORD delay = g_Opt.SendKeysDelay();
  216. Sleep(delay);
  217. theApp.PumpMessageEx();
  218. Log(StrF(_T("Sending copy to app %s key stroke: %s, Delay: %d"), csToApp, csString, delay));
  219. //give the app some time to take focus before sending paste
  220. Sleep(delay);
  221. send.SetKeyDownDelay(max(50, delay));
  222. send.SendKeys(csString, true);
  223. Log(_T("Post sending copy"));
  224. }
  225. // sends Ctrl-X to the TargetWnd
  226. void ExternalWindowTracker::SendCut()
  227. {
  228. CSendKeys send;
  229. send.AllKeysUp();
  230. CString csToApp = GetProcessName(m_activeWnd);
  231. CString csString = g_Opt.GetCopyString(csToApp);
  232. DWORD delay = g_Opt.SendKeysDelay();
  233. Sleep(delay);
  234. theApp.PumpMessageEx();
  235. Log(StrF(_T("Sending cut to app %s key stroke: %s, Delay: %d"), csToApp, csString, delay));
  236. //give the app some time to take focus before sending paste
  237. Sleep(delay);
  238. send.SetKeyDownDelay(max(50, delay));
  239. send.SendKeys(csString, true);
  240. Log(_T("Post sending cut"));
  241. }
  242. CString ExternalWindowTracker::ActiveWndName()
  243. {
  244. return WndName(m_activeWnd);
  245. }
  246. CString ExternalWindowTracker::WndName(HWND hWnd)
  247. {
  248. TCHAR cWindowText[200];
  249. HWND hParent = hWnd;
  250. ::GetWindowText(hParent, cWindowText, 100);
  251. int nCount = 0;
  252. while(STRLEN(cWindowText) <= 0)
  253. {
  254. hParent = ::GetParent(hParent);
  255. if(hParent == NULL)
  256. break;
  257. ::GetWindowText(hParent, cWindowText, 100);
  258. nCount++;
  259. if(nCount > 100)
  260. {
  261. Log(_T("GetTargetName reached maximum search depth of 100"));
  262. break;
  263. }
  264. }
  265. return cWindowText;
  266. }
  267. bool ExternalWindowTracker::ReleaseFocus()
  268. {
  269. if( IsAppWnd(::GetForegroundWindow()) )
  270. {
  271. return ActivateTarget();
  272. }
  273. return false;
  274. }
  275. CPoint ExternalWindowTracker::FocusCaret()
  276. {
  277. CPoint pt(-1, -1);
  278. if(m_activeWnd)
  279. {
  280. GUITHREADINFO guiThreadInfo;
  281. guiThreadInfo.cbSize = sizeof(GUITHREADINFO);
  282. DWORD OtherThreadID = GetWindowThreadProcessId(m_activeWnd, NULL);
  283. if(GetGUIThreadInfo(OtherThreadID, &guiThreadInfo))
  284. {
  285. CRect rc(guiThreadInfo.rcCaret);
  286. if(rc.IsRectEmpty() == FALSE)
  287. {
  288. pt = rc.BottomRight();
  289. ::ClientToScreen(m_focusWnd, &pt);
  290. }
  291. }
  292. if(pt.x < 0 || pt.y < 0)
  293. {
  294. if(m_focusWnd != NULL &&
  295. m_activeWnd != NULL &&
  296. AttachThreadInput(GetWindowThreadProcessId(m_activeWnd, NULL), GetCurrentThreadId(), TRUE))
  297. {
  298. BOOL ret = GetCaretPos(&pt);
  299. if(ret && (pt.x > 0 || pt.y > 0))
  300. {
  301. ClientToScreen(m_focusWnd, &pt);
  302. pt.y += 20;
  303. }
  304. AttachThreadInput(GetWindowThreadProcessId(m_activeWnd, NULL), GetCurrentThreadId(), FALSE);
  305. }
  306. }
  307. }
  308. return pt;
  309. }