default to insert mode instead of replace mode in console
[divverent/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
25
26 /*
27 key up events are sent even if in console mode
28 */
29
30 #define MAX_INPUTLINE 256
31 char            key_lines[32][MAX_INPUTLINE];
32 int                     key_linepos;
33 static int      ctrl_down = false;
34 static int      key_lastpress;
35 int                     key_insert = true;      // insert key toggle (for editing)
36
37 int         edit_line = 0;
38 int         history_line = 0;
39
40 keydest_t       key_dest;
41 int                     key_consoleactive;
42
43 static int      key_count;                                      // incremented every key event
44 static int      key_bmap, key_bmap2;
45
46 char                            *keybindings[8][1024];
47 static qboolean         consolekeys[1024];      // if true, can't be rebound while in
48                                                                                 // console
49 static qboolean         menubound[1024];                // if true, can't be rebound while in
50                                                                                 // menu
51 static unsigned int     key_repeats[1024];      // if > 1, it is autorepeating
52 static qboolean         keydown[1024];
53
54 typedef struct {
55         const char      *name;
56         int                     keynum;
57 } keyname_t;
58
59 static const keyname_t   keynames[] = {
60         {"TAB", K_TAB},
61         {"ENTER", K_ENTER},
62         {"ESCAPE", K_ESCAPE},
63         {"SPACE", K_SPACE},
64         {"BACKSPACE", K_BACKSPACE},
65         {"UPARROW", K_UPARROW},
66         {"DOWNARROW", K_DOWNARROW},
67         {"LEFTARROW", K_LEFTARROW},
68         {"RIGHTARROW", K_RIGHTARROW},
69
70         {"ALT", K_ALT},
71         {"CTRL", K_CTRL},
72         {"SHIFT", K_SHIFT},
73
74         {"F1", K_F1},
75         {"F2", K_F2},
76         {"F3", K_F3},
77         {"F4", K_F4},
78         {"F5", K_F5},
79         {"F6", K_F6},
80         {"F7", K_F7},
81         {"F8", K_F8},
82         {"F9", K_F9},
83         {"F10", K_F10},
84         {"F11", K_F11},
85         {"F12", K_F12},
86
87         {"INS", K_INS},
88         {"DEL", K_DEL},
89         {"PGDN", K_PGDN},
90         {"PGUP", K_PGUP},
91         {"HOME", K_HOME},
92         {"END", K_END},
93
94         {"PAUSE", K_PAUSE},
95
96         {"MWHEELUP", K_MWHEELUP},
97         {"MWHEELDOWN", K_MWHEELDOWN},
98
99         {"MOUSE1", K_MOUSE1},
100         {"MOUSE2", K_MOUSE2},
101         {"MOUSE3", K_MOUSE3},
102         {"MOUSE4", K_MOUSE4},
103         {"MOUSE5", K_MOUSE5},
104         {"MOUSE6", K_MOUSE6},
105         {"MOUSE7", K_MOUSE7},
106         {"MOUSE8", K_MOUSE8},
107         {"MOUSE9", K_MOUSE9},
108         {"MOUSE10", K_MOUSE10},
109         {"MOUSE11", K_MOUSE11},
110         {"MOUSE12", K_MOUSE12},
111         {"MOUSE13", K_MOUSE13},
112         {"MOUSE14", K_MOUSE14},
113         {"MOUSE15", K_MOUSE15},
114         {"MOUSE16", K_MOUSE16},
115
116         {"NUMLOCK", K_NUMLOCK},
117         {"CAPSLOCK", K_CAPSLOCK},
118         {"SCROLLOCK", K_SCROLLOCK},
119
120         {"KP_HOME",                     K_KP_HOME },
121         {"KP_UPARROW",          K_KP_UPARROW },
122         {"KP_PGUP",                     K_KP_PGUP },
123         {"KP_LEFTARROW",        K_KP_LEFTARROW },
124         {"KP_RIGHTARROW",       K_KP_RIGHTARROW },
125         {"KP_END",                      K_KP_END },
126         {"KP_DOWNARROW",        K_KP_DOWNARROW },
127         {"KP_PGDN",                     K_KP_PGDN },
128         {"KP_INS",                      K_KP_INS },
129         {"KP_DEL",                      K_KP_DEL },
130         {"KP_SLASH",            K_KP_SLASH },
131
132         {"KP_0", K_KP_0},
133         {"KP_1", K_KP_1},
134         {"KP_2", K_KP_2},
135         {"KP_3", K_KP_3},
136         {"KP_4", K_KP_4},
137         {"KP_5", K_KP_5},
138         {"KP_6", K_KP_6},
139         {"KP_7", K_KP_7},
140         {"KP_8", K_KP_8},
141         {"KP_9", K_KP_9},
142         {"KP_PERIOD", K_KP_PERIOD},
143         {"KP_DIVIDE", K_KP_DIVIDE},
144         {"KP_MULTIPLY", K_KP_MULTIPLY},
145         {"KP_MINUS", K_KP_MINUS},
146         {"KP_PLUS", K_KP_PLUS},
147         {"KP_ENTER", K_KP_ENTER},
148         {"KP_EQUALS", K_KP_EQUALS},
149
150         {"JOY1",  K_JOY1},
151         {"JOY2",  K_JOY2},
152         {"JOY3",  K_JOY3},
153         {"JOY4",  K_JOY4},
154         {"JOY5",  K_JOY5},
155         {"JOY6",  K_JOY6},
156         {"JOY7",  K_JOY7},
157         {"JOY8",  K_JOY8},
158         {"JOY9",  K_JOY9},
159         {"JOY10", K_JOY10},
160         {"JOY11", K_JOY11},
161         {"JOY12", K_JOY12},
162         {"JOY13", K_JOY13},
163         {"JOY14", K_JOY14},
164         {"JOY15", K_JOY15},
165         {"JOY16", K_JOY16},
166
167         {"AUX1", K_AUX1},
168         {"AUX2", K_AUX2},
169         {"AUX3", K_AUX3},
170         {"AUX4", K_AUX4},
171         {"AUX5", K_AUX5},
172         {"AUX6", K_AUX6},
173         {"AUX7", K_AUX7},
174         {"AUX8", K_AUX8},
175         {"AUX9", K_AUX9},
176         {"AUX10", K_AUX10},
177         {"AUX11", K_AUX11},
178         {"AUX12", K_AUX12},
179         {"AUX13", K_AUX13},
180         {"AUX14", K_AUX14},
181         {"AUX15", K_AUX15},
182         {"AUX16", K_AUX16},
183         {"AUX17", K_AUX17},
184         {"AUX18", K_AUX18},
185         {"AUX19", K_AUX19},
186         {"AUX20", K_AUX20},
187         {"AUX21", K_AUX21},
188         {"AUX22", K_AUX22},
189         {"AUX23", K_AUX23},
190         {"AUX24", K_AUX24},
191         {"AUX25", K_AUX25},
192         {"AUX26", K_AUX26},
193         {"AUX27", K_AUX27},
194         {"AUX28", K_AUX28},
195         {"AUX29", K_AUX29},
196         {"AUX30", K_AUX30},
197         {"AUX31", K_AUX31},
198         {"AUX32", K_AUX32},
199
200         {"SEMICOLON", ';'},                     // because a raw semicolon seperates commands
201         {"TILDE", '~'},
202         {"BACKQUOTE", '`'},
203         {"QUOTE", '"'},
204         {"APOSTROPHE", '\''},
205
206         {NULL, 0}
207 };
208
209 /*
210 ==============================================================================
211
212                         LINE TYPING INTO THE CONSOLE
213
214 ==============================================================================
215 */
216
217 void
218 Key_ClearEditLine (int edit_line)
219 {
220         memset (key_lines[edit_line], '\0', MAX_INPUTLINE);
221         key_lines[edit_line][0] = ']';
222         key_linepos = 1;
223 }
224
225 /*
226 ====================
227 Interactive line editing and console scrollback
228 ====================
229 */
230 static void
231 Key_Console (int key, char ascii)
232 {
233         // LordHavoc: copied most of this from Q2 to improve keyboard handling
234         switch (key)
235         {
236         case K_KP_SLASH:
237                 key = '/';
238                 break;
239         case K_KP_MINUS:
240                 key = '-';
241                 break;
242         case K_KP_PLUS:
243                 key = '+';
244                 break;
245         case K_KP_HOME:
246                 key = '7';
247                 break;
248         case K_KP_UPARROW:
249                 key = '8';
250                 break;
251         case K_KP_PGUP:
252                 key = '9';
253                 break;
254         case K_KP_LEFTARROW:
255                 key = '4';
256                 break;
257         case K_KP_5:
258                 key = '5';
259                 break;
260         case K_KP_RIGHTARROW:
261                 key = '6';
262                 break;
263         case K_KP_END:
264                 key = '1';
265                 break;
266         case K_KP_DOWNARROW:
267                 key = '2';
268                 break;
269         case K_KP_PGDN:
270                 key = '3';
271                 break;
272         case K_KP_INS:
273                 key = '0';
274                 break;
275         case K_KP_DEL:
276                 key = '.';
277                 break;
278         }
279
280         if ((toupper(key) == 'V' && keydown[K_CTRL]) || ((key == K_INS || key == K_KP_INS) && keydown[K_SHIFT]))
281         {
282                 char *cbd;
283                 if ((cbd = Sys_GetClipboardData()) != 0)
284                 {
285                         int i;
286                         strtok(cbd, "\n\r\b");
287                         i = strlen(cbd);
288                         if (i + key_linepos >= MAX_INPUTLINE)
289                                 i= MAX_INPUTLINE - key_linepos;
290                         if (i > 0)
291                         {
292                                 cbd[i]=0;
293                                 strcat(key_lines[edit_line], cbd);
294                                 key_linepos += i;
295                         }
296                         free(cbd);
297                 }
298                 return;
299         }
300
301         if (key == 'l')
302         {
303                 if (keydown[K_CTRL])
304                 {
305                         Cbuf_AddText ("clear\n");
306                         return;
307                 }
308         }
309
310         if (key == K_ENTER || key == K_KP_ENTER)
311         {
312                 Cbuf_AddText (key_lines[edit_line]+1);  // skip the ]
313                 Cbuf_AddText ("\n");
314                 Con_Printf("%s\n",key_lines[edit_line]);
315                 edit_line = (edit_line + 1) & 31;
316                 history_line = edit_line;
317                 key_lines[edit_line][0] = ']';
318                 key_lines[edit_line][1] = 0;    // EvilTypeGuy: null terminate
319                 key_linepos = 1;
320                 // force an update, because the command may take some time
321                 if (cls.state == ca_disconnected)
322                         CL_UpdateScreen ();
323                 return;
324         }
325
326         if (key == K_TAB)
327         {
328                 // Enhanced command completion
329                 // by EvilTypeGuy eviltypeguy@qeradiant.com
330                 // Thanks to Fett, Taniwha
331                 Con_CompleteCommandLine();
332                 return;
333         }
334
335         // Advanced Console Editing by Radix radix@planetquake.com
336         // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
337
338         // left arrow will just move left one without erasing, backspace will
339         // actually erase charcter
340         if (key == K_LEFTARROW || key == K_KP_LEFTARROW)
341         {
342                 if (key_linepos > 1)
343                         key_linepos--;
344                 return;
345         }
346
347         // delete char before cursor
348         if (key == K_BACKSPACE || (key == 'h' && keydown[K_CTRL]))
349         {
350                 if (key_linepos > 1)
351                 {
352                         strcpy(key_lines[edit_line] + key_linepos - 1, key_lines[edit_line] + key_linepos);
353                         key_linepos--;
354                 }
355                 return;
356         }
357
358         // delete char on cursor
359         if (key == K_DEL || key == K_KP_DEL)
360         {
361                 if ((size_t)key_linepos < strlen(key_lines[edit_line]))
362                         strcpy(key_lines[edit_line] + key_linepos, key_lines[edit_line] + key_linepos + 1);
363                 return;
364         }
365
366
367         // if we're at the end, get one character from previous line,
368         // otherwise just go right one
369         if (key == K_RIGHTARROW || key == K_KP_RIGHTARROW)
370         {
371                 if ((size_t)key_linepos < strlen(key_lines[edit_line]))
372                         key_linepos++;
373
374                 return;
375         }
376
377         if (key == K_INS || key == K_KP_INS) // toggle insert mode
378         {
379                 key_insert ^= 1;
380                 return;
381         }
382
383         // End Advanced Console Editing
384
385         if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL]))
386         {
387                 do
388                 {
389                         history_line = (history_line - 1) & 31;
390                 } while (history_line != edit_line
391                                 && !key_lines[history_line][1]);
392                 if (history_line == edit_line)
393                         history_line = (edit_line+1)&31;
394                 strcpy(key_lines[edit_line], key_lines[history_line]);
395                 key_linepos = strlen(key_lines[edit_line]);
396                 return;
397         }
398
399         if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL]))
400         {
401                 if (history_line == edit_line) return;
402                 do
403                 {
404                         history_line = (history_line + 1) & 31;
405                 }
406                 while (history_line != edit_line
407                         && !key_lines[history_line][1]);
408                 if (history_line == edit_line)
409                 {
410                         key_lines[edit_line][0] = ']';
411                         key_linepos = 1;
412                 }
413                 else
414                 {
415                         strcpy(key_lines[edit_line], key_lines[history_line]);
416                         key_linepos = strlen(key_lines[edit_line]);
417                 }
418                 return;
419         }
420
421         if (key == K_PGUP || key == K_KP_PGUP || key == K_MWHEELUP)
422         {
423                 con_backscroll += ((int) vid.conheight >> 5);
424                 if (con_backscroll > con_totallines - (vid.conheight>>3) - 1)
425                         con_backscroll = con_totallines - (vid.conheight>>3) - 1;
426                 return;
427         }
428
429         if (key == K_PGDN || key == K_KP_PGDN || key == K_MWHEELDOWN)
430         {
431                 con_backscroll -= ((int) vid.conheight >> 5);
432                 if (con_backscroll < 0)
433                         con_backscroll = 0;
434                 return;
435         }
436
437         if (key == K_HOME || key == K_KP_HOME)
438         {
439                 con_backscroll = con_totallines - (vid.conheight>>3) - 1;
440                 return;
441         }
442
443         if (key == K_END || key == K_KP_END)
444         {
445                 con_backscroll = 0;
446                 return;
447         }
448
449         // non printable
450         if (ascii < 32)
451                 return;
452
453         if (key_linepos < MAX_INPUTLINE-1)
454         {
455                 int i;
456
457                 if (key_insert) // check insert mode
458                 {
459                         // can't do strcpy to move string to right
460                         i = strlen(key_lines[edit_line]) - 1;
461
462                         if (i == 254)
463                                 i--;
464
465                         for (; i >= key_linepos; i--)
466                                 key_lines[edit_line][i + 1] = key_lines[edit_line][i];
467                 }
468
469                 // only null terminate if at the end
470                 i = key_lines[edit_line][key_linepos];
471                 key_lines[edit_line][key_linepos] = ascii;
472                 key_linepos++;
473
474                 if (!i)
475                         key_lines[edit_line][key_linepos] = 0;
476         }
477 }
478
479 //============================================================================
480
481 qboolean        chat_team;
482 char            chat_buffer[MAX_INPUTLINE];
483 unsigned int    chat_bufferlen = 0;
484
485 static void
486 Key_Message (int key, char ascii)
487 {
488
489         if (key == K_ENTER)
490         {
491                 Cmd_ForwardStringToServer(va("%s %s", chat_team ? "say_team" : "say ", chat_buffer));
492
493                 key_dest = key_game;
494                 chat_bufferlen = 0;
495                 chat_buffer[0] = 0;
496                 return;
497         }
498
499         if (key == K_ESCAPE) {
500                 key_dest = key_game;
501                 chat_bufferlen = 0;
502                 chat_buffer[0] = 0;
503                 return;
504         }
505
506         if (key == K_BACKSPACE) {
507                 if (chat_bufferlen) {
508                         chat_bufferlen--;
509                         chat_buffer[chat_bufferlen] = 0;
510                 }
511                 return;
512         }
513
514         if (chat_bufferlen == sizeof (chat_buffer) - 1)
515                 return;                                                 // all full
516
517         if (!ascii)
518                 return;                                                 // non printable
519
520         chat_buffer[chat_bufferlen++] = ascii;
521         chat_buffer[chat_bufferlen] = 0;
522 }
523
524 //============================================================================
525
526
527 /*
528 ===================
529 Returns a key number to be used to index keybindings[] by looking at
530 the given string.  Single ascii characters return themselves, while
531 the K_* names are matched up.
532 ===================
533 */
534 int
535 Key_StringToKeynum (const char *str)
536 {
537         const keyname_t  *kn;
538
539         if (!str || !str[0])
540                 return -1;
541         if (!str[1])
542                 return tolower(str[0]);
543
544         for (kn = keynames; kn->name; kn++) {
545                 if (!strcasecmp (str, kn->name))
546                         return kn->keynum;
547         }
548         return -1;
549 }
550
551 /*
552 ===================
553 Returns a string (either a single ascii char, or a K_* name) for the
554 given keynum.
555 FIXME: handle quote special (general escape sequence?)
556 ===================
557 */
558 const char *
559 Key_KeynumToString (int keynum)
560 {
561         const keyname_t  *kn;
562         static char tinystr[2];
563
564         if (keynum == -1)
565                 return "<KEY NOT FOUND>";
566         if (keynum > 32 && keynum < 127) {      // printable ascii
567                 tinystr[0] = keynum;
568                 tinystr[1] = 0;
569                 return tinystr;
570         }
571
572         for (kn = keynames; kn->name; kn++)
573                 if (keynum == kn->keynum)
574                         return kn->name;
575
576         return "<UNKNOWN KEYNUM>";
577 }
578
579
580 void
581 Key_SetBinding (int keynum, int bindmap, const char *binding)
582 {
583         char       *new;
584         int         l;
585
586         if (keynum == -1)
587                 return;
588
589 // free old bindings
590         if (keybindings[bindmap][keynum]) {
591                 Z_Free (keybindings[bindmap][keynum]);
592                 keybindings[bindmap][keynum] = NULL;
593         }
594 // allocate memory for new binding
595         l = strlen (binding);
596         new = Z_Malloc (l + 1);
597         strcpy (new, binding);
598         new[l] = 0;
599         keybindings[bindmap][keynum] = new;
600 }
601
602 static void
603 Key_In_Unbind_f (void)
604 {
605         int         b, m;
606
607         if (Cmd_Argc () != 3) {
608                 Con_Print("in_unbind <bindmap> <key> : remove commands from a key\n");
609                 return;
610         }
611
612         m = strtol(Cmd_Argv (1), NULL, 0);
613         if ((m < 0) || (m >= 8)) {
614                 Con_Printf("%d isn't a valid bindmap\n", m);
615                 return;
616         }
617
618         b = Key_StringToKeynum (Cmd_Argv (2));
619         if (b == -1) {
620                 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
621                 return;
622         }
623
624         Key_SetBinding (b, m, "");
625 }
626
627 static void
628 Key_In_Bind_f (void)
629 {
630         int         i, c, b, m;
631         char        cmd[1024];
632
633         c = Cmd_Argc ();
634
635         if (c != 3 && c != 4) {
636                 Con_Print("in_bind <bindmap> <key> [command] : attach a command to a key\n");
637                 return;
638         }
639
640         m = strtol(Cmd_Argv (1), NULL, 0);
641         if ((m < 0) || (m >= 8)) {
642                 Con_Printf("%d isn't a valid bindmap\n", m);
643                 return;
644         }
645
646         b = Key_StringToKeynum (Cmd_Argv (2));
647         if (b == -1) {
648                 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
649                 return;
650         }
651
652         if (c == 3) {
653                 if (keybindings[m][b])
654                         Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (2), keybindings[m][b]);
655                 else
656                         Con_Printf("\"%s\" is not bound\n", Cmd_Argv (2));
657                 return;
658         }
659 // copy the rest of the command line
660         cmd[0] = 0;                                                     // start out with a null string
661         for (i = 3; i < c; i++) {
662                 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
663                 if (i != (c - 1))
664                         strlcat (cmd, " ", sizeof (cmd));
665         }
666
667         Key_SetBinding (b, m, cmd);
668 }
669
670 static void
671 Key_In_Bindmap_f (void)
672 {
673         int         m1, m2, c;
674
675         c = Cmd_Argc ();
676
677         if (c != 3) {
678                 Con_Print("in_bindmap <bindmap> <fallback>: set current bindmap and fallback\n");
679                 return;
680         }
681
682         m1 = strtol(Cmd_Argv (1), NULL, 0);
683         if ((m1 < 0) || (m1 >= 8)) {
684                 Con_Printf("%d isn't a valid bindmap\n", m1);
685                 return;
686         }
687
688         m2 = strtol(Cmd_Argv (2), NULL, 0);
689         if ((m2 < 0) || (m2 >= 8)) {
690                 Con_Printf("%d isn't a valid bindmap\n", m2);
691                 return;
692         }
693
694         key_bmap = m1;
695         key_bmap2 = m2;
696 }
697
698 static void
699 Key_Unbind_f (void)
700 {
701         int         b;
702
703         if (Cmd_Argc () != 2) {
704                 Con_Print("unbind <key> : remove commands from a key\n");
705                 return;
706         }
707
708         b = Key_StringToKeynum (Cmd_Argv (1));
709         if (b == -1) {
710                 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
711                 return;
712         }
713
714         Key_SetBinding (b, 0, "");
715 }
716
717 static void
718 Key_Unbindall_f (void)
719 {
720         int         i, j;
721
722         for (j = 0; j < 8; j++)
723                 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
724                         if (keybindings[j][i])
725                                 Key_SetBinding (i, j, "");
726 }
727
728
729 static void
730 Key_Bind_f (void)
731 {
732         int         i, c, b;
733         char        cmd[1024];
734
735         c = Cmd_Argc ();
736
737         if (c != 2 && c != 3) {
738                 Con_Print("bind <key> [command] : attach a command to a key\n");
739                 return;
740         }
741         b = Key_StringToKeynum (Cmd_Argv (1));
742         if (b == -1) {
743                 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
744                 return;
745         }
746
747         if (c == 2) {
748                 if (keybindings[0][b])
749                         Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (1), keybindings[0][b]);
750                 else
751                         Con_Printf("\"%s\" is not bound\n", Cmd_Argv (1));
752                 return;
753         }
754 // copy the rest of the command line
755         cmd[0] = 0;                                                     // start out with a null string
756         for (i = 2; i < c; i++) {
757                 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
758                 if (i != (c - 1))
759                         strlcat (cmd, " ", sizeof (cmd));
760         }
761
762         Key_SetBinding (b, 0, cmd);
763 }
764
765 /*
766 ============
767 Writes lines containing "bind key value"
768 ============
769 */
770 void
771 Key_WriteBindings (qfile_t *f)
772 {
773         int         i, j;
774
775         for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
776                 if (keybindings[0][i])
777                         FS_Printf(f, "bind \"%s\" \"%s\"\n",
778                                         Key_KeynumToString (i), keybindings[0][i]);
779         for (j = 1; j < 8; j++)
780                 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
781                         if (keybindings[j][i])
782                                 FS_Printf(f, "in_bind %d \"%s\" \"%s\"\n",
783                                                 j, Key_KeynumToString (i), keybindings[j][i]);
784 }
785
786
787 void
788 Key_Init (void)
789 {
790         int         i;
791
792         for (i = 0; i < 32; i++) {
793                 key_lines[i][0] = ']';
794                 key_lines[i][1] = 0;
795         }
796         key_linepos = 1;
797
798 //
799 // init ascii characters in console mode
800 //
801         for (i = 32; i < 128; i++)
802                 consolekeys[i] = true;
803         consolekeys[K_ENTER] = true; consolekeys[K_KP_ENTER] = true;
804         consolekeys[K_TAB] = true;
805         consolekeys[K_LEFTARROW] = true; consolekeys[K_KP_LEFTARROW] = true;
806         consolekeys[K_RIGHTARROW] = true; consolekeys[K_KP_RIGHTARROW] = true;
807         consolekeys[K_UPARROW] = true; consolekeys[K_KP_UPARROW] = true;
808         consolekeys[K_DOWNARROW] = true; consolekeys[K_KP_DOWNARROW] = true;
809         consolekeys[K_BACKSPACE] = true;
810         consolekeys[K_DEL] = true; consolekeys[K_KP_DEL] = true;
811         consolekeys[K_INS] = true; consolekeys[K_KP_INS] = true;
812         consolekeys[K_HOME] = true; consolekeys[K_KP_HOME] = true;
813         consolekeys[K_END] = true; consolekeys[K_KP_END] = true;
814         consolekeys[K_PGUP] = true; consolekeys[K_KP_PGUP] = true;
815         consolekeys[K_PGDN] = true; consolekeys[K_KP_PGDN] = true;
816         consolekeys[K_SHIFT] = true;
817         consolekeys[K_MWHEELUP] = true;
818         consolekeys[K_MWHEELDOWN] = true;
819         consolekeys[K_KP_PLUS] = true;
820         consolekeys[K_KP_MINUS] = true;
821         consolekeys[K_KP_DIVIDE] = true;
822         consolekeys[K_KP_MULTIPLY] = true;
823         consolekeys['`'] = false;
824         consolekeys['~'] = false;
825
826         menubound[K_ESCAPE] = true;
827         for (i = 0; i < 12; i++)
828                 menubound[K_F1 + i] = true;
829
830 //
831 // register our functions
832 //
833         Cmd_AddCommand ("in_bind", Key_In_Bind_f);
834         Cmd_AddCommand ("in_unbind", Key_In_Unbind_f);
835         Cmd_AddCommand ("in_bindmap", Key_In_Bindmap_f);
836
837         Cmd_AddCommand ("bind", Key_Bind_f);
838         Cmd_AddCommand ("unbind", Key_Unbind_f);
839         Cmd_AddCommand ("unbindall", Key_Unbindall_f);
840 }
841
842
843 /*
844 ===================
845 Called by the system between frames for both key up and key down events
846 Should NOT be called during an interrupt!
847 ===================
848 */
849 void
850 Key_Event (int key, char ascii, qboolean down)
851 {
852         const char      *kb;
853         char            cmd[1024];
854
855         keydown[key] = down;
856
857         if (!down)
858                 key_repeats[key] = 0;
859
860         key_lastpress = key;
861         key_count++;
862         if (key_count <= 0) {
863                 return;                                                 // just catching keys for Con_NotifyBox
864         }
865
866         // update auto-repeat status
867         if (down) {
868                 key_repeats[key]++;
869                 if (key_repeats[key] > 1) {
870                         if ((key_consoleactive && !consolekeys[key]) ||
871                                         (!key_consoleactive && key_dest == key_game &&
872                                          (cls.state == ca_connected && cls.signon == SIGNONS)))
873                                 return;                                         // ignore most autorepeats
874                 }
875         }
876
877         if (key == K_CTRL)
878                 ctrl_down = down;
879
880         //
881         // handle escape specially, so the user can never unbind it
882         //
883         if (key == K_ESCAPE) {
884                 if (!down)
885                         return;
886                 switch (key_dest) {
887                         case key_message:
888                                 Key_Message (key, ascii);
889                                 break;
890                         case key_menu:
891                                 MR_Keydown (key, ascii);
892                                 break;
893                         case key_game:
894                                 MR_ToggleMenu_f ();
895                                 break;
896                         default:
897                                 if(UI_Callback_IsSlotUsed(key_dest - 3))
898                                         UI_Callback_KeyDown (key, ascii);
899                                 else
900                                         Sys_Error ("Bad key_dest");
901                 }
902                 return;
903         }
904
905         if (down)
906         {
907                 if (!(kb = keybindings[key_bmap][key]))
908                         kb = keybindings[key_bmap2][key];
909                 if (kb && !strncmp(kb, "toggleconsole", strlen("toggleconsole")))
910                 {
911                         Cbuf_AddText (kb);
912                         Cbuf_AddText ("\n");
913                         return;
914                 }
915         }
916
917         if (key_consoleactive && consolekeys[key] && down)
918                 Key_Console (key, ascii);
919         else
920         {
921                 //
922                 // key up events only generate commands if the game key binding is a button
923                 // command (leading + sign).  These will occur even in console mode, to
924                 // keep the character from continuing an action started before a console
925                 // switch.  Button commands include the kenum as a parameter, so multiple
926                 // downs can be matched with ups
927                 //
928                 if (!down) {
929                         if (!(kb = keybindings[key_bmap][key]))
930                                 kb = keybindings[key_bmap2][key];
931
932                         if (kb && kb[0] == '+') {
933                                 dpsnprintf (cmd, sizeof(cmd), "-%s %i\n", kb + 1, key);
934                                 Cbuf_AddText (cmd);
935                         }
936                         return;
937                 }
938
939                 //
940                 // during demo playback, most keys bring up the main menu
941                 //
942                 if (cls.demoplayback && down && consolekeys[key] && key_dest == key_game) {
943                         MR_ToggleMenu_f ();
944                         return;
945                 }
946
947                 //
948                 // if not a consolekey, send to the interpreter no matter what mode is
949                 //
950                 if ((key_dest == key_menu && menubound[key])
951                                 || (key_consoleactive && !consolekeys[key])
952                                 || (key_dest == key_game &&
953                                         ((cls.state == ca_connected) || !consolekeys[key]))) {
954                         if (!(kb = keybindings[key_bmap][key]))
955                                 kb = keybindings[key_bmap2][key];
956                         if (kb) {
957                                 if (kb[0] == '+') {                     // button commands add keynum as a parm
958                                         dpsnprintf (cmd, sizeof(cmd), "%s %i\n", kb, key);
959                                         Cbuf_AddText (cmd);
960                                 } else {
961                                         Cbuf_AddText (kb);
962                                         Cbuf_AddText ("\n");
963                                 }
964                         }
965                         return;
966                 }
967
968                 if (!down)
969                         return;                                                 // other systems only care about key
970                 // down events
971
972                 switch (key_dest) {
973                         case key_message:
974                                 Key_Message (key, ascii);
975                                 break;
976                         case key_menu:
977                                 MR_Keydown (key, ascii);
978                                 break;
979                         case key_game:
980                                 Key_Console (key, ascii);
981                                 break;
982                         default:
983                                 if(UI_Callback_IsSlotUsed(key_dest - 3))
984                                         UI_Callback_KeyDown (key, ascii);
985                                 else
986                                         Sys_Error ("Bad key_dest");
987                 }
988         }
989 }
990
991 /*
992 ===================
993 Key_ClearStates
994 ===================
995 */
996 void
997 Key_ClearStates (void)
998 {
999         int i;
1000
1001         for (i = 0; i < (int)(sizeof(keydown)/sizeof(keydown[0])); i++)
1002         {
1003                 keydown[i] = false;
1004                 key_repeats[i] = 0;
1005         }
1006 }