mirror of
https://git.code.sf.net/p/quake/newtree
synced 2025-04-07 07:21:10 +00:00
536 lines
9.6 KiB
C
536 lines
9.6 KiB
C
/*
|
|
keys.c
|
|
|
|
(description)
|
|
|
|
Copyright (C) 1996-1997 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:
|
|
|
|
Free Software Foundation, Inc.
|
|
59 Temple Place - Suite 330
|
|
Boston, MA 02111-1307, USA
|
|
|
|
$Id$
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
# include <config.h>
|
|
#endif
|
|
#ifdef _WIN32
|
|
#include <windows.h>
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#ifdef HAVE_STRINGS_H
|
|
#include <strings.h>
|
|
#endif
|
|
|
|
#include "qtypes.h"
|
|
#include "sys.h"
|
|
#include "keys.h"
|
|
#include "menu.h"
|
|
#include "cmd.h"
|
|
#include "console.h"
|
|
#include "cvar.h"
|
|
#include "screen.h"
|
|
#include "client.h"
|
|
#include "wtoi.h"
|
|
|
|
/*
|
|
|
|
key up events are sent even if in console mode
|
|
|
|
*/
|
|
|
|
#define NUM_BINDS 256 // Can never be higher, can it?
|
|
#define BIND_STATES 3 // Number of bind states.
|
|
|
|
char *bindings[BIND_STATES][NUM_BINDS];
|
|
int keyshift[256]; // key to map to if shift held down in console
|
|
int key_repeats[256]; // if > 1, it is autorepeating
|
|
qboolean keydown[256];
|
|
|
|
bind_states bind_state = BIND_CONSOLE;
|
|
|
|
wic_t bindnames[] =
|
|
{
|
|
{ "GAME", BIND_GAME },
|
|
{ "CONSOLE", BIND_CONSOLE },
|
|
{ "ALL", BIND_ALL },
|
|
{ NULL, 0 }
|
|
};
|
|
|
|
wic_t keynames[] =
|
|
{
|
|
{"TAB", K_TAB},
|
|
{"ENTER", K_ENTER},
|
|
{"ESCAPE", K_ESCAPE},
|
|
{"SPACE", K_SPACE},
|
|
{"BACKSPACE", K_BACKSPACE},
|
|
|
|
{"CAPSLOCK", K_CAPSLOCK},
|
|
{"PRINTSCR", K_PRNTSCR},
|
|
{"SCRLCK", K_SCRLCK},
|
|
{"PAUSE", K_PAUSE},
|
|
|
|
{"UPARROW", K_UPARROW},
|
|
{"DOWNARROW", K_DOWNARROW},
|
|
{"LEFTARROW", K_LEFTARROW},
|
|
{"RIGHTARROW", K_RIGHTARROW},
|
|
|
|
{"ALT", K_ALT},
|
|
{"CTRL", K_CTRL},
|
|
{"SHIFT", K_SHIFT},
|
|
|
|
// Keypad stuff..
|
|
|
|
// These are duplicated
|
|
{"NUMLOCK", KP_NUMLCK},
|
|
{"KP_NUMLCK", KP_NUMLCK},
|
|
{"KP_NUMLOCK", KP_NUMLCK},
|
|
{"KP_SLASH", KP_DIVIDE},
|
|
{"KP_DIVIDE", KP_DIVIDE},
|
|
{"KP_STAR", KP_MULTIPLY},
|
|
{"KP_MULTIPLY", KP_MULTIPLY},
|
|
{"KP_MINUS", KP_MINUS},
|
|
|
|
{"KP_HOME", KP_HOME},
|
|
{"KP_UPARROW", KP_UPARROW},
|
|
{"KP_PGUP", KP_PGUP},
|
|
{"KP_PLUS", KP_PLUS},
|
|
|
|
{"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_INS", KP_INS},
|
|
{"KP_DEL", KP_DEL},
|
|
{"KP_ENTER", KP_ENTER},
|
|
|
|
|
|
{"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},
|
|
|
|
{"MOUSE1", K_MOUSE1},
|
|
{"MOUSE2", K_MOUSE2},
|
|
{"MOUSE3", K_MOUSE3},
|
|
|
|
{"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},
|
|
|
|
{"MWHEELUP", K_MWHEELUP},
|
|
{"MWHEELDOWN", K_MWHEELDOWN},
|
|
|
|
{"SEMICOLON", ';'}, // because a raw semicolon seperates commands
|
|
{"DOUBLEQUOTE", '"'},
|
|
|
|
{NULL,0}
|
|
};
|
|
|
|
/*
|
|
==============================================================================
|
|
|
|
LINE TYPING INTO THE CONSOLE
|
|
|
|
==============================================================================
|
|
*/
|
|
|
|
/*
|
|
===================
|
|
Key_StringToKeynum
|
|
|
|
Returns a key number to be used to index bindings[] by looking at
|
|
the given string. Single ascii characters return themselves, while
|
|
the K_* names are matched up.
|
|
===================
|
|
*/
|
|
int
|
|
Key_StringToKeynum (char *str)
|
|
{
|
|
if (!str || !str[0])
|
|
return -1;
|
|
if (!str[1])
|
|
return str[0];
|
|
|
|
return WIC_StrToInt(str, keynames);
|
|
}
|
|
|
|
/*
|
|
===================
|
|
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 num)
|
|
{
|
|
static char *str, tinystr[2];
|
|
|
|
if (num == -1)
|
|
return "<KEY NOT FOUND>";
|
|
if (num > 32 && num < 127) { // printable ascii
|
|
tinystr[0] = num;
|
|
tinystr[1] = 0;
|
|
return tinystr;
|
|
}
|
|
|
|
if ((str = WIC_IntToStr(num, keynames)))
|
|
return str;
|
|
else
|
|
return "<UNKNOWN KEYNUM>";
|
|
}
|
|
|
|
|
|
/*
|
|
===================
|
|
Key_SetBinding
|
|
===================
|
|
*/
|
|
void
|
|
Key_SetBinding (bind_states state, int keynum, char *binding)
|
|
{
|
|
char *new;
|
|
int l;
|
|
|
|
if (keynum == -1)
|
|
return;
|
|
|
|
// free old bindings
|
|
if (bindings[state][keynum])
|
|
{
|
|
free (bindings[state][keynum]);
|
|
bindings[state][keynum] = NULL;
|
|
}
|
|
|
|
// allocate memory for new binding
|
|
l = strlen (binding);
|
|
new = malloc (l+1);
|
|
strcpy (new, binding);
|
|
new[l] = 0;
|
|
bindings[state][keynum] = new;
|
|
}
|
|
|
|
/*
|
|
===================
|
|
Key_Unbind_f
|
|
===================
|
|
*/
|
|
void
|
|
Key_Unbind_f ( void )
|
|
{
|
|
int b, state;
|
|
|
|
if (Cmd_Argc() != 3)
|
|
{
|
|
Con_Printf ("unbind <state> <key> : remove commands from a key\n");
|
|
return;
|
|
}
|
|
|
|
b = Key_StringToKeynum (Cmd_Argv(1));
|
|
if (b==-1)
|
|
{
|
|
Con_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
|
|
return;
|
|
}
|
|
state = WIC_StrToInt(Cmd_Argv(2), bindnames);
|
|
|
|
Key_SetBinding (state, b, "");
|
|
}
|
|
|
|
void
|
|
Key_Unbindall_f ( void )
|
|
{
|
|
int a, i;
|
|
|
|
for (a = 0; a < BIND_MAX; a++) {
|
|
for (i=0 ; i < NUM_BINDS ; i++) {
|
|
if (bindings[a][i]) {
|
|
Key_SetBinding (a, i, "");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
===================
|
|
Key_Bind_f
|
|
===================
|
|
*/
|
|
void
|
|
Key_Bind_f ( void )
|
|
{
|
|
int i, c, b;
|
|
char cmd[1024];
|
|
|
|
c = Cmd_Argc();
|
|
|
|
if (c < 2) {
|
|
Con_Printf("bind <key> [command] :attach a command to a key\n");
|
|
return;
|
|
}
|
|
b = Key_StringToKeynum (Cmd_Argv(1));
|
|
if (b == -1) {
|
|
Con_Printf ("\"%s\" isn't a valid key\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++) {
|
|
strcat (cmd, Cmd_Argv(i));
|
|
if (i != (c-1))
|
|
strcat (cmd, " ");
|
|
}
|
|
|
|
Key_SetBinding (BIND_ALL, b, cmd);
|
|
}
|
|
|
|
/*
|
|
===================
|
|
Key_SBind_f
|
|
===================
|
|
*/
|
|
void
|
|
Key_SBind_f ( void )
|
|
{
|
|
int i, c, b;
|
|
char cmd[1024];
|
|
bind_states state;
|
|
|
|
c = Cmd_Argc();
|
|
|
|
if (c < 3) {
|
|
Con_Printf("sbind <state> <key> [command] :attach a command to a key\n");
|
|
return;
|
|
}
|
|
state = WIC_StrToInt(Cmd_Argv(1), bindnames);
|
|
if (state == -1) {
|
|
Con_Printf ("\"%s\" isn't a valid state\n", Cmd_Argv(1));
|
|
return;
|
|
}
|
|
|
|
b = Key_StringToKeynum (Cmd_Argv(2));
|
|
if (b == -1) {
|
|
Con_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(2));
|
|
return;
|
|
}
|
|
|
|
// copy the rest of the command line
|
|
cmd[0] = 0; // start out with a null string
|
|
for (i = 3 ; i < c ; i++) {
|
|
strcat (cmd, Cmd_Argv(i));
|
|
if (i != (c-1))
|
|
strcat (cmd, " ");
|
|
}
|
|
|
|
Key_SetBinding (state, b, cmd);
|
|
}
|
|
|
|
/*
|
|
============
|
|
Key_WriteBindings
|
|
|
|
Writes lines containing "bind key value"
|
|
============
|
|
*/
|
|
void
|
|
Key_WriteBindings ( QFile *f )
|
|
{
|
|
int i, a;
|
|
|
|
for (a=0; a<BIND_MAX; a++)
|
|
for (i=0 ; i<256 ; i++)
|
|
if (bindings[a][i])
|
|
Qprintf (f, "sbind %s \"%s\" \"%s\"\n", WIC_IntToStr(a, bindnames), Key_KeynumToString(i), bindings[a][i]);
|
|
}
|
|
|
|
|
|
/*
|
|
===================
|
|
Key_Init
|
|
===================
|
|
*/
|
|
void
|
|
Key_Init ( void )
|
|
{
|
|
int i;
|
|
|
|
for (i=0 ; i<256 ; i++)
|
|
keyshift[i] = i;
|
|
for (i='a' ; i<='z' ; i++)
|
|
keyshift[i] = i - 'a' + 'A';
|
|
keyshift['1'] = '!';
|
|
keyshift['2'] = '@';
|
|
keyshift['3'] = '#';
|
|
keyshift['4'] = '$';
|
|
keyshift['5'] = '%';
|
|
keyshift['6'] = '^';
|
|
keyshift['7'] = '&';
|
|
keyshift['8'] = '*';
|
|
keyshift['9'] = '(';
|
|
keyshift['0'] = ')';
|
|
keyshift['-'] = '_';
|
|
keyshift['='] = '+';
|
|
keyshift[','] = '<';
|
|
keyshift['.'] = '>';
|
|
keyshift['/'] = '?';
|
|
keyshift[';'] = ':';
|
|
keyshift['\''] = '"';
|
|
keyshift['['] = '{';
|
|
keyshift[']'] = '}';
|
|
keyshift['`'] = '~';
|
|
keyshift['\\'] = '|';
|
|
|
|
//
|
|
// register our functions
|
|
//
|
|
Cmd_AddCommand ("bind", Key_Bind_f);
|
|
Cmd_AddCommand ("sbind", Key_SBind_f);
|
|
Cmd_AddCommand ("unbind", Key_Unbind_f);
|
|
Cmd_AddCommand ("unbindall", Key_Unbindall_f);
|
|
}
|
|
|
|
char *
|
|
bind_get(int key, bind_states state)
|
|
{
|
|
if (bindings[state][key])
|
|
return bindings[state][key];
|
|
else if (bindings[BIND_ALL][key])
|
|
return bindings[BIND_ALL][key];
|
|
else
|
|
return NULL;
|
|
}
|
|
|
|
/*
|
|
===================
|
|
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 )
|
|
{
|
|
char *kb = NULL;
|
|
char cmd[1024];
|
|
|
|
// Check to see if this is just a repeat.
|
|
if (down == keydown[key])
|
|
return;
|
|
|
|
keydown[key] = down;
|
|
|
|
if (keydown[K_SHIFT]) {
|
|
int shifted = keyshift[key];
|
|
|
|
if ((kb = bind_get(shifted, bind_state)))
|
|
key = shifted;
|
|
}
|
|
|
|
if (!kb)
|
|
kb = bind_get(key, bind_state);
|
|
|
|
if (kb) {
|
|
if (kb[0] == '+') { // button commands add keynum as a parm
|
|
snprintf (cmd, sizeof(cmd), "%s %i\n", kb, key);
|
|
if (!down)
|
|
cmd[0] = '-';
|
|
Cbuf_AddText (cmd);
|
|
} else if (down) {
|
|
snprintf(cmd, sizeof(cmd), "%s\n", kb);
|
|
Cbuf_AddText (cmd);
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
===================
|
|
Key_ClearStates
|
|
===================
|
|
*/
|
|
void
|
|
Key_ClearStates ( void )
|
|
{
|
|
int i;
|
|
|
|
for (i=0 ; i<256 ; i++)
|
|
{
|
|
keydown[i] = false;
|
|
key_repeats[i] = false;
|
|
}
|
|
}
|