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 strcat(key_lines[edit_line], cbd);
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 // LordHavoc: redesigned edit_line/history_line
307 history_line = edit_line;
308 memmove(key_lines[0], key_lines[1], sizeof(key_lines[0]) * edit_line);
309 key_lines[edit_line][0] = ']';
310 key_lines[edit_line][1] = 0; // EvilTypeGuy: null terminate
312 // force an update, because the command may take some time
313 if (cls.state == ca_disconnected)
320 // Enhanced command completion
321 // by EvilTypeGuy eviltypeguy@qeradiant.com
322 // Thanks to Fett, Taniwha
323 Con_CompleteCommandLine();
327 // Advanced Console Editing by Radix radix@planetquake.com
328 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
331 // left arrow will just move left one without erasing, backspace will
332 // actually erase character
333 if (key == K_LEFTARROW || key == K_KP_LEFTARROW)
345 k = key_lines[edit_line][pos];
346 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
349 key_linepos = pos + 1;
356 // delete char before cursor
357 if (key == K_BACKSPACE || (key == 'h' && keydown[K_CTRL]))
361 strcpy(key_lines[edit_line] + key_linepos - 1, key_lines[edit_line] + key_linepos);
367 // delete char on cursor
368 if (key == K_DEL || key == K_KP_DEL)
370 if (key_linepos < (int)strlen(key_lines[edit_line]))
371 strcpy(key_lines[edit_line] + key_linepos, key_lines[edit_line] + key_linepos + 1);
376 // if we're at the end, get one character from previous line,
377 // otherwise just go right one
378 if (key == K_RIGHTARROW || key == K_KP_RIGHTARROW)
380 if (key_linepos >= (int)strlen(key_lines[edit_line]))
386 len = (int)strlen(key_lines[edit_line]);
390 k = key_lines[edit_line][pos];
391 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
401 if (key == K_INS || key == K_KP_INS) // toggle insert mode
407 // End Advanced Console Editing
409 if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL]))
411 if (history_line > 0 && key_lines[history_line-1][1])
414 strcpy(key_lines[edit_line], key_lines[history_line]);
415 key_linepos = (int)strlen(key_lines[edit_line]);
420 if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL]))
423 if (history_line >= edit_line)
425 history_line = edit_line;
426 key_lines[edit_line][0] = ']';
427 key_lines[edit_line][1] = 0;
432 strcpy(key_lines[edit_line], key_lines[history_line]);
433 key_linepos = (int)strlen(key_lines[edit_line]);
438 if (key == K_PGUP || key == K_KP_PGUP || key == K_MWHEELUP)
440 con_backscroll += ((int) vid_conheight.integer >> 5);
441 if (con_backscroll > con_totallines - (vid_conheight.integer>>3) - 1)
442 con_backscroll = con_totallines - (vid_conheight.integer>>3) - 1;
446 if (key == K_PGDN || key == K_KP_PGDN || key == K_MWHEELDOWN)
448 con_backscroll -= ((int) vid_conheight.integer >> 5);
449 if (con_backscroll < 0)
454 if (key == K_HOME || key == K_KP_HOME)
457 con_backscroll = con_totallines - (vid_conheight.integer>>3) - 1;
463 if (key == K_END || key == K_KP_END)
468 key_linepos = (int)strlen(key_lines[edit_line]);
476 if (key_linepos < MAX_INPUTLINE-1)
479 len = (int)strlen(&key_lines[edit_line][key_linepos]);
480 // check insert mode, or always insert if at end of line
481 if (key_insert || len == 0)
483 // can't use strcpy to move string to right
485 memmove(&key_lines[edit_line][key_linepos + 1], &key_lines[edit_line][key_linepos], len);
487 key_lines[edit_line][key_linepos] = ascii;
492 //============================================================================
495 char chat_buffer[MAX_INPUTLINE];
496 unsigned int chat_bufferlen = 0;
499 Key_Message (int key, char ascii)
504 Cmd_ForwardStringToServer(va("%s %s", chat_team ? "say_team" : "say ", chat_buffer));
512 if (key == K_ESCAPE) {
519 if (key == K_BACKSPACE) {
520 if (chat_bufferlen) {
522 chat_buffer[chat_bufferlen] = 0;
527 if (chat_bufferlen == sizeof (chat_buffer) - 1)
531 return; // non printable
533 chat_buffer[chat_bufferlen++] = ascii;
534 chat_buffer[chat_bufferlen] = 0;
537 //============================================================================
542 Returns a key number to be used to index keybindings[] by looking at
543 the given string. Single ascii characters return themselves, while
544 the K_* names are matched up.
548 Key_StringToKeynum (const char *str)
555 return tolower(str[0]);
557 for (kn = keynames; kn->name; kn++) {
558 if (!strcasecmp (str, kn->name))
566 Returns a string (either a single ascii char, or a K_* name) for the
568 FIXME: handle quote special (general escape sequence?)
572 Key_KeynumToString (int keynum)
575 static char tinystr[2];
578 return "<KEY NOT FOUND>";
579 if (keynum > 32 && keynum < 127) { // printable ascii
585 for (kn = keynames; kn->name; kn++)
586 if (keynum == kn->keynum)
589 return "<UNKNOWN KEYNUM>";
594 Key_SetBinding (int keynum, int bindmap, const char *binding)
603 if (keybindings[bindmap][keynum]) {
604 Z_Free (keybindings[bindmap][keynum]);
605 keybindings[bindmap][keynum] = NULL;
607 // allocate memory for new binding
608 l = strlen (binding);
609 newbinding = (char *)Z_Malloc (l + 1);
610 strcpy (newbinding, binding);
612 keybindings[bindmap][keynum] = newbinding;
616 Key_In_Unbind_f (void)
620 if (Cmd_Argc () != 3) {
621 Con_Print("in_unbind <bindmap> <key> : remove commands from a key\n");
625 m = strtol(Cmd_Argv (1), NULL, 0);
626 if ((m < 0) || (m >= 8)) {
627 Con_Printf("%d isn't a valid bindmap\n", m);
631 b = Key_StringToKeynum (Cmd_Argv (2));
633 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
637 Key_SetBinding (b, m, "");
644 char cmd[MAX_INPUTLINE];
648 if (c != 3 && c != 4) {
649 Con_Print("in_bind <bindmap> <key> [command] : attach a command to a key\n");
653 m = strtol(Cmd_Argv (1), NULL, 0);
654 if ((m < 0) || (m >= 8)) {
655 Con_Printf("%d isn't a valid bindmap\n", m);
659 b = Key_StringToKeynum (Cmd_Argv (2));
661 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
666 if (keybindings[m][b])
667 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (2), keybindings[m][b]);
669 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (2));
672 // copy the rest of the command line
673 cmd[0] = 0; // start out with a null string
674 for (i = 3; i < c; i++) {
675 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
677 strlcat (cmd, " ", sizeof (cmd));
680 Key_SetBinding (b, m, cmd);
684 Key_In_Bindmap_f (void)
691 Con_Print("in_bindmap <bindmap> <fallback>: set current bindmap and fallback\n");
695 m1 = strtol(Cmd_Argv (1), NULL, 0);
696 if ((m1 < 0) || (m1 >= 8)) {
697 Con_Printf("%d isn't a valid bindmap\n", m1);
701 m2 = strtol(Cmd_Argv (2), NULL, 0);
702 if ((m2 < 0) || (m2 >= 8)) {
703 Con_Printf("%d isn't a valid bindmap\n", m2);
716 if (Cmd_Argc () != 2) {
717 Con_Print("unbind <key> : remove commands from a key\n");
721 b = Key_StringToKeynum (Cmd_Argv (1));
723 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
727 Key_SetBinding (b, 0, "");
731 Key_Unbindall_f (void)
735 for (j = 0; j < 8; j++)
736 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
737 if (keybindings[j][i])
738 Key_SetBinding (i, j, "");
746 char cmd[MAX_INPUTLINE];
750 if (c != 2 && c != 3) {
751 Con_Print("bind <key> [command] : attach a command to a key\n");
754 b = Key_StringToKeynum (Cmd_Argv (1));
756 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
761 if (keybindings[0][b])
762 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (1), keybindings[0][b]);
764 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (1));
767 // copy the rest of the command line
768 cmd[0] = 0; // start out with a null string
769 for (i = 2; i < c; i++) {
770 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
772 strlcat (cmd, " ", sizeof (cmd));
775 Key_SetBinding (b, 0, cmd);
780 Writes lines containing "bind key value"
784 Key_WriteBindings (qfile_t *f)
788 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
789 if (keybindings[0][i])
790 FS_Printf(f, "bind \"%s\" \"%s\"\n",
791 Key_KeynumToString (i), keybindings[0][i]);
792 for (j = 1; j < 8; j++)
793 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
794 if (keybindings[j][i])
795 FS_Printf(f, "in_bind %d \"%s\" \"%s\"\n",
796 j, Key_KeynumToString (i), keybindings[j][i]);
805 for (i = 0; i < 32; i++) {
806 key_lines[i][0] = ']';
812 // register our functions
814 Cmd_AddCommand ("in_bind", Key_In_Bind_f, "binds a command to the specified key in the selected bindmap");
815 Cmd_AddCommand ("in_unbind", Key_In_Unbind_f, "removes command on the specified key in the selected bindmap");
816 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");
818 Cmd_AddCommand ("bind", Key_Bind_f, "binds a command to the specified key in bindmap 0");
819 Cmd_AddCommand ("unbind", Key_Unbind_f, "removes a command on the specified key in bindmap 0");
820 Cmd_AddCommand ("unbindall", Key_Unbindall_f, "removes all commands from all keys in all bindmaps (leaving only shift-escape and escape)");
823 const char *Key_GetBind (int key)
826 bind = keybindings[key_bmap][key];
828 bind = keybindings[key_bmap2][key];
832 qboolean CL_VM_InputEvent (qboolean pressed, int key);
836 Called by the system between frames for both key up and key down events
837 Should NOT be called during an interrupt!
841 Key_Event (int key, char ascii, qboolean down)
847 bind = keybindings[key_bmap][key];
849 bind = keybindings[key_bmap2][key];
851 if(key_dest == key_game)
853 q = CL_VM_InputEvent(!down, key);
857 keydown[key] = min(keydown[key] + 1, 2);
866 // clear repeat count now that the key is released
868 // key up events only generate commands if the game key binding is a button
869 // command (leading + sign). These will occur even in console mode, to
870 // keep the character from continuing an action started before a console
871 // switch. Button commands include the kenum as a parameter, so multiple
872 // downs can be matched with ups
873 if (bind && bind[0] == '+')
874 Cbuf_AddText(va("-%s %i\n", bind + 1, key));
878 // from here on we know this is a down event
880 // increment key repeat count each time a down is received so that things
881 // which want to ignore key repeat can ignore it
882 keydown[key] = min(keydown[key] + 1, 2);
884 // key_consoleactive is a flag not a key_dest because the console is a
885 // high priority overlay ontop of the normal screen (designed as a safety
886 // feature so that developers and users can rescue themselves from a bad
889 // this also means that toggling the console on/off does not lose the old
892 // specially handle escape (togglemenu) and shift-escape (toggleconsole)
893 // engine bindings, these are not handled as normal binds so that the user
894 // can recover from a completely empty bindmap
897 // ignore key repeats on escape
898 if (keydown[key] > 1)
900 // escape does these things:
901 // key_consoleactive - close console
902 // key_message - abort messagemode
903 // key_menu - go to parent menu (or key_game)
904 // key_game - open menu
905 // in all modes shift-escape toggles console
906 if ((key_consoleactive & KEY_CONSOLEACTIVE_USER) || keydown[K_SHIFT])
908 Con_ToggleConsole_f ();
914 Key_Message (key, ascii);
917 MR_Keydown (key, ascii);
923 if(UI_Callback_IsSlotUsed(key_dest - 3))
924 UI_Callback_KeyDown (key, ascii);
926 Con_Printf ("Key_Event: Bad key_dest\n");
931 // send function keydowns to interpreter no matter what mode is
932 if (key >= K_F1 && key <= K_F12)
934 // ignore key repeats on F1-F12 binds
935 if (keydown[key] > 1)
939 // button commands add keynum as a parm
941 Cbuf_AddText (va("%s %i\n", bind, key));
952 // ignore binds (other than the above escape/F1-F12 keys) while in console
953 if (key_consoleactive)
955 // respond to toggleconsole binds while in console unless the pressed key
956 // happens to be the color prefix character (such as on German keyboards)
957 if (key_consoleactive && (strncmp(bind, "toggleconsole", strlen("toggleconsole")) || ascii == STRING_COLOR_TAG))
960 Key_Console (key, ascii);
964 // anything else is a key press into the game, chat line, or menu
968 Key_Message (key, ascii);
971 MR_Keydown (key, ascii);
974 // ignore key repeats on binds
975 if (bind && keydown[key] == 1)
977 // button commands add keynum as a parm
979 Cbuf_AddText (va("%s %i\n", bind, key));
988 if(UI_Callback_IsSlotUsed(key_dest - 3))
989 UI_Callback_KeyDown (key, ascii);
991 Con_Printf ("Key_Event: Bad key_dest\n");
1001 Key_ClearStates (void)
1003 memset(keydown, 0, sizeof(keydown));