Debian Codegraph @

code/client/cl_keys.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22 #include "client.h"
23
24 /*
25
26 key up events are sent even if in console mode
27
28 */
29
30 field_t historyEditLines[COMMAND_HISTORY];
31
32 int nextHistoryLine; // the last line in the history buffer, not masked
33 int historyLine; // the line being displayed from history buffer
34 // will be <= nextHistoryLine
35
36 field_t g_consoleField;
37 field_t chatField;
38 qboolean chat_team;
39
40 int chat_playerNum;
41
42
43 qboolean key_overstrikeMode;
44
45 int anykeydown;
46 qkey_t keys[MAX_KEYS];
47
48
49 typedef struct {
50 char *name;
51 int keynum;
52 } keyname_t;
53
54
55 // names not in this list can either be lowercase ascii, or '0xnn' hex sequences
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 {"COMMAND", K_COMMAND},
73
74 {"CAPSLOCK", K_CAPSLOCK},
75
76
77 {"F1", K_F1},
78 {"F2", K_F2},
79 {"F3", K_F3},
80 {"F4", K_F4},
81 {"F5", K_F5},
82 {"F6", K_F6},
83 {"F7", K_F7},
84 {"F8", K_F8},
85 {"F9", K_F9},
86 {"F10", K_F10},
87 {"F11", K_F11},
88 {"F12", K_F12},
89 {"F13", K_F13},
90 {"F14", K_F14},
91 {"F15", K_F15},
92
93 {"INS", K_INS},
94 {"DEL", K_DEL},
95 {"PGDN", K_PGDN},
96 {"PGUP", K_PGUP},
97 {"HOME", K_HOME},
98 {"END", K_END},
99
100 {"MOUSE1", K_MOUSE1},
101 {"MOUSE2", K_MOUSE2},
102 {"MOUSE3", K_MOUSE3},
103 {"MOUSE4", K_MOUSE4},
104 {"MOUSE5", K_MOUSE5},
105
106 {"MWHEELUP", K_MWHEELUP },
107 {"MWHEELDOWN", K_MWHEELDOWN },
108
109 {"JOY1", K_JOY1},
110 {"JOY2", K_JOY2},
111 {"JOY3", K_JOY3},
112 {"JOY4", K_JOY4},
113 {"JOY5", K_JOY5},
114 {"JOY6", K_JOY6},
115 {"JOY7", K_JOY7},
116 {"JOY8", K_JOY8},
117 {"JOY9", K_JOY9},
118 {"JOY10", K_JOY10},
119 {"JOY11", K_JOY11},
120 {"JOY12", K_JOY12},
121 {"JOY13", K_JOY13},
122 {"JOY14", K_JOY14},
123 {"JOY15", K_JOY15},
124 {"JOY16", K_JOY16},
125 {"JOY17", K_JOY17},
126 {"JOY18", K_JOY18},
127 {"JOY19", K_JOY19},
128 {"JOY20", K_JOY20},
129 {"JOY21", K_JOY21},
130 {"JOY22", K_JOY22},
131 {"JOY23", K_JOY23},
132 {"JOY24", K_JOY24},
133 {"JOY25", K_JOY25},
134 {"JOY26", K_JOY26},
135 {"JOY27", K_JOY27},
136 {"JOY28", K_JOY28},
137 {"JOY29", K_JOY29},
138 {"JOY30", K_JOY30},
139 {"JOY31", K_JOY31},
140 {"JOY32", K_JOY32},
141
142 {"AUX1", K_AUX1},
143 {"AUX2", K_AUX2},
144 {"AUX3", K_AUX3},
145 {"AUX4", K_AUX4},
146 {"AUX5", K_AUX5},
147 {"AUX6", K_AUX6},
148 {"AUX7", K_AUX7},
149 {"AUX8", K_AUX8},
150 {"AUX9", K_AUX9},
151 {"AUX10", K_AUX10},
152 {"AUX11", K_AUX11},
153 {"AUX12", K_AUX12},
154 {"AUX13", K_AUX13},
155 {"AUX14", K_AUX14},
156 {"AUX15", K_AUX15},
157 {"AUX16", K_AUX16},
158
159 {"KP_HOME", K_KP_HOME },
160 {"KP_UPARROW", K_KP_UPARROW },
161 {"KP_PGUP", K_KP_PGUP },
162 {"KP_LEFTARROW", K_KP_LEFTARROW },
163 {"KP_5", K_KP_5 },
164 {"KP_RIGHTARROW", K_KP_RIGHTARROW },
165 {"KP_END", K_KP_END },
166 {"KP_DOWNARROW", K_KP_DOWNARROW },
167 {"KP_PGDN", K_KP_PGDN },
168 {"KP_ENTER", K_KP_ENTER },
169 {"KP_INS", K_KP_INS },
170 {"KP_DEL", K_KP_DEL },
171 {"KP_SLASH", K_KP_SLASH },
172 {"KP_MINUS", K_KP_MINUS },
173 {"KP_PLUS", K_KP_PLUS },
174 {"KP_NUMLOCK", K_KP_NUMLOCK },
175 {"KP_STAR", K_KP_STAR },
176 {"KP_EQUALS", K_KP_EQUALS },
177
178 {"PAUSE", K_PAUSE},
179
180 {"SEMICOLON", ';'}, // because a raw semicolon separates commands
181
182 {"WORLD_0", K_WORLD_0},
183 {"WORLD_1", K_WORLD_1},
184 {"WORLD_2", K_WORLD_2},
185 {"WORLD_3", K_WORLD_3},
186 {"WORLD_4", K_WORLD_4},
187 {"WORLD_5", K_WORLD_5},
188 {"WORLD_6", K_WORLD_6},
189 {"WORLD_7", K_WORLD_7},
190 {"WORLD_8", K_WORLD_8},
191 {"WORLD_9", K_WORLD_9},
192 {"WORLD_10", K_WORLD_10},
193 {"WORLD_11", K_WORLD_11},
194 {"WORLD_12", K_WORLD_12},
195 {"WORLD_13", K_WORLD_13},
196 {"WORLD_14", K_WORLD_14},
197 {"WORLD_15", K_WORLD_15},
198 {"WORLD_16", K_WORLD_16},
199 {"WORLD_17", K_WORLD_17},
200 {"WORLD_18", K_WORLD_18},
201 {"WORLD_19", K_WORLD_19},
202 {"WORLD_20", K_WORLD_20},
203 {"WORLD_21", K_WORLD_21},
204 {"WORLD_22", K_WORLD_22},
205 {"WORLD_23", K_WORLD_23},
206 {"WORLD_24", K_WORLD_24},
207 {"WORLD_25", K_WORLD_25},
208 {"WORLD_26", K_WORLD_26},
209 {"WORLD_27", K_WORLD_27},
210 {"WORLD_28", K_WORLD_28},
211 {"WORLD_29", K_WORLD_29},
212 {"WORLD_30", K_WORLD_30},
213 {"WORLD_31", K_WORLD_31},
214 {"WORLD_32", K_WORLD_32},
215 {"WORLD_33", K_WORLD_33},
216 {"WORLD_34", K_WORLD_34},
217 {"WORLD_35", K_WORLD_35},
218 {"WORLD_36", K_WORLD_36},
219 {"WORLD_37", K_WORLD_37},
220 {"WORLD_38", K_WORLD_38},
221 {"WORLD_39", K_WORLD_39},
222 {"WORLD_40", K_WORLD_40},
223 {"WORLD_41", K_WORLD_41},
224 {"WORLD_42", K_WORLD_42},
225 {"WORLD_43", K_WORLD_43},
226 {"WORLD_44", K_WORLD_44},
227 {"WORLD_45", K_WORLD_45},
228 {"WORLD_46", K_WORLD_46},
229 {"WORLD_47", K_WORLD_47},
230 {"WORLD_48", K_WORLD_48},
231 {"WORLD_49", K_WORLD_49},
232 {"WORLD_50", K_WORLD_50},
233 {"WORLD_51", K_WORLD_51},
234 {"WORLD_52", K_WORLD_52},
235 {"WORLD_53", K_WORLD_53},
236 {"WORLD_54", K_WORLD_54},
237 {"WORLD_55", K_WORLD_55},
238 {"WORLD_56", K_WORLD_56},
239 {"WORLD_57", K_WORLD_57},
240 {"WORLD_58", K_WORLD_58},
241 {"WORLD_59", K_WORLD_59},
242 {"WORLD_60", K_WORLD_60},
243 {"WORLD_61", K_WORLD_61},
244 {"WORLD_62", K_WORLD_62},
245 {"WORLD_63", K_WORLD_63},
246 {"WORLD_64", K_WORLD_64},
247 {"WORLD_65", K_WORLD_65},
248 {"WORLD_66", K_WORLD_66},
249 {"WORLD_67", K_WORLD_67},
250 {"WORLD_68", K_WORLD_68},
251 {"WORLD_69", K_WORLD_69},
252 {"WORLD_70", K_WORLD_70},
253 {"WORLD_71", K_WORLD_71},
254 {"WORLD_72", K_WORLD_72},
255 {"WORLD_73", K_WORLD_73},
256 {"WORLD_74", K_WORLD_74},
257 {"WORLD_75", K_WORLD_75},
258 {"WORLD_76", K_WORLD_76},
259 {"WORLD_77", K_WORLD_77},
260 {"WORLD_78", K_WORLD_78},
261 {"WORLD_79", K_WORLD_79},
262 {"WORLD_80", K_WORLD_80},
263 {"WORLD_81", K_WORLD_81},
264 {"WORLD_82", K_WORLD_82},
265 {"WORLD_83", K_WORLD_83},
266 {"WORLD_84", K_WORLD_84},
267 {"WORLD_85", K_WORLD_85},
268 {"WORLD_86", K_WORLD_86},
269 {"WORLD_87", K_WORLD_87},
270 {"WORLD_88", K_WORLD_88},
271 {"WORLD_89", K_WORLD_89},
272 {"WORLD_90", K_WORLD_90},
273 {"WORLD_91", K_WORLD_91},
274 {"WORLD_92", K_WORLD_92},
275 {"WORLD_93", K_WORLD_93},
276 {"WORLD_94", K_WORLD_94},
277 {"WORLD_95", K_WORLD_95},
278
279 {"WINDOWS", K_SUPER},
280 {"COMPOSE", K_COMPOSE},
281 {"MODE", K_MODE},
282 {"HELP", K_HELP},
283 {"PRINT", K_PRINT},
284 {"SYSREQ", K_SYSREQ},
285 {"SCROLLOCK", K_SCROLLOCK },
286 {"BREAK", K_BREAK},
287 {"MENU", K_MENU},
288 {"POWER", K_POWER},
289 {"EURO", K_EURO},
290 {"UNDO", K_UNDO},
291
292 {"PAD0_A", K_PAD0_A },
293 {"PAD0_B", K_PAD0_B },
294 {"PAD0_X", K_PAD0_X },
295 {"PAD0_Y", K_PAD0_Y },
296 {"PAD0_BACK", K_PAD0_BACK },
297 {"PAD0_GUIDE", K_PAD0_GUIDE },
298 {"PAD0_START", K_PAD0_START },
299 {"PAD0_LEFTSTICK_CLICK", K_PAD0_LEFTSTICK_CLICK },
300 {"PAD0_RIGHTSTICK_CLICK", K_PAD0_RIGHTSTICK_CLICK },
301 {"PAD0_LEFTSHOULDER", K_PAD0_LEFTSHOULDER },
302 {"PAD0_RIGHTSHOULDER", K_PAD0_RIGHTSHOULDER },
303 {"PAD0_DPAD_UP", K_PAD0_DPAD_UP },
304 {"PAD0_DPAD_DOWN", K_PAD0_DPAD_DOWN },
305 {"PAD0_DPAD_LEFT", K_PAD0_DPAD_LEFT },
306 {"PAD0_DPAD_RIGHT", K_PAD0_DPAD_RIGHT },
307
308 {"PAD0_LEFTSTICK_LEFT", K_PAD0_LEFTSTICK_LEFT },
309 {"PAD0_LEFTSTICK_RIGHT", K_PAD0_LEFTSTICK_RIGHT },
310 {"PAD0_LEFTSTICK_UP", K_PAD0_LEFTSTICK_UP },
311 {"PAD0_LEFTSTICK_DOWN", K_PAD0_LEFTSTICK_DOWN },
312 {"PAD0_RIGHTSTICK_LEFT", K_PAD0_RIGHTSTICK_LEFT },
313 {"PAD0_RIGHTSTICK_RIGHT", K_PAD0_RIGHTSTICK_RIGHT },
314 {"PAD0_RIGHTSTICK_UP", K_PAD0_RIGHTSTICK_UP },
315 {"PAD0_RIGHTSTICK_DOWN", K_PAD0_RIGHTSTICK_DOWN },
316 {"PAD0_LEFTTRIGGER", K_PAD0_LEFTTRIGGER },
317 {"PAD0_RIGHTTRIGGER", K_PAD0_RIGHTTRIGGER },
318
319 {"PAD0_MISC1", K_PAD0_MISC1 },
320 {"PAD0_PADDLE1", K_PAD0_PADDLE1 },
321 {"PAD0_PADDLE2", K_PAD0_PADDLE2 },
322 {"PAD0_PADDLE3", K_PAD0_PADDLE3 },
323 {"PAD0_PADDLE4", K_PAD0_PADDLE4 },
324 {"PAD0_TOUCHPAD", K_PAD0_TOUCHPAD },
325
326 {NULL,0}
327 };
328
329 /*
330 =============================================================================
331
332 EDIT FIELDS
333
334 =============================================================================
335 */
336
337
338 /*
339 ===================
340 Field_Draw
341
342 Handles horizontal scrolling and cursor blinking
343 x, y, and width are in pixels
344 ===================
345 */
346 static void Field_VariableSizeDraw( field_t *edit, int x, int y, int width, int size,
347 qboolean drawSmall, qboolean showCursor, qboolean noColorEscape ) {
348 int len;
349 int drawLen;
350 int prestep;
351 int cursorChar;
352 char str[MAX_STRING_CHARS];
353 int i;
354
355 drawLen = edit->widthInChars - 1; // - 1 so there is always a space for the cursor
356 len = strlen( edit->buffer );
357
358 // guarantee that cursor will be visible
359 if ( len <= drawLen ) {
360 prestep = 0;
361 } else {
362 if ( edit->scroll + drawLen > len ) {
363 edit->scroll = len - drawLen;
364 if ( edit->scroll < 0 ) {
365 edit->scroll = 0;
366 }
367 }
368 prestep = edit->scroll;
369 }
370
371 if ( prestep + drawLen > len ) {
372 drawLen = len - prestep;
373 }
374
375 // extract <drawLen> characters from the field at <prestep>
376 if ( drawLen >= MAX_STRING_CHARS ) {
377 Com_Error( ERR_DROP, "drawLen >= MAX_STRING_CHARS" );
378 }
379
380 Com_Memcpy( str, edit->buffer + prestep, drawLen );
381 str[ drawLen ] = 0;
382
383 // draw it
384 if ( drawSmall ) {
385 float color[4];
386
387 color[0] = color[1] = color[2] = color[3] = 1.0;
388 SCR_DrawSmallStringExt( x, y, str, color, qfalse, noColorEscape );
389 } else {
390 // draw big string with drop shadow
391 SCR_DrawBigString( x, y, str, 1.0, noColorEscape );
392 }
393
394 // draw the cursor
395 if ( showCursor ) {
396 if ( (int)( cls.realtime >> 8 ) & 1 ) {
397 return; // off blink
398 }
399
400 if ( key_overstrikeMode ) {
401 cursorChar = 11;
402 } else {
403 cursorChar = 10;
404 }
405
406 i = drawLen - strlen( str );
407
408 if ( drawSmall ) {
409 SCR_DrawSmallChar( x + ( edit->cursor - prestep - i ) * size, y, cursorChar );
410 } else {
411 str[0] = cursorChar;
412 str[1] = 0;
413 SCR_DrawBigString( x + ( edit->cursor - prestep - i ) * size, y, str, 1.0, qfalse );
414
415 }
416 }
417 }
418
419 void Field_Draw( field_t *edit, int x, int y, int width, qboolean showCursor, qboolean noColorEscape )
420 {
421 Field_VariableSizeDraw( edit, x, y, width, g_smallchar_width, qtrue, showCursor, noColorEscape );
422 }
423
424 void Field_BigDraw( field_t *edit, int x, int y, int width, qboolean showCursor, qboolean noColorEscape )
425 {
426 Field_VariableSizeDraw( edit, x, y, width, BIGCHAR_WIDTH, qfalse, showCursor, noColorEscape );
427 }
428
429 /*
430 ================
431 Field_Paste
432 ================
433 */
434 void Field_Paste( field_t *edit ) {
435 char *cbd;
436 int pasteLen, i;
437
438 cbd = Sys_GetClipboardData();
439
440 if ( !cbd ) {
441 return;
442 }
443
444 // send as if typed, so insert / overstrike works properly
445 pasteLen = strlen( cbd );
446 for ( i = 0 ; i < pasteLen ; i++ ) {
447 Field_CharEvent( edit, cbd[i] );
448 }
449
450 Z_Free( cbd );
451 }
452
453 /*
454 =================
455 Field_KeyDownEvent
456
457 Performs the basic line editing functions for the console,
458 in-game talk, and menu fields
459
460 Key events are used for non-printable characters, others are gotten from char events.
461 =================
462 */
463 void Field_KeyDownEvent( field_t *edit, int key ) {
464 int len;
465
466 // shift-insert is paste
467 if ( ( ( key == K_INS ) || ( key == K_KP_INS ) ) && keys[K_SHIFT].down ) {
468 Field_Paste( edit );
469 return;
470 }
471
472 key = tolower( key );
473 len = strlen( edit->buffer );
474
475 switch ( key ) {
476 case K_DEL:
477 if ( edit->cursor < len ) {
478 memmove( edit->buffer + edit->cursor,
479 edit->buffer + edit->cursor + 1, len - edit->cursor );
480 }
481 break;
482
483 case K_RIGHTARROW:
484 if ( edit->cursor < len ) {
485 edit->cursor++;
486 }
487 break;
488
489 case K_LEFTARROW:
490 if ( edit->cursor > 0 ) {
491 edit->cursor--;
492 }
493 break;
494
495 case K_HOME:
496 edit->cursor = 0;
497 break;
498
499 case K_END:
500 edit->cursor = len;
501 break;
502
503 case K_INS:
504 key_overstrikeMode = !key_overstrikeMode;
505 break;
506
507 default:
508 break;
509 }
510
511 // Change scroll if cursor is no longer visible
512 if ( edit->cursor < edit->scroll ) {
513 edit->scroll = edit->cursor;
514 } else if ( edit->cursor >= edit->scroll + edit->widthInChars && edit->cursor <= len ) {
515 edit->scroll = edit->cursor - edit->widthInChars + 1;
516 }
517 }
518
519 /*
520 ==================
521 Field_CharEvent
522 ==================
523 */
524 void Field_CharEvent( field_t *edit, int ch ) {
525 int len;
526
527 if ( ch == 'v' - 'a' + 1 ) { // ctrl-v is paste
528 Field_Paste( edit );
529 return;
530 }
531
532 if ( ch == 'c' - 'a' + 1 ) { // ctrl-c clears the field
533 Field_Clear( edit );
534 return;
535 }
536
537 len = strlen( edit->buffer );
538
539 if ( ch == 'h' - 'a' + 1 ) { // ctrl-h is backspace
540 if ( edit->cursor > 0 ) {
541 memmove( edit->buffer + edit->cursor - 1,
542 edit->buffer + edit->cursor, len + 1 - edit->cursor );
543 edit->cursor--;
544 if ( edit->cursor < edit->scroll )
545 {
546 edit->scroll--;
547 }
548 }
549 return;
550 }
551
552 if ( ch == 'a' - 'a' + 1 ) { // ctrl-a is home
553 edit->cursor = 0;
554 edit->scroll = 0;
555 return;
556 }
557
558 if ( ch == 'e' - 'a' + 1 ) { // ctrl-e is end
559 edit->cursor = len;
560 edit->scroll = edit->cursor - edit->widthInChars;
561 return;
562 }
563
564 //
565 // ignore any other non printable chars
566 //
567 if ( ch < 32 ) {
568 return;
569 }
570
571 if ( key_overstrikeMode ) {
572 // - 2 to leave room for the leading slash and trailing \0
573 if ( edit->cursor == MAX_EDIT_LINE - 2 )
574 return;
575 edit->buffer[edit->cursor] = ch;
576 edit->cursor++;
577 } else { // insert mode
578 // - 2 to leave room for the leading slash and trailing \0
579 if ( len == MAX_EDIT_LINE - 2 ) {
580 return; // all full
581 }
582 memmove( edit->buffer + edit->cursor + 1,
583 edit->buffer + edit->cursor, len + 1 - edit->cursor );
584 edit->buffer[edit->cursor] = ch;
585 edit->cursor++;
586 }
587
588
589 if ( edit->cursor >= edit->widthInChars ) {
590 edit->scroll++;
591 }
592
593 if ( edit->cursor == len + 1) {
594 edit->buffer[edit->cursor] = 0;
595 }
596 }
597
598 /*
599 =============================================================================
600
601 CONSOLE LINE EDITING
602
603 ==============================================================================
604 */
605
606 /*
607 ====================
608 Console_Key
609
610 Handles history and console scrollback
611 ====================
612 */
613 void Console_Key (int key) {
614 // ctrl-L clears screen
615 if ( key == 'l' && keys[K_CTRL].down ) {
616 Cbuf_AddText ("clear\n");
617 return;
618 }
619
620 // enter finishes the line
621 if ( key == K_ENTER || key == K_KP_ENTER ) {
622 // if not in the game explicitly prepend a slash if needed
623 if ( clc.state != CA_ACTIVE && con_autochat->integer &&
624 g_consoleField.buffer[0] &&
625 g_consoleField.buffer[0] != '\\' &&
626 g_consoleField.buffer[0] != '/' ) {
627 char temp[MAX_EDIT_LINE-1];
628
629 Q_strncpyz( temp, g_consoleField.buffer, sizeof( temp ) );
630 Com_sprintf( g_consoleField.buffer, sizeof( g_consoleField.buffer ), "\\%s", temp );
631 g_consoleField.cursor++;
632 }
633
634 Com_Printf ( "]%s\n", g_consoleField.buffer );
635
636 // leading slash is an explicit command
637 if ( g_consoleField.buffer[0] == '\\' || g_consoleField.buffer[0] == '/' ) {
638 Cbuf_AddText( g_consoleField.buffer+1 ); // valid command
639 Cbuf_AddText ("\n");
640 } else {
641 // other text will be chat messages
642 if ( !g_consoleField.buffer[0] ) {
643 return; // empty lines just scroll the console without adding to history
644 } else {
645 if ( con_autochat->integer ) {
646 Cbuf_AddText ("cmd say ");
647 }
648
649 Cbuf_AddText( g_consoleField.buffer );
650 Cbuf_AddText ("\n");
651 }
652 }
653
654 // copy line to history buffer
655 historyEditLines[nextHistoryLine % COMMAND_HISTORY] = g_consoleField;
656 nextHistoryLine++;
657 historyLine = nextHistoryLine;
658
659 Field_Clear( &g_consoleField );
660
661 g_consoleField.widthInChars = g_console_field_width;
662
663 CL_SaveConsoleHistory( );
664
665 if ( clc.state == CA_DISCONNECTED ) {
666 SCR_UpdateScreen (); // force an update, because the command
667 } // may take some time
668 return;
669 }
670
671 // command completion
672
673 if (key == K_TAB) {
674 Field_AutoComplete(&g_consoleField);
675 return;
676 }
677
678 // command history (ctrl-p ctrl-n for unix style)
679
680 if ( (key == K_MWHEELUP && keys[K_SHIFT].down) || ( key == K_UPARROW ) || ( key == K_KP_UPARROW ) ||
681 ( ( tolower(key) == 'p' ) && keys[K_CTRL].down ) ) {
682 if ( nextHistoryLine - historyLine < COMMAND_HISTORY
683 && historyLine > 0 ) {
684 historyLine--;
685 }
686 g_consoleField = historyEditLines[ historyLine % COMMAND_HISTORY ];
687 return;
688 }
689
690 if ( (key == K_MWHEELDOWN && keys[K_SHIFT].down) || ( key == K_DOWNARROW ) || ( key == K_KP_DOWNARROW ) ||
691 ( ( tolower(key) == 'n' ) && keys[K_CTRL].down ) ) {
692 historyLine++;
693 if (historyLine >= nextHistoryLine) {
694 historyLine = nextHistoryLine;
695 Field_Clear( &g_consoleField );
696 g_consoleField.widthInChars = g_console_field_width;
697 return;
698 }
699 g_consoleField = historyEditLines[ historyLine % COMMAND_HISTORY ];
700 return;
701 }
702
703 // console scrolling
704 if ( key == K_PGUP ) {
705 Con_PageUp();
706 return;
707 }
708
709 if ( key == K_PGDN) {
710 Con_PageDown();
711 return;
712 }
713
714 if ( key == K_MWHEELUP) { //----(SA) added some mousewheel functionality to the console
715 Con_PageUp();
716 if(keys[K_CTRL].down) { // hold <ctrl> to accelerate scrolling
717 Con_PageUp();
718 Con_PageUp();
719 }
720 return;
721 }
722
723 if ( key == K_MWHEELDOWN) { //----(SA) added some mousewheel functionality to the console
724 Con_PageDown();
725 if(keys[K_CTRL].down) { // hold <ctrl> to accelerate scrolling
726 Con_PageDown();
727 Con_PageDown();
728 }
729 return;
730 }
731
732 // ctrl-home = top of console
733 if ( key == K_HOME && keys[K_CTRL].down ) {
734 Con_Top();
735 return;
736 }
737
738 // ctrl-end = bottom of console
739 if ( key == K_END && keys[K_CTRL].down ) {
740 Con_Bottom();
741 return;
742 }
743
744 // pass to the normal editline routine
745 Field_KeyDownEvent( &g_consoleField, key );
746 }
747
748 //============================================================================
749
750
751 /*
752 ================
753 Message_Key
754
755 In game talk message
756 ================
757 */
758 void Message_Key( int key ) {
759
760 char buffer[MAX_STRING_CHARS];
761
762
763 if (key == K_ESCAPE) {
764 Key_SetCatcher( Key_GetCatcher( ) & ~KEYCATCH_MESSAGE );
765 Field_Clear( &chatField );
766 return;
767 }
768
769 if ( key == K_ENTER || key == K_KP_ENTER )
770 {
771 if ( chatField.buffer[0] && clc.state == CA_ACTIVE ) {
772 if (chat_playerNum != -1 )
773
774 Com_sprintf( buffer, sizeof( buffer ), "tell %i \"%s\"\n", chat_playerNum, chatField.buffer );
775
776 else if (chat_team)
777
778 Com_sprintf( buffer, sizeof( buffer ), "say_team \"%s\"\n", chatField.buffer );
779 else
780 Com_sprintf( buffer, sizeof( buffer ), "say \"%s\"\n", chatField.buffer );
781
782
783
784 CL_AddReliableCommand(buffer, qfalse);
785 }
786 Key_SetCatcher( Key_GetCatcher( ) & ~KEYCATCH_MESSAGE );
787 Field_Clear( &chatField );
788 return;
789 }
790
791 Field_KeyDownEvent( &chatField, key );
792 }
793
794 //============================================================================
795
796
797 qboolean Key_GetOverstrikeMode( void ) {
798 return key_overstrikeMode;
799 }
800
801
802 void Key_SetOverstrikeMode( qboolean state ) {
803 key_overstrikeMode = state;
804 }
805
806
807 /*
808 ===================
809 Key_IsDown
810 ===================
811 */
812 qboolean Key_IsDown( int keynum ) {
813 if ( keynum < 0 || keynum >= MAX_KEYS ) {
814 return qfalse;
815 }
816
817 return keys[keynum].down;
818 }
819
820 /*
821 ===================
822 Key_StringToKeynum
823
824 Returns a key number to be used to index keys[] by looking at
825 the given string. Single ascii characters return themselves, while
826 the K_* names are matched up.
827
828 0x11 will be interpreted as raw hex, which will allow new controlers
829
830 to be configured even if they don't have defined names.
831 ===================
832 */
833 int Key_StringToKeynum( char *str ) {
834 keyname_t *kn;
835 int n;
836
837 if ( !str || !str[0] ) {
838 return -1;
839 }
840 if ( !str[1] ) {
841 return tolower( str[0] );
842 }
843
844 // check for hex code
845 n = Com_HexStrToInt( str );
846 if ( n >= 0 && n < MAX_KEYS ) {
847 return n;
848 }
849
850 // scan for a text match
851 for ( kn=keynames ; kn->name ; kn++ ) {
852 if ( !Q_stricmp( str,kn->name ) )
853 return kn->keynum;
854 }
855
856 return -1;
857 }
858
859 /*
860 ===================
861 Key_KeynumToString
862
863 Returns a string (either a single ascii char, a K_* name, or a 0x11 hex string) for the
864 given keynum.
865 ===================
866 */
867 char *Key_KeynumToString( int keynum ) {
868 keyname_t *kn;
869 static char tinystr[5];
870 int i, j;
871
872 if ( keynum == -1 ) {
873 return "<KEY NOT FOUND>";
874 }
875
876 if ( keynum < 0 || keynum >= MAX_KEYS ) {
877 return "<OUT OF RANGE>";
878 }
879
880 // check for printable ascii (don't use quote)
881 if ( keynum > 32 && keynum < 127 && keynum != '"' && keynum != ';' ) {
882 tinystr[0] = keynum;
883 tinystr[1] = 0;
884 return tinystr;
885 }
886
887 // check for a key string
888 for ( kn=keynames ; kn->name ; kn++ ) {
889 if (keynum == kn->keynum) {
890 return kn->name;
891 }
892 }
893
894 // make a hex string
895 i = keynum >> 4;
896 j = keynum & 15;
897
898 tinystr[0] = '0';
899 tinystr[1] = 'x';
900 tinystr[2] = i > 9 ? i - 10 + 'a' : i + '0';
901 tinystr[3] = j > 9 ? j - 10 + 'a' : j + '0';
902 tinystr[4] = 0;
903
904 return tinystr;
905 }
906
907
908 /*
909 ===================
910 Key_SetBinding
911 ===================
912 */
913 void Key_SetBinding( int keynum, const char *binding ) {
914 if ( keynum < 0 || keynum >= MAX_KEYS ) {
915 return;
916 }
917
918 // free old bindings
919 if ( keys[ keynum ].binding ) {
920 Z_Free( keys[ keynum ].binding );
921 }
922
923 // allocate memory for new binding
924 keys[keynum].binding = CopyString( binding );
925
926 // consider this like modifying an archived cvar, so the
927 // file write will be triggered at the next opportunity
928 cvar_modifiedFlags |= CVAR_ARCHIVE;
929 }
930
931
932 /*
933 ===================
934 Key_GetBinding
935 ===================
936 */
937 char *Key_GetBinding( int keynum ) {
938 if ( keynum < 0 || keynum >= MAX_KEYS ) {
939 return "";
940 }
941
942 return keys[ keynum ].binding;
943 }
944
945 /*
946 ===================
947 Key_GetKey
948 ===================
949 */
950
951 int Key_GetKey(const char *binding) {
952 int i;
953
954 if (binding) {
955 for (i=0 ; i < MAX_KEYS ; i++) {
956 if (keys[i].binding && Q_stricmp(binding, keys[i].binding) == 0) {
957 return i;
958 }
959 }
960 }
961 return -1;
962 }
963
964 /*
965 ===================
966 Key_Unbind_f
967 ===================
968 */
969 void Key_Unbind_f (void)
970 {
971 int b;
972
973 if (Cmd_Argc() != 2)
974 {
975 Com_Printf ("unbind <key> : remove commands from a key\n");
976 return;
977 }
978
979 b = Key_StringToKeynum (Cmd_Argv(1));
980 if (b==-1)
981 {
982 Com_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
983 return;
984 }
985
986 Key_SetBinding (b, "");
987 }
988
989 /*
990 ===================
991 Key_Unbindall_f
992 ===================
993 */
994 void Key_Unbindall_f (void)
995 {
996 int i;
997
998 for (i=0 ; i < MAX_KEYS; i++)
999 if (keys[i].binding)
1000 Key_SetBinding (i, "");
1001 }
1002
1003
1004 /*
1005 ===================
1006 Key_Bind_f
1007 ===================
1008 */
1009 void Key_Bind_f (void)
1010 {
1011 int i, c, b;
1012 char cmd[1024];
1013
1014 c = Cmd_Argc();
1015
1016 if (c < 2)
1017 {
1018 Com_Printf ("bind <key> [command] : attach a command to a key\n");
1019 return;
1020 }
1021 b = Key_StringToKeynum (Cmd_Argv(1));
1022 if (b==-1)
1023 {
1024 Com_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
1025 return;
1026 }
1027
1028 if (c == 2)
1029 {
1030 if (keys[b].binding && keys[b].binding[0])
1031 Com_Printf ("\"%s\" = \"%s\"\n", Key_KeynumToString(b), keys[b].binding );
1032 else
1033 Com_Printf ("\"%s\" is not bound\n", Key_KeynumToString(b) );
1034 return;
1035 }
1036
1037 // copy the rest of the command line
1038 cmd[0] = 0; // start out with a null string
1039 for (i=2 ; i< c ; i++)
1040 {
1041 strcat (cmd, Cmd_Argv(i));
1042 if (i != (c-1))
1043 strcat (cmd, " ");
1044 }
1045
1046 Key_SetBinding (b, cmd);
1047 }
1048
1049 /*
1050 ============
1051 Key_WriteBindings
1052
1053 Writes lines containing "bind key value"
1054 ============
1055 */
1056 void Key_WriteBindings( fileHandle_t f ) {
1057 int i;
1058
1059 FS_Printf (f, "unbindall\n" );
1060
1061 for (i=0 ; i<MAX_KEYS ; i++) {
1062 if (keys[i].binding && keys[i].binding[0] ) {
1063 FS_Printf (f, "bind %s \"%s\"\n", Key_KeynumToString(i), keys[i].binding);
1064
1065 }
1066
1067 }
1068 }
1069
1070
1071 /*
1072 ============
1073 Key_Bindlist_f
1074
1075 ============
1076 */
1077 void Key_Bindlist_f( void ) {
1078 int i;
1079
1080 for ( i = 0 ; i < MAX_KEYS ; i++ ) {
1081 if ( keys[i].binding && keys[i].binding[0] ) {
1082 Com_Printf( "%s \"%s\"\n", Key_KeynumToString(i), keys[i].binding );
1083 }
1084 }
1085 }
1086
1087 /*
1088 ============
1089 Key_KeynameCompletion
1090 ============
1091 */
1092 void Key_KeynameCompletion( void(*callback)(const char *s) ) {
1093 int i;
1094
1095 for( i = 0; keynames[ i ].name != NULL; i++ )
1096 callback( keynames[ i ].name );
1097 }
1098
1099 /*
1100 ====================
1101 Key_CompleteUnbind
1102 ====================
1103 */
1104 static void Key_CompleteUnbind( char *args, int argNum )
1105 {
1106 if( argNum == 2 )
1107 {
1108 // Skip "unbind "
1109 char *p = Com_SkipTokens( args, 1, " " );
1110
1111 if( p > args )
1112 Field_CompleteKeyname( );
1113 }
1114 }
1115
1116 /*
1117 ====================
1118 Key_CompleteBind
1119 ====================
1120 */
1121 static void Key_CompleteBind( char *args, int argNum )
1122 {
1123 char *p;
1124
1125 if( argNum == 2 )
1126 {
1127 // Skip "bind "
1128 p = Com_SkipTokens( args, 1, " " );
1129
1130 if( p > args )
1131 Field_CompleteKeyname( );
1132 }
1133 else if( argNum >= 3 )
1134 {
1135 // Skip "bind <key> "
1136 p = Com_SkipTokens( args, 2, " " );
1137
1138 if( p > args )
1139 Field_CompleteCommand( p, qtrue, qtrue );
1140 }
1141 }
1142
1143 /*
1144 ===================
1145 CL_InitKeyCommands
1146 ===================
1147 */
1148 void CL_InitKeyCommands( void ) {
1149 // register our functions
1150 Cmd_AddCommand ("bind",Key_Bind_f);
1151 Cmd_SetCommandCompletionFunc( "bind", Key_CompleteBind );
1152 Cmd_AddCommand ("unbind",Key_Unbind_f);
1153 Cmd_SetCommandCompletionFunc( "unbind", Key_CompleteUnbind );
1154 Cmd_AddCommand ("unbindall",Key_Unbindall_f);
1155 Cmd_AddCommand ("bindlist",Key_Bindlist_f);
1156 }
1157
1158 /*
1159 ===================
1160 CL_BindUICommand
1161
1162 Returns qtrue if bind command should be executed while user interface is shown
1163 ===================
1164 */
1165 static qboolean CL_BindUICommand( const char *cmd ) {
1166 if ( Key_GetCatcher( ) & KEYCATCH_CONSOLE )
1167 return qfalse;
1168
1169 if ( !Q_stricmp( cmd, "toggleconsole" ) )
1170 return qtrue;
1171 if ( !Q_stricmp( cmd, "togglemenu" ) )
1172 return qtrue;
1173
1174 return qfalse;
1175 }
1176
1177 /*
1178 ===================
1179 CL_ParseBinding
1180
1181 Execute the commands in the bind string
1182 ===================
1183 */
1184 void CL_ParseBinding( int key, qboolean down, unsigned time )
1185 {
1186 char buf[ MAX_STRING_CHARS ], *p = buf, *end;
1187 qboolean allCommands, allowUpCmds;
1188
1189 if( clc.state == CA_DISCONNECTED && Key_GetCatcher( ) == 0 )
1190 return;
1191 if( !keys[key].binding || !keys[key].binding[0] )
1192 return;
1193 Q_strncpyz( buf, keys[key].binding, sizeof( buf ) );
1194
1195 // run all bind commands if console, ui, etc aren't reading keys
1196 allCommands = ( Key_GetCatcher( ) == 0 );
1197
1198 // allow button up commands if in game even if key catcher is set
1199 allowUpCmds = ( clc.state != CA_DISCONNECTED );
1200
1201 while( 1 )
1202 {
1203 while( isspace( *p ) )
1204 p++;
1205 end = strchr( p, ';' );
1206 if( end )
1207 *end = '\0';
1208 if( *p == '+' )
1209 {
1210 // button commands add keynum and time as parameters
1211 // so that multiple sources can be discriminated and
1212 // subframe corrected
1213 if ( allCommands || ( allowUpCmds && !down ) ) {
1214 char cmd[1024];
1215 Com_sprintf( cmd, sizeof( cmd ), "%c%s %d %d\n",
1216 ( down ) ? '+' : '-', p + 1, key, time );
1217 Cbuf_AddText( cmd );
1218 }
1219 }
1220 else if( down )
1221 {
1222 // normal commands only execute on key press
1223 if ( allCommands || CL_BindUICommand( p ) ) {
1224 Cbuf_AddText( p );
1225 Cbuf_AddText( "\n" );
1226 }
1227 }
1228 if( !end )
1229 break;
1230 p = end + 1;
1231 }
1232 }
1233
1234 /*
1235 ===================
1236 CL_KeyDownEvent
1237
1238 Called by CL_KeyEvent to handle a keypress
1239 ===================
1240 */
1241 void CL_KeyDownEvent( int key, unsigned time )
1242 {
1243 keys[key].down = qtrue;
1244 keys[key].repeats++;
1245 if( keys[key].repeats == 1 )
1246 anykeydown++;
1247
1248 if( keys[K_ALT].down && key == K_ENTER )
1249 {
1250 // don't repeat fullscreen toggle when keys are held down
1251 if ( keys[K_ENTER].repeats > 1 ) {
1252 return;
1253 }
1254
1255 Cvar_SetValue( "r_fullscreen",
1256 !Cvar_VariableIntegerValue( "r_fullscreen" ) );
1257 return;
1258 }
1259
1260 // console key is hardcoded, so the user can never unbind it
1261 if( key == K_CONSOLE || ( keys[K_SHIFT].down && key == K_ESCAPE ) )
1262 {
1263 Con_ToggleConsole_f ();
1264 Key_ClearStates ();
1265 return;
1266 }
1267
1268
1269 // keys can still be used for bound actions
1270 if ( ( key < 128 || key == K_MOUSE1 ) &&
1271 ( clc.demoplaying || clc.state == CA_CINEMATIC ) && Key_GetCatcher( ) == 0 ) {
1272
1273 if (Cvar_VariableValue ("com_cameraMode") == 0) {
1274 Cvar_Set ("nextdemo","");
1275 key = K_ESCAPE;
1276 }
1277 }
1278
1279 // escape is always handled special
1280 if ( key == K_ESCAPE ) {
1281 if ( Key_GetCatcher( ) & KEYCATCH_MESSAGE ) {
1282 // clear message mode
1283 Message_Key( key );
1284 return;
1285 }
1286
1287 // escape always gets out of CGAME stuff
1288 if (Key_GetCatcher( ) & KEYCATCH_CGAME) {
1289 Key_SetCatcher( Key_GetCatcher( ) & ~KEYCATCH_CGAME );
1290 VM_Call (cgvm, CG_EVENT_HANDLING, CGAME_EVENT_NONE);
1291 return;
1292 }
1293
1294 if ( !( Key_GetCatcher( ) & KEYCATCH_UI ) ) {
1295 if ( clc.state == CA_ACTIVE && !clc.demoplaying ) {
1296 VM_Call( uivm, UI_SET_ACTIVE_MENU, UIMENU_INGAME );
1297 }
1298 else if ( clc.state != CA_DISCONNECTED ) {
1299 CL_Disconnect_f();
1300 S_StopAllSounds();
1301 VM_Call( uivm, UI_SET_ACTIVE_MENU, UIMENU_MAIN );
1302 }
1303 return;
1304 }
1305
1306 VM_Call( uivm, UI_KEY_EVENT, key, qtrue );
1307 return;
1308 }
1309
1310 // send the bound action
1311 CL_ParseBinding( key, qtrue, time );
1312
1313 // distribute the key down event to the appropriate handler
1314 if ( Key_GetCatcher( ) & KEYCATCH_CONSOLE ) {
1315 Console_Key( key );
1316 } else if ( Key_GetCatcher( ) & KEYCATCH_UI ) {
1317 if ( uivm ) {
1318 VM_Call( uivm, UI_KEY_EVENT, key, qtrue );
1319 }
1320 } else if ( Key_GetCatcher( ) & KEYCATCH_CGAME ) {
1321 if ( cgvm ) {
1322 VM_Call( cgvm, CG_KEY_EVENT, key, qtrue );
1323 }
1324 } else if ( Key_GetCatcher( ) & KEYCATCH_MESSAGE ) {
1325 Message_Key( key );
1326 } else if ( clc.state == CA_DISCONNECTED ) {
1327 Console_Key( key );
1328 }
1329 }
1330
1331 /*
1332 ===================
1333 CL_KeyUpEvent
1334
1335 Called by CL_KeyEvent to handle a keyrelease
1336 ===================
1337 */
1338 void CL_KeyUpEvent( int key, unsigned time )
1339 {
1340 keys[key].repeats = 0;
1341 keys[key].down = qfalse;
1342 anykeydown--;
1343
1344 if (anykeydown < 0) {
1345 anykeydown = 0;
1346 }
1347
1348 // don't process key-up events for the console key
1349 if ( key == K_CONSOLE || ( key == K_ESCAPE && keys[K_SHIFT].down ) )
1350 return;
1351
1352 //
1353 // key up events only perform actions if the game key binding is
1354 // a button command (leading + sign). These will be processed even in
1355 // console mode and menu mode, to keep the character from continuing
1356 // an action started before a mode switch.
1357 //
1358 CL_ParseBinding( key, qfalse, time );
1359
1360 if ( Key_GetCatcher( ) & KEYCATCH_UI && uivm ) {
1361 VM_Call( uivm, UI_KEY_EVENT, key, qfalse );
1362 } else if ( Key_GetCatcher( ) & KEYCATCH_CGAME && cgvm ) {
1363 VM_Call( cgvm, CG_KEY_EVENT, key, qfalse );
1364 }
1365 }
1366
1367 /*
1368 ===================
1369 CL_KeyEvent
1370
1371 Called by the system for both key up and key down events
1372 ===================
1373 */
1374 void CL_KeyEvent (int key, qboolean down, unsigned time) {
1375 if( down )
1376 CL_KeyDownEvent( key, time );
1377 else
1378 CL_KeyUpEvent( key, time );
1379 }
1380
1381 /*
1382 ===================
1383 CL_CharEvent
1384
1385 Normal keyboard characters, already shifted / capslocked / etc
1386 ===================
1387 */
1388 void CL_CharEvent( int key ) {
1389 // delete is not a printable character and is
1390 // otherwise handled by Field_KeyDownEvent
1391 if ( key == 127 ) {
1392 return;
1393 }
1394
1395 // distribute the key down event to the appropriate handler
1396 if ( Key_GetCatcher( ) & KEYCATCH_CONSOLE )
1397 {
1398 Field_CharEvent( &g_consoleField, key );
1399 }
1400 else if ( Key_GetCatcher( ) & KEYCATCH_UI )
1401 {
1402 VM_Call( uivm, UI_KEY_EVENT, key | K_CHAR_FLAG, qtrue );
1403 }
1404 else if ( Key_GetCatcher( ) & KEYCATCH_MESSAGE )
1405 {
1406 Field_CharEvent( &chatField, key );
1407 }
1408 else if ( clc.state == CA_DISCONNECTED )
1409 {
1410 Field_CharEvent( &g_consoleField, key );
1411 }
1412 }
1413
1414
1415 /*
1416 ===================
1417 Key_ClearStates
1418 ===================
1419 */
1420 void Key_ClearStates (void)
1421 {
1422 int i;
1423
1424 anykeydown = 0;
1425
1426 for ( i=0 ; i < MAX_KEYS ; i++ ) {
1427 if ( keys[i].down ) {
1428 CL_KeyEvent( i, qfalse, 0 );
1429
1430 }
1431 keys[i].down = 0;
1432 keys[i].repeats = 0;
1433 }
1434 }
1435
1436 static int keyCatchers = 0;
1437
1438 /*
1439 ====================
1440 Key_GetCatcher
1441 ====================
1442 */
1443 int Key_GetCatcher( void ) {
1444 return keyCatchers;
1445 }
1446
1447 /*
1448 ====================
1449 Key_SetCatcher
1450 ====================
1451 */
1452 void Key_SetCatcher( int catcher ) {
1453 // If the catcher state is changing, clear all key states
1454 if( catcher != keyCatchers )
1455 Key_ClearStates( );
1456
1457 keyCatchers = catcher;
1458 }
1459
1460 // This must not exceed MAX_CMD_LINE
1461 #define MAX_CONSOLE_SAVE_BUFFER 1024
1462 #define CONSOLE_HISTORY_FILE "q3history"
1463 static char consoleSaveBuffer[ MAX_CONSOLE_SAVE_BUFFER ];
1464 static int consoleSaveBufferSize = 0;
1465
1466 /*
1467 ================
1468 CL_LoadConsoleHistory
1469
1470 Load the console history from CONSOLE_HISTORY_FILE
1471 ================
1472 */
1473 void CL_LoadConsoleHistory( void )
1474 {
1475 char *token, *text_p;
1476 int i, numChars, numLines = 0;
1477 fileHandle_t f;
1478
1479 consoleSaveBufferSize = FS_FOpenFileRead( CONSOLE_HISTORY_FILE, &f, qfalse );
1480 if( !f )
1481 {
1482 Com_Printf( "Couldn't read %s.\n", CONSOLE_HISTORY_FILE );
1483 return;
1484 }
1485
1486 if( consoleSaveBufferSize < MAX_CONSOLE_SAVE_BUFFER &&
1487 FS_Read( consoleSaveBuffer, consoleSaveBufferSize, f ) == consoleSaveBufferSize )
1488 {
1489 consoleSaveBuffer[consoleSaveBufferSize] = '\0';
1490 text_p = consoleSaveBuffer;
1491
1492 for( i = COMMAND_HISTORY - 1; i >= 0; i-- )
1493 {
1494 if( !*( token = COM_Parse( &text_p ) ) )
1495 break;
1496
1497 historyEditLines[ i ].cursor = atoi( token );
1498
1499 if( !*( token = COM_Parse( &text_p ) ) )
1500 break;
1501
1502 historyEditLines[ i ].scroll = atoi( token );
1503
1504 if( !*( token = COM_Parse( &text_p ) ) )
1505 break;
1506
1507 numChars = atoi( token );
1508 text_p++;
1509 if( numChars > ( strlen( consoleSaveBuffer ) - ( text_p - consoleSaveBuffer ) ) )
1510 {
1511 Com_DPrintf( S_COLOR_YELLOW "WARNING: probable corrupt history\n" );
1512 break;
1513 }
1514 Com_Memcpy( historyEditLines[ i ].buffer,
1515 text_p, numChars );
1516 historyEditLines[ i ].buffer[ numChars ] = '\0';
1517 text_p += numChars;
1518
1519 numLines++;
1520 }
1521
1522 memmove( &historyEditLines[ 0 ], &historyEditLines[ i + 1 ],
1523 numLines * sizeof( field_t ) );
1524 for( i = numLines; i < COMMAND_HISTORY; i++ )
1525 Field_Clear( &historyEditLines[ i ] );
1526
1527 historyLine = nextHistoryLine = numLines;
1528 }
1529 else
1530 Com_Printf( "Couldn't read %s.\n", CONSOLE_HISTORY_FILE );
1531
1532 FS_FCloseFile( f );
1533 }
1534
1535 /*
1536 ================
1537 CL_SaveConsoleHistory
1538
1539 Save the console history into CONSOLE_HISTORY_FILE
1540 so that it persists across invocations of q3
1541 ================
1542 */
1543 void CL_SaveConsoleHistory( void )
1544 {
1545 int i;
1546 int lineLength, saveBufferLength, additionalLength;
1547 fileHandle_t f;
1548
1549 consoleSaveBuffer[ 0 ] = '\0';
1550
1551 i = ( nextHistoryLine - 1 ) % COMMAND_HISTORY;
1552 do
1553 {
1554 if( historyEditLines[ i ].buffer[ 0 ] )
1555 {
1556 lineLength = strlen( historyEditLines[ i ].buffer );
1557 saveBufferLength = strlen( consoleSaveBuffer );
1558
1559 //ICK
1560 additionalLength = lineLength + strlen( "999 999 999 " );
1561
1562 if( saveBufferLength + additionalLength < MAX_CONSOLE_SAVE_BUFFER )
1563 {
1564 Q_strcat( consoleSaveBuffer, MAX_CONSOLE_SAVE_BUFFER,
1565 va( "%d %d %d %s ",
1566 historyEditLines[ i ].cursor,
1567 historyEditLines[ i ].scroll,
1568 lineLength,
1569 historyEditLines[ i ].buffer ) );
1570 }
1571 else
1572 break;
1573 }
1574 i = ( i - 1 + COMMAND_HISTORY ) % COMMAND_HISTORY;
1575 }
1576 while( i != ( nextHistoryLine - 1 ) % COMMAND_HISTORY );
1577
1578 consoleSaveBufferSize = strlen( consoleSaveBuffer );
1579
1580 f = FS_FOpenFileWrite_HomeState( CONSOLE_HISTORY_FILE );
1581 if( !f )
1582 {
1583 Com_Printf( "Couldn't write %s.\n", CONSOLE_HISTORY_FILE );
1584 return;
1585 }
1586
1587 if( FS_Write( consoleSaveBuffer, consoleSaveBufferSize, f ) < consoleSaveBufferSize )
1588 Com_Printf( "Couldn't write %s.\n", CONSOLE_HISTORY_FILE );
1589
1590 FS_FCloseFile( f );
1591 }