Support more than one keydest callback.

This commit is contained in:
Bill Currie 2013-01-14 12:17:27 +09:00
parent 8975dd95fe
commit cf0729c818
3 changed files with 32 additions and 7 deletions

View file

@ -457,7 +457,6 @@ extern keydest_t key_dest;
extern imt_t key_game_target;
extern knum_t key_togglemenu;
extern knum_t key_toggleconsole;
extern void (*key_dest_callback) (void);
extern struct keybind_s {
char *str;
@ -473,6 +472,8 @@ void Key_ClearStates (void);
const char *Key_GetBinding (imt_t imt, knum_t key);
void Key_SetBinding (imt_t target, knum_t keynum, const char *binding);
void Key_SetKeyDest(keydest_t kd);
typedef void keydest_callback_t (keydest_t);
void Key_KeydestCallback (keydest_callback_t *callback);
const char *Key_KeynumToString (knum_t keynum);

View file

@ -63,7 +63,11 @@ VISIBLE keydest_t key_dest = key_console;
VISIBLE imt_t key_game_target = IMT_0;
VISIBLE knum_t key_togglemenu = QFK_ESCAPE;
VISIBLE knum_t key_toggleconsole = QFK_BACKQUOTE;
VISIBLE void (*key_dest_callback) (void);
#define KEYDEST_CALLBACK_CHUNK 16
static keydest_callback_t **keydest_callbacks;
static int num_keydest_callbacks;
static int max_keydest_callbacks;
VISIBLE struct keybind_s keybindings[IMT_LAST][QFK_LAST];
VISIBLE int keydown[QFK_LAST];
@ -1026,6 +1030,8 @@ Key_SetBinding (imt_t target, knum_t keynum, const char *binding)
VISIBLE void
Key_SetKeyDest(keydest_t kd)
{
int i;
key_dest = kd;
switch (key_dest) {
default:
@ -1041,6 +1047,24 @@ Key_SetKeyDest(keydest_t kd)
key_target = IMT_MENU;
break;
}
if (key_dest_callback)
key_dest_callback ();
for (i = 0; i < num_keydest_callbacks; i++)
keydest_callbacks[i] (key_dest);
}
VISIBLE void
Key_KeydestCallback (keydest_callback_t *callback)
{
if (num_keydest_callbacks == max_keydest_callbacks) {
size_t size = (max_keydest_callbacks + KEYDEST_CALLBACK_CHUNK)
* sizeof (keydest_callback_t *);
keydest_callbacks = realloc (keydest_callbacks, size);
if (!keydest_callbacks)
Sys_Error ("Too many keydest callbacks!");
max_keydest_callbacks += KEYDEST_CALLBACK_CHUNK;
}
if (!callback)
Sys_Error ("null keydest callback");
keydest_callbacks[num_keydest_callbacks++] = callback;
}

View file

@ -238,9 +238,9 @@ CL_ChatInfo (int val)
}
static void
cl_chat_key_dest (void)
cl_chat_keydest (keydest_t keydest)
{
switch (key_dest) {
switch (keydest) {
case key_game:
CL_ChatInfo (0);
break;
@ -265,5 +265,5 @@ CL_Chat_Init (void)
Cmd_AddCommand ("ignore", CL_Ignore_f, "Ignores chat and name-change messages from a user.");
Cmd_AddCommand ("unignore", CL_Unignore_f, "Removes a previously ignored user from the ignore list.");
key_dest_callback = cl_chat_key_dest;
Key_KeydestCallback (cl_chat_keydest);
}