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 qbyte keydown[MAX_KEYS]; // 0 = up, 1 = down, 2 = repeating
47 static const keyname_t keynames[] = {
52 {"BACKSPACE", K_BACKSPACE},
53 {"UPARROW", K_UPARROW},
54 {"DOWNARROW", K_DOWNARROW},
55 {"LEFTARROW", K_LEFTARROW},
56 {"RIGHTARROW", K_RIGHTARROW},
84 {"MWHEELUP", K_MWHEELUP},
85 {"MWHEELDOWN", K_MWHEELDOWN},
96 {"MOUSE10", K_MOUSE10},
97 {"MOUSE11", K_MOUSE11},
98 {"MOUSE12", K_MOUSE12},
99 {"MOUSE13", K_MOUSE13},
100 {"MOUSE14", K_MOUSE14},
101 {"MOUSE15", K_MOUSE15},
102 {"MOUSE16", K_MOUSE16},
104 {"NUMLOCK", K_NUMLOCK},
105 {"CAPSLOCK", K_CAPSLOCK},
106 {"SCROLLOCK", K_SCROLLOCK},
108 {"KP_HOME", K_KP_HOME },
109 {"KP_UPARROW", K_KP_UPARROW },
110 {"KP_PGUP", K_KP_PGUP },
111 {"KP_LEFTARROW", K_KP_LEFTARROW },
112 {"KP_RIGHTARROW", K_KP_RIGHTARROW },
113 {"KP_END", K_KP_END },
114 {"KP_DOWNARROW", K_KP_DOWNARROW },
115 {"KP_PGDN", K_KP_PGDN },
116 {"KP_INS", K_KP_INS },
117 {"KP_DEL", K_KP_DEL },
118 {"KP_SLASH", K_KP_SLASH },
130 {"KP_PERIOD", K_KP_PERIOD},
131 {"KP_DIVIDE", K_KP_DIVIDE},
132 {"KP_MULTIPLY", K_KP_MULTIPLY},
133 {"KP_MINUS", K_KP_MINUS},
134 {"KP_PLUS", K_KP_PLUS},
135 {"KP_ENTER", K_KP_ENTER},
136 {"KP_EQUALS", K_KP_EQUALS},
188 {"SEMICOLON", ';'}, // because a raw semicolon seperates commands
192 {"APOSTROPHE", '\''},
198 ==============================================================================
200 LINE TYPING INTO THE CONSOLE
202 ==============================================================================
206 Key_ClearEditLine (int edit_line)
208 memset (key_lines[edit_line], '\0', sizeof(key_lines[edit_line]));
209 key_lines[edit_line][0] = ']';
215 Interactive line editing and console scrollback
219 Key_Console (int key, char ascii)
221 // LordHavoc: copied most of this from Q2 to improve keyboard handling
248 case K_KP_RIGHTARROW:
268 if ((toupper(key) == 'V' && keydown[K_CTRL]) || ((key == K_INS || key == K_KP_INS) && keydown[K_SHIFT]))
271 if ((cbd = Sys_GetClipboardData()) != 0)
274 strtok(cbd, "\n\r\b");
275 i = (int)strlen(cbd);
276 if (i + key_linepos >= MAX_INPUTLINE)
277 i= MAX_INPUTLINE - key_linepos;
281 strcat(key_lines[edit_line], cbd);
293 Cbuf_AddText ("clear\n");
298 if (key == K_ENTER || key == K_KP_ENTER)
300 Cbuf_AddText (key_lines[edit_line]+1); // skip the ]
302 Con_Printf("%s\n",key_lines[edit_line]);
303 // LordHavoc: redesigned edit_line/history_line
305 history_line = edit_line;
306 memmove(key_lines[0], key_lines[1], sizeof(key_lines[0]) * edit_line);
307 key_lines[edit_line][0] = ']';
308 key_lines[edit_line][1] = 0; // EvilTypeGuy: null terminate
310 // force an update, because the command may take some time
311 if (cls.state == ca_disconnected)
318 // Enhanced command completion
319 // by EvilTypeGuy eviltypeguy@qeradiant.com
320 // Thanks to Fett, Taniwha
321 Con_CompleteCommandLine();
325 // Advanced Console Editing by Radix radix@planetquake.com
326 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
328 // left arrow will just move left one without erasing, backspace will
329 // actually erase charcter
330 if (key == K_LEFTARROW || key == K_KP_LEFTARROW)
337 // delete char before cursor
338 if (key == K_BACKSPACE || (key == 'h' && keydown[K_CTRL]))
342 strcpy(key_lines[edit_line] + key_linepos - 1, key_lines[edit_line] + key_linepos);
348 // delete char on cursor
349 if (key == K_DEL || key == K_KP_DEL)
351 if (key_linepos < (int)strlen(key_lines[edit_line]))
352 strcpy(key_lines[edit_line] + key_linepos, key_lines[edit_line] + key_linepos + 1);
357 // if we're at the end, get one character from previous line,
358 // otherwise just go right one
359 if (key == K_RIGHTARROW || key == K_KP_RIGHTARROW)
361 if (key_linepos < (int)strlen(key_lines[edit_line]))
367 if (key == K_INS || key == K_KP_INS) // toggle insert mode
373 // End Advanced Console Editing
375 if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL]))
377 if (history_line > 0 && key_lines[history_line-1][1])
380 strcpy(key_lines[edit_line], key_lines[history_line]);
381 key_linepos = (int)strlen(key_lines[edit_line]);
386 if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL]))
389 if (history_line >= edit_line)
391 history_line = edit_line;
392 key_lines[edit_line][0] = ']';
393 key_lines[edit_line][1] = 0;
398 strcpy(key_lines[edit_line], key_lines[history_line]);
399 key_linepos = (int)strlen(key_lines[edit_line]);
404 if (key == K_PGUP || key == K_KP_PGUP || key == K_MWHEELUP)
406 con_backscroll += ((int) vid_conheight.integer >> 5);
407 if (con_backscroll > con_totallines - (vid_conheight.integer>>3) - 1)
408 con_backscroll = con_totallines - (vid_conheight.integer>>3) - 1;
412 if (key == K_PGDN || key == K_KP_PGDN || key == K_MWHEELDOWN)
414 con_backscroll -= ((int) vid_conheight.integer >> 5);
415 if (con_backscroll < 0)
420 if (key == K_HOME || key == K_KP_HOME)
422 con_backscroll = con_totallines - (vid_conheight.integer>>3) - 1;
426 if (key == K_END || key == K_KP_END)
436 if (key_linepos < MAX_INPUTLINE-1)
439 len = (int)strlen(&key_lines[edit_line][key_linepos]);
440 // check insert mode, or always insert if at end of line
441 if (key_insert || len == 0)
443 // can't use strcpy to move string to right
445 memmove(&key_lines[edit_line][key_linepos + 1], &key_lines[edit_line][key_linepos], len);
447 key_lines[edit_line][key_linepos] = ascii;
452 //============================================================================
455 char chat_buffer[MAX_INPUTLINE];
456 unsigned int chat_bufferlen = 0;
459 Key_Message (int key, char ascii)
464 Cmd_ForwardStringToServer(va("%s %s", chat_team ? "say_team" : "say ", chat_buffer));
472 if (key == K_ESCAPE) {
479 if (key == K_BACKSPACE) {
480 if (chat_bufferlen) {
482 chat_buffer[chat_bufferlen] = 0;
487 if (chat_bufferlen == sizeof (chat_buffer) - 1)
491 return; // non printable
493 chat_buffer[chat_bufferlen++] = ascii;
494 chat_buffer[chat_bufferlen] = 0;
497 //============================================================================
502 Returns a key number to be used to index keybindings[] by looking at
503 the given string. Single ascii characters return themselves, while
504 the K_* names are matched up.
508 Key_StringToKeynum (const char *str)
515 return tolower(str[0]);
517 for (kn = keynames; kn->name; kn++) {
518 if (!strcasecmp (str, kn->name))
526 Returns a string (either a single ascii char, or a K_* name) for the
528 FIXME: handle quote special (general escape sequence?)
532 Key_KeynumToString (int keynum)
535 static char tinystr[2];
538 return "<KEY NOT FOUND>";
539 if (keynum > 32 && keynum < 127) { // printable ascii
545 for (kn = keynames; kn->name; kn++)
546 if (keynum == kn->keynum)
549 return "<UNKNOWN KEYNUM>";
554 Key_SetBinding (int keynum, int bindmap, const char *binding)
563 if (keybindings[bindmap][keynum]) {
564 Z_Free (keybindings[bindmap][keynum]);
565 keybindings[bindmap][keynum] = NULL;
567 // allocate memory for new binding
568 l = strlen (binding);
569 new = Z_Malloc (l + 1);
570 strcpy (new, binding);
572 keybindings[bindmap][keynum] = new;
576 Key_In_Unbind_f (void)
580 if (Cmd_Argc () != 3) {
581 Con_Print("in_unbind <bindmap> <key> : remove commands from a key\n");
585 m = strtol(Cmd_Argv (1), NULL, 0);
586 if ((m < 0) || (m >= 8)) {
587 Con_Printf("%d isn't a valid bindmap\n", m);
591 b = Key_StringToKeynum (Cmd_Argv (2));
593 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
597 Key_SetBinding (b, m, "");
608 if (c != 3 && c != 4) {
609 Con_Print("in_bind <bindmap> <key> [command] : attach a command to a key\n");
613 m = strtol(Cmd_Argv (1), NULL, 0);
614 if ((m < 0) || (m >= 8)) {
615 Con_Printf("%d isn't a valid bindmap\n", m);
619 b = Key_StringToKeynum (Cmd_Argv (2));
621 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
626 if (keybindings[m][b])
627 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (2), keybindings[m][b]);
629 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (2));
632 // copy the rest of the command line
633 cmd[0] = 0; // start out with a null string
634 for (i = 3; i < c; i++) {
635 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
637 strlcat (cmd, " ", sizeof (cmd));
640 Key_SetBinding (b, m, cmd);
644 Key_In_Bindmap_f (void)
651 Con_Print("in_bindmap <bindmap> <fallback>: set current bindmap and fallback\n");
655 m1 = strtol(Cmd_Argv (1), NULL, 0);
656 if ((m1 < 0) || (m1 >= 8)) {
657 Con_Printf("%d isn't a valid bindmap\n", m1);
661 m2 = strtol(Cmd_Argv (2), NULL, 0);
662 if ((m2 < 0) || (m2 >= 8)) {
663 Con_Printf("%d isn't a valid bindmap\n", m2);
676 if (Cmd_Argc () != 2) {
677 Con_Print("unbind <key> : remove commands from a key\n");
681 b = Key_StringToKeynum (Cmd_Argv (1));
683 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
687 Key_SetBinding (b, 0, "");
691 Key_Unbindall_f (void)
695 for (j = 0; j < 8; j++)
696 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
697 if (keybindings[j][i])
698 Key_SetBinding (i, j, "");
710 if (c != 2 && c != 3) {
711 Con_Print("bind <key> [command] : attach a command to a key\n");
714 b = Key_StringToKeynum (Cmd_Argv (1));
716 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
721 if (keybindings[0][b])
722 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (1), keybindings[0][b]);
724 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (1));
727 // copy the rest of the command line
728 cmd[0] = 0; // start out with a null string
729 for (i = 2; i < c; i++) {
730 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
732 strlcat (cmd, " ", sizeof (cmd));
735 Key_SetBinding (b, 0, cmd);
740 Writes lines containing "bind key value"
744 Key_WriteBindings (qfile_t *f)
748 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
749 if (keybindings[0][i])
750 FS_Printf(f, "bind \"%s\" \"%s\"\n",
751 Key_KeynumToString (i), keybindings[0][i]);
752 for (j = 1; j < 8; j++)
753 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
754 if (keybindings[j][i])
755 FS_Printf(f, "in_bind %d \"%s\" \"%s\"\n",
756 j, Key_KeynumToString (i), keybindings[j][i]);
765 for (i = 0; i < 32; i++) {
766 key_lines[i][0] = ']';
772 // register our functions
774 Cmd_AddCommand ("in_bind", Key_In_Bind_f);
775 Cmd_AddCommand ("in_unbind", Key_In_Unbind_f);
776 Cmd_AddCommand ("in_bindmap", Key_In_Bindmap_f);
778 Cmd_AddCommand ("bind", Key_Bind_f);
779 Cmd_AddCommand ("unbind", Key_Unbind_f);
780 Cmd_AddCommand ("unbindall", Key_Unbindall_f);
786 Called by the system between frames for both key up and key down events
787 Should NOT be called during an interrupt!
791 Key_Event (int key, char ascii, qboolean down)
796 bind = keybindings[key_bmap][key];
798 bind = keybindings[key_bmap2][key];
802 // clear repeat count now that the key is released
804 // key up events only generate commands if the game key binding is a button
805 // command (leading + sign). These will occur even in console mode, to
806 // keep the character from continuing an action started before a console
807 // switch. Button commands include the kenum as a parameter, so multiple
808 // downs can be matched with ups
809 if (bind && bind[0] == '+')
810 Cbuf_AddText(va("-%s %i\n", bind + 1, key));
814 // from here on we know this is a down event
816 // increment key repeat count each time a down is received so that things
817 // which want to ignore key repeat can ignore it
818 keydown[key] = min(keydown[key] + 1, 2);
820 // key_consoleactive is a flag not a key_dest because the console is a
821 // high priority overlay ontop of the normal screen (designed as a safety
822 // feature so that developers and users can rescue themselves from a bad
825 // this also means that toggling the console on/off does not lose the old
828 // specially handle escape (togglemenu) and shift-escape (toggleconsole)
829 // engine bindings, these are not handled as normal binds so that the user
830 // can recover from a completely empty bindmap
833 // ignore key repeats on escape
834 if (keydown[key] > 1)
836 // escape does these things:
837 // key_consoleactive - close console
838 // key_message - abort messagemode
839 // key_menu - go to parent menu (or key_game)
840 // key_game - open menu
841 // in all modes shift-escape toggles console
842 if ((key_consoleactive & KEY_CONSOLEACTIVE_USER) || keydown[K_SHIFT])
844 Con_ToggleConsole_f ();
850 Key_Message (key, ascii);
853 MR_Keydown (key, ascii);
859 if(UI_Callback_IsSlotUsed(key_dest - 3))
860 UI_Callback_KeyDown (key, ascii);
862 Con_Printf ("Key_Event: Bad key_dest");
867 // send function keydowns to interpreter no matter what mode is
868 if (key >= K_F1 && key <= K_F12)
870 // ignore key repeats on F1-F12 binds
871 if (keydown[key] > 1)
875 // button commands add keynum as a parm
877 Cbuf_AddText (va("%s %i\n", bind, key));
888 // ignore binds (other than the above escape/F1-F12 keys) while in console
889 if (key_consoleactive)
891 // respond to toggleconsole binds while in console unless the pressed key
892 // happens to be the color prefix character (such as on German keyboards)
893 if (key_consoleactive && (strncmp(bind, "toggleconsole", strlen("toggleconsole")) || ascii == STRING_COLOR_TAG))
896 Key_Console (key, ascii);
900 // anything else is a key press into the game, chat line, or menu
904 Key_Message (key, ascii);
907 MR_Keydown (key, ascii);
910 // ignore key repeats on binds
911 if (bind && keydown[key] == 1)
913 // button commands add keynum as a parm
915 Cbuf_AddText (va("%s %i\n", bind, key));
924 if(UI_Callback_IsSlotUsed(key_dest - 3))
925 UI_Callback_KeyDown (key, ascii);
927 Con_Printf ("Key_Event: Bad key_dest");
937 Key_ClearStates (void)
939 memset(keydown, 0, sizeof(keydown));