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
28 cvar_t con_closeontoggleconsole = {CVAR_SAVE, "con_closeontoggleconsole","1", "allows toggleconsole binds to close the console as well"};
31 key up events are sent even if in console mode
34 char key_line[MAX_INPUTLINE];
36 qboolean key_insert = true; // insert key toggle (for editing)
38 int key_consoleactive;
39 char *keybindings[MAX_BINDMAPS][MAX_KEYS];
41 char history_savedline[MAX_INPUTLINE];
43 #define HIST_TEXTSIZE 262144
44 #define HIST_MAXLINES 4096
46 extern cvar_t con_textsize;
47 static qboolean uim_in_console = false;
50 static void Key_History_Init(void)
53 ConBuffer_Init(&history, HIST_TEXTSIZE, HIST_MAXLINES, zonemempool);
55 historyfile = FS_OpenRealFile("darkplaces_history.txt", "rb", false); // rb to handle unix line endings on windows too
58 char buf[MAX_INPUTLINE];
65 c = FS_Getc(historyfile);
66 if(c < 0 || c == 0 || c == '\r' || c == '\n')
71 ConBuffer_AddLine(&history, buf, bufpos, 0);
79 if(bufpos < MAX_INPUTLINE - 1)
84 FS_Close(historyfile);
90 static void Key_History_Shutdown(void)
92 // TODO write history to a file
94 qfile_t *historyfile = FS_OpenRealFile("darkplaces_history.txt", "w", false);
98 for(i = 0; i < CONBUFFER_LINES_COUNT(&history); ++i)
99 FS_Printf(historyfile, "%s\n", ConBuffer_GetLine(&history, i));
100 FS_Close(historyfile);
103 ConBuffer_Shutdown(&history);
106 static void Key_History_Push(void)
108 if(key_line[1]) // empty?
109 if(strcmp(key_line, "]quit")) // putting these into the history just sucks
110 if(strncmp(key_line, "]quit ", 6)) // putting these into the history just sucks
111 ConBuffer_AddLine(&history, key_line + 1, strlen(key_line) - 1, 0);
112 Con_Printf("%s\n", key_line); // don't mark empty lines as history
116 static void Key_History_Up(void)
118 if(history_line == -1) // editing the "new" line
119 strlcpy(history_savedline, key_line + 1, sizeof(history_savedline));
121 if(history_line == -1)
123 history_line = CONBUFFER_LINES_COUNT(&history) - 1;
124 if(history_line != -1)
126 strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
127 key_linepos = strlen(key_line);
130 else if(history_line > 0)
132 --history_line; // this also does -1 -> 0, so it is good
133 strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
134 key_linepos = strlen(key_line);
138 static void Key_History_Down(void)
140 if(history_line == -1) // editing the "new" line
143 if(history_line < CONBUFFER_LINES_COUNT(&history) - 1)
146 strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
151 strlcpy(key_line + 1, history_savedline, sizeof(key_line) - 1);
154 key_linepos = strlen(key_line);
157 static int key_bmap, key_bmap2;
158 static unsigned char keydown[MAX_KEYS]; // 0 = up, 1 = down, 2 = repeating
160 typedef struct keyname_s
167 static const keyname_t keynames[] = {
170 {"ESCAPE", K_ESCAPE},
173 // spacer so it lines up with keys.h
175 {"BACKSPACE", K_BACKSPACE},
176 {"UPARROW", K_UPARROW},
177 {"DOWNARROW", K_DOWNARROW},
178 {"LEFTARROW", K_LEFTARROW},
179 {"RIGHTARROW", K_RIGHTARROW},
207 {"NUMLOCK", K_NUMLOCK},
208 {"CAPSLOCK", K_CAPSLOCK},
209 {"SCROLLOCK", K_SCROLLOCK},
211 {"KP_INS", K_KP_INS },
213 {"KP_END", K_KP_END },
215 {"KP_DOWNARROW", K_KP_DOWNARROW },
217 {"KP_PGDN", K_KP_PGDN },
219 {"KP_LEFTARROW", K_KP_LEFTARROW },
222 {"KP_RIGHTARROW", K_KP_RIGHTARROW },
224 {"KP_HOME", K_KP_HOME },
226 {"KP_UPARROW", K_KP_UPARROW },
228 {"KP_PGUP", K_KP_PGUP },
230 {"KP_DEL", K_KP_DEL },
231 {"KP_PERIOD", K_KP_PERIOD},
232 {"KP_SLASH", K_KP_SLASH },
233 {"KP_DIVIDE", K_KP_DIVIDE},
234 {"KP_MULTIPLY", K_KP_MULTIPLY},
235 {"KP_MINUS", K_KP_MINUS},
236 {"KP_PLUS", K_KP_PLUS},
237 {"KP_ENTER", K_KP_ENTER},
238 {"KP_EQUALS", K_KP_EQUALS},
242 {"MOUSE1", K_MOUSE1},
244 {"MOUSE2", K_MOUSE2},
245 {"MOUSE3", K_MOUSE3},
246 {"MWHEELUP", K_MWHEELUP},
247 {"MWHEELDOWN", K_MWHEELDOWN},
248 {"MOUSE4", K_MOUSE4},
249 {"MOUSE5", K_MOUSE5},
250 {"MOUSE6", K_MOUSE6},
251 {"MOUSE7", K_MOUSE7},
252 {"MOUSE8", K_MOUSE8},
253 {"MOUSE9", K_MOUSE9},
254 {"MOUSE10", K_MOUSE10},
255 {"MOUSE11", K_MOUSE11},
256 {"MOUSE12", K_MOUSE12},
257 {"MOUSE13", K_MOUSE13},
258 {"MOUSE14", K_MOUSE14},
259 {"MOUSE15", K_MOUSE15},
260 {"MOUSE16", K_MOUSE16},
320 {"SEMICOLON", ';'}, // because a raw semicolon separates commands
324 {"APOSTROPHE", '\''},
325 {"BACKSLASH", '\\'}, // because a raw backslash is used for special characters
331 ==============================================================================
333 LINE TYPING INTO THE CONSOLE
335 ==============================================================================
339 Key_ClearEditLine (int edit_line)
341 memset (key_line, '\0', sizeof(key_line));
348 Interactive line editing and console scrollback
352 static void Key_Console_SetCursor(size_t pos)
355 //Con_Printf("Position: %lu\n", pos);
359 Key_Console (int key, int unicode)
364 if (UIM_Key(key, unicode))
369 // LordHavoc: copied most of this from Q2 to improve keyboard handling
396 case K_KP_RIGHTARROW:
416 if ((key == 'v' && keydown[K_CTRL]) || ((key == K_INS || key == K_KP_INS) && keydown[K_SHIFT]))
419 if ((cbd = Sys_GetClipboardData()) != 0)
426 if (*p == '\r' && *(p+1) == '\n')
431 else if (*p == '\n' || *p == '\r' || *p == '\b')
436 strtok(cbd, "\n\r\b");
438 i = (int)strlen(cbd);
439 if (i + key_linepos >= MAX_INPUTLINE)
440 i= MAX_INPUTLINE - key_linepos - 1;
443 // terencehill: insert the clipboard text between the characters of the line
445 char *temp = (char *) Z_Malloc(MAX_INPUTLINE);
448 if ( key_linepos < (int)strlen(key_line) )
449 strlcpy(temp, key_line + key_linepos, (int)strlen(key_line) - key_linepos +1);
450 key_line[key_linepos] = 0;
451 strlcat(key_line, cbd, sizeof(key_line));
453 strlcat(key_line, temp, sizeof(key_line));
457 // blub: I'm changing this to use memmove() like the rest of the code does.
459 memmove(key_line + key_linepos + i, key_line + key_linepos, sizeof(key_line) - key_linepos - i);
460 memcpy(key_line + key_linepos, cbd, i);
462 //UIM_SetCursor(key_linepos);
469 if (key == 'l' && keydown[K_CTRL])
471 Cbuf_AddText ("clear\n");
475 if (key == 'u' && keydown[K_CTRL]) // like vi/readline ^u: delete currently edited line
481 //UIM_SetCursor(key_linepos);
485 if (key == 'q' && keydown[K_CTRL]) // like zsh ^q: push line to history, don't execute, and clear
492 //UIM_SetCursor(key_linepos);
496 if (key == K_ENTER || key == K_KP_ENTER)
498 Cbuf_AddText (key_line+1); // skip the ]
502 key_line[1] = 0; // EvilTypeGuy: null terminate
504 //UIM_CancelBuffer();
505 //uim_in_console = false;
506 // force an update, because the command may take some time
507 if (cls.state == ca_disconnected)
514 if(keydown[K_CTRL]) // append to the cvar its value
516 int cvar_len, cvar_str_len, chars_to_move;
518 char cvar[MAX_INPUTLINE];
519 const char *cvar_str;
521 // go to the start of the variable
524 k = key_line[key_linepos];
525 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
530 // save the variable name in cvar
531 for(cvar_len=0; (k = key_line[key_linepos + cvar_len]) != 0; cvar_len++)
533 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
539 //UIM_SetCursor(key_linepos);
544 // go to the end of the cvar
545 key_linepos += cvar_len;
547 // save the content of the variable in cvar_str
548 cvar_str = Cvar_VariableString(cvar);
549 cvar_str_len = strlen(cvar_str);
552 //UIM_SetCursor(key_linepos);
556 // insert space and cvar_str in key_line
557 chars_to_move = strlen(&key_line[key_linepos]);
558 if (key_linepos + 1 + cvar_str_len + chars_to_move < MAX_INPUTLINE)
561 memmove(&key_line[key_linepos + 1 + cvar_str_len], &key_line[key_linepos], chars_to_move);
562 key_line[key_linepos++] = ' ';
563 memcpy(&key_line[key_linepos], cvar_str, cvar_str_len);
564 key_linepos += cvar_str_len;
565 key_line[key_linepos + chars_to_move] = 0;
568 Con_Printf("Couldn't append cvar value, edit line too long.\n");
569 //UIM_SetCursor(key_linepos);
572 // Enhanced command completion
573 // by EvilTypeGuy eviltypeguy@qeradiant.com
574 // Thanks to Fett, Taniwha
575 Con_CompleteCommandLine();
579 // Advanced Console Editing by Radix radix@planetquake.com
580 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
582 // Enhanced by terencehill
584 // move cursor to the previous character
585 if (key == K_LEFTARROW || key == K_KP_LEFTARROW)
589 if(keydown[K_CTRL]) // move cursor to the previous word
595 if(pos) // skip all "; ' after the word
599 if (!(k == '\"' || k == ';' || k == ' ' || k == '\''))
607 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
610 key_linepos = pos + 1;
612 else if(keydown[K_SHIFT]) // move cursor to the previous character ignoring colors
616 pos = u8_prevbyte(key_line, key_linepos);
618 if(pos-1 > 0 && key_line[pos-1] == STRING_COLOR_TAG && isdigit(key_line[pos]))
620 else if(pos-4 > 0 && key_line[pos-4] == STRING_COLOR_TAG && key_line[pos-3] == STRING_COLOR_RGB_TAG_CHAR
621 && isxdigit(key_line[pos-2]) && isxdigit(key_line[pos-1]) && isxdigit(key_line[pos]))
625 if(pos-1 > 0 && key_line[pos-1] == STRING_COLOR_TAG && key_line[pos] == STRING_COLOR_TAG) // consider ^^ as a character
630 // we need to move to the beginning of the character when in a wide character:
631 u8_charidx(key_line, pos + 1, &inchar);
632 key_linepos = pos + 1 - inchar;
636 key_linepos = u8_prevbyte(key_line, key_linepos);
638 //UIM_SetCursor(key_linepos);
642 // delete char before cursor
643 if (key == K_BACKSPACE || (key == 'h' && keydown[K_CTRL]))
647 int newpos = u8_prevbyte(key_line, key_linepos);
648 strlcpy(key_line + newpos, key_line + key_linepos, sizeof(key_line) + 1 - key_linepos);
649 key_linepos = newpos;
651 //UIM_SetCursor(key_linepos);
655 // delete char on cursor
656 if (key == K_DEL || key == K_KP_DEL)
659 linelen = strlen(key_line);
660 if (key_linepos < (int)linelen)
661 memmove(key_line + key_linepos, key_line + key_linepos + u8_bytelen(key_line + key_linepos, 1), linelen - key_linepos);
666 // move cursor to the next character
667 if (key == K_RIGHTARROW || key == K_KP_RIGHTARROW)
669 if (key_linepos >= (int)strlen(key_line))
671 if(keydown[K_CTRL]) // move cursor to the next word
675 len = (int)strlen(key_line);
681 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
685 if (pos < len) // skip all "; ' after the word
689 if (!(k == '\"' || k == ';' || k == ' ' || k == '\''))
694 else if(keydown[K_SHIFT]) // move cursor to the next character ignoring colors
697 len = (int)strlen(key_line);
700 // go beyond all initial consecutive color tags, if any
702 while (key_line[pos] == STRING_COLOR_TAG)
704 if(isdigit(key_line[pos+1]))
706 else if(key_line[pos+1] == STRING_COLOR_RGB_TAG_CHAR && isxdigit(key_line[pos+2]) && isxdigit(key_line[pos+3]) && isxdigit(key_line[pos+4]))
713 if (key_line[pos] == STRING_COLOR_TAG && key_line[pos+1] == STRING_COLOR_TAG) // consider ^^ as a character
715 pos += u8_bytelen(key_line + pos, 1);
717 // now go beyond all next consecutive color tags, if any
719 while (key_line[pos] == STRING_COLOR_TAG)
721 if(isdigit(key_line[pos+1]))
723 else if(key_line[pos+1] == STRING_COLOR_RGB_TAG_CHAR && isxdigit(key_line[pos+2]) && isxdigit(key_line[pos+3]) && isxdigit(key_line[pos+4]))
731 key_linepos += u8_bytelen(key_line + key_linepos, 1);
732 //UIM_SetCursor(key_linepos);
736 if (key == K_INS || key == K_KP_INS) // toggle insert mode
742 // End Advanced Console Editing
744 if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL]))
747 //UIM_SetCursor(key_linepos);
751 if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL]))
754 //UIM_SetCursor(key_linepos);
757 // ~1.0795 = 82/76 using con_textsize 64 76 is height of the char, 6 is the distance between 2 lines
758 if (key == K_PGUP || key == K_KP_PGUP)
762 con_backscroll += (int)floor((vid_conheight.integer >> 2) / con_textsize.integer)-1;
765 con_backscroll += (int)floor((vid_conheight.integer >> 1) / con_textsize.integer)-3;
769 if (key == K_PGDN || key == K_KP_PGDN)
773 con_backscroll -= (int)floor((vid_conheight.integer >> 2) / con_textsize.integer)-1;
776 con_backscroll -= (int)floor((vid_conheight.integer >> 1) / con_textsize.integer)-3;
780 if (key == K_MWHEELUP)
784 else if(keydown[K_SHIFT])
785 con_backscroll += (int)floor((vid_conheight.integer >> 2) / con_textsize.integer)-1;
791 if (key == K_MWHEELDOWN)
795 else if(keydown[K_SHIFT])
796 con_backscroll -= (int)floor((vid_conheight.integer >> 2) / con_textsize.integer)-1;
805 if (key == '+' || key == K_KP_PLUS)
807 if (con_textsize.integer < 128)
808 Cvar_SetValueQuick(&con_textsize, con_textsize.integer + 1);
812 if (key == '-' || key == K_KP_MINUS)
814 if (con_textsize.integer > 1)
815 Cvar_SetValueQuick(&con_textsize, con_textsize.integer - 1);
819 if (key == '0' || key == K_KP_INS)
821 Cvar_SetValueQuick(&con_textsize, atoi(Cvar_VariableDefString("con_textsize")));
826 if (key == K_HOME || key == K_KP_HOME)
829 con_backscroll = INT_MAX;
832 //UIM_SetCursor(key_linepos);
836 if (key == K_END || key == K_KP_END)
841 key_linepos = (int)strlen(key_line);
842 //UIM_SetCursor(key_linepos);
850 if (key_linepos < MAX_INPUTLINE-1)
855 blen = u8_fromchar(unicode, buf, sizeof(buf));
858 len = (int)strlen(&key_line[key_linepos]);
859 // check insert mode, or always insert if at end of line
860 if (key_insert || len == 0)
862 // can't use strcpy to move string to right
864 //memmove(&key_line[key_linepos + u8_bytelen(key_line + key_linepos, 1)], &key_line[key_linepos], len);
865 memmove(&key_line[key_linepos + blen], &key_line[key_linepos], len);
867 memcpy(key_line + key_linepos, buf, blen);
869 //UIM_SetCursor(key_linepos);
870 //key_linepos += u8_fromchar(unicode, key_line + key_linepos, sizeof(key_line) - key_linepos - 1);
871 //key_line[key_linepos] = ascii;
876 //============================================================================
879 char chat_buffer[MAX_INPUTLINE];
880 unsigned int chat_bufferlen = 0;
882 extern int Nicks_CompleteChatLine(char *buffer, size_t size, unsigned int pos);
885 Key_Message (int key, int ascii)
889 if (UIM_Key(key, ascii))
892 if (key >= K_F1 && key <= K_F12)
895 if (key == K_ENTER || ascii == 10 || ascii == 13)
898 Cmd_ExecuteString(chat_buffer, src_command); // not Cbuf_AddText to allow semiclons in args; however, this allows no variables then. Use aliases!
900 Cmd_ForwardStringToServer(va("%s %s", chat_mode ? "say_team" : "say ", chat_buffer));
909 // TODO add support for arrow keys and simple editing
910 if (key == K_ESCAPE) {
918 if (key == K_BACKSPACE) {
919 if (chat_bufferlen) {
920 chat_bufferlen = u8_prevbyte(chat_buffer, chat_bufferlen);
921 UIM_SetCursor(chat_bufferlen);
922 chat_buffer[chat_bufferlen] = 0;
928 chat_bufferlen = Nicks_CompleteChatLine(chat_buffer, sizeof(chat_buffer), chat_bufferlen);
929 UIM_SetCursor(chat_bufferlen);
933 if (chat_bufferlen == sizeof (chat_buffer) - 1)
934 return true; // all full
937 return true; // non printable
939 if (keydown[K_CTRL] || keydown[K_ALT]) // no evil trickery, yeah?
942 chat_bufferlen += u8_fromchar(ascii, chat_buffer+chat_bufferlen, sizeof(chat_buffer) - chat_bufferlen - 1);
943 UIM_SetCursor(chat_bufferlen);
945 //chat_buffer[chat_bufferlen++] = ascii;
946 //chat_buffer[chat_bufferlen] = 0;
950 //============================================================================
955 Returns a key number to be used to index keybindings[] by looking at
956 the given string. Single ascii characters return themselves, while
957 the K_* names are matched up.
961 Key_StringToKeynum (const char *str)
968 return tolower(str[0]);
970 for (kn = keynames; kn->name; kn++) {
971 if (!strcasecmp (str, kn->name))
979 Returns a string (either a single ascii char, or a K_* name) for the
981 FIXME: handle quote special (general escape sequence?)
985 Key_KeynumToString (int keynum)
988 static char tinystr[2];
990 // -1 is an invalid code
992 return "<KEY NOT FOUND>";
994 // search overrides first, because some characters are special
995 for (kn = keynames; kn->name; kn++)
996 if (keynum == kn->keynum)
999 // if it is printable, output it as a single character
1000 if (keynum > 32 && keynum < 256)
1002 tinystr[0] = keynum;
1007 // if it is not overridden and not printable, we don't know what to do with it
1008 return "<UNKNOWN KEYNUM>";
1013 Key_SetBinding (int keynum, int bindmap, const char *binding)
1018 if (keynum == -1 || keynum >= MAX_KEYS)
1021 // free old bindings
1022 if (keybindings[bindmap][keynum]) {
1023 Z_Free (keybindings[bindmap][keynum]);
1024 keybindings[bindmap][keynum] = NULL;
1026 if(!binding[0]) // make "" binds be removed --blub
1028 // allocate memory for new binding
1029 l = strlen (binding);
1030 newbinding = (char *)Z_Malloc (l + 1);
1031 memcpy (newbinding, binding, l + 1);
1033 keybindings[bindmap][keynum] = newbinding;
1037 Key_In_Unbind_f (void)
1040 char *errchar = NULL;
1042 if (Cmd_Argc () != 3) {
1043 Con_Print("in_unbind <bindmap> <key> : remove commands from a key\n");
1047 m = strtol(Cmd_Argv (1), &errchar, 0);
1048 if ((m < 0) || (m >= 8) || (errchar && *errchar)) {
1049 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(1));
1053 b = Key_StringToKeynum (Cmd_Argv (2));
1055 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
1059 Key_SetBinding (b, m, "");
1063 Key_In_Bind_f (void)
1066 char cmd[MAX_INPUTLINE];
1067 char *errchar = NULL;
1071 if (c != 3 && c != 4) {
1072 Con_Print("in_bind <bindmap> <key> [command] : attach a command to a key\n");
1076 m = strtol(Cmd_Argv (1), &errchar, 0);
1077 if ((m < 0) || (m >= 8) || (errchar && *errchar)) {
1078 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(1));
1082 b = Key_StringToKeynum (Cmd_Argv (2));
1083 if (b == -1 || b >= MAX_KEYS) {
1084 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
1089 if (keybindings[m][b])
1090 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (2), keybindings[m][b]);
1092 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (2));
1095 // copy the rest of the command line
1096 cmd[0] = 0; // start out with a null string
1097 for (i = 3; i < c; i++) {
1098 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
1100 strlcat (cmd, " ", sizeof (cmd));
1103 Key_SetBinding (b, m, cmd);
1107 Key_In_Bindmap_f (void)
1110 char *errchar = NULL;
1115 Con_Print("in_bindmap <bindmap> <fallback>: set current bindmap and fallback\n");
1119 m1 = strtol(Cmd_Argv (1), &errchar, 0);
1120 if ((m1 < 0) || (m1 >= 8) || (errchar && *errchar)) {
1121 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(1));
1125 m2 = strtol(Cmd_Argv (2), &errchar, 0);
1126 if ((m2 < 0) || (m2 >= 8) || (errchar && *errchar)) {
1127 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(2));
1140 if (Cmd_Argc () != 2) {
1141 Con_Print("unbind <key> : remove commands from a key\n");
1145 b = Key_StringToKeynum (Cmd_Argv (1));
1147 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
1151 Key_SetBinding (b, 0, "");
1155 Key_Unbindall_f (void)
1159 for (j = 0; j < 8; j++)
1160 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
1161 if (keybindings[j][i])
1162 Key_SetBinding (i, j, "");
1166 Key_PrintBindList(int j)
1168 char bindbuf[MAX_INPUTLINE];
1172 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
1174 p = keybindings[j][i];
1177 Cmd_QuoteString(bindbuf, sizeof(bindbuf), p, "\"\\");
1179 Con_Printf("^2%s ^7= \"%s\"\n", Key_KeynumToString (i), bindbuf);
1181 Con_Printf("^3bindmap %d: ^2%s ^7= \"%s\"\n", j, Key_KeynumToString (i), bindbuf);
1187 Key_In_BindList_f (void)
1190 char *errchar = NULL;
1194 m = strtol(Cmd_Argv(1), &errchar, 0);
1195 if ((m < 0) || (m >= 8) || (errchar && *errchar)) {
1196 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(1));
1199 Key_PrintBindList(m);
1203 for (m = 0; m < MAX_BINDMAPS; m++)
1204 Key_PrintBindList(m);
1209 Key_BindList_f (void)
1211 Key_PrintBindList(0);
1218 char cmd[MAX_INPUTLINE];
1222 if (c != 2 && c != 3) {
1223 Con_Print("bind <key> [command] : attach a command to a key\n");
1226 b = Key_StringToKeynum (Cmd_Argv (1));
1227 if (b == -1 || b >= MAX_KEYS) {
1228 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
1233 if (keybindings[0][b])
1234 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (1), keybindings[0][b]);
1236 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (1));
1239 // copy the rest of the command line
1240 cmd[0] = 0; // start out with a null string
1241 for (i = 2; i < c; i++) {
1242 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
1244 strlcat (cmd, " ", sizeof (cmd));
1247 Key_SetBinding (b, 0, cmd);
1252 Writes lines containing "bind key value"
1256 Key_WriteBindings (qfile_t *f)
1259 char bindbuf[MAX_INPUTLINE];
1262 for (j = 0; j < MAX_BINDMAPS; j++)
1264 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
1266 p = keybindings[j][i];
1269 Cmd_QuoteString(bindbuf, sizeof(bindbuf), p, "\"\\");
1271 FS_Printf(f, "bind %s \"%s\"\n", Key_KeynumToString (i), bindbuf);
1273 FS_Printf(f, "in_bind %d %s \"%s\"\n", j, Key_KeynumToString (i), bindbuf);
1289 // register our functions
1291 Cmd_AddCommand ("in_bind", Key_In_Bind_f, "binds a command to the specified key in the selected bindmap");
1292 Cmd_AddCommand ("in_unbind", Key_In_Unbind_f, "removes command on the specified key in the selected bindmap");
1293 Cmd_AddCommand ("in_bindlist", Key_In_BindList_f, "bindlist: displays bound keys for all bindmaps, or the given bindmap");
1294 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");
1296 Cmd_AddCommand ("bind", Key_Bind_f, "binds a command to the specified key in bindmap 0");
1297 Cmd_AddCommand ("unbind", Key_Unbind_f, "removes a command on the specified key in bindmap 0");
1298 Cmd_AddCommand ("bindlist", Key_BindList_f, "bindlist: displays bound keys for bindmap 0 bindmaps");
1299 Cmd_AddCommand ("unbindall", Key_Unbindall_f, "removes all commands from all keys in all bindmaps (leaving only shift-escape and escape)");
1301 Cvar_RegisterVariable (&con_closeontoggleconsole);
1307 Key_History_Shutdown();
1310 const char *Key_GetBind (int key)
1313 if (key < 0 || key >= MAX_KEYS)
1315 bind = keybindings[key_bmap][key];
1317 bind = keybindings[key_bmap2][key];
1321 qboolean CL_VM_InputEvent (qboolean down, int key, int ascii);
1325 Called by the system between frames for both key up and key down events
1326 Should NOT be called during an interrupt!
1329 static char tbl_keyascii[MAX_KEYS];
1330 static keydest_t tbl_keydest[MAX_KEYS];
1333 Key_Event (int key, int ascii, qboolean down)
1337 keydest_t keydest = key_dest;
1339 if (key < 0 || key >= MAX_KEYS)
1343 bind = keybindings[key_bmap][key];
1345 bind = keybindings[key_bmap2][key];
1347 if (developer.integer >= 1000)
1348 Con_Printf("Key_Event(%i, '%c', %s) keydown %i bind \"%s\"\n", key, ascii, down ? "down" : "up", keydown[key], bind ? bind : "");
1350 if (key_consoleactive)
1353 if (!uim_in_console)
1355 UIM_EnterBuffer(key_line, sizeof(key_line), key_linepos, &Key_Console_SetCursor);
1356 uim_in_console = true;
1359 keydest = key_console;
1361 else if (uim_in_console)
1365 uim_in_console = false;
1371 // increment key repeat count each time a down is received so that things
1372 // which want to ignore key repeat can ignore it
1373 keydown[key] = min(keydown[key] + 1, 2);
1374 if(keydown[key] == 1) {
1375 tbl_keyascii[key] = ascii;
1376 tbl_keydest[key] = keydest;
1378 ascii = tbl_keyascii[key];
1379 keydest = tbl_keydest[key];
1384 // clear repeat count now that the key is released
1386 keydest = tbl_keydest[key];
1387 ascii = tbl_keyascii[key];
1390 if(keydest == key_void)
1393 // key_consoleactive is a flag not a key_dest because the console is a
1394 // high priority overlay ontop of the normal screen (designed as a safety
1395 // feature so that developers and users can rescue themselves from a bad
1398 // this also means that toggling the console on/off does not lose the old
1401 // specially handle escape (togglemenu) and shift-escape (toggleconsole)
1402 // engine bindings, these are not handled as normal binds so that the user
1403 // can recover from a completely empty bindmap
1404 if (key == K_ESCAPE)
1406 // ignore key repeats on escape
1407 if (keydown[key] > 1)
1410 // escape does these things:
1411 // key_consoleactive - close console
1412 // key_message - abort messagemode
1413 // key_menu - go to parent menu (or key_game)
1414 // key_game - open menu
1416 // in all modes shift-escape toggles console
1417 if (keydown[K_SHIFT])
1421 Con_ToggleConsole_f ();
1422 tbl_keydest[key] = key_void; // esc release should go nowhere (especially not to key_menu or key_game)
1432 if(key_consoleactive & KEY_CONSOLEACTIVE_FORCED)
1434 key_consoleactive &= ~KEY_CONSOLEACTIVE_USER;
1438 Con_ToggleConsole_f();
1444 Key_Message (key, ascii); // that'll close the message input
1448 case key_menu_grabbed:
1449 MR_KeyEvent (key, ascii, down);
1453 // csqc has priority over toggle menu if it wants to (e.g. handling escape for UI stuff in-game.. :sick:)
1454 q = CL_VM_InputEvent(down, key, ascii);
1460 Con_Printf ("Key_Event: Bad key_dest\n");
1465 // send function keydowns to interpreter no matter what mode is (unless the menu has specifically grabbed the keyboard, for rebinding keys)
1466 // NOTE: if UIM is active, let it handle function keys too:
1467 if (keydest == key_message && key >= K_F1 && key <= K_F12)
1470 Key_Message(key, ascii);
1473 if (keydest != key_menu_grabbed)
1474 if (key >= K_F1 && key <= K_F12)
1478 if(keydown[key] == 1 && down)
1480 // button commands add keynum as a parm
1482 Cbuf_AddText (va("%s %i\n", bind, key));
1485 Cbuf_AddText (bind);
1486 Cbuf_AddText ("\n");
1488 } else if(bind[0] == '+' && !down && keydown[key] == 0)
1489 Cbuf_AddText(va("-%s %i\n", bind + 1, key));
1494 // send input to console if it wants it
1495 if (keydest == key_console)
1499 // con_closeontoggleconsole enables toggleconsole keys to close the
1500 // console, as long as they are not the color prefix character
1501 // (special exemption for german keyboard layouts)
1502 if (con_closeontoggleconsole.integer && bind && !strncmp(bind, "toggleconsole", strlen("toggleconsole")) && (key_consoleactive & KEY_CONSOLEACTIVE_USER) && ascii != STRING_COLOR_TAG)
1504 Con_ToggleConsole_f ();
1507 Key_Console (key, ascii);
1511 // handle toggleconsole in menu too
1512 if (keydest == key_menu)
1514 if (down && con_closeontoggleconsole.integer && bind && !strncmp(bind, "toggleconsole", strlen("toggleconsole")) && ascii != STRING_COLOR_TAG)
1516 Con_ToggleConsole_f ();
1517 tbl_keydest[key] = key_void; // key release should go nowhere (especially not to key_menu or key_game)
1522 // ignore binds while a video is played, let the video system handle the key event
1523 if (cl_videoplaying)
1525 CL_Video_KeyEvent (key, ascii, keydown[key] != 0);
1529 // anything else is a key press into the game, chat line, or menu
1534 Key_Message (key, ascii);
1537 case key_menu_grabbed:
1538 MR_KeyEvent (key, ascii, down);
1541 q = CL_VM_InputEvent(down, key, ascii);
1542 // ignore key repeats on binds and only send the bind if the event hasnt been already processed by csqc
1545 if(keydown[key] == 1 && down)
1547 // button commands add keynum as a parm
1549 Cbuf_AddText (va("%s %i\n", bind, key));
1552 Cbuf_AddText (bind);
1553 Cbuf_AddText ("\n");
1555 } else if(bind[0] == '+' && !down && keydown[key] == 0)
1556 Cbuf_AddText(va("-%s %i\n", bind + 1, key));
1560 Con_Printf ("Key_Event: Bad key_dest\n");
1570 Key_ClearStates (void)
1572 memset(keydown, 0, sizeof(keydown));
1580 qboolean Key_IsPressed (int key)
1582 if (key < 0 || key >= (int)sizeof(keydown))
1584 // handle repeats differently?