/*
Copyright (C) 1997-2001 Id Software, Inc.

This program 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 program 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 program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
#include "client.h"

/*

key up events are sent even if in console mode

*/


#define		MAXCMDLINE	256
char		key_lines[32][MAXCMDLINE];
unsigned	key_linepos;
qboolean	key_insert = qtrue;

int			anykeydown;

int			edit_line = 0;
int			history_line = 0;

int			key_waiting;
char		*keybindings[256];
qboolean	consolekeys[256];	// if qtrue, can't be rebound while in console
qboolean	menubound[256];		// if qtrue, can't be rebound while in menu
int			key_repeats[256];	// if > 1, it is autorepeating
qboolean	keydown[256];

typedef struct
{
	char	*name;
	int		keynum;
} keyname_t;

keyname_t keynames[] =
{
	{"TAB", K_TAB},
	{"ENTER", K_ENTER},
	{"ESCAPE", K_ESCAPE},
	{"SPACE", K_SPACE},
	{"CAPSLOCK", K_CAPSLOCK},
	{"SCROLLLOCK", K_SCROLLLOCK},
	{"NUMLOCK", K_NUMLOCK},
	{"KP_NUMLOCK", K_NUMLOCK},
	{"BACKSPACE", K_BACKSPACE},
	{"UPARROW", K_UPARROW},
	{"DOWNARROW", K_DOWNARROW},
	{"LEFTARROW", K_LEFTARROW},
	{"RIGHTARROW", K_RIGHTARROW},

	{"ALT", K_ALT},
	{"CTRL", K_CTRL},
	{"SHIFT", K_SHIFT},
	
	{"F1", K_F1},
	{"F2", K_F2},
	{"F3", K_F3},
	{"F4", K_F4},
	{"F5", K_F5},
	{"F6", K_F6},
	{"F7", K_F7},
	{"F8", K_F8},
	{"F9", K_F9},
	{"F10", K_F10},
	{"F11", K_F11},
	{"F12", K_F12},

	{"INS", K_INS},
	{"DEL", K_DEL},
	{"PGDN", K_PGDN},
	{"PGUP", K_PGUP},
	{"HOME", K_HOME},
	{"END", K_END},
	
#if defined (__APPLE__) || defined (MACOSX)
	{"F13", K_F13},
	{"F14", K_F14},
	{"F15", K_F15},
	{"COMMAND", K_COMMAND},
#endif /* __APPLE__ || MACOSX */

	{"MOUSE1", K_MOUSE1},
	{"MOUSE2", K_MOUSE2},
	{"MOUSE3", K_MOUSE3},
	{"MOUSE4", K_MOUSE4},
	{"MOUSE5", K_MOUSE5},
#if !defined (__APPLE__) && !defined (MACOSX)
	{"MOUSE6", K_MOUSE6},
	{"MOUSE7", K_MOUSE7},
	{"MOUSE8", K_MOUSE8},
#endif /* !__APPLE__ && !MACOSX */

	{"JOY1", K_JOY1},
	{"JOY2", K_JOY2},
	{"JOY3", K_JOY3},
	{"JOY4", K_JOY4},

	{"AUX1", K_AUX1},
	{"AUX2", K_AUX2},
	{"AUX3", K_AUX3},
	{"AUX4", K_AUX4},
	{"AUX5", K_AUX5},
	{"AUX6", K_AUX6},
	{"AUX7", K_AUX7},
	{"AUX8", K_AUX8},
	{"AUX9", K_AUX9},
	{"AUX10", K_AUX10},
	{"AUX11", K_AUX11},
	{"AUX12", K_AUX12},
	{"AUX13", K_AUX13},
	{"AUX14", K_AUX14},
	{"AUX15", K_AUX15},
	{"AUX16", K_AUX16},
	{"AUX17", K_AUX17},
	{"AUX18", K_AUX18},
	{"AUX19", K_AUX19},
	{"AUX20", K_AUX20},
	{"AUX21", K_AUX21},
	{"AUX22", K_AUX22},
	{"AUX23", K_AUX23},
	{"AUX24", K_AUX24},
	{"AUX25", K_AUX25},
	{"AUX26", K_AUX26},
	{"AUX27", K_AUX27},
	{"AUX28", K_AUX28},
	{"AUX29", K_AUX29},
	{"AUX30", K_AUX30},
	{"AUX31", K_AUX31},
	{"AUX32", K_AUX32},

	{"KP_HOME",			KP_HOME },
	{"KP_UPARROW",		KP_UPARROW },
	{"KP_PGUP",			KP_PGUP },
	{"KP_LEFTARROW",	KP_LEFTARROW },
	{"KP_5",			KP_5 },
	{"KP_RIGHTARROW",	KP_RIGHTARROW },
	{"KP_END",			KP_END },
	{"KP_DOWNARROW",	KP_DOWNARROW },
	{"KP_PGDN",			KP_PGDN },
	{"KP_ENTER",		KP_ENTER },
	{"KP_INS",			KP_INS },
	{"KP_DEL",			KP_DEL },
	{"KP_STAR",			KP_STAR },
	{"KP_SLASH",		KP_SLASH },
	{"KP_MINUS",		KP_MINUS },
	{"KP_PLUS",			KP_PLUS },

#if defined(__APPLE__) || (MACOSX)
	{"KP_MULT", KP_MULT},
	{"KP_EQUAL", KP_EQUAL},
#endif /* __APPLE__ || MACOSX */

	{"MWHEELUP", K_MWHEELUP },
	{"MWHEELDOWN", K_MWHEELDOWN },

	{"PAUSE", K_PAUSE},

	{"SEMICOLON", ';'},	// because a raw semicolon separates commands

	{NULL,0}
};

/*
==============================================================================

			LINE TYPING INTO THE CONSOLE

==============================================================================
*/

void CompleteCommand (void)
{
	char	*cmd, *s;

	s = key_lines[edit_line]+1;
	if (*s == '\\' || *s == '/')
		s++;

	cmd = Cmd_CompleteCommand (s);
	if (!cmd)
		cmd = Cvar_CompleteVariable (s);
	if (cmd)
	{
		key_lines[edit_line][1] = '/';
		Q_strncpyz (key_lines[edit_line]+2, cmd, sizeof(key_lines[edit_line])-2);
		key_linepos = (unsigned int)strlen(cmd)+2;
		key_lines[edit_line][key_linepos] = ' ';
		key_linepos++;
		key_lines[edit_line][key_linepos] = 0;
		return;
	}
}

int Q_ColorCharCount (char *s, int byteofs);
int Q_ColorCharOffset (char *s, int charcount);

/*
====================
Key_charConsole

Interactive line editing and console scrollback only for ascii char
====================
*/
void Key_CharConsole( int key)
{
	if (cls.state == CA_CONNECTING || cls.state == CA_CONNECTED)
		return;

	switch( key )
	{
	case 22: // CTRL - V : paste
		{
			char cbd[256];

			CL_GetClipboardData (cbd, sizeof(cbd));
			if ( cbd[0] )
			{
				int i;

				strtok( cbd, "\n\r\b" );

				i = (int)strlen( cbd );
				if ( i + key_linepos >= MAXCMDLINE)
					i= MAXCMDLINE - key_linepos;

				if ( i > 0 )
				{
					cbd[i] = 0;
					Q_strncatz( key_lines[edit_line], cbd, sizeof(key_lines[edit_line]) );
					key_linepos += i;
				}
				// Q_free( cbd ); this is one of the crapiest qfusion bug (trying to free a PTR from stack)
			}

			return;
		}
		break;

	case 12: // CTRL - L : clear
		{
			Cbuf_AddText ("clear\n");
			return;
		}
		break;
	/*
	case 8: // CTRL+H or Backspace
		{
			if (key_linepos > 1)
			{
				// skip to the end of color sequence
				while (Q_IsColorString(key_lines[edit_line] + key_linepos))
					key_linepos += 2;

				strcpy (key_lines[edit_line] + key_linepos - 1, key_lines[edit_line] + key_linepos);
				key_linepos--;
			}

			return;
		}
		break;
	*/
	case 16: // CTRL+P : history prev
		{
			do
			{
				history_line = (history_line - 1) & 31;
			} while (history_line != edit_line
				&& !key_lines[history_line][1]);
			if (history_line == edit_line)
				history_line = (edit_line+1)&31;
			Q_strncpyz(key_lines[edit_line], key_lines[history_line], sizeof(key_lines[edit_line]));
			key_linepos = (unsigned int)strlen(key_lines[edit_line]);
			return;
		}
		break;
	
	case 14: // CTRL+N : history next
		{
			if (history_line == edit_line) return;
			do
			{
				history_line = (history_line + 1) & 31;
			}
			while (history_line != edit_line
				&& !key_lines[history_line][1]);
			if (history_line == edit_line)
			{
				key_lines[edit_line][0] = ']';
				key_linepos = 1;
			}
			else
			{
				Q_strncpyz(key_lines[edit_line], key_lines[history_line], sizeof(key_lines[edit_line]));
				key_linepos = (unsigned int)strlen(key_lines[edit_line]);
			}
			return;
		}
		break;

		// maybe add CTRL-A : HOME and CTRL-E : END
	}

	if( key < 32 || key > 126 )
		return;	// non printable

	if( key_linepos < MAXCMDLINE-1 )
	{
		unsigned i;

		// check insert mode
		if( key_insert )
		{
			// can't do strcpy to move string to right
			key_lines[edit_line][MAXCMDLINE-1] = 0;
			for( i = MAXCMDLINE-3; i >= key_linepos; i-- )
				key_lines[edit_line][i + 1] = key_lines[edit_line][i];
		}

		// only null terminate if at the end
		i = key_lines[edit_line][key_linepos];
		key_lines[edit_line][key_linepos] = key;
		key_linepos++;
		if (!i)
			key_lines[edit_line][key_linepos] = 0;	
	}
}
/*
====================
Key_Console

Interactive line editing and console scrollback except for ascii char
====================
*/
void Key_Console (int key)
{
	if (cls.state == CA_CONNECTING || cls.state == CA_CONNECTED)
		return;

	switch ( key )
	{
	case KP_SLASH:
		key = '/';
		break;
	case KP_MINUS:
		key = '-';
		break;
	case KP_PLUS:
		key = '+';
		break;
	case KP_HOME:
		key = '7';
		break;
	case KP_UPARROW:
		key = '8';
		break;
	case KP_PGUP:
		key = '9';
		break;
	case KP_LEFTARROW:
		key = '4';
		break;
	case KP_5:
		key = '5';
		break;
	case KP_RIGHTARROW:
		key = '6';
		break;
	case KP_END:
		key = '1';
		break;
	case KP_DOWNARROW:
		key = '2';
		break;
	case KP_PGDN:
		key = '3';
		break;
	case KP_INS:
		key = '0';
		break;
	case KP_DEL:
		key = '.';
		break;
	}

	if (  ( ( key == K_INS ) || ( key == KP_INS ) ) && keydown[K_SHIFT] )
	{
		char cbd[256];

		CL_GetClipboardData (cbd, sizeof(cbd));
		if ( cbd[0] )
		{
			int i;

			strtok( cbd, "\n\r\b" );

			i = (int)strlen( cbd );
			if ( i + key_linepos >= MAXCMDLINE)
				i= MAXCMDLINE - key_linepos;

			if ( i > 0 )
			{
				cbd[i] = 0;
				Q_strncatz( key_lines[edit_line], cbd, sizeof(key_lines[edit_line]) );
				key_linepos += i;
			}
			// Q_free( cbd ); this is one of the crapiest qfusion bug (trying to free a PTR from stack)
		}

		return;
	}

	if ( key == K_ENTER || key == KP_ENTER )
	{	// backslash text are commands, else chat
		if (key_lines[edit_line][1] == '\\' || key_lines[edit_line][1] == '/')
			Cbuf_AddText (key_lines[edit_line]+2);	// skip the >
		else
			Cbuf_AddText (key_lines[edit_line]+1);	// valid command

		Cbuf_AddText ("\n");
		Com_Printf ("%s\n",key_lines[edit_line]);
		edit_line = (edit_line + 1) & 31;
		history_line = edit_line;
		key_lines[edit_line][0] = ']';
		key_lines[edit_line][1] = 0;
		key_linepos = 1;
		if (cls.state == CA_DISCONNECTED)
			SCR_UpdateScreen ();	// force an update, because the command
									// may take some time
		return;
	}

	if (key == K_TAB)
	{	// command completion
		Con_CompleteCommandLine ();
		return;
	}
	
	if ( ( key == K_LEFTARROW ) || ( key == KP_LEFTARROW ) )
	{
		int charcount;
		// jump over invisible color sequences
		charcount = Q_ColorCharCount (key_lines[edit_line], key_linepos);
		if (charcount > 1)
			key_linepos = Q_ColorCharOffset (key_lines[edit_line], charcount - 1);
		return;
	}

	if ( ( key == K_BACKSPACE ) )
	{
		if (key_linepos > 1)
		{
			// skip to the end of color sequence
			while (Q_IsColorString(key_lines[edit_line] + key_linepos))
				key_linepos += 2;

			Q_strncpyz (key_lines[edit_line] + key_linepos - 1, key_lines[edit_line] + key_linepos,
			            sizeof(key_lines[edit_line]) - (key_linepos - 1));
			key_linepos--;
		}

		return;
	}
	
	if ( key == K_DEL )
	{
		if (key_linepos < strlen(key_lines[edit_line]))
			Q_strncpyz(key_lines[edit_line] + key_linepos, key_lines[edit_line] + key_linepos + 1,
			           sizeof(key_lines[edit_line]) - key_linepos);
		return;
	}
	
	if ( key == K_INS )
	{ // toggle insert mode
		key_insert = !key_insert;
		return;
	}

	if ( key == K_RIGHTARROW )
	{
		if (strlen(key_lines[edit_line]) == key_linepos)
		{
			if (strlen(key_lines[(edit_line + 31) & 31]) <= key_linepos)
				return;

			key_lines[edit_line][key_linepos] = key_lines[(edit_line + 31) & 31][key_linepos];
			key_linepos++;
			key_lines[edit_line][key_linepos] = 0;
		}
		else
		{
			int charcount;
			// jump over invisible color sequences
			charcount = Q_ColorCharCount (key_lines[edit_line], key_linepos);
			key_linepos = Q_ColorCharOffset (key_lines[edit_line], charcount + 1);
		}
		return;
	}

	if ( ( key == K_UPARROW ) || ( key == KP_UPARROW ) )
	{
		do
		{
			history_line = (history_line - 1) & 31;
		} while (history_line != edit_line
				&& !key_lines[history_line][1]);
		if (history_line == edit_line)
			history_line = (edit_line+1)&31;
		Q_strncpyz(key_lines[edit_line], key_lines[history_line], sizeof(key_lines[edit_line]));
		key_linepos = (unsigned int)strlen(key_lines[edit_line]);
		return;
	}

	if ( ( key == K_DOWNARROW ) || ( key == KP_DOWNARROW ) )
	{
		if (history_line == edit_line) return;
		do
		{
			history_line = (history_line + 1) & 31;
		}
		while (history_line != edit_line
			&& !key_lines[history_line][1]);
		if (history_line == edit_line)
		{
			key_lines[edit_line][0] = ']';
			key_linepos = 1;
		}
		else
		{
			Q_strncpyz(key_lines[edit_line], key_lines[history_line], sizeof(key_lines[edit_line]));
			key_linepos = (unsigned int)strlen(key_lines[edit_line]);
		}
		return;
	}

	if (key == K_PGUP || key == KP_PGUP || key == K_MWHEELUP ) 	// wsw : pb : support mwheel in console
	{
		con.display -= 2;
		return;
	}

	if (key == K_PGDN || key == KP_PGDN || key == K_MWHEELDOWN ) 	// wsw : pb : support mwheel in console
	{
		con.display += 2;
		if (con.display > con.current)
			con.display = con.current;
		return;
	}

	if (key == K_HOME || key == KP_HOME )
	{
		if (keydown[K_CTRL])
			con.display = con.current - con.totallines + 10;
		else
			key_linepos = 1;
		return;
	}

	if (key == K_END || key == KP_END )
	{
		if (keydown[K_CTRL])
			con.display = con.current;
		else
			key_linepos = (unsigned int)strlen(key_lines[edit_line]);
		return;
	}

	// key is a normal printable key normal which wil be HANDLE later in response to WM_CHAR event
}

//============================================================================

qboolean	chat_team;
char		chat_buffer[MAXCMDLINE];
int			chat_bufferlen = 0;

void Key_CharMessage( int key)
{
	if( key == 12 ) // CTRL - L : clear
	{
		chat_bufferlen=0;
		memset( chat_buffer, 0, MAXCMDLINE);
		return;
	}

	if (key < 32 || key > 126)
		return;	// non printable

	/*
	if (key == K_BACKSPACE)
	{
		if (chat_bufferlen)
		{
			chat_bufferlen--;
			chat_buffer[chat_bufferlen] = 0;
		}
		return;
	}
	*/

	if (chat_bufferlen == sizeof(chat_buffer)-1)
		return; // all full

	chat_buffer[chat_bufferlen++] = key;
	chat_buffer[chat_bufferlen] = 0;
}

void Key_Message (int key)
{
	if ( key == K_ENTER || key == KP_ENTER )
	{
		if (chat_bufferlen > 0)
		{
			if (chat_team)
				Cbuf_AddText ("say_team \"");
			else
				Cbuf_AddText ("say \"");
			Cbuf_AddText(chat_buffer);
			Cbuf_AddText("\"\n");

			chat_bufferlen = 0;
			chat_buffer[0] = 0;
		}

		CL_SetKeyDest (key_game);
		return;
	}

	if (key == K_BACKSPACE)
	{
		if (chat_bufferlen)
		{
			chat_bufferlen--;
			chat_buffer[chat_bufferlen] = 0;
		}
		return;
	}

	if (key == K_ESCAPE)
	{
		CL_SetKeyDest (key_game);
		chat_bufferlen = 0;
		chat_buffer[0] = 0;
		return;
	}
}

//============================================================================


/*
===================
Key_StringToKeynum

Returns a key number to be used to index keybindings[] by looking at
the given string.  Single ascii characters return themselves, while
the K_* names are matched up.
===================
*/
int Key_StringToKeynum (char *str)
{
	keyname_t	*kn;
	
	if (!str || !str[0])
		return -1;
	if (!str[1])
		return (int)(unsigned char)str[0];

	for (kn=keynames ; kn->name ; kn++)
	{
		if (!Q_stricmp(str, kn->name))
			return kn->keynum;
	}
	return -1;
}

/*
===================
Key_KeynumToString

Returns a string (either a single ascii char, or a K_* name) for the
given keynum.
FIXME: handle quote special (general escape sequence?)
===================
*/
char *Key_KeynumToString (int keynum)
{
	keyname_t	*kn;	
	static	char	tinystr[2];
	
	if (keynum == -1)
		return "<KEY NOT FOUND>";
	if (keynum > 32 && keynum < 127)
	{	// printable ascii
		tinystr[0] = keynum;
		tinystr[1] = 0;
		return tinystr;
	}
	
	for (kn=keynames ; kn->name ; kn++)
		if (keynum == kn->keynum)
			return kn->name;

	return "<UNKNOWN KEYNUM>";
}


/*
===================
Key_SetBinding
===================
*/
void Key_SetBinding (int keynum, char *binding)
{
	if (keynum == -1)
		return;

// free old bindings
	if (keybindings[keynum])
	{
		Mem_ZoneFree (keybindings[keynum]);
		keybindings[keynum] = NULL;
	}

	if (!binding)
		return;

// allocate memory for new binding
	keybindings[keynum] = CopyString (binding);
}

/*
===================
Key_Unbind_f
===================
*/
void Key_Unbind_f (void)
{
	int		b;

	if (Cmd_Argc() != 2)
	{
		Com_Printf ("unbind <key> : remove commands from a key\n");
		return;
	}
	
	b = Key_StringToKeynum (Cmd_Argv(1));
	if (b == -1)
	{
		Com_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
		return;
	}

	Key_SetBinding (b, NULL);
}

void Key_Unbindall_f (void)
{
	int		i;
	
	for (i=0 ; i<256 ; i++)
		if (keybindings[i])
			Key_SetBinding (i, NULL);
}


/*
===================
Key_Bind_f
===================
*/
void Key_Bind_f (void)
{
	int			i, c, b;
	char		cmd[1024];
	
	c = Cmd_Argc();
	if (c < 2)
	{
		Com_Printf ("bind <key> [command] : attach a command to a key\n");
		return;
	}

	b = Key_StringToKeynum (Cmd_Argv(1));
	if (b == -1)
	{
		Com_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
		return;
	}

	if (c == 2)
	{
		if (keybindings[b])
			Com_Printf ("\"%s\" = \"%s\"\n", Cmd_Argv(1), keybindings[b] );
		else
			Com_Printf ("\"%s\" is not bound\n", Cmd_Argv(1) );
		return;
	}
	
// copy the rest of the command line
	cmd[0] = 0;		// start out with a null string
	for (i=2 ; i< c ; i++)
	{
		Q_strncatz (cmd, Cmd_Argv(i), sizeof(cmd));
		if (i != (c-1))
			Q_strncatz (cmd, " ", sizeof(cmd));
	}

	Key_SetBinding (b, cmd);
}

/*
============
Key_WriteBindings

Writes lines containing "bind key value"
============
*/
void Key_WriteBindings (int file)
{
	int		i;

	FS_Printf( file, "unbindall\n" );

	for( i=0 ; i<256 ; i++ )
		if( keybindings[i] && keybindings[i][0] )
			FS_Printf( file, "bind %s \"%s\"\n", Key_KeynumToString(i), keybindings[i] );
}


/*
============
Key_Bindlist_f

============
*/
void Key_Bindlist_f (void)
{
	int		i;

	for (i=0 ; i<256 ; i++)
		if (keybindings[i] && keybindings[i][0])
			Com_Printf ("%s \"%s\"\n", Key_KeynumToString(i), keybindings[i]);
}


//#include "../win32/winquake.h"

/*
===================
Key_Init
===================
*/
void Key_Init (void)
{
	int		i;

	for (i=0 ; i<32 ; i++)
	{
		key_lines[i][0] = ']';
		key_lines[i][1] = 0;
	}
	key_linepos = 1;
	
//
// init ascii characters in console mode
//
	for (i=32 ; i<128 ; i++)
		consolekeys[i] = qtrue;
	consolekeys[K_ENTER] = qtrue;
	consolekeys[KP_ENTER] = qtrue;
	consolekeys[K_TAB] = qtrue;
	consolekeys[K_LEFTARROW] = qtrue;
	consolekeys[KP_LEFTARROW] = qtrue;
	consolekeys[K_RIGHTARROW] = qtrue;
	consolekeys[KP_RIGHTARROW] = qtrue;
	consolekeys[K_UPARROW] = qtrue;
	consolekeys[KP_UPARROW] = qtrue;
	consolekeys[K_DOWNARROW] = qtrue;
	consolekeys[KP_DOWNARROW] = qtrue;
	consolekeys[K_BACKSPACE] = qtrue;
	consolekeys[K_HOME] = qtrue;
	consolekeys[KP_HOME] = qtrue;
	consolekeys[K_END] = qtrue;
	consolekeys[KP_END] = qtrue;
	consolekeys[K_PGUP] = qtrue;
	consolekeys[KP_PGUP] = qtrue;
	consolekeys[K_PGDN] = qtrue;
	consolekeys[KP_PGDN] = qtrue;
	consolekeys[K_SHIFT] = qtrue;
	consolekeys[K_INS] = qtrue;
	consolekeys[K_DEL] = qtrue;
	consolekeys[KP_INS] = qtrue;
	consolekeys[KP_DEL] = qtrue;
	consolekeys[KP_SLASH] = qtrue;
	consolekeys[KP_PLUS] = qtrue;
	consolekeys[KP_MINUS] = qtrue;
	consolekeys[KP_5] = qtrue;

	consolekeys[K_CTRL] = qtrue;	// wsw : pb : ctrl in console for ctrl-v

	consolekeys['`'] = qfalse;
	consolekeys['~'] = qfalse;

	// wsw : pb : support mwheel in console
	consolekeys[K_MWHEELDOWN] = qtrue;
	consolekeys[K_MWHEELUP] = qtrue;

	menubound[K_ESCAPE] = qtrue;
	for (i=0 ; i<12 ; i++)
		menubound[K_F1+i] = qtrue;

//
// register our functions
//
	Cmd_AddCommand ("bind",Key_Bind_f);
	Cmd_AddCommand ("unbind",Key_Unbind_f);
	Cmd_AddCommand ("unbindall",Key_Unbindall_f);
	Cmd_AddCommand ("bindlist",Key_Bindlist_f);
}

/*
===================
Key_CharEvent

Called by the system between frames for key down events for standard characters
Should NOT be called during an interrupt!
===================
*/
void Key_CharEvent (int key)
{
	// the console key should never be used as a char
	if (key == '`' || key == '~')
	{
		return;
	}

	switch (cls.key_dest)
	{
	case key_message:
		Key_CharMessage (key);
		break;
	case key_menu:
		CL_UIModule_CharEvent (key);
		break;
	case key_game:
	case key_console:
		Key_CharConsole (key);
		break;
	default:
		Com_Error (ERR_FATAL, "Bad cls.key_dest");
	}
}

/*
===================
Key_Event

Called by the system between frames for both key up and key down events
Should NOT be called during an interrupt!
===================
*/
void Key_Event( int key, qboolean down, unsigned time )
{
	char		*kb;
	char		cmd[1024];
	qboolean	handled = qfalse;

	// hack for modal presses
	if( key_waiting == -1 )
	{
		if( down )
			key_waiting = key;
		return;
	}

	// update auto-repeat status
	if( down )
	{
		key_repeats[key]++;
		if( key_repeats[key] > 1 )
		{
			if( (key != K_BACKSPACE && key != K_DEL
				&& key != K_LEFTARROW && key != K_RIGHTARROW
				&& key != K_UPARROW && key != K_DOWNARROW
				&& key != K_PGUP && key != K_PGDN && (key < 32 || key > 126 || key == '`'))
				|| cls.key_dest == key_game )
				return;
		}
	}
	else
	{
		key_repeats[key] = 0;
	}

#ifndef WIN32
	// switch between fullscreen/windowed when ALT+ENTER is pressed
	if( key == K_ENTER && down && keydown[K_ALT] )
		Cbuf_ExecuteText( EXEC_APPEND, "toggle vid_fullscreen; vid_restart\n" );
#endif

	// console key is hardcoded, so the user can never unbind it
	if( key == '`' || key == '~' )
	{
		if( !down )
			return;
		Con_ToggleConsole_f();
		return;
	}

	// menu key is hardcoded, so the user can never unbind it
	if( key == K_ESCAPE )
	{
		if( !down )
			return;

		if( cls.state != CA_ACTIVE )
		{
			if( cls.key_dest == key_game || cls.key_dest == key_menu )
			{
				if( cls.state != CA_DISCONNECTED )
					Cbuf_AddText( "disconnect\n" );
				else if( cls.key_dest == key_menu )
					CL_UIModule_Keydown( key );
				return;
			}
		}

		switch( cls.key_dest )
		{
		case key_message:
			Key_Message( key );
			break;
		case key_menu:
			CL_UIModule_Keydown( key );
			break;
		case key_game:
			// put away help computer / inventory
			if( cls.demoplaying ) {
				CL_Disconnect( NULL );
			} else {
				Cbuf_AddText( "cmd putaway\n" ); // close scoreboard
				CL_GameModule_InGameMenu();
			}
			break;
		case key_console:
			Con_ToggleConsole_f();
			break;
		default:
			Com_Error( ERR_FATAL, "Bad cls.key_dest" );
		}
		return;
	}

//
// if not a consolekey, send to the interpreter no matter what mode is
//
	if( (cls.key_dest == key_menu && menubound[key])
	|| (cls.key_dest == key_console && !consolekeys[key])
	|| (cls.key_dest == key_game && ( cls.state == CA_ACTIVE || !consolekeys[key] ) ) )
	{
		kb = keybindings[key];

		if( kb )
		{
			if( kb[0] == '+' )
			{	// button commands add keynum and time as a parm
				if (down) {
					Q_snprintfz (cmd, sizeof(cmd), "%s %i %u\n", kb, key, time);
					Cbuf_AddText (cmd);
				} else if (keydown[key]) {
					Q_snprintfz (cmd, sizeof(cmd), "-%s %i %u\n", kb+1, key, time);
					Cbuf_AddText (cmd);
				}
			}
			else if( down )
			{
				Cbuf_AddText( kb );
				Cbuf_AddText( "\n" );
			}
		}
		handled = qtrue; // can't return here, because we want to track keydown & repeats
	}

	// track if any key is down for BUTTON_ANY
	keydown[key] = down;
	if( down )
	{
		if( key_repeats[key] == 1 )
			anykeydown++;
	}
	else
	{
		anykeydown--;
		if( anykeydown < 0 )
			anykeydown = 0;
	}

	if( handled || !down )
		return;		// other systems only care about key down events

	switch( cls.key_dest )
	{
	case key_message:
		Key_Message( key );
		break;
	case key_menu:
		CL_UIModule_Keydown( key );
		break;
	case key_game:
	case key_console:
		Key_Console( key );
		break;
	default:
		Com_Error( ERR_FATAL, "Bad cls.key_dest" );
	}
}

/*
===================
Key_ClearStates
===================
*/
void Key_ClearStates (void)
{
	int		i;

	anykeydown = qfalse;

	for (i=0 ; i<256 ; i++)
	{
		if ( keydown[i] || key_repeats[i] )
			Key_Event( i, qfalse, 0 );
		keydown[i] = 0;
		key_repeats[i] = 0;
	}
}


/*
===================
Key_GetKey
===================
*/
int Key_GetKey (void)
{
	key_waiting = -1;

	while (key_waiting == -1)
		Sys_SendKeyEvents ();

	return key_waiting;
}

/*
===================
Key_GetBindingBuf
===================
*/
char *Key_GetBindingBuf (int binding)
{
	return keybindings[binding];
}

/*
===================
Key_GetKey
===================
*/
qboolean Key_IsDown (int keynum)
{
	if (keynum < 0 || keynum > 255)
		return qfalse;
	return keydown[keynum];
}

