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