Rewrote hook support to store the hook chain in the server.
Split off 16-bit hook functions and re-implemented them on top of the
32-bit ones; system-wide 16-bit hooks are no longer supported at this
point.
diff --git a/server/Makefile.in b/server/Makefile.in
index 6486a5d..91dde18 100644
--- a/server/Makefile.in
+++ b/server/Makefile.in
@@ -18,6 +18,7 @@
event.c \
file.c \
handle.c \
+ hook.c \
main.c \
mapping.c \
mutex.c \
diff --git a/server/hook.c b/server/hook.c
new file mode 100644
index 0000000..aa1e493
--- /dev/null
+++ b/server/hook.c
@@ -0,0 +1,316 @@
+/*
+ * Server-side window hooks support
+ *
+ * Copyright (C) 2002 Alexandre Julliard
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include "config.h"
+#include "wine/port.h"
+
+#include <assert.h>
+#include <stdio.h>
+
+#include "winbase.h"
+#include "winuser.h"
+
+#include "object.h"
+#include "request.h"
+#include "user.h"
+
+struct hook_table;
+
+struct hook
+{
+ struct list chain; /* hook chain entry */
+ user_handle_t handle; /* user handle for this hook */
+ struct thread *thread; /* thread owning the hook */
+ int index; /* hook table index */
+ void *proc; /* hook function */
+ int unicode; /* is it a unicode hook? */
+};
+
+#define NB_HOOKS (WH_MAXHOOK-WH_MINHOOK+1)
+#define HOOK_ENTRY(p) LIST_ENTRY( (p), struct hook, chain )
+
+struct hook_table
+{
+ struct object obj; /* object header */
+ struct list hooks[NB_HOOKS]; /* array of hook chains */
+ int counts[NB_HOOKS]; /* use counts for each hook chain */
+};
+
+static void hook_table_dump( struct object *obj, int verbose );
+static void hook_table_destroy( struct object *obj );
+
+static const struct object_ops hook_table_ops =
+{
+ sizeof(struct hook_table), /* size */
+ hook_table_dump, /* dump */
+ no_add_queue, /* add_queue */
+ NULL, /* remove_queue */
+ NULL, /* signaled */
+ NULL, /* satisfied */
+ NULL, /* get_poll_events */
+ NULL, /* poll_event */
+ no_get_fd, /* get_fd */
+ no_flush, /* flush */
+ no_get_file_info, /* get_file_info */
+ NULL, /* queue_async */
+ hook_table_destroy /* destroy */
+};
+
+
+/* create a new hook table */
+static struct hook_table *alloc_hook_table(void)
+{
+ struct hook_table *table;
+ int i;
+
+ if ((table = alloc_object( &hook_table_ops, -1 )))
+ {
+ for (i = 0; i < NB_HOOKS; i++)
+ {
+ list_init( &table->hooks[i] );
+ table->counts[i] = 0;
+ }
+ }
+ return table;
+}
+
+/* create a new hook and add it to the specified table */
+static struct hook *add_hook( struct thread *thread, int index )
+{
+ struct hook *hook;
+ struct hook_table *table = thread->hooks;
+
+ if (!table)
+ {
+ if (!(table = alloc_hook_table())) return NULL;
+ thread->hooks = table;
+ }
+ if (!(hook = mem_alloc( sizeof(*hook) ))) return NULL;
+
+ if (!(hook->handle = alloc_user_handle( hook, USER_HOOK )))
+ {
+ free( hook );
+ return NULL;
+ }
+ hook->thread = thread ? (struct thread *)grab_object( thread ) : NULL;
+ hook->index = index;
+ list_add_head( &table->hooks[index], &hook->chain );
+ return hook;
+}
+
+/* free a hook, removing it from its chain */
+static void free_hook( struct hook *hook )
+{
+ free_user_handle( hook->handle );
+ if (hook->thread) release_object( hook->thread );
+ list_remove( &hook->chain );
+ free( hook );
+}
+
+/* find a hook from its index and proc */
+static struct hook *find_hook( struct thread *thread, int index, void *proc )
+{
+ struct list *p;
+ struct hook_table *table = thread->hooks;
+
+ if (table)
+ {
+ LIST_FOR_EACH( p, &table->hooks[index] )
+ {
+ struct hook *hook = HOOK_ENTRY( p );
+ if (hook->proc == proc) return hook;
+ }
+ }
+ return NULL;
+}
+
+/* get the hook table that a given hook belongs to */
+inline static struct hook_table *get_table( struct hook *hook )
+{
+ return hook->thread->hooks;
+}
+
+/* get the first hook in the chain */
+inline static struct hook *get_first_hook( struct hook_table *table, int index )
+{
+ struct list *elem = list_head( &table->hooks[index] );
+ return elem ? HOOK_ENTRY( elem ) : NULL;
+}
+
+/* find the next hook in the chain, skipping the deleted ones */
+static struct hook *get_next_hook( struct hook *hook )
+{
+ struct hook_table *table = get_table( hook );
+ struct hook *next;
+
+ while ((next = HOOK_ENTRY( list_next( &table->hooks[hook->index], &hook->chain ) )))
+ {
+ if (next->proc) break;
+ }
+ return next;
+}
+
+static void hook_table_dump( struct object *obj, int verbose )
+{
+/* struct hook_table *table = (struct hook_table *)obj; */
+ fprintf( stderr, "Hook table\n" );
+}
+
+static void hook_table_destroy( struct object *obj )
+{
+ int i;
+ struct hook *hook;
+ struct hook_table *table = (struct hook_table *)obj;
+
+ for (i = 0; i < NB_HOOKS; i++)
+ {
+ while ((hook = get_first_hook( table, i )) != NULL) free_hook( hook );
+ }
+}
+
+/* remove a hook, freeing it if the chain is not in use */
+static void remove_hook( struct hook *hook )
+{
+ struct hook_table *table = get_table( hook );
+ if (table->counts[hook->index])
+ hook->proc = NULL; /* chain is in use, just mark it and return */
+ else
+ free_hook( hook );
+}
+
+/* release a hook chain, removing deleted hooks if the use count drops to 0 */
+static void release_hook_chain( struct hook_table *table, int index )
+{
+ if (!table->counts[index]) /* use count shouldn't already be 0 */
+ {
+ set_error( STATUS_INVALID_PARAMETER );
+ return;
+ }
+ if (!--table->counts[index])
+ {
+ struct hook *hook = get_first_hook( table, index );
+ while (hook)
+ {
+ struct hook *next = HOOK_ENTRY( list_next( &table->hooks[hook->index], &hook->chain ) );
+ if (!hook->proc) free_hook( hook );
+ hook = next;
+ }
+ }
+}
+
+
+/* set a window hook */
+DECL_HANDLER(set_hook)
+{
+ struct thread *thread;
+ struct hook *hook;
+
+ if (!req->proc || req->id < WH_MINHOOK || req->id > WH_MAXHOOK)
+ {
+ set_error( STATUS_INVALID_PARAMETER );
+ return;
+ }
+ if (!(thread = get_thread_from_id( req->tid ))) return;
+
+ if ((hook = add_hook( thread, req->id - WH_MINHOOK )))
+ {
+ hook->proc = req->proc;
+ hook->unicode = req->unicode;
+ reply->handle = hook->handle;
+ }
+ release_object( thread );
+}
+
+
+/* remove a window hook */
+DECL_HANDLER(remove_hook)
+{
+ struct hook *hook;
+
+ if (req->handle) hook = get_user_object( req->handle, USER_HOOK );
+ else
+ {
+ if (!req->proc || req->id < WH_MINHOOK || req->id > WH_MAXHOOK)
+ {
+ set_error( STATUS_INVALID_PARAMETER );
+ return;
+ }
+ if (!(hook = find_hook( current, req->id - WH_MINHOOK, req->proc )))
+ set_error( STATUS_INVALID_PARAMETER );
+ }
+ if (hook) remove_hook( hook );
+}
+
+
+/* start calling a hook chain */
+DECL_HANDLER(start_hook_chain)
+{
+ struct hook *hook;
+ struct hook_table *table = current->hooks;
+
+ if (req->id < WH_MINHOOK || req->id > WH_MAXHOOK)
+ {
+ set_error( STATUS_INVALID_PARAMETER );
+ return;
+ }
+ if (!table) return; /* no hook set */
+ if (!(hook = get_first_hook( table, req->id - WH_MINHOOK ))) return; /* no hook set */
+ reply->handle = hook->handle;
+ reply->proc = hook->proc;
+ reply->unicode = hook->unicode;
+ table->counts[hook->index]++;
+}
+
+
+/* finished calling a hook chain */
+DECL_HANDLER(finish_hook_chain)
+{
+ struct hook_table *table = current->hooks;
+ int index = req->id - WH_MINHOOK;
+
+ if (req->id < WH_MINHOOK || req->id > WH_MAXHOOK)
+ {
+ set_error( STATUS_INVALID_PARAMETER );
+ return;
+ }
+ if (table) release_hook_chain( table, index );
+}
+
+
+/* get the next hook to call */
+DECL_HANDLER(get_next_hook)
+{
+ struct hook *hook, *next;
+
+ if (!(hook = get_user_object( req->handle, USER_HOOK ))) return;
+ if (hook->thread != current)
+ {
+ set_error( STATUS_INVALID_HANDLE );
+ return;
+ }
+ if ((next = get_next_hook( hook )))
+ {
+ reply->next = next->handle;
+ reply->id = next->index + WH_MINHOOK;
+ reply->proc = next->proc;
+ reply->prev_unicode = hook->unicode;
+ reply->next_unicode = next->unicode;
+ }
+}
diff --git a/server/list.h b/server/list.h
index b4a4c1f..fcf687e 100644
--- a/server/list.h
+++ b/server/list.h
@@ -52,6 +52,34 @@
elem->prev->next = elem->next;
}
+/* get the next element */
+inline static struct list *list_next( struct list *list, struct list *elem )
+{
+ struct list *ret = elem->next;
+ if (elem->next == list) ret = NULL;
+ return ret;
+}
+
+/* get the previous element */
+inline static struct list *list_prev( struct list *list, struct list *elem )
+{
+ struct list *ret = elem->prev;
+ if (elem->prev == list) ret = NULL;
+ return ret;
+}
+
+/* get the first element */
+inline static struct list *list_head( struct list *list )
+{
+ return list_next( list, list );
+}
+
+/* get the last element */
+inline static struct list *list_tail( struct list *list )
+{
+ return list_prev( list, list );
+}
+
/* initialize a list */
inline static void list_init( struct list *list )
{
diff --git a/server/protocol.def b/server/protocol.def
index 325d62c..38c91c4 100644
--- a/server/protocol.def
+++ b/server/protocol.def
@@ -2004,3 +2004,50 @@
#define SET_CARET_POS 0x01 /* set the caret position from x,y */
#define SET_CARET_HIDE 0x02 /* increment the caret hide count */
#define SET_CARET_STATE 0x04 /* set the caret on/off state */
+
+
+/* Set a window hook */
+@REQ(set_hook)
+ int id; /* id of the hook */
+ thread_id_t tid; /* id of thread to set the hook into */
+ void* proc; /* hook procedure */
+ int unicode; /* is it a unicode hook? */
+@REPLY
+ user_handle_t handle; /* handle to the hook */
+@END
+
+
+/* Remove a window hook */
+@REQ(remove_hook)
+ user_handle_t handle; /* handle to the hook */
+ int id; /* id of the hook if handle is 0 */
+ void* proc; /* hook procedure if handle is 0 */
+@END
+
+
+/* Start calling a hook chain */
+@REQ(start_hook_chain)
+ int id; /* id of the hook */
+@REPLY
+ user_handle_t handle; /* handle to the next hook */
+ void* proc; /* hook procedure */
+ int unicode; /* is it a unicode hook? */
+@END
+
+
+/* Finished calling a hook chain */
+@REQ(finish_hook_chain)
+ int id; /* id of the hook */
+@END
+
+
+/* Get the next hook to call */
+@REQ(get_next_hook)
+ user_handle_t handle; /* handle to the current hook */
+@REPLY
+ user_handle_t next; /* handle to the next hook */
+ int id; /* id of the next hook */
+ void* proc; /* next hook procedure */
+ int prev_unicode; /* was the previous a unicode hook? */
+ int next_unicode; /* is the next a unicode hook? */
+@END
diff --git a/server/request.h b/server/request.h
index 7051e88..f5fd97d 100644
--- a/server/request.h
+++ b/server/request.h
@@ -267,6 +267,11 @@
DECL_HANDLER(set_capture_window);
DECL_HANDLER(set_caret_window);
DECL_HANDLER(set_caret_info);
+DECL_HANDLER(set_hook);
+DECL_HANDLER(remove_hook);
+DECL_HANDLER(start_hook_chain);
+DECL_HANDLER(finish_hook_chain);
+DECL_HANDLER(get_next_hook);
#ifdef WANT_REQUEST_HANDLERS
@@ -437,6 +442,11 @@
(req_handler)req_set_capture_window,
(req_handler)req_set_caret_window,
(req_handler)req_set_caret_info,
+ (req_handler)req_set_hook,
+ (req_handler)req_remove_hook,
+ (req_handler)req_start_hook_chain,
+ (req_handler)req_finish_hook_chain,
+ (req_handler)req_get_next_hook,
};
#endif /* WANT_REQUEST_HANDLERS */
diff --git a/server/thread.c b/server/thread.c
index 0338b8b..c29ba46 100644
--- a/server/thread.c
+++ b/server/thread.c
@@ -110,6 +110,7 @@
thread->debug_ctx = NULL;
thread->debug_event = NULL;
thread->queue = NULL;
+ thread->hooks = NULL;
thread->info = NULL;
thread->wait = NULL;
thread->system_apc.head = NULL;
@@ -187,6 +188,7 @@
if (thread->request_fd != -1) close( thread->request_fd );
if (thread->reply_fd != -1) close( thread->reply_fd );
if (thread->wait_fd != -1) close( thread->wait_fd );
+ if (thread->hooks) release_object( thread->hooks );
free_msg_queue( thread );
destroy_thread_windows( thread );
for (i = 0; i < MAX_INFLIGHT_FDS; i++)
@@ -202,6 +204,7 @@
thread->request_fd = -1;
thread->reply_fd = -1;
thread->wait_fd = -1;
+ thread->hooks = NULL;
if (thread == booting_thread) /* killing booting thread */
{
diff --git a/server/thread.h b/server/thread.h
index fed326b..0c02901 100644
--- a/server/thread.h
+++ b/server/thread.h
@@ -33,6 +33,7 @@
struct debug_event;
struct startup_info;
struct msg_queue;
+struct hook_table;
enum run_state
{
@@ -66,6 +67,7 @@
struct debug_ctx *debug_ctx; /* debugger context if this thread is a debugger */
struct debug_event *debug_event; /* debug event being sent to debugger */
struct msg_queue *queue; /* message queue */
+ struct hook_table *hooks; /* hooks table */
struct startup_info *info; /* startup info for child process */
struct thread_wait *wait; /* current wait condition if sleeping */
struct apc_queue system_apc; /* queue of system async procedure calls */
diff --git a/server/trace.c b/server/trace.c
index f06d2b6..9d89ef7 100644
--- a/server/trace.c
+++ b/server/trace.c
@@ -2282,6 +2282,57 @@
fprintf( stderr, " old_state=%d", req->old_state );
}
+static void dump_set_hook_request( const struct set_hook_request *req )
+{
+ fprintf( stderr, " id=%d,", req->id );
+ fprintf( stderr, " tid=%08x,", req->tid );
+ fprintf( stderr, " proc=%p,", req->proc );
+ fprintf( stderr, " unicode=%d", req->unicode );
+}
+
+static void dump_set_hook_reply( const struct set_hook_reply *req )
+{
+ fprintf( stderr, " handle=%p", req->handle );
+}
+
+static void dump_remove_hook_request( const struct remove_hook_request *req )
+{
+ fprintf( stderr, " handle=%p,", req->handle );
+ fprintf( stderr, " id=%d,", req->id );
+ fprintf( stderr, " proc=%p", req->proc );
+}
+
+static void dump_start_hook_chain_request( const struct start_hook_chain_request *req )
+{
+ fprintf( stderr, " id=%d", req->id );
+}
+
+static void dump_start_hook_chain_reply( const struct start_hook_chain_reply *req )
+{
+ fprintf( stderr, " handle=%p,", req->handle );
+ fprintf( stderr, " proc=%p,", req->proc );
+ fprintf( stderr, " unicode=%d", req->unicode );
+}
+
+static void dump_finish_hook_chain_request( const struct finish_hook_chain_request *req )
+{
+ fprintf( stderr, " id=%d", req->id );
+}
+
+static void dump_get_next_hook_request( const struct get_next_hook_request *req )
+{
+ fprintf( stderr, " handle=%p", req->handle );
+}
+
+static void dump_get_next_hook_reply( const struct get_next_hook_reply *req )
+{
+ fprintf( stderr, " next=%p,", req->next );
+ fprintf( stderr, " id=%d,", req->id );
+ fprintf( stderr, " proc=%p,", req->proc );
+ fprintf( stderr, " prev_unicode=%d,", req->prev_unicode );
+ fprintf( stderr, " next_unicode=%d", req->next_unicode );
+}
+
static const dump_func req_dumpers[REQ_NB_REQUESTS] = {
(dump_func)dump_new_process_request,
(dump_func)dump_get_new_process_info_request,
@@ -2447,6 +2498,11 @@
(dump_func)dump_set_capture_window_request,
(dump_func)dump_set_caret_window_request,
(dump_func)dump_set_caret_info_request,
+ (dump_func)dump_set_hook_request,
+ (dump_func)dump_remove_hook_request,
+ (dump_func)dump_start_hook_chain_request,
+ (dump_func)dump_finish_hook_chain_request,
+ (dump_func)dump_get_next_hook_request,
};
static const dump_func reply_dumpers[REQ_NB_REQUESTS] = {
@@ -2614,6 +2670,11 @@
(dump_func)dump_set_capture_window_reply,
(dump_func)dump_set_caret_window_reply,
(dump_func)dump_set_caret_info_reply,
+ (dump_func)dump_set_hook_reply,
+ (dump_func)0,
+ (dump_func)dump_start_hook_chain_reply,
+ (dump_func)0,
+ (dump_func)dump_get_next_hook_reply,
};
static const char * const req_names[REQ_NB_REQUESTS] = {
@@ -2781,6 +2842,11 @@
"set_capture_window",
"set_caret_window",
"set_caret_info",
+ "set_hook",
+ "remove_hook",
+ "start_hook_chain",
+ "finish_hook_chain",
+ "get_next_hook",
};
/* ### make_requests end ### */
diff --git a/server/user.h b/server/user.h
index c153d78..f70cfea 100644
--- a/server/user.h
+++ b/server/user.h
@@ -29,7 +29,8 @@
enum user_object
{
- USER_WINDOW = 1
+ USER_WINDOW = 1,
+ USER_HOOK
};
/* user handles functions */