keys.c 14 KB

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