hook.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /*
  2. * Server-side window hooks support
  3. *
  4. * Copyright (C) 2002 Alexandre Julliard
  5. * Copyright (C) 2005 Dmitry Timoshkov
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  20. */
  21. #include "config.h"
  22. #include "wine/port.h"
  23. #include <assert.h>
  24. #include <stdarg.h>
  25. #include <stdio.h>
  26. #include "ntstatus.h"
  27. #define WIN32_NO_STATUS
  28. #include "windef.h"
  29. #include "winbase.h"
  30. #include "winuser.h"
  31. #include "winternl.h"
  32. #include "object.h"
  33. #include "process.h"
  34. #include "request.h"
  35. #include "user.h"
  36. struct hook_table;
  37. struct hook
  38. {
  39. struct list chain; /* hook chain entry */
  40. user_handle_t handle; /* user handle for this hook */
  41. struct process *process; /* process the hook is set to */
  42. struct thread *thread; /* thread the hook is set to */
  43. struct thread *owner; /* owner of the out of context hook */
  44. struct hook_table *table; /* hook table that contains this hook */
  45. int index; /* hook table index */
  46. int event_min;
  47. int event_max;
  48. int flags;
  49. client_ptr_t proc; /* hook function */
  50. int unicode; /* is it a unicode hook? */
  51. WCHAR *module; /* module name for global hooks */
  52. data_size_t module_size;
  53. };
  54. #define WH_WINEVENT (WH_MAXHOOK+1)
  55. #define NB_HOOKS (WH_WINEVENT-WH_MINHOOK+1)
  56. #define HOOK_ENTRY(p) LIST_ENTRY( (p), struct hook, chain )
  57. struct hook_table
  58. {
  59. struct object obj; /* object header */
  60. struct list hooks[NB_HOOKS]; /* array of hook chains */
  61. int counts[NB_HOOKS]; /* use counts for each hook chain */
  62. };
  63. static void hook_table_dump( struct object *obj, int verbose );
  64. static void hook_table_destroy( struct object *obj );
  65. static const struct object_ops hook_table_ops =
  66. {
  67. sizeof(struct hook_table), /* size */
  68. &no_type, /* type */
  69. hook_table_dump, /* dump */
  70. no_add_queue, /* add_queue */
  71. NULL, /* remove_queue */
  72. NULL, /* signaled */
  73. NULL, /* satisfied */
  74. no_signal, /* signal */
  75. no_get_fd, /* get_fd */
  76. default_map_access, /* map_access */
  77. default_get_sd, /* get_sd */
  78. default_set_sd, /* set_sd */
  79. no_get_full_name, /* get_full_name */
  80. no_lookup_name, /* lookup_name */
  81. no_link_name, /* link_name */
  82. NULL, /* unlink_name */
  83. no_open_file, /* open_file */
  84. no_kernel_obj_list, /* get_kernel_obj_list */
  85. no_close_handle, /* close_handle */
  86. hook_table_destroy /* destroy */
  87. };
  88. /* create a new hook table */
  89. static struct hook_table *alloc_hook_table(void)
  90. {
  91. struct hook_table *table;
  92. int i;
  93. if ((table = alloc_object( &hook_table_ops )))
  94. {
  95. for (i = 0; i < NB_HOOKS; i++)
  96. {
  97. list_init( &table->hooks[i] );
  98. table->counts[i] = 0;
  99. }
  100. }
  101. return table;
  102. }
  103. static struct hook_table *get_global_hooks( struct thread *thread )
  104. {
  105. struct hook_table *table;
  106. struct desktop *desktop;
  107. if (!thread->desktop) return NULL;
  108. if (!(desktop = get_thread_desktop( thread, 0 ))) return NULL;
  109. table = desktop->global_hooks;
  110. release_object( desktop );
  111. return table;
  112. }
  113. /* create a new hook and add it to the specified table */
  114. static struct hook *add_hook( struct desktop *desktop, struct thread *thread, int index, int global )
  115. {
  116. struct hook *hook;
  117. struct hook_table *table = global ? desktop->global_hooks : get_queue_hooks(thread);
  118. if (!table)
  119. {
  120. if (!(table = alloc_hook_table())) return NULL;
  121. if (global) desktop->global_hooks = table;
  122. else set_queue_hooks( thread, table );
  123. }
  124. if (!(hook = mem_alloc( sizeof(*hook) ))) return NULL;
  125. if (!(hook->handle = alloc_user_handle( hook, USER_HOOK )))
  126. {
  127. free( hook );
  128. return NULL;
  129. }
  130. hook->thread = thread ? (struct thread *)grab_object( thread ) : NULL;
  131. hook->table = table;
  132. hook->index = index;
  133. list_add_head( &table->hooks[index], &hook->chain );
  134. if (thread) thread->desktop_users++;
  135. return hook;
  136. }
  137. /* free a hook, removing it from its chain */
  138. static void free_hook( struct hook *hook )
  139. {
  140. free_user_handle( hook->handle );
  141. free( hook->module );
  142. if (hook->thread)
  143. {
  144. assert( hook->thread->desktop_users > 0 );
  145. hook->thread->desktop_users--;
  146. release_object( hook->thread );
  147. }
  148. if (hook->process) release_object( hook->process );
  149. release_object( hook->owner );
  150. list_remove( &hook->chain );
  151. free( hook );
  152. }
  153. /* find a hook from its index and proc */
  154. static struct hook *find_hook( struct thread *thread, int index, client_ptr_t proc )
  155. {
  156. struct list *p;
  157. struct hook_table *table = get_queue_hooks( thread );
  158. if (table)
  159. {
  160. LIST_FOR_EACH( p, &table->hooks[index] )
  161. {
  162. struct hook *hook = HOOK_ENTRY( p );
  163. if (hook->proc == proc) return hook;
  164. }
  165. }
  166. return NULL;
  167. }
  168. /* get the first hook in the chain */
  169. static inline struct hook *get_first_hook( struct hook_table *table, int index )
  170. {
  171. struct list *elem = list_head( &table->hooks[index] );
  172. return elem ? HOOK_ENTRY( elem ) : NULL;
  173. }
  174. /* check if a given hook should run in the owner thread instead of the current thread */
  175. static inline int run_hook_in_owner_thread( struct hook *hook )
  176. {
  177. if ((hook->index == WH_MOUSE_LL - WH_MINHOOK ||
  178. hook->index == WH_KEYBOARD_LL - WH_MINHOOK))
  179. return hook->owner != current;
  180. return 0;
  181. }
  182. /* check if a given hook should run in the current thread */
  183. static inline int run_hook_in_current_thread( struct hook *hook )
  184. {
  185. if (hook->process && hook->process != current->process) return 0;
  186. if ((hook->flags & WINEVENT_SKIPOWNPROCESS) && hook->process == current->process) return 0;
  187. if (hook->thread && hook->thread != current) return 0;
  188. if ((hook->flags & WINEVENT_SKIPOWNTHREAD) && hook->thread == current) return 0;
  189. /* don't run low-level hooks in processes suspended for debugging */
  190. if (run_hook_in_owner_thread( hook ) && hook->owner->process->suspend) return 0;
  191. return 1;
  192. }
  193. /* find the first non-deleted hook in the chain */
  194. static inline struct hook *get_first_valid_hook( struct hook_table *table, int index,
  195. int event, user_handle_t win,
  196. int object_id, int child_id )
  197. {
  198. struct hook *hook = get_first_hook( table, index );
  199. while (hook)
  200. {
  201. if (hook->proc && run_hook_in_current_thread( hook ))
  202. {
  203. if (event >= hook->event_min && event <= hook->event_max)
  204. {
  205. if (hook->flags & WINEVENT_INCONTEXT) return hook;
  206. /* only winevent hooks may be out of context */
  207. assert(hook->index + WH_MINHOOK == WH_WINEVENT);
  208. post_win_event( hook->owner, event, win, object_id, child_id,
  209. hook->proc, hook->module, hook->module_size,
  210. hook->handle );
  211. }
  212. }
  213. hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
  214. }
  215. return hook;
  216. }
  217. /* find the next hook in the chain, skipping the deleted ones */
  218. static struct hook *get_next_hook( struct thread *thread, struct hook *hook, int event,
  219. user_handle_t win, int object_id, int child_id )
  220. {
  221. struct hook_table *global_hooks, *table = hook->table;
  222. int index = hook->index;
  223. while ((hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) )))
  224. {
  225. if (hook->proc && run_hook_in_current_thread( hook ))
  226. {
  227. if (event >= hook->event_min && event <= hook->event_max)
  228. {
  229. if (hook->flags & WINEVENT_INCONTEXT) return hook;
  230. /* only winevent hooks may be out of context */
  231. assert(hook->index + WH_MINHOOK == WH_WINEVENT);
  232. post_win_event( hook->owner, event, win, object_id, child_id,
  233. hook->proc, hook->module, hook->module_size,
  234. hook->handle );
  235. }
  236. }
  237. }
  238. global_hooks = get_global_hooks( thread );
  239. if (global_hooks && table != global_hooks) /* now search through the global table */
  240. {
  241. hook = get_first_valid_hook( global_hooks, index, event, win, object_id, child_id );
  242. }
  243. return hook;
  244. }
  245. static void hook_table_dump( struct object *obj, int verbose )
  246. {
  247. /* struct hook_table *table = (struct hook_table *)obj; */
  248. fprintf( stderr, "Hook table\n" );
  249. }
  250. static void hook_table_destroy( struct object *obj )
  251. {
  252. int i;
  253. struct hook *hook;
  254. struct hook_table *table = (struct hook_table *)obj;
  255. for (i = 0; i < NB_HOOKS; i++)
  256. {
  257. while ((hook = get_first_hook( table, i )) != NULL) free_hook( hook );
  258. }
  259. }
  260. /* remove a hook, freeing it if the chain is not in use */
  261. static void remove_hook( struct hook *hook )
  262. {
  263. if (hook->table->counts[hook->index])
  264. hook->proc = 0; /* chain is in use, just mark it and return */
  265. else
  266. free_hook( hook );
  267. }
  268. /* release a hook chain, removing deleted hooks if the use count drops to 0 */
  269. static void release_hook_chain( struct hook_table *table, int index )
  270. {
  271. if (!table->counts[index]) /* use count shouldn't already be 0 */
  272. {
  273. set_error( STATUS_INVALID_PARAMETER );
  274. return;
  275. }
  276. if (!--table->counts[index])
  277. {
  278. struct hook *hook = get_first_hook( table, index );
  279. while (hook)
  280. {
  281. struct hook *next = HOOK_ENTRY( list_next( &table->hooks[hook->index], &hook->chain ) );
  282. if (!hook->proc) free_hook( hook );
  283. hook = next;
  284. }
  285. }
  286. }
  287. /* remove all global hooks owned by a given thread */
  288. void remove_thread_hooks( struct thread *thread )
  289. {
  290. struct hook_table *global_hooks = get_global_hooks( thread );
  291. int index;
  292. if (!global_hooks) return;
  293. /* only low-level keyboard/mouse global hooks can be owned by a thread */
  294. for (index = WH_KEYBOARD_LL - WH_MINHOOK; index <= WH_MOUSE_LL - WH_MINHOOK; index++)
  295. {
  296. struct hook *hook = get_first_hook( global_hooks, index );
  297. while (hook)
  298. {
  299. struct hook *next = HOOK_ENTRY( list_next( &global_hooks->hooks[index], &hook->chain ) );
  300. if (hook->thread == thread) remove_hook( hook );
  301. hook = next;
  302. }
  303. }
  304. }
  305. /* get a bitmap of active hooks in a hook table */
  306. static int is_hook_active( struct hook_table *table, int index )
  307. {
  308. struct hook *hook = get_first_hook( table, index );
  309. while (hook)
  310. {
  311. if (hook->proc && run_hook_in_current_thread( hook )) return 1;
  312. hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
  313. }
  314. return 0;
  315. }
  316. /* get a bitmap of all active hooks for the current thread */
  317. unsigned int get_active_hooks(void)
  318. {
  319. struct hook_table *table = get_queue_hooks( current );
  320. struct hook_table *global_hooks = get_global_hooks( current );
  321. unsigned int ret = 1u << 31; /* set high bit to indicate that the bitmap is valid */
  322. int id;
  323. for (id = WH_MINHOOK; id <= WH_WINEVENT; id++)
  324. {
  325. if ((table && is_hook_active( table, id - WH_MINHOOK )) ||
  326. (global_hooks && is_hook_active( global_hooks, id - WH_MINHOOK )))
  327. ret |= 1 << (id - WH_MINHOOK);
  328. }
  329. return ret;
  330. }
  331. /* return the thread that owns the first global hook */
  332. struct thread *get_first_global_hook( int id )
  333. {
  334. struct hook *hook;
  335. struct hook_table *global_hooks = get_global_hooks( current );
  336. if (!global_hooks) return NULL;
  337. if (!(hook = get_first_valid_hook( global_hooks, id - WH_MINHOOK, EVENT_MIN, 0, 0, 0 ))) return NULL;
  338. return hook->owner;
  339. }
  340. /* set a window hook */
  341. DECL_HANDLER(set_hook)
  342. {
  343. struct process *process = NULL;
  344. struct thread *thread = NULL;
  345. struct desktop *desktop;
  346. struct hook *hook;
  347. WCHAR *module;
  348. int global;
  349. data_size_t module_size = get_req_data_size();
  350. if (!req->proc || req->id < WH_MINHOOK || req->id > WH_WINEVENT)
  351. {
  352. set_error( STATUS_INVALID_PARAMETER );
  353. return;
  354. }
  355. if (!(desktop = get_thread_desktop( current, DESKTOP_HOOKCONTROL ))) return;
  356. if (req->pid && !(process = get_process_from_id( req->pid ))) goto done;
  357. if (req->tid)
  358. {
  359. if (!(thread = get_thread_from_id( req->tid ))) goto done;
  360. if (process && process != thread->process)
  361. {
  362. set_error( STATUS_INVALID_PARAMETER );
  363. goto done;
  364. }
  365. }
  366. if (req->id == WH_KEYBOARD_LL || req->id == WH_MOUSE_LL)
  367. {
  368. /* low-level hardware hooks are special: always global, but without a module */
  369. if (thread)
  370. {
  371. set_error( STATUS_INVALID_PARAMETER );
  372. goto done;
  373. }
  374. module = NULL;
  375. global = 1;
  376. }
  377. else if (!req->tid)
  378. {
  379. /* out of context hooks do not need a module handle */
  380. if (!module_size && (req->flags & WINEVENT_INCONTEXT))
  381. {
  382. set_error( STATUS_INVALID_PARAMETER );
  383. goto done;
  384. }
  385. if (!(module = memdup( get_req_data(), module_size ))) goto done;
  386. global = 1;
  387. }
  388. else
  389. {
  390. /* module is optional only if hook is in current process */
  391. if (!module_size)
  392. {
  393. module = NULL;
  394. if (thread->process != current->process)
  395. {
  396. set_error( STATUS_INVALID_PARAMETER );
  397. goto done;
  398. }
  399. }
  400. else if (!(module = memdup( get_req_data(), module_size ))) goto done;
  401. global = 0;
  402. }
  403. if ((hook = add_hook( desktop, thread, req->id - WH_MINHOOK, global )))
  404. {
  405. hook->owner = (struct thread *)grab_object( current );
  406. hook->process = process ? (struct process *)grab_object( process ) : NULL;
  407. hook->event_min = req->event_min;
  408. hook->event_max = req->event_max;
  409. hook->flags = req->flags;
  410. hook->proc = req->proc;
  411. hook->unicode = req->unicode;
  412. hook->module = module;
  413. hook->module_size = module_size;
  414. reply->handle = hook->handle;
  415. reply->active_hooks = get_active_hooks();
  416. }
  417. else free( module );
  418. done:
  419. if (process) release_object( process );
  420. if (thread) release_object( thread );
  421. release_object( desktop );
  422. }
  423. /* remove a window hook */
  424. DECL_HANDLER(remove_hook)
  425. {
  426. struct hook *hook;
  427. if (req->handle)
  428. {
  429. if (!(hook = get_user_object( req->handle, USER_HOOK )))
  430. {
  431. set_error( STATUS_INVALID_HANDLE );
  432. return;
  433. }
  434. }
  435. else
  436. {
  437. if (!req->proc || req->id < WH_MINHOOK || req->id > WH_WINEVENT)
  438. {
  439. set_error( STATUS_INVALID_PARAMETER );
  440. return;
  441. }
  442. if (!(hook = find_hook( current, req->id - WH_MINHOOK, req->proc )))
  443. {
  444. set_error( STATUS_INVALID_PARAMETER );
  445. return;
  446. }
  447. }
  448. remove_hook( hook );
  449. reply->active_hooks = get_active_hooks();
  450. }
  451. /* start calling a hook chain */
  452. DECL_HANDLER(start_hook_chain)
  453. {
  454. struct hook *hook;
  455. struct hook_table *table = get_queue_hooks( current );
  456. struct hook_table *global_table = get_global_hooks( current );
  457. if (req->id < WH_MINHOOK || req->id > WH_WINEVENT)
  458. {
  459. set_error( STATUS_INVALID_PARAMETER );
  460. return;
  461. }
  462. reply->active_hooks = get_active_hooks();
  463. if (!table || !(hook = get_first_valid_hook( table, req->id - WH_MINHOOK, req->event,
  464. req->window, req->object_id, req->child_id )))
  465. {
  466. /* try global table */
  467. if (!global_table || !(hook = get_first_valid_hook( global_table, req->id - WH_MINHOOK, req->event,
  468. req->window, req->object_id, req->child_id )))
  469. return; /* no hook set */
  470. }
  471. if (run_hook_in_owner_thread( hook ))
  472. {
  473. reply->pid = get_process_id( hook->owner->process );
  474. reply->tid = get_thread_id( hook->owner );
  475. }
  476. else
  477. {
  478. reply->pid = 0;
  479. reply->tid = 0;
  480. }
  481. reply->proc = hook->proc;
  482. reply->handle = hook->handle;
  483. reply->unicode = hook->unicode;
  484. if (table) table->counts[hook->index]++;
  485. if (global_table) global_table->counts[hook->index]++;
  486. if (hook->module) set_reply_data( hook->module, hook->module_size );
  487. }
  488. /* finished calling a hook chain */
  489. DECL_HANDLER(finish_hook_chain)
  490. {
  491. struct hook_table *table = get_queue_hooks( current );
  492. struct hook_table *global_hooks = get_global_hooks( current );
  493. int index = req->id - WH_MINHOOK;
  494. if (req->id < WH_MINHOOK || req->id > WH_WINEVENT)
  495. {
  496. set_error( STATUS_INVALID_PARAMETER );
  497. return;
  498. }
  499. if (table) release_hook_chain( table, index );
  500. if (global_hooks) release_hook_chain( global_hooks, index );
  501. }
  502. /* get the hook information */
  503. DECL_HANDLER(get_hook_info)
  504. {
  505. struct hook *hook;
  506. if (!(hook = get_user_object( req->handle, USER_HOOK ))) return;
  507. if (hook->thread && (hook->thread != current))
  508. {
  509. set_error( STATUS_INVALID_HANDLE );
  510. return;
  511. }
  512. if (req->get_next && !(hook = get_next_hook( current, hook, req->event, req->window,
  513. req->object_id, req->child_id )))
  514. return;
  515. reply->handle = hook->handle;
  516. reply->id = hook->index + WH_MINHOOK;
  517. reply->unicode = hook->unicode;
  518. if (hook->module) set_reply_data( hook->module, min(hook->module_size,get_reply_max_size()) );
  519. if (run_hook_in_owner_thread( hook ))
  520. {
  521. reply->pid = get_process_id( hook->owner->process );
  522. reply->tid = get_thread_id( hook->owner );
  523. }
  524. else
  525. {
  526. reply->pid = 0;
  527. reply->tid = 0;
  528. }
  529. reply->proc = hook->proc;
  530. }