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},
240 {"MOUSE1", K_MOUSE1},
242 {"MOUSE2", K_MOUSE2},
243 {"MOUSE3", K_MOUSE3},
244 {"MWHEELUP", K_MWHEELUP},
245 {"MWHEELDOWN", K_MWHEELDOWN},
246 {"MOUSE4", K_MOUSE4},
247 {"MOUSE5", K_MOUSE5},
248 {"MOUSE6", K_MOUSE6},
249 {"MOUSE7", K_MOUSE7},
250 {"MOUSE8", K_MOUSE8},
251 {"MOUSE9", K_MOUSE9},
252 {"MOUSE10", K_MOUSE10},
253 {"MOUSE11", K_MOUSE11},
254 {"MOUSE12", K_MOUSE12},
255 {"MOUSE13", K_MOUSE13},
256 {"MOUSE14", K_MOUSE14},
257 {"MOUSE15", K_MOUSE15},
258 {"MOUSE16", K_MOUSE16},
318 {"SEMICOLON", ';'}, // because a raw semicolon separates commands
322 {"APOSTROPHE", '\''},
323 {"BACKSLASH", '\\'}, // because a raw backslash is used for special characters
325 {"MIDINOTE0", K_MIDINOTE0},
326 {"MIDINOTE1", K_MIDINOTE1},
327 {"MIDINOTE2", K_MIDINOTE2},
328 {"MIDINOTE3", K_MIDINOTE3},
329 {"MIDINOTE4", K_MIDINOTE4},
330 {"MIDINOTE5", K_MIDINOTE5},
331 {"MIDINOTE6", K_MIDINOTE6},
332 {"MIDINOTE7", K_MIDINOTE7},
333 {"MIDINOTE8", K_MIDINOTE8},
334 {"MIDINOTE9", K_MIDINOTE9},
335 {"MIDINOTE10", K_MIDINOTE10},
336 {"MIDINOTE11", K_MIDINOTE11},
337 {"MIDINOTE12", K_MIDINOTE12},
338 {"MIDINOTE13", K_MIDINOTE13},
339 {"MIDINOTE14", K_MIDINOTE14},
340 {"MIDINOTE15", K_MIDINOTE15},
341 {"MIDINOTE16", K_MIDINOTE16},
342 {"MIDINOTE17", K_MIDINOTE17},
343 {"MIDINOTE18", K_MIDINOTE18},
344 {"MIDINOTE19", K_MIDINOTE19},
345 {"MIDINOTE20", K_MIDINOTE20},
346 {"MIDINOTE21", K_MIDINOTE21},
347 {"MIDINOTE22", K_MIDINOTE22},
348 {"MIDINOTE23", K_MIDINOTE23},
349 {"MIDINOTE24", K_MIDINOTE24},
350 {"MIDINOTE25", K_MIDINOTE25},
351 {"MIDINOTE26", K_MIDINOTE26},
352 {"MIDINOTE27", K_MIDINOTE27},
353 {"MIDINOTE28", K_MIDINOTE28},
354 {"MIDINOTE29", K_MIDINOTE29},
355 {"MIDINOTE30", K_MIDINOTE30},
356 {"MIDINOTE31", K_MIDINOTE31},
357 {"MIDINOTE32", K_MIDINOTE32},
358 {"MIDINOTE33", K_MIDINOTE33},
359 {"MIDINOTE34", K_MIDINOTE34},
360 {"MIDINOTE35", K_MIDINOTE35},
361 {"MIDINOTE36", K_MIDINOTE36},
362 {"MIDINOTE37", K_MIDINOTE37},
363 {"MIDINOTE38", K_MIDINOTE38},
364 {"MIDINOTE39", K_MIDINOTE39},
365 {"MIDINOTE40", K_MIDINOTE40},
366 {"MIDINOTE41", K_MIDINOTE41},
367 {"MIDINOTE42", K_MIDINOTE42},
368 {"MIDINOTE43", K_MIDINOTE43},
369 {"MIDINOTE44", K_MIDINOTE44},
370 {"MIDINOTE45", K_MIDINOTE45},
371 {"MIDINOTE46", K_MIDINOTE46},
372 {"MIDINOTE47", K_MIDINOTE47},
373 {"MIDINOTE48", K_MIDINOTE48},
374 {"MIDINOTE49", K_MIDINOTE49},
375 {"MIDINOTE50", K_MIDINOTE50},
376 {"MIDINOTE51", K_MIDINOTE51},
377 {"MIDINOTE52", K_MIDINOTE52},
378 {"MIDINOTE53", K_MIDINOTE53},
379 {"MIDINOTE54", K_MIDINOTE54},
380 {"MIDINOTE55", K_MIDINOTE55},
381 {"MIDINOTE56", K_MIDINOTE56},
382 {"MIDINOTE57", K_MIDINOTE57},
383 {"MIDINOTE58", K_MIDINOTE58},
384 {"MIDINOTE59", K_MIDINOTE59},
385 {"MIDINOTE60", K_MIDINOTE60},
386 {"MIDINOTE61", K_MIDINOTE61},
387 {"MIDINOTE62", K_MIDINOTE62},
388 {"MIDINOTE63", K_MIDINOTE63},
389 {"MIDINOTE64", K_MIDINOTE64},
390 {"MIDINOTE65", K_MIDINOTE65},
391 {"MIDINOTE66", K_MIDINOTE66},
392 {"MIDINOTE67", K_MIDINOTE67},
393 {"MIDINOTE68", K_MIDINOTE68},
394 {"MIDINOTE69", K_MIDINOTE69},
395 {"MIDINOTE70", K_MIDINOTE70},
396 {"MIDINOTE71", K_MIDINOTE71},
397 {"MIDINOTE72", K_MIDINOTE72},
398 {"MIDINOTE73", K_MIDINOTE73},
399 {"MIDINOTE74", K_MIDINOTE74},
400 {"MIDINOTE75", K_MIDINOTE75},
401 {"MIDINOTE76", K_MIDINOTE76},
402 {"MIDINOTE77", K_MIDINOTE77},
403 {"MIDINOTE78", K_MIDINOTE78},
404 {"MIDINOTE79", K_MIDINOTE79},
405 {"MIDINOTE80", K_MIDINOTE80},
406 {"MIDINOTE81", K_MIDINOTE81},
407 {"MIDINOTE82", K_MIDINOTE82},
408 {"MIDINOTE83", K_MIDINOTE83},
409 {"MIDINOTE84", K_MIDINOTE84},
410 {"MIDINOTE85", K_MIDINOTE85},
411 {"MIDINOTE86", K_MIDINOTE86},
412 {"MIDINOTE87", K_MIDINOTE87},
413 {"MIDINOTE88", K_MIDINOTE88},
414 {"MIDINOTE89", K_MIDINOTE89},
415 {"MIDINOTE90", K_MIDINOTE90},
416 {"MIDINOTE91", K_MIDINOTE91},
417 {"MIDINOTE92", K_MIDINOTE92},
418 {"MIDINOTE93", K_MIDINOTE93},
419 {"MIDINOTE94", K_MIDINOTE94},
420 {"MIDINOTE95", K_MIDINOTE95},
421 {"MIDINOTE96", K_MIDINOTE96},
422 {"MIDINOTE97", K_MIDINOTE97},
423 {"MIDINOTE98", K_MIDINOTE98},
424 {"MIDINOTE99", K_MIDINOTE99},
425 {"MIDINOTE100", K_MIDINOTE100},
426 {"MIDINOTE101", K_MIDINOTE101},
427 {"MIDINOTE102", K_MIDINOTE102},
428 {"MIDINOTE103", K_MIDINOTE103},
429 {"MIDINOTE104", K_MIDINOTE104},
430 {"MIDINOTE105", K_MIDINOTE105},
431 {"MIDINOTE106", K_MIDINOTE106},
432 {"MIDINOTE107", K_MIDINOTE107},
433 {"MIDINOTE108", K_MIDINOTE108},
434 {"MIDINOTE109", K_MIDINOTE109},
435 {"MIDINOTE110", K_MIDINOTE110},
436 {"MIDINOTE111", K_MIDINOTE111},
437 {"MIDINOTE112", K_MIDINOTE112},
438 {"MIDINOTE113", K_MIDINOTE113},
439 {"MIDINOTE114", K_MIDINOTE114},
440 {"MIDINOTE115", K_MIDINOTE115},
441 {"MIDINOTE116", K_MIDINOTE116},
442 {"MIDINOTE117", K_MIDINOTE117},
443 {"MIDINOTE118", K_MIDINOTE118},
444 {"MIDINOTE119", K_MIDINOTE119},
445 {"MIDINOTE120", K_MIDINOTE120},
446 {"MIDINOTE121", K_MIDINOTE121},
447 {"MIDINOTE122", K_MIDINOTE122},
448 {"MIDINOTE123", K_MIDINOTE123},
449 {"MIDINOTE124", K_MIDINOTE124},
450 {"MIDINOTE125", K_MIDINOTE125},
451 {"MIDINOTE126", K_MIDINOTE126},
452 {"MIDINOTE127", K_MIDINOTE127},
458 ==============================================================================
460 LINE TYPING INTO THE CONSOLE
462 ==============================================================================
466 Key_ClearEditLine (int edit_line)
468 memset (key_line, '\0', sizeof(key_line));
475 Interactive line editing and console scrollback
479 static void Key_Console_SetCursor(size_t pos)
482 //Con_Printf("Position: %lu\n", pos);
486 Key_Console (int key, int unicode)
491 if (UIM_Key(key, unicode))
496 // LordHavoc: copied most of this from Q2 to improve keyboard handling
523 case K_KP_RIGHTARROW:
543 if ((key == 'v' && keydown[K_CTRL]) || ((key == K_INS || key == K_KP_INS) && keydown[K_SHIFT]))
546 if ((cbd = Sys_GetClipboardData()) != 0)
553 if (*p == '\r' && *(p+1) == '\n')
558 else if (*p == '\n' || *p == '\r' || *p == '\b')
563 strtok(cbd, "\n\r\b");
565 i = (int)strlen(cbd);
566 if (i + key_linepos >= MAX_INPUTLINE)
567 i= MAX_INPUTLINE - key_linepos - 1;
570 // terencehill: insert the clipboard text between the characters of the line
572 char *temp = (char *) Z_Malloc(MAX_INPUTLINE);
575 if ( key_linepos < (int)strlen(key_line) )
576 strlcpy(temp, key_line + key_linepos, (int)strlen(key_line) - key_linepos +1);
577 key_line[key_linepos] = 0;
578 strlcat(key_line, cbd, sizeof(key_line));
580 strlcat(key_line, temp, sizeof(key_line));
584 // blub: I'm changing this to use memmove() like the rest of the code does.
586 memmove(key_line + key_linepos + i, key_line + key_linepos, sizeof(key_line) - key_linepos - i);
587 memcpy(key_line + key_linepos, cbd, i);
589 //UIM_SetCursor(key_linepos);
596 if (key == 'l' && keydown[K_CTRL])
598 Cbuf_AddText ("clear\n");
602 if (key == 'u' && keydown[K_CTRL]) // like vi/readline ^u: delete currently edited line
608 //UIM_SetCursor(key_linepos);
612 if (key == 'q' && keydown[K_CTRL]) // like zsh ^q: push line to history, don't execute, and clear
619 //UIM_SetCursor(key_linepos);
623 if (key == K_ENTER || key == K_KP_ENTER)
625 Cbuf_AddText (key_line+1); // skip the ]
629 key_line[1] = 0; // EvilTypeGuy: null terminate
631 //UIM_CancelBuffer();
632 //uim_in_console = false;
633 // force an update, because the command may take some time
634 if (cls.state == ca_disconnected)
641 if(keydown[K_CTRL]) // append to the cvar its value
643 int cvar_len, cvar_str_len, chars_to_move;
645 char cvar[MAX_INPUTLINE];
646 const char *cvar_str;
648 // go to the start of the variable
651 k = key_line[key_linepos];
652 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
657 // save the variable name in cvar
658 for(cvar_len=0; (k = key_line[key_linepos + cvar_len]) != 0; cvar_len++)
660 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
666 //UIM_SetCursor(key_linepos);
671 // go to the end of the cvar
672 key_linepos += cvar_len;
674 // save the content of the variable in cvar_str
675 cvar_str = Cvar_VariableString(cvar);
676 cvar_str_len = strlen(cvar_str);
679 //UIM_SetCursor(key_linepos);
683 // insert space and cvar_str in key_line
684 chars_to_move = strlen(&key_line[key_linepos]);
685 if (key_linepos + 1 + cvar_str_len + chars_to_move < MAX_INPUTLINE)
688 memmove(&key_line[key_linepos + 1 + cvar_str_len], &key_line[key_linepos], chars_to_move);
689 key_line[key_linepos++] = ' ';
690 memcpy(&key_line[key_linepos], cvar_str, cvar_str_len);
691 key_linepos += cvar_str_len;
692 key_line[key_linepos + chars_to_move] = 0;
695 Con_Printf("Couldn't append cvar value, edit line too long.\n");
696 //UIM_SetCursor(key_linepos);
699 // Enhanced command completion
700 // by EvilTypeGuy eviltypeguy@qeradiant.com
701 // Thanks to Fett, Taniwha
702 Con_CompleteCommandLine();
706 // Advanced Console Editing by Radix radix@planetquake.com
707 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
709 // Enhanced by terencehill
711 // move cursor to the previous character
712 if (key == K_LEFTARROW || key == K_KP_LEFTARROW)
716 if(keydown[K_CTRL]) // move cursor to the previous word
722 if(pos) // skip all "; ' after the word
726 if (!(k == '\"' || k == ';' || k == ' ' || k == '\''))
734 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
737 key_linepos = pos + 1;
739 else if(keydown[K_SHIFT]) // move cursor to the previous character ignoring colors
743 pos = u8_prevbyte(key_line, key_linepos);
745 if(pos-1 > 0 && key_line[pos-1] == STRING_COLOR_TAG && isdigit(key_line[pos]))
747 else if(pos-4 > 0 && key_line[pos-4] == STRING_COLOR_TAG && key_line[pos-3] == STRING_COLOR_RGB_TAG_CHAR
748 && isxdigit(key_line[pos-2]) && isxdigit(key_line[pos-1]) && isxdigit(key_line[pos]))
752 if(pos-1 > 0 && key_line[pos-1] == STRING_COLOR_TAG && key_line[pos] == STRING_COLOR_TAG) // consider ^^ as a character
757 // we need to move to the beginning of the character when in a wide character:
758 u8_charidx(key_line, pos + 1, &inchar);
759 key_linepos = pos + 1 - inchar;
763 key_linepos = u8_prevbyte(key_line, key_linepos);
765 //UIM_SetCursor(key_linepos);
769 // delete char before cursor
770 if (key == K_BACKSPACE || (key == 'h' && keydown[K_CTRL]))
774 int newpos = u8_prevbyte(key_line, key_linepos);
775 strlcpy(key_line + newpos, key_line + key_linepos, sizeof(key_line) + 1 - key_linepos);
776 key_linepos = newpos;
778 //UIM_SetCursor(key_linepos);
782 // delete char on cursor
783 if (key == K_DEL || key == K_KP_DEL)
786 linelen = strlen(key_line);
787 if (key_linepos < (int)linelen)
788 memmove(key_line + key_linepos, key_line + key_linepos + u8_bytelen(key_line + key_linepos, 1), linelen - key_linepos);
793 // move cursor to the next character
794 if (key == K_RIGHTARROW || key == K_KP_RIGHTARROW)
796 if (key_linepos >= (int)strlen(key_line))
798 if(keydown[K_CTRL]) // move cursor to the next word
802 len = (int)strlen(key_line);
808 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
812 if (pos < len) // skip all "; ' after the word
816 if (!(k == '\"' || k == ';' || k == ' ' || k == '\''))
821 else if(keydown[K_SHIFT]) // move cursor to the next character ignoring colors
824 len = (int)strlen(key_line);
827 // go beyond all initial consecutive color tags, if any
829 while (key_line[pos] == STRING_COLOR_TAG)
831 if(isdigit(key_line[pos+1]))
833 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]))
840 if (key_line[pos] == STRING_COLOR_TAG && key_line[pos+1] == STRING_COLOR_TAG) // consider ^^ as a character
842 pos += u8_bytelen(key_line + pos, 1);
844 // now go beyond all next consecutive color tags, if any
846 while (key_line[pos] == STRING_COLOR_TAG)
848 if(isdigit(key_line[pos+1]))
850 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]))
858 key_linepos += u8_bytelen(key_line + key_linepos, 1);
859 //UIM_SetCursor(key_linepos);
863 if (key == K_INS || key == K_KP_INS) // toggle insert mode
869 // End Advanced Console Editing
871 if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL]))
874 //UIM_SetCursor(key_linepos);
878 if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL]))
881 //UIM_SetCursor(key_linepos);
884 // ~1.0795 = 82/76 using con_textsize 64 76 is height of the char, 6 is the distance between 2 lines
885 if (key == K_PGUP || key == K_KP_PGUP)
889 con_backscroll += ((vid_conheight.integer >> 2) / con_textsize.integer)-1;
892 con_backscroll += ((vid_conheight.integer >> 1) / con_textsize.integer)-3;
896 if (key == K_PGDN || key == K_KP_PGDN)
900 con_backscroll -= ((vid_conheight.integer >> 2) / con_textsize.integer)-1;
903 con_backscroll -= ((vid_conheight.integer >> 1) / con_textsize.integer)-3;
907 if (key == K_MWHEELUP)
911 else if(keydown[K_SHIFT])
912 con_backscroll += ((vid_conheight.integer >> 2) / con_textsize.integer)-1;
918 if (key == K_MWHEELDOWN)
922 else if(keydown[K_SHIFT])
923 con_backscroll -= ((vid_conheight.integer >> 2) / con_textsize.integer)-1;
932 if (key == '+' || key == K_KP_PLUS)
934 if (con_textsize.integer < 128)
935 Cvar_SetValueQuick(&con_textsize, con_textsize.integer + 1);
939 if (key == '-' || key == K_KP_MINUS)
941 if (con_textsize.integer > 1)
942 Cvar_SetValueQuick(&con_textsize, con_textsize.integer - 1);
946 if (key == '0' || key == K_KP_INS)
948 Cvar_SetValueQuick(&con_textsize, atoi(Cvar_VariableDefString("con_textsize")));
953 if (key == K_HOME || key == K_KP_HOME)
956 con_backscroll = INT_MAX;
959 //UIM_SetCursor(key_linepos);
963 if (key == K_END || key == K_KP_END)
968 key_linepos = (int)strlen(key_line);
969 //UIM_SetCursor(key_linepos);
977 if (key_linepos < MAX_INPUTLINE-1)
982 blen = u8_fromchar(unicode, buf, sizeof(buf));
985 len = (int)strlen(&key_line[key_linepos]);
986 // check insert mode, or always insert if at end of line
987 if (key_insert || len == 0)
989 // can't use strcpy to move string to right
991 //memmove(&key_line[key_linepos + u8_bytelen(key_line + key_linepos, 1)], &key_line[key_linepos], len);
992 memmove(&key_line[key_linepos + blen], &key_line[key_linepos], len);
994 memcpy(key_line + key_linepos, buf, blen);
996 //UIM_SetCursor(key_linepos);
997 //key_linepos += u8_fromchar(unicode, key_line + key_linepos, sizeof(key_line) - key_linepos - 1);
998 //key_line[key_linepos] = ascii;
1003 //============================================================================
1006 char chat_buffer[MAX_INPUTLINE];
1007 unsigned int chat_bufferlen = 0;
1009 extern int Nicks_CompleteChatLine(char *buffer, size_t size, unsigned int pos);
1012 Key_Message (int key, int ascii)
1016 if (UIM_Key(key, ascii))
1019 if (key >= K_F1 && key <= K_F12)
1022 if (key == K_ENTER || ascii == 10 || ascii == 13)
1025 Cmd_ExecuteString(chat_buffer, src_command); // not Cbuf_AddText to allow semiclons in args; however, this allows no variables then. Use aliases!
1027 Cmd_ForwardStringToServer(va("%s %s", chat_mode ? "say_team" : "say ", chat_buffer));
1029 key_dest = key_game;
1036 // TODO add support for arrow keys and simple editing
1037 if (key == K_ESCAPE) {
1039 key_dest = key_game;
1045 if (key == K_BACKSPACE) {
1046 if (chat_bufferlen) {
1047 chat_bufferlen = u8_prevbyte(chat_buffer, chat_bufferlen);
1048 UIM_SetCursor(chat_bufferlen);
1049 chat_buffer[chat_bufferlen] = 0;
1055 chat_bufferlen = Nicks_CompleteChatLine(chat_buffer, sizeof(chat_buffer), chat_bufferlen);
1056 UIM_SetCursor(chat_bufferlen);
1060 if (chat_bufferlen == sizeof (chat_buffer) - 1)
1061 return true; // all full
1064 return true; // non printable
1066 if (keydown[K_CTRL] || keydown[K_ALT]) // no evil trickery, yeah?
1069 chat_bufferlen += u8_fromchar(ascii, chat_buffer+chat_bufferlen, sizeof(chat_buffer) - chat_bufferlen - 1);
1070 UIM_SetCursor(chat_bufferlen);
1072 //chat_buffer[chat_bufferlen++] = ascii;
1073 //chat_buffer[chat_bufferlen] = 0;
1077 //============================================================================
1082 Returns a key number to be used to index keybindings[] by looking at
1083 the given string. Single ascii characters return themselves, while
1084 the K_* names are matched up.
1088 Key_StringToKeynum (const char *str)
1090 const keyname_t *kn;
1092 if (!str || !str[0])
1095 return tolower(str[0]);
1097 for (kn = keynames; kn->name; kn++) {
1098 if (!strcasecmp (str, kn->name))
1106 Returns a string (either a single ascii char, or a K_* name) for the
1108 FIXME: handle quote special (general escape sequence?)
1112 Key_KeynumToString (int keynum)
1114 const keyname_t *kn;
1115 static char tinystr[2];
1117 // -1 is an invalid code
1119 return "<KEY NOT FOUND>";
1121 // search overrides first, because some characters are special
1122 for (kn = keynames; kn->name; kn++)
1123 if (keynum == kn->keynum)
1126 // if it is printable, output it as a single character
1127 if (keynum > 32 && keynum < 256)
1129 tinystr[0] = keynum;
1134 // if it is not overridden and not printable, we don't know what to do with it
1135 return "<UNKNOWN KEYNUM>";
1140 Key_SetBinding (int keynum, int bindmap, const char *binding)
1145 if (keynum == -1 || keynum >= MAX_KEYS)
1148 // free old bindings
1149 if (keybindings[bindmap][keynum]) {
1150 Z_Free (keybindings[bindmap][keynum]);
1151 keybindings[bindmap][keynum] = NULL;
1153 if(!binding[0]) // make "" binds be removed --blub
1155 // allocate memory for new binding
1156 l = strlen (binding);
1157 newbinding = (char *)Z_Malloc (l + 1);
1158 memcpy (newbinding, binding, l + 1);
1160 keybindings[bindmap][keynum] = newbinding;
1164 Key_In_Unbind_f (void)
1167 char *errchar = NULL;
1169 if (Cmd_Argc () != 3) {
1170 Con_Print("in_unbind <bindmap> <key> : remove commands from a key\n");
1174 m = strtol(Cmd_Argv (1), &errchar, 0);
1175 if ((m < 0) || (m >= 8) || (errchar && *errchar)) {
1176 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(1));
1180 b = Key_StringToKeynum (Cmd_Argv (2));
1182 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
1186 Key_SetBinding (b, m, "");
1190 Key_In_Bind_f (void)
1193 char cmd[MAX_INPUTLINE];
1194 char *errchar = NULL;
1198 if (c != 3 && c != 4) {
1199 Con_Print("in_bind <bindmap> <key> [command] : attach a command to a key\n");
1203 m = strtol(Cmd_Argv (1), &errchar, 0);
1204 if ((m < 0) || (m >= 8) || (errchar && *errchar)) {
1205 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(1));
1209 b = Key_StringToKeynum (Cmd_Argv (2));
1210 if (b == -1 || b >= MAX_KEYS) {
1211 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
1216 if (keybindings[m][b])
1217 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (2), keybindings[m][b]);
1219 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (2));
1222 // copy the rest of the command line
1223 cmd[0] = 0; // start out with a null string
1224 for (i = 3; i < c; i++) {
1225 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
1227 strlcat (cmd, " ", sizeof (cmd));
1230 Key_SetBinding (b, m, cmd);
1234 Key_In_Bindmap_f (void)
1237 char *errchar = NULL;
1242 Con_Print("in_bindmap <bindmap> <fallback>: set current bindmap and fallback\n");
1246 m1 = strtol(Cmd_Argv (1), &errchar, 0);
1247 if ((m1 < 0) || (m1 >= 8) || (errchar && *errchar)) {
1248 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(1));
1252 m2 = strtol(Cmd_Argv (2), &errchar, 0);
1253 if ((m2 < 0) || (m2 >= 8) || (errchar && *errchar)) {
1254 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(2));
1267 if (Cmd_Argc () != 2) {
1268 Con_Print("unbind <key> : remove commands from a key\n");
1272 b = Key_StringToKeynum (Cmd_Argv (1));
1274 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
1278 Key_SetBinding (b, 0, "");
1282 Key_Unbindall_f (void)
1286 for (j = 0; j < 8; j++)
1287 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
1288 if (keybindings[j][i])
1289 Key_SetBinding (i, j, "");
1293 Key_PrintBindList(int j)
1295 char bindbuf[MAX_INPUTLINE];
1299 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
1301 p = keybindings[j][i];
1304 Cmd_QuoteString(bindbuf, sizeof(bindbuf), p, "\"\\");
1306 Con_Printf("^2%s ^7= \"%s\"\n", Key_KeynumToString (i), bindbuf);
1308 Con_Printf("^3bindmap %d: ^2%s ^7= \"%s\"\n", j, Key_KeynumToString (i), bindbuf);
1314 Key_In_BindList_f (void)
1317 char *errchar = NULL;
1321 m = strtol(Cmd_Argv(1), &errchar, 0);
1322 if ((m < 0) || (m >= 8) || (errchar && *errchar)) {
1323 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(1));
1326 Key_PrintBindList(m);
1330 for (m = 0; m < MAX_BINDMAPS; m++)
1331 Key_PrintBindList(m);
1336 Key_BindList_f (void)
1338 Key_PrintBindList(0);
1345 char cmd[MAX_INPUTLINE];
1349 if (c != 2 && c != 3) {
1350 Con_Print("bind <key> [command] : attach a command to a key\n");
1353 b = Key_StringToKeynum (Cmd_Argv (1));
1354 if (b == -1 || b >= MAX_KEYS) {
1355 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
1360 if (keybindings[0][b])
1361 Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (1), keybindings[0][b]);
1363 Con_Printf("\"%s\" is not bound\n", Cmd_Argv (1));
1366 // copy the rest of the command line
1367 cmd[0] = 0; // start out with a null string
1368 for (i = 2; i < c; i++) {
1369 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
1371 strlcat (cmd, " ", sizeof (cmd));
1374 Key_SetBinding (b, 0, cmd);
1379 Writes lines containing "bind key value"
1383 Key_WriteBindings (qfile_t *f)
1386 char bindbuf[MAX_INPUTLINE];
1389 for (j = 0; j < MAX_BINDMAPS; j++)
1391 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
1393 p = keybindings[j][i];
1396 Cmd_QuoteString(bindbuf, sizeof(bindbuf), p, "\"\\"); // don't need to escape $ because cvars are not expanded inside bind
1398 FS_Printf(f, "bind %s \"%s\"\n", Key_KeynumToString (i), bindbuf);
1400 FS_Printf(f, "in_bind %d %s \"%s\"\n", j, Key_KeynumToString (i), bindbuf);
1416 // register our functions
1418 Cmd_AddCommand ("in_bind", Key_In_Bind_f, "binds a command to the specified key in the selected bindmap");
1419 Cmd_AddCommand ("in_unbind", Key_In_Unbind_f, "removes command on the specified key in the selected bindmap");
1420 Cmd_AddCommand ("in_bindlist", Key_In_BindList_f, "bindlist: displays bound keys for all bindmaps, or the given bindmap");
1421 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");
1423 Cmd_AddCommand ("bind", Key_Bind_f, "binds a command to the specified key in bindmap 0");
1424 Cmd_AddCommand ("unbind", Key_Unbind_f, "removes a command on the specified key in bindmap 0");
1425 Cmd_AddCommand ("bindlist", Key_BindList_f, "bindlist: displays bound keys for bindmap 0 bindmaps");
1426 Cmd_AddCommand ("unbindall", Key_Unbindall_f, "removes all commands from all keys in all bindmaps (leaving only shift-escape and escape)");
1428 Cvar_RegisterVariable (&con_closeontoggleconsole);
1434 Key_History_Shutdown();
1437 const char *Key_GetBind (int key)
1440 if (key < 0 || key >= MAX_KEYS)
1442 bind = keybindings[key_bmap][key];
1444 bind = keybindings[key_bmap2][key];
1448 qboolean CL_VM_InputEvent (qboolean down, int key, int ascii);
1452 Called by the system between frames for both key up and key down events
1453 Should NOT be called during an interrupt!
1456 static char tbl_keyascii[MAX_KEYS];
1457 static keydest_t tbl_keydest[MAX_KEYS];
1460 Key_Event (int key, int ascii, qboolean down)
1464 keydest_t keydest = key_dest;
1466 if (key < 0 || key >= MAX_KEYS)
1470 bind = keybindings[key_bmap][key];
1472 bind = keybindings[key_bmap2][key];
1474 if (developer.integer >= 1000)
1475 Con_Printf("Key_Event(%i, '%c', %s) keydown %i bind \"%s\"\n", key, ascii, down ? "down" : "up", keydown[key], bind ? bind : "");
1477 if (key_consoleactive)
1480 if (!uim_in_console)
1482 UIM_EnterBuffer(key_line, sizeof(key_line), key_linepos, &Key_Console_SetCursor);
1483 uim_in_console = true;
1486 keydest = key_console;
1488 else if (uim_in_console)
1492 uim_in_console = false;
1498 // increment key repeat count each time a down is received so that things
1499 // which want to ignore key repeat can ignore it
1500 keydown[key] = min(keydown[key] + 1, 2);
1501 if(keydown[key] == 1) {
1502 tbl_keyascii[key] = ascii;
1503 tbl_keydest[key] = keydest;
1505 ascii = tbl_keyascii[key];
1506 keydest = tbl_keydest[key];
1511 // clear repeat count now that the key is released
1513 keydest = tbl_keydest[key];
1514 ascii = tbl_keyascii[key];
1517 if(keydest == key_void)
1520 // key_consoleactive is a flag not a key_dest because the console is a
1521 // high priority overlay ontop of the normal screen (designed as a safety
1522 // feature so that developers and users can rescue themselves from a bad
1525 // this also means that toggling the console on/off does not lose the old
1528 // specially handle escape (togglemenu) and shift-escape (toggleconsole)
1529 // engine bindings, these are not handled as normal binds so that the user
1530 // can recover from a completely empty bindmap
1531 if (key == K_ESCAPE)
1533 // ignore key repeats on escape
1534 if (keydown[key] > 1)
1537 // escape does these things:
1538 // key_consoleactive - close console
1539 // key_message - abort messagemode
1540 // key_menu - go to parent menu (or key_game)
1541 // key_game - open menu
1543 // in all modes shift-escape toggles console
1544 if (keydown[K_SHIFT])
1548 Con_ToggleConsole_f ();
1549 tbl_keydest[key] = key_void; // esc release should go nowhere (especially not to key_menu or key_game)
1559 if(key_consoleactive & KEY_CONSOLEACTIVE_FORCED)
1561 key_consoleactive &= ~KEY_CONSOLEACTIVE_USER;
1565 Con_ToggleConsole_f();
1571 Key_Message (key, ascii); // that'll close the message input
1575 case key_menu_grabbed:
1576 MR_KeyEvent (key, ascii, down);
1580 // csqc has priority over toggle menu if it wants to (e.g. handling escape for UI stuff in-game.. :sick:)
1581 q = CL_VM_InputEvent(down, key, ascii);
1587 Con_Printf ("Key_Event: Bad key_dest\n");
1592 // send function keydowns to interpreter no matter what mode is (unless the menu has specifically grabbed the keyboard, for rebinding keys)
1593 // NOTE: if UIM is active, let it handle function keys too:
1594 if (keydest == key_message && key >= K_F1 && key <= K_F12)
1597 Key_Message(key, ascii);
1600 if (keydest != key_menu_grabbed)
1601 if (key >= K_F1 && key <= K_F12)
1605 if(keydown[key] == 1 && down)
1607 // button commands add keynum as a parm
1609 Cbuf_AddText (va("%s %i\n", bind, key));
1612 Cbuf_AddText (bind);
1613 Cbuf_AddText ("\n");
1615 } else if(bind[0] == '+' && !down && keydown[key] == 0)
1616 Cbuf_AddText(va("-%s %i\n", bind + 1, key));
1621 // send input to console if it wants it
1622 if (keydest == key_console)
1626 // con_closeontoggleconsole enables toggleconsole keys to close the
1627 // console, as long as they are not the color prefix character
1628 // (special exemption for german keyboard layouts)
1629 if (con_closeontoggleconsole.integer && bind && !strncmp(bind, "toggleconsole", strlen("toggleconsole")) && (key_consoleactive & KEY_CONSOLEACTIVE_USER) && ascii != STRING_COLOR_TAG)
1631 Con_ToggleConsole_f ();
1634 Key_Console (key, ascii);
1638 // handle toggleconsole in menu too
1639 if (keydest == key_menu)
1641 if (down && con_closeontoggleconsole.integer && bind && !strncmp(bind, "toggleconsole", strlen("toggleconsole")) && ascii != STRING_COLOR_TAG)
1643 Con_ToggleConsole_f ();
1644 tbl_keydest[key] = key_void; // key release should go nowhere (especially not to key_menu or key_game)
1649 // ignore binds while a video is played, let the video system handle the key event
1650 if (cl_videoplaying)
1652 CL_Video_KeyEvent (key, ascii, keydown[key] != 0);
1656 // anything else is a key press into the game, chat line, or menu
1661 Key_Message (key, ascii);
1664 case key_menu_grabbed:
1665 MR_KeyEvent (key, ascii, down);
1668 q = CL_VM_InputEvent(down, key, ascii);
1669 // ignore key repeats on binds and only send the bind if the event hasnt been already processed by csqc
1672 if(keydown[key] == 1 && down)
1674 // button commands add keynum as a parm
1676 Cbuf_AddText (va("%s %i\n", bind, key));
1679 Cbuf_AddText (bind);
1680 Cbuf_AddText ("\n");
1682 } else if(bind[0] == '+' && !down && keydown[key] == 0)
1683 Cbuf_AddText(va("-%s %i\n", bind + 1, key));
1687 Con_Printf ("Key_Event: Bad key_dest\n");
1697 Key_ClearStates (void)
1699 memset(keydown, 0, sizeof(keydown));
1707 qboolean Key_IsPressed (int key)
1709 if (key < 0 || key >= (int)sizeof(keydown))
1711 // handle repeats differently?