]> icculus.org git repositories - divverent/darkplaces.git/blob - keys.c
improved setinfo handling so that quakeworld works much better now (name/topcolor...
[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 #include "cl_video.h"
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 unsigned char 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                         Z_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         // Enhanced by [515]
330
331         // left arrow will just move left one without erasing, backspace will
332         // actually erase character
333         if (key == K_LEFTARROW || key == K_KP_LEFTARROW)
334         {
335                 if (key_linepos < 2)
336                         return;
337                 if(keydown[K_CTRL])
338                 {
339                         int             pos;
340                         char    k;
341                         pos = key_linepos-1;
342                         if(pos)
343                                 while(--pos)
344                                 {
345                                         k = key_lines[edit_line][pos];
346                                         if(k == '\"' || k == ';' || k == ' ' || k == '\'')
347                                                 break;
348                                 }
349                         key_linepos = pos + 1;
350                 }
351                 else
352                         key_linepos--;
353                 return;
354         }
355
356         // delete char before cursor
357         if (key == K_BACKSPACE || (key == 'h' && keydown[K_CTRL]))
358         {
359                 if (key_linepos > 1)
360                 {
361                         strcpy(key_lines[edit_line] + key_linepos - 1, key_lines[edit_line] + key_linepos);
362                         key_linepos--;
363                 }
364                 return;
365         }
366
367         // delete char on cursor
368         if (key == K_DEL || key == K_KP_DEL)
369         {
370                 if (key_linepos < (int)strlen(key_lines[edit_line]))
371                         strcpy(key_lines[edit_line] + key_linepos, key_lines[edit_line] + key_linepos + 1);
372                 return;
373         }
374
375
376         // if we're at the end, get one character from previous line,
377         // otherwise just go right one
378         if (key == K_RIGHTARROW || key == K_KP_RIGHTARROW)
379         {
380                 if (key_linepos >= (int)strlen(key_lines[edit_line]))
381                         return;
382                 if(keydown[K_CTRL])
383                 {
384                         int             pos, len;
385                         char    k;
386                         len = (int)strlen(key_lines[edit_line]);
387                         pos = key_linepos;
388                         while(++pos < len)
389                         {
390                                 k = key_lines[edit_line][pos];
391                                 if(k == '\"' || k == ';' || k == ' ' || k == '\'')
392                                         break;
393                         }
394                         key_linepos = pos;
395                 }
396                 else
397                         key_linepos++;
398                 return;
399         }
400
401         if (key == K_INS || key == K_KP_INS) // toggle insert mode
402         {
403                 key_insert ^= 1;
404                 return;
405         }
406
407         // End Advanced Console Editing
408
409         if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL]))
410         {
411                 if (history_line > 0 && key_lines[history_line-1][1])
412                 {
413                         history_line--;
414                         strcpy(key_lines[edit_line], key_lines[history_line]);
415                         key_linepos = (int)strlen(key_lines[edit_line]);
416                 }
417                 return;
418         }
419
420         if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL]))
421         {
422                 history_line++;
423                 if (history_line >= edit_line)
424                 {
425                         history_line = edit_line;
426                         key_lines[edit_line][0] = ']';
427                         key_lines[edit_line][1] = 0;
428                         key_linepos = 1;
429                 }
430                 else
431                 {
432                         strcpy(key_lines[edit_line], key_lines[history_line]);
433                         key_linepos = (int)strlen(key_lines[edit_line]);
434                 }
435                 return;
436         }
437
438         if (key == K_PGUP || key == K_KP_PGUP || key == K_MWHEELUP)
439         {
440                 con_backscroll += ((int) vid_conheight.integer >> 5);
441                 if (con_backscroll > con_totallines - (vid_conheight.integer>>3) - 1)
442                         con_backscroll = con_totallines - (vid_conheight.integer>>3) - 1;
443                 return;
444         }
445
446         if (key == K_PGDN || key == K_KP_PGDN || key == K_MWHEELDOWN)
447         {
448                 con_backscroll -= ((int) vid_conheight.integer >> 5);
449                 if (con_backscroll < 0)
450                         con_backscroll = 0;
451                 return;
452         }
453
454         if (key == K_HOME || key == K_KP_HOME)
455         {
456                 if (keydown[K_CTRL])
457                         con_backscroll = con_totallines - (vid_conheight.integer>>3) - 1;
458                 else
459                         key_linepos = 1;
460                 return;
461         }
462
463         if (key == K_END || key == K_KP_END)
464         {
465                 if (keydown[K_CTRL])
466                         con_backscroll = 0;
467                 else
468                         key_linepos = (int)strlen(key_lines[edit_line]);
469                 return;
470         }
471
472         // non printable
473         if (ascii < 32)
474                 return;
475
476         if (key_linepos < MAX_INPUTLINE-1)
477         {
478                 int len;
479                 len = (int)strlen(&key_lines[edit_line][key_linepos]);
480                 // check insert mode, or always insert if at end of line
481                 if (key_insert || len == 0)
482                 {
483                         // can't use strcpy to move string to right
484                         len++;
485                         memmove(&key_lines[edit_line][key_linepos + 1], &key_lines[edit_line][key_linepos], len);
486                 }
487                 key_lines[edit_line][key_linepos] = ascii;
488                 key_linepos++;
489         }
490 }
491
492 //============================================================================
493
494 qboolean        chat_team;
495 char            chat_buffer[MAX_INPUTLINE];
496 unsigned int    chat_bufferlen = 0;
497
498 static void
499 Key_Message (int key, char ascii)
500 {
501
502         if (key == K_ENTER)
503         {
504                 Cmd_ForwardStringToServer(va("%s %s", chat_team ? "say_team" : "say ", chat_buffer));
505
506                 key_dest = key_game;
507                 chat_bufferlen = 0;
508                 chat_buffer[0] = 0;
509                 return;
510         }
511
512         if (key == K_ESCAPE) {
513                 key_dest = key_game;
514                 chat_bufferlen = 0;
515                 chat_buffer[0] = 0;
516                 return;
517         }
518
519         if (key == K_BACKSPACE) {
520                 if (chat_bufferlen) {
521                         chat_bufferlen--;
522                         chat_buffer[chat_bufferlen] = 0;
523                 }
524                 return;
525         }
526
527         if (chat_bufferlen == sizeof (chat_buffer) - 1)
528                 return;                                                 // all full
529
530         if (!ascii)
531                 return;                                                 // non printable
532
533         chat_buffer[chat_bufferlen++] = ascii;
534         chat_buffer[chat_bufferlen] = 0;
535 }
536
537 //============================================================================
538
539
540 /*
541 ===================
542 Returns a key number to be used to index keybindings[] by looking at
543 the given string.  Single ascii characters return themselves, while
544 the K_* names are matched up.
545 ===================
546 */
547 int
548 Key_StringToKeynum (const char *str)
549 {
550         const keyname_t  *kn;
551
552         if (!str || !str[0])
553                 return -1;
554         if (!str[1])
555                 return tolower(str[0]);
556
557         for (kn = keynames; kn->name; kn++) {
558                 if (!strcasecmp (str, kn->name))
559                         return kn->keynum;
560         }
561         return -1;
562 }
563
564 /*
565 ===================
566 Returns a string (either a single ascii char, or a K_* name) for the
567 given keynum.
568 FIXME: handle quote special (general escape sequence?)
569 ===================
570 */
571 const char *
572 Key_KeynumToString (int keynum)
573 {
574         const keyname_t  *kn;
575         static char tinystr[2];
576
577         if (keynum == -1)
578                 return "<KEY NOT FOUND>";
579         if (keynum > 32 && keynum < 127) {      // printable ascii
580                 tinystr[0] = keynum;
581                 tinystr[1] = 0;
582                 return tinystr;
583         }
584
585         for (kn = keynames; kn->name; kn++)
586                 if (keynum == kn->keynum)
587                         return kn->name;
588
589         return "<UNKNOWN KEYNUM>";
590 }
591
592
593 void
594 Key_SetBinding (int keynum, int bindmap, const char *binding)
595 {
596         char *newbinding;
597         size_t l;
598
599         if (keynum == -1)
600                 return;
601
602 // free old bindings
603         if (keybindings[bindmap][keynum]) {
604                 Z_Free (keybindings[bindmap][keynum]);
605                 keybindings[bindmap][keynum] = NULL;
606         }
607 // allocate memory for new binding
608         l = strlen (binding);
609         newbinding = (char *)Z_Malloc (l + 1);
610         strcpy (newbinding, binding);
611         newbinding[l] = 0;
612         keybindings[bindmap][keynum] = newbinding;
613 }
614
615 static void
616 Key_In_Unbind_f (void)
617 {
618         int         b, m;
619
620         if (Cmd_Argc () != 3) {
621                 Con_Print("in_unbind <bindmap> <key> : remove commands from a key\n");
622                 return;
623         }
624
625         m = strtol(Cmd_Argv (1), NULL, 0);
626         if ((m < 0) || (m >= 8)) {
627                 Con_Printf("%d isn't a valid bindmap\n", m);
628                 return;
629         }
630
631         b = Key_StringToKeynum (Cmd_Argv (2));
632         if (b == -1) {
633                 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
634                 return;
635         }
636
637         Key_SetBinding (b, m, "");
638 }
639
640 static void
641 Key_In_Bind_f (void)
642 {
643         int         i, c, b, m;
644         char        cmd[MAX_INPUTLINE];
645
646         c = Cmd_Argc ();
647
648         if (c != 3 && c != 4) {
649                 Con_Print("in_bind <bindmap> <key> [command] : attach a command to a key\n");
650                 return;
651         }
652
653         m = strtol(Cmd_Argv (1), NULL, 0);
654         if ((m < 0) || (m >= 8)) {
655                 Con_Printf("%d isn't a valid bindmap\n", m);
656                 return;
657         }
658
659         b = Key_StringToKeynum (Cmd_Argv (2));
660         if (b == -1) {
661                 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (2));
662                 return;
663         }
664
665         if (c == 3) {
666                 if (keybindings[m][b])
667                         Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (2), keybindings[m][b]);
668                 else
669                         Con_Printf("\"%s\" is not bound\n", Cmd_Argv (2));
670                 return;
671         }
672 // copy the rest of the command line
673         cmd[0] = 0;                                                     // start out with a null string
674         for (i = 3; i < c; i++) {
675                 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
676                 if (i != (c - 1))
677                         strlcat (cmd, " ", sizeof (cmd));
678         }
679
680         Key_SetBinding (b, m, cmd);
681 }
682
683 static void
684 Key_In_Bindmap_f (void)
685 {
686         int         m1, m2, c;
687
688         c = Cmd_Argc ();
689
690         if (c != 3) {
691                 Con_Print("in_bindmap <bindmap> <fallback>: set current bindmap and fallback\n");
692                 return;
693         }
694
695         m1 = strtol(Cmd_Argv (1), NULL, 0);
696         if ((m1 < 0) || (m1 >= 8)) {
697                 Con_Printf("%d isn't a valid bindmap\n", m1);
698                 return;
699         }
700
701         m2 = strtol(Cmd_Argv (2), NULL, 0);
702         if ((m2 < 0) || (m2 >= 8)) {
703                 Con_Printf("%d isn't a valid bindmap\n", m2);
704                 return;
705         }
706
707         key_bmap = m1;
708         key_bmap2 = m2;
709 }
710
711 static void
712 Key_Unbind_f (void)
713 {
714         int         b;
715
716         if (Cmd_Argc () != 2) {
717                 Con_Print("unbind <key> : remove commands from a key\n");
718                 return;
719         }
720
721         b = Key_StringToKeynum (Cmd_Argv (1));
722         if (b == -1) {
723                 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
724                 return;
725         }
726
727         Key_SetBinding (b, 0, "");
728 }
729
730 static void
731 Key_Unbindall_f (void)
732 {
733         int         i, j;
734
735         for (j = 0; j < 8; j++)
736                 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
737                         if (keybindings[j][i])
738                                 Key_SetBinding (i, j, "");
739 }
740
741
742 static void
743 Key_Bind_f (void)
744 {
745         int         i, c, b;
746         char        cmd[MAX_INPUTLINE];
747
748         c = Cmd_Argc ();
749
750         if (c != 2 && c != 3) {
751                 Con_Print("bind <key> [command] : attach a command to a key\n");
752                 return;
753         }
754         b = Key_StringToKeynum (Cmd_Argv (1));
755         if (b == -1) {
756                 Con_Printf("\"%s\" isn't a valid key\n", Cmd_Argv (1));
757                 return;
758         }
759
760         if (c == 2) {
761                 if (keybindings[0][b])
762                         Con_Printf("\"%s\" = \"%s\"\n", Cmd_Argv (1), keybindings[0][b]);
763                 else
764                         Con_Printf("\"%s\" is not bound\n", Cmd_Argv (1));
765                 return;
766         }
767 // copy the rest of the command line
768         cmd[0] = 0;                                                     // start out with a null string
769         for (i = 2; i < c; i++) {
770                 strlcat (cmd, Cmd_Argv (i), sizeof (cmd));
771                 if (i != (c - 1))
772                         strlcat (cmd, " ", sizeof (cmd));
773         }
774
775         Key_SetBinding (b, 0, cmd);
776 }
777
778 /*
779 ============
780 Writes lines containing "bind key value"
781 ============
782 */
783 void
784 Key_WriteBindings (qfile_t *f)
785 {
786         int         i, j;
787
788         for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
789                 if (keybindings[0][i])
790                         FS_Printf(f, "bind \"%s\" \"%s\"\n",
791                                         Key_KeynumToString (i), keybindings[0][i]);
792         for (j = 1; j < 8; j++)
793                 for (i = 0; i < (int)(sizeof(keybindings[0])/sizeof(keybindings[0][0])); i++)
794                         if (keybindings[j][i])
795                                 FS_Printf(f, "in_bind %d \"%s\" \"%s\"\n",
796                                                 j, Key_KeynumToString (i), keybindings[j][i]);
797 }
798
799
800 void
801 Key_Init (void)
802 {
803         int         i;
804
805         for (i = 0; i < 32; i++) {
806                 key_lines[i][0] = ']';
807                 key_lines[i][1] = 0;
808         }
809         key_linepos = 1;
810
811 //
812 // register our functions
813 //
814         Cmd_AddCommand ("in_bind", Key_In_Bind_f, "binds a command to the specified key in the selected bindmap");
815         Cmd_AddCommand ("in_unbind", Key_In_Unbind_f, "removes command on the specified key in the selected bindmap");
816         Cmd_AddCommand ("in_bindmap", Key_In_Bindmap_f, "selects active foreground and background (used only if a key is not bound in the foreground) bindmaps for typing");
817
818         Cmd_AddCommand ("bind", Key_Bind_f, "binds a command to the specified key in bindmap 0");
819         Cmd_AddCommand ("unbind", Key_Unbind_f, "removes a command on the specified key in bindmap 0");
820         Cmd_AddCommand ("unbindall", Key_Unbindall_f, "removes all commands from all keys in all bindmaps (leaving only shift-escape and escape)");
821 }
822
823 const char *Key_GetBind (int key)
824 {
825         const char *bind;
826         bind = keybindings[key_bmap][key];
827         if (!bind)
828                 bind = keybindings[key_bmap2][key];
829         return bind;
830 }
831
832 qboolean CL_VM_InputEvent (qboolean pressed, int key);
833
834 /*
835 ===================
836 Called by the system between frames for both key up and key down events
837 Should NOT be called during an interrupt!
838 ===================
839 */
840 void
841 Key_Event (int key, char ascii, qboolean down)
842 {
843         const char *bind;
844         qboolean q;
845
846         // get key binding
847         bind = keybindings[key_bmap][key];
848         if (!bind)
849                 bind = keybindings[key_bmap2][key];
850
851         if(key_dest == key_game)
852         {
853                 q = CL_VM_InputEvent(!down, key);
854                 if(q)
855                 {
856                         if (down)
857                                 keydown[key] = min(keydown[key] + 1, 2);
858                         else
859                                 keydown[key] = 0;
860                         return;
861                 }
862         }
863
864         if (down)
865         {
866                 // increment key repeat count each time a down is received so that things
867                 // which want to ignore key repeat can ignore it
868                 keydown[key] = min(keydown[key] + 1, 2);
869         }
870         else
871         {
872                 // clear repeat count now that the key is released
873                 keydown[key] = 0;
874                 // key up events only generate commands if the game key binding is a button
875                 // command (leading + sign).  These will occur even in console mode, to
876                 // keep the character from continuing an action started before a console
877                 // switch.  Button commands include the kenum as a parameter, so multiple
878                 // downs can be matched with ups
879                 if (bind && bind[0] == '+')
880                         Cbuf_AddText(va("-%s %i\n", bind + 1, key));
881         }
882
883         // key_consoleactive is a flag not a key_dest because the console is a
884         // high priority overlay ontop of the normal screen (designed as a safety
885         // feature so that developers and users can rescue themselves from a bad
886         // situation).
887         //
888         // this also means that toggling the console on/off does not lose the old
889         // key_dest state
890
891         // specially handle escape (togglemenu) and shift-escape (toggleconsole)
892         // engine bindings, these are not handled as normal binds so that the user
893         // can recover from a completely empty bindmap
894         if (key == K_ESCAPE)
895         {
896                 // ignore key repeats on escape
897                 if (keydown[key] > 1)
898                         return;
899                 // escape does these things:
900                 // key_consoleactive - close console
901                 // key_message - abort messagemode
902                 // key_menu - go to parent menu (or key_game)
903                 // key_game - open menu
904                 // in all modes shift-escape toggles console
905                 if (((key_consoleactive & KEY_CONSOLEACTIVE_USER) || keydown[K_SHIFT]) && down)
906                 {
907                         Con_ToggleConsole_f ();
908                         return;
909                 }
910                 switch (key_dest)
911                 {
912                         case key_message:
913                                 if (down)
914                                         Key_Message (key, ascii);
915                                 break;
916                         case key_menu:
917                                 MR_KeyEvent (key, ascii, down);
918                                 break;
919                         case key_game:
920                                 if (down)
921                                         MR_ToggleMenu_f ();
922                                 break;
923                         default:
924                                 Con_Printf ("Key_Event: Bad key_dest\n");
925                 }
926                 return;
927         }
928
929         // send function keydowns to interpreter no matter what mode is
930         if (key >= K_F1 && key <= K_F12 && down)
931         {
932                 // ignore key repeats on F1-F12 binds
933                 if (keydown[key] > 1)
934                         return;
935                 if (bind)
936                 {
937                         // button commands add keynum as a parm
938                         if (bind[0] == '+')
939                                 Cbuf_AddText (va("%s %i\n", bind, key));
940                         else
941                         {
942                                 Cbuf_AddText (bind);
943                                 Cbuf_AddText ("\n");
944                         }
945                 }
946                 return;
947         }
948
949 #if 1
950         // ignore binds (other than the above escape/F1-F12 keys) while in console
951         if (key_consoleactive && down)
952 #else
953         // respond to toggleconsole binds while in console unless the pressed key
954         // happens to be the color prefix character (such as on German keyboards)
955         if (key_consoleactive && down && (strncmp(bind, "toggleconsole", strlen("toggleconsole")) || ascii == STRING_COLOR_TAG))
956 #endif
957         {
958                 Key_Console (key, ascii);
959                 return;
960         }
961
962         // ignore binds while a video is played, let the video system handle the key event
963         if (cl_videoplaying)
964         {
965                 CL_Video_KeyEvent (key, ascii, keydown[key] != 0); 
966                 return;
967         }
968
969         // anything else is a key press into the game, chat line, or menu
970         switch (key_dest)
971         {
972                 case key_message:
973                         if (down)
974                                 Key_Message (key, ascii);
975                         break;
976                 case key_menu:
977                         MR_KeyEvent (key, ascii, down);
978                         break;
979                 case key_game:
980                         // ignore key repeats on binds
981                         if (bind && keydown[key] == 1 && down)
982                         {
983                                 // button commands add keynum as a parm
984                                 if (bind[0] == '+')
985                                         Cbuf_AddText (va("%s %i\n", bind, key));
986                                 else
987                                 {
988                                         Cbuf_AddText (bind);
989                                         Cbuf_AddText ("\n");
990                                 }
991                         }
992                         break;
993                 default:
994                         Con_Printf ("Key_Event: Bad key_dest\n");
995         }
996 }
997
998 /*
999 ===================
1000 Key_ClearStates
1001 ===================
1002 */
1003 void
1004 Key_ClearStates (void)
1005 {
1006         memset(keydown, 0, sizeof(keydown));
1007 }