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