You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WMHooks.cxx 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. // -=- WMHooks.cxx
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #include <os/Mutex.h>
  23. #include <os/Thread.h>
  24. #include <rfb_win32/WMHooks.h>
  25. #include <rfb_win32/Service.h>
  26. #include <rfb_win32/MsgWindow.h>
  27. #include <rfb_win32/IntervalTimer.h>
  28. #include <rfb/LogWriter.h>
  29. #include <list>
  30. using namespace rfb;
  31. using namespace rfb::win32;
  32. static LogWriter vlog("WMHooks");
  33. static HMODULE hooksLibrary;
  34. typedef UINT (*WM_Hooks_WMVAL_proto)();
  35. static WM_Hooks_WMVAL_proto WM_Hooks_WindowChanged;
  36. static WM_Hooks_WMVAL_proto WM_Hooks_WindowBorderChanged;
  37. static WM_Hooks_WMVAL_proto WM_Hooks_WindowClientAreaChanged;
  38. static WM_Hooks_WMVAL_proto WM_Hooks_RectangleChanged;
  39. #ifdef _DEBUG
  40. static WM_Hooks_WMVAL_proto WM_Hooks_Diagnostic;
  41. #endif
  42. typedef BOOL (*WM_Hooks_Install_proto)(DWORD owner, DWORD thread);
  43. static WM_Hooks_Install_proto WM_Hooks_Install;
  44. typedef BOOL (*WM_Hooks_Remove_proto)(DWORD owner);
  45. static WM_Hooks_Remove_proto WM_Hooks_Remove;
  46. #ifdef _DEBUG
  47. typedef void (*WM_Hooks_SetDiagnosticRange_proto)(UINT min, UINT max);
  48. static WM_Hooks_SetDiagnosticRange_proto WM_Hooks_SetDiagnosticRange;
  49. #endif
  50. typedef BOOL (*WM_Hooks_EnableRealInputs_proto)(BOOL pointer, BOOL keyboard);
  51. static WM_Hooks_EnableRealInputs_proto WM_Hooks_EnableRealInputs;
  52. static void LoadHooks()
  53. {
  54. if (hooksLibrary != NULL)
  55. return;
  56. hooksLibrary = LoadLibrary("wm_hooks.dll");
  57. if (hooksLibrary == NULL)
  58. return;
  59. WM_Hooks_WindowChanged = (WM_Hooks_WMVAL_proto)GetProcAddress(hooksLibrary, "WM_Hooks_WindowChanged");
  60. if (WM_Hooks_WindowChanged == NULL)
  61. goto error;
  62. WM_Hooks_WindowBorderChanged = (WM_Hooks_WMVAL_proto)GetProcAddress(hooksLibrary, "WM_Hooks_WindowBorderChanged");
  63. if (WM_Hooks_WindowBorderChanged == NULL)
  64. goto error;
  65. WM_Hooks_WindowClientAreaChanged = (WM_Hooks_WMVAL_proto)GetProcAddress(hooksLibrary, "WM_Hooks_WindowClientAreaChanged");
  66. if (WM_Hooks_WindowClientAreaChanged == NULL)
  67. goto error;
  68. WM_Hooks_RectangleChanged = (WM_Hooks_WMVAL_proto)GetProcAddress(hooksLibrary, "WM_Hooks_RectangleChanged");
  69. if (WM_Hooks_RectangleChanged == NULL)
  70. goto error;
  71. #ifdef _DEBUG
  72. WM_Hooks_Diagnostic = (WM_Hooks_WMVAL_proto)GetProcAddress(hooksLibrary, "WM_Hooks_Diagnostic");
  73. if (WM_Hooks_Diagnostic == NULL)
  74. goto error;
  75. #endif
  76. WM_Hooks_Install = (WM_Hooks_Install_proto)GetProcAddress(hooksLibrary, "WM_Hooks_Install");
  77. if (WM_Hooks_Install == NULL)
  78. goto error;
  79. WM_Hooks_Remove = (WM_Hooks_Remove_proto)GetProcAddress(hooksLibrary, "WM_Hooks_Remove");
  80. if (WM_Hooks_Remove == NULL)
  81. goto error;
  82. #ifdef _DEBUG
  83. WM_Hooks_SetDiagnosticRange = (WM_Hooks_SetDiagnosticRange_proto)GetProcAddress(hooksLibrary, "WM_Hooks_SetDiagnosticRange");
  84. if (WM_Hooks_SetDiagnosticRange == NULL)
  85. goto error;
  86. #endif
  87. WM_Hooks_EnableRealInputs = (WM_Hooks_EnableRealInputs_proto)GetProcAddress(hooksLibrary, "WM_Hooks_EnableRealInputs");
  88. if (WM_Hooks_EnableRealInputs == NULL)
  89. goto error;
  90. return;
  91. error:
  92. FreeLibrary(hooksLibrary);
  93. hooksLibrary = NULL;
  94. }
  95. class WMHooksThread : public os::Thread {
  96. public:
  97. WMHooksThread() : active(true), thread_id(-1) { }
  98. void stop();
  99. DWORD getThreadId() { return thread_id; }
  100. protected:
  101. virtual void worker();
  102. protected:
  103. bool active;
  104. DWORD thread_id;
  105. };
  106. static WMHooksThread* hook_mgr = 0;
  107. static std::list<WMHooks*> hooks;
  108. static os::Mutex hook_mgr_lock;
  109. static bool StartHookThread() {
  110. if (hook_mgr)
  111. return true;
  112. if (hooksLibrary == NULL)
  113. return false;
  114. vlog.debug("creating thread");
  115. hook_mgr = new WMHooksThread();
  116. hook_mgr->start();
  117. while (hook_mgr->getThreadId() == (DWORD)-1)
  118. Sleep(0);
  119. vlog.debug("installing hooks");
  120. if (!WM_Hooks_Install(hook_mgr->getThreadId(), 0)) {
  121. vlog.error("failed to initialise hooks");
  122. hook_mgr->stop();
  123. delete hook_mgr;
  124. hook_mgr = 0;
  125. return false;
  126. }
  127. return true;
  128. }
  129. static void StopHookThread() {
  130. if (!hook_mgr)
  131. return;
  132. if (!hooks.empty())
  133. return;
  134. vlog.debug("closing thread");
  135. hook_mgr->stop();
  136. delete hook_mgr;
  137. hook_mgr = 0;
  138. }
  139. static bool AddHook(WMHooks* hook) {
  140. vlog.debug("adding hook");
  141. os::AutoMutex a(&hook_mgr_lock);
  142. if (!StartHookThread())
  143. return false;
  144. hooks.push_back(hook);
  145. return true;
  146. }
  147. static bool RemHook(WMHooks* hook) {
  148. {
  149. vlog.debug("removing hook");
  150. os::AutoMutex a(&hook_mgr_lock);
  151. hooks.remove(hook);
  152. }
  153. StopHookThread();
  154. return true;
  155. }
  156. static void NotifyHooksRegion(const Region& r) {
  157. os::AutoMutex a(&hook_mgr_lock);
  158. std::list<WMHooks*>::iterator i;
  159. for (i=hooks.begin(); i!=hooks.end(); i++)
  160. (*i)->NotifyHooksRegion(r);
  161. }
  162. void
  163. WMHooksThread::worker() {
  164. // Obtain message ids for all supported hook messages
  165. UINT windowMsg = WM_Hooks_WindowChanged();
  166. UINT clientAreaMsg = WM_Hooks_WindowClientAreaChanged();
  167. UINT borderMsg = WM_Hooks_WindowBorderChanged();
  168. UINT rectangleMsg = WM_Hooks_RectangleChanged();
  169. #ifdef _DEBUG
  170. UINT diagnosticMsg = WM_Hooks_Diagnostic();
  171. #endif
  172. MSG msg;
  173. RECT wrect;
  174. HWND hwnd;
  175. int count = 0;
  176. // Update delay handling
  177. // We delay updates by 40-80ms, so that the triggering application has time to
  178. // actually complete them before we notify the hook callbacks & they go off
  179. // capturing screen state.
  180. const int updateDelayMs = 40;
  181. MsgWindow updateDelayWnd(_T("WMHooks::updateDelay"));
  182. IntervalTimer updateDelayTimer(updateDelayWnd.getHandle(), 1);
  183. Region updates[2];
  184. int activeRgn = 0;
  185. vlog.debug("starting hook thread");
  186. thread_id = GetCurrentThreadId();
  187. while (active && GetMessage(&msg, NULL, 0, 0)) {
  188. count++;
  189. if (msg.message == WM_TIMER) {
  190. // Actually notify callbacks of graphical updates
  191. NotifyHooksRegion(updates[1-activeRgn]);
  192. if (updates[activeRgn].is_empty())
  193. updateDelayTimer.stop();
  194. activeRgn = 1-activeRgn;
  195. updates[activeRgn].clear();
  196. } else if (msg.message == windowMsg) {
  197. // An entire window has (potentially) changed
  198. hwnd = (HWND) msg.lParam;
  199. if (IsWindow(hwnd) && IsWindowVisible(hwnd) && !IsIconic(hwnd) &&
  200. GetWindowRect(hwnd, &wrect) && !IsRectEmpty(&wrect)) {
  201. updates[activeRgn].assign_union(Rect(wrect.left, wrect.top,
  202. wrect.right, wrect.bottom));
  203. updateDelayTimer.start(updateDelayMs);
  204. }
  205. } else if (msg.message == clientAreaMsg) {
  206. // The client area of a window has (potentially) changed
  207. hwnd = (HWND) msg.lParam;
  208. if (IsWindow(hwnd) && IsWindowVisible(hwnd) && !IsIconic(hwnd) &&
  209. GetClientRect(hwnd, &wrect) && !IsRectEmpty(&wrect))
  210. {
  211. POINT pt = {0,0};
  212. if (ClientToScreen(hwnd, &pt)) {
  213. updates[activeRgn].assign_union(Rect(wrect.left+pt.x, wrect.top+pt.y,
  214. wrect.right+pt.x, wrect.bottom+pt.y));
  215. updateDelayTimer.start(updateDelayMs);
  216. }
  217. }
  218. } else if (msg.message == borderMsg) {
  219. hwnd = (HWND) msg.lParam;
  220. if (IsWindow(hwnd) && IsWindowVisible(hwnd) && !IsIconic(hwnd) &&
  221. GetWindowRect(hwnd, &wrect) && !IsRectEmpty(&wrect))
  222. {
  223. Region changed(Rect(wrect.left, wrect.top, wrect.right, wrect.bottom));
  224. RECT crect;
  225. POINT pt = {0,0};
  226. if (GetClientRect(hwnd, &crect) && ClientToScreen(hwnd, &pt) &&
  227. !IsRectEmpty(&crect))
  228. {
  229. changed.assign_subtract(Rect(crect.left+pt.x, crect.top+pt.y,
  230. crect.right+pt.x, crect.bottom+pt.y));
  231. }
  232. if (!changed.is_empty()) {
  233. updates[activeRgn].assign_union(changed);
  234. updateDelayTimer.start(updateDelayMs);
  235. }
  236. }
  237. } else if (msg.message == rectangleMsg) {
  238. Rect r = Rect(LOWORD(msg.wParam), HIWORD(msg.wParam),
  239. LOWORD(msg.lParam), HIWORD(msg.lParam));
  240. if (!r.is_empty()) {
  241. updates[activeRgn].assign_union(r);
  242. updateDelayTimer.start(updateDelayMs);
  243. }
  244. #ifdef _DEBUG
  245. } else if (msg.message == diagnosticMsg) {
  246. vlog.info("DIAG msg=%x(%d) wnd=%lx",
  247. (unsigned)msg.wParam, (int)msg.wParam,
  248. (unsigned long)msg.lParam);
  249. #endif
  250. }
  251. }
  252. vlog.debug("stopping hook thread - processed %d events", count);
  253. WM_Hooks_Remove(getThreadId());
  254. }
  255. void
  256. WMHooksThread::stop() {
  257. vlog.debug("stopping WMHooks thread");
  258. active = false;
  259. PostThreadMessage(thread_id, WM_QUIT, 0, 0);
  260. vlog.debug("waiting for WMHooks thread");
  261. wait();
  262. }
  263. // -=- WMHooks class
  264. rfb::win32::WMHooks::WMHooks() : updateEvent(0) {
  265. LoadHooks();
  266. }
  267. rfb::win32::WMHooks::~WMHooks() {
  268. RemHook(this);
  269. }
  270. bool rfb::win32::WMHooks::setEvent(HANDLE ue) {
  271. if (updateEvent)
  272. RemHook(this);
  273. updateEvent = ue;
  274. return AddHook(this);
  275. }
  276. bool rfb::win32::WMHooks::getUpdates(UpdateTracker* ut) {
  277. if (!updatesReady) return false;
  278. os::AutoMutex a(&hook_mgr_lock);
  279. updates.copyTo(ut);
  280. updates.clear();
  281. updatesReady = false;
  282. return true;
  283. }
  284. #ifdef _DEBUG
  285. void
  286. rfb::win32::WMHooks::setDiagnosticRange(UINT min, UINT max) {
  287. WM_Hooks_SetDiagnosticRange(min, max);
  288. }
  289. #endif
  290. void rfb::win32::WMHooks::NotifyHooksRegion(const Region& r) {
  291. // hook_mgr_lock is already held at this point
  292. updates.add_changed(r);
  293. updatesReady = true;
  294. SetEvent(updateEvent);
  295. }
  296. // -=- WMBlockInput class
  297. rfb::win32::WMBlockInput::WMBlockInput() : active(false) {
  298. LoadHooks();
  299. }
  300. rfb::win32::WMBlockInput::~WMBlockInput() {
  301. blockInputs(false);
  302. }
  303. static bool blocking = false;
  304. static bool blockRealInputs(bool block_) {
  305. // NB: Requires blockMutex to be held!
  306. if (hooksLibrary == NULL)
  307. return false;
  308. if (block_) {
  309. if (blocking)
  310. return true;
  311. // Enable blocking
  312. if (!WM_Hooks_EnableRealInputs(false, false))
  313. return false;
  314. blocking = true;
  315. }
  316. if (blocking) {
  317. WM_Hooks_EnableRealInputs(true, true);
  318. blocking = false;
  319. }
  320. return block_ == blocking;
  321. }
  322. static os::Mutex blockMutex;
  323. static int blockCount = 0;
  324. bool rfb::win32::WMBlockInput::blockInputs(bool on) {
  325. if (active == on) return true;
  326. os::AutoMutex a(&blockMutex);
  327. int newCount = on ? blockCount+1 : blockCount-1;
  328. if (!blockRealInputs(newCount > 0))
  329. return false;
  330. blockCount = newCount;
  331. active = on;
  332. return true;
  333. }