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 = {CF_CLIENT | CF_ARCHIVE, "con_closeontoggleconsole","1", "allows toggleconsole binds to close the console as well; when set to 2, this even works when not at the start of the line in console input; when set to 3, this works even if the toggleconsole key is the color tag"};
31 key up events are sent even if in console mode
34 char key_line[MAX_INPUTLINE];
36 qbool key_insert = true; // insert key toggle (for editing)
38 int key_consoleactive;
39 char *keybindings[MAX_BINDMAPS][MAX_KEYS];
42 char history_savedline[MAX_INPUTLINE];
43 char history_searchstring[MAX_INPUTLINE];
44 qbool history_matchfound = false;
47 extern cvar_t con_textsize;
50 static void Key_History_Init(void)
53 ConBuffer_Init(&history, HIST_TEXTSIZE, HIST_MAXLINES, zonemempool);
55 // not necessary for mobile
56 #ifndef DP_MOBILETOUCH
57 historyfile = FS_OpenRealFile("darkplaces_history.txt", "rb", false); // rb to handle unix line endings on windows too
60 char buf[MAX_INPUTLINE];
67 c = FS_Getc(historyfile);
68 if(c < 0 || c == 0 || c == '\r' || c == '\n')
73 ConBuffer_AddLine(&history, buf, bufpos, 0);
81 if(bufpos < MAX_INPUTLINE - 1)
86 FS_Close(historyfile);
93 static void Key_History_Shutdown(void)
95 // not necessary for mobile
96 #ifndef DP_MOBILETOUCH
97 qfile_t *historyfile = FS_OpenRealFile("darkplaces_history.txt", "w", false);
102 Con_Print("Saving command history to darkplaces_history.txt ...\n");
103 for(i = 0; i < CONBUFFER_LINES_COUNT(&history); ++i)
104 FS_Printf(historyfile, "%s\n", ConBuffer_GetLine(&history, i));
105 FS_Close(historyfile);
108 Con_Print(CON_ERROR "Couldn't write darkplaces_history.txt\n");
111 ConBuffer_Shutdown(&history);
114 static void Key_History_Push(void)
116 if(key_line[1]) // empty?
117 if(strcmp(key_line, "]quit")) // putting these into the history just sucks
118 if(strncmp(key_line, "]quit ", 6)) // putting these into the history just sucks
119 if(strcmp(key_line, "]rcon_password")) // putting these into the history just sucks
120 if(strncmp(key_line, "]rcon_password ", 15)) // putting these into the history just sucks
121 ConBuffer_AddLine(&history, key_line + 1, (int)strlen(key_line) - 1, 0);
122 Con_Printf("%s\n", key_line); // don't mark empty lines as history
124 if (history_matchfound)
125 history_matchfound = false;
128 static qbool Key_History_Get_foundCommand(void)
130 if (!history_matchfound)
132 strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
133 key_linepos = (int)strlen(key_line);
134 history_matchfound = false;
138 static void Key_History_Up(void)
140 if(history_line == -1) // editing the "new" line
141 strlcpy(history_savedline, key_line + 1, sizeof(history_savedline));
143 if (Key_History_Get_foundCommand())
146 if(history_line == -1)
148 history_line = CONBUFFER_LINES_COUNT(&history) - 1;
149 if(history_line != -1)
151 strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
152 key_linepos = (int)strlen(key_line);
155 else if(history_line > 0)
157 --history_line; // this also does -1 -> 0, so it is good
158 strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
159 key_linepos = (int)strlen(key_line);
163 static void Key_History_Down(void)
165 if(history_line == -1) // editing the "new" line
168 if (Key_History_Get_foundCommand())
171 if(history_line < CONBUFFER_LINES_COUNT(&history) - 1)
174 strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
179 strlcpy(key_line + 1, history_savedline, sizeof(key_line) - 1);
182 key_linepos = (int)strlen(key_line);
185 static void Key_History_First(void)
187 if(history_line == -1) // editing the "new" line
188 strlcpy(history_savedline, key_line + 1, sizeof(history_savedline));
190 if (CONBUFFER_LINES_COUNT(&history) > 0)
193 strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
194 key_linepos = (int)strlen(key_line);
198 static void Key_History_Last(void)
200 if(history_line == -1) // editing the "new" line
201 strlcpy(history_savedline, key_line + 1, sizeof(history_savedline));
203 if (CONBUFFER_LINES_COUNT(&history) > 0)
205 history_line = CONBUFFER_LINES_COUNT(&history) - 1;
206 strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
207 key_linepos = (int)strlen(key_line);
211 static void Key_History_Find_Backwards(void)
214 const char *partial = key_line + 1;
216 size_t digits = strlen(va(vabuf, sizeof(vabuf), "%i", HIST_MAXLINES));
218 if (history_line == -1) // editing the "new" line
219 strlcpy(history_savedline, key_line + 1, sizeof(history_savedline));
221 if (strcmp(key_line + 1, history_searchstring)) // different string? Start a new search
223 strlcpy(history_searchstring, key_line + 1, sizeof(history_searchstring));
224 i = CONBUFFER_LINES_COUNT(&history) - 1;
226 else if (history_line == -1)
227 i = CONBUFFER_LINES_COUNT(&history) - 1;
229 i = history_line - 1;
233 else if (!( strchr(partial, '*') || strchr(partial, '?') )) // no pattern?
234 partial = va(vabuf, sizeof(vabuf), "*%s*", partial);
237 if (matchpattern_with_separator(ConBuffer_GetLine(&history, i), partial, true, "", false))
239 Con_Printf("^2%*i^7 %s\n", (int)digits, i+1, ConBuffer_GetLine(&history, i));
241 history_matchfound = true;
246 static void Key_History_Find_Forwards(void)
249 const char *partial = key_line + 1;
251 size_t digits = strlen(va(vabuf, sizeof(vabuf), "%i", HIST_MAXLINES));
253 if (history_line == -1) // editing the "new" line
256 if (strcmp(key_line + 1, history_searchstring)) // different string? Start a new search
258 strlcpy(history_searchstring, key_line + 1, sizeof(history_searchstring));
261 else i = history_line + 1;
265 else if (!( strchr(partial, '*') || strchr(partial, '?') )) // no pattern?
266 partial = va(vabuf, sizeof(vabuf), "*%s*", partial);
268 for ( ; i < CONBUFFER_LINES_COUNT(&history); i++)
269 if (matchpattern_with_separator(ConBuffer_GetLine(&history, i), partial, true, "", false))
271 Con_Printf("^2%*i^7 %s\n", (int)digits, i+1, ConBuffer_GetLine(&history, i));
273 history_matchfound = true;
278 static void Key_History_Find_All(void)
280 const char *partial = key_line + 1;
283 size_t digits = strlen(va(vabuf, sizeof(vabuf), "%i", HIST_MAXLINES));
284 Con_Printf("History commands containing \"%s\":\n", key_line + 1);
288 else if (!( strchr(partial, '*') || strchr(partial, '?') )) // no pattern?
289 partial = va(vabuf, sizeof(vabuf), "*%s*", partial);
291 for (i=0; i<CONBUFFER_LINES_COUNT(&history); i++)
292 if (matchpattern_with_separator(ConBuffer_GetLine(&history, i), partial, true, "", false))
294 Con_Printf("%s%*i^7 %s\n", (i == history_line) ? "^2" : "^3", (int)digits, i+1, ConBuffer_GetLine(&history, i));
297 Con_Printf("%i result%s\n\n", count, (count != 1) ? "s" : "");
300 static void Key_History_f(cmd_state_t *cmd)
302 char *errchar = NULL;
305 size_t digits = strlen(va(vabuf, sizeof(vabuf), "%i", HIST_MAXLINES));
307 if (Cmd_Argc (cmd) > 1)
309 if (!strcmp(Cmd_Argv(cmd, 1), "-c"))
311 ConBuffer_Clear(&history);
314 i = strtol(Cmd_Argv(cmd, 1), &errchar, 0);
315 if ((i < 0) || (i > CONBUFFER_LINES_COUNT(&history)) || (errchar && *errchar))
318 i = CONBUFFER_LINES_COUNT(&history) - i;
321 for ( ; i<CONBUFFER_LINES_COUNT(&history); i++)
322 Con_Printf("^3%*i^7 %s\n", (int)digits, i+1, ConBuffer_GetLine(&history, i));
326 static int key_bmap, key_bmap2;
327 static unsigned char keydown[MAX_KEYS]; // 0 = up, 1 = down, 2 = repeating
329 typedef struct keyname_s
336 static const keyname_t keynames[] = {
339 {"ESCAPE", K_ESCAPE},
342 // spacer so it lines up with keys.h
344 {"BACKSPACE", K_BACKSPACE},
345 {"UPARROW", K_UPARROW},
346 {"DOWNARROW", K_DOWNARROW},
347 {"LEFTARROW", K_LEFTARROW},
348 {"RIGHTARROW", K_RIGHTARROW},
376 {"NUMLOCK", K_NUMLOCK},
377 {"CAPSLOCK", K_CAPSLOCK},
378 {"SCROLLOCK", K_SCROLLOCK},
380 {"KP_INS", K_KP_INS },
382 {"KP_END", K_KP_END },
384 {"KP_DOWNARROW", K_KP_DOWNARROW },
386 {"KP_PGDN", K_KP_PGDN },
388 {"KP_LEFTARROW", K_KP_LEFTARROW },
391 {"KP_RIGHTARROW", K_KP_RIGHTARROW },
393 {"KP_HOME", K_KP_HOME },
395 {"KP_UPARROW", K_KP_UPARROW },
397 {"KP_PGUP", K_KP_PGUP },
399 {"KP_DEL", K_KP_DEL },
400 {"KP_PERIOD", K_KP_PERIOD},
401 {"KP_SLASH", K_KP_SLASH },
402 {"KP_DIVIDE", K_KP_DIVIDE},
403 {"KP_MULTIPLY", K_KP_MULTIPLY},
404 {"KP_MINUS", K_KP_MINUS},
405 {"KP_PLUS", K_KP_PLUS},
406 {"KP_ENTER", K_KP_ENTER},
407 {"KP_EQUALS", K_KP_EQUALS},
409 {"PRINTSCREEN", K_PRINTSCREEN},
413 {"MOUSE1", K_MOUSE1},
415 {"MOUSE2", K_MOUSE2},
416 {"MOUSE3", K_MOUSE3},
417 {"MWHEELUP", K_MWHEELUP},
418 {"MWHEELDOWN", K_MWHEELDOWN},
419 {"MOUSE4", K_MOUSE4},
420 {"MOUSE5", K_MOUSE5},
421 {"MOUSE6", K_MOUSE6},
422 {"MOUSE7", K_MOUSE7},
423 {"MOUSE8", K_MOUSE8},
424 {"MOUSE9", K_MOUSE9},
425 {"MOUSE10", K_MOUSE10},
426 {"MOUSE11", K_MOUSE11},
427 {"MOUSE12", K_MOUSE12},
428 {"MOUSE13", K_MOUSE13},
429 {"MOUSE14", K_MOUSE14},
430 {"MOUSE15", K_MOUSE15},
431 {"MOUSE16", K_MOUSE16},
491 {"X360_DPAD_UP", K_X360_DPAD_UP},
492 {"X360_DPAD_DOWN", K_X360_DPAD_DOWN},
493 {"X360_DPAD_LEFT", K_X360_DPAD_LEFT},
494 {"X360_DPAD_RIGHT", K_X360_DPAD_RIGHT},
495 {"X360_START", K_X360_START},
496 {"X360_BACK", K_X360_BACK},
497 {"X360_LEFT_THUMB", K_X360_LEFT_THUMB},
498 {"X360_RIGHT_THUMB", K_X360_RIGHT_THUMB},
499 {"X360_LEFT_SHOULDER", K_X360_LEFT_SHOULDER},
500 {"X360_RIGHT_SHOULDER", K_X360_RIGHT_SHOULDER},
501 {"X360_A", K_X360_A},
502 {"X360_B", K_X360_B},
503 {"X360_X", K_X360_X},
504 {"X360_Y", K_X360_Y},
505 {"X360_LEFT_TRIGGER", K_X360_LEFT_TRIGGER},
506 {"X360_RIGHT_TRIGGER", K_X360_RIGHT_TRIGGER},
507 {"X360_LEFT_THUMB_UP", K_X360_LEFT_THUMB_UP},
508 {"X360_LEFT_THUMB_DOWN", K_X360_LEFT_THUMB_DOWN},
509 {"X360_LEFT_THUMB_LEFT", K_X360_LEFT_THUMB_LEFT},
510 {"X360_LEFT_THUMB_RIGHT", K_X360_LEFT_THUMB_RIGHT},
511 {"X360_RIGHT_THUMB_UP", K_X360_RIGHT_THUMB_UP},
512 {"X360_RIGHT_THUMB_DOWN", K_X360_RIGHT_THUMB_DOWN},
513 {"X360_RIGHT_THUMB_LEFT", K_X360_RIGHT_THUMB_LEFT},
514 {"X360_RIGHT_THUMB_RIGHT", K_X360_RIGHT_THUMB_RIGHT},
516 {"JOY_UP", K_JOY_UP},
517 {"JOY_DOWN", K_JOY_DOWN},
518 {"JOY_LEFT", K_JOY_LEFT},
519 {"JOY_RIGHT", K_JOY_RIGHT},
521 {"SEMICOLON", ';'}, // because a raw semicolon separates commands
525 {"APOSTROPHE", '\''},
526 {"BACKSLASH", '\\'}, // because a raw backslash is used for special characters
528 {"MIDINOTE0", K_MIDINOTE0},
529 {"MIDINOTE1", K_MIDINOTE1},
530 {"MIDINOTE2", K_MIDINOTE2},
531 {"MIDINOTE3", K_MIDINOTE3},
532 {"MIDINOTE4", K_MIDINOTE4},
533 {"MIDINOTE5", K_MIDINOTE5},
534 {"MIDINOTE6", K_MIDINOTE6},
535 {"MIDINOTE7", K_MIDINOTE7},
536 {"MIDINOTE8", K_MIDINOTE8},
537 {"MIDINOTE9", K_MIDINOTE9},
538 {"MIDINOTE10", K_MIDINOTE10},
539 {"MIDINOTE11", K_MIDINOTE11},
540 {"MIDINOTE12", K_MIDINOTE12},
541 {"MIDINOTE13", K_MIDINOTE13},
542 {"MIDINOTE14", K_MIDINOTE14},
543 {"MIDINOTE15", K_MIDINOTE15},
544 {"MIDINOTE16", K_MIDINOTE16},
545 {"MIDINOTE17", K_MIDINOTE17},
546 {"MIDINOTE18", K_MIDINOTE18},
547 {"MIDINOTE19", K_MIDINOTE19},
548 {"MIDINOTE20", K_MIDINOTE20},
549 {"MIDINOTE21", K_MIDINOTE21},
550 {"MIDINOTE22", K_MIDINOTE22},
551 {"MIDINOTE23", K_MIDINOTE23},
552 {"MIDINOTE24", K_MIDINOTE24},
553 {"MIDINOTE25", K_MIDINOTE25},
554 {"MIDINOTE26", K_MIDINOTE26},
555 {"MIDINOTE27", K_MIDINOTE27},
556 {"MIDINOTE28", K_MIDINOTE28},
557 {"MIDINOTE29", K_MIDINOTE29},
558 {"MIDINOTE30", K_MIDINOTE30},
559 {"MIDINOTE31", K_MIDINOTE31},
560 {"MIDINOTE32", K_MIDINOTE32},
561 {"MIDINOTE33", K_MIDINOTE33},
562 {"MIDINOTE34", K_MIDINOTE34},
563 {"MIDINOTE35", K_MIDINOTE35},
564 {"MIDINOTE36", K_MIDINOTE36},
565 {"MIDINOTE37", K_MIDINOTE37},
566 {"MIDINOTE38", K_MIDINOTE38},
567 {"MIDINOTE39", K_MIDINOTE39},
568 {"MIDINOTE40", K_MIDINOTE40},
569 {"MIDINOTE41", K_MIDINOTE41},
570 {"MIDINOTE42", K_MIDINOTE42},
571 {"MIDINOTE43", K_MIDINOTE43},
572 {"MIDINOTE44", K_MIDINOTE44},
573 {"MIDINOTE45", K_MIDINOTE45},
574 {"MIDINOTE46", K_MIDINOTE46},
575 {"MIDINOTE47", K_MIDINOTE47},
576 {"MIDINOTE48", K_MIDINOTE48},
577 {"MIDINOTE49", K_MIDINOTE49},
578 {"MIDINOTE50", K_MIDINOTE50},
579 {"MIDINOTE51", K_MIDINOTE51},
580 {"MIDINOTE52", K_MIDINOTE52},
581 {"MIDINOTE53", K_MIDINOTE53},
582 {"MIDINOTE54", K_MIDINOTE54},
583 {"MIDINOTE55", K_MIDINOTE55},
584 {"MIDINOTE56", K_MIDINOTE56},
585 {"MIDINOTE57", K_MIDINOTE57},
586 {"MIDINOTE58", K_MIDINOTE58},
587 {"MIDINOTE59", K_MIDINOTE59},
588 {"MIDINOTE60", K_MIDINOTE60},
589 {"MIDINOTE61", K_MIDINOTE61},
590 {"MIDINOTE62", K_MIDINOTE62},
591 {"MIDINOTE63", K_MIDINOTE63},
592 {"MIDINOTE64", K_MIDINOTE64},
593 {"MIDINOTE65", K_MIDINOTE65},
594 {"MIDINOTE66", K_MIDINOTE66},
595 {"MIDINOTE67", K_MIDINOTE67},
596 {"MIDINOTE68", K_MIDINOTE68},
597 {"MIDINOTE69", K_MIDINOTE69},
598 {"MIDINOTE70", K_MIDINOTE70},
599 {"MIDINOTE71", K_MIDINOTE71},
600 {"MIDINOTE72", K_MIDINOTE72},
601 {"MIDINOTE73", K_MIDINOTE73},
602 {"MIDINOTE74", K_MIDINOTE74},
603 {"MIDINOTE75", K_MIDINOTE75},
604 {"MIDINOTE76", K_MIDINOTE76},
605 {"MIDINOTE77", K_MIDINOTE77},
606 {"MIDINOTE78", K_MIDINOTE78},
607 {"MIDINOTE79", K_MIDINOTE79},
608 {"MIDINOTE80", K_MIDINOTE80},
609 {"MIDINOTE81", K_MIDINOTE81},
610 {"MIDINOTE82", K_MIDINOTE82},
611 {"MIDINOTE83", K_MIDINOTE83},
612 {"MIDINOTE84", K_MIDINOTE84},
613 {"MIDINOTE85", K_MIDINOTE85},
614 {"MIDINOTE86", K_MIDINOTE86},
615 {"MIDINOTE87", K_MIDINOTE87},
616 {"MIDINOTE88", K_MIDINOTE88},
617 {"MIDINOTE89", K_MIDINOTE89},
618 {"MIDINOTE90", K_MIDINOTE90},
619 {"MIDINOTE91", K_MIDINOTE91},
620 {"MIDINOTE92", K_MIDINOTE92},
621 {"MIDINOTE93", K_MIDINOTE93},
622 {"MIDINOTE94", K_MIDINOTE94},
623 {"MIDINOTE95", K_MIDINOTE95},
624 {"MIDINOTE96", K_MIDINOTE96},
625 {"MIDINOTE97", K_MIDINOTE97},
626 {"MIDINOTE98", K_MIDINOTE98},
627 {"MIDINOTE99", K_MIDINOTE99},
628 {"MIDINOTE100", K_MIDINOTE100},
629 {"MIDINOTE101", K_MIDINOTE101},
630 {"MIDINOTE102", K_MIDINOTE102},
631 {"MIDINOTE103", K_MIDINOTE103},
632 {"MIDINOTE104", K_MIDINOTE104},
633 {"MIDINOTE105", K_MIDINOTE105},
634 {"MIDINOTE106", K_MIDINOTE106},
635 {"MIDINOTE107", K_MIDINOTE107},
636 {"MIDINOTE108", K_MIDINOTE108},
637 {"MIDINOTE109", K_MIDINOTE109},
638 {"MIDINOTE110", K_MIDINOTE110},
639 {"MIDINOTE111", K_MIDINOTE111},
640 {"MIDINOTE112", K_MIDINOTE112},
641 {"MIDINOTE113", K_MIDINOTE113},
642 {"MIDINOTE114", K_MIDINOTE114},
643 {"MIDINOTE115", K_MIDINOTE115},
644 {"MIDINOTE116", K_MIDINOTE116},
645 {"MIDINOTE117", K_MIDINOTE117},
646 {"MIDINOTE118", K_MIDINOTE118},
647 {"MIDINOTE119", K_MIDINOTE119},
648 {"MIDINOTE120", K_MIDINOTE120},
649 {"MIDINOTE121", K_MIDINOTE121},
650 {"MIDINOTE122", K_MIDINOTE122},
651 {"MIDINOTE123", K_MIDINOTE123},
652 {"MIDINOTE124", K_MIDINOTE124},
653 {"MIDINOTE125", K_MIDINOTE125},
654 {"MIDINOTE126", K_MIDINOTE126},
655 {"MIDINOTE127", K_MIDINOTE127},
661 ==============================================================================
663 LINE TYPING INTO THE CONSOLE
665 ==============================================================================
668 int Key_ClearEditLine(qbool is_console)
683 // key modifier states
684 #define KM_NONE (!keydown[K_CTRL] && !keydown[K_SHIFT] && !keydown[K_ALT])
685 #define KM_CTRL_SHIFT_ALT ( keydown[K_CTRL] && keydown[K_SHIFT] && keydown[K_ALT])
686 #define KM_CTRL_SHIFT ( keydown[K_CTRL] && keydown[K_SHIFT] && !keydown[K_ALT])
687 #define KM_CTRL_ALT ( keydown[K_CTRL] && !keydown[K_SHIFT] && keydown[K_ALT])
688 #define KM_SHIFT_ALT (!keydown[K_CTRL] && keydown[K_SHIFT] && keydown[K_ALT])
689 #define KM_CTRL ( keydown[K_CTRL] && !keydown[K_SHIFT] && !keydown[K_ALT])
690 #define KM_SHIFT (!keydown[K_CTRL] && keydown[K_SHIFT] && !keydown[K_ALT])
691 #define KM_ALT (!keydown[K_CTRL] && !keydown[K_SHIFT] && keydown[K_ALT])
695 Interactive line editing and console scrollback
699 signed char chat_mode; // 0 for say, 1 for say_team, -1 for command
700 char chat_buffer[MAX_INPUTLINE];
701 int chat_bufferpos = 0;
703 int Key_AddChar(int unicode, qbool is_console)
707 int len, blen, linepos;
712 linepos = key_linepos;
717 linepos = chat_bufferpos;
720 if (linepos >= MAX_INPUTLINE-1)
723 blen = u8_fromchar(unicode, buf, sizeof(buf));
726 len = (int)strlen(&line[linepos]);
727 // check insert mode, or always insert if at end of line
728 if (key_insert || len == 0)
730 if (linepos + len + blen >= MAX_INPUTLINE)
732 // can't use strcpy to move string to right
734 if (linepos + blen + len >= MAX_INPUTLINE)
736 memmove(&line[linepos + blen], &line[linepos], len);
738 else if (linepos + len + blen - u8_bytelen(line + linepos, 1) >= MAX_INPUTLINE)
740 memcpy(line + linepos, buf, blen);
742 line[linepos + blen] = 0;
747 // returns -1 if no key has been recognized
748 // returns linepos (>= 0) otherwise
749 // if is_console is true can modify key_line (doesn't change key_linepos)
750 int Key_Parse_CommonKeys(cmd_state_t *cmd, qbool is_console, int key, int unicode)
753 int linepos, linestart;
754 unsigned int linesize;
758 linepos = key_linepos;
759 linesize = sizeof(key_line);
765 linepos = chat_bufferpos;
766 linesize = sizeof(chat_buffer);
770 if ((key == 'v' && KM_CTRL) || ((key == K_INS || key == K_KP_INS) && KM_SHIFT))
773 if ((cbd = Sys_SDL_GetClipboardData()) != 0)
780 if (*p == '\r' && *(p+1) == '\n')
785 else if (*p == '\n' || *p == '\r' || *p == '\b')
791 strtok(cbd, "\n\r\b");
793 i = (int)strlen(cbd);
794 if (i + linepos >= MAX_INPUTLINE)
795 i= MAX_INPUTLINE - linepos - 1;
799 memmove(line + linepos + i, line + linepos, linesize - linepos - i);
800 memcpy(line + linepos, cbd, i);
808 if (key == 'u' && KM_CTRL) // like vi/readline ^u: delete currently edited line
810 return Key_ClearEditLine(is_console);
815 if (is_console && KM_CTRL) // append the cvar value to the cvar name
817 int cvar_len, cvar_str_len, chars_to_move;
819 char cvar[MAX_INPUTLINE];
820 const char *cvar_str;
822 // go to the start of the variable
826 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
831 // save the variable name in cvar
832 for(cvar_len=0; (k = line[linepos + cvar_len]) != 0; cvar_len++)
834 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
842 // go to the end of the cvar
845 // save the content of the variable in cvar_str
846 cvar_str = Cvar_VariableString(&cvars_all, cvar, CF_CLIENT | CF_SERVER);
847 cvar_str_len = (int)strlen(cvar_str);
851 // insert space and cvar_str in line
852 chars_to_move = (int)strlen(&line[linepos]);
853 if (linepos + 1 + cvar_str_len + chars_to_move < MAX_INPUTLINE)
856 memmove(&line[linepos + 1 + cvar_str_len], &line[linepos], chars_to_move);
857 line[linepos++] = ' ';
858 memcpy(&line[linepos], cvar_str, cvar_str_len);
859 linepos += cvar_str_len;
860 line[linepos + chars_to_move] = 0;
863 Con_Printf("Couldn't append cvar value, edit line too long.\n");
868 return Con_CompleteCommandLine(cmd, is_console);
871 // Advanced Console Editing by Radix radix@planetquake.com
872 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
874 // Enhanced by terencehill
876 // move cursor to the previous character
877 if (key == K_LEFTARROW || key == K_KP_LEFTARROW)
879 if(KM_CTRL) // move cursor to the previous word
883 if (linepos <= linestart + 1)
889 if (!(k == '\"' || k == ';' || k == ' ' || k == '\''))
891 } while(pos > linestart); // skip all "; ' after the word
893 if (pos == linestart)
898 if (k == '\"' || k == ';' || k == ' ' || k == '\'')
903 } while(pos > linestart);
909 if(KM_SHIFT) // move cursor to the previous character ignoring colors
913 if (linepos <= linestart + 1)
915 pos = (int)u8_prevbyte(line + linestart, linepos - linestart) + linestart;
916 while (pos > linestart)
917 if(pos-1 >= linestart && line[pos-1] == STRING_COLOR_TAG && isdigit(line[pos]))
919 else if(pos-4 >= linestart && line[pos-4] == STRING_COLOR_TAG && line[pos-3] == STRING_COLOR_RGB_TAG_CHAR
920 && isxdigit(line[pos-2]) && isxdigit(line[pos-1]) && isxdigit(line[pos]))
924 if(pos-1 >= linestart && line[pos-1] == STRING_COLOR_TAG && line[pos] == STRING_COLOR_TAG) // consider ^^ as a character
931 // we need to move to the beginning of the character when in a wide character:
932 u8_charidx(line, pos + 1, &inchar);
933 linepos = (int)(pos + 1 - inchar);
939 if (linepos <= linestart + 1)
941 // hide ']' from u8_prevbyte otherwise it could go out of bounds
942 linepos = (int)u8_prevbyte(line + linestart, linepos - linestart) + linestart;
947 // delete char before cursor
948 if ((key == K_BACKSPACE && KM_NONE) || (key == 'h' && KM_CTRL))
950 if (linepos > linestart)
952 // hide ']' from u8_prevbyte otherwise it could go out of bounds
953 int newpos = (int)u8_prevbyte(line + linestart, linepos - linestart) + linestart;
954 strlcpy(line + newpos, line + linepos, linesize + 1 - linepos);
960 // delete char on cursor
961 if ((key == K_DEL || key == K_KP_DEL) && KM_NONE)
964 linelen = strlen(line);
965 if (linepos < (int)linelen)
966 memmove(line + linepos, line + linepos + u8_bytelen(line + linepos, 1), linelen - linepos);
970 // move cursor to the next character
971 if (key == K_RIGHTARROW || key == K_KP_RIGHTARROW)
973 if (KM_CTRL) // move cursor to the next word
977 len = (int)strlen(line);
985 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
989 if (pos < len) // skip all "; ' after the word
993 if (!(k == '\"' || k == ';' || k == ' ' || k == '\''))
1000 if (KM_SHIFT) // move cursor to the next character ignoring colors
1003 len = (int)strlen(line);
1008 // go beyond all initial consecutive color tags, if any
1010 while (line[pos] == STRING_COLOR_TAG)
1012 if(isdigit(line[pos+1]))
1014 else if(line[pos+1] == STRING_COLOR_RGB_TAG_CHAR && isxdigit(line[pos+2]) && isxdigit(line[pos+3]) && isxdigit(line[pos+4]))
1021 if (line[pos] == STRING_COLOR_TAG && line[pos+1] == STRING_COLOR_TAG) // consider ^^ as a character
1023 pos += (int)u8_bytelen(line + pos, 1);
1025 // now go beyond all next consecutive color tags, if any
1027 while (line[pos] == STRING_COLOR_TAG)
1029 if(isdigit(line[pos+1]))
1031 else if(line[pos+1] == STRING_COLOR_RGB_TAG_CHAR && isxdigit(line[pos+2]) && isxdigit(line[pos+3]) && isxdigit(line[pos+4]))
1042 if (linepos >= (int)strlen(line))
1044 linepos += (int)u8_bytelen(line + linepos, 1);
1049 if ((key == K_INS || key == K_KP_INS) && KM_NONE) // toggle insert mode
1055 if (key == K_HOME || key == K_KP_HOME)
1057 if (is_console && KM_CTRL)
1059 con_backscroll = CON_TEXTSIZE;
1066 if (key == K_END || key == K_KP_END)
1068 if (is_console && KM_CTRL)
1074 return (int)strlen(line);
1080 static int Key_Convert_NumPadKey(int key)
1082 // LadyHavoc: copied most of this from Q2 to improve keyboard handling
1085 case K_KP_SLASH: return '/';
1086 case K_KP_MINUS: return '-';
1087 case K_KP_PLUS: return '+';
1088 case K_KP_HOME: return '7';
1089 case K_KP_UPARROW: return '8';
1090 case K_KP_PGUP: return '9';
1091 case K_KP_LEFTARROW: return '4';
1092 case K_KP_5: return '5';
1093 case K_KP_RIGHTARROW: return '6';
1094 case K_KP_END: return '1';
1095 case K_KP_DOWNARROW: return '2';
1096 case K_KP_PGDN: return '3';
1097 case K_KP_INS: return '0';
1098 case K_KP_DEL: return '.';
1103 static void Key_Console(cmd_state_t *cmd, int key, int unicode)
1107 key = Key_Convert_NumPadKey(key);
1109 // Forbid Ctrl Alt shortcuts since on Windows they are used to type some characters
1110 // in certain non-English keyboards using the AltGr key (which emulates Ctrl Alt)
1111 // Reference: "Why Ctrl+Alt shouldn't be used as a shortcut modifier"
1112 // https://blogs.msdn.microsoft.com/oldnewthing/20040329-00/?p=40003
1113 if (keydown[K_CTRL] && keydown[K_ALT])
1116 linepos = Key_Parse_CommonKeys(cmd, true, key, unicode);
1119 key_linepos = linepos;
1123 if ((key == K_ENTER || key == K_KP_ENTER) && KM_NONE)
1125 // bones_was_here: prepending allows a loop such as `alias foo "bar; wait; foo"; foo`
1126 // to be broken with an alias or unalias command
1127 Cbuf_InsertText(cmd, key_line+1); // skip the ]
1129 key_linepos = Key_ClearEditLine(true);
1130 // force an update, because the command may take some time
1131 if (cls.state == ca_disconnected)
1136 if (key == 'l' && KM_CTRL)
1138 Cbuf_AddText (cmd, "clear\n");
1142 if (key == 'q' && KM_CTRL) // like zsh ^q: push line to history, don't execute, and clear
1146 key_linepos = Key_ClearEditLine(true);
1150 // End Advanced Console Editing
1152 if (((key == K_UPARROW || key == K_KP_UPARROW) && KM_NONE) || (key == 'p' && KM_CTRL))
1158 if (((key == K_DOWNARROW || key == K_KP_DOWNARROW) && KM_NONE) || (key == 'n' && KM_CTRL))
1164 if (keydown[K_CTRL])
1166 // prints all the matching commands
1167 if (key == 'f' && KM_CTRL)
1169 Key_History_Find_All();
1172 // Search forwards/backwards, pointing the history's index to the
1173 // matching command but without fetching it to let one continue the search.
1174 // To fetch it, it suffices to just press UP or DOWN.
1175 if (key == 'r' && KM_CTRL_SHIFT)
1177 Key_History_Find_Forwards();
1180 if (key == 'r' && KM_CTRL)
1182 Key_History_Find_Backwards();
1186 // go to the last/first command of the history
1187 if (key == ',' && KM_CTRL)
1189 Key_History_First();
1192 if (key == '.' && KM_CTRL)
1199 if (key == K_PGUP || key == K_KP_PGUP)
1203 con_backscroll += ((vid_conheight.integer >> 2) / con_textsize.integer)-1;
1208 con_backscroll += ((vid_conheight.integer >> 1) / con_textsize.integer)-3;
1213 if (key == K_PGDN || key == K_KP_PGDN)
1217 con_backscroll -= ((vid_conheight.integer >> 2) / con_textsize.integer)-1;
1222 con_backscroll -= ((vid_conheight.integer >> 1) / con_textsize.integer)-3;
1227 if (key == K_MWHEELUP)
1231 con_backscroll += 1;
1236 con_backscroll += ((vid_conheight.integer >> 2) / con_textsize.integer)-1;
1241 con_backscroll += 5;
1246 if (key == K_MWHEELDOWN)
1250 con_backscroll -= 1;
1255 con_backscroll -= ((vid_conheight.integer >> 2) / con_textsize.integer)-1;
1260 con_backscroll -= 5;
1265 if (keydown[K_CTRL])
1268 if ((key == '+' || key == K_KP_PLUS) && KM_CTRL)
1270 if (con_textsize.integer < 128)
1271 Cvar_SetValueQuick(&con_textsize, con_textsize.integer + 1);
1275 if ((key == '-' || key == K_KP_MINUS) && KM_CTRL)
1277 if (con_textsize.integer > 1)
1278 Cvar_SetValueQuick(&con_textsize, con_textsize.integer - 1);
1282 if ((key == '0' || key == K_KP_INS) && KM_CTRL)
1284 Cvar_SetValueQuick(&con_textsize, atoi(Cvar_VariableDefString(&cvars_all, "con_textsize", CF_CLIENT | CF_SERVER)));
1295 key_linepos = Key_AddChar(unicode, true);
1298 //============================================================================
1301 Key_Message (cmd_state_t *cmd, int key, int ascii)
1306 key = Key_Convert_NumPadKey(key);
1308 if (key == K_ENTER || key == K_KP_ENTER || ascii == 10 || ascii == 13)
1311 Cmd_ExecuteString(cmd, chat_buffer, src_local, true); // not Cbuf_AddText to allow semiclons in args; however, this allows no variables then. Use aliases!
1313 CL_ForwardToServer(va(vabuf, sizeof(vabuf), "%s %s", chat_mode ? "say_team" : "say ", chat_buffer));
1315 key_dest = key_game;
1316 chat_bufferpos = Key_ClearEditLine(false);
1320 if (key == K_ESCAPE) {
1321 key_dest = key_game;
1322 chat_bufferpos = Key_ClearEditLine(false);
1326 linepos = Key_Parse_CommonKeys(cmd, false, key, ascii);
1329 chat_bufferpos = linepos;
1333 // ctrl+key generates an ascii value < 32 and shows a char from the charmap
1334 if (ascii > 0 && ascii < 32 && utf8_enable.integer)
1335 ascii = 0xE000 + ascii;
1338 return; // non printable
1340 chat_bufferpos = Key_AddChar(ascii, false);
1343 //============================================================================
1348 Returns a key number to be used to index keybindings[] by looking at
1349 the given string. Single ascii characters return themselves, while
1350 the K_* names are matched up.
1354 Key_StringToKeynum (const char *str)
1357 const keyname_t *kn;
1359 if (!str || !str[0])
1362 return tolower(str[0]);
1364 for (kn = keynames; kn->name; kn++) {
1365 if (!strcasecmp (str, kn->name))
1369 // non-ascii keys are Unicode codepoints, so give the character if it's valid;
1370 // error message have more than one character, don't allow it
1371 ch = u8_getnchar(str, &str, 3);
1372 return (ch == 0 || *str != 0) ? -1 : (int)ch;
1377 Returns a string (either a single ascii char, or a K_* name) for the
1379 FIXME: handle quote special (general escape sequence?)
1383 Key_KeynumToString (int keynum, char *tinystr, size_t tinystrlength)
1385 const keyname_t *kn;
1387 // -1 is an invalid code
1389 return "<KEY NOT FOUND>";
1391 // search overrides first, because some characters are special
1392 for (kn = keynames; kn->name; kn++)
1393 if (keynum == kn->keynum)
1396 // if it is printable, output it as a single character
1399 u8_fromchar(keynum, tinystr, tinystrlength);
1403 // if it is not overridden and not printable, we don't know what to do with it
1404 return "<UNKNOWN KEYNUM>";
1409 Key_SetBinding (int keynum, int bindmap, const char *binding)
1414 if (keynum == -1 || keynum >= MAX_KEYS)
1416 if ((bindmap < 0) || (bindmap >= MAX_BINDMAPS))
1419 // free old bindings
1420 if (keybindings[bindmap][keynum]) {
1421 Z_Free (keybindings[bindmap][keynum]);
1422 keybindings[bindmap][keynum] = NULL;
1424 if(!binding[0]) // make "" binds be removed --blub
1426 // allocate memory for new binding
1427 l = strlen (binding);
1428 newbinding = (char *)Z_Malloc (l + 1);
1429 memcpy (newbinding, binding, l + 1);
1431 keybindings[bindmap][keynum] = newbinding;
1435 void Key_GetBindMap(int *fg, int *bg)
1443 qbool Key_SetBindMap(int fg, int bg)
1445 if(fg >= MAX_BINDMAPS)
1447 if(bg >= MAX_BINDMAPS)
1457 Key_In_Unbind_f(cmd_state_t *cmd)
1460 char *errchar = NULL;
1462 if (Cmd_Argc (cmd) != 3) {
1463 Con_Print("in_unbind <bindmap> <key> : remove commands from a key\n");
1467 m = strtol(Cmd_Argv(cmd, 1), &errchar, 0);
1468 if ((m < 0) || (m >= MAX_BINDMAPS) || (errchar && *errchar)) {
1469 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(cmd, 1));
1473 b = Key_StringToKeynum (Cmd_Argv(cmd, 2));
1475 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv(cmd, 2));
1479 if(!Key_SetBinding (b, m, ""))
1480 Con_Printf("Key_SetBinding failed for unknown reason\n");
1484 Key_In_Bind_f(cmd_state_t *cmd)
1487 char line[MAX_INPUTLINE];
1488 char *errchar = NULL;
1492 if (c != 3 && c != 4) {
1493 Con_Print("in_bind <bindmap> <key> [command] : attach a command to a key\n");
1497 m = strtol(Cmd_Argv(cmd, 1), &errchar, 0);
1498 if ((m < 0) || (m >= MAX_BINDMAPS) || (errchar && *errchar)) {
1499 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(cmd, 1));
1503 b = Key_StringToKeynum (Cmd_Argv(cmd, 2));
1504 if (b == -1 || b >= MAX_KEYS) {
1505 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv(cmd, 2));
1510 if (keybindings[m][b])
1511 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv(cmd, 2), keybindings[m][b]);
1513 Con_Printf("\"%s\" is not bound\n", Cmd_Argv(cmd, 2));
1516 // copy the rest of the command line
1517 line[0] = 0; // start out with a null string
1518 for (i = 3; i < c; i++) {
1519 strlcat (line, Cmd_Argv(cmd, i), sizeof (line));
1521 strlcat (line, " ", sizeof (line));
1524 if(!Key_SetBinding (b, m, line))
1525 Con_Printf("Key_SetBinding failed for unknown reason\n");
1529 Key_In_Bindmap_f(cmd_state_t *cmd)
1532 char *errchar = NULL;
1537 Con_Print("in_bindmap <bindmap> <fallback>: set current bindmap and fallback\n");
1541 m1 = strtol(Cmd_Argv(cmd, 1), &errchar, 0);
1542 if ((m1 < 0) || (m1 >= MAX_BINDMAPS) || (errchar && *errchar)) {
1543 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(cmd, 1));
1547 m2 = strtol(Cmd_Argv(cmd, 2), &errchar, 0);
1548 if ((m2 < 0) || (m2 >= MAX_BINDMAPS) || (errchar && *errchar)) {
1549 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(cmd, 2));
1558 Key_Unbind_f(cmd_state_t *cmd)
1562 if (Cmd_Argc (cmd) != 2) {
1563 Con_Print("unbind <key> : remove commands from a key\n");
1567 b = Key_StringToKeynum (Cmd_Argv(cmd, 1));
1569 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv(cmd, 1));
1573 if(!Key_SetBinding (b, 0, ""))
1574 Con_Printf("Key_SetBinding failed for unknown reason\n");
1578 Key_Unbindall_f(cmd_state_t *cmd)
1582 for (j = 0; j < MAX_BINDMAPS; j++)
1583 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
1584 if (keybindings[j][i])
1585 Key_SetBinding (i, j, "");
1589 Key_PrintBindList(int j)
1591 char bindbuf[MAX_INPUTLINE];
1592 char tinystr[TINYSTR_LEN];
1596 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
1598 p = keybindings[j][i];
1601 Cmd_QuoteString(bindbuf, sizeof(bindbuf), p, "\"\\", false);
1603 Con_Printf("^2%s ^7= \"%s\"\n", Key_KeynumToString (i, tinystr, TINYSTR_LEN), bindbuf);
1605 Con_Printf("^3bindmap %d: ^2%s ^7= \"%s\"\n", j, Key_KeynumToString (i, tinystr, TINYSTR_LEN), bindbuf);
1611 Key_In_BindList_f(cmd_state_t *cmd)
1614 char *errchar = NULL;
1616 if(Cmd_Argc(cmd) >= 2)
1618 m = strtol(Cmd_Argv(cmd, 1), &errchar, 0);
1619 if ((m < 0) || (m >= MAX_BINDMAPS) || (errchar && *errchar)) {
1620 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(cmd, 1));
1623 Key_PrintBindList(m);
1627 for (m = 0; m < MAX_BINDMAPS; m++)
1628 Key_PrintBindList(m);
1633 Key_BindList_f(cmd_state_t *cmd)
1635 Key_PrintBindList(0);
1639 Key_Bind_f(cmd_state_t *cmd)
1642 char line[MAX_INPUTLINE];
1646 if (c != 2 && c != 3) {
1647 Con_Print("bind <key> [command] : attach a command to a key\n");
1650 b = Key_StringToKeynum (Cmd_Argv(cmd, 1));
1651 if (b == -1 || b >= MAX_KEYS) {
1652 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv(cmd, 1));
1657 if (keybindings[0][b])
1658 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv(cmd, 1), keybindings[0][b]);
1660 Con_Printf("\"%s\" is not bound\n", Cmd_Argv(cmd, 1));
1663 // copy the rest of the command line
1664 line[0] = 0; // start out with a null string
1665 for (i = 2; i < c; i++) {
1666 strlcat (line, Cmd_Argv(cmd, i), sizeof (line));
1668 strlcat (line, " ", sizeof (line));
1671 if(!Key_SetBinding (b, 0, line))
1672 Con_Printf("Key_SetBinding failed for unknown reason\n");
1677 Writes lines containing "bind key value"
1681 Key_WriteBindings (qfile_t *f)
1684 char bindbuf[MAX_INPUTLINE];
1685 char tinystr[TINYSTR_LEN];
1688 // Override default binds
1689 FS_Printf(f, "unbindall\n");
1691 for (j = 0; j < MAX_BINDMAPS; j++)
1693 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
1695 p = keybindings[j][i];
1698 Cmd_QuoteString(bindbuf, sizeof(bindbuf), p, "\"\\", false); // don't need to escape $ because cvars are not expanded inside bind
1700 FS_Printf(f, "bind %s \"%s\"\n", Key_KeynumToString (i, tinystr, TINYSTR_LEN), bindbuf);
1702 FS_Printf(f, "in_bind %d %s \"%s\"\n", j, Key_KeynumToString (i, tinystr, TINYSTR_LEN), bindbuf);
1713 key_linepos = Key_ClearEditLine(true);
1716 // register our functions
1718 Cmd_AddCommand(CF_CLIENT, "in_bind", Key_In_Bind_f, "binds a command to the specified key in the selected bindmap");
1719 Cmd_AddCommand(CF_CLIENT, "in_unbind", Key_In_Unbind_f, "removes command on the specified key in the selected bindmap");
1720 Cmd_AddCommand(CF_CLIENT, "in_bindlist", Key_In_BindList_f, "bindlist: displays bound keys for all bindmaps, or the given bindmap");
1721 Cmd_AddCommand(CF_CLIENT, "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");
1722 Cmd_AddCommand(CF_CLIENT, "in_releaseall", Key_ReleaseAll_f, "releases all currently pressed keys (debug command)");
1724 Cmd_AddCommand(CF_CLIENT, "bind", Key_Bind_f, "binds a command to the specified key in bindmap 0");
1725 Cmd_AddCommand(CF_CLIENT, "unbind", Key_Unbind_f, "removes a command on the specified key in bindmap 0");
1726 Cmd_AddCommand(CF_CLIENT, "bindlist", Key_BindList_f, "bindlist: displays bound keys for bindmap 0 bindmaps");
1727 Cmd_AddCommand(CF_CLIENT, "unbindall", Key_Unbindall_f, "removes all commands from all keys in all bindmaps (leaving only shift-escape and escape)");
1729 Cmd_AddCommand(CF_CLIENT, "history", Key_History_f, "prints the history of executed commands (history X prints the last X entries, history -c clears the whole history)");
1731 Cvar_RegisterVariable (&con_closeontoggleconsole);
1737 Key_History_Shutdown();
1740 const char *Key_GetBind (int key, int bindmap)
1743 if (key < 0 || key >= MAX_KEYS)
1745 if(bindmap >= MAX_BINDMAPS)
1749 bind = keybindings[bindmap][key];
1753 bind = keybindings[key_bmap][key];
1755 bind = keybindings[key_bmap2][key];
1760 void Key_FindKeysForCommand (const char *command, int *keys, int numkeys, int bindmap)
1766 for (j = 0;j < numkeys;j++)
1769 if(bindmap >= MAX_BINDMAPS)
1774 for (j = 0; j < MAX_KEYS; ++j)
1776 b = Key_GetBind(j, bindmap);
1779 if (!strcmp (b, command) )
1782 if (count == numkeys)
1790 Called by the system between frames for both key up and key down events
1791 Should NOT be called during an interrupt!
1794 static char tbl_keyascii[MAX_KEYS];
1795 static keydest_t tbl_keydest[MAX_KEYS];
1797 typedef struct eventqueueitem_s
1804 static int events_blocked = 0;
1805 static eventqueueitem_t eventqueue[32];
1806 static unsigned eventqueue_idx = 0;
1808 static void Key_EventQueue_Add(int key, int ascii, qbool down)
1810 if(eventqueue_idx < sizeof(eventqueue) / sizeof(*eventqueue))
1812 eventqueue[eventqueue_idx].key = key;
1813 eventqueue[eventqueue_idx].ascii = ascii;
1814 eventqueue[eventqueue_idx].down = down;
1819 void Key_EventQueue_Block(void)
1821 // block key events until call to Unblock
1822 events_blocked = true;
1825 void Key_EventQueue_Unblock(void)
1827 // unblocks key events again
1829 events_blocked = false;
1830 for(i = 0; i < eventqueue_idx; ++i)
1831 Key_Event(eventqueue[i].key, eventqueue[i].ascii, eventqueue[i].down);
1836 Key_Event (int key, int ascii, qbool down)
1838 cmd_state_t *cmd = cmd_local;
1841 keydest_t keydest = key_dest;
1844 if (key < 0 || key >= MAX_KEYS)
1849 Key_EventQueue_Add(key, ascii, down);
1854 bind = keybindings[key_bmap][key];
1856 bind = keybindings[key_bmap2][key];
1858 if (developer_insane.integer)
1859 Con_DPrintf("Key_Event(%i, '%c', %s) keydown %i bind \"%s\"\n", key, ascii ? ascii : '?', down ? "down" : "up", keydown[key], bind ? bind : "");
1861 if(key_consoleactive)
1862 keydest = key_console;
1866 // increment key repeat count each time a down is received so that things
1867 // which want to ignore key repeat can ignore it
1868 keydown[key] = min(keydown[key] + 1, 2);
1869 if(keydown[key] == 1) {
1870 tbl_keyascii[key] = ascii;
1871 tbl_keydest[key] = keydest;
1873 ascii = tbl_keyascii[key];
1874 keydest = tbl_keydest[key];
1879 // clear repeat count now that the key is released
1881 keydest = tbl_keydest[key];
1882 ascii = tbl_keyascii[key];
1885 if(keydest == key_void)
1888 // key_consoleactive is a flag not a key_dest because the console is a
1889 // high priority overlay ontop of the normal screen (designed as a safety
1890 // feature so that developers and users can rescue themselves from a bad
1893 // this also means that toggling the console on/off does not lose the old
1896 // specially handle escape (togglemenu) and shift-escape (toggleconsole)
1897 // engine bindings, these are not handled as normal binds so that the user
1898 // can recover from a completely empty bindmap
1899 if (key == K_ESCAPE)
1901 // ignore key repeats on escape
1902 if (keydown[key] > 1)
1905 // escape does these things:
1906 // key_consoleactive - close console
1907 // key_message - abort messagemode
1908 // key_menu - go to parent menu (or key_game)
1909 // key_game - open menu
1911 // in all modes shift-escape toggles console
1912 if (keydown[K_SHIFT])
1916 Con_ToggleConsole_f(cmd_local);
1917 tbl_keydest[key] = key_void; // esc release should go nowhere (especially not to key_menu or key_game)
1927 if(key_consoleactive & KEY_CONSOLEACTIVE_FORCED)
1929 key_consoleactive &= ~KEY_CONSOLEACTIVE_USER;
1935 Con_ToggleConsole_f(cmd_local);
1941 Key_Message (cmd, key, ascii); // that'll close the message input
1945 case key_menu_grabbed:
1947 MR_KeyEvent (key, ascii, down);
1952 // csqc has priority over toggle menu if it wants to (e.g. handling escape for UI stuff in-game.. :sick:)
1953 q = CL_VM_InputEvent(down ? 0 : 1, key, ascii);
1961 Con_Printf ("Key_Event: Bad key_dest\n");
1966 // send function keydowns to interpreter no matter what mode is (unless the menu has specifically grabbed the keyboard, for rebinding keys)
1967 // VorteX: Omnicide does bind F* keys
1968 if (keydest != key_menu_grabbed)
1969 if (key >= K_F1 && key <= K_F12 && gamemode != GAME_BLOODOMNICIDE)
1973 if(keydown[key] == 1 && down)
1975 // button commands add keynum as a parm
1976 // prepend to avoid delays from `wait` commands added by other sources
1978 Cbuf_InsertText(cmd, va(vabuf, sizeof(vabuf), "%s %i\n", bind, key));
1980 Cbuf_InsertText(cmd, bind);
1982 else if(bind[0] == '+' && !down && keydown[key] == 0)
1983 // append -bind to ensure it's after the +bind in case they arrive in the same frame
1984 Cbuf_AddText(cmd, va(vabuf, sizeof(vabuf), "-%s %i\n", bind + 1, key));
1989 // send input to console if it wants it
1990 if (keydest == key_console)
1994 // con_closeontoggleconsole enables toggleconsole keys to close the
1995 // console, as long as they are not the color prefix character
1996 // (special exemption for german keyboard layouts)
1997 if (con_closeontoggleconsole.integer && bind && !strncmp(bind, "toggleconsole", strlen("toggleconsole")) && (key_consoleactive & KEY_CONSOLEACTIVE_USER) && (con_closeontoggleconsole.integer >= ((ascii != STRING_COLOR_TAG) ? 2 : 3) || key_linepos == 1))
1999 Con_ToggleConsole_f(cmd_local);
2003 if (Sys_CheckParm ("-noconsole"))
2004 return; // only allow the key bind to turn off console
2006 Key_Console (cmd, key, ascii);
2010 // handle toggleconsole in menu too
2011 if (keydest == key_menu)
2013 if (down && con_closeontoggleconsole.integer && bind && !strncmp(bind, "toggleconsole", strlen("toggleconsole")) && ascii != STRING_COLOR_TAG)
2015 Cbuf_InsertText(cmd, "toggleconsole\n"); // Deferred to next frame so we're not sending the text event to the console.
2016 tbl_keydest[key] = key_void; // key release should go nowhere (especially not to key_menu or key_game)
2021 // ignore binds while a video is played, let the video system handle the key event
2022 if (cl_videoplaying)
2024 if (gamemode == GAME_BLOODOMNICIDE) // menu controls key events
2026 MR_KeyEvent(key, ascii, down);
2032 CL_Video_KeyEvent (key, ascii, keydown[key] != 0);
2036 // anything else is a key press into the game, chat line, or menu
2041 Key_Message (cmd, key, ascii);
2044 case key_menu_grabbed:
2046 MR_KeyEvent (key, ascii, down);
2050 q = CL_VM_InputEvent(down ? 0 : 1, key, ascii);
2051 // ignore key repeats on binds and only send the bind if the event hasnt been already processed by csqc
2054 if(keydown[key] == 1 && down)
2056 // button commands add keynum as a parm
2057 // prepend to avoid delays from `wait` commands added by other sources
2059 Cbuf_InsertText(cmd, va(vabuf, sizeof(vabuf), "%s %i\n", bind, key));
2061 Cbuf_InsertText(cmd, bind);
2063 else if(bind[0] == '+' && !down && keydown[key] == 0)
2064 // append -bind to ensure it's after the +bind in case they arrive in the same frame
2065 Cbuf_AddText(cmd, va(vabuf, sizeof(vabuf), "-%s %i\n", bind + 1, key));
2069 Con_Printf ("Key_Event: Bad key_dest\n");
2073 // a helper to simulate release of ALL keys
2075 Key_ReleaseAll (void)
2078 // clear the event queue first
2080 // then send all down events (possibly into the event queue)
2081 for(key = 0; key < MAX_KEYS; ++key)
2083 Key_Event(key, 0, false);
2084 // now all keys are guaranteed down (once the event queue is unblocked)
2085 // and only future events count
2088 void Key_ReleaseAll_f(cmd_state_t *cmd)