ExternalWindowTracker.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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(HWND focus, 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 = focus;
  25. HWND newActive = ::GetForegroundWindow();
  26. if(newFocus == NULL)
  27. {
  28. if(AttachThreadInput(GetWindowThreadProcessId(newActive, NULL), GetCurrentThreadId(), TRUE))
  29. {
  30. newFocus = GetFocus();
  31. AttachThreadInput(GetWindowThreadProcessId(newActive, NULL), GetCurrentThreadId(), FALSE);
  32. }
  33. else
  34. {
  35. //Log(_T("TrackActiveWnd - AttachThreadInput failed"));
  36. }
  37. fromHook = false;
  38. }
  39. if(newFocus == 0 && newActive != 0)
  40. {
  41. newFocus = newActive;
  42. }
  43. else if(newActive == 0 && newFocus != 0)
  44. {
  45. newActive = newFocus;
  46. }
  47. if(newFocus == 0 || !IsWindow(newFocus) || newActive == 0 || !IsWindow(newActive))
  48. {
  49. Log(_T("TargetActiveWindow values invalid"));
  50. return false;
  51. }
  52. if(newActive == m_activeWnd)
  53. {
  54. // Log(_T("TargetActiveWindow window the same"));
  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. if(activateTarget)
  189. {
  190. ActivateTarget();
  191. theApp.PumpMessageEx();
  192. WaitForActiveWnd(activeWnd, max(25, g_Opt.WaitForActiveWndTimeout()));
  193. }
  194. else
  195. {
  196. theApp.PumpMessageEx();
  197. }
  198. m_dittoHasFocus = false;
  199. Log(StrF(_T("Sending paste to app %s key stroke: %s, SeDelay: %d"), csPasteToApp, csPasteString, delay));
  200. Sleep(delay);
  201. send.SetKeyDownDelay(max(50, delay));
  202. send.SendKeys(csPasteString, true);
  203. Log(_T("Post sending paste"));
  204. }
  205. void ExternalWindowTracker::SendCopy()
  206. {
  207. CSendKeys send;
  208. send.AllKeysUp();
  209. CString csToApp = GetProcessName(m_activeWnd);
  210. CString csString = g_Opt.GetCopyString(csToApp);
  211. DWORD delay = g_Opt.SendKeysDelay();
  212. Sleep(delay);
  213. theApp.PumpMessageEx();
  214. Log(StrF(_T("Sending copy to app %s key stroke: %s, Delay: %d"), csToApp, csString, delay));
  215. //give the app some time to take focus before sending paste
  216. Sleep(delay);
  217. send.SetKeyDownDelay(max(50, delay));
  218. send.SendKeys(csString, true);
  219. Log(_T("Post sending copy"));
  220. }
  221. // sends Ctrl-X to the TargetWnd
  222. void ExternalWindowTracker::SendCut()
  223. {
  224. CSendKeys send;
  225. send.AllKeysUp();
  226. CString csToApp = GetProcessName(m_activeWnd);
  227. CString csString = g_Opt.GetCopyString(csToApp);
  228. DWORD delay = g_Opt.SendKeysDelay();
  229. Sleep(delay);
  230. theApp.PumpMessageEx();
  231. Log(StrF(_T("Sending cut to app %s key stroke: %s, Delay: %d"), csToApp, csString, delay));
  232. //give the app some time to take focus before sending paste
  233. Sleep(delay);
  234. send.SetKeyDownDelay(max(50, delay));
  235. send.SendKeys(csString, true);
  236. Log(_T("Post sending cut"));
  237. }
  238. CString ExternalWindowTracker::ActiveWndName()
  239. {
  240. return WndName(m_activeWnd);
  241. }
  242. CString ExternalWindowTracker::WndName(HWND hWnd)
  243. {
  244. TCHAR cWindowText[200];
  245. HWND hParent = hWnd;
  246. ::GetWindowText(hParent, cWindowText, 100);
  247. int nCount = 0;
  248. while(STRLEN(cWindowText) <= 0)
  249. {
  250. hParent = ::GetParent(hParent);
  251. if(hParent == NULL)
  252. break;
  253. ::GetWindowText(hParent, cWindowText, 100);
  254. nCount++;
  255. if(nCount > 100)
  256. {
  257. Log(_T("GetTargetName reached maximum search depth of 100"));
  258. break;
  259. }
  260. }
  261. return cWindowText;
  262. }
  263. bool ExternalWindowTracker::ReleaseFocus()
  264. {
  265. if( IsAppWnd(::GetForegroundWindow()) )
  266. {
  267. return ActivateTarget();
  268. }
  269. return false;
  270. }
  271. CPoint ExternalWindowTracker::FocusCaret()
  272. {
  273. CPoint pt(-1, -1);
  274. if(m_activeWnd)
  275. {
  276. GUITHREADINFO guiThreadInfo;
  277. guiThreadInfo.cbSize = sizeof(GUITHREADINFO);
  278. DWORD OtherThreadID = GetWindowThreadProcessId(m_activeWnd, NULL);
  279. if(GetGUIThreadInfo(OtherThreadID, &guiThreadInfo))
  280. {
  281. CRect rc(guiThreadInfo.rcCaret);
  282. if(rc.IsRectEmpty() == FALSE)
  283. {
  284. pt = rc.BottomRight();
  285. ::ClientToScreen(m_focusWnd, &pt);
  286. }
  287. }
  288. if(pt.x < 0 || pt.y < 0)
  289. {
  290. if(m_focusWnd != NULL &&
  291. m_activeWnd != NULL &&
  292. AttachThreadInput(GetWindowThreadProcessId(m_activeWnd, NULL), GetCurrentThreadId(), TRUE))
  293. {
  294. BOOL ret = GetCaretPos(&pt);
  295. if(ret && (pt.x > 0 || pt.y > 0))
  296. {
  297. ClientToScreen(m_focusWnd, &pt);
  298. pt.y += 20;
  299. }
  300. AttachThreadInput(GetWindowThreadProcessId(m_activeWnd, NULL), GetCurrentThreadId(), FALSE);
  301. }
  302. }
  303. }
  304. return pt;
  305. }