ExternalWindowTracker.cpp 8.6 KB

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