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