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];
44 extern cvar_t con_textsize;
45 static qboolean uim_in_console = false;
48 static void Key_History_Init(void)
51 ConBuffer_Init(&history, HIST_TEXTSIZE, HIST_MAXLINES, zonemempool);
53 historyfile = FS_OpenRealFile("darkplaces_history.txt", "rb", false); // rb to handle unix line endings on windows too
56 char buf[MAX_INPUTLINE];
63 c = FS_Getc(historyfile);
64 if(c < 0 || c == 0 || c == '\r' || c == '\n')
69 ConBuffer_AddLine(&history, buf, bufpos, 0);
77 if(bufpos < MAX_INPUTLINE - 1)
82 FS_Close(historyfile);
88 static void Key_History_Shutdown(void)
90 // TODO write history to a file
92 qfile_t *historyfile = FS_OpenRealFile("darkplaces_history.txt", "w", false);
96 for(i = 0; i < CONBUFFER_LINES_COUNT(&history); ++i)
97 FS_Printf(historyfile, "%s\n", ConBuffer_GetLine(&history, i));
98 FS_Close(historyfile);
101 ConBuffer_Shutdown(&history);
104 static void Key_History_Push(void)
106 if(key_line[1]) // empty?
107 if(strcmp(key_line, "]quit")) // putting these into the history just sucks
108 if(strncmp(key_line, "]quit ", 6)) // putting these into the history just sucks
109 ConBuffer_AddLine(&history, key_line + 1, strlen(key_line) - 1, 0);
110 Con_Printf("%s\n", key_line); // don't mark empty lines as history
114 static void Key_History_Up(void)
116 if(history_line == -1) // editing the "new" line
117 strlcpy(history_savedline, key_line + 1, sizeof(history_savedline));
119 if(history_line == -1)
121 history_line = CONBUFFER_LINES_COUNT(&history) - 1;
122 if(history_line != -1)
124 strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
125 key_linepos = strlen(key_line);
128 else if(history_line > 0)
130 --history_line; // this also does -1 -> 0, so it is good
131 strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
132 key_linepos = strlen(key_line);
136 static void Key_History_Down(void)
138 if(history_line == -1) // editing the "new" line
141 if(history_line < CONBUFFER_LINES_COUNT(&history) - 1)
144 strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
149 strlcpy(key_line + 1, history_savedline, sizeof(key_line) - 1);
152 key_linepos = strlen(key_line);
155 static int key_bmap, key_bmap2;
156 static unsigned char keydown[MAX_KEYS]; // 0 = up, 1 = down, 2 = repeating
158 typedef struct keyname_s
165 static const keyname_t keynames[] = {
168 {"ESCAPE", K_ESCAPE},
171 // spacer so it lines up with keys.h
173 {"BACKSPACE", K_BACKSPACE},
174 {"UPARROW", K_UPARROW},
175 {"DOWNARROW", K_DOWNARROW},
176 {"LEFTARROW", K_LEFTARROW},
177 {"RIGHTARROW", K_RIGHTARROW},
205 {"NUMLOCK", K_NUMLOCK},
206 {"CAPSLOCK", K_CAPSLOCK},
207 {"SCROLLOCK", K_SCROLLOCK},
209 {"KP_INS", K_KP_INS },
211 {"KP_END", K_KP_END },
213 {"KP_DOWNARROW", K_KP_DOWNARROW },
215 {"KP_PGDN", K_KP_PGDN },
217 {"KP_LEFTARROW", K_KP_LEFTARROW },
220 {"KP_RIGHTARROW", K_KP_RIGHTARROW },
222 {"KP_HOME", K_KP_HOME },
224 {"KP_UPARROW", K_KP_UPARROW },
226 {"KP_PGUP", K_KP_PGUP },
228 {"KP_DEL", K_KP_DEL },
229 {"KP_PERIOD", K_KP_PERIOD},
230 {"KP_SLASH", K_KP_SLASH },
231 {"KP_DIVIDE", K_KP_DIVIDE},
232 {"KP_MULTIPLY", K_KP_MULTIPLY},
233 {"KP_MINUS", K_KP_MINUS},
234 {"KP_PLUS", K_KP_PLUS},
235 {"KP_ENTER", K_KP_ENTER},
236 {"KP_EQUALS", K_KP_EQUALS},
238 {"PRINTSCREEN", K_PRINTSCREEN},
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
327 {"MIDINOTE0", K_MIDINOTE0},
328 {"MIDINOTE1", K_MIDINOTE1},
329 {"MIDINOTE2", K_MIDINOTE2},
330 {"MIDINOTE3", K_MIDINOTE3},
331 {"MIDINOTE4", K_MIDINOTE4},
332 {"MIDINOTE5", K_MIDINOTE5},
333 {"MIDINOTE6", K_MIDINOTE6},
334 {"MIDINOTE7", K_MIDINOTE7},
335 {"MIDINOTE8", K_MIDINOTE8},
336 {"MIDINOTE9", K_MIDINOTE9},
337 {"MIDINOTE10", K_MIDINOTE10},
338 {"MIDINOTE11", K_MIDINOTE11},
339 {"MIDINOTE12", K_MIDINOTE12},
340 {"MIDINOTE13", K_MIDINOTE13},
341 {"MIDINOTE14", K_MIDINOTE14},
342 {"MIDINOTE15", K_MIDINOTE15},
343 {"MIDINOTE16", K_MIDINOTE16},
344 {"MIDINOTE17", K_MIDINOTE17},
345 {"MIDINOTE18", K_MIDINOTE18},
346 {"MIDINOTE19", K_MIDINOTE19},
347 {"MIDINOTE20", K_MIDINOTE20},
348 {"MIDINOTE21", K_MIDINOTE21},
349 {"MIDINOTE22", K_MIDINOTE22},
350 {"MIDINOTE23", K_MIDINOTE23},
351 {"MIDINOTE24", K_MIDINOTE24},
352 {"MIDINOTE25", K_MIDINOTE25},
353 {"MIDINOTE26", K_MIDINOTE26},
354 {"MIDINOTE27", K_MIDINOTE27},
355 {"MIDINOTE28", K_MIDINOTE28},
356 {"MIDINOTE29", K_MIDINOTE29},
357 {"MIDINOTE30", K_MIDINOTE30},
358 {"MIDINOTE31", K_MIDINOTE31},
359 {"MIDINOTE32", K_MIDINOTE32},
360 {"MIDINOTE33", K_MIDINOTE33},
361 {"MIDINOTE34", K_MIDINOTE34},
362 {"MIDINOTE35", K_MIDINOTE35},
363 {"MIDINOTE36", K_MIDINOTE36},
364 {"MIDINOTE37", K_MIDINOTE37},
365 {"MIDINOTE38", K_MIDINOTE38},
366 {"MIDINOTE39", K_MIDINOTE39},
367 {"MIDINOTE40", K_MIDINOTE40},
368 {"MIDINOTE41", K_MIDINOTE41},
369 {"MIDINOTE42", K_MIDINOTE42},
370 {"MIDINOTE43", K_MIDINOTE43},
371 {"MIDINOTE44", K_MIDINOTE44},
372 {"MIDINOTE45", K_MIDINOTE45},
373 {"MIDINOTE46", K_MIDINOTE46},
374 {"MIDINOTE47", K_MIDINOTE47},
375 {"MIDINOTE48", K_MIDINOTE48},
376 {"MIDINOTE49", K_MIDINOTE49},
377 {"MIDINOTE50", K_MIDINOTE50},
378 {"MIDINOTE51", K_MIDINOTE51},
379 {"MIDINOTE52", K_MIDINOTE52},
380 {"MIDINOTE53", K_MIDINOTE53},
381 {"MIDINOTE54", K_MIDINOTE54},
382 {"MIDINOTE55", K_MIDINOTE55},
383 {"MIDINOTE56", K_MIDINOTE56},
384 {"MIDINOTE57", K_MIDINOTE57},
385 {"MIDINOTE58", K_MIDINOTE58},
386 {"MIDINOTE59", K_MIDINOTE59},
387 {"MIDINOTE60", K_MIDINOTE60},
388 {"MIDINOTE61", K_MIDINOTE61},
389 {"MIDINOTE62", K_MIDINOTE62},
390 {"MIDINOTE63", K_MIDINOTE63},
391 {"MIDINOTE64", K_MIDINOTE64},
392 {"MIDINOTE65", K_MIDINOTE65},
393 {"MIDINOTE66", K_MIDINOTE66},
394 {"MIDINOTE67", K_MIDINOTE67},
395 {"MIDINOTE68", K_MIDINOTE68},
396 {"MIDINOTE69", K_MIDINOTE69},
397 {"MIDINOTE70", K_MIDINOTE70},
398 {"MIDINOTE71", K_MIDINOTE71},
399 {"MIDINOTE72", K_MIDINOTE72},
400 {"MIDINOTE73", K_MIDINOTE73},
401 {"MIDINOTE74", K_MIDINOTE74},
402 {"MIDINOTE75", K_MIDINOTE75},
403 {"MIDINOTE76", K_MIDINOTE76},
404 {"MIDINOTE77", K_MIDINOTE77},
405 {"MIDINOTE78", K_MIDINOTE78},
406 {"MIDINOTE79", K_MIDINOTE79},
407 {"MIDINOTE80", K_MIDINOTE80},
408 {"MIDINOTE81", K_MIDINOTE81},
409 {"MIDINOTE82", K_MIDINOTE82},
410 {"MIDINOTE83", K_MIDINOTE83},
411 {"MIDINOTE84", K_MIDINOTE84},
412 {"MIDINOTE85", K_MIDINOTE85},
413 {"MIDINOTE86", K_MIDINOTE86},
414 {"MIDINOTE87", K_MIDINOTE87},
415 {"MIDINOTE88", K_MIDINOTE88},
416 {"MIDINOTE89", K_MIDINOTE89},
417 {"MIDINOTE90", K_MIDINOTE90},
418 {"MIDINOTE91", K_MIDINOTE91},
419 {"MIDINOTE92", K_MIDINOTE92},
420 {"MIDINOTE93", K_MIDINOTE93},
421 {"MIDINOTE94", K_MIDINOTE94},
422 {"MIDINOTE95", K_MIDINOTE95},
423 {"MIDINOTE96", K_MIDINOTE96},
424 {"MIDINOTE97", K_MIDINOTE97},
425 {"MIDINOTE98", K_MIDINOTE98},
426 {"MIDINOTE99", K_MIDINOTE99},
427 {"MIDINOTE100", K_MIDINOTE100},
428 {"MIDINOTE101", K_MIDINOTE101},
429 {"MIDINOTE102", K_MIDINOTE102},
430 {"MIDINOTE103", K_MIDINOTE103},
431 {"MIDINOTE104", K_MIDINOTE104},
432 {"MIDINOTE105", K_MIDINOTE105},
433 {"MIDINOTE106", K_MIDINOTE106},
434 {"MIDINOTE107", K_MIDINOTE107},
435 {"MIDINOTE108", K_MIDINOTE108},
436 {"MIDINOTE109", K_MIDINOTE109},
437 {"MIDINOTE110", K_MIDINOTE110},
438 {"MIDINOTE111", K_MIDINOTE111},
439 {"MIDINOTE112", K_MIDINOTE112},
440 {"MIDINOTE113", K_MIDINOTE113},
441 {"MIDINOTE114", K_MIDINOTE114},
442 {"MIDINOTE115", K_MIDINOTE115},
443 {"MIDINOTE116", K_MIDINOTE116},
444 {"MIDINOTE117", K_MIDINOTE117},
445 {"MIDINOTE118", K_MIDINOTE118},
446 {"MIDINOTE119", K_MIDINOTE119},
447 {"MIDINOTE120", K_MIDINOTE120},
448 {"MIDINOTE121", K_MIDINOTE121},
449 {"MIDINOTE122", K_MIDINOTE122},
450 {"MIDINOTE123", K_MIDINOTE123},
451 {"MIDINOTE124", K_MIDINOTE124},
452 {"MIDINOTE125", K_MIDINOTE125},
453 {"MIDINOTE126", K_MIDINOTE126},
454 {"MIDINOTE127", K_MIDINOTE127},
460 ==============================================================================
462 LINE TYPING INTO THE CONSOLE
464 ==============================================================================
468 Key_ClearEditLine (int edit_line)
470 memset (key_line, '\0', sizeof(key_line));
477 Interactive line editing and console scrollback
481 static void Key_Console_SetCursor(size_t pos)
484 //Con_Printf("Position: %lu\n", pos);
488 Key_Console (int key, int unicode)
493 if (UIM_Key(key, unicode))
498 // LordHavoc: copied most of this from Q2 to improve keyboard handling
525 case K_KP_RIGHTARROW:
545 if ((key == 'v' && keydown[K_CTRL]) || ((key == K_INS || key == K_KP_INS) && keydown[K_SHIFT]))
548 if ((cbd = Sys_GetClipboardData()) != 0)
555 if (*p == '\r' && *(p+1) == '\n')
560 else if (*p == '\n' || *p == '\r' || *p == '\b')
565 strtok(cbd, "\n\r\b");
567 i = (int)strlen(cbd);
568 if (i + key_linepos >= MAX_INPUTLINE)
569 i= MAX_INPUTLINE - key_linepos - 1;
572 // terencehill: insert the clipboard text between the characters of the line
574 char *temp = (char *) Z_Malloc(MAX_INPUTLINE);
577 if ( key_linepos < (int)strlen(key_line) )
578 strlcpy(temp, key_line + key_linepos, (int)strlen(key_line) - key_linepos +1);
579 key_line[key_linepos] = 0;
580 strlcat(key_line, cbd, sizeof(key_line));
582 strlcat(key_line, temp, sizeof(key_line));
586 // blub: I'm changing this to use memmove() like the rest of the code does.
588 memmove(key_line + key_linepos + i, key_line + key_linepos, sizeof(key_line) - key_linepos - i);
589 memcpy(key_line + key_linepos, cbd, i);
591 //UIM_SetCursor(key_linepos);
598 if (key == 'l' && keydown[K_CTRL])
600 Cbuf_AddText ("clear\n");
604 if (key == 'u' && keydown[K_CTRL]) // like vi/readline ^u: delete currently edited line
610 //UIM_SetCursor(key_linepos);
614 if (key == 'q' && keydown[K_CTRL]) // like zsh ^q: push line to history, don't execute, and clear
621 //UIM_SetCursor(key_linepos);
625 if (key == K_ENTER || key == K_KP_ENTER)
627 Cbuf_AddText (key_line+1); // skip the ]
631 key_line[1] = 0; // EvilTypeGuy: null terminate
633 //UIM_CancelBuffer();
634 //uim_in_console = false;
635 // force an update, because the command may take some time
636 if (cls.state == ca_disconnected)
643 if(keydown[K_CTRL]) // append to the cvar its value
645 int cvar_len, cvar_str_len, chars_to_move;
647 char cvar[MAX_INPUTLINE];
648 const char *cvar_str;
650 // go to the start of the variable
653 k = key_line[key_linepos];
654 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
659 // save the variable name in cvar
660 for(cvar_len=0; (k = key_line[key_linepos + cvar_len]) != 0; cvar_len++)
662 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
668 //UIM_SetCursor(key_linepos);
673 // go to the end of the cvar
674 key_linepos += cvar_len;
676 // save the content of the variable in cvar_str
677 cvar_str = Cvar_VariableString(cvar);
678 cvar_str_len = strlen(cvar_str);
681 //UIM_SetCursor(key_linepos);
685 // insert space and cvar_str in key_line
686 chars_to_move = strlen(&key_line[key_linepos]);
687 if (key_linepos + 1 + cvar_str_len + chars_to_move < MAX_INPUTLINE)
690 memmove(&key_line[key_linepos + 1 + cvar_str_len], &key_line[key_linepos], chars_to_move);
691 key_line[key_linepos++] = ' ';
692 memcpy(&key_line[key_linepos], cvar_str, cvar_str_len);
693 key_linepos += cvar_str_len;
694 key_line[key_linepos + chars_to_move] = 0;
697 Con_Printf("Couldn't append cvar value, edit line too long.\n");
698 //UIM_SetCursor(key_linepos);
701 // Enhanced command completion
702 // by EvilTypeGuy eviltypeguy@qeradiant.com
703 // Thanks to Fett, Taniwha
704 Con_CompleteCommandLine();
708 // Advanced Console Editing by Radix radix@planetquake.com
709 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
711 // Enhanced by terencehill
713 // move cursor to the previous character
714 if (key == K_LEFTARROW || key == K_KP_LEFTARROW)
718 if(keydown[K_CTRL]) // move cursor to the previous word
724 if(pos) // skip all "; ' after the word
728 if (!(k == '\"' || k == ';' || k == ' ' || k == '\''))
736 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
739 key_linepos = pos + 1;
741 else if(keydown[K_SHIFT]) // move cursor to the previous character ignoring colors
745 pos = u8_prevbyte(key_line, key_linepos);
747 if(pos-1 > 0 && key_line[pos-1] == STRING_COLOR_TAG && isdigit(key_line[pos]))
749 else if(pos-4 > 0 && key_line[pos-4] == STRING_COLOR_TAG && key_line[pos-3] == STRING_COLOR_RGB_TAG_CHAR
750 && isxdigit(key_line[pos-2]) && isxdigit(key_line[pos-1]) && isxdigit(key_line[pos]))
754 if(pos-1 > 0 && key_line[pos-1] == STRING_COLOR_TAG && key_line[pos] == STRING_COLOR_TAG) // consider ^^ as a character
759 // we need to move to the beginning of the character when in a wide character:
760 u8_charidx(key_line, pos + 1, &inchar);
761 key_linepos = pos + 1 - inchar;
765 key_linepos = u8_prevbyte(key_line, key_linepos);
767 //UIM_SetCursor(key_linepos);
771 // delete char before cursor
772 if (key == K_BACKSPACE || (key == 'h' && keydown[K_CTRL]))
776 int newpos = u8_prevbyte(key_line, key_linepos);
777 strlcpy(key_line + newpos, key_line + key_linepos, sizeof(key_line) + 1 - key_linepos);
778 key_linepos = newpos;
780 //UIM_SetCursor(key_linepos);
784 // delete char on cursor
785 if (key == K_DEL || key == K_KP_DEL)
788 linelen = strlen(key_line);
789 if (key_linepos < (int)linelen)
790 memmove(key_line + key_linepos, key_line + key_linepos + u8_bytelen(key_line + key_linepos, 1), linelen - key_linepos);
795 // move cursor to the next character
796 if (key == K_RIGHTARROW || key == K_KP_RIGHTARROW)
798 if (key_linepos >= (int)strlen(key_line))
800 if(keydown[K_CTRL]) // move cursor to the next word
804 len = (int)strlen(key_line);
810 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
814 if (pos < len) // skip all "; ' after the word
818 if (!(k == '\"' || k == ';' || k == ' ' || k == '\''))
823 else if(keydown[K_SHIFT]) // move cursor to the next character ignoring colors
826 len = (int)strlen(key_line);
829 // go beyond all initial consecutive color tags, if any
831 while (key_line[pos] == STRING_COLOR_TAG)
833 if(isdigit(key_line[pos+1]))
835 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]))
842 if (key_line[pos] == STRING_COLOR_TAG && key_line[pos+1] == STRING_COLOR_TAG) // consider ^^ as a character
844 pos += u8_bytelen(key_line + pos, 1);
846 // now go beyond all next consecutive color tags, if any
848 while (key_line[pos] == STRING_COLOR_TAG)
850 if(isdigit(key_line[pos+1]))
852 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]))
860 key_linepos += u8_bytelen(key_line + key_linepos, 1);
861 //UIM_SetCursor(key_linepos);
865 if (key == K_INS || key == K_KP_INS) // toggle insert mode
871 // End Advanced Console Editing
873 if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL]))
876 //UIM_SetCursor(key_linepos);
880 if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL]))
883 //UIM_SetCursor(key_linepos);
886 // ~1.0795 = 82/76 using con_textsize 64 76 is height of the char, 6 is the distance between 2 lines
887 if (key == K_PGUP || key == K_KP_PGUP)
891 con_backscroll += ((vid_conheight.integer >> 2) / con_textsize.integer)-1;
894 con_backscroll += ((vid_conheight.integer >> 1) / con_textsize.integer)-3;
898 if (key == K_PGDN || key == K_KP_PGDN)
902 con_backscroll -= ((vid_conheight.integer >> 2) / con_textsize.integer)-1;
905 con_backscroll -= ((vid_conheight.integer >> 1) / con_textsize.integer)-3;
909 if (key == K_MWHEELUP)
913 else if(keydown[K_SHIFT])
914 con_backscroll += ((vid_conheight.integer >> 2) / con_textsize.integer)-1;
920 if (key == K_MWHEELDOWN)
924 else if(keydown[K_SHIFT])
925 con_backscroll -= ((vid_conheight.integer >> 2) / con_textsize.integer)-1;
934 if (key == '+' || key == K_KP_PLUS)
936 if (con_textsize.integer < 128)
937 Cvar_SetValueQuick(&con_textsize, con_textsize.integer + 1);
941 if (key == '-' || key == K_KP_MINUS)
943 if (con_textsize.integer > 1)
944 Cvar_SetValueQuick(&con_textsize, con_textsize.integer - 1);
948 if (key == '0' || key == K_KP_INS)
950 Cvar_SetValueQuick(&con_textsize, atoi(Cvar_VariableDefString("con_textsize")));
955 if (key == K_HOME || key == K_KP_HOME)
958 con_backscroll = CON_TEXTSIZE;
961 //UIM_SetCursor(key_linepos);
965 if (key == K_END || key == K_KP_END)
970 key_linepos = (int)strlen(key_line);
971 //UIM_SetCursor(key_linepos);
979 if (key_linepos < MAX_INPUTLINE-1)
984 blen = u8_fromchar(unicode, buf, sizeof(buf));
987 len = (int)strlen(&key_line[key_linepos]);
988 // check insert mode, or always insert if at end of line
989 if (key_insert || len == 0)
991 // can't use strcpy to move string to right
993 //memmove(&key_line[key_linepos + u8_bytelen(key_line + key_linepos, 1)], &key_line[key_linepos], len);
994 memmove(&key_line[key_linepos + blen], &key_line[key_linepos], len);
996 memcpy(key_line + key_linepos, buf, blen);
998 //UIM_SetCursor(key_linepos);
999 //key_linepos += u8_fromchar(unicode, key_line + key_linepos, sizeof(key_line) - key_linepos - 1);
1000 //key_line[key_linepos] = ascii;
1005 //============================================================================
1008 char chat_buffer[MAX_INPUTLINE];
1009 unsigned int chat_bufferlen = 0;
1011 extern int Nicks_CompleteChatLine(char *buffer, size_t size, unsigned int pos);
1014 Key_Message (int key, int ascii)
1018 if (UIM_Key(key, ascii))
1021 if (key >= K_F1 && key <= K_F12)
1024 if (key == K_ENTER || ascii == 10 || ascii == 13)
1027 Cmd_ExecuteString(chat_buffer, src_command); // not Cbuf_AddText to allow semiclons in args; however, this allows no variables then. Use aliases!
1029 Cmd_ForwardStringToServer(va("%s %s", chat_mode ? "say_team" : "say ", chat_buffer));
1031 key_dest = key_game;
1038 // TODO add support for arrow keys and simple editing
1039 if (key == K_ESCAPE) {
1041 key_dest = key_game;
1047 if (key == K_BACKSPACE) {
1048 if (chat_bufferlen) {
1049 chat_bufferlen = u8_prevbyte(chat_buffer, chat_bufferlen);
1050 UIM_SetCursor(chat_bufferlen);
1051 chat_buffer[chat_bufferlen] = 0;
1057 chat_bufferlen = Nicks_CompleteChatLine(chat_buffer, sizeof(chat_buffer), chat_bufferlen);
1058 UIM_SetCursor(chat_bufferlen);
1062 if (chat_bufferlen == sizeof (chat_buffer) - 1)
1063 return true; // all full
1066 return true; // non printable
1068 if (keydown[K_CTRL] || keydown[K_ALT]) // no evil trickery, yeah?
1071 chat_bufferlen += u8_fromchar(ascii, chat_buffer+chat_bufferlen, sizeof(chat_buffer) - chat_bufferlen - 1);
1072 UIM_SetCursor(chat_bufferlen);
1074 //chat_buffer[chat_bufferlen++] = ascii;
1075 //chat_buffer[chat_bufferlen] = 0;
1079 //============================================================================
1084 Returns a key number to be used to index keybindings[] by looking at
1085 the given string. Single ascii characters return themselves, while
1086 the K_* names are matched up.
1090 Key_StringToKeynum (const char *str)
1092 const keyname_t *kn;
1094 if (!str || !str[0])
1097 return tolower(str[0]);
1099 for (kn = keynames; kn->name; kn++) {
1100 if (!strcasecmp (str, kn->name))
1108 Returns a string (either a single ascii char, or a K_* name) for the
1110 FIXME: handle quote special (general escape sequence?)
1114 Key_KeynumToString (int keynum)
1116 const keyname_t *kn;
1117 static char tinystr[2];
1119 // -1 is an invalid code
1121 return "<KEY NOT FOUND>";
1123 // search overrides first, because some characters are special
1124 for (kn = keynames; kn->name; kn++)
1125 if (keynum == kn->keynum)
1128 // if it is printable, output it as a single character
1129 if (keynum > 32 && keynum < 256)
1131 tinystr[0] = keynum;
1136 // if it is not overridden and not printable, we don't know what to do with it
1137 return "<UNKNOWN KEYNUM>";
1142 Key_SetBinding (int keynum, int bindmap, const char *binding)
1147 if (keynum == -1 || keynum >= MAX_KEYS)
1150 // free old bindings
1151 if (keybindings[bindmap][keynum]) {
1152 Z_Free (keybindings[bindmap][keynum]);
1153 keybindings[bindmap][keynum] = NULL;
1155 if(!binding[0]) // make "" binds be removed --blub
1157 // allocate memory for new binding
1158 l = strlen (binding);
1159 newbinding = (char *)Z_Malloc (l + 1);
1160 memcpy (newbinding, binding, l + 1);
1162 keybindings[bindmap][keynum] = newbinding;
1166 Key_In_Unbind_f (void)
1169 char *errchar = NULL;
1171 if (Cmd_Argc () != 3) {
1172 Con_Print("in_unbind <bindmap> <key> : remove commands from a key\n");
1176 m = strtol(Cmd_Argv (1), &errchar, 0);
1177 if ((m < 0) || (m >= 8) || (errchar && *errchar)) {
1178 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(1));
1182 b = Key_StringToKeynum (Cmd_Argv (2));
1184 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
1188 Key_SetBinding (b, m, "");
1192 Key_In_Bind_f (void)
1195 char cmd[MAX_INPUTLINE];
1196 char *errchar = NULL;
1200 if (c != 3 && c != 4) {
1201 Con_Print("in_bind <bindmap> <key> [command] : attach a command to a key\n");
1205 m = strtol(Cmd_Argv (1), &errchar, 0);
1206 if ((m < 0) || (m >= 8) || (errchar && *errchar)) {
1207 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(1));
1211 b = Key_StringToKeynum (Cmd_Argv (2));
1212 if (b == -1 || b >= MAX_KEYS) {
1213 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
1218 if (keybindings[m][b])
1219 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (2), keybindings[m][b]);
1221 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (2));
1224 // copy the rest of the command line
1225 cmd[0] = 0; // start out with a null string
1226 for (i = 3; i < c; i++) {
1227 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
1229 strlcat (cmd, " ", sizeof (cmd));
1232 Key_SetBinding (b, m, cmd);
1236 Key_In_Bindmap_f (void)
1239 char *errchar = NULL;
1244 Con_Print("in_bindmap <bindmap> <fallback>: set current bindmap and fallback\n");
1248 m1 = strtol(Cmd_Argv (1), &errchar, 0);
1249 if ((m1 < 0) || (m1 >= 8) || (errchar && *errchar)) {
1250 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(1));
1254 m2 = strtol(Cmd_Argv (2), &errchar, 0);
1255 if ((m2 < 0) || (m2 >= 8) || (errchar && *errchar)) {
1256 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(2));
1269 if (Cmd_Argc () != 2) {
1270 Con_Print("unbind <key> : remove commands from a key\n");
1274 b = Key_StringToKeynum (Cmd_Argv (1));
1276 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
1280 Key_SetBinding (b, 0, "");
1284 Key_Unbindall_f (void)
1288 for (j = 0; j < 8; j++)
1289 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
1290 if (keybindings[j][i])
1291 Key_SetBinding (i, j, "");
1295 Key_PrintBindList(int j)
1297 char bindbuf[MAX_INPUTLINE];
1301 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
1303 p = keybindings[j][i];
1306 Cmd_QuoteString(bindbuf, sizeof(bindbuf), p, "\"\\");
1308 Con_Printf("^2%s ^7= \"%s\"\n", Key_KeynumToString (i), bindbuf);
1310 Con_Printf("^3bindmap %d: ^2%s ^7= \"%s\"\n", j, Key_KeynumToString (i), bindbuf);
1316 Key_In_BindList_f (void)
1319 char *errchar = NULL;
1323 m = strtol(Cmd_Argv(1), &errchar, 0);
1324 if ((m < 0) || (m >= 8) || (errchar && *errchar)) {
1325 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(1));
1328 Key_PrintBindList(m);
1332 for (m = 0; m < MAX_BINDMAPS; m++)
1333 Key_PrintBindList(m);
1338 Key_BindList_f (void)
1340 Key_PrintBindList(0);
1347 char cmd[MAX_INPUTLINE];
1351 if (c != 2 && c != 3) {
1352 Con_Print("bind <key> [command] : attach a command to a key\n");
1355 b = Key_StringToKeynum (Cmd_Argv (1));
1356 if (b == -1 || b >= MAX_KEYS) {
1357 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
1362 if (keybindings[0][b])
1363 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (1), keybindings[0][b]);
1365 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (1));
1368 // copy the rest of the command line
1369 cmd[0] = 0; // start out with a null string
1370 for (i = 2; i < c; i++) {
1371 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
1373 strlcat (cmd, " ", sizeof (cmd));
1376 Key_SetBinding (b, 0, cmd);
1381 Writes lines containing "bind key value"
1385 Key_WriteBindings (qfile_t *f)
1388 char bindbuf[MAX_INPUTLINE];
1391 for (j = 0; j < MAX_BINDMAPS; j++)
1393 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
1395 p = keybindings[j][i];
1398 Cmd_QuoteString(bindbuf, sizeof(bindbuf), p, "\"\\"); // don't need to escape $ because cvars are not expanded inside bind
1400 FS_Printf(f, "bind %s \"%s\"\n", Key_KeynumToString (i), bindbuf);
1402 FS_Printf(f, "in_bind %d %s \"%s\"\n", j, Key_KeynumToString (i), bindbuf);
1418 // register our functions
1420 Cmd_AddCommand ("in_bind", Key_In_Bind_f, "binds a command to the specified key in the selected bindmap");
1421 Cmd_AddCommand ("in_unbind", Key_In_Unbind_f, "removes command on the specified key in the selected bindmap");
1422 Cmd_AddCommand ("in_bindlist", Key_In_BindList_f, "bindlist: displays bound keys for all bindmaps, or the given bindmap");
1423 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");
1425 Cmd_AddCommand ("bind", Key_Bind_f, "binds a command to the specified key in bindmap 0");
1426 Cmd_AddCommand ("unbind", Key_Unbind_f, "removes a command on the specified key in bindmap 0");
1427 Cmd_AddCommand ("bindlist", Key_BindList_f, "bindlist: displays bound keys for bindmap 0 bindmaps");
1428 Cmd_AddCommand ("unbindall", Key_Unbindall_f, "removes all commands from all keys in all bindmaps (leaving only shift-escape and escape)");
1430 Cvar_RegisterVariable (&con_closeontoggleconsole);
1436 Key_History_Shutdown();
1439 const char *Key_GetBind (int key)
1442 if (key < 0 || key >= MAX_KEYS)
1444 bind = keybindings[key_bmap][key];
1446 bind = keybindings[key_bmap2][key];
1450 qboolean CL_VM_InputEvent (qboolean down, int key, int ascii);
1454 Called by the system between frames for both key up and key down events
1455 Should NOT be called during an interrupt!
1458 static char tbl_keyascii[MAX_KEYS];
1459 static keydest_t tbl_keydest[MAX_KEYS];
1462 Key_Event (int key, int ascii, qboolean down)
1466 keydest_t keydest = key_dest;
1468 if (key < 0 || key >= MAX_KEYS)
1472 bind = keybindings[key_bmap][key];
1474 bind = keybindings[key_bmap2][key];
1476 if (developer_insane.integer)
1477 Con_DPrintf("Key_Event(%i, '%c', %s) keydown %i bind \"%s\"\n", key, ascii ? ascii : '?', down ? "down" : "up", keydown[key], bind ? bind : "");
1479 if (key_consoleactive)
1482 if (!uim_in_console)
1484 UIM_EnterBuffer(key_line, sizeof(key_line), key_linepos, &Key_Console_SetCursor);
1485 uim_in_console = true;
1488 keydest = key_console;
1490 else if (uim_in_console)
1494 uim_in_console = false;
1500 // increment key repeat count each time a down is received so that things
1501 // which want to ignore key repeat can ignore it
1502 keydown[key] = min(keydown[key] + 1, 2);
1503 if(keydown[key] == 1) {
1504 tbl_keyascii[key] = ascii;
1505 tbl_keydest[key] = keydest;
1507 ascii = tbl_keyascii[key];
1508 keydest = tbl_keydest[key];
1513 // clear repeat count now that the key is released
1515 keydest = tbl_keydest[key];
1516 ascii = tbl_keyascii[key];
1519 if(keydest == key_void)
1522 // key_consoleactive is a flag not a key_dest because the console is a
1523 // high priority overlay ontop of the normal screen (designed as a safety
1524 // feature so that developers and users can rescue themselves from a bad
1527 // this also means that toggling the console on/off does not lose the old
1530 // specially handle escape (togglemenu) and shift-escape (toggleconsole)
1531 // engine bindings, these are not handled as normal binds so that the user
1532 // can recover from a completely empty bindmap
1533 if (key == K_ESCAPE)
1535 // ignore key repeats on escape
1536 if (keydown[key] > 1)
1539 // escape does these things:
1540 // key_consoleactive - close console
1541 // key_message - abort messagemode
1542 // key_menu - go to parent menu (or key_game)
1543 // key_game - open menu
1545 // in all modes shift-escape toggles console
1546 if (keydown[K_SHIFT])
1550 Con_ToggleConsole_f ();
1551 tbl_keydest[key] = key_void; // esc release should go nowhere (especially not to key_menu or key_game)
1561 if(key_consoleactive & KEY_CONSOLEACTIVE_FORCED)
1563 key_consoleactive &= ~KEY_CONSOLEACTIVE_USER;
1567 Con_ToggleConsole_f();
1573 Key_Message (key, ascii); // that'll close the message input
1577 case key_menu_grabbed:
1578 MR_KeyEvent (key, ascii, down);
1582 // csqc has priority over toggle menu if it wants to (e.g. handling escape for UI stuff in-game.. :sick:)
1583 q = CL_VM_InputEvent(down, key, ascii);
1589 Con_Printf ("Key_Event: Bad key_dest\n");
1594 // send function keydowns to interpreter no matter what mode is (unless the menu has specifically grabbed the keyboard, for rebinding keys)
1595 // NOTE: if UIM is active, let it handle function keys too:
1596 if (keydest == key_message && key >= K_F1 && key <= K_F12)
1599 Key_Message(key, ascii);
1602 if (keydest != key_menu_grabbed)
1603 if (key >= K_F1 && key <= K_F12)
1607 if(keydown[key] == 1 && down)
1609 // button commands add keynum as a parm
1611 Cbuf_AddText (va("%s %i\n", bind, key));
1614 Cbuf_AddText (bind);
1615 Cbuf_AddText ("\n");
1617 } else if(bind[0] == '+' && !down && keydown[key] == 0)
1618 Cbuf_AddText(va("-%s %i\n", bind + 1, key));
1623 // send input to console if it wants it
1624 if (keydest == key_console)
1628 // con_closeontoggleconsole enables toggleconsole keys to close the
1629 // console, as long as they are not the color prefix character
1630 // (special exemption for german keyboard layouts)
1631 if (con_closeontoggleconsole.integer && bind && !strncmp(bind, "toggleconsole", strlen("toggleconsole")) && (key_consoleactive & KEY_CONSOLEACTIVE_USER) && ascii != STRING_COLOR_TAG)
1633 Con_ToggleConsole_f ();
1636 Key_Console (key, ascii);
1640 // handle toggleconsole in menu too
1641 if (keydest == key_menu)
1643 if (down && con_closeontoggleconsole.integer && bind && !strncmp(bind, "toggleconsole", strlen("toggleconsole")) && ascii != STRING_COLOR_TAG)
1645 Con_ToggleConsole_f ();
1646 tbl_keydest[key] = key_void; // key release should go nowhere (especially not to key_menu or key_game)
1651 // ignore binds while a video is played, let the video system handle the key event
1652 if (cl_videoplaying)
1654 CL_Video_KeyEvent (key, ascii, keydown[key] != 0);
1658 // anything else is a key press into the game, chat line, or menu
1663 Key_Message (key, ascii);
1666 case key_menu_grabbed:
1667 MR_KeyEvent (key, ascii, down);
1670 q = CL_VM_InputEvent(down, key, ascii);
1671 // ignore key repeats on binds and only send the bind if the event hasnt been already processed by csqc
1674 if(keydown[key] == 1 && down)
1676 // button commands add keynum as a parm
1678 Cbuf_AddText (va("%s %i\n", bind, key));
1681 Cbuf_AddText (bind);
1682 Cbuf_AddText ("\n");
1684 } else if(bind[0] == '+' && !down && keydown[key] == 0)
1685 Cbuf_AddText(va("-%s %i\n", bind + 1, key));
1689 Con_Printf ("Key_Event: Bad key_dest\n");
1699 Key_ClearStates (void)
1701 memset(keydown, 0, sizeof(keydown));
1709 qboolean Key_IsPressed (int key)
1711 if (key < 0 || key >= (int)sizeof(keydown))
1713 // handle repeats differently?