]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - keys.c
add a helper function to release all keys "properly"
[xonotic/darkplaces.git] / keys.c
1 /*
2         Copyright (C) 1996-1997  Id Software, Inc.
3
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.
8
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.
12
13         See the GNU General Public License for more details.
14
15         You should have received a copy of the GNU General Public License
16         along with this program; if not, write to:
17
18                 Free Software Foundation, Inc.
19                 59 Temple Place - Suite 330
20                 Boston, MA  02111-1307, USA
21 */
22
23 #include "quakedef.h"
24 #include "cl_video.h"
25 #include "utf8lib.h"
26
27 cvar_t con_closeontoggleconsole = {CVAR_SAVE, "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"};
28
29 /*
30 key up events are sent even if in console mode
31 */
32
33 char            key_line[MAX_INPUTLINE];
34 int                     key_linepos;
35 qboolean        key_insert = true;      // insert key toggle (for editing)
36 keydest_t       key_dest;
37 int                     key_consoleactive;
38 char            *keybindings[MAX_BINDMAPS][MAX_KEYS];
39
40 int                     history_line;
41 char            history_savedline[MAX_INPUTLINE];
42 char            history_searchstring[MAX_INPUTLINE];
43 qboolean        history_matchfound = false;
44 conbuffer_t history;
45
46 extern cvar_t   con_textsize;
47
48
49 static void Key_History_Init(void)
50 {
51         qfile_t *historyfile;
52         ConBuffer_Init(&history, HIST_TEXTSIZE, HIST_MAXLINES, zonemempool);
53
54         historyfile = FS_OpenRealFile("darkplaces_history.txt", "rb", false); // rb to handle unix line endings on windows too
55         if(historyfile)
56         {
57                 char buf[MAX_INPUTLINE];
58                 int bufpos;
59                 int c;
60
61                 bufpos = 0;
62                 for(;;)
63                 {
64                         c = FS_Getc(historyfile);
65                         if(c < 0 || c == 0 || c == '\r' || c == '\n')
66                         {
67                                 if(bufpos > 0)
68                                 {
69                                         buf[bufpos] = 0;
70                                         ConBuffer_AddLine(&history, buf, bufpos, 0);
71                                         bufpos = 0;
72                                 }
73                                 if(c < 0)
74                                         break;
75                         }
76                         else
77                         {
78                                 if(bufpos < MAX_INPUTLINE - 1)
79                                         buf[bufpos++] = c;
80                         }
81                 }
82
83                 FS_Close(historyfile);
84         }
85
86         history_line = -1;
87 }
88
89 static void Key_History_Shutdown(void)
90 {
91         // TODO write history to a file
92
93         qfile_t *historyfile = FS_OpenRealFile("darkplaces_history.txt", "w", false);
94         if(historyfile)
95         {
96                 int i;
97                 for(i = 0; i < CONBUFFER_LINES_COUNT(&history); ++i)
98                         FS_Printf(historyfile, "%s\n", ConBuffer_GetLine(&history, i));
99                 FS_Close(historyfile);
100         }
101
102         ConBuffer_Shutdown(&history);
103 }
104
105 static void Key_History_Push(void)
106 {
107         if(key_line[1]) // empty?
108         if(strcmp(key_line, "]quit")) // putting these into the history just sucks
109         if(strncmp(key_line, "]quit ", 6)) // putting these into the history just sucks
110                 ConBuffer_AddLine(&history, key_line + 1, strlen(key_line) - 1, 0);
111         Con_Printf("%s\n", key_line); // don't mark empty lines as history
112         history_line = -1;
113         if (history_matchfound)
114                 history_matchfound = false;
115 }
116
117 qboolean Key_History_Get_foundCommand(void)
118 {
119         if (!history_matchfound)
120                 return false;
121         strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
122         key_linepos = strlen(key_line);
123         history_matchfound = false;
124         return true;
125 }
126
127 static void Key_History_Up(void)
128 {
129         if(history_line == -1) // editing the "new" line
130                 strlcpy(history_savedline, key_line + 1, sizeof(history_savedline));
131
132         if (Key_History_Get_foundCommand())
133                 return;
134
135         if(history_line == -1)
136         {
137                 history_line = CONBUFFER_LINES_COUNT(&history) - 1;
138                 if(history_line != -1)
139                 {
140                         strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
141                         key_linepos = strlen(key_line);
142                 }
143         }
144         else if(history_line > 0)
145         {
146                 --history_line; // this also does -1 -> 0, so it is good
147                 strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
148                 key_linepos = strlen(key_line);
149         }
150 }
151
152 static void Key_History_Down(void)
153 {
154         if(history_line == -1) // editing the "new" line
155                 return;
156
157         if (Key_History_Get_foundCommand())
158                 return;
159
160         if(history_line < CONBUFFER_LINES_COUNT(&history) - 1)
161         {
162                 ++history_line;
163                 strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
164         }
165         else
166         {
167                 history_line = -1;
168                 strlcpy(key_line + 1, history_savedline, sizeof(key_line) - 1);
169         }
170
171         key_linepos = strlen(key_line);
172 }
173
174 static void Key_History_First(void)
175 {
176         if(history_line == -1) // editing the "new" line
177                 strlcpy(history_savedline, key_line + 1, sizeof(history_savedline));
178
179         if (CONBUFFER_LINES_COUNT(&history) > 0)
180         {
181                 history_line = 0;
182                 strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
183                 key_linepos = strlen(key_line);
184         }
185 }
186
187 static void Key_History_Last(void)
188 {
189         if(history_line == -1) // editing the "new" line
190                 strlcpy(history_savedline, key_line + 1, sizeof(history_savedline));
191
192         if (CONBUFFER_LINES_COUNT(&history) > 0)
193         {
194                 history_line = CONBUFFER_LINES_COUNT(&history) - 1;
195                 strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
196                 key_linepos = strlen(key_line);
197         }
198 }
199
200 static void Key_History_Find_Backwards(void)
201 {
202         int i;
203         const char *partial = key_line + 1;
204         size_t digits = strlen(va("%i", HIST_MAXLINES));
205
206         if (history_line == -1) // editing the "new" line
207                 strlcpy(history_savedline, key_line + 1, sizeof(history_savedline));
208
209         if (strcmp(key_line + 1, history_searchstring)) // different string? Start a new search
210         {
211                 strlcpy(history_searchstring, key_line + 1, sizeof(history_searchstring));
212                 i = CONBUFFER_LINES_COUNT(&history) - 1;
213         }
214         else if (history_line == -1)
215                 i = CONBUFFER_LINES_COUNT(&history) - 1;
216         else
217                 i = history_line - 1;
218
219         if (!*partial)
220                 partial = "*";
221         else if (!( strchr(partial, '*') || strchr(partial, '?') )) // no pattern?
222                 partial = va("*%s*", partial);
223
224         for ( ; i >= 0; i--)
225                 if (matchpattern_with_separator(ConBuffer_GetLine(&history, i), partial, true, "", false))
226                 {
227                         Con_Printf("^2%*i^7 %s\n", (int)digits, i+1, ConBuffer_GetLine(&history, i));
228                         history_line = i;
229                         history_matchfound = true;
230                         return;
231                 }
232 }
233
234 static void Key_History_Find_Forwards(void)
235 {
236         int i;
237         const char *partial = key_line + 1;
238         size_t digits = strlen(va("%i", HIST_MAXLINES));
239
240         if (history_line == -1) // editing the "new" line
241                 return;
242
243         if (strcmp(key_line + 1, history_searchstring)) // different string? Start a new search
244         {
245                 strlcpy(history_searchstring, key_line + 1, sizeof(history_searchstring));
246                 i = 0;
247         }
248         else i = history_line + 1;
249
250         if (!*partial)
251                 partial = "*";
252         else if (!( strchr(partial, '*') || strchr(partial, '?') )) // no pattern?
253                 partial = va("*%s*", partial);
254
255         for ( ; i < CONBUFFER_LINES_COUNT(&history); i++)
256                 if (matchpattern_with_separator(ConBuffer_GetLine(&history, i), partial, true, "", false))
257                 {
258                         Con_Printf("^2%*i^7 %s\n", (int)digits, i+1, ConBuffer_GetLine(&history, i));
259                         history_line = i;
260                         history_matchfound = true;
261                         return;
262                 }
263 }
264
265 static void Key_History_Find_All(void)
266 {
267         const char *partial = key_line + 1;
268         int i, count = 0;
269         size_t digits = strlen(va("%i", HIST_MAXLINES));
270         Con_Printf("History commands containing \"%s\":\n", key_line + 1);
271
272         if (!*partial)
273                 partial = "*";
274         else if (!( strchr(partial, '*') || strchr(partial, '?') )) // no pattern?
275                 partial = va("*%s*", partial);
276
277         for (i=0; i<CONBUFFER_LINES_COUNT(&history); i++)
278                 if (matchpattern_with_separator(ConBuffer_GetLine(&history, i), partial, true, "", false))
279                 {
280                         Con_Printf("%s%*i^7 %s\n", (i == history_line) ? "^2" : "^3", (int)digits, i+1, ConBuffer_GetLine(&history, i));
281                         count++;
282                 }
283         Con_Printf("%i result%s\n\n", count, (count != 1) ? "s" : "");
284 }
285
286 static void Key_History_f(void)
287 {
288         char *errchar = NULL;
289         int i = 0;
290         size_t digits = strlen(va("%i", HIST_MAXLINES));
291
292         if (Cmd_Argc () > 1)
293         {
294                 if (!strcmp(Cmd_Argv (1), "-c"))
295                 {
296                         ConBuffer_Clear(&history);
297                         return;
298                 }
299                 i = strtol(Cmd_Argv (1), &errchar, 0);
300                 if ((i < 0) || (i > CONBUFFER_LINES_COUNT(&history)) || (errchar && *errchar))
301                         i = 0;
302                 else
303                         i = CONBUFFER_LINES_COUNT(&history) - i;
304         }
305
306         for ( ; i<CONBUFFER_LINES_COUNT(&history); i++)
307                 Con_Printf("^3%*i^7 %s\n", (int)digits, i+1, ConBuffer_GetLine(&history, i));
308         Con_Printf("\n");
309 }
310
311 static int      key_bmap, key_bmap2;
312 static unsigned char keydown[MAX_KEYS]; // 0 = up, 1 = down, 2 = repeating
313
314 typedef struct keyname_s
315 {
316         const char      *name;
317         int                     keynum;
318 }
319 keyname_t;
320
321 static const keyname_t   keynames[] = {
322         {"TAB", K_TAB},
323         {"ENTER", K_ENTER},
324         {"ESCAPE", K_ESCAPE},
325         {"SPACE", K_SPACE},
326
327         // spacer so it lines up with keys.h
328
329         {"BACKSPACE", K_BACKSPACE},
330         {"UPARROW", K_UPARROW},
331         {"DOWNARROW", K_DOWNARROW},
332         {"LEFTARROW", K_LEFTARROW},
333         {"RIGHTARROW", K_RIGHTARROW},
334
335         {"ALT", K_ALT},
336         {"CTRL", K_CTRL},
337         {"SHIFT", K_SHIFT},
338
339         {"F1", K_F1},
340         {"F2", K_F2},
341         {"F3", K_F3},
342         {"F4", K_F4},
343         {"F5", K_F5},
344         {"F6", K_F6},
345         {"F7", K_F7},
346         {"F8", K_F8},
347         {"F9", K_F9},
348         {"F10", K_F10},
349         {"F11", K_F11},
350         {"F12", K_F12},
351
352         {"INS", K_INS},
353         {"DEL", K_DEL},
354         {"PGDN", K_PGDN},
355         {"PGUP", K_PGUP},
356         {"HOME", K_HOME},
357         {"END", K_END},
358
359         {"PAUSE", K_PAUSE},
360
361         {"NUMLOCK", K_NUMLOCK},
362         {"CAPSLOCK", K_CAPSLOCK},
363         {"SCROLLOCK", K_SCROLLOCK},
364
365         {"KP_INS",                      K_KP_INS },
366         {"KP_0", K_KP_0},
367         {"KP_END",                      K_KP_END },
368         {"KP_1", K_KP_1},
369         {"KP_DOWNARROW",        K_KP_DOWNARROW },
370         {"KP_2", K_KP_2},
371         {"KP_PGDN",                     K_KP_PGDN },
372         {"KP_3", K_KP_3},
373         {"KP_LEFTARROW",        K_KP_LEFTARROW },
374         {"KP_4", K_KP_4},
375         {"KP_5", K_KP_5},
376         {"KP_RIGHTARROW",       K_KP_RIGHTARROW },
377         {"KP_6", K_KP_6},
378         {"KP_HOME",                     K_KP_HOME },
379         {"KP_7", K_KP_7},
380         {"KP_UPARROW",          K_KP_UPARROW },
381         {"KP_8", K_KP_8},
382         {"KP_PGUP",                     K_KP_PGUP },
383         {"KP_9", K_KP_9},
384         {"KP_DEL",                      K_KP_DEL },
385         {"KP_PERIOD", K_KP_PERIOD},
386         {"KP_SLASH",            K_KP_SLASH },
387         {"KP_DIVIDE", K_KP_DIVIDE},
388         {"KP_MULTIPLY", K_KP_MULTIPLY},
389         {"KP_MINUS", K_KP_MINUS},
390         {"KP_PLUS", K_KP_PLUS},
391         {"KP_ENTER", K_KP_ENTER},
392         {"KP_EQUALS", K_KP_EQUALS},
393
394         {"PRINTSCREEN", K_PRINTSCREEN},
395
396
397
398         {"MOUSE1", K_MOUSE1},
399
400         {"MOUSE2", K_MOUSE2},
401         {"MOUSE3", K_MOUSE3},
402         {"MWHEELUP", K_MWHEELUP},
403         {"MWHEELDOWN", K_MWHEELDOWN},
404         {"MOUSE4", K_MOUSE4},
405         {"MOUSE5", K_MOUSE5},
406         {"MOUSE6", K_MOUSE6},
407         {"MOUSE7", K_MOUSE7},
408         {"MOUSE8", K_MOUSE8},
409         {"MOUSE9", K_MOUSE9},
410         {"MOUSE10", K_MOUSE10},
411         {"MOUSE11", K_MOUSE11},
412         {"MOUSE12", K_MOUSE12},
413         {"MOUSE13", K_MOUSE13},
414         {"MOUSE14", K_MOUSE14},
415         {"MOUSE15", K_MOUSE15},
416         {"MOUSE16", K_MOUSE16},
417
418
419
420
421         {"JOY1",  K_JOY1},
422         {"JOY2",  K_JOY2},
423         {"JOY3",  K_JOY3},
424         {"JOY4",  K_JOY4},
425         {"JOY5",  K_JOY5},
426         {"JOY6",  K_JOY6},
427         {"JOY7",  K_JOY7},
428         {"JOY8",  K_JOY8},
429         {"JOY9",  K_JOY9},
430         {"JOY10", K_JOY10},
431         {"JOY11", K_JOY11},
432         {"JOY12", K_JOY12},
433         {"JOY13", K_JOY13},
434         {"JOY14", K_JOY14},
435         {"JOY15", K_JOY15},
436         {"JOY16", K_JOY16},
437
438
439
440
441
442
443         {"AUX1", K_AUX1},
444         {"AUX2", K_AUX2},
445         {"AUX3", K_AUX3},
446         {"AUX4", K_AUX4},
447         {"AUX5", K_AUX5},
448         {"AUX6", K_AUX6},
449         {"AUX7", K_AUX7},
450         {"AUX8", K_AUX8},
451         {"AUX9", K_AUX9},
452         {"AUX10", K_AUX10},
453         {"AUX11", K_AUX11},
454         {"AUX12", K_AUX12},
455         {"AUX13", K_AUX13},
456         {"AUX14", K_AUX14},
457         {"AUX15", K_AUX15},
458         {"AUX16", K_AUX16},
459         {"AUX17", K_AUX17},
460         {"AUX18", K_AUX18},
461         {"AUX19", K_AUX19},
462         {"AUX20", K_AUX20},
463         {"AUX21", K_AUX21},
464         {"AUX22", K_AUX22},
465         {"AUX23", K_AUX23},
466         {"AUX24", K_AUX24},
467         {"AUX25", K_AUX25},
468         {"AUX26", K_AUX26},
469         {"AUX27", K_AUX27},
470         {"AUX28", K_AUX28},
471         {"AUX29", K_AUX29},
472         {"AUX30", K_AUX30},
473         {"AUX31", K_AUX31},
474         {"AUX32", K_AUX32},
475
476         {"X360_DPAD_UP", K_X360_DPAD_UP},
477         {"X360_DPAD_DOWN", K_X360_DPAD_DOWN},
478         {"X360_DPAD_LEFT", K_X360_DPAD_LEFT},
479         {"X360_DPAD_RIGHT", K_X360_DPAD_RIGHT},
480         {"X360_START", K_X360_START},
481         {"X360_BACK", K_X360_BACK},
482         {"X360_LEFT_THUMB", K_X360_LEFT_THUMB},
483         {"X360_RIGHT_THUMB", K_X360_RIGHT_THUMB},
484         {"X360_LEFT_SHOULDER", K_X360_LEFT_SHOULDER},
485         {"X360_RIGHT_SHOULDER", K_X360_RIGHT_SHOULDER},
486         {"X360_A", K_X360_A},
487         {"X360_B", K_X360_B},
488         {"X360_X", K_X360_X},
489         {"X360_Y", K_X360_Y},
490         {"X360_LEFT_TRIGGER", K_X360_LEFT_TRIGGER},
491         {"X360_RIGHT_TRIGGER", K_X360_RIGHT_TRIGGER},
492         {"X360_LEFT_THUMB_UP", K_X360_LEFT_THUMB_UP},
493         {"X360_LEFT_THUMB_DOWN", K_X360_LEFT_THUMB_DOWN},
494         {"X360_LEFT_THUMB_LEFT", K_X360_LEFT_THUMB_LEFT},
495         {"X360_LEFT_THUMB_RIGHT", K_X360_LEFT_THUMB_RIGHT},
496         {"X360_RIGHT_THUMB_UP", K_X360_RIGHT_THUMB_UP},
497         {"X360_RIGHT_THUMB_DOWN", K_X360_RIGHT_THUMB_DOWN},
498         {"X360_RIGHT_THUMB_LEFT", K_X360_RIGHT_THUMB_LEFT},
499         {"X360_RIGHT_THUMB_RIGHT", K_X360_RIGHT_THUMB_RIGHT},
500
501         {"JOY_UP", K_JOY_UP},
502         {"JOY_DOWN", K_JOY_DOWN},
503         {"JOY_LEFT", K_JOY_LEFT},
504         {"JOY_RIGHT", K_JOY_RIGHT},
505
506         {"SEMICOLON", ';'},                     // because a raw semicolon separates commands
507         {"TILDE", '~'},
508         {"BACKQUOTE", '`'},
509         {"QUOTE", '"'},
510         {"APOSTROPHE", '\''},
511         {"BACKSLASH", '\\'},            // because a raw backslash is used for special characters
512
513         {"MIDINOTE0", K_MIDINOTE0},
514         {"MIDINOTE1", K_MIDINOTE1},
515         {"MIDINOTE2", K_MIDINOTE2},
516         {"MIDINOTE3", K_MIDINOTE3},
517         {"MIDINOTE4", K_MIDINOTE4},
518         {"MIDINOTE5", K_MIDINOTE5},
519         {"MIDINOTE6", K_MIDINOTE6},
520         {"MIDINOTE7", K_MIDINOTE7},
521         {"MIDINOTE8", K_MIDINOTE8},
522         {"MIDINOTE9", K_MIDINOTE9},
523         {"MIDINOTE10", K_MIDINOTE10},
524         {"MIDINOTE11", K_MIDINOTE11},
525         {"MIDINOTE12", K_MIDINOTE12},
526         {"MIDINOTE13", K_MIDINOTE13},
527         {"MIDINOTE14", K_MIDINOTE14},
528         {"MIDINOTE15", K_MIDINOTE15},
529         {"MIDINOTE16", K_MIDINOTE16},
530         {"MIDINOTE17", K_MIDINOTE17},
531         {"MIDINOTE18", K_MIDINOTE18},
532         {"MIDINOTE19", K_MIDINOTE19},
533         {"MIDINOTE20", K_MIDINOTE20},
534         {"MIDINOTE21", K_MIDINOTE21},
535         {"MIDINOTE22", K_MIDINOTE22},
536         {"MIDINOTE23", K_MIDINOTE23},
537         {"MIDINOTE24", K_MIDINOTE24},
538         {"MIDINOTE25", K_MIDINOTE25},
539         {"MIDINOTE26", K_MIDINOTE26},
540         {"MIDINOTE27", K_MIDINOTE27},
541         {"MIDINOTE28", K_MIDINOTE28},
542         {"MIDINOTE29", K_MIDINOTE29},
543         {"MIDINOTE30", K_MIDINOTE30},
544         {"MIDINOTE31", K_MIDINOTE31},
545         {"MIDINOTE32", K_MIDINOTE32},
546         {"MIDINOTE33", K_MIDINOTE33},
547         {"MIDINOTE34", K_MIDINOTE34},
548         {"MIDINOTE35", K_MIDINOTE35},
549         {"MIDINOTE36", K_MIDINOTE36},
550         {"MIDINOTE37", K_MIDINOTE37},
551         {"MIDINOTE38", K_MIDINOTE38},
552         {"MIDINOTE39", K_MIDINOTE39},
553         {"MIDINOTE40", K_MIDINOTE40},
554         {"MIDINOTE41", K_MIDINOTE41},
555         {"MIDINOTE42", K_MIDINOTE42},
556         {"MIDINOTE43", K_MIDINOTE43},
557         {"MIDINOTE44", K_MIDINOTE44},
558         {"MIDINOTE45", K_MIDINOTE45},
559         {"MIDINOTE46", K_MIDINOTE46},
560         {"MIDINOTE47", K_MIDINOTE47},
561         {"MIDINOTE48", K_MIDINOTE48},
562         {"MIDINOTE49", K_MIDINOTE49},
563         {"MIDINOTE50", K_MIDINOTE50},
564         {"MIDINOTE51", K_MIDINOTE51},
565         {"MIDINOTE52", K_MIDINOTE52},
566         {"MIDINOTE53", K_MIDINOTE53},
567         {"MIDINOTE54", K_MIDINOTE54},
568         {"MIDINOTE55", K_MIDINOTE55},
569         {"MIDINOTE56", K_MIDINOTE56},
570         {"MIDINOTE57", K_MIDINOTE57},
571         {"MIDINOTE58", K_MIDINOTE58},
572         {"MIDINOTE59", K_MIDINOTE59},
573         {"MIDINOTE60", K_MIDINOTE60},
574         {"MIDINOTE61", K_MIDINOTE61},
575         {"MIDINOTE62", K_MIDINOTE62},
576         {"MIDINOTE63", K_MIDINOTE63},
577         {"MIDINOTE64", K_MIDINOTE64},
578         {"MIDINOTE65", K_MIDINOTE65},
579         {"MIDINOTE66", K_MIDINOTE66},
580         {"MIDINOTE67", K_MIDINOTE67},
581         {"MIDINOTE68", K_MIDINOTE68},
582         {"MIDINOTE69", K_MIDINOTE69},
583         {"MIDINOTE70", K_MIDINOTE70},
584         {"MIDINOTE71", K_MIDINOTE71},
585         {"MIDINOTE72", K_MIDINOTE72},
586         {"MIDINOTE73", K_MIDINOTE73},
587         {"MIDINOTE74", K_MIDINOTE74},
588         {"MIDINOTE75", K_MIDINOTE75},
589         {"MIDINOTE76", K_MIDINOTE76},
590         {"MIDINOTE77", K_MIDINOTE77},
591         {"MIDINOTE78", K_MIDINOTE78},
592         {"MIDINOTE79", K_MIDINOTE79},
593         {"MIDINOTE80", K_MIDINOTE80},
594         {"MIDINOTE81", K_MIDINOTE81},
595         {"MIDINOTE82", K_MIDINOTE82},
596         {"MIDINOTE83", K_MIDINOTE83},
597         {"MIDINOTE84", K_MIDINOTE84},
598         {"MIDINOTE85", K_MIDINOTE85},
599         {"MIDINOTE86", K_MIDINOTE86},
600         {"MIDINOTE87", K_MIDINOTE87},
601         {"MIDINOTE88", K_MIDINOTE88},
602         {"MIDINOTE89", K_MIDINOTE89},
603         {"MIDINOTE90", K_MIDINOTE90},
604         {"MIDINOTE91", K_MIDINOTE91},
605         {"MIDINOTE92", K_MIDINOTE92},
606         {"MIDINOTE93", K_MIDINOTE93},
607         {"MIDINOTE94", K_MIDINOTE94},
608         {"MIDINOTE95", K_MIDINOTE95},
609         {"MIDINOTE96", K_MIDINOTE96},
610         {"MIDINOTE97", K_MIDINOTE97},
611         {"MIDINOTE98", K_MIDINOTE98},
612         {"MIDINOTE99", K_MIDINOTE99},
613         {"MIDINOTE100", K_MIDINOTE100},
614         {"MIDINOTE101", K_MIDINOTE101},
615         {"MIDINOTE102", K_MIDINOTE102},
616         {"MIDINOTE103", K_MIDINOTE103},
617         {"MIDINOTE104", K_MIDINOTE104},
618         {"MIDINOTE105", K_MIDINOTE105},
619         {"MIDINOTE106", K_MIDINOTE106},
620         {"MIDINOTE107", K_MIDINOTE107},
621         {"MIDINOTE108", K_MIDINOTE108},
622         {"MIDINOTE109", K_MIDINOTE109},
623         {"MIDINOTE110", K_MIDINOTE110},
624         {"MIDINOTE111", K_MIDINOTE111},
625         {"MIDINOTE112", K_MIDINOTE112},
626         {"MIDINOTE113", K_MIDINOTE113},
627         {"MIDINOTE114", K_MIDINOTE114},
628         {"MIDINOTE115", K_MIDINOTE115},
629         {"MIDINOTE116", K_MIDINOTE116},
630         {"MIDINOTE117", K_MIDINOTE117},
631         {"MIDINOTE118", K_MIDINOTE118},
632         {"MIDINOTE119", K_MIDINOTE119},
633         {"MIDINOTE120", K_MIDINOTE120},
634         {"MIDINOTE121", K_MIDINOTE121},
635         {"MIDINOTE122", K_MIDINOTE122},
636         {"MIDINOTE123", K_MIDINOTE123},
637         {"MIDINOTE124", K_MIDINOTE124},
638         {"MIDINOTE125", K_MIDINOTE125},
639         {"MIDINOTE126", K_MIDINOTE126},
640         {"MIDINOTE127", K_MIDINOTE127},
641
642         {NULL, 0}
643 };
644
645 /*
646 ==============================================================================
647
648                         LINE TYPING INTO THE CONSOLE
649
650 ==============================================================================
651 */
652
653 void
654 Key_ClearEditLine (int edit_line)
655 {
656         memset (key_line, '\0', sizeof(key_line));
657         key_line[0] = ']';
658         key_linepos = 1;
659 }
660
661 /*
662 ====================
663 Interactive line editing and console scrollback
664 ====================
665 */
666 static void
667 Key_Console (int key, int unicode)
668 {
669         // LordHavoc: copied most of this from Q2 to improve keyboard handling
670         switch (key)
671         {
672         case K_KP_SLASH:
673                 key = '/';
674                 break;
675         case K_KP_MINUS:
676                 key = '-';
677                 break;
678         case K_KP_PLUS:
679                 key = '+';
680                 break;
681         case K_KP_HOME:
682                 key = '7';
683                 break;
684         case K_KP_UPARROW:
685                 key = '8';
686                 break;
687         case K_KP_PGUP:
688                 key = '9';
689                 break;
690         case K_KP_LEFTARROW:
691                 key = '4';
692                 break;
693         case K_KP_5:
694                 key = '5';
695                 break;
696         case K_KP_RIGHTARROW:
697                 key = '6';
698                 break;
699         case K_KP_END:
700                 key = '1';
701                 break;
702         case K_KP_DOWNARROW:
703                 key = '2';
704                 break;
705         case K_KP_PGDN:
706                 key = '3';
707                 break;
708         case K_KP_INS:
709                 key = '0';
710                 break;
711         case K_KP_DEL:
712                 key = '.';
713                 break;
714         }
715
716         if ((key == 'v' && keydown[K_CTRL]) || ((key == K_INS || key == K_KP_INS) && keydown[K_SHIFT]))
717         {
718                 char *cbd, *p;
719                 if ((cbd = Sys_GetClipboardData()) != 0)
720                 {
721                         int i;
722 #if 1
723                         p = cbd;
724                         while (*p)
725                         {
726                                 if (*p == '\r' && *(p+1) == '\n')
727                                 {
728                                         *p++ = ';';
729                                         *p++ = ' ';
730                                 }
731                                 else if (*p == '\n' || *p == '\r' || *p == '\b')
732                                         *p++ = ';';
733                                 p++;
734                         }
735 #else
736                         strtok(cbd, "\n\r\b");
737 #endif
738                         i = (int)strlen(cbd);
739                         if (i + key_linepos >= MAX_INPUTLINE)
740                                 i= MAX_INPUTLINE - key_linepos - 1;
741                         if (i > 0)
742                         {
743                                 // terencehill: insert the clipboard text between the characters of the line
744                                 /*
745                                 char *temp = (char *) Z_Malloc(MAX_INPUTLINE);
746                                 cbd[i]=0;
747                                 temp[0]=0;
748                                 if ( key_linepos < (int)strlen(key_line) )
749                                         strlcpy(temp, key_line + key_linepos, (int)strlen(key_line) - key_linepos +1);
750                                 key_line[key_linepos] = 0;
751                                 strlcat(key_line, cbd, sizeof(key_line));
752                                 if (temp[0])
753                                         strlcat(key_line, temp, sizeof(key_line));
754                                 Z_Free(temp);
755                                 key_linepos += i;
756                                 */
757                                 // blub: I'm changing this to use memmove() like the rest of the code does.
758                                 cbd[i] = 0;
759                                 memmove(key_line + key_linepos + i, key_line + key_linepos, sizeof(key_line) - key_linepos - i);
760                                 memcpy(key_line + key_linepos, cbd, i);
761                                 key_linepos += i;
762                         }
763                         Z_Free(cbd);
764                 }
765                 return;
766         }
767
768         if (key == 'l' && keydown[K_CTRL])
769         {
770                 Cbuf_AddText ("clear\n");
771                 return;
772         }
773
774         if (key == 'u' && keydown[K_CTRL]) // like vi/readline ^u: delete currently edited line
775         {
776                 // clear line
777                 key_line[0] = ']';
778                 key_line[1] = 0;
779                 key_linepos = 1;
780                 return;
781         }
782
783         if (key == 'q' && keydown[K_CTRL]) // like zsh ^q: push line to history, don't execute, and clear
784         {
785                 // clear line
786                 Key_History_Push();
787                 key_line[0] = ']';
788                 key_line[1] = 0;
789                 key_linepos = 1;
790                 return;
791         }
792
793         if (key == K_ENTER || key == K_KP_ENTER)
794         {
795                 Cbuf_AddText (key_line+1);      // skip the ]
796                 Cbuf_AddText ("\n");
797                 Key_History_Push();
798                 key_line[0] = ']';
799                 key_line[1] = 0;        // EvilTypeGuy: null terminate
800                 key_linepos = 1;
801                 // force an update, because the command may take some time
802                 if (cls.state == ca_disconnected)
803                         CL_UpdateScreen ();
804                 return;
805         }
806
807         if (key == K_TAB)
808         {
809                 if(keydown[K_CTRL]) // append to the cvar its value
810                 {
811                         int             cvar_len, cvar_str_len, chars_to_move;
812                         char    k;
813                         char    cvar[MAX_INPUTLINE];
814                         const char *cvar_str;
815                         
816                         // go to the start of the variable
817                         while(--key_linepos)
818                         {
819                                 k = key_line[key_linepos];
820                                 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
821                                         break;
822                         }
823                         key_linepos++;
824                         
825                         // save the variable name in cvar
826                         for(cvar_len=0; (k = key_line[key_linepos + cvar_len]) != 0; cvar_len++)
827                         {
828                                 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
829                                         break;
830                                 cvar[cvar_len] = k;
831                         }
832                         if (cvar_len==0)
833                                 return;
834                         cvar[cvar_len] = 0;
835                         
836                         // go to the end of the cvar
837                         key_linepos += cvar_len;
838                         
839                         // save the content of the variable in cvar_str
840                         cvar_str = Cvar_VariableString(cvar);
841                         cvar_str_len = strlen(cvar_str);
842                         if (cvar_str_len==0)
843                                 return;
844                         
845                         // insert space and cvar_str in key_line
846                         chars_to_move = strlen(&key_line[key_linepos]);
847                         if (key_linepos + 1 + cvar_str_len + chars_to_move < MAX_INPUTLINE)
848                         {
849                                 if (chars_to_move)
850                                         memmove(&key_line[key_linepos + 1 + cvar_str_len], &key_line[key_linepos], chars_to_move);
851                                 key_line[key_linepos++] = ' ';
852                                 memcpy(&key_line[key_linepos], cvar_str, cvar_str_len);
853                                 key_linepos += cvar_str_len;
854                                 key_line[key_linepos + chars_to_move] = 0;
855                         }
856                         else
857                                 Con_Printf("Couldn't append cvar value, edit line too long.\n");
858                         return;
859                 }
860                 // Enhanced command completion
861                 // by EvilTypeGuy eviltypeguy@qeradiant.com
862                 // Thanks to Fett, Taniwha
863                 Con_CompleteCommandLine();
864                 return;
865         }
866
867         // Advanced Console Editing by Radix radix@planetquake.com
868         // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
869         // Enhanced by [515]
870         // Enhanced by terencehill
871
872         // move cursor to the previous character
873         if (key == K_LEFTARROW || key == K_KP_LEFTARROW)
874         {
875                 if (key_linepos < 2)
876                         return;
877                 if(keydown[K_CTRL]) // move cursor to the previous word
878                 {
879                         int             pos;
880                         char    k;
881                         pos = key_linepos-1;
882
883                         if(pos) // skip all "; ' after the word
884                                 while(--pos)
885                                 {
886                                         k = key_line[pos];
887                                         if (!(k == '\"' || k == ';' || k == ' ' || k == '\''))
888                                                 break;
889                                 }
890
891                         if(pos)
892                                 while(--pos)
893                                 {
894                                         k = key_line[pos];
895                                         if(k == '\"' || k == ';' || k == ' ' || k == '\'')
896                                                 break;
897                                 }
898                         key_linepos = pos + 1;
899                 }
900                 else if(keydown[K_SHIFT]) // move cursor to the previous character ignoring colors
901                 {
902                         int             pos;
903                         size_t          inchar = 0;
904                         pos = u8_prevbyte(key_line+1, key_linepos-1) + 1; // do NOT give the ']' to u8_prevbyte
905                         while (pos)
906                                 if(pos-1 > 0 && key_line[pos-1] == STRING_COLOR_TAG && isdigit(key_line[pos]))
907                                         pos-=2;
908                                 else if(pos-4 > 0 && key_line[pos-4] == STRING_COLOR_TAG && key_line[pos-3] == STRING_COLOR_RGB_TAG_CHAR
909                                                 && isxdigit(key_line[pos-2]) && isxdigit(key_line[pos-1]) && isxdigit(key_line[pos]))
910                                         pos-=5;
911                                 else
912                                 {
913                                         if(pos-1 > 0 && key_line[pos-1] == STRING_COLOR_TAG && key_line[pos] == STRING_COLOR_TAG) // consider ^^ as a character
914                                                 pos--;
915                                         pos--;
916                                         break;
917                                 }
918                         // we need to move to the beginning of the character when in a wide character:
919                         u8_charidx(key_line, pos + 1, &inchar);
920                         key_linepos = pos + 1 - inchar;
921                 }
922                 else
923                 {
924                         key_linepos = u8_prevbyte(key_line+1, key_linepos-1) + 1; // do NOT give the ']' to u8_prevbyte
925                 }
926                 return;
927         }
928
929         // delete char before cursor
930         if (key == K_BACKSPACE || (key == 'h' && keydown[K_CTRL]))
931         {
932                 if (key_linepos > 1)
933                 {
934                         int newpos = u8_prevbyte(key_line+1, key_linepos-1) + 1; // do NOT give the ']' to u8_prevbyte
935                         strlcpy(key_line + newpos, key_line + key_linepos, sizeof(key_line) + 1 - key_linepos);
936                         key_linepos = newpos;
937                 }
938                 return;
939         }
940
941         // delete char on cursor
942         if (key == K_DEL || key == K_KP_DEL)
943         {
944                 size_t linelen;
945                 linelen = strlen(key_line);
946                 if (key_linepos < (int)linelen)
947                         memmove(key_line + key_linepos, key_line + key_linepos + u8_bytelen(key_line + key_linepos, 1), linelen - key_linepos);
948                 return;
949         }
950
951
952         // move cursor to the next character
953         if (key == K_RIGHTARROW || key == K_KP_RIGHTARROW)
954         {
955                 if (key_linepos >= (int)strlen(key_line))
956                         return;
957                 if(keydown[K_CTRL]) // move cursor to the next word
958                 {
959                         int             pos, len;
960                         char    k;
961                         len = (int)strlen(key_line);
962                         pos = key_linepos;
963
964                         while(++pos < len)
965                         {
966                                 k = key_line[pos];
967                                 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
968                                         break;
969                         }
970                         
971                         if (pos < len) // skip all "; ' after the word
972                                 while(++pos < len)
973                                 {
974                                         k = key_line[pos];
975                                         if (!(k == '\"' || k == ';' || k == ' ' || k == '\''))
976                                                 break;
977                                 }
978                         key_linepos = pos;
979                 }
980                 else if(keydown[K_SHIFT]) // move cursor to the next character ignoring colors
981                 {
982                         int             pos, len;
983                         len = (int)strlen(key_line);
984                         pos = key_linepos;
985                         
986                         // go beyond all initial consecutive color tags, if any
987                         if(pos < len)
988                                 while (key_line[pos] == STRING_COLOR_TAG)
989                                 {
990                                         if(isdigit(key_line[pos+1]))
991                                                 pos+=2;
992                                         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]))
993                                                 pos+=5;
994                                         else
995                                                 break;
996                                 }
997                         
998                         // skip the char
999                         if (key_line[pos] == STRING_COLOR_TAG && key_line[pos+1] == STRING_COLOR_TAG) // consider ^^ as a character
1000                                 pos++;
1001                         pos += u8_bytelen(key_line + pos, 1);
1002                         
1003                         // now go beyond all next consecutive color tags, if any
1004                         if(pos < len)
1005                                 while (key_line[pos] == STRING_COLOR_TAG)
1006                                 {
1007                                         if(isdigit(key_line[pos+1]))
1008                                                 pos+=2;
1009                                         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]))
1010                                                 pos+=5;
1011                                         else
1012                                                 break;
1013                                 }
1014                         key_linepos = pos;
1015                 }
1016                 else
1017                         key_linepos += u8_bytelen(key_line + key_linepos, 1);
1018                 return;
1019         }
1020
1021         if (key == K_INS || key == K_KP_INS) // toggle insert mode
1022         {
1023                 key_insert ^= 1;
1024                 return;
1025         }
1026
1027         // End Advanced Console Editing
1028
1029         if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL]))
1030         {
1031                 Key_History_Up();
1032                 return;
1033         }
1034
1035         if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL]))
1036         {
1037                 Key_History_Down();
1038                 return;
1039         }
1040         // ~1.0795 = 82/76  using con_textsize 64 76 is height of the char, 6 is the distance between 2 lines
1041
1042         if (keydown[K_CTRL])
1043         {
1044                 // prints all the matching commands
1045                 if (key == 'f')
1046                 {
1047                         Key_History_Find_All();
1048                         return;
1049                 }
1050                 // Search forwards/backwards, pointing the history's index to the
1051                 // matching command but without fetching it to let one continue the search.
1052                 // To fetch it, it suffices to just press UP or DOWN.
1053                 if (key == 'r')
1054                 {
1055                         if (keydown[K_SHIFT])
1056                                 Key_History_Find_Forwards();
1057                         else
1058                                 Key_History_Find_Backwards();
1059                         return;
1060                 }
1061                 // go to the last/first command of the history
1062                 if (key == ',')
1063                 {
1064                         Key_History_First();
1065                         return;
1066                 }
1067                 if (key == '.')
1068                 {
1069                         Key_History_Last();
1070                         return;
1071                 }
1072         }
1073
1074         if (key == K_PGUP || key == K_KP_PGUP)
1075         {
1076                 if(keydown[K_CTRL])
1077                 {
1078                         con_backscroll += ((vid_conheight.integer >> 2) / con_textsize.integer)-1;
1079                 }
1080                 else
1081                         con_backscroll += ((vid_conheight.integer >> 1) / con_textsize.integer)-3;
1082                 return;
1083         }
1084
1085         if (key == K_PGDN || key == K_KP_PGDN)
1086         {
1087                 if(keydown[K_CTRL])
1088                 {
1089                         con_backscroll -= ((vid_conheight.integer >> 2) / con_textsize.integer)-1;
1090                 }
1091                 else
1092                         con_backscroll -= ((vid_conheight.integer >> 1) / con_textsize.integer)-3;
1093                 return;
1094         }
1095  
1096         if (key == K_MWHEELUP)
1097         {
1098                 if(keydown[K_CTRL])
1099                         con_backscroll += 1;
1100                 else if(keydown[K_SHIFT])
1101                         con_backscroll += ((vid_conheight.integer >> 2) / con_textsize.integer)-1;
1102                 else
1103                         con_backscroll += 5;
1104                 return;
1105         }
1106
1107         if (key == K_MWHEELDOWN)
1108         {
1109                 if(keydown[K_CTRL])
1110                         con_backscroll -= 1;
1111                 else if(keydown[K_SHIFT])
1112                         con_backscroll -= ((vid_conheight.integer >> 2) / con_textsize.integer)-1;
1113                 else
1114                         con_backscroll -= 5;
1115                 return;
1116         }
1117
1118         if (keydown[K_CTRL])
1119         {
1120                 // text zoom in
1121                 if (key == '+' || key == K_KP_PLUS)
1122                 {
1123                         if (con_textsize.integer < 128)
1124                                 Cvar_SetValueQuick(&con_textsize, con_textsize.integer + 1);
1125                         return;
1126                 }
1127                 // text zoom out
1128                 if (key == '-' || key == K_KP_MINUS)
1129                 {
1130                         if (con_textsize.integer > 1)
1131                                 Cvar_SetValueQuick(&con_textsize, con_textsize.integer - 1);
1132                         return;
1133                 }
1134                 // text zoom reset
1135                 if (key == '0' || key == K_KP_INS)
1136                 {
1137                         Cvar_SetValueQuick(&con_textsize, atoi(Cvar_VariableDefString("con_textsize")));
1138                         return;
1139                 }
1140         }
1141
1142         if (key == K_HOME || key == K_KP_HOME)
1143         {
1144                 if (keydown[K_CTRL])
1145                         con_backscroll = CON_TEXTSIZE;
1146                 else
1147                         key_linepos = 1;
1148                 return;
1149         }
1150
1151         if (key == K_END || key == K_KP_END)
1152         {
1153                 if (keydown[K_CTRL])
1154                         con_backscroll = 0;
1155                 else
1156                         key_linepos = (int)strlen(key_line);
1157                 return;
1158         }
1159
1160         // non printable
1161         if (unicode < 32)
1162                 return;
1163
1164         if (key_linepos < MAX_INPUTLINE-1)
1165         {
1166                 char buf[16];
1167                 int len;
1168                 int blen;
1169                 blen = u8_fromchar(unicode, buf, sizeof(buf));
1170                 if (!blen)
1171                         return;
1172                 len = (int)strlen(&key_line[key_linepos]);
1173                 // check insert mode, or always insert if at end of line
1174                 if (key_insert || len == 0)
1175                 {
1176                         // can't use strcpy to move string to right
1177                         len++;
1178                         //memmove(&key_line[key_linepos + u8_bytelen(key_line + key_linepos, 1)], &key_line[key_linepos], len);
1179                         memmove(&key_line[key_linepos + blen], &key_line[key_linepos], len);
1180                 }
1181                 memcpy(key_line + key_linepos, buf, blen);
1182                 key_linepos += blen;
1183                 //key_linepos += u8_fromchar(unicode, key_line + key_linepos, sizeof(key_line) - key_linepos - 1);
1184                 //key_line[key_linepos] = ascii;
1185                 //key_linepos++;
1186         }
1187 }
1188
1189 //============================================================================
1190
1191 int chat_mode;
1192 char            chat_buffer[MAX_INPUTLINE];
1193 unsigned int    chat_bufferlen = 0;
1194
1195 extern int Nicks_CompleteChatLine(char *buffer, size_t size, unsigned int pos);
1196
1197 static void
1198 Key_Message (int key, int ascii)
1199 {
1200         if (key == K_ENTER || ascii == 10 || ascii == 13)
1201         {
1202                 if(chat_mode < 0)
1203                         Cmd_ExecuteString(chat_buffer, src_command); // not Cbuf_AddText to allow semiclons in args; however, this allows no variables then. Use aliases!
1204                 else
1205                         Cmd_ForwardStringToServer(va("%s %s", chat_mode ? "say_team" : "say ", chat_buffer));
1206
1207                 key_dest = key_game;
1208                 chat_bufferlen = 0;
1209                 chat_buffer[0] = 0;
1210                 return;
1211         }
1212
1213         // TODO add support for arrow keys and simple editing
1214
1215         if (key == K_ESCAPE) {
1216                 key_dest = key_game;
1217                 chat_bufferlen = 0;
1218                 chat_buffer[0] = 0;
1219                 return;
1220         }
1221
1222         if (key == K_BACKSPACE) {
1223                 if (chat_bufferlen) {
1224                         chat_bufferlen = u8_prevbyte(chat_buffer, chat_bufferlen);
1225                         chat_buffer[chat_bufferlen] = 0;
1226                 }
1227                 return;
1228         }
1229
1230         if(key == K_TAB) {
1231                 chat_bufferlen = Nicks_CompleteChatLine(chat_buffer, sizeof(chat_buffer), chat_bufferlen);
1232                 return;
1233         }
1234
1235         // ctrl+key generates an ascii value < 32 and shows a char from the charmap
1236         if (ascii > 0 && ascii < 32 && utf8_enable.integer)
1237                 ascii = 0xE000 + ascii;
1238
1239         if (chat_bufferlen == sizeof (chat_buffer) - 1)
1240                 return;                                                 // all full
1241
1242         if (!ascii)
1243                 return;                                                 // non printable
1244
1245         chat_bufferlen += u8_fromchar(ascii, chat_buffer+chat_bufferlen, sizeof(chat_buffer) - chat_bufferlen - 1);
1246
1247         //chat_buffer[chat_bufferlen++] = ascii;
1248         //chat_buffer[chat_bufferlen] = 0;
1249 }
1250
1251 //============================================================================
1252
1253
1254 /*
1255 ===================
1256 Returns a key number to be used to index keybindings[] by looking at
1257 the given string.  Single ascii characters return themselves, while
1258 the K_* names are matched up.
1259 ===================
1260 */
1261 int
1262 Key_StringToKeynum (const char *str)
1263 {
1264         const keyname_t  *kn;
1265
1266         if (!str || !str[0])
1267                 return -1;
1268         if (!str[1])
1269                 return tolower(str[0]);
1270
1271         for (kn = keynames; kn->name; kn++) {
1272                 if (!strcasecmp (str, kn->name))
1273                         return kn->keynum;
1274         }
1275         return -1;
1276 }
1277
1278 /*
1279 ===================
1280 Returns a string (either a single ascii char, or a K_* name) for the
1281 given keynum.
1282 FIXME: handle quote special (general escape sequence?)
1283 ===================
1284 */
1285 const char *
1286 Key_KeynumToString (int keynum)
1287 {
1288         const keyname_t  *kn;
1289         static char tinystr[2];
1290
1291         // -1 is an invalid code
1292         if (keynum < 0)
1293                 return "<KEY NOT FOUND>";
1294
1295         // search overrides first, because some characters are special
1296         for (kn = keynames; kn->name; kn++)
1297                 if (keynum == kn->keynum)
1298                         return kn->name;
1299
1300         // if it is printable, output it as a single character
1301         if (keynum > 32 && keynum < 256)
1302         {
1303                 tinystr[0] = keynum;
1304                 tinystr[1] = 0;
1305                 return tinystr;
1306         }
1307
1308         // if it is not overridden and not printable, we don't know what to do with it
1309         return "<UNKNOWN KEYNUM>";
1310 }
1311
1312
1313 qboolean
1314 Key_SetBinding (int keynum, int bindmap, const char *binding)
1315 {
1316         char *newbinding;
1317         size_t l;
1318
1319         if (keynum == -1 || keynum >= MAX_KEYS)
1320                 return false;
1321         if ((bindmap < 0) || (bindmap >= MAX_BINDMAPS))
1322                 return false;
1323
1324 // free old bindings
1325         if (keybindings[bindmap][keynum]) {
1326                 Z_Free (keybindings[bindmap][keynum]);
1327                 keybindings[bindmap][keynum] = NULL;
1328         }
1329         if(!binding[0]) // make "" binds be removed --blub
1330                 return true;
1331 // allocate memory for new binding
1332         l = strlen (binding);
1333         newbinding = (char *)Z_Malloc (l + 1);
1334         memcpy (newbinding, binding, l + 1);
1335         newbinding[l] = 0;
1336         keybindings[bindmap][keynum] = newbinding;
1337         return true;
1338 }
1339
1340 void Key_GetBindMap(int *fg, int *bg)
1341 {
1342         if(fg)
1343                 *fg = key_bmap;
1344         if(bg)
1345                 *bg = key_bmap2;
1346 }
1347
1348 qboolean Key_SetBindMap(int fg, int bg)
1349 {
1350         if(fg >= MAX_BINDMAPS)
1351                 return false;
1352         if(bg >= MAX_BINDMAPS)
1353                 return false;
1354         if(fg >= 0)
1355                 key_bmap = fg;
1356         if(bg >= 0)
1357                 key_bmap2 = bg;
1358         return true;
1359 }
1360
1361 static void
1362 Key_In_Unbind_f (void)
1363 {
1364         int         b, m;
1365         char *errchar = NULL;
1366
1367         if (Cmd_Argc () != 3) {
1368                 Con_Print("in_unbind <bindmap> <key> : remove commands from a key\n");
1369                 return;
1370         }
1371
1372         m = strtol(Cmd_Argv (1), &errchar, 0);
1373         if ((m < 0) || (m >= MAX_BINDMAPS) || (errchar && *errchar)) {
1374                 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(1));
1375                 return;
1376         }
1377
1378         b = Key_StringToKeynum (Cmd_Argv (2));
1379         if (b == -1) {
1380                 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
1381                 return;
1382         }
1383
1384         if(!Key_SetBinding (b, m, ""))
1385                 Con_Printf("Key_SetBinding failed for unknown reason\n");
1386 }
1387
1388 static void
1389 Key_In_Bind_f (void)
1390 {
1391         int         i, c, b, m;
1392         char        cmd[MAX_INPUTLINE];
1393         char *errchar = NULL;
1394
1395         c = Cmd_Argc ();
1396
1397         if (c != 3 && c != 4) {
1398                 Con_Print("in_bind <bindmap> <key> [command] : attach a command to a key\n");
1399                 return;
1400         }
1401
1402         m = strtol(Cmd_Argv (1), &errchar, 0);
1403         if ((m < 0) || (m >= MAX_BINDMAPS) || (errchar && *errchar)) {
1404                 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(1));
1405                 return;
1406         }
1407
1408         b = Key_StringToKeynum (Cmd_Argv (2));
1409         if (b == -1 || b >= MAX_KEYS) {
1410                 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
1411                 return;
1412         }
1413
1414         if (c == 3) {
1415                 if (keybindings[m][b])
1416                         Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (2), keybindings[m][b]);
1417                 else
1418                         Con_Printf("\"%s\" is not bound\n", Cmd_Argv (2));
1419                 return;
1420         }
1421 // copy the rest of the command line
1422         cmd[0] = 0;                                                     // start out with a null string
1423         for (i = 3; i < c; i++) {
1424                 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
1425                 if (i != (c - 1))
1426                         strlcat (cmd, " ", sizeof (cmd));
1427         }
1428
1429         if(!Key_SetBinding (b, m, cmd))
1430                 Con_Printf("Key_SetBinding failed for unknown reason\n");
1431 }
1432
1433 static void
1434 Key_In_Bindmap_f (void)
1435 {
1436         int         m1, m2, c;
1437         char *errchar = NULL;
1438
1439         c = Cmd_Argc ();
1440
1441         if (c != 3) {
1442                 Con_Print("in_bindmap <bindmap> <fallback>: set current bindmap and fallback\n");
1443                 return;
1444         }
1445
1446         m1 = strtol(Cmd_Argv (1), &errchar, 0);
1447         if ((m1 < 0) || (m1 >= MAX_BINDMAPS) || (errchar && *errchar)) {
1448                 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(1));
1449                 return;
1450         }
1451
1452         m2 = strtol(Cmd_Argv (2), &errchar, 0);
1453         if ((m2 < 0) || (m2 >= MAX_BINDMAPS) || (errchar && *errchar)) {
1454                 Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(2));
1455                 return;
1456         }
1457
1458         key_bmap = m1;
1459         key_bmap2 = m2;
1460 }
1461
1462 static void
1463 Key_Unbind_f (void)
1464 {
1465         int         b;
1466
1467         if (Cmd_Argc () != 2) {
1468                 Con_Print("unbind <key> : remove commands from a key\n");
1469                 return;
1470         }
1471
1472         b = Key_StringToKeynum (Cmd_Argv (1));
1473         if (b == -1) {
1474                 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
1475                 return;
1476         }
1477
1478         if(!Key_SetBinding (b, 0, ""))
1479                 Con_Printf("Key_SetBinding failed for unknown reason\n");
1480 }
1481
1482 static void
1483 Key_Unbindall_f (void)
1484 {
1485         int         i, j;
1486
1487         for (j = 0; j < MAX_BINDMAPS; j++)
1488                 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
1489                         if (keybindings[j][i])
1490                                 Key_SetBinding (i, j, "");
1491 }
1492
1493 static void
1494 Key_PrintBindList(int j)
1495 {
1496         char bindbuf[MAX_INPUTLINE];
1497         const char *p;
1498         int i;
1499
1500         for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
1501         {
1502                 p = keybindings[j][i];
1503                 if (p)
1504                 {
1505                         Cmd_QuoteString(bindbuf, sizeof(bindbuf), p, "\"\\", false);
1506                         if (j == 0)
1507                                 Con_Printf("^2%s ^7= \"%s\"\n", Key_KeynumToString (i), bindbuf);
1508                         else
1509                                 Con_Printf("^3bindmap %d: ^2%s ^7= \"%s\"\n", j, Key_KeynumToString (i), bindbuf);
1510                 }
1511         }
1512 }
1513
1514 static void
1515 Key_In_BindList_f (void)
1516 {
1517         int m;
1518         char *errchar = NULL;
1519
1520         if(Cmd_Argc() >= 2)
1521         {
1522                 m = strtol(Cmd_Argv(1), &errchar, 0);
1523                 if ((m < 0) || (m >= MAX_BINDMAPS) || (errchar && *errchar)) {
1524                         Con_Printf("%s isn't a valid bindmap\n", Cmd_Argv(1));
1525                         return;
1526                 }
1527                 Key_PrintBindList(m);
1528         }
1529         else
1530         {
1531                 for (m = 0; m < MAX_BINDMAPS; m++)
1532                         Key_PrintBindList(m);
1533         }
1534 }
1535
1536 static void
1537 Key_BindList_f (void)
1538 {
1539         Key_PrintBindList(0);
1540 }
1541
1542 static void
1543 Key_Bind_f (void)
1544 {
1545         int         i, c, b;
1546         char        cmd[MAX_INPUTLINE];
1547
1548         c = Cmd_Argc ();
1549
1550         if (c != 2 && c != 3) {
1551                 Con_Print("bind <key> [command] : attach a command to a key\n");
1552                 return;
1553         }
1554         b = Key_StringToKeynum (Cmd_Argv (1));
1555         if (b == -1 || b >= MAX_KEYS) {
1556                 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
1557                 return;
1558         }
1559
1560         if (c == 2) {
1561                 if (keybindings[0][b])
1562                         Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (1), keybindings[0][b]);
1563                 else
1564                         Con_Printf("\"%s\" is not bound\n", Cmd_Argv (1));
1565                 return;
1566         }
1567 // copy the rest of the command line
1568         cmd[0] = 0;                                                     // start out with a null string
1569         for (i = 2; i < c; i++) {
1570                 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
1571                 if (i != (c - 1))
1572                         strlcat (cmd, " ", sizeof (cmd));
1573         }
1574
1575         if(!Key_SetBinding (b, 0, cmd))
1576                 Con_Printf("Key_SetBinding failed for unknown reason\n");
1577 }
1578
1579 /*
1580 ============
1581 Writes lines containing "bind key value"
1582 ============
1583 */
1584 void
1585 Key_WriteBindings (qfile_t *f)
1586 {
1587         int         i, j;
1588         char bindbuf[MAX_INPUTLINE];
1589         const char *p;
1590
1591         for (j = 0; j < MAX_BINDMAPS; j++)
1592         {
1593                 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
1594                 {
1595                         p = keybindings[j][i];
1596                         if (p)
1597                         {
1598                                 Cmd_QuoteString(bindbuf, sizeof(bindbuf), p, "\"\\", false); // don't need to escape $ because cvars are not expanded inside bind
1599                                 if (j == 0)
1600                                         FS_Printf(f, "bind %s \"%s\"\n", Key_KeynumToString (i), bindbuf);
1601                                 else
1602                                         FS_Printf(f, "in_bind %d %s \"%s\"\n", j, Key_KeynumToString (i), bindbuf);
1603                         }
1604                 }
1605         }
1606 }
1607
1608
1609 void
1610 Key_Init (void)
1611 {
1612         Key_History_Init();
1613         key_line[0] = ']';
1614         key_line[1] = 0;
1615         key_linepos = 1;
1616
1617 //
1618 // register our functions
1619 //
1620         Cmd_AddCommand ("in_bind", Key_In_Bind_f, "binds a command to the specified key in the selected bindmap");
1621         Cmd_AddCommand ("in_unbind", Key_In_Unbind_f, "removes command on the specified key in the selected bindmap");
1622         Cmd_AddCommand ("in_bindlist", Key_In_BindList_f, "bindlist: displays bound keys for all bindmaps, or the given bindmap");
1623         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");
1624
1625         Cmd_AddCommand ("bind", Key_Bind_f, "binds a command to the specified key in bindmap 0");
1626         Cmd_AddCommand ("unbind", Key_Unbind_f, "removes a command on the specified key in bindmap 0");
1627         Cmd_AddCommand ("bindlist", Key_BindList_f, "bindlist: displays bound keys for bindmap 0 bindmaps");
1628         Cmd_AddCommand ("unbindall", Key_Unbindall_f, "removes all commands from all keys in all bindmaps (leaving only shift-escape and escape)");
1629
1630         Cmd_AddCommand ("history", Key_History_f, "prints the history of executed commands (history X prints the last X entries, history -c clears the whole history)");
1631
1632         Cvar_RegisterVariable (&con_closeontoggleconsole);
1633 }
1634
1635 void
1636 Key_Shutdown (void)
1637 {
1638         Key_History_Shutdown();
1639 }
1640
1641 const char *Key_GetBind (int key, int bindmap)
1642 {
1643         const char *bind;
1644         if (key < 0 || key >= MAX_KEYS)
1645                 return NULL;
1646         if(bindmap >= MAX_BINDMAPS)
1647                 return NULL;
1648         if(bindmap >= 0)
1649         {
1650                 bind = keybindings[bindmap][key];
1651         }
1652         else
1653         {
1654                 bind = keybindings[key_bmap][key];
1655                 if (!bind)
1656                         bind = keybindings[key_bmap2][key];
1657         }
1658         return bind;
1659 }
1660
1661 void Key_FindKeysForCommand (const char *command, int *keys, int numkeys, int bindmap)
1662 {
1663         int             count;
1664         int             j;
1665         const char      *b;
1666
1667         for (j = 0;j < numkeys;j++)
1668                 keys[j] = -1;
1669
1670         if(bindmap >= MAX_BINDMAPS)
1671                 return;
1672
1673         count = 0;
1674
1675         for (j = 0; j < MAX_KEYS; ++j)
1676         {
1677                 b = Key_GetBind(j, bindmap);
1678                 if (!b)
1679                         continue;
1680                 if (!strcmp (b, command) )
1681                 {
1682                         keys[count++] = j;
1683                         if (count == numkeys)
1684                                 break;
1685                 }
1686         }
1687 }
1688
1689 qboolean CL_VM_InputEvent (qboolean down, int key, int ascii);
1690
1691 /*
1692 ===================
1693 Called by the system between frames for both key up and key down events
1694 Should NOT be called during an interrupt!
1695 ===================
1696 */
1697 static char tbl_keyascii[MAX_KEYS];
1698 static keydest_t tbl_keydest[MAX_KEYS];
1699
1700 typedef struct eventqueueitem_s
1701 {
1702         int key;
1703         int ascii;
1704         qboolean down;
1705 }
1706 eventqueueitem_t;
1707 static int events_blocked = 0;
1708 static eventqueueitem_t eventqueue[32];
1709 static unsigned eventqueue_idx = 0;
1710
1711 static void Key_EventQueue_Add(int key, int ascii, qboolean down)
1712 {
1713         if(eventqueue_idx < sizeof(eventqueue) / sizeof(*eventqueue))
1714         {
1715                 eventqueue[eventqueue_idx].key = key;
1716                 eventqueue[eventqueue_idx].ascii = ascii;
1717                 eventqueue[eventqueue_idx].down = down;
1718                 ++eventqueue_idx;
1719         }
1720 }
1721
1722 void Key_EventQueue_Block(void)
1723 {
1724         // block key events until call to Unblock
1725         events_blocked = true;
1726 }
1727
1728 void Key_EventQueue_Unblock(void)
1729 {
1730         // unblocks key events again
1731         unsigned i;
1732         events_blocked = false;
1733         for(i = 0; i < eventqueue_idx; ++i)
1734                 Key_Event(eventqueue[i].key, eventqueue[i].ascii, eventqueue[i].down);
1735         eventqueue_idx = 0;
1736 }
1737
1738 void
1739 Key_Event (int key, int ascii, qboolean down)
1740 {
1741         const char *bind;
1742         qboolean q;
1743         keydest_t keydest = key_dest;
1744
1745         if (key < 0 || key >= MAX_KEYS)
1746                 return;
1747
1748         if(events_blocked)
1749         {
1750                 Key_EventQueue_Add(key, ascii, down);
1751                 return;
1752         }
1753
1754         if (ascii == 0x80 && utf8_enable.integer) // pressing AltGr-5 (or AltGr-e) and for some reason we get windows-1252 encoding?
1755                 ascii = 0x20AC; // we want the Euro currency sign
1756                 // TODO find out which vid_ drivers do it and fix it there
1757                 // but catching U+0080 here is no loss as that char is not useful anyway
1758
1759         // get key binding
1760         bind = keybindings[key_bmap][key];
1761         if (!bind)
1762                 bind = keybindings[key_bmap2][key];
1763
1764         if (developer_insane.integer)
1765                 Con_DPrintf("Key_Event(%i, '%c', %s) keydown %i bind \"%s\"\n", key, ascii ? ascii : '?', down ? "down" : "up", keydown[key], bind ? bind : "");
1766
1767         if(key_consoleactive)
1768                 keydest = key_console;
1769         
1770         if (down)
1771         {
1772                 // increment key repeat count each time a down is received so that things
1773                 // which want to ignore key repeat can ignore it
1774                 keydown[key] = min(keydown[key] + 1, 2);
1775                 if(keydown[key] == 1) {
1776                         tbl_keyascii[key] = ascii;
1777                         tbl_keydest[key] = keydest;
1778                 } else {
1779                         ascii = tbl_keyascii[key];
1780                         keydest = tbl_keydest[key];
1781                 }
1782         }
1783         else
1784         {
1785                 // clear repeat count now that the key is released
1786                 keydown[key] = 0;
1787                 keydest = tbl_keydest[key];
1788                 ascii = tbl_keyascii[key];
1789         }
1790
1791         if(keydest == key_void)
1792                 return;
1793         
1794         // key_consoleactive is a flag not a key_dest because the console is a
1795         // high priority overlay ontop of the normal screen (designed as a safety
1796         // feature so that developers and users can rescue themselves from a bad
1797         // situation).
1798         //
1799         // this also means that toggling the console on/off does not lose the old
1800         // key_dest state
1801
1802         // specially handle escape (togglemenu) and shift-escape (toggleconsole)
1803         // engine bindings, these are not handled as normal binds so that the user
1804         // can recover from a completely empty bindmap
1805         if (key == K_ESCAPE)
1806         {
1807                 // ignore key repeats on escape
1808                 if (keydown[key] > 1)
1809                         return;
1810
1811                 // escape does these things:
1812                 // key_consoleactive - close console
1813                 // key_message - abort messagemode
1814                 // key_menu - go to parent menu (or key_game)
1815                 // key_game - open menu
1816
1817                 // in all modes shift-escape toggles console
1818                 if (keydown[K_SHIFT])
1819                 {
1820                         if(down)
1821                         {
1822                                 Con_ToggleConsole_f ();
1823                                 tbl_keydest[key] = key_void; // esc release should go nowhere (especially not to key_menu or key_game)
1824                         }
1825                         return;
1826                 }
1827
1828                 switch (keydest)
1829                 {
1830                         case key_console:
1831                                 if(down)
1832                                 {
1833                                         if(key_consoleactive & KEY_CONSOLEACTIVE_FORCED)
1834                                         {
1835                                                 key_consoleactive &= ~KEY_CONSOLEACTIVE_USER;
1836                                                 MR_ToggleMenu(1);
1837                                         }
1838                                         else
1839                                                 Con_ToggleConsole_f();
1840                                 }
1841                                 break;
1842
1843                         case key_message:
1844                                 if (down)
1845                                         Key_Message (key, ascii); // that'll close the message input
1846                                 break;
1847
1848                         case key_menu:
1849                         case key_menu_grabbed:
1850                                 MR_KeyEvent (key, ascii, down);
1851                                 break;
1852
1853                         case key_game:
1854                                 // csqc has priority over toggle menu if it wants to (e.g. handling escape for UI stuff in-game.. :sick:)
1855                                 q = CL_VM_InputEvent(down, key, ascii);
1856                                 if (!q && down)
1857                                         MR_ToggleMenu(1);
1858                                 break;
1859
1860                         default:
1861                                 Con_Printf ("Key_Event: Bad key_dest\n");
1862                 }
1863                 return;
1864         }
1865
1866         // send function keydowns to interpreter no matter what mode is (unless the menu has specifically grabbed the keyboard, for rebinding keys)
1867         // VorteX: Omnicide does bind F* keys
1868         if (keydest != key_menu_grabbed)
1869         if (key >= K_F1 && key <= K_F12 && gamemode != GAME_BLOODOMNICIDE)
1870         {
1871                 if (bind)
1872                 {
1873                         if(keydown[key] == 1 && down)
1874                         {
1875                                 // button commands add keynum as a parm
1876                                 if (bind[0] == '+')
1877                                         Cbuf_AddText (va("%s %i\n", bind, key));
1878                                 else
1879                                 {
1880                                         Cbuf_AddText (bind);
1881                                         Cbuf_AddText ("\n");
1882                                 }
1883                         } else if(bind[0] == '+' && !down && keydown[key] == 0)
1884                                 Cbuf_AddText(va("-%s %i\n", bind + 1, key));
1885                 }
1886                 return;
1887         }
1888
1889         // send input to console if it wants it
1890         if (keydest == key_console)
1891         {
1892                 if (!down)
1893                         return;
1894                 // con_closeontoggleconsole enables toggleconsole keys to close the
1895                 // console, as long as they are not the color prefix character
1896                 // (special exemption for german keyboard layouts)
1897                 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))
1898                 {
1899                         Con_ToggleConsole_f ();
1900                         return;
1901                 }
1902                 Key_Console (key, ascii);
1903                 return;
1904         }
1905
1906         // handle toggleconsole in menu too
1907         if (keydest == key_menu)
1908         {
1909                 if (down && con_closeontoggleconsole.integer && bind && !strncmp(bind, "toggleconsole", strlen("toggleconsole")) && ascii != STRING_COLOR_TAG)
1910                 {
1911                         Con_ToggleConsole_f ();
1912                         tbl_keydest[key] = key_void; // key release should go nowhere (especially not to key_menu or key_game)
1913                         return;
1914                 }
1915         }
1916
1917         // ignore binds while a video is played, let the video system handle the key event
1918         if (cl_videoplaying)
1919         {
1920                 if (gamemode == GAME_BLOODOMNICIDE) // menu controls key events
1921                         MR_KeyEvent(key, ascii, down);
1922                 else
1923                         CL_Video_KeyEvent (key, ascii, keydown[key] != 0);
1924                 return;
1925         }
1926
1927         // anything else is a key press into the game, chat line, or menu
1928         switch (keydest)
1929         {
1930                 case key_message:
1931                         if (down)
1932                                 Key_Message (key, ascii);
1933                         break;
1934                 case key_menu:
1935                 case key_menu_grabbed:
1936                         MR_KeyEvent (key, ascii, down);
1937                         break;
1938                 case key_game:
1939                         q = CL_VM_InputEvent(down, key, ascii);
1940                         // ignore key repeats on binds and only send the bind if the event hasnt been already processed by csqc
1941                         if (!q && bind)
1942                         {
1943                                 if(keydown[key] == 1 && down)
1944                                 {
1945                                         // button commands add keynum as a parm
1946                                         if (bind[0] == '+')
1947                                                 Cbuf_AddText (va("%s %i\n", bind, key));
1948                                         else
1949                                         {
1950                                                 Cbuf_AddText (bind);
1951                                                 Cbuf_AddText ("\n");
1952                                         }
1953                                 } else if(bind[0] == '+' && !down && keydown[key] == 0)
1954                                         Cbuf_AddText(va("-%s %i\n", bind + 1, key));
1955                         }
1956                         break;
1957                 default:
1958                         Con_Printf ("Key_Event: Bad key_dest\n");
1959         }
1960 }
1961
1962 // a helper to simulate release of ALL keys
1963 void
1964 Key_ReleaseAll (void)
1965 {
1966         int key;
1967         // clear the event queue first
1968         eventqueue_idx = 0;
1969         // then send all down events (possibly into the event queue)
1970         for(key = 0; key < MAX_KEYS; ++key)
1971                 if(keydown[key])
1972                         Key_Event(key, 0, false);
1973         // now all keys are guaranteed down (once the event queue is unblocked)
1974         // and only future events count
1975 }
1976
1977 /*
1978 ===================
1979 Key_ClearStates
1980 ===================
1981 */
1982 void
1983 Key_ClearStates (void)
1984 {
1985         memset(keydown, 0, sizeof(keydown));
1986 }