]> icculus.org git repositories - divverent/darkplaces.git/blob - keys.c
made most particles additive (speedup in fog)
[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 the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 #include "quakedef.h"
21
22 /*
23
24 key up events are sent even if in console mode
25
26 */
27
28
29 #define         MAXCMDLINE      256
30 char    key_lines[32][MAXCMDLINE];
31 int             key_linepos;
32 int             shift_down = false;
33 int             key_lastpress;
34 int             key_insert;     // insert key toggle (for editing)
35
36 int             edit_line = 0;
37 int             history_line = 0;
38
39 keydest_t       key_dest;
40
41 int             key_count;                      // incremented every key event
42
43 char    *keybindings[256];
44 qboolean        consolekeys[256];       // if true, can't be rebound while in console
45 qboolean        menubound[256]; // if true, can't be rebound while in menu
46 int             keyshift[256];          // key to map to if shift held down in console
47 int             key_repeats[256];       // if > 1, it is autorepeating
48 qboolean        keydown[256];
49
50 typedef struct
51 {
52         char    *name;
53         int             keynum;
54 } keyname_t;
55
56 keyname_t keynames[] =
57 {
58         {"TAB", K_TAB},
59         {"ENTER", K_ENTER},
60         {"ESCAPE", K_ESCAPE},
61         {"SPACE", K_SPACE},
62         {"BACKSPACE", K_BACKSPACE},
63         {"UPARROW", K_UPARROW},
64         {"DOWNARROW", K_DOWNARROW},
65         {"LEFTARROW", K_LEFTARROW},
66         {"RIGHTARROW", K_RIGHTARROW},
67
68         {"ALT", K_ALT},
69         {"CTRL", K_CTRL},
70         {"SHIFT", K_SHIFT},
71
72         {"F1", K_F1},
73         {"F2", K_F2},
74         {"F3", K_F3},
75         {"F4", K_F4},
76         {"F5", K_F5},
77         {"F6", K_F6},
78         {"F7", K_F7},
79         {"F8", K_F8},
80         {"F9", K_F9},
81         {"F10", K_F10},
82         {"F11", K_F11},
83         {"F12", K_F12},
84
85         {"INS", K_INS},
86         {"DEL", K_DEL},
87         {"PGDN", K_PGDN},
88         {"PGUP", K_PGUP},
89         {"HOME", K_HOME},
90         {"END", K_END},
91
92         {"MOUSE1", K_MOUSE1},
93         {"MOUSE2", K_MOUSE2},
94         {"MOUSE3", K_MOUSE3},
95
96         {"JOY1", K_JOY1},
97         {"JOY2", K_JOY2},
98         {"JOY3", K_JOY3},
99         {"JOY4", K_JOY4},
100
101         {"AUX1", K_AUX1},
102         {"AUX2", K_AUX2},
103         {"AUX3", K_AUX3},
104         {"AUX4", K_AUX4},
105         {"AUX5", K_AUX5},
106         {"AUX6", K_AUX6},
107         {"AUX7", K_AUX7},
108         {"AUX8", K_AUX8},
109         {"AUX9", K_AUX9},
110         {"AUX10", K_AUX10},
111         {"AUX11", K_AUX11},
112         {"AUX12", K_AUX12},
113         {"AUX13", K_AUX13},
114         {"AUX14", K_AUX14},
115         {"AUX15", K_AUX15},
116         {"AUX16", K_AUX16},
117         {"AUX17", K_AUX17},
118         {"AUX18", K_AUX18},
119         {"AUX19", K_AUX19},
120         {"AUX20", K_AUX20},
121         {"AUX21", K_AUX21},
122         {"AUX22", K_AUX22},
123         {"AUX23", K_AUX23},
124         {"AUX24", K_AUX24},
125         {"AUX25", K_AUX25},
126         {"AUX26", K_AUX26},
127         {"AUX27", K_AUX27},
128         {"AUX28", K_AUX28},
129         {"AUX29", K_AUX29},
130         {"AUX30", K_AUX30},
131         {"AUX31", K_AUX31},
132         {"AUX32", K_AUX32},
133
134         {"PAUSE", K_PAUSE},
135
136         {"MWHEELUP", K_MWHEELUP},
137         {"MWHEELDOWN", K_MWHEELDOWN},
138
139         {"SEMICOLON", ';'},     // because a raw semicolon seperates commands
140
141         {NULL,0}
142 };
143
144 /*
145 ==============================================================================
146
147                         LINE TYPING INTO THE CONSOLE
148
149 ==============================================================================
150 */
151
152
153 /*
154 ====================
155 Key_Console
156
157 Interactive line editing and console scrollback
158 ====================
159 */
160 void Key_Console (int key)
161 {
162         if (key == K_ENTER)
163         {
164                 Cbuf_AddText (key_lines[edit_line]+1);  // skip the >
165                 Cbuf_AddText ("\n");
166                 Con_Printf ("%s\n",key_lines[edit_line]);
167                 edit_line = (edit_line + 1) & 31;
168                 history_line = edit_line;
169                 key_lines[edit_line][0] = ']';
170                 key_lines[edit_line][1] = 0;    // EvilTypeGuy: null terminate
171                 key_linepos = 1;
172                 // force an update, because the command may take some time
173                 if (cls.state == ca_disconnected)
174                         CL_UpdateScreen ();
175                 return;
176         }
177
178         if (key == K_TAB)
179         {
180                 // Enhanced command completion
181                 // by EvilTypeGuy eviltypeguy@qeradiant.com
182                 // Thanks to Fett, Taniwha
183                 Con_CompleteCommandLine();
184         }
185
186         // Advanced Console Editing by Radix radix@planetquake.com
187         // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
188
189         // left arrow will just move left one without erasing, backspace will
190         // actually erase charcter
191         if (key == K_LEFTARROW)
192         {
193                 if (key_linepos > 1)
194                         key_linepos--;
195                 return;
196         }
197
198         if (key == K_BACKSPACE) // delete char before cursor
199         {
200                 if (key_linepos > 1)
201                 {
202                         strcpy(key_lines[edit_line] + key_linepos - 1, key_lines[edit_line] + key_linepos);
203                         key_linepos--;
204                 }
205                 return;
206         }
207
208         if (key == K_DEL)               // delete char on cursor
209         {
210                 if (key_linepos < strlen(key_lines[edit_line]))
211                         strcpy(key_lines[edit_line] + key_linepos, key_lines[edit_line] + key_linepos + 1);
212                 return;
213         }
214
215
216         // if we're at the end, get one character from previous line,
217         // otherwise just go right one
218         if (key == K_RIGHTARROW)
219         {
220                 if (strlen(key_lines[edit_line]) == key_linepos)
221                 {
222                         if (strlen(key_lines[(edit_line + 31) & 31]) <= key_linepos)
223                                 return; // no character to get
224
225                         key_lines[edit_line][key_linepos] = key_lines[(edit_line + 31) & 31][key_linepos];
226                         key_linepos++;
227                         key_lines[edit_line][key_linepos] = 0;
228                 }
229                 else
230                         key_linepos++;
231
232                 return;
233         }
234
235         if (key == K_INS) // toggle insert mode
236         {
237                 key_insert ^= 1;
238                 return;
239         }
240
241         // End Advanced Console Editing
242
243         if (key == K_UPARROW)
244         {
245                 do
246                 {
247                         history_line = (history_line - 1) & 31;
248                 } while (history_line != edit_line
249                                 && !key_lines[history_line][1]);
250                 if (history_line == edit_line)
251                         history_line = (edit_line+1)&31;
252                 strcpy(key_lines[edit_line], key_lines[history_line]);
253                 key_linepos = strlen(key_lines[edit_line]);
254                 return;
255         }
256
257         if (key == K_DOWNARROW)
258         {
259                 if (history_line == edit_line) return;
260                 do
261                 {
262                         history_line = (history_line + 1) & 31;
263                 }
264                 while (history_line != edit_line
265                         && !key_lines[history_line][1]);
266                 if (history_line == edit_line)
267                 {
268                         key_lines[edit_line][0] = ']';
269                         key_linepos = 1;
270                 }
271                 else
272                 {
273                         strcpy(key_lines[edit_line], key_lines[history_line]);
274                         key_linepos = strlen(key_lines[edit_line]);
275                 }
276                 return;
277         }
278
279         if (key == K_PGUP || key==K_MWHEELUP)
280         {
281                 con_backscroll += 2;
282                 if (con_backscroll > con_totallines - (vid.conheight>>3) - 1)
283                         con_backscroll = con_totallines - (vid.conheight>>3) - 1;
284                 return;
285         }
286
287         if (key == K_PGDN || key==K_MWHEELDOWN)
288         {
289                 con_backscroll -= 2;
290                 if (con_backscroll < 0)
291                         con_backscroll = 0;
292                 return;
293         }
294
295         if (key == K_HOME)
296         {
297                 con_backscroll = con_totallines - (vid.conheight>>3) - 1;
298                 return;
299         }
300
301         if (key == K_END)
302         {
303                 con_backscroll = 0;
304                 return;
305         }
306         
307         if (key < 32 || key > 127)
308                 return; // non printable
309                 
310
311
312         if (key_linepos < MAXCMDLINE-1)
313         {
314                 int i;
315
316                 if (key_insert) // check insert mode
317                 {
318                         // can't do strcpy to move string to right
319                         i = strlen(key_lines[edit_line]) - 1;
320
321                         if (i == 254)
322                                 i--;
323
324                         for (; i >= key_linepos; i--)
325                                 key_lines[edit_line][i + 1] = key_lines[edit_line][i];
326                 }
327
328                 // only null terminate if at the end
329                 i = key_lines[edit_line][key_linepos];
330                 key_lines[edit_line][key_linepos] = key;
331                 key_linepos++;
332
333                 if (!i)
334                         key_lines[edit_line][key_linepos] = 0;
335         }
336 }
337
338 //============================================================================
339
340 // LordHavoc: increased messagemode length (was 32)
341 char chat_buffer[256];
342 qboolean team_message = false;
343
344 void Key_Message (int key)
345 {
346         static int chat_bufferlen = 0;
347
348         if (key == K_ENTER)
349         {
350                 if (team_message)
351                         Cbuf_AddText ("say_team \"");
352                 else
353                         Cbuf_AddText ("say \"");
354                 Cbuf_AddText(chat_buffer);
355                 Cbuf_AddText("\"\n");
356
357                 key_dest = key_game;
358                 chat_bufferlen = 0;
359                 chat_buffer[0] = 0;
360                 return;
361         }
362
363         if (key == K_ESCAPE)
364         {
365                 key_dest = key_game;
366                 chat_bufferlen = 0;
367                 chat_buffer[0] = 0;
368                 return;
369         }
370
371         if (key < 32 || key > 127)
372                 return; // non printable
373
374         if (key == K_BACKSPACE)
375         {
376                 if (chat_bufferlen)
377                 {
378                         chat_bufferlen--;
379                         chat_buffer[chat_bufferlen] = 0;
380                 }
381                 return;
382         }
383
384         // LordHavoc: increased messagemode length (was 31)
385         if (chat_bufferlen == 240)
386                 return; // all full
387
388         chat_buffer[chat_bufferlen++] = key;
389         chat_buffer[chat_bufferlen] = 0;
390 }
391
392 //============================================================================
393
394
395 /*
396 ===================
397 Key_StringToKeynum
398
399 Returns a key number to be used to index keybindings[] by looking at
400 the given string.  Single ascii characters return themselves, while
401 the K_* names are matched up.
402 ===================
403 */
404 int Key_StringToKeynum (char *str)
405 {
406         keyname_t       *kn;
407
408         if (!str || !str[0])
409                 return -1;
410         if (!str[1])
411                 return str[0];
412
413         for (kn=keynames ; kn->name ; kn++)
414         {
415                 if (!Q_strcasecmp(str,kn->name))
416                         return kn->keynum;
417         }
418         return -1;
419 }
420
421 /*
422 ===================
423 Key_KeynumToString
424
425 Returns a string (either a single ascii char, or a K_* name) for the
426 given keynum.
427 FIXME: handle quote special (general escape sequence?)
428 ===================
429 */
430 char *Key_KeynumToString (int keynum)
431 {
432         keyname_t       *kn;
433         static  char    tinystr[2];
434
435         if (keynum == -1)
436                 return "<KEY NOT FOUND>";
437         if (keynum > 32 && keynum < 127)
438         {       // printable ascii
439                 tinystr[0] = keynum;
440                 tinystr[1] = 0;
441                 return tinystr;
442         }
443         
444         for (kn=keynames ; kn->name ; kn++)
445                 if (keynum == kn->keynum)
446                         return kn->name;
447
448         return "<UNKNOWN KEYNUM>";
449 }
450
451
452 /*
453 ===================
454 Key_SetBinding
455 ===================
456 */
457 void Key_SetBinding (int keynum, char *binding)
458 {
459         char    *new;
460         int             l;
461                         
462         if (keynum == -1)
463                 return;
464
465 // free old bindings
466         if (keybindings[keynum])
467         {
468                 Z_Free (keybindings[keynum]);
469                 keybindings[keynum] = NULL;
470         }
471                         
472 // allocate memory for new binding
473         l = strlen (binding);   
474         new = Z_Malloc (l+1);
475         strcpy (new, binding);
476         new[l] = 0;
477         keybindings[keynum] = new;      
478 }
479
480 /*
481 ===================
482 Key_Unbind_f
483 ===================
484 */
485 void Key_Unbind_f (void)
486 {
487         int             b;
488
489         if (Cmd_Argc() != 2)
490         {
491                 Con_Printf ("unbind <key> : remove commands from a key\n");
492                 return;
493         }
494
495         b = Key_StringToKeynum (Cmd_Argv(1));
496         if (b==-1)
497         {
498                 Con_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
499                 return;
500         }
501
502         Key_SetBinding (b, "");
503 }
504
505 void Key_Unbindall_f (void)
506 {
507         int             i;
508
509         for (i=0 ; i<256 ; i++)
510                 if (keybindings[i])
511                         Key_SetBinding (i, "");
512 }
513
514
515 /*
516 ===================
517 Key_Bind_f
518 ===================
519 */
520 void Key_Bind_f (void)
521 {
522         int                     i, c, b;
523         char            cmd[1024];
524
525         c = Cmd_Argc();
526
527         if (c != 2 && c != 3)
528         {
529                 Con_Printf ("bind <key> [command] : attach a command to a key\n");
530                 return;
531         }
532         b = Key_StringToKeynum (Cmd_Argv(1));
533         if (b==-1)
534         {
535                 Con_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
536                 return;
537         }
538
539         if (c == 2)
540         {
541                 if (keybindings[b])
542                         Con_Printf ("\"%s\" = \"%s\"\n", Cmd_Argv(1), keybindings[b] );
543                 else
544                         Con_Printf ("\"%s\" is not bound\n", Cmd_Argv(1) );
545                 return;
546         }
547
548 // copy the rest of the command line
549         cmd[0] = 0;             // start out with a null string
550         for (i=2 ; i< c ; i++)
551         {
552                 if (i > 2)
553                         strcat (cmd, " ");
554                 strcat (cmd, Cmd_Argv(i));
555         }
556
557         Key_SetBinding (b, cmd);
558 }
559
560 /*
561 ============
562 Key_WriteBindings
563
564 Writes lines containing "bind key value"
565 ============
566 */
567 void Key_WriteBindings (QFile *f)
568 {
569         int             i;
570
571         for (i=0 ; i<256 ; i++)
572                 if (keybindings[i])
573                         if (*keybindings[i])
574                                 Qprintf (f, "bind \"%s\" \"%s\"\n", Key_KeynumToString(i), keybindings[i]);
575 }
576
577
578 /*
579 ===================
580 Key_Init
581 ===================
582 */
583 void Key_Init (void)
584 {
585         int             i;
586
587         // LordHavoc: clear keybindings array so bounds checking won't freak
588         for (i = 0;i < 256;i++)
589                 keybindings[i] = NULL;
590
591         for (i=0 ; i<32 ; i++)
592         {
593                 key_lines[i][0] = ']';
594                 key_lines[i][1] = 0;
595         }
596         key_linepos = 1;
597
598 //
599 // init ascii characters in console mode
600 //
601         for (i=32 ; i<128 ; i++)
602                 consolekeys[i] = true;
603         consolekeys[K_ENTER] = true;
604         consolekeys[K_TAB] = true;
605         consolekeys[K_LEFTARROW] = true;
606         consolekeys[K_RIGHTARROW] = true;
607         consolekeys[K_UPARROW] = true;
608         consolekeys[K_DOWNARROW] = true;
609         consolekeys[K_BACKSPACE] = true;
610         consolekeys[K_DEL] = true;
611         consolekeys[K_INS] = true;
612         consolekeys[K_PGUP] = true;
613         consolekeys[K_PGDN] = true;
614         consolekeys[K_SHIFT] = true;
615         consolekeys[K_MWHEELUP] = true;
616         consolekeys[K_MWHEELDOWN] = true;
617         consolekeys['`'] = false;
618         consolekeys['~'] = false;
619
620         for (i=0 ; i<256 ; i++)
621                 keyshift[i] = i;
622         for (i='a' ; i<='z' ; i++)
623                 keyshift[i] = i - 'a' + 'A';
624         keyshift['1'] = '!';
625         keyshift['2'] = '@';
626         keyshift['3'] = '#';
627         keyshift['4'] = '$';
628         keyshift['5'] = '%';
629         keyshift['6'] = '^';
630         keyshift['7'] = '&';
631         keyshift['8'] = '*';
632         keyshift['9'] = '(';
633         keyshift['0'] = ')';
634         keyshift['-'] = '_';
635         keyshift['='] = '+';
636         keyshift[','] = '<';
637         keyshift['.'] = '>';
638         keyshift['/'] = '?';
639         keyshift[';'] = ':';
640         keyshift['\''] = '"';
641         keyshift['['] = '{';
642         keyshift[']'] = '}';
643         keyshift['`'] = '~';
644         keyshift['\\'] = '|';
645
646         menubound[K_ESCAPE] = true;
647         for (i=0 ; i<12 ; i++)
648                 menubound[K_F1+i] = true;
649
650 //
651 // register our functions
652 //
653         Cmd_AddCommand ("bind",Key_Bind_f);
654         Cmd_AddCommand ("unbind",Key_Unbind_f);
655         Cmd_AddCommand ("unbindall",Key_Unbindall_f);
656 }
657
658 /*
659 ===================
660 Key_Event
661
662 Called by the system between frames for both key up and key down events
663 Should NOT be called during an interrupt!
664 ===================
665 */
666 void Key_Event (int key, qboolean down)
667 {
668         char    *kb;
669         char    cmd[1024];
670
671         keydown[key] = down;
672
673         if (!down)
674                 key_repeats[key] = 0;
675
676         key_lastpress = key;
677         key_count++;
678         if (key_count <= 0)
679                 return;         // just catching keys for Con_NotifyBox
680
681 // update auto-repeat status
682         if (down)
683         {
684                 key_repeats[key]++;
685                 if (key != K_BACKSPACE && key != K_PAUSE && key_repeats[key] > 1)
686                         return; // ignore most autorepeats
687                         
688                 if (key >= 200 && !keybindings[key])
689                         Con_Printf ("%s is unbound, hit F4 to set.\n", Key_KeynumToString (key) );
690         }
691
692         if (key == K_SHIFT)
693                 shift_down = down;
694
695 //
696 // handle escape specialy, so the user can never unbind it
697 //
698         if (key == K_ESCAPE)
699         {
700                 if (!down)
701                         return;
702                 switch (key_dest)
703                 {
704                 case key_message:
705                         Key_Message (key);
706                         break;
707                 case key_menu:
708                         M_Keydown (key);
709                         break;
710                 case key_game:
711                 case key_console:
712                         M_ToggleMenu_f ();
713                         break;
714                 default:
715                         Sys_Error ("Bad key_dest");
716                 }
717                 return;
718         }
719
720 //
721 // key up events only generate commands if the game key binding is
722 // a button command (leading + sign).  These will occur even in console mode,
723 // to keep the character from continuing an action started before a console
724 // switch.  Button commands include the keynum as a parameter, so multiple
725 // downs can be matched with ups
726 //
727         if (!down)
728         {
729                 kb = keybindings[key];
730                 if (kb && kb[0] == '+')
731                 {
732                         sprintf (cmd, "-%s %i\n", kb+1, key);
733                         Cbuf_AddText (cmd);
734                 }
735                 if (keyshift[key] != key)
736                 {
737                         kb = keybindings[keyshift[key]];
738                         if (kb && kb[0] == '+')
739                         {
740                                 sprintf (cmd, "-%s %i\n", kb+1, key);
741                                 Cbuf_AddText (cmd);
742                         }
743                 }
744                 return;
745         }
746
747 //
748 // during demo playback, most keys bring up the main menu
749 //
750         if (cls.demoplayback && down && consolekeys[key] && key_dest == key_game)
751         {
752                 M_ToggleMenu_f ();
753                 return;
754         }
755
756 //
757 // if not a consolekey, send to the interpreter no matter what mode is
758 //
759         if ( (key_dest == key_menu && menubound[key])
760         || (key_dest == key_console && !consolekeys[key])
761         || (key_dest == key_game && ( !con_forcedup || !consolekeys[key] ) ) )
762         {
763                 kb = keybindings[key];
764                 if (kb)
765                 {
766                         if (kb[0] == '+')
767                         {       // button commands add keynum as a parm
768                                 sprintf (cmd, "%s %i\n", kb, key);
769                                 Cbuf_AddText (cmd);
770                         }
771                         else
772                         {
773                                 Cbuf_AddText (kb);
774                                 Cbuf_AddText ("\n");
775                         }
776                 }
777                 return;
778         }
779
780         if (!down)
781                 return;         // other systems only care about key down events
782
783         if (shift_down)
784         {
785                 key = keyshift[key];
786         }
787
788         switch (key_dest)
789         {
790         case key_message:
791                 Key_Message (key);
792                 break;
793         case key_menu:
794                 M_Keydown (key);
795                 break;
796
797         case key_game:
798         case key_console:
799                 Key_Console (key);
800                 break;
801         default:
802                 Sys_Error ("Bad key_dest");
803         }
804 }
805
806
807 /*
808 ===================
809 Key_ClearStates
810 ===================
811 */
812 void Key_ClearStates (void)
813 {
814         int             i;
815
816         for (i=0 ; i<256 ; i++)
817         {
818                 keydown[i] = false;
819                 key_repeats[i] = 0;
820         }
821 }
822