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 seperates commands
196 {"APOSTROPHE", '\''},
202 ==============================================================================
204 LINE TYPING INTO THE CONSOLE
206 ==============================================================================
210 Key_ClearEditLine (int edit_line)
212 memset (key_lines[edit_line], '\0', sizeof(key_lines[edit_line]));
213 key_lines[edit_line][0] = ']';
219 Interactive line editing and console scrollback
223 Key_Console (int key, char ascii)
225 // LordHavoc: copied most of this from Q2 to improve keyboard handling
252 case K_KP_RIGHTARROW:
272 if ((toupper(key) == 'V' && keydown[K_CTRL]) || ((key == K_INS || key == K_KP_INS) && keydown[K_SHIFT]))
275 if ((cbd = Sys_GetClipboardData()) != 0)
278 strtok(cbd, "\n\r\b");
279 i = (int)strlen(cbd);
280 if (i + key_linepos >= MAX_INPUTLINE)
281 i= MAX_INPUTLINE - key_linepos;
285 strlcat(key_lines[edit_line], cbd, sizeof(key_lines[edit_line]));
297 Cbuf_AddText ("clear\n");
302 if (key == K_ENTER || key == K_KP_ENTER)
304 Cbuf_AddText (key_lines[edit_line]+1); // skip the ]
306 Con_Printf("%s\n",key_lines[edit_line]);
307 if(key_lines[edit_line][1] == 0) // empty line (just a ])?
308 return; // no, no, you can't submit empty lines to the history...
309 // LordHavoc: redesigned edit_line/history_line
311 history_line = edit_line;
312 memmove(key_lines[0], key_lines[1], sizeof(key_lines[0]) * edit_line);
313 key_lines[edit_line][0] = ']';
314 key_lines[edit_line][1] = 0; // EvilTypeGuy: null terminate
316 // force an update, because the command may take some time
317 if (cls.state == ca_disconnected)
324 // Enhanced command completion
325 // by EvilTypeGuy eviltypeguy@qeradiant.com
326 // Thanks to Fett, Taniwha
327 Con_CompleteCommandLine();
331 // Advanced Console Editing by Radix radix@planetquake.com
332 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
335 // left arrow will just move left one without erasing, backspace will
336 // actually erase character
337 if (key == K_LEFTARROW || key == K_KP_LEFTARROW)
349 k = key_lines[edit_line][pos];
350 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
353 key_linepos = pos + 1;
360 // delete char before cursor
361 if (key == K_BACKSPACE || (key == 'h' && keydown[K_CTRL]))
365 strlcpy(key_lines[edit_line] + key_linepos - 1, key_lines[edit_line] + key_linepos, sizeof(key_lines[edit_line]) + 1 - key_linepos);
371 // delete char on cursor
372 if (key == K_DEL || key == K_KP_DEL)
375 linelen = strlen(key_lines[edit_line]);
376 if (key_linepos < (int)linelen)
377 memmove(key_lines[edit_line] + key_linepos, key_lines[edit_line] + key_linepos + 1, linelen - key_linepos);
382 // if we're at the end, get one character from previous line,
383 // otherwise just go right one
384 if (key == K_RIGHTARROW || key == K_KP_RIGHTARROW)
386 if (key_linepos >= (int)strlen(key_lines[edit_line]))
392 len = (int)strlen(key_lines[edit_line]);
396 k = key_lines[edit_line][pos];
397 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
407 if (key == K_INS || key == K_KP_INS) // toggle insert mode
413 // End Advanced Console Editing
415 if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL]))
417 if (history_line > 0 && key_lines[history_line-1][1])
421 linelen = strlen(key_lines[history_line]);
422 memcpy(key_lines[edit_line], key_lines[history_line], linelen + 1);
423 key_linepos = (int)linelen;
428 if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL]))
431 if (history_line >= edit_line)
433 history_line = edit_line;
434 key_lines[edit_line][0] = ']';
435 key_lines[edit_line][1] = 0;
441 linelen = strlen(key_lines[history_line]);
442 memcpy(key_lines[edit_line], key_lines[history_line], linelen + 1);
443 key_linepos = (int)linelen;
448 if (key == K_PGUP || key == K_KP_PGUP || key == K_MWHEELUP)
450 con_backscroll += ((int) vid_conheight.integer >> 5);
451 if (con_backscroll > con_totallines - (vid_conheight.integer>>3) - 1)
452 con_backscroll = con_totallines - (vid_conheight.integer>>3) - 1;
456 if (key == K_PGDN || key == K_KP_PGDN || key == K_MWHEELDOWN)
458 con_backscroll -= ((int) vid_conheight.integer >> 5);
459 if (con_backscroll < 0)
464 if (key == K_HOME || key == K_KP_HOME)
467 con_backscroll = con_totallines - (vid_conheight.integer>>3) - 1;
473 if (key == K_END || key == K_KP_END)
478 key_linepos = (int)strlen(key_lines[edit_line]);
486 if (key_linepos < MAX_INPUTLINE-1)
489 len = (int)strlen(&key_lines[edit_line][key_linepos]);
490 // check insert mode, or always insert if at end of line
491 if (key_insert || len == 0)
493 // can't use strcpy to move string to right
495 memmove(&key_lines[edit_line][key_linepos + 1], &key_lines[edit_line][key_linepos], len);
497 key_lines[edit_line][key_linepos] = ascii;
502 //============================================================================
505 char chat_buffer[MAX_INPUTLINE];
506 unsigned int chat_bufferlen = 0;
509 Key_Message (int key, char ascii)
514 Cmd_ForwardStringToServer(va("%s %s", chat_team ? "say_team" : "say ", chat_buffer));
522 if (key == K_ESCAPE) {
529 if (key == K_BACKSPACE) {
530 if (chat_bufferlen) {
532 chat_buffer[chat_bufferlen] = 0;
537 if (chat_bufferlen == sizeof (chat_buffer) - 1)
541 return; // non printable
543 chat_buffer[chat_bufferlen++] = ascii;
544 chat_buffer[chat_bufferlen] = 0;
547 //============================================================================
552 Returns a key number to be used to index keybindings[] by looking at
553 the given string. Single ascii characters return themselves, while
554 the K_* names are matched up.
558 Key_StringToKeynum (const char *str)
565 return tolower(str[0]);
567 for (kn = keynames; kn->name; kn++) {
568 if (!strcasecmp (str, kn->name))
576 Returns a string (either a single ascii char, or a K_* name) for the
578 FIXME: handle quote special (general escape sequence?)
582 Key_KeynumToString (int keynum)
585 static char tinystr[2];
588 return "<KEY NOT FOUND>";
589 if (keynum > 32 && keynum < 127) { // printable ascii
595 for (kn = keynames; kn->name; kn++)
596 if (keynum == kn->keynum)
599 return "<UNKNOWN KEYNUM>";
604 Key_SetBinding (int keynum, int bindmap, const char *binding)
609 if (keynum == -1 || keynum >= MAX_KEYS)
613 if (keybindings[bindmap][keynum]) {
614 Z_Free (keybindings[bindmap][keynum]);
615 keybindings[bindmap][keynum] = NULL;
617 // allocate memory for new binding
618 l = strlen (binding);
619 newbinding = (char *)Z_Malloc (l + 1);
620 memcpy (newbinding, binding, l + 1);
622 keybindings[bindmap][keynum] = newbinding;
626 Key_In_Unbind_f (void)
630 if (Cmd_Argc () != 3) {
631 Con_Print("in_unbind <bindmap> <key> : remove commands from a key\n");
635 m = strtol(Cmd_Argv (1), NULL, 0);
636 if ((m < 0) || (m >= 8)) {
637 Con_Printf("%d isn't a valid bindmap\n", m);
641 b = Key_StringToKeynum (Cmd_Argv (2));
643 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
647 Key_SetBinding (b, m, "");
654 char cmd[MAX_INPUTLINE];
658 if (c != 3 && c != 4) {
659 Con_Print("in_bind <bindmap> <key> [command] : attach a command to a key\n");
663 m = strtol(Cmd_Argv (1), NULL, 0);
664 if ((m < 0) || (m >= 8)) {
665 Con_Printf("%d isn't a valid bindmap\n", m);
669 b = Key_StringToKeynum (Cmd_Argv (2));
670 if (b == -1 || b >= MAX_KEYS) {
671 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
676 if (keybindings[m][b])
677 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (2), keybindings[m][b]);
679 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (2));
682 // copy the rest of the command line
683 cmd[0] = 0; // start out with a null string
684 for (i = 3; i < c; i++) {
685 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
687 strlcat (cmd, " ", sizeof (cmd));
690 Key_SetBinding (b, m, cmd);
694 Key_In_Bindmap_f (void)
701 Con_Print("in_bindmap <bindmap> <fallback>: set current bindmap and fallback\n");
705 m1 = strtol(Cmd_Argv (1), NULL, 0);
706 if ((m1 < 0) || (m1 >= 8)) {
707 Con_Printf("%d isn't a valid bindmap\n", m1);
711 m2 = strtol(Cmd_Argv (2), NULL, 0);
712 if ((m2 < 0) || (m2 >= 8)) {
713 Con_Printf("%d isn't a valid bindmap\n", m2);
726 if (Cmd_Argc () != 2) {
727 Con_Print("unbind <key> : remove commands from a key\n");
731 b = Key_StringToKeynum (Cmd_Argv (1));
733 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
737 Key_SetBinding (b, 0, "");
741 Key_Unbindall_f (void)
745 for (j = 0; j < 8; j++)
746 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
747 if (keybindings[j][i])
748 Key_SetBinding (i, j, "");
756 char cmd[MAX_INPUTLINE];
760 if (c != 2 && c != 3) {
761 Con_Print("bind <key> [command] : attach a command to a key\n");
764 b = Key_StringToKeynum (Cmd_Argv (1));
765 if (b == -1 || b >= MAX_KEYS) {
766 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
771 if (keybindings[0][b])
772 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (1), keybindings[0][b]);
774 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (1));
777 // copy the rest of the command line
778 cmd[0] = 0; // start out with a null string
779 for (i = 2; i < c; i++) {
780 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
782 strlcat (cmd, " ", sizeof (cmd));
785 Key_SetBinding (b, 0, cmd);
790 Writes lines containing "bind key value"
794 Key_WriteBindings (qfile_t *f)
798 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
799 if (keybindings[0][i])
800 FS_Printf(f, "bind \"%s\" \"%s\"\n",
801 Key_KeynumToString (i), keybindings[0][i]);
802 for (j = 1; j < 8; j++)
803 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
804 if (keybindings[j][i])
805 FS_Printf(f, "in_bind %d \"%s\" \"%s\"\n",
806 j, Key_KeynumToString (i), keybindings[j][i]);
815 for (i = 0; i < 32; i++) {
816 key_lines[i][0] = ']';
822 // register our functions
824 Cmd_AddCommand ("in_bind", Key_In_Bind_f, "binds a command to the specified key in the selected bindmap");
825 Cmd_AddCommand ("in_unbind", Key_In_Unbind_f, "removes command on the specified key in the selected bindmap");
826 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");
828 Cmd_AddCommand ("bind", Key_Bind_f, "binds a command to the specified key in bindmap 0");
829 Cmd_AddCommand ("unbind", Key_Unbind_f, "removes a command on the specified key in bindmap 0");
830 Cmd_AddCommand ("unbindall", Key_Unbindall_f, "removes all commands from all keys in all bindmaps (leaving only shift-escape and escape)");
832 Cvar_RegisterVariable (&con_closeontoggleconsole);
835 const char *Key_GetBind (int key)
838 if (key < 0 || key >= MAX_KEYS)
840 bind = keybindings[key_bmap][key];
842 bind = keybindings[key_bmap2][key];
846 qboolean CL_VM_InputEvent (qboolean pressed, int key);
850 Called by the system between frames for both key up and key down events
851 Should NOT be called during an interrupt!
855 Key_Event (int key, char ascii, qboolean down)
860 if (key < 0 || key >= MAX_KEYS)
864 bind = keybindings[key_bmap][key];
866 bind = keybindings[key_bmap2][key];
868 if(key_dest == key_game)
870 q = CL_VM_InputEvent(!down, key);
874 keydown[key] = min(keydown[key] + 1, 2);
883 // increment key repeat count each time a down is received so that things
884 // which want to ignore key repeat can ignore it
885 keydown[key] = min(keydown[key] + 1, 2);
889 // clear repeat count now that the key is released
891 // key up events only generate commands if the game key binding is a button
892 // command (leading + sign). These will occur even in console mode, to
893 // keep the character from continuing an action started before a console
894 // switch. Button commands include the kenum as a parameter, so multiple
895 // downs can be matched with ups
896 if (bind && bind[0] == '+')
897 Cbuf_AddText(va("-%s %i\n", bind + 1, key));
900 // key_consoleactive is a flag not a key_dest because the console is a
901 // high priority overlay ontop of the normal screen (designed as a safety
902 // feature so that developers and users can rescue themselves from a bad
905 // this also means that toggling the console on/off does not lose the old
908 // specially handle escape (togglemenu) and shift-escape (toggleconsole)
909 // engine bindings, these are not handled as normal binds so that the user
910 // can recover from a completely empty bindmap
913 // ignore key repeats on escape
914 if (keydown[key] > 1)
916 // escape does these things:
917 // key_consoleactive - close console
918 // key_message - abort messagemode
919 // key_menu - go to parent menu (or key_game)
920 // key_game - open menu
921 // in all modes shift-escape toggles console
922 if (((key_consoleactive & KEY_CONSOLEACTIVE_USER) || keydown[K_SHIFT]) && down)
924 Con_ToggleConsole_f ();
931 Key_Message (key, ascii);
934 MR_KeyEvent (key, ascii, down);
941 Con_Printf ("Key_Event: Bad key_dest\n");
946 // send function keydowns to interpreter no matter what mode is
947 if (key >= K_F1 && key <= K_F12 && down)
949 // ignore key repeats on F1-F12 binds
950 if (keydown[key] > 1)
954 // button commands add keynum as a parm
956 Cbuf_AddText (va("%s %i\n", bind, key));
967 // ignore binds (other than the above escape/F1-F12 keys) while in console
968 if (key_consoleactive && down)
970 // respond to toggleconsole binds while in console unless the pressed key
971 // happens to be the color prefix character (such as on German keyboards)
972 if (key_consoleactive && down && (!con_closeontoggleconsole.integer || !bind || strncmp(bind, "toggleconsole", strlen("toggleconsole")) || ascii == STRING_COLOR_TAG))
975 Key_Console (key, ascii);
979 // ignore binds while a video is played, let the video system handle the key event
982 CL_Video_KeyEvent (key, ascii, keydown[key] != 0);
986 // anything else is a key press into the game, chat line, or menu
991 Key_Message (key, ascii);
994 MR_KeyEvent (key, ascii, down);
997 // ignore key repeats on binds
998 if (bind && keydown[key] == 1 && down)
1000 // button commands add keynum as a parm
1002 Cbuf_AddText (va("%s %i\n", bind, key));
1005 Cbuf_AddText (bind);
1006 Cbuf_AddText ("\n");
1011 Con_Printf ("Key_Event: Bad key_dest\n");
1021 Key_ClearStates (void)
1023 memset(keydown, 0, sizeof(keydown));