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
330 // left arrow will just move left one without erasing, backspace will
331 // actually erase charcter
332 if (key == K_LEFTARROW || key == K_KP_LEFTARROW)
339 // delete char before cursor
340 if (key == K_BACKSPACE || (key == 'h' && keydown[K_CTRL]))
344 strcpy(key_lines[edit_line] + key_linepos - 1, key_lines[edit_line] + key_linepos);
350 // delete char on cursor
351 if (key == K_DEL || key == K_KP_DEL)
353 if (key_linepos < (int)strlen(key_lines[edit_line]))
354 strcpy(key_lines[edit_line] + key_linepos, key_lines[edit_line] + key_linepos + 1);
359 // if we're at the end, get one character from previous line,
360 // otherwise just go right one
361 if (key == K_RIGHTARROW || key == K_KP_RIGHTARROW)
363 if (key_linepos < (int)strlen(key_lines[edit_line]))
369 if (key == K_INS || key == K_KP_INS) // toggle insert mode
375 // End Advanced Console Editing
377 if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL]))
379 if (history_line > 0 && key_lines[history_line-1][1])
382 strcpy(key_lines[edit_line], key_lines[history_line]);
383 key_linepos = (int)strlen(key_lines[edit_line]);
388 if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL]))
391 if (history_line >= edit_line)
393 history_line = edit_line;
394 key_lines[edit_line][0] = ']';
395 key_lines[edit_line][1] = 0;
400 strcpy(key_lines[edit_line], key_lines[history_line]);
401 key_linepos = (int)strlen(key_lines[edit_line]);
406 if (key == K_PGUP || key == K_KP_PGUP || key == K_MWHEELUP)
408 con_backscroll += ((int) vid_conheight.integer >> 5);
409 if (con_backscroll > con_totallines - (vid_conheight.integer>>3) - 1)
410 con_backscroll = con_totallines - (vid_conheight.integer>>3) - 1;
414 if (key == K_PGDN || key == K_KP_PGDN || key == K_MWHEELDOWN)
416 con_backscroll -= ((int) vid_conheight.integer >> 5);
417 if (con_backscroll < 0)
422 if (key == K_HOME || key == K_KP_HOME)
424 con_backscroll = con_totallines - (vid_conheight.integer>>3) - 1;
428 if (key == K_END || key == K_KP_END)
438 if (key_linepos < MAX_INPUTLINE-1)
441 len = (int)strlen(&key_lines[edit_line][key_linepos]);
442 // check insert mode, or always insert if at end of line
443 if (key_insert || len == 0)
445 // can't use strcpy to move string to right
447 memmove(&key_lines[edit_line][key_linepos + 1], &key_lines[edit_line][key_linepos], len);
449 key_lines[edit_line][key_linepos] = ascii;
454 //============================================================================
457 char chat_buffer[MAX_INPUTLINE];
458 unsigned int chat_bufferlen = 0;
461 Key_Message (int key, char ascii)
466 Cmd_ForwardStringToServer(va("%s %s", chat_team ? "say_team" : "say ", chat_buffer));
474 if (key == K_ESCAPE) {
481 if (key == K_BACKSPACE) {
482 if (chat_bufferlen) {
484 chat_buffer[chat_bufferlen] = 0;
489 if (chat_bufferlen == sizeof (chat_buffer) - 1)
493 return; // non printable
495 chat_buffer[chat_bufferlen++] = ascii;
496 chat_buffer[chat_bufferlen] = 0;
499 //============================================================================
504 Returns a key number to be used to index keybindings[] by looking at
505 the given string. Single ascii characters return themselves, while
506 the K_* names are matched up.
510 Key_StringToKeynum (const char *str)
517 return tolower(str[0]);
519 for (kn = keynames; kn->name; kn++) {
520 if (!strcasecmp (str, kn->name))
528 Returns a string (either a single ascii char, or a K_* name) for the
530 FIXME: handle quote special (general escape sequence?)
534 Key_KeynumToString (int keynum)
537 static char tinystr[2];
540 return "<KEY NOT FOUND>";
541 if (keynum > 32 && keynum < 127) { // printable ascii
547 for (kn = keynames; kn->name; kn++)
548 if (keynum == kn->keynum)
551 return "<UNKNOWN KEYNUM>";
556 Key_SetBinding (int keynum, int bindmap, const char *binding)
565 if (keybindings[bindmap][keynum]) {
566 Z_Free (keybindings[bindmap][keynum]);
567 keybindings[bindmap][keynum] = NULL;
569 // allocate memory for new binding
570 l = strlen (binding);
571 newbinding = (char *)Z_Malloc (l + 1);
572 strcpy (newbinding, binding);
574 keybindings[bindmap][keynum] = newbinding;
578 Key_In_Unbind_f (void)
582 if (Cmd_Argc () != 3) {
583 Con_Print("in_unbind <bindmap> <key> : remove commands from a key\n");
587 m = strtol(Cmd_Argv (1), NULL, 0);
588 if ((m < 0) || (m >= 8)) {
589 Con_Printf("%d isn't a valid bindmap\n", m);
593 b = Key_StringToKeynum (Cmd_Argv (2));
595 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
599 Key_SetBinding (b, m, "");
606 char cmd[MAX_INPUTLINE];
610 if (c != 3 && c != 4) {
611 Con_Print("in_bind <bindmap> <key> [command] : attach a command to a key\n");
615 m = strtol(Cmd_Argv (1), NULL, 0);
616 if ((m < 0) || (m >= 8)) {
617 Con_Printf("%d isn't a valid bindmap\n", m);
621 b = Key_StringToKeynum (Cmd_Argv (2));
623 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
628 if (keybindings[m][b])
629 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (2), keybindings[m][b]);
631 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (2));
634 // copy the rest of the command line
635 cmd[0] = 0; // start out with a null string
636 for (i = 3; i < c; i++) {
637 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
639 strlcat (cmd, " ", sizeof (cmd));
642 Key_SetBinding (b, m, cmd);
646 Key_In_Bindmap_f (void)
653 Con_Print("in_bindmap <bindmap> <fallback>: set current bindmap and fallback\n");
657 m1 = strtol(Cmd_Argv (1), NULL, 0);
658 if ((m1 < 0) || (m1 >= 8)) {
659 Con_Printf("%d isn't a valid bindmap\n", m1);
663 m2 = strtol(Cmd_Argv (2), NULL, 0);
664 if ((m2 < 0) || (m2 >= 8)) {
665 Con_Printf("%d isn't a valid bindmap\n", m2);
678 if (Cmd_Argc () != 2) {
679 Con_Print("unbind <key> : remove commands from a key\n");
683 b = Key_StringToKeynum (Cmd_Argv (1));
685 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
689 Key_SetBinding (b, 0, "");
693 Key_Unbindall_f (void)
697 for (j = 0; j < 8; j++)
698 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
699 if (keybindings[j][i])
700 Key_SetBinding (i, j, "");
708 char cmd[MAX_INPUTLINE];
712 if (c != 2 && c != 3) {
713 Con_Print("bind <key> [command] : attach a command to a key\n");
716 b = Key_StringToKeynum (Cmd_Argv (1));
718 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
723 if (keybindings[0][b])
724 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (1), keybindings[0][b]);
726 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (1));
729 // copy the rest of the command line
730 cmd[0] = 0; // start out with a null string
731 for (i = 2; i < c; i++) {
732 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
734 strlcat (cmd, " ", sizeof (cmd));
737 Key_SetBinding (b, 0, cmd);
742 Writes lines containing "bind key value"
746 Key_WriteBindings (qfile_t *f)
750 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
751 if (keybindings[0][i])
752 FS_Printf(f, "bind \"%s\" \"%s\"\n",
753 Key_KeynumToString (i), keybindings[0][i]);
754 for (j = 1; j < 8; j++)
755 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
756 if (keybindings[j][i])
757 FS_Printf(f, "in_bind %d \"%s\" \"%s\"\n",
758 j, Key_KeynumToString (i), keybindings[j][i]);
767 for (i = 0; i < 32; i++) {
768 key_lines[i][0] = ']';
774 // register our functions
776 Cmd_AddCommand ("in_bind", Key_In_Bind_f);
777 Cmd_AddCommand ("in_unbind", Key_In_Unbind_f);
778 Cmd_AddCommand ("in_bindmap", Key_In_Bindmap_f);
780 Cmd_AddCommand ("bind", Key_Bind_f);
781 Cmd_AddCommand ("unbind", Key_Unbind_f);
782 Cmd_AddCommand ("unbindall", Key_Unbindall_f);
788 Called by the system between frames for both key up and key down events
789 Should NOT be called during an interrupt!
793 Key_Event (int key, char ascii, qboolean down)
798 bind = keybindings[key_bmap][key];
800 bind = keybindings[key_bmap2][key];
804 // clear repeat count now that the key is released
806 // key up events only generate commands if the game key binding is a button
807 // command (leading + sign). These will occur even in console mode, to
808 // keep the character from continuing an action started before a console
809 // switch. Button commands include the kenum as a parameter, so multiple
810 // downs can be matched with ups
811 if (bind && bind[0] == '+')
812 Cbuf_AddText(va("-%s %i\n", bind + 1, key));
816 // from here on we know this is a down event
818 // increment key repeat count each time a down is received so that things
819 // which want to ignore key repeat can ignore it
820 keydown[key] = min(keydown[key] + 1, 2);
822 // key_consoleactive is a flag not a key_dest because the console is a
823 // high priority overlay ontop of the normal screen (designed as a safety
824 // feature so that developers and users can rescue themselves from a bad
827 // this also means that toggling the console on/off does not lose the old
830 // specially handle escape (togglemenu) and shift-escape (toggleconsole)
831 // engine bindings, these are not handled as normal binds so that the user
832 // can recover from a completely empty bindmap
835 // ignore key repeats on escape
836 if (keydown[key] > 1)
838 // escape does these things:
839 // key_consoleactive - close console
840 // key_message - abort messagemode
841 // key_menu - go to parent menu (or key_game)
842 // key_game - open menu
843 // in all modes shift-escape toggles console
844 if ((key_consoleactive & KEY_CONSOLEACTIVE_USER) || keydown[K_SHIFT])
846 Con_ToggleConsole_f ();
852 Key_Message (key, ascii);
855 MR_Keydown (key, ascii);
861 if(UI_Callback_IsSlotUsed(key_dest - 3))
862 UI_Callback_KeyDown (key, ascii);
864 Con_Printf ("Key_Event: Bad key_dest\n");
869 // send function keydowns to interpreter no matter what mode is
870 if (key >= K_F1 && key <= K_F12)
872 // ignore key repeats on F1-F12 binds
873 if (keydown[key] > 1)
877 // button commands add keynum as a parm
879 Cbuf_AddText (va("%s %i\n", bind, key));
890 // ignore binds (other than the above escape/F1-F12 keys) while in console
891 if (key_consoleactive)
893 // respond to toggleconsole binds while in console unless the pressed key
894 // happens to be the color prefix character (such as on German keyboards)
895 if (key_consoleactive && (strncmp(bind, "toggleconsole", strlen("toggleconsole")) || ascii == STRING_COLOR_TAG))
898 Key_Console (key, ascii);
902 // anything else is a key press into the game, chat line, or menu
906 Key_Message (key, ascii);
909 MR_Keydown (key, ascii);
912 // ignore key repeats on binds
913 if (bind && keydown[key] == 1)
915 // button commands add keynum as a parm
917 Cbuf_AddText (va("%s %i\n", bind, key));
926 if(UI_Callback_IsSlotUsed(key_dest - 3))
927 UI_Callback_KeyDown (key, ascii);
929 Con_Printf ("Key_Event: Bad key_dest\n");
939 Key_ClearStates (void)
941 memset(keydown, 0, sizeof(keydown));