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