aboutsummaryrefslogtreecommitdiffstats
path: root/win/rfb_win32/WMHooks.cxx
blob: 62fc49f6bd24f9e83467beb2f383e4369f258b50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/* Copyright (C) 2002-2005 RealVNC Ltd.  All Rights Reserved.
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

// -=- WMHooks.cxx

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <os/Mutex.h>
#include <os/Thread.h>

#include <rfb_win32/WMHooks.h>
#include <rfb_win32/Service.h>
#include <rfb_win32/MsgWindow.h>
#include <rfb_win32/IntervalTimer.h>
#include <rfb/LogWriter.h>

#include <list>

using namespace rfb;
using namespace rfb::win32;

static LogWriter vlog("WMHooks");


static HMODULE hooksLibrary;

typedef UINT (*WM_Hooks_WMVAL_proto)();
static WM_Hooks_WMVAL_proto WM_Hooks_WindowChanged;
static WM_Hooks_WMVAL_proto WM_Hooks_WindowBorderChanged;
static WM_Hooks_WMVAL_proto WM_Hooks_WindowClientAreaChanged;
static WM_Hooks_WMVAL_proto WM_Hooks_RectangleChanged;
#ifdef _DEBUG
static WM_Hooks_WMVAL_proto WM_Hooks_Diagnostic;
#endif

typedef BOOL (*WM_Hooks_Install_proto)(DWORD owner, DWORD thread);
static WM_Hooks_Install_proto WM_Hooks_Install;
typedef BOOL (*WM_Hooks_Remove_proto)(DWORD owner);
static WM_Hooks_Remove_proto WM_Hooks_Remove;
#ifdef _DEBUG
typedef void (*WM_Hooks_SetDiagnosticRange_proto)(UINT min, UINT max);
static WM_Hooks_SetDiagnosticRange_proto WM_Hooks_SetDiagnosticRange;
#endif

typedef BOOL (*WM_Hooks_EnableRealInputs_proto)(BOOL pointer, BOOL keyboard);
static WM_Hooks_EnableRealInputs_proto WM_Hooks_EnableRealInputs;


static void LoadHooks()
{
  if (hooksLibrary != nullptr)
    return;

  hooksLibrary = LoadLibrary("wm_hooks.dll");
  if (hooksLibrary == nullptr)
    return;

  WM_Hooks_WindowChanged = (WM_Hooks_WMVAL_proto)(void*)GetProcAddress(hooksLibrary, "WM_Hooks_WindowChanged");
  if (WM_Hooks_WindowChanged == nullptr)
    goto error;
  WM_Hooks_WindowBorderChanged = (WM_Hooks_WMVAL_proto)(void*)GetProcAddress(hooksLibrary, "WM_Hooks_WindowBorderChanged");
  if (WM_Hooks_WindowBorderChanged == nullptr)
    goto error;
  WM_Hooks_WindowClientAreaChanged = (WM_Hooks_WMVAL_proto)(void*)GetProcAddress(hooksLibrary, "WM_Hooks_WindowClientAreaChanged");
  if (WM_Hooks_WindowClientAreaChanged == nullptr)
    goto error;
  WM_Hooks_RectangleChanged = (WM_Hooks_WMVAL_proto)(void*)GetProcAddress(hooksLibrary, "WM_Hooks_RectangleChanged");
  if (WM_Hooks_RectangleChanged == nullptr)
    goto error;
#ifdef _DEBUG
  WM_Hooks_Diagnostic = (WM_Hooks_WMVAL_proto)(void*)GetProcAddress(hooksLibrary, "WM_Hooks_Diagnostic");
  if (WM_Hooks_Diagnostic == nullptr)
    goto error;
#endif

  WM_Hooks_Install = (WM_Hooks_Install_proto)(void*)GetProcAddress(hooksLibrary, "WM_Hooks_Install");
  if (WM_Hooks_Install == nullptr)
    goto error;
  WM_Hooks_Remove = (WM_Hooks_Remove_proto)(void*)GetProcAddress(hooksLibrary, "WM_Hooks_Remove");
  if (WM_Hooks_Remove == nullptr)
    goto error;
#ifdef _DEBUG
  WM_Hooks_SetDiagnosticRange = (WM_Hooks_SetDiagnosticRange_proto)(void*)GetProcAddress(hooksLibrary, "WM_Hooks_SetDiagnosticRange");
  if (WM_Hooks_SetDiagnosticRange == nullptr)
    goto error;
#endif

  WM_Hooks_EnableRealInputs = (WM_Hooks_EnableRealInputs_proto)(void*)GetProcAddress(hooksLibrary, "WM_Hooks_EnableRealInputs");
  if (WM_Hooks_EnableRealInputs == nullptr)
    goto error;

  return;

error:
  FreeLibrary(hooksLibrary);
  hooksLibrary = nullptr;
}


class WMHooksThread : public os::Thread {
public:
  WMHooksThread() : active(true), thread_id(-1) { }
  void stop();
  DWORD getThreadId() { return thread_id; }
protected:
  virtual void worker();
protected:
  bool active;
  DWORD thread_id;
};

static WMHooksThread* hook_mgr = nullptr;
static std::list<WMHooks*> hooks;
static os::Mutex hook_mgr_lock;


static bool StartHookThread() {
  if (hook_mgr)
    return true;
  if (hooksLibrary == nullptr)
    return false;
  vlog.debug("creating thread");
  hook_mgr = new WMHooksThread();
  hook_mgr->start();
  while (hook_mgr->getThreadId() == (DWORD)-1)
    Sleep(0);
  vlog.debug("installing hooks");
  if (!WM_Hooks_Install(hook_mgr->getThreadId(), 0)) {
    vlog.error("failed to initialise hooks");
    hook_mgr->stop();
    delete hook_mgr;
    hook_mgr = nullptr;
    return false;
  }
  return true;
}

static void StopHookThread() {
  if (!hook_mgr)
    return;
  if (!hooks.empty())
    return;
  vlog.debug("closing thread");
  hook_mgr->stop();
  delete hook_mgr;
  hook_mgr = nullptr;
}


static bool AddHook(WMHooks* hook) {
  vlog.debug("adding hook");
  os::AutoMutex a(&hook_mgr_lock);
  if (!StartHookThread())
    return false;
  hooks.push_back(hook);
  return true;
}

static bool RemHook(WMHooks* hook) {
  {
    vlog.debug("removing hook");
    os::AutoMutex a(&hook_mgr_lock);
    hooks.remove(hook);
  }
  StopHookThread();
  return true;
}

static void NotifyHooksRegion(const Region& r) {
  os::AutoMutex a(&hook_mgr_lock);
  std::list<WMHooks*>::iterator i;
  for (i=hooks.begin(); i!=hooks.end(); i++)
    (*i)->NotifyHooksRegion(r);
}


void
WMHooksThread::worker() {
  // Obtain message ids for all supported hook messages
  UINT windowMsg = WM_Hooks_WindowChanged();
  UINT clientAreaMsg = WM_Hooks_WindowClientAreaChanged();
  UINT borderMsg = WM_Hooks_WindowBorderChanged();
  UINT rectangleMsg = WM_Hooks_RectangleChanged();
#ifdef _DEBUG
  UINT diagnosticMsg = WM_Hooks_Diagnostic();
#endif
  MSG msg;
  RECT wrect;
  HWND hwnd;
  int count = 0;

  // Update delay handling
  //   We delay updates by 40-80ms, so that the triggering application has time to
  //   actually complete them before we notify the hook callbacks & they go off
  //   capturing screen state.
  const int updateDelayMs = 40;
  MsgWindow updateDelayWnd("WMHooks::updateDelay");
  IntervalTimer updateDelayTimer(updateDelayWnd.getHandle(), 1);
  Region updates[2];
  int activeRgn = 0;

  vlog.debug("starting hook thread");

  thread_id = GetCurrentThreadId();

  while (active && GetMessage(&msg, nullptr, 0, 0)) {
    count++;

    if (msg.message == WM_TIMER) {
      // Actually notify callbacks of graphical updates
      NotifyHooksRegion(updates[1-activeRgn]);
      if (updates[activeRgn].is_empty())
        updateDelayTimer.stop();
      activeRgn = 1-activeRgn;
      updates[activeRgn].clear();

    } else if (msg.message == windowMsg) {
      // An entire window has (potentially) changed
      hwnd = (HWND) msg.lParam;
      if (IsWindow(hwnd) && IsWindowVisible(hwnd) && !IsIconic(hwnd) &&
        GetWindowRect(hwnd, &wrect) && !IsRectEmpty(&wrect)) {
          updates[activeRgn].assign_union(Rect(wrect.left, wrect.top,
                                               wrect.right, wrect.bottom));
          updateDelayTimer.start(updateDelayMs);
      }

    } else if (msg.message == clientAreaMsg) {
      // The client area of a window has (potentially) changed
      hwnd = (HWND) msg.lParam;
      if (IsWindow(hwnd) && IsWindowVisible(hwnd) && !IsIconic(hwnd) &&
          GetClientRect(hwnd, &wrect) && !IsRectEmpty(&wrect))
      {
        POINT pt = {0,0};
        if (ClientToScreen(hwnd, &pt)) {
          updates[activeRgn].assign_union(Rect(wrect.left+pt.x, wrect.top+pt.y,
                                               wrect.right+pt.x, wrect.bottom+pt.y));
          updateDelayTimer.start(updateDelayMs);
        }
      }

    } else if (msg.message == borderMsg) {
      hwnd = (HWND) msg.lParam;
      if (IsWindow(hwnd) && IsWindowVisible(hwnd) && !IsIconic(hwnd) &&
          GetWindowRect(hwnd, &wrect) && !IsRectEmpty(&wrect))
      {
        Region changed(Rect(wrect.left, wrect.top, wrect.right, wrect.bottom));
        RECT crect;
        POINT pt = {0,0};
        if (GetClientRect(hwnd, &crect) && ClientToScreen(hwnd, &pt) &&
            !IsRectEmpty(&crect))
        {
          changed.assign_subtract(Rect(crect.left+pt.x, crect.top+pt.y,
                                       crect.right+pt.x, crect.bottom+pt.y));
        }
        if (!changed.is_empty()) {
          updates[activeRgn].assign_union(changed);
          updateDelayTimer.start(updateDelayMs);
        }
      }
    } else if (msg.message == rectangleMsg) {
      Rect r = Rect(LOWORD(msg.wParam), HIWORD(msg.wParam),
                    LOWORD(msg.lParam), HIWORD(msg.lParam));
      if (!r.is_empty()) {
        updates[activeRgn].assign_union(r);
        updateDelayTimer.start(updateDelayMs);
      }

#ifdef _DEBUG
    } else if (msg.message == diagnosticMsg) {
      vlog.info("DIAG msg=%x(%d) wnd=%lx",
                (unsigned)msg.wParam, (int)msg.wParam,
                (unsigned long)msg.lParam);
#endif
    }
  }

  vlog.debug("stopping hook thread - processed %d events", count);
  WM_Hooks_Remove(getThreadId());
}

void
WMHooksThread::stop() {
  vlog.debug("stopping WMHooks thread");
  active = false;
  PostThreadMessage(thread_id, WM_QUIT, 0, 0);
  vlog.debug("waiting for WMHooks thread");
  wait();
}

// -=- WMHooks class

rfb::win32::WMHooks::WMHooks() : updateEvent(nullptr) {
  LoadHooks();
}

rfb::win32::WMHooks::~WMHooks() {
  RemHook(this);
}

bool rfb::win32::WMHooks::setEvent(HANDLE ue) {
  if (updateEvent)
    RemHook(this);
  updateEvent = ue;
  return AddHook(this);
}

bool rfb::win32::WMHooks::getUpdates(UpdateTracker* ut) {
  if (!updatesReady) return false;
  os::AutoMutex a(&hook_mgr_lock);
  updates.copyTo(ut);
  updates.clear();
  updatesReady = false;
  return true;
}

#ifdef _DEBUG
void
rfb::win32::WMHooks::setDiagnosticRange(UINT min, UINT max) {
  WM_Hooks_SetDiagnosticRange(min, max);
}
#endif

void rfb::win32::WMHooks::NotifyHooksRegion(const Region& r) {
  // hook_mgr_lock is already held at this point
  updates.add_changed(r);
  updatesReady = true;
  SetEvent(updateEvent);
}


// -=- WMBlockInput class

rfb::win32::WMBlockInput::WMBlockInput() : active(false) {
  LoadHooks();
}

rfb::win32::WMBlockInput::~WMBlockInput() {
  blockInputs(false);
}

static bool blocking = false;
static bool blockRealInputs(bool block_) {
  // NB: Requires blockMutex to be held!
  if (hooksLibrary == nullptr)
    return false;
  if (block_) {
    if (blocking)
      return true;
    // Enable blocking
    if (!WM_Hooks_EnableRealInputs(false, false))
      return false;
    blocking = true;
  }
  if (blocking) {
    WM_Hooks_EnableRealInputs(true, true);
    blocking = false;
  }
  return block_ == blocking;
}

static os::Mutex blockMutex;
static int blockCount = 0;

bool rfb::win32::WMBlockInput::blockInputs(bool on) {
  if (active == on) return true;
  os::AutoMutex a(&blockMutex);
  int newCount = on ? blockCount+1 : blockCount-1;
  if (!blockRealInputs(newCount > 0))
    return false;
  blockCount = newCount;
  active = on;
  return true;
}