Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 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 | |
| 19 | // -=- WMHooks.cxx |
| 20 | |
| 21 | #include <rfb_win32/WMHooks.h> |
| 22 | #include <rfb_win32/DynamicFn.h> |
| 23 | #include <rfb_win32/Service.h> |
| 24 | #include <rfb_win32/MsgWindow.h> |
| 25 | #include <rfb_win32/IntervalTimer.h> |
| 26 | #include <rfb/Threading.h> |
| 27 | #include <rfb/LogWriter.h> |
| 28 | |
| 29 | #include <list> |
| 30 | |
| 31 | using namespace rfb; |
| 32 | using namespace rfb::win32; |
| 33 | |
| 34 | static LogWriter vlog("WMHooks"); |
| 35 | |
| 36 | |
| 37 | typedef UINT (*WM_Hooks_WMVAL_proto)(); |
| 38 | typedef BOOL (*WM_Hooks_Install_proto)(DWORD owner, DWORD thread); |
| 39 | typedef BOOL (*WM_Hooks_Remove_proto)(DWORD owner); |
| 40 | typedef BOOL (*WM_Hooks_EnableCursorShape_proto)(BOOL enable); |
| 41 | #ifdef _DEBUG |
| 42 | typedef void (*WM_Hooks_SetDiagnosticRange_proto)(UINT min, UINT max); |
| 43 | DynamicFn<WM_Hooks_SetDiagnosticRange_proto> WM_Hooks_SetDiagnosticRange(_T("wm_hooks.dll"), "WM_Hooks_SetDiagnosticRange"); |
| 44 | #endif |
| 45 | |
| 46 | |
| 47 | class WMHooksThread : public Thread { |
| 48 | public: |
Peter Åstrand | 0a7700e | 2008-12-10 13:12:38 +0000 | [diff] [blame] | 49 | WMHooksThread() : Thread("WMHookThread"), |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 50 | WM_Hooks_Install(_T("wm_hooks.dll"), "WM_Hooks_Install"), |
| 51 | WM_Hooks_Remove(_T("wm_hooks.dll"), "WM_Hooks_Remove"), |
Peter Åstrand | 0a7700e | 2008-12-10 13:12:38 +0000 | [diff] [blame] | 52 | WM_Hooks_EnableCursorShape(_T("wm_hooks.dll"), "WM_Hooks_EnableCursorShape"), |
| 53 | active(true) { |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 54 | } |
| 55 | virtual void run(); |
| 56 | virtual Thread* join(); |
| 57 | DynamicFn<WM_Hooks_Install_proto> WM_Hooks_Install;; |
| 58 | DynamicFn<WM_Hooks_Remove_proto> WM_Hooks_Remove; |
| 59 | DynamicFn<WM_Hooks_EnableCursorShape_proto> WM_Hooks_EnableCursorShape; |
| 60 | protected: |
| 61 | bool active; |
| 62 | }; |
| 63 | |
| 64 | WMHooksThread* hook_mgr = 0; |
| 65 | std::list<WMHooks*> hooks; |
| 66 | std::list<WMCursorHooks*> cursor_hooks; |
| 67 | Mutex hook_mgr_lock; |
| 68 | HCURSOR hook_cursor = (HCURSOR)LoadImage(0, IDC_ARROW, IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED); |
| 69 | |
| 70 | |
| 71 | static bool StartHookThread() { |
| 72 | if (hook_mgr) |
| 73 | return true; |
| 74 | vlog.debug("creating thread"); |
| 75 | hook_mgr = new WMHooksThread(); |
| 76 | if (!hook_mgr->WM_Hooks_Install.isValid() || |
| 77 | !hook_mgr->WM_Hooks_Remove.isValid()) { |
| 78 | vlog.debug("hooks not available"); |
| 79 | return false; |
| 80 | } |
| 81 | vlog.debug("installing hooks"); |
| 82 | if (!(*hook_mgr->WM_Hooks_Install)(hook_mgr->getThreadId(), 0)) { |
| 83 | vlog.error("failed to initialise hooks"); |
| 84 | delete hook_mgr->join(); |
| 85 | hook_mgr = 0; |
| 86 | return false; |
| 87 | } |
| 88 | vlog.debug("starting thread"); |
| 89 | hook_mgr->start(); |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | static void StopHookThread() { |
| 94 | if (!hook_mgr) |
| 95 | return; |
| 96 | if (!hooks.empty() || !cursor_hooks.empty()) |
| 97 | return; |
| 98 | vlog.debug("closing thread"); |
| 99 | delete hook_mgr->join(); |
| 100 | hook_mgr = 0; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | static bool AddHook(WMHooks* hook) { |
| 105 | vlog.debug("adding hook"); |
| 106 | Lock l(hook_mgr_lock); |
| 107 | if (!StartHookThread()) |
| 108 | return false; |
| 109 | hooks.push_back(hook); |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | static bool AddCursorHook(WMCursorHooks* hook) { |
| 114 | vlog.debug("adding cursor hook"); |
| 115 | Lock l(hook_mgr_lock); |
| 116 | if (!StartHookThread()) |
| 117 | return false; |
| 118 | if (!hook_mgr->WM_Hooks_EnableCursorShape.isValid()) |
| 119 | return false; |
| 120 | if (cursor_hooks.empty() && !(*hook_mgr->WM_Hooks_EnableCursorShape)(TRUE)) |
| 121 | return false; |
| 122 | cursor_hooks.push_back(hook); |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | static bool RemHook(WMHooks* hook) { |
| 127 | { |
| 128 | vlog.debug("removing hook"); |
| 129 | Lock l(hook_mgr_lock); |
| 130 | hooks.remove(hook); |
| 131 | } |
| 132 | StopHookThread(); |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | static bool RemCursorHook(WMCursorHooks* hook) { |
| 137 | { |
| 138 | vlog.debug("removing cursor hook"); |
| 139 | Lock l(hook_mgr_lock); |
| 140 | cursor_hooks.remove(hook); |
| 141 | if (hook_mgr->WM_Hooks_EnableCursorShape.isValid() && |
| 142 | cursor_hooks.empty()) |
| 143 | (*hook_mgr->WM_Hooks_EnableCursorShape)(FALSE); |
| 144 | } |
| 145 | StopHookThread(); |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | static void NotifyHooksRegion(const Region& r) { |
| 150 | Lock l(hook_mgr_lock); |
| 151 | std::list<WMHooks*>::iterator i; |
| 152 | for (i=hooks.begin(); i!=hooks.end(); i++) |
| 153 | (*i)->NotifyHooksRegion(r); |
| 154 | } |
| 155 | |
| 156 | static void NotifyHooksCursor(HCURSOR c) { |
| 157 | Lock l(hook_mgr_lock); |
| 158 | hook_cursor = c; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | static UINT GetMsgVal(DynamicFn<WM_Hooks_WMVAL_proto>& fn) { |
| 163 | if (fn.isValid()) |
| 164 | return (*fn)(); |
| 165 | return WM_NULL; |
| 166 | } |
| 167 | |
| 168 | void |
| 169 | WMHooksThread::run() { |
| 170 | // Obtain message ids for all supported hook messages |
| 171 | DynamicFn<WM_Hooks_WMVAL_proto> WM_Hooks_WindowChanged(_T("wm_hooks.dll"), "WM_Hooks_WindowChanged"); |
| 172 | DynamicFn<WM_Hooks_WMVAL_proto> WM_Hooks_WindowBorderChanged(_T("wm_hooks.dll"), "WM_Hooks_WindowBorderChanged"); |
| 173 | DynamicFn<WM_Hooks_WMVAL_proto> WM_Hooks_WindowClientAreaChanged(_T("wm_hooks.dll"), "WM_Hooks_WindowClientAreaChanged"); |
| 174 | DynamicFn<WM_Hooks_WMVAL_proto> WM_Hooks_RectangleChanged(_T("wm_hooks.dll"), "WM_Hooks_RectangleChanged"); |
| 175 | DynamicFn<WM_Hooks_WMVAL_proto> WM_Hooks_CursorChanged(_T("wm_hooks.dll"), "WM_Hooks_CursorChanged"); |
| 176 | UINT windowMsg = GetMsgVal(WM_Hooks_WindowChanged); |
| 177 | UINT clientAreaMsg = GetMsgVal(WM_Hooks_WindowClientAreaChanged); |
| 178 | UINT borderMsg = GetMsgVal(WM_Hooks_WindowBorderChanged); |
| 179 | UINT rectangleMsg = GetMsgVal(WM_Hooks_RectangleChanged); |
| 180 | UINT cursorMsg = GetMsgVal(WM_Hooks_CursorChanged); |
| 181 | #ifdef _DEBUG |
| 182 | DynamicFn<WM_Hooks_WMVAL_proto> WM_Hooks_Diagnostic(_T("wm_hooks.dll"), "WM_Hooks_Diagnostic"); |
| 183 | UINT diagnosticMsg = GetMsgVal(WM_Hooks_Diagnostic); |
| 184 | #endif |
| 185 | MSG msg; |
| 186 | RECT wrect; |
| 187 | HWND hwnd; |
| 188 | int count = 0; |
| 189 | |
| 190 | // Update delay handling |
| 191 | // We delay updates by 40-80ms, so that the triggering application has time to |
| 192 | // actually complete them before we notify the hook callbacks & they go off |
| 193 | // capturing screen state. |
| 194 | const int updateDelayMs = 40; |
| 195 | MsgWindow updateDelayWnd(_T("WMHooks::updateDelay")); |
| 196 | IntervalTimer updateDelayTimer(updateDelayWnd.getHandle(), 1); |
| 197 | Region updates[2]; |
| 198 | int activeRgn = 0; |
| 199 | |
| 200 | vlog.debug("starting hook thread"); |
| 201 | |
| 202 | while (active && GetMessage(&msg, NULL, 0, 0)) { |
| 203 | count++; |
| 204 | |
| 205 | if (msg.message == WM_TIMER) { |
| 206 | // Actually notify callbacks of graphical updates |
| 207 | NotifyHooksRegion(updates[1-activeRgn]); |
| 208 | if (updates[activeRgn].is_empty()) |
| 209 | updateDelayTimer.stop(); |
| 210 | activeRgn = 1-activeRgn; |
| 211 | updates[activeRgn].clear(); |
| 212 | |
| 213 | } else if (msg.message == windowMsg) { |
| 214 | // An entire window has (potentially) changed |
| 215 | hwnd = (HWND) msg.lParam; |
| 216 | if (IsWindow(hwnd) && IsWindowVisible(hwnd) && !IsIconic(hwnd) && |
| 217 | GetWindowRect(hwnd, &wrect) && !IsRectEmpty(&wrect)) { |
| 218 | updates[activeRgn].assign_union(Rect(wrect.left, wrect.top, |
| 219 | wrect.right, wrect.bottom)); |
| 220 | updateDelayTimer.start(updateDelayMs); |
| 221 | } |
| 222 | |
| 223 | } else if (msg.message == clientAreaMsg) { |
| 224 | // The client area of a window has (potentially) changed |
| 225 | hwnd = (HWND) msg.lParam; |
| 226 | if (IsWindow(hwnd) && IsWindowVisible(hwnd) && !IsIconic(hwnd) && |
| 227 | GetClientRect(hwnd, &wrect) && !IsRectEmpty(&wrect)) |
| 228 | { |
| 229 | POINT pt = {0,0}; |
| 230 | if (ClientToScreen(hwnd, &pt)) { |
| 231 | updates[activeRgn].assign_union(Rect(wrect.left+pt.x, wrect.top+pt.y, |
| 232 | wrect.right+pt.x, wrect.bottom+pt.y)); |
| 233 | updateDelayTimer.start(updateDelayMs); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | } else if (msg.message == borderMsg) { |
| 238 | hwnd = (HWND) msg.lParam; |
| 239 | if (IsWindow(hwnd) && IsWindowVisible(hwnd) && !IsIconic(hwnd) && |
| 240 | GetWindowRect(hwnd, &wrect) && !IsRectEmpty(&wrect)) |
| 241 | { |
| 242 | Region changed(Rect(wrect.left, wrect.top, wrect.right, wrect.bottom)); |
| 243 | RECT crect; |
| 244 | POINT pt = {0,0}; |
| 245 | if (GetClientRect(hwnd, &crect) && ClientToScreen(hwnd, &pt) && |
| 246 | !IsRectEmpty(&crect)) |
| 247 | { |
| 248 | changed.assign_subtract(Rect(crect.left+pt.x, crect.top+pt.y, |
| 249 | crect.right+pt.x, crect.bottom+pt.y)); |
| 250 | } |
| 251 | if (!changed.is_empty()) { |
| 252 | updates[activeRgn].assign_union(changed); |
| 253 | updateDelayTimer.start(updateDelayMs); |
| 254 | } |
| 255 | } |
| 256 | } else if (msg.message == rectangleMsg) { |
| 257 | Rect r = Rect(LOWORD(msg.wParam), HIWORD(msg.wParam), |
| 258 | LOWORD(msg.lParam), HIWORD(msg.lParam)); |
| 259 | if (!r.is_empty()) { |
| 260 | updates[activeRgn].assign_union(r); |
| 261 | updateDelayTimer.start(updateDelayMs); |
| 262 | } |
| 263 | |
| 264 | } else if (msg.message == cursorMsg) { |
| 265 | NotifyHooksCursor((HCURSOR)msg.lParam); |
| 266 | #ifdef _DEBUG |
| 267 | } else if (msg.message == diagnosticMsg) { |
| 268 | vlog.info("DIAG msg=%x(%d) wnd=%lx", msg.wParam, msg.wParam, msg.lParam); |
| 269 | #endif |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | vlog.debug("stopping hook thread - processed %d events", count); |
| 274 | (*WM_Hooks_Remove)(getThreadId()); |
| 275 | } |
| 276 | |
| 277 | Thread* |
| 278 | WMHooksThread::join() { |
| 279 | vlog.debug("stopping WMHooks thread"); |
| 280 | active = false; |
| 281 | PostThreadMessage(thread_id, WM_QUIT, 0, 0); |
| 282 | vlog.debug("joining WMHooks thread"); |
| 283 | return Thread::join(); |
| 284 | } |
| 285 | |
| 286 | // -=- WMHooks class |
| 287 | |
| 288 | rfb::win32::WMHooks::WMHooks() : updateEvent(0) { |
| 289 | } |
| 290 | |
| 291 | rfb::win32::WMHooks::~WMHooks() { |
| 292 | RemHook(this); |
| 293 | } |
| 294 | |
| 295 | bool rfb::win32::WMHooks::setEvent(HANDLE ue) { |
| 296 | if (updateEvent) |
| 297 | RemHook(this); |
| 298 | updateEvent = ue; |
| 299 | return AddHook(this); |
| 300 | } |
| 301 | |
| 302 | bool rfb::win32::WMHooks::getUpdates(UpdateTracker* ut) { |
| 303 | if (!updatesReady) return false; |
| 304 | Lock l(hook_mgr_lock); |
| 305 | updates.copyTo(ut); |
| 306 | updates.clear(); |
| 307 | updatesReady = false; |
| 308 | return true; |
| 309 | } |
| 310 | |
| 311 | bool rfb::win32::WMHooks::areAvailable() { |
| 312 | WMHooksThread wmht; |
| 313 | return wmht.WM_Hooks_Install.isValid(); |
| 314 | } |
| 315 | |
| 316 | #ifdef _DEBUG |
| 317 | void |
| 318 | rfb::win32::WMHooks::setDiagnosticRange(UINT min, UINT max) { |
| 319 | if (WM_Hooks_SetDiagnosticRange.isValid()) |
| 320 | (*WM_Hooks_SetDiagnosticRange)(min, max); |
| 321 | } |
| 322 | #endif |
| 323 | |
| 324 | void rfb::win32::WMHooks::NotifyHooksRegion(const Region& r) { |
| 325 | // hook_mgr_lock is already held at this point |
| 326 | updates.add_changed(r); |
| 327 | updatesReady = true; |
| 328 | SetEvent(updateEvent); |
| 329 | } |
| 330 | |
| 331 | |
| 332 | // -=- WMBlockInput class |
| 333 | |
| 334 | rfb::win32::WMBlockInput::WMBlockInput() : active(false) { |
| 335 | } |
| 336 | |
| 337 | rfb::win32::WMBlockInput::~WMBlockInput() { |
| 338 | blockInputs(false); |
| 339 | } |
| 340 | |
| 341 | typedef BOOL (*WM_Hooks_EnableRealInputs_proto)(BOOL pointer, BOOL keyboard); |
| 342 | DynamicFn<WM_Hooks_EnableRealInputs_proto>* WM_Hooks_EnableRealInputs = 0; |
| 343 | static bool blockRealInputs(bool block_) { |
| 344 | // NB: Requires blockMutex to be held! |
| 345 | if (block_) { |
| 346 | if (WM_Hooks_EnableRealInputs) |
| 347 | return true; |
| 348 | // Enable blocking |
| 349 | WM_Hooks_EnableRealInputs = new DynamicFn<WM_Hooks_EnableRealInputs_proto>(_T("wm_hooks.dll"), "WM_Hooks_EnableRealInputs"); |
| 350 | if (WM_Hooks_EnableRealInputs->isValid() && (**WM_Hooks_EnableRealInputs)(false, false)) |
| 351 | return true; |
| 352 | } |
| 353 | if (WM_Hooks_EnableRealInputs) { |
| 354 | // Clean up the DynamicFn, either if init failed, or block_ is false |
| 355 | if (WM_Hooks_EnableRealInputs->isValid()) |
| 356 | (**WM_Hooks_EnableRealInputs)(true, true); |
| 357 | delete WM_Hooks_EnableRealInputs; |
| 358 | WM_Hooks_EnableRealInputs = 0; |
| 359 | } |
| 360 | return block_ == (WM_Hooks_EnableRealInputs != 0); |
| 361 | } |
| 362 | |
| 363 | Mutex blockMutex; |
| 364 | int blockCount = 0; |
| 365 | |
| 366 | bool rfb::win32::WMBlockInput::blockInputs(bool on) { |
| 367 | if (active == on) return true; |
| 368 | Lock l(blockMutex); |
| 369 | int newCount = on ? blockCount+1 : blockCount-1; |
| 370 | if (!blockRealInputs(newCount > 0)) |
| 371 | return false; |
| 372 | blockCount = newCount; |
| 373 | active = on; |
| 374 | return true; |
| 375 | } |
| 376 | |
| 377 | |
| 378 | // -=- WMCursorHooks class |
| 379 | |
| 380 | rfb::win32::WMCursorHooks::WMCursorHooks() { |
| 381 | } |
| 382 | |
| 383 | rfb::win32::WMCursorHooks::~WMCursorHooks() { |
| 384 | RemCursorHook(this); |
| 385 | } |
| 386 | |
| 387 | bool |
| 388 | rfb::win32::WMCursorHooks::start() { |
| 389 | return AddCursorHook(this); |
| 390 | } |
| 391 | |
| 392 | HCURSOR |
| 393 | rfb::win32::WMCursorHooks::getCursor() const { |
| 394 | return hook_cursor; |
| 395 | } |