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
27 key up events are sent even if in console mode
30 int edit_line = MAX_INPUTLINES-1;
31 int history_line = MAX_INPUTLINES-1;
32 char key_lines[MAX_INPUTLINES][MAX_INPUTLINE];
34 qboolean key_insert = true; // insert key toggle (for editing)
36 int key_consoleactive;
37 char *keybindings[MAX_BINDMAPS][MAX_KEYS];
39 static int key_bmap, key_bmap2;
40 static unsigned char keydown[MAX_KEYS]; // 0 = up, 1 = down, 2 = repeating
42 typedef struct keyname_s
49 static const keyname_t keynames[] = {
54 {"BACKSPACE", K_BACKSPACE},
55 {"UPARROW", K_UPARROW},
56 {"DOWNARROW", K_DOWNARROW},
57 {"LEFTARROW", K_LEFTARROW},
58 {"RIGHTARROW", K_RIGHTARROW},
86 {"MWHEELUP", K_MWHEELUP},
87 {"MWHEELDOWN", K_MWHEELDOWN},
98 {"MOUSE10", K_MOUSE10},
99 {"MOUSE11", K_MOUSE11},
100 {"MOUSE12", K_MOUSE12},
101 {"MOUSE13", K_MOUSE13},
102 {"MOUSE14", K_MOUSE14},
103 {"MOUSE15", K_MOUSE15},
104 {"MOUSE16", K_MOUSE16},
106 {"NUMLOCK", K_NUMLOCK},
107 {"CAPSLOCK", K_CAPSLOCK},
108 {"SCROLLOCK", K_SCROLLOCK},
110 {"KP_HOME", K_KP_HOME },
111 {"KP_UPARROW", K_KP_UPARROW },
112 {"KP_PGUP", K_KP_PGUP },
113 {"KP_LEFTARROW", K_KP_LEFTARROW },
114 {"KP_RIGHTARROW", K_KP_RIGHTARROW },
115 {"KP_END", K_KP_END },
116 {"KP_DOWNARROW", K_KP_DOWNARROW },
117 {"KP_PGDN", K_KP_PGDN },
118 {"KP_INS", K_KP_INS },
119 {"KP_DEL", K_KP_DEL },
120 {"KP_SLASH", K_KP_SLASH },
132 {"KP_PERIOD", K_KP_PERIOD},
133 {"KP_DIVIDE", K_KP_DIVIDE},
134 {"KP_MULTIPLY", K_KP_MULTIPLY},
135 {"KP_MINUS", K_KP_MINUS},
136 {"KP_PLUS", K_KP_PLUS},
137 {"KP_ENTER", K_KP_ENTER},
138 {"KP_EQUALS", K_KP_EQUALS},
190 {"SEMICOLON", ';'}, // because a raw semicolon seperates commands
194 {"APOSTROPHE", '\''},
200 ==============================================================================
202 LINE TYPING INTO THE CONSOLE
204 ==============================================================================
208 Key_ClearEditLine (int edit_line)
210 memset (key_lines[edit_line], '\0', sizeof(key_lines[edit_line]));
211 key_lines[edit_line][0] = ']';
217 Interactive line editing and console scrollback
221 Key_Console (int key, char ascii)
223 // LordHavoc: copied most of this from Q2 to improve keyboard handling
250 case K_KP_RIGHTARROW:
270 if ((toupper(key) == 'V' && keydown[K_CTRL]) || ((key == K_INS || key == K_KP_INS) && keydown[K_SHIFT]))
273 if ((cbd = Sys_GetClipboardData()) != 0)
276 strtok(cbd, "\n\r\b");
277 i = (int)strlen(cbd);
278 if (i + key_linepos >= MAX_INPUTLINE)
279 i= MAX_INPUTLINE - key_linepos;
283 strlcat(key_lines[edit_line], cbd, sizeof(key_lines[edit_line]));
295 Cbuf_AddText ("clear\n");
300 if (key == K_ENTER || key == K_KP_ENTER)
302 Cbuf_AddText (key_lines[edit_line]+1); // skip the ]
304 Con_Printf("%s\n",key_lines[edit_line]);
305 if(key_lines[edit_line][1] == 0) // empty line (just a [)?
306 return; // no, no, you can't submit empty lines to the history...
307 // LordHavoc: redesigned edit_line/history_line
309 history_line = edit_line;
310 memmove(key_lines[0], key_lines[1], sizeof(key_lines[0]) * edit_line);
311 key_lines[edit_line][0] = ']';
312 key_lines[edit_line][1] = 0; // EvilTypeGuy: null terminate
314 // force an update, because the command may take some time
315 if (cls.state == ca_disconnected)
322 // Enhanced command completion
323 // by EvilTypeGuy eviltypeguy@qeradiant.com
324 // Thanks to Fett, Taniwha
325 Con_CompleteCommandLine();
329 // Advanced Console Editing by Radix radix@planetquake.com
330 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
333 // left arrow will just move left one without erasing, backspace will
334 // actually erase character
335 if (key == K_LEFTARROW || key == K_KP_LEFTARROW)
347 k = key_lines[edit_line][pos];
348 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
351 key_linepos = pos + 1;
358 // delete char before cursor
359 if (key == K_BACKSPACE || (key == 'h' && keydown[K_CTRL]))
363 strlcpy(key_lines[edit_line] + key_linepos - 1, key_lines[edit_line] + key_linepos, sizeof(key_lines[edit_line]) + 1 - key_linepos);
369 // delete char on cursor
370 if (key == K_DEL || key == K_KP_DEL)
373 linelen = strlen(key_lines[edit_line]);
374 if (key_linepos < (int)linelen)
375 memmove(key_lines[edit_line] + key_linepos, key_lines[edit_line] + key_linepos + 1, linelen - key_linepos);
380 // if we're at the end, get one character from previous line,
381 // otherwise just go right one
382 if (key == K_RIGHTARROW || key == K_KP_RIGHTARROW)
384 if (key_linepos >= (int)strlen(key_lines[edit_line]))
390 len = (int)strlen(key_lines[edit_line]);
394 k = key_lines[edit_line][pos];
395 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
405 if (key == K_INS || key == K_KP_INS) // toggle insert mode
411 // End Advanced Console Editing
413 if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL]))
415 if (history_line > 0 && key_lines[history_line-1][1])
419 linelen = strlen(key_lines[history_line]);
420 memcpy(key_lines[edit_line], key_lines[history_line], linelen + 1);
421 key_linepos = (int)linelen;
426 if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL]))
429 if (history_line >= edit_line)
431 history_line = edit_line;
432 key_lines[edit_line][0] = ']';
433 key_lines[edit_line][1] = 0;
439 linelen = strlen(key_lines[history_line]);
440 memcpy(key_lines[edit_line], key_lines[history_line], linelen + 1);
441 key_linepos = (int)linelen;
446 if (key == K_PGUP || key == K_KP_PGUP || key == K_MWHEELUP)
448 con_backscroll += ((int) vid_conheight.integer >> 5);
449 if (con_backscroll > con_totallines - (vid_conheight.integer>>3) - 1)
450 con_backscroll = con_totallines - (vid_conheight.integer>>3) - 1;
454 if (key == K_PGDN || key == K_KP_PGDN || key == K_MWHEELDOWN)
456 con_backscroll -= ((int) vid_conheight.integer >> 5);
457 if (con_backscroll < 0)
462 if (key == K_HOME || key == K_KP_HOME)
465 con_backscroll = con_totallines - (vid_conheight.integer>>3) - 1;
471 if (key == K_END || key == K_KP_END)
476 key_linepos = (int)strlen(key_lines[edit_line]);
484 if (key_linepos < MAX_INPUTLINE-1)
487 len = (int)strlen(&key_lines[edit_line][key_linepos]);
488 // check insert mode, or always insert if at end of line
489 if (key_insert || len == 0)
491 // can't use strcpy to move string to right
493 memmove(&key_lines[edit_line][key_linepos + 1], &key_lines[edit_line][key_linepos], len);
495 key_lines[edit_line][key_linepos] = ascii;
500 //============================================================================
503 char chat_buffer[MAX_INPUTLINE];
504 unsigned int chat_bufferlen = 0;
507 Key_Message (int key, char ascii)
512 Cmd_ForwardStringToServer(va("%s %s", chat_team ? "say_team" : "say ", chat_buffer));
520 if (key == K_ESCAPE) {
527 if (key == K_BACKSPACE) {
528 if (chat_bufferlen) {
530 chat_buffer[chat_bufferlen] = 0;
535 if (chat_bufferlen == sizeof (chat_buffer) - 1)
539 return; // non printable
541 chat_buffer[chat_bufferlen++] = ascii;
542 chat_buffer[chat_bufferlen] = 0;
545 //============================================================================
550 Returns a key number to be used to index keybindings[] by looking at
551 the given string. Single ascii characters return themselves, while
552 the K_* names are matched up.
556 Key_StringToKeynum (const char *str)
563 return tolower(str[0]);
565 for (kn = keynames; kn->name; kn++) {
566 if (!strcasecmp (str, kn->name))
574 Returns a string (either a single ascii char, or a K_* name) for the
576 FIXME: handle quote special (general escape sequence?)
580 Key_KeynumToString (int keynum)
583 static char tinystr[2];
586 return "<KEY NOT FOUND>";
587 if (keynum > 32 && keynum < 127) { // printable ascii
593 for (kn = keynames; kn->name; kn++)
594 if (keynum == kn->keynum)
597 return "<UNKNOWN KEYNUM>";
602 Key_SetBinding (int keynum, int bindmap, const char *binding)
607 if (keynum == -1 || keynum >= MAX_KEYS)
611 if (keybindings[bindmap][keynum]) {
612 Z_Free (keybindings[bindmap][keynum]);
613 keybindings[bindmap][keynum] = NULL;
615 // allocate memory for new binding
616 l = strlen (binding);
617 newbinding = (char *)Z_Malloc (l + 1);
618 memcpy (newbinding, binding, l + 1);
620 keybindings[bindmap][keynum] = newbinding;
624 Key_In_Unbind_f (void)
628 if (Cmd_Argc () != 3) {
629 Con_Print("in_unbind <bindmap> <key> : remove commands from a key\n");
633 m = strtol(Cmd_Argv (1), NULL, 0);
634 if ((m < 0) || (m >= 8)) {
635 Con_Printf("%d isn't a valid bindmap\n", m);
639 b = Key_StringToKeynum (Cmd_Argv (2));
641 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
645 Key_SetBinding (b, m, "");
652 char cmd[MAX_INPUTLINE];
656 if (c != 3 && c != 4) {
657 Con_Print("in_bind <bindmap> <key> [command] : attach a command to a key\n");
661 m = strtol(Cmd_Argv (1), NULL, 0);
662 if ((m < 0) || (m >= 8)) {
663 Con_Printf("%d isn't a valid bindmap\n", m);
667 b = Key_StringToKeynum (Cmd_Argv (2));
668 if (b == -1 || b >= MAX_KEYS) {
669 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
674 if (keybindings[m][b])
675 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (2), keybindings[m][b]);
677 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (2));
680 // copy the rest of the command line
681 cmd[0] = 0; // start out with a null string
682 for (i = 3; i < c; i++) {
683 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
685 strlcat (cmd, " ", sizeof (cmd));
688 Key_SetBinding (b, m, cmd);
692 Key_In_Bindmap_f (void)
699 Con_Print("in_bindmap <bindmap> <fallback>: set current bindmap and fallback\n");
703 m1 = strtol(Cmd_Argv (1), NULL, 0);
704 if ((m1 < 0) || (m1 >= 8)) {
705 Con_Printf("%d isn't a valid bindmap\n", m1);
709 m2 = strtol(Cmd_Argv (2), NULL, 0);
710 if ((m2 < 0) || (m2 >= 8)) {
711 Con_Printf("%d isn't a valid bindmap\n", m2);
724 if (Cmd_Argc () != 2) {
725 Con_Print("unbind <key> : remove commands from a key\n");
729 b = Key_StringToKeynum (Cmd_Argv (1));
731 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
735 Key_SetBinding (b, 0, "");
739 Key_Unbindall_f (void)
743 for (j = 0; j < 8; j++)
744 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
745 if (keybindings[j][i])
746 Key_SetBinding (i, j, "");
754 char cmd[MAX_INPUTLINE];
758 if (c != 2 && c != 3) {
759 Con_Print("bind <key> [command] : attach a command to a key\n");
762 b = Key_StringToKeynum (Cmd_Argv (1));
763 if (b == -1 || b >= MAX_KEYS) {
764 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
769 if (keybindings[0][b])
770 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (1), keybindings[0][b]);
772 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (1));
775 // copy the rest of the command line
776 cmd[0] = 0; // start out with a null string
777 for (i = 2; i < c; i++) {
778 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
780 strlcat (cmd, " ", sizeof (cmd));
783 Key_SetBinding (b, 0, cmd);
788 Writes lines containing "bind key value"
792 Key_WriteBindings (qfile_t *f)
796 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
797 if (keybindings[0][i])
798 FS_Printf(f, "bind \"%s\" \"%s\"\n",
799 Key_KeynumToString (i), keybindings[0][i]);
800 for (j = 1; j < 8; j++)
801 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
802 if (keybindings[j][i])
803 FS_Printf(f, "in_bind %d \"%s\" \"%s\"\n",
804 j, Key_KeynumToString (i), keybindings[j][i]);
813 for (i = 0; i < 32; i++) {
814 key_lines[i][0] = ']';
820 // register our functions
822 Cmd_AddCommand ("in_bind", Key_In_Bind_f, "binds a command to the specified key in the selected bindmap");
823 Cmd_AddCommand ("in_unbind", Key_In_Unbind_f, "removes command on the specified key in the selected bindmap");
824 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");
826 Cmd_AddCommand ("bind", Key_Bind_f, "binds a command to the specified key in bindmap 0");
827 Cmd_AddCommand ("unbind", Key_Unbind_f, "removes a command on the specified key in bindmap 0");
828 Cmd_AddCommand ("unbindall", Key_Unbindall_f, "removes all commands from all keys in all bindmaps (leaving only shift-escape and escape)");
831 const char *Key_GetBind (int key)
834 if (key < 0 || key >= MAX_KEYS)
836 bind = keybindings[key_bmap][key];
838 bind = keybindings[key_bmap2][key];
842 qboolean CL_VM_InputEvent (qboolean pressed, int key);
846 Called by the system between frames for both key up and key down events
847 Should NOT be called during an interrupt!
851 Key_Event (int key, char ascii, qboolean down)
856 if (key < 0 || key >= MAX_KEYS)
860 bind = keybindings[key_bmap][key];
862 bind = keybindings[key_bmap2][key];
864 if(key_dest == key_game)
866 q = CL_VM_InputEvent(!down, key);
870 keydown[key] = min(keydown[key] + 1, 2);
879 // increment key repeat count each time a down is received so that things
880 // which want to ignore key repeat can ignore it
881 keydown[key] = min(keydown[key] + 1, 2);
885 // clear repeat count now that the key is released
887 // key up events only generate commands if the game key binding is a button
888 // command (leading + sign). These will occur even in console mode, to
889 // keep the character from continuing an action started before a console
890 // switch. Button commands include the kenum as a parameter, so multiple
891 // downs can be matched with ups
892 if (bind && bind[0] == '+')
893 Cbuf_AddText(va("-%s %i\n", bind + 1, key));
896 // key_consoleactive is a flag not a key_dest because the console is a
897 // high priority overlay ontop of the normal screen (designed as a safety
898 // feature so that developers and users can rescue themselves from a bad
901 // this also means that toggling the console on/off does not lose the old
904 // specially handle escape (togglemenu) and shift-escape (toggleconsole)
905 // engine bindings, these are not handled as normal binds so that the user
906 // can recover from a completely empty bindmap
909 // ignore key repeats on escape
910 if (keydown[key] > 1)
912 // escape does these things:
913 // key_consoleactive - close console
914 // key_message - abort messagemode
915 // key_menu - go to parent menu (or key_game)
916 // key_game - open menu
917 // in all modes shift-escape toggles console
918 if (((key_consoleactive & KEY_CONSOLEACTIVE_USER) || keydown[K_SHIFT]) && down)
920 Con_ToggleConsole_f ();
927 Key_Message (key, ascii);
930 MR_KeyEvent (key, ascii, down);
937 Con_Printf ("Key_Event: Bad key_dest\n");
942 // send function keydowns to interpreter no matter what mode is
943 if (key >= K_F1 && key <= K_F12 && down)
945 // ignore key repeats on F1-F12 binds
946 if (keydown[key] > 1)
950 // button commands add keynum as a parm
952 Cbuf_AddText (va("%s %i\n", bind, key));
963 // ignore binds (other than the above escape/F1-F12 keys) while in console
964 if (key_consoleactive && down)
966 // respond to toggleconsole binds while in console unless the pressed key
967 // happens to be the color prefix character (such as on German keyboards)
968 if (key_consoleactive && down && (strncmp(bind, "toggleconsole", strlen("toggleconsole")) || ascii == STRING_COLOR_TAG))
971 Key_Console (key, ascii);
975 // ignore binds while a video is played, let the video system handle the key event
978 CL_Video_KeyEvent (key, ascii, keydown[key] != 0);
982 // anything else is a key press into the game, chat line, or menu
987 Key_Message (key, ascii);
990 MR_KeyEvent (key, ascii, down);
993 // ignore key repeats on binds
994 if (bind && keydown[key] == 1 && down)
996 // button commands add keynum as a parm
998 Cbuf_AddText (va("%s %i\n", bind, key));
1001 Cbuf_AddText (bind);
1002 Cbuf_AddText ("\n");
1007 Con_Printf ("Key_Event: Bad key_dest\n");
1017 Key_ClearStates (void)
1019 memset(keydown, 0, sizeof(keydown));