2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to:
18 Free Software Foundation, Inc.
19 59 Temple Place - Suite 330
20 Boston, MA 02111-1307, USA
26 cvar_t con_closeontoggleconsole = {CVAR_SAVE, "con_closeontoggleconsole","1", "allows toggleconsole binds to close the console as well"};
29 key up events are sent even if in console mode
32 int edit_line = MAX_INPUTLINES-1;
33 int history_line = MAX_INPUTLINES-1;
34 char key_lines[MAX_INPUTLINES][MAX_INPUTLINE];
36 qboolean key_insert = true; // insert key toggle (for editing)
38 int key_consoleactive;
39 char *keybindings[MAX_BINDMAPS][MAX_KEYS];
41 static int key_bmap, key_bmap2;
42 static unsigned char keydown[MAX_KEYS]; // 0 = up, 1 = down, 2 = repeating
44 typedef struct keyname_s
51 static const keyname_t keynames[] = {
56 {"BACKSPACE", K_BACKSPACE},
57 {"UPARROW", K_UPARROW},
58 {"DOWNARROW", K_DOWNARROW},
59 {"LEFTARROW", K_LEFTARROW},
60 {"RIGHTARROW", K_RIGHTARROW},
88 {"MWHEELUP", K_MWHEELUP},
89 {"MWHEELDOWN", K_MWHEELDOWN},
100 {"MOUSE10", K_MOUSE10},
101 {"MOUSE11", K_MOUSE11},
102 {"MOUSE12", K_MOUSE12},
103 {"MOUSE13", K_MOUSE13},
104 {"MOUSE14", K_MOUSE14},
105 {"MOUSE15", K_MOUSE15},
106 {"MOUSE16", K_MOUSE16},
108 {"NUMLOCK", K_NUMLOCK},
109 {"CAPSLOCK", K_CAPSLOCK},
110 {"SCROLLOCK", K_SCROLLOCK},
112 {"KP_HOME", K_KP_HOME },
113 {"KP_UPARROW", K_KP_UPARROW },
114 {"KP_PGUP", K_KP_PGUP },
115 {"KP_LEFTARROW", K_KP_LEFTARROW },
116 {"KP_RIGHTARROW", K_KP_RIGHTARROW },
117 {"KP_END", K_KP_END },
118 {"KP_DOWNARROW", K_KP_DOWNARROW },
119 {"KP_PGDN", K_KP_PGDN },
120 {"KP_INS", K_KP_INS },
121 {"KP_DEL", K_KP_DEL },
122 {"KP_SLASH", K_KP_SLASH },
134 {"KP_PERIOD", K_KP_PERIOD},
135 {"KP_DIVIDE", K_KP_DIVIDE},
136 {"KP_MULTIPLY", K_KP_MULTIPLY},
137 {"KP_MINUS", K_KP_MINUS},
138 {"KP_PLUS", K_KP_PLUS},
139 {"KP_ENTER", K_KP_ENTER},
140 {"KP_EQUALS", K_KP_EQUALS},
192 {"SEMICOLON", ';'}, // because a raw semicolon separates commands
196 {"APOSTROPHE", '\''},
197 {"BACKSLASH", '\\'}, // because a raw backslash is used for special characters
203 ==============================================================================
205 LINE TYPING INTO THE CONSOLE
207 ==============================================================================
211 Key_ClearEditLine (int edit_line)
213 memset (key_lines[edit_line], '\0', sizeof(key_lines[edit_line]));
214 key_lines[edit_line][0] = ']';
220 Interactive line editing and console scrollback
224 Key_Console (int key, char ascii)
226 // LordHavoc: copied most of this from Q2 to improve keyboard handling
253 case K_KP_RIGHTARROW:
273 if ((toupper(key) == 'V' && keydown[K_CTRL]) || ((key == K_INS || key == K_KP_INS) && keydown[K_SHIFT]))
276 if ((cbd = Sys_GetClipboardData()) != 0)
279 strtok(cbd, "\n\r\b");
280 i = (int)strlen(cbd);
281 if (i + key_linepos >= MAX_INPUTLINE)
282 i= MAX_INPUTLINE - key_linepos;
286 strlcat(key_lines[edit_line], cbd, sizeof(key_lines[edit_line]));
298 Cbuf_AddText ("clear\n");
303 if (key == K_ENTER || key == K_KP_ENTER)
305 Cbuf_AddText (key_lines[edit_line]+1); // skip the ]
307 Con_Printf("%s\n",key_lines[edit_line]);
308 if(key_lines[edit_line][1] == 0) // empty line (just a ])?
309 return; // no, no, you can't submit empty lines to the history...
310 // LordHavoc: redesigned edit_line/history_line
312 history_line = edit_line;
313 memmove(key_lines[0], key_lines[1], sizeof(key_lines[0]) * edit_line);
314 key_lines[edit_line][0] = ']';
315 key_lines[edit_line][1] = 0; // EvilTypeGuy: null terminate
317 // force an update, because the command may take some time
318 if (cls.state == ca_disconnected)
325 // Enhanced command completion
326 // by EvilTypeGuy eviltypeguy@qeradiant.com
327 // Thanks to Fett, Taniwha
328 Con_CompleteCommandLine();
332 // Advanced Console Editing by Radix radix@planetquake.com
333 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
336 // left arrow will just move left one without erasing, backspace will
337 // actually erase character
338 if (key == K_LEFTARROW || key == K_KP_LEFTARROW)
350 k = key_lines[edit_line][pos];
351 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
354 key_linepos = pos + 1;
361 // delete char before cursor
362 if (key == K_BACKSPACE || (key == 'h' && keydown[K_CTRL]))
366 strlcpy(key_lines[edit_line] + key_linepos - 1, key_lines[edit_line] + key_linepos, sizeof(key_lines[edit_line]) + 1 - key_linepos);
372 // delete char on cursor
373 if (key == K_DEL || key == K_KP_DEL)
376 linelen = strlen(key_lines[edit_line]);
377 if (key_linepos < (int)linelen)
378 memmove(key_lines[edit_line] + key_linepos, key_lines[edit_line] + key_linepos + 1, linelen - key_linepos);
383 // if we're at the end, get one character from previous line,
384 // otherwise just go right one
385 if (key == K_RIGHTARROW || key == K_KP_RIGHTARROW)
387 if (key_linepos >= (int)strlen(key_lines[edit_line]))
393 len = (int)strlen(key_lines[edit_line]);
397 k = key_lines[edit_line][pos];
398 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
408 if (key == K_INS || key == K_KP_INS) // toggle insert mode
414 // End Advanced Console Editing
416 if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL]))
418 if (history_line > 0 && key_lines[history_line-1][1])
422 linelen = strlen(key_lines[history_line]);
423 memcpy(key_lines[edit_line], key_lines[history_line], linelen + 1);
424 key_linepos = (int)linelen;
429 if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL]))
432 if (history_line >= edit_line)
434 history_line = edit_line;
435 key_lines[edit_line][0] = ']';
436 key_lines[edit_line][1] = 0;
442 linelen = strlen(key_lines[history_line]);
443 memcpy(key_lines[edit_line], key_lines[history_line], linelen + 1);
444 key_linepos = (int)linelen;
449 if (key == K_PGUP || key == K_KP_PGUP || key == K_MWHEELUP)
451 con_backscroll += ((int) vid_conheight.integer >> 5);
455 if (key == K_PGDN || key == K_KP_PGDN || key == K_MWHEELDOWN)
457 con_backscroll -= ((int) vid_conheight.integer >> 5);
461 if (key == K_HOME || key == K_KP_HOME)
464 con_backscroll = INT_MAX;
470 if (key == K_END || key == K_KP_END)
475 key_linepos = (int)strlen(key_lines[edit_line]);
483 if (key_linepos < MAX_INPUTLINE-1)
486 len = (int)strlen(&key_lines[edit_line][key_linepos]);
487 // check insert mode, or always insert if at end of line
488 if (key_insert || len == 0)
490 // can't use strcpy to move string to right
492 memmove(&key_lines[edit_line][key_linepos + 1], &key_lines[edit_line][key_linepos], len);
494 key_lines[edit_line][key_linepos] = ascii;
499 //============================================================================
502 char chat_buffer[MAX_INPUTLINE];
503 unsigned int chat_bufferlen = 0;
505 extern int Nicks_CompleteChatLine(char *buffer, size_t size, unsigned int pos);
508 Key_Message (int key, char ascii)
511 if (key == K_ENTER || ascii == 10 || ascii == 13)
513 Cmd_ForwardStringToServer(va("%s %s", chat_team ? "say_team" : "say ", chat_buffer));
521 if (key == K_ESCAPE) {
528 if (key == K_BACKSPACE) {
529 if (chat_bufferlen) {
531 chat_buffer[chat_bufferlen] = 0;
537 chat_bufferlen = Nicks_CompleteChatLine(chat_buffer, sizeof(chat_buffer), chat_bufferlen);
541 if (chat_bufferlen == sizeof (chat_buffer) - 1)
545 return; // non printable
547 chat_buffer[chat_bufferlen++] = ascii;
548 chat_buffer[chat_bufferlen] = 0;
551 //============================================================================
556 Returns a key number to be used to index keybindings[] by looking at
557 the given string. Single ascii characters return themselves, while
558 the K_* names are matched up.
562 Key_StringToKeynum (const char *str)
569 return tolower(str[0]);
571 for (kn = keynames; kn->name; kn++) {
572 if (!strcasecmp (str, kn->name))
580 Returns a string (either a single ascii char, or a K_* name) for the
582 FIXME: handle quote special (general escape sequence?)
586 Key_KeynumToString (int keynum)
589 static char tinystr[2];
591 // -1 is an invalid code
593 return "<KEY NOT FOUND>";
595 // search overrides first, because some characters are special
596 for (kn = keynames; kn->name; kn++)
597 if (keynum == kn->keynum)
600 // if it is printable, output it as a single character
601 if (keynum > 32 && keynum < 256)
608 // if it is not overridden and not printable, we don't know what to do with it
609 return "<UNKNOWN KEYNUM>";
614 Key_SetBinding (int keynum, int bindmap, const char *binding)
619 if (keynum == -1 || keynum >= MAX_KEYS)
623 if (keybindings[bindmap][keynum]) {
624 Z_Free (keybindings[bindmap][keynum]);
625 keybindings[bindmap][keynum] = NULL;
627 if(!binding[0]) // make "" binds be removed --blub
629 // allocate memory for new binding
630 l = strlen (binding);
631 newbinding = (char *)Z_Malloc (l + 1);
632 memcpy (newbinding, binding, l + 1);
634 keybindings[bindmap][keynum] = newbinding;
638 Key_In_Unbind_f (void)
642 if (Cmd_Argc () != 3) {
643 Con_Print("in_unbind <bindmap> <key> : remove commands from a key\n");
647 m = strtol(Cmd_Argv (1), NULL, 0);
648 if ((m < 0) || (m >= 8)) {
649 Con_Printf("%d isn't a valid bindmap\n", m);
653 b = Key_StringToKeynum (Cmd_Argv (2));
655 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
659 Key_SetBinding (b, m, "");
666 char cmd[MAX_INPUTLINE];
670 if (c != 3 && c != 4) {
671 Con_Print("in_bind <bindmap> <key> [command] : attach a command to a key\n");
675 m = strtol(Cmd_Argv (1), NULL, 0);
676 if ((m < 0) || (m >= 8)) {
677 Con_Printf("%d isn't a valid bindmap\n", m);
681 b = Key_StringToKeynum (Cmd_Argv (2));
682 if (b == -1 || b >= MAX_KEYS) {
683 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
688 if (keybindings[m][b])
689 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (2), keybindings[m][b]);
691 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (2));
694 // copy the rest of the command line
695 cmd[0] = 0; // start out with a null string
696 for (i = 3; i < c; i++) {
697 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
699 strlcat (cmd, " ", sizeof (cmd));
702 Key_SetBinding (b, m, cmd);
706 Key_In_Bindmap_f (void)
713 Con_Print("in_bindmap <bindmap> <fallback>: set current bindmap and fallback\n");
717 m1 = strtol(Cmd_Argv (1), NULL, 0);
718 if ((m1 < 0) || (m1 >= 8)) {
719 Con_Printf("%d isn't a valid bindmap\n", m1);
723 m2 = strtol(Cmd_Argv (2), NULL, 0);
724 if ((m2 < 0) || (m2 >= 8)) {
725 Con_Printf("%d isn't a valid bindmap\n", m2);
738 if (Cmd_Argc () != 2) {
739 Con_Print("unbind <key> : remove commands from a key\n");
743 b = Key_StringToKeynum (Cmd_Argv (1));
745 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
749 Key_SetBinding (b, 0, "");
753 Key_Unbindall_f (void)
757 for (j = 0; j < 8; j++)
758 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
759 if (keybindings[j][i])
760 Key_SetBinding (i, j, "");
768 char cmd[MAX_INPUTLINE];
772 if (c != 2 && c != 3) {
773 Con_Print("bind <key> [command] : attach a command to a key\n");
776 b = Key_StringToKeynum (Cmd_Argv (1));
777 if (b == -1 || b >= MAX_KEYS) {
778 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
783 if (keybindings[0][b])
784 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (1), keybindings[0][b]);
786 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (1));
789 // copy the rest of the command line
790 cmd[0] = 0; // start out with a null string
791 for (i = 2; i < c; i++) {
792 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
794 strlcat (cmd, " ", sizeof (cmd));
797 Key_SetBinding (b, 0, cmd);
802 Writes lines containing "bind key value"
806 Key_WriteBindings (qfile_t *f)
809 char bindbuf[MAX_INPUTLINE];
812 for (j = 0; j < MAX_BINDMAPS; j++)
814 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
816 p = keybindings[j][i];
819 Cmd_QuoteString(bindbuf, sizeof(bindbuf), p, "\"\\");
821 FS_Printf(f, "bind %s \"%s\"\n", Key_KeynumToString (i), bindbuf);
823 FS_Printf(f, "in_bind %d %s \"%s\"\n", j, Key_KeynumToString (i), bindbuf);
835 for (i = 0; i < 32; i++) {
836 key_lines[i][0] = ']';
842 // register our functions
844 Cmd_AddCommand ("in_bind", Key_In_Bind_f, "binds a command to the specified key in the selected bindmap");
845 Cmd_AddCommand ("in_unbind", Key_In_Unbind_f, "removes command on the specified key in the selected bindmap");
846 Cmd_AddCommand ("in_bindmap", Key_In_Bindmap_f, "selects active foreground and background (used only if a key is not bound in the foreground) bindmaps for typing");
848 Cmd_AddCommand ("bind", Key_Bind_f, "binds a command to the specified key in bindmap 0");
849 Cmd_AddCommand ("unbind", Key_Unbind_f, "removes a command on the specified key in bindmap 0");
850 Cmd_AddCommand ("unbindall", Key_Unbindall_f, "removes all commands from all keys in all bindmaps (leaving only shift-escape and escape)");
852 Cvar_RegisterVariable (&con_closeontoggleconsole);
855 const char *Key_GetBind (int key)
858 if (key < 0 || key >= MAX_KEYS)
860 bind = keybindings[key_bmap][key];
862 bind = keybindings[key_bmap2][key];
866 qboolean CL_VM_InputEvent (qboolean down, int key);
870 Called by the system between frames for both key up and key down events
871 Should NOT be called during an interrupt!
875 Key_Event (int key, char ascii, qboolean down)
880 if (key < 0 || key >= MAX_KEYS)
884 bind = keybindings[key_bmap][key];
886 bind = keybindings[key_bmap2][key];
888 if (developer.integer >= 1000)
889 Con_Printf("Key_Event(%i, '%c', %s) keydown %i bind \"%s\"\n", key, ascii, down ? "down" : "up", keydown[key], bind ? bind : "");
892 if(key_dest == key_game)
894 q = CL_VM_InputEvent(down, key);
898 keydown[key] = min(keydown[key] + 1, 2);
908 // increment key repeat count each time a down is received so that things
909 // which want to ignore key repeat can ignore it
910 keydown[key] = min(keydown[key] + 1, 2);
914 // clear repeat count now that the key is released
918 // key_consoleactive is a flag not a key_dest because the console is a
919 // high priority overlay ontop of the normal screen (designed as a safety
920 // feature so that developers and users can rescue themselves from a bad
923 // this also means that toggling the console on/off does not lose the old
926 // specially handle escape (togglemenu) and shift-escape (toggleconsole)
927 // engine bindings, these are not handled as normal binds so that the user
928 // can recover from a completely empty bindmap
931 // ignore key repeats on escape
932 if (keydown[key] > 1)
934 // escape does these things:
935 // key_consoleactive - close console
936 // key_message - abort messagemode
937 // key_menu - go to parent menu (or key_game)
938 // key_game - open menu
939 // in all modes shift-escape toggles console
940 if (((key_consoleactive & KEY_CONSOLEACTIVE_USER) || keydown[K_SHIFT]) && down)
942 Con_ToggleConsole_f ();
949 Key_Message (key, ascii);
952 MR_KeyEvent (key, ascii, down);
955 // csqc has priority over toggle menu if it wants to (e.g. handling escape for UI stuff in-game.. :sick:)
956 q = CL_VM_InputEvent(down, key);
961 Con_Printf ("Key_Event: Bad key_dest\n");
966 // send function keydowns to interpreter no matter what mode is
967 if (key >= K_F1 && key <= K_F12 && down)
969 // ignore key repeats on F1-F12 binds
970 if (keydown[key] > 1)
974 // button commands add keynum as a parm
976 Cbuf_AddText (va("%s %i\n", bind, key));
987 // ignore binds (other than the above escape/F1-F12 keys) while in console
988 if (key_consoleactive && down)
990 // respond to toggleconsole binds while in console unless the pressed key
991 // happens to be the color prefix character (such as on German keyboards)
992 if (key_consoleactive && down && (!con_closeontoggleconsole.integer || !bind || strncmp(bind, "toggleconsole", strlen("toggleconsole")) || ascii == STRING_COLOR_TAG))
995 Key_Console (key, ascii);
1000 // FIXME: actually the up-bind should only be called if the button was actually pressed while key_dest == key_game [12/17/2007 Black]
1001 // especially CL_VM_InputEvent should be able to prevent it from being called (to intercept the binds)
1002 // key up events only generate commands if the game key binding is a button
1003 // command (leading + sign). These will occur even in console mode, to
1004 // keep the character from continuing an action started before a console
1005 // switch. Button commands include the kenum as a parameter, so multiple
1006 // downs can be matched with ups
1007 if (!down && bind && bind[0] == '+')
1008 Cbuf_AddText(va("-%s %i\n", bind + 1, key));
1010 // ignore binds while a video is played, let the video system handle the key event
1011 if (cl_videoplaying)
1013 CL_Video_KeyEvent (key, ascii, keydown[key] != 0);
1017 // anything else is a key press into the game, chat line, or menu
1022 Key_Message (key, ascii);
1025 MR_KeyEvent (key, ascii, down);
1028 q = CL_VM_InputEvent(down, key);
1029 // ignore key repeats on binds and only send the bind if the event hasnt been already processed by csqc
1030 if (!q && bind && keydown[key] == 1 && down)
1032 // button commands add keynum as a parm
1034 Cbuf_AddText (va("%s %i\n", bind, key));
1037 Cbuf_AddText (bind);
1038 Cbuf_AddText ("\n");
1043 Con_Printf ("Key_Event: Bad key_dest\n");
1053 Key_ClearStates (void)
1055 memset(keydown, 0, sizeof(keydown));