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.

wm_hooks.cxx 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. // -=- wm_hooks.cxx
  19. //
  20. // Window Message Hooks Dynamic Link library
  21. #include <tchar.h>
  22. #include <wm_hooks/wm_hooks.h>
  23. #include <os/os.h>
  24. #define SHARED __attribute__((section ("shared"), shared))
  25. UINT WM_HK_PingThread = RegisterWindowMessage(_T("RFB.WM_Hooks.PingThread"));
  26. UINT WM_HK_WindowChanged = RegisterWindowMessage(_T("RFB.WM_Hooks.WindowChanged"));
  27. UINT WM_Hooks_WindowChanged() {
  28. return WM_HK_WindowChanged;
  29. }
  30. UINT WM_HK_WindowClientAreaChanged = RegisterWindowMessage(_T("RFB.WM_Hooks.WindowClientAreaChanged"));
  31. UINT WM_Hooks_WindowClientAreaChanged() {
  32. return WM_HK_WindowClientAreaChanged;
  33. }
  34. UINT WM_HK_WindowBorderChanged = RegisterWindowMessage(_T("RFB.WM_Hooks.WindowBorderChanged"));
  35. UINT WM_Hooks_WindowBorderChanged() {
  36. return WM_HK_WindowBorderChanged;
  37. }
  38. UINT WM_HK_RectangleChanged = RegisterWindowMessage(_T("RFB.WM_Hooks.RectangleChanged"));
  39. UINT WM_Hooks_RectangleChanged() {
  40. return WM_HK_RectangleChanged;
  41. }
  42. UINT WM_HK_CursorChanged = RegisterWindowMessage(_T("RFB.WM_Hooks.CursorChanged"));
  43. UINT WM_Hooks_CursorChanged() {
  44. return WM_HK_CursorChanged;
  45. }
  46. #ifdef _DEBUG
  47. UINT WM_HK_Diagnostic = RegisterWindowMessage(_T("RFB.WM_Hooks.Diagnostic"));
  48. UINT WM_Hooks_Diagnostic() {
  49. return WM_HK_Diagnostic;
  50. }
  51. #endif
  52. ATOM ATOM_Popup_Selection = GlobalAddAtom(_T("RFB.WM_Hooks.PopupSelectionAtom"));
  53. //
  54. // -=- DLL entry point
  55. //
  56. HINSTANCE dll_instance = 0;
  57. BOOL WINAPI DllMain(HANDLE instance, ULONG reason, LPVOID reserved) {
  58. switch (reason) {
  59. case DLL_PROCESS_ATTACH:
  60. dll_instance = (HINSTANCE)instance;
  61. return TRUE;
  62. case DLL_PROCESS_DETACH:
  63. return TRUE;
  64. case DLL_THREAD_DETACH:
  65. WM_Hooks_Remove(GetCurrentThreadId());
  66. return TRUE;
  67. default:
  68. return TRUE;
  69. };
  70. }
  71. //
  72. // -=- Display update hooks
  73. //
  74. DWORD hook_owner SHARED = 0;
  75. DWORD hook_target SHARED = 0;
  76. HHOOK hook_CallWndProc SHARED = 0;
  77. HHOOK hook_CallWndProcRet SHARED = 0;
  78. HHOOK hook_GetMessage SHARED = 0;
  79. HHOOK hook_DialogMessage SHARED = 0;
  80. BOOL enable_cursor_shape SHARED = FALSE;
  81. HCURSOR cursor SHARED = 0;
  82. #ifdef _DEBUG
  83. UINT diagnostic_min SHARED =1;
  84. UINT diagnostic_max SHARED =0;
  85. #endif
  86. #ifdef _DEBUG
  87. DLLEXPORT void WM_Hooks_SetDiagnosticRange(UINT min, UINT max) {
  88. diagnostic_min = min; diagnostic_max=max;
  89. }
  90. #endif
  91. bool NotifyHookOwner(UINT event, WPARAM wParam, LPARAM lParam) {
  92. if (hook_owner) {
  93. return PostThreadMessage(hook_owner, event, wParam, lParam)!=0;
  94. /*
  95. if (last_event)
  96. return PostThreadMessage(hook_owner, last_event, last_wParam, last_lParam);
  97. last_event = event;
  98. last_wParam = wParam;
  99. last_lParam = lParam;
  100. return true;
  101. */
  102. }
  103. return false;
  104. }
  105. bool NotifyWindow(HWND hwnd, UINT msg) {
  106. return NotifyHookOwner(WM_HK_WindowChanged, msg, (LPARAM)hwnd);
  107. }
  108. bool NotifyWindowBorder(HWND hwnd, UINT msg) {
  109. return NotifyHookOwner(WM_HK_WindowBorderChanged, msg, (LPARAM)hwnd);
  110. }
  111. bool NotifyWindowClientArea(HWND hwnd, UINT msg) {
  112. return NotifyHookOwner(WM_HK_WindowClientAreaChanged, msg, (LPARAM)hwnd);
  113. }
  114. bool NotifyRectangle(RECT* rect) {
  115. WPARAM w = MAKELONG((SHORT)rect->left, (SHORT)rect->top);
  116. LPARAM l = MAKELONG((SHORT)rect->right, (SHORT)rect->bottom);
  117. return NotifyHookOwner(WM_HK_RectangleChanged, w, l);
  118. }
  119. bool NotifyCursor(HCURSOR cursor) {
  120. return NotifyHookOwner(WM_HK_CursorChanged, 0, (LPARAM)cursor);
  121. }
  122. void ProcessWindowMessage(UINT msg, HWND wnd, WPARAM wParam, LPARAM lParam) {
  123. #ifdef _DEBUG
  124. if ((msg >= diagnostic_min) && (msg <= diagnostic_max))
  125. PostThreadMessage(hook_owner, WM_HK_Diagnostic, msg, (LPARAM)wnd);
  126. #endif
  127. if (!IsWindowVisible(wnd)) return;
  128. switch (msg) {
  129. // -=- Border update events
  130. case WM_NCPAINT:
  131. case WM_NCACTIVATE:
  132. NotifyWindowBorder(wnd, msg);
  133. break;
  134. // -=- Client area update events
  135. case BM_SETCHECK:
  136. case BM_SETSTATE:
  137. case EM_SETSEL:
  138. case WM_CHAR:
  139. case WM_ENABLE:
  140. case WM_KEYUP:
  141. case WM_LBUTTONUP:
  142. case WM_MBUTTONUP:
  143. case WM_PALETTECHANGED:
  144. case WM_RBUTTONUP:
  145. case WM_SYSCOLORCHANGE:
  146. case WM_SETTEXT:
  147. case WM_SETFOCUS:
  148. //case WM_TIMER:
  149. NotifyWindowClientArea(wnd, msg);
  150. break;
  151. case WM_HSCROLL:
  152. case WM_VSCROLL:
  153. if (((int) LOWORD(wParam) == SB_THUMBTRACK) || ((int) LOWORD(wParam) == SB_ENDSCROLL))
  154. NotifyWindow(wnd, msg);
  155. break;
  156. case WM_WINDOWPOSCHANGING:
  157. case WM_DESTROY:
  158. {
  159. RECT wrect;
  160. if (GetWindowRect(wnd, &wrect)) {
  161. NotifyRectangle(&wrect);
  162. }
  163. }
  164. break;
  165. case WM_WINDOWPOSCHANGED:
  166. NotifyWindow(wnd, msg);
  167. break;
  168. case WM_PAINT:
  169. // *** could improve this
  170. NotifyWindowClientArea(wnd, msg);
  171. break;
  172. // Handle pop-up menus appearing
  173. case 482:
  174. NotifyWindow(wnd, 482);
  175. break;
  176. // Handle pop-up menus having items selected
  177. case 485:
  178. {
  179. HANDLE prop = GetProp(wnd, (LPCTSTR) (intptr_t) ATOM_Popup_Selection);
  180. if (prop != (HANDLE) wParam) {
  181. NotifyWindow(wnd, 485);
  182. SetProp(wnd,
  183. (LPCTSTR) (intptr_t) ATOM_Popup_Selection,
  184. (HANDLE) wParam);
  185. }
  186. }
  187. break;
  188. case WM_NCMOUSEMOVE:
  189. case WM_MOUSEMOVE:
  190. if (enable_cursor_shape) {
  191. HCURSOR new_cursor = GetCursor();
  192. if (new_cursor != cursor) {
  193. cursor = new_cursor;
  194. NotifyCursor(cursor);
  195. }
  196. }
  197. break;
  198. /* ***
  199. if (prf_use_GetUpdateRect)
  200. {
  201. HRGN region;
  202. region = CreateRectRgn(0, 0, 0, 0);
  203. // Get the affected region
  204. if (GetUpdateRgn(hWnd, region, FALSE) != ERROR)
  205. {
  206. int buffsize;
  207. UINT x;
  208. RGNDATA *buff;
  209. POINT TopLeft;
  210. // Get the top-left point of the client area
  211. TopLeft.x = 0;
  212. TopLeft.y = 0;
  213. if (!ClientToScreen(hWnd, &TopLeft))
  214. break;
  215. // Get the size of buffer required
  216. buffsize = GetRegionData(region, 0, 0);
  217. if (buffsize != 0)
  218. {
  219. buff = (RGNDATA *) new BYTE [buffsize];
  220. if (buff == NULL)
  221. break;
  222. // Now get the region data
  223. if(GetRegionData(region, buffsize, buff))
  224. {
  225. for (x=0; x<(buff->rdh.nCount); x++)
  226. {
  227. // Obtain the rectangles from the list
  228. RECT *urect = (RECT *) (((BYTE *) buff) + sizeof(RGNDATAHEADER) + (x * sizeof(RECT)));
  229. SendDeferredUpdateRect(
  230. hWnd,
  231. (SHORT) (TopLeft.x + urect->left),
  232. (SHORT) (TopLeft.y + urect->top),
  233. (SHORT) (TopLeft.x + urect->right),
  234. (SHORT) (TopLeft.y + urect->bottom)
  235. );
  236. }
  237. }
  238. delete [] buff;
  239. }
  240. }
  241. // Now free the region
  242. if (region != NULL)
  243. DeleteObject(region);
  244. }
  245. */
  246. };
  247. }
  248. LRESULT CALLBACK HookCallWndProc(int nCode, WPARAM wParam, LPARAM lParam) {
  249. if (nCode == HC_ACTION) {
  250. CWPSTRUCT* info = (CWPSTRUCT*) lParam;
  251. ProcessWindowMessage(info->message, info->hwnd, info->wParam, info->lParam);
  252. }
  253. return CallNextHookEx(hook_CallWndProc, nCode, wParam, lParam);
  254. }
  255. LRESULT CALLBACK HookCallWndProcRet(int nCode, WPARAM wParam, LPARAM lParam) {
  256. if (nCode == HC_ACTION) {
  257. CWPRETSTRUCT* info = (CWPRETSTRUCT*) lParam;
  258. ProcessWindowMessage(info->message, info->hwnd, info->wParam, info->lParam);
  259. }
  260. return CallNextHookEx(hook_CallWndProcRet, nCode, wParam, lParam);
  261. }
  262. LRESULT CALLBACK HookGetMessage(int nCode, WPARAM wParam, LPARAM lParam) {
  263. if (nCode == HC_ACTION) {
  264. if (wParam & PM_REMOVE) {
  265. MSG* msg = (MSG*) lParam;
  266. ProcessWindowMessage(msg->message, msg->hwnd, msg->wParam, msg->lParam);
  267. }
  268. }
  269. return CallNextHookEx(hook_GetMessage, nCode, wParam, lParam);
  270. }
  271. LRESULT CALLBACK HookDialogMessage(int nCode, WPARAM wParam, LPARAM lParam) {
  272. if (nCode == HC_ACTION) {
  273. MSG* msg = (MSG*) lParam;
  274. ProcessWindowMessage(msg->message, msg->hwnd, msg->wParam, msg->lParam);
  275. }
  276. return CallNextHookEx(hook_DialogMessage, nCode, wParam, lParam);
  277. }
  278. // - WM_Hooks_Install
  279. BOOL WM_Hooks_Install(DWORD owner, DWORD thread) {
  280. // - Are there already hooks set?
  281. if (hook_owner) {
  282. if (!PostThreadMessage(hook_owner, WM_HK_PingThread, 0, 0)) {
  283. WM_Hooks_Remove(hook_owner);
  284. } else {
  285. return FALSE;
  286. }
  287. }
  288. // - Initialise the hooks
  289. hook_owner = owner;
  290. hook_target = thread;
  291. hook_CallWndProc = SetWindowsHookEx(WH_CALLWNDPROC, HookCallWndProc, dll_instance, thread);
  292. hook_CallWndProcRet = SetWindowsHookEx(WH_CALLWNDPROCRET, HookCallWndProcRet, dll_instance, thread);
  293. hook_GetMessage = SetWindowsHookEx(WH_GETMESSAGE, HookGetMessage, dll_instance, thread);
  294. hook_DialogMessage = SetWindowsHookEx(WH_SYSMSGFILTER, HookDialogMessage, dll_instance, thread);
  295. if (!hook_CallWndProc /*|| !hook_CallWndProcRet*/ || !hook_GetMessage || !hook_DialogMessage) {
  296. WM_Hooks_Remove(owner);
  297. return FALSE;
  298. }
  299. return TRUE;
  300. }
  301. // - WM_Hooks_Remove
  302. BOOL WM_Hooks_Remove(DWORD owner) {
  303. if (owner != hook_owner) return FALSE;
  304. if (hook_CallWndProc) {
  305. UnhookWindowsHookEx(hook_CallWndProc);
  306. hook_CallWndProc = 0;
  307. }
  308. if (hook_CallWndProcRet) {
  309. UnhookWindowsHookEx(hook_CallWndProcRet);
  310. hook_CallWndProcRet = 0;
  311. }
  312. if (hook_GetMessage) {
  313. UnhookWindowsHookEx(hook_GetMessage);
  314. hook_GetMessage = 0;
  315. }
  316. if (hook_DialogMessage) {
  317. UnhookWindowsHookEx(hook_DialogMessage);
  318. hook_DialogMessage = 0;
  319. }
  320. hook_owner = 0;
  321. hook_target = 0;
  322. return TRUE;
  323. }
  324. //
  325. // -=- User input hooks
  326. //
  327. HHOOK hook_keyboard SHARED = 0;
  328. HHOOK hook_pointer SHARED = 0;
  329. bool enable_real_ptr SHARED = true;
  330. bool enable_synth_ptr SHARED = true;
  331. bool enable_real_kbd SHARED = true;
  332. bool enable_synth_kbd SHARED = true;
  333. #ifdef WH_KEYBOARD_LL
  334. LRESULT CALLBACK HookKeyboardHook(int nCode, WPARAM wParam, LPARAM lParam) {
  335. if (nCode >= 0) {
  336. KBDLLHOOKSTRUCT* info = (KBDLLHOOKSTRUCT*) lParam;
  337. bool real_event = (info->flags & LLKHF_INJECTED) == 0;
  338. if ((real_event && !enable_real_kbd) ||
  339. (!real_event && !enable_synth_kbd)) {
  340. return 1;
  341. }
  342. }
  343. return CallNextHookEx(hook_keyboard, nCode, wParam, lParam);
  344. }
  345. LRESULT CALLBACK HookPointerHook(int nCode, WPARAM wParam, LPARAM lParam) {
  346. if (nCode >= 0) {
  347. MSLLHOOKSTRUCT* info = (MSLLHOOKSTRUCT*) lParam;
  348. bool real_event = (info->flags & LLMHF_INJECTED) == 0;
  349. if ((real_event && !enable_real_ptr) ||
  350. (!real_event && !enable_synth_ptr)) {
  351. return 1;
  352. }
  353. }
  354. return CallNextHookEx(hook_keyboard, nCode, wParam, lParam);
  355. }
  356. bool RefreshInputHooks() {
  357. bool success = true;
  358. bool set_ptr_hook = !enable_real_ptr || !enable_synth_ptr;
  359. bool set_kbd_hook = !enable_real_kbd || !enable_synth_kbd;
  360. if (hook_keyboard && !set_kbd_hook) {
  361. UnhookWindowsHookEx(hook_keyboard);
  362. hook_keyboard = 0;
  363. }
  364. if (hook_pointer && !set_ptr_hook) {
  365. UnhookWindowsHookEx(hook_pointer);
  366. hook_pointer = 0;
  367. }
  368. if (!hook_keyboard && set_kbd_hook) {
  369. hook_keyboard = SetWindowsHookEx(WH_KEYBOARD_LL, HookKeyboardHook, dll_instance, 0);
  370. if (!hook_keyboard) success = false;
  371. }
  372. if (!hook_pointer && set_ptr_hook) {
  373. hook_pointer = SetWindowsHookEx(WH_MOUSE_LL, HookPointerHook, dll_instance, 0);
  374. if (!hook_pointer) success = false;
  375. }
  376. return success;
  377. }
  378. #else
  379. #pragma message(" NOTE: low-level mouse and keyboard hooks not supported")
  380. #endif
  381. // - WM_Hooks_EnableRealInputs
  382. BOOL WM_Hooks_EnableRealInputs(BOOL pointer, BOOL keyboard) {
  383. #ifdef WH_KEYBOARD_LL
  384. enable_real_ptr = pointer!=0;
  385. enable_real_kbd = keyboard!=0;
  386. return RefreshInputHooks();
  387. #else
  388. return FALSE;
  389. #endif
  390. }
  391. // - WM_Hooks_EnableSynthInputs
  392. BOOL WM_Hooks_EnableSynthInputs(BOOL pointer, BOOL keyboard) {
  393. #ifdef WH_KEYBOARD_LL
  394. enable_synth_ptr = pointer!=0;
  395. enable_synth_kbd = keyboard!=0;
  396. return RefreshInputHooks();
  397. #else
  398. return FALSE;
  399. #endif
  400. }
  401. // - WM_Hooks_EnableCursorShape
  402. BOOL WM_Hooks_EnableCursorShape(BOOL enable) {
  403. enable_cursor_shape = enable;
  404. return TRUE;
  405. }