git-svn-id: http://bladebattles.com/kurok/SVN@11 20cd92bb-ff49-0410-b73e-96a06e42c3b9
[kurok.git] / keys.c
blob298cf17af076637655bda1778e1d9825f4569f62
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
20 #include "quakedef.h"
24 key up events are sent even if in console mode
29 #define MAXCMDLINE 256
30 char key_lines[32][MAXCMDLINE];
31 int key_linepos;
32 int shift_down=false;
33 int key_lastpress;
35 int edit_line=0;
36 int history_line=0;
38 keydest_t key_dest;
40 int key_count; // incremented every key event
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];
49 void Con_OSK_f (char *input, char *output, int outlen);
50 void Con_SetOSKActive(qboolean active);
51 qboolean Con_isSetOSKActive(void);
52 void Con_OSK_Key (int key);
54 typedef struct
56 char *name;
57 int keynum;
58 } keyname_t;
60 keyname_t keynames[] =
62 {"TAB", K_TAB},
63 {"ENTER", K_ENTER},
64 {"ESCAPE", K_ESCAPE},
65 {"SPACE", K_SPACE},
66 {"BACKSPACE", K_BACKSPACE},
67 {"UPARROW", K_UPARROW},
68 {"DOWNARROW", K_DOWNARROW},
69 {"LEFTARROW", K_LEFTARROW},
70 {"RIGHTARROW", K_RIGHTARROW},
72 {"ALT", K_ALT},
73 {"CTRL", K_CTRL},
74 {"SHIFT", K_SHIFT},
76 {"F1", K_F1},
77 {"F2", K_F2},
78 {"F3", K_F3},
79 {"F4", K_F4},
80 {"F5", K_F5},
81 {"F6", K_F6},
82 {"F7", K_F7},
83 {"F8", K_F8},
84 {"F9", K_F9},
85 {"F10", K_F10},
86 {"F11", K_F11},
87 {"F12", K_F12},
89 {"INS", K_INS},
90 {"DEL", K_DEL},
91 {"PGDN", K_PGDN},
92 {"PGUP", K_PGUP},
93 {"HOME", K_HOME},
94 {"END", K_END},
96 {"MOUSE1", K_MOUSE1},
97 {"MOUSE2", K_MOUSE2},
98 {"MOUSE3", K_MOUSE3},
100 #ifdef PSP
101 {"TRIANGLE", K_JOY1},
102 {"CIRCLE", K_JOY2},
103 {"CROSS", K_JOY3},
104 {"SQUARE", K_JOY4},
105 #else
106 {"JOY1", K_JOY1},
107 {"JOY2", K_JOY2},
108 {"JOY3", K_JOY3},
109 {"JOY4", K_JOY4},
110 #endif
112 #ifdef PSP
113 {"LTRIGGER", K_AUX1},
114 {"RTRIGGER", K_AUX2},
115 #else
116 {"AUX1", K_AUX1},
117 {"AUX2", K_AUX2},
118 #endif
119 {"AUX3", K_AUX3},
120 {"AUX4", K_AUX4},
121 {"AUX5", K_AUX5},
122 {"AUX6", K_AUX6},
123 {"AUX7", K_AUX7},
124 {"AUX8", K_AUX8},
125 {"AUX9", K_AUX9},
126 {"AUX10", K_AUX10},
127 {"AUX11", K_AUX11},
128 {"AUX12", K_AUX12},
129 {"AUX13", K_AUX13},
130 {"AUX14", K_AUX14},
131 {"AUX15", K_AUX15},
132 {"AUX16", K_AUX16},
133 {"AUX17", K_AUX17},
134 {"AUX18", K_AUX18},
135 {"AUX19", K_AUX19},
136 {"AUX20", K_AUX20},
137 {"AUX21", K_AUX21},
138 {"AUX22", K_AUX22},
139 {"AUX23", K_AUX23},
140 {"AUX24", K_AUX24},
141 {"AUX25", K_AUX25},
142 {"AUX26", K_AUX26},
143 {"AUX27", K_AUX27},
144 {"AUX28", K_AUX28},
145 {"AUX29", K_AUX29},
146 {"AUX30", K_AUX30},
147 {"AUX31", K_AUX31},
148 {"AUX32", K_AUX32},
150 {"PAUSE", K_PAUSE},
152 {"MWHEELUP", K_MWHEELUP},
153 {"MWHEELDOWN", K_MWHEELDOWN},
155 {"SEMICOLON", ';'}, // because a raw semicolon seperates commands
157 {NULL,0}
160 char consoleInput[MAXCMDLINE];
161 qboolean consoleOskDone = false;
164 ==============================================================================
166 LINE TYPING INTO THE CONSOLE
168 ==============================================================================
173 ====================
174 Key_Console
176 Interactive line editing and console scrollback
177 ====================
179 void Key_Console (int key)
181 char *cmd;
183 #ifdef PSP
184 if (Con_isSetOSKActive()) {
185 if (key == K_JOY1)
186 Con_OSK_Key (K_DEL);
187 else if (key == K_JOY2)
188 Con_OSK_Key (K_ESCAPE);
189 else if (key == K_JOY3)
190 Con_OSK_Key (K_ENTER);
191 else if (key == K_JOY4)
192 Con_OSK_Key (K_INS);
193 else
194 Con_OSK_Key (key);
195 return;
198 if ( key == K_JOY4 || key == K_INS)
200 consoleOskDone = false;
201 Con_SetOSKActive(true);
202 Con_OSK_f(key_lines[edit_line]+1, consoleInput, 72);
203 return;
205 #endif
207 if (key == K_JOY3 || key == K_ENTER)
209 Cbuf_AddText (key_lines[edit_line]+1); // skip the >
210 Cbuf_AddText ("\n");
211 Con_Printf ("%s\n",key_lines[edit_line]);
212 edit_line = (edit_line + 1) & 31;
213 history_line = edit_line;
214 key_lines[edit_line][0] = ']';
215 key_linepos = 1;
216 if (cls.state == ca_disconnected)
217 SCR_UpdateScreen (); // force an update, because the command
218 // may take some time
219 return;
224 if (key == K_TAB || key == K_RIGHTARROW)
225 { // command completion
226 cmd = Cmd_CompleteCommand (key_lines[edit_line]+1);
227 if (!cmd)
228 cmd = Cvar_CompleteVariable (key_lines[edit_line]+1);
229 if (cmd)
231 Q_strcpy (key_lines[edit_line]+1, cmd);
232 key_linepos = Q_strlen(cmd)+1;
233 key_lines[edit_line][key_linepos] = ' ';
234 key_linepos++;
235 key_lines[edit_line][key_linepos] = 0;
236 return;
240 if (key == K_BACKSPACE || key == K_LEFTARROW)
242 if (key_linepos > 1)
243 key_linepos--;
244 return;
247 if (key == K_UPARROW)
251 history_line = (history_line - 1) & 31;
252 } while (history_line != edit_line
253 && !key_lines[history_line][1]);
254 if (history_line == edit_line)
255 history_line = (edit_line+1)&31;
256 Q_strcpy(key_lines[edit_line], key_lines[history_line]);
257 key_linepos = Q_strlen(key_lines[edit_line]);
258 return;
261 if (key == K_DOWNARROW)
263 if (history_line == edit_line) return;
266 history_line = (history_line + 1) & 31;
268 while (history_line != edit_line
269 && !key_lines[history_line][1]);
270 if (history_line == edit_line)
272 key_lines[edit_line][0] = ']';
273 key_linepos = 1;
275 else
277 Q_strcpy(key_lines[edit_line], key_lines[history_line]);
278 key_linepos = Q_strlen(key_lines[edit_line]);
280 return;
283 if (key == K_PGUP || key==K_MWHEELUP)
285 con_backscroll += 2;
286 if (con_backscroll > con_totallines - (vid.height>>3) - 1)
287 con_backscroll = con_totallines - (vid.height>>3) - 1;
288 return;
291 if (key == K_PGDN || key==K_MWHEELDOWN)
293 con_backscroll -= 2;
294 if (con_backscroll < 0)
295 con_backscroll = 0;
296 return;
299 if (key == K_HOME)
301 con_backscroll = con_totallines - (vid.height>>3) - 1;
302 return;
305 if (key == K_END)
307 con_backscroll = 0;
308 return;
311 if (key < 32 || key > 127)
312 return; // non printable
314 if (key_linepos < MAXCMDLINE-1)
316 key_lines[edit_line][key_linepos] = key;
317 key_linepos++;
318 key_lines[edit_line][key_linepos] = 0;
323 //============================================================================
325 char chat_buffer[32];
326 qboolean team_message = false;
328 void Key_Message (int key)
330 static int chat_bufferlen = 0;
333 #ifdef PSP
334 if (Con_isSetOSKActive()) {
335 if (key == K_JOY1)
336 Con_OSK_Key (K_DEL);
337 else if (key == K_JOY2)
338 Con_OSK_Key (K_ESCAPE);
339 else if (key == K_JOY3)
340 Con_OSK_Key (K_ENTER);
341 else if (key == K_JOY4)
342 Con_OSK_Key (K_INS);
343 else
344 Con_OSK_Key (key);
345 return;
348 if ( key == K_JOY4 || key == K_INS)
350 consoleOskDone = false;
351 Con_SetOSKActive(true);
352 Con_OSK_f(key_lines[edit_line]+1, consoleInput, 72);
353 return;
355 #endif
358 if (key == K_ENTER)
360 if (team_message)
361 Cbuf_AddText ("say_team \"");
362 else
363 Cbuf_AddText ("say \"");
364 Cbuf_AddText(chat_buffer);
365 Cbuf_AddText("\"\n");
367 key_dest = key_game;
368 chat_bufferlen = 0;
369 chat_buffer[0] = 0;
370 return;
373 if (key == K_ESCAPE)
375 key_dest = key_game;
376 chat_bufferlen = 0;
377 chat_buffer[0] = 0;
378 return;
381 if (key < 32 || key > 127)
382 return; // non printable
384 if (key == K_BACKSPACE)
386 if (chat_bufferlen)
388 chat_bufferlen--;
389 chat_buffer[chat_bufferlen] = 0;
391 return;
394 if (chat_bufferlen == 31)
395 return; // all full
397 chat_buffer[chat_bufferlen++] = key;
398 chat_buffer[chat_bufferlen] = 0;
401 //============================================================================
405 ===================
406 Key_StringToKeynum
408 Returns a key number to be used to index keybindings[] by looking at
409 the given string. Single ascii characters return themselves, while
410 the K_* names are matched up.
411 ===================
413 int Key_StringToKeynum (char *str)
415 keyname_t *kn;
417 if (!str || !str[0])
418 return -1;
419 if (!str[1])
420 return str[0];
422 for (kn=keynames ; kn->name ; kn++)
424 if (!Q_strcasecmp(str,kn->name))
425 return kn->keynum;
427 return -1;
431 ===================
432 Key_KeynumToString
434 Returns a string (either a single ascii char, or a K_* name) for the
435 given keynum.
436 FIXME: handle quote special (general escape sequence?)
437 ===================
439 char *Key_KeynumToString (int keynum)
441 keyname_t *kn;
442 static char tinystr[2];
444 if (keynum == -1)
445 return "<KEY NOT FOUND>";
446 if (keynum > 32 && keynum < 127)
447 { // printable ascii
448 tinystr[0] = keynum;
449 tinystr[1] = 0;
450 return tinystr;
453 for (kn=keynames ; kn->name ; kn++)
454 if (keynum == kn->keynum)
455 return kn->name;
457 return "<UNKNOWN KEYNUM>";
462 ===================
463 Key_SetBinding
464 ===================
466 void Key_SetBinding (int keynum, char *binding)
468 char *new;
469 int l;
471 if (keynum == -1)
472 return;
474 // free old bindings
475 if (keybindings[keynum])
477 Z_Free (keybindings[keynum]);
478 keybindings[keynum] = NULL;
481 // allocate memory for new binding
482 l = Q_strlen (binding);
483 new = Z_Malloc (l+1);
484 Q_strcpy (new, binding);
485 new[l] = 0;
486 keybindings[keynum] = new;
490 ===================
491 Key_Unbind_f
492 ===================
494 void Key_Unbind_f (void)
496 int b;
498 if (Cmd_Argc() != 2)
500 Con_Printf ("unbind <key> : remove commands from a key\n");
501 return;
504 b = Key_StringToKeynum (Cmd_Argv(1));
505 if (b==-1)
507 Con_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
508 return;
511 Key_SetBinding (b, "");
514 void Key_Unbindall_f (void)
516 int i;
518 for (i=0 ; i<256 ; i++)
519 if (keybindings[i])
520 Key_SetBinding (i, "");
525 ===================
526 Key_Bind_f
527 ===================
529 void Key_Bind_f (void)
531 int i, c, b;
532 char cmd[1024];
534 c = Cmd_Argc();
536 if (c != 2 && c != 3)
538 Con_Printf ("bind <key> [command] : attach a command to a key\n");
539 return;
541 b = Key_StringToKeynum (Cmd_Argv(1));
542 if (b==-1)
544 Con_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
545 return;
548 if (c == 2)
550 if (keybindings[b])
551 Con_Printf ("\"%s\" = \"%s\"\n", Cmd_Argv(1), keybindings[b] );
552 else
553 Con_Printf ("\"%s\" is not bound\n", Cmd_Argv(1) );
554 return;
557 // copy the rest of the command line
558 cmd[0] = 0; // start out with a null string
559 for (i=2 ; i< c ; i++)
561 if (i > 2)
562 strcat (cmd, " ");
563 strcat (cmd, Cmd_Argv(i));
566 Key_SetBinding (b, cmd);
570 ============
571 Key_WriteBindings
573 Writes lines containing "bind key value"
574 ============
576 void Key_WriteBindings (FILE *f)
578 int i;
580 for (i=0 ; i<256 ; i++)
581 if (keybindings[i])
582 if (*keybindings[i])
583 fprintf (f, "bind \"%s\" \"%s\"\n", Key_KeynumToString(i), keybindings[i]);
588 ===================
589 Key_Init
590 ===================
592 void Key_Init (void)
594 int i;
596 for (i=0 ; i<32 ; i++)
598 key_lines[i][0] = ']';
599 key_lines[i][1] = 0;
601 key_linepos = 1;
604 // init ascii characters in console mode
606 for (i=32 ; i<128 ; i++)
607 consolekeys[i] = true;
608 consolekeys[K_ENTER] = true;
609 consolekeys[K_TAB] = true;
610 consolekeys[K_LEFTARROW] = true;
611 consolekeys[K_RIGHTARROW] = true;
612 consolekeys[K_UPARROW] = true;
613 consolekeys[K_DOWNARROW] = true;
614 consolekeys[K_BACKSPACE] = true;
615 consolekeys[K_PGUP] = true;
616 consolekeys[K_PGDN] = true;
617 consolekeys[K_SHIFT] = true;
618 consolekeys[K_MWHEELUP] = true;
619 consolekeys[K_MWHEELDOWN] = true;
620 consolekeys['`'] = false;
621 consolekeys['~'] = false;
622 #ifdef PSP
623 consolekeys[K_INS] = true;
624 consolekeys[K_JOY1] = true;
625 consolekeys[K_JOY2] = true;
626 consolekeys[K_JOY3] = true;
627 consolekeys[K_JOY4] = true;
628 #endif
630 for (i=0 ; i<256 ; i++)
631 keyshift[i] = i;
632 for (i='a' ; i<='z' ; i++)
633 keyshift[i] = i - 'a' + 'A';
634 keyshift['1'] = '!';
635 keyshift['2'] = '@';
636 keyshift['3'] = '#';
637 keyshift['4'] = '$';
638 keyshift['5'] = '%';
639 keyshift['6'] = '^';
640 keyshift['7'] = '&';
641 keyshift['8'] = '*';
642 keyshift['9'] = '(';
643 keyshift['0'] = ')';
644 keyshift['-'] = '_';
645 keyshift['='] = '+';
646 keyshift[','] = '<';
647 keyshift['.'] = '>';
648 keyshift['/'] = '?';
649 keyshift[';'] = ':';
650 keyshift['\''] = '"';
651 keyshift['['] = '{';
652 keyshift[']'] = '}';
653 keyshift['`'] = '~';
654 keyshift['\\'] = '|';
656 menubound[K_ESCAPE] = true;
657 for (i=0 ; i<12 ; i++)
658 menubound[K_F1+i] = true;
661 // register our functions
663 Cmd_AddCommand ("bind",Key_Bind_f);
664 Cmd_AddCommand ("unbind",Key_Unbind_f);
665 Cmd_AddCommand ("unbindall",Key_Unbindall_f);
671 ===================
672 Key_Event
674 Called by the system between frames for both key up and key down events
675 Should NOT be called during an interrupt!
676 ===================
678 void Key_Event (int key, qboolean down)
680 char *kb;
681 char cmd[1024];
683 keydown[key] = down;
685 #ifdef PSP
686 if (Con_isSetOSKActive() && down) {
687 if (key == K_JOY1)
688 Con_OSK_Key (K_DEL);
689 else if (key == K_JOY2)
690 Con_OSK_Key (K_ESCAPE);
691 else if (key == K_JOY3)
692 Con_OSK_Key (K_ENTER);
693 else if (key == K_JOY4)
694 Con_OSK_Key (K_INS);
695 else
696 Con_OSK_Key (key);
698 if (!Con_isSetOSKActive()) {
699 consoleOskDone = true;
700 strcpy(key_lines[edit_line]+1, consoleInput);
701 key_linepos = Q_strlen(key_lines[edit_line]);
702 consoleOskDone = false;
703 consoleInput[0] = 0;
704 return;
707 else {
708 return;
711 #endif
712 if (!down)
713 key_repeats[key] = 0;
715 key_lastpress = key;
716 key_count++;
717 if (key_count <= 0)
719 return; // just catching keys for Con_NotifyBox
722 // update auto-repeat status
723 if (down)
725 key_repeats[key]++;
726 if (key != K_PAUSE && key_repeats[key] > 1)
728 return; // ignore most autorepeats
731 if (key >= 200 && !keybindings[key])
732 #ifdef PSP
733 Con_Printf ("%s is unbound, hit START to set.\n", Key_KeynumToString (key) );
734 #else
735 Con_Printf ("%s is unbound, hit F4 to set.\n", Key_KeynumToString (key) );
736 #endif
739 if (key == K_SHIFT)
740 shift_down = down;
743 // handle escape specialy, so the user can never unbind it
745 if (key == K_ESCAPE)
747 if (!down)
748 return;
749 switch (key_dest)
751 case key_message:
752 Key_Message (key);
753 break;
754 case key_menu:
755 M_Keydown (key);
756 break;
757 case key_game:
758 case key_console:
759 M_ToggleMenu_f ();
760 break;
761 default:
762 Sys_Error ("Bad key_dest");
764 return;
768 // key up events only generate commands if the game key binding is
769 // a button command (leading + sign). These will occur even in console mode,
770 // to keep the character from continuing an action started before a console
771 // switch. Button commands include the kenum as a parameter, so multiple
772 // downs can be matched with ups
774 if (!down)
776 kb = keybindings[key];
777 if (kb && kb[0] == '+')
779 sprintf (cmd, "-%s %i\n", kb+1, key);
780 Cbuf_AddText (cmd);
782 if (keyshift[key] != key)
784 kb = keybindings[keyshift[key]];
785 if (kb && kb[0] == '+')
787 sprintf (cmd, "-%s %i\n", kb+1, key);
788 Cbuf_AddText (cmd);
791 return;
795 // during demo playback, most keys bring up the main menu
797 if (cls.demoplayback && down && consolekeys[key] && key_dest == key_game)
799 M_ToggleMenu_f ();
800 return;
804 // if not a consolekey, send to the interpreter no matter what mode is
806 if ( (key_dest == key_menu && menubound[key])
807 || (key_dest == key_console && !consolekeys[key])
808 || (key_dest == key_game && ( !con_forcedup || !consolekeys[key] ) ) )
810 kb = keybindings[key];
811 if (kb)
813 if (kb[0] == '+')
814 { // button commands add keynum as a parm
815 sprintf (cmd, "%s %i\n", kb, key);
816 Cbuf_AddText (cmd);
818 else
820 Cbuf_AddText (kb);
821 Cbuf_AddText ("\n");
824 return;
827 if (!down)
828 return; // other systems only care about key down events
830 if (shift_down)
832 key = keyshift[key];
835 switch (key_dest)
837 case key_message:
838 Key_Message (key);
839 break;
840 case key_menu:
841 M_Keydown (key);
842 break;
844 case key_game:
845 case key_console:
846 Key_Console (key);
847 break;
848 default:
849 Sys_Error ("Bad key_dest");
855 ===================
856 Key_ClearStates
857 ===================
859 void Key_ClearStates (void)
861 int i;
863 for (i=0 ; i<256 ; i++)
865 keydown[i] = false;
866 key_repeats[i] = 0;