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