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.
22 #if !defined(WIN32) || defined(__MINGW32__)
30 float con_cursorspeed = 4;
32 #define CON_TEXTSIZE 131072
34 // total lines in console scrollback
36 // lines up from bottom to display
38 // where next message will be printed
40 // offset in current line for next print
45 cvar_t con_notifytime = {CVAR_SAVE, "con_notifytime","3"};
46 cvar_t con_notify = {CVAR_SAVE, "con_notify","4"};
48 #define MAX_NOTIFYLINES 32
49 // cl.time time the line was generated for transparent notify lines
50 float con_times[MAX_NOTIFYLINES];
54 #define MAXCMDLINE 256
55 extern char key_lines[32][MAXCMDLINE];
57 extern int key_linepos;
58 extern int key_insert;
61 qboolean con_initialized;
63 mempool_t *console_mempool;
67 ==============================================================================
71 ==============================================================================
74 cvar_t log_file = {0, "log_file",""};
75 char crt_log_file [MAX_OSPATH] = "";
76 qfile_t* logfile = NULL;
78 qbyte* logqueue = NULL;
82 void Log_ConPrint (const char *msg);
89 const char* Log_Timestamp (const char *desc)
91 static char timestamp [128];
93 const struct tm *crt_tm;
96 // Build the time stamp (ex: "Wed Jun 30 21:49:08 1993");
98 crt_tm = localtime (&crt_time);
99 strftime (timestring, sizeof (timestring), "%a %b %d %H:%M:%S %Y", crt_tm);
102 snprintf (timestamp, sizeof (timestamp), "====== %s (%s) ======\n", desc, timestring);
104 snprintf (timestamp, sizeof (timestamp), "====== %s ======\n", timestring);
117 // Allocate a log queue
119 logqueue = Mem_Alloc (tempmempool, logq_size);
122 Cvar_RegisterVariable (&log_file);
124 // support for the classic Quake option
125 // COMMANDLINEOPTION: Console: -condebug logs console messages to qconsole.log, see also log_file
126 if (COM_CheckParm ("-condebug") != 0)
127 Cvar_SetQuick (&log_file, "qconsole.log");
138 if (logfile != NULL || log_file.string[0] == '\0')
141 logfile = FS_Open (log_file.string, "a", false);
144 strlcpy (crt_log_file, log_file.string, sizeof (crt_log_file));
145 FS_Print (logfile, Log_Timestamp ("Log started"));
155 void Log_Close (void)
160 FS_Print (logfile, Log_Timestamp ("Log stopped"));
161 FS_Print (logfile, "\n");
165 crt_log_file[0] = '\0';
174 void Log_Start (void)
178 // Dump the contents of the log queue into the log file and free it
179 if (logqueue != NULL)
181 if (logfile != NULL && logq_ind != 0)
182 FS_Write (logfile, logqueue, logq_ind);
196 void Log_ConPrint (const char *msg)
198 static qboolean inprogress = false;
200 // don't allow feedback loops with memory error reports
205 // Until the host is completely initialized, we maintain a log queue
206 // to store the messages, since the log can't be started before
207 if (logqueue != NULL)
209 size_t remain = logq_size - logq_ind;
210 size_t len = strlen (msg);
212 // If we need to enlarge the log queue
215 unsigned int factor = ((logq_ind + len) / logq_size) + 1;
219 newqueue = Mem_Alloc (tempmempool, logq_size);
220 memcpy (newqueue, logqueue, logq_ind);
223 remain = logq_size - logq_ind;
225 memcpy (&logqueue[logq_ind], msg, len);
232 // Check if log_file has changed
233 if (strcmp (crt_log_file, log_file.string) != 0)
239 // If a log file is available
241 FS_Print (logfile, msg);
251 void Log_Printf (const char *logfilename, const char *fmt, ...)
255 file = FS_Open (logfilename, "a", true);
260 va_start (argptr, fmt);
261 FS_VPrintf (file, fmt, argptr);
270 ==============================================================================
274 ==============================================================================
282 void Con_ToggleConsole_f (void)
284 // toggle the 'user wants console' bit
285 key_consoleactive ^= KEY_CONSOLEACTIVE_USER;
286 memset (con_times, 0, sizeof(con_times));
294 void Con_Clear_f (void)
297 memset (con_text, ' ', CON_TEXTSIZE);
306 void Con_ClearNotify (void)
310 for (i=0 ; i<MAX_NOTIFYLINES ; i++)
320 void Con_MessageMode_f (void)
322 key_dest = key_message;
332 void Con_MessageMode2_f (void)
334 key_dest = key_message;
343 If the line width has changed, reformat the buffer.
346 void Con_CheckResize (void)
348 int i, j, width, oldwidth, oldtotallines, numlines, numchars;
349 char tbuf[CON_TEXTSIZE];
351 width = (vid.conwidth >> 3);
353 if (width == con_linewidth)
356 if (width < 1) // video hasn't been initialized yet
359 con_linewidth = width;
360 con_totallines = CON_TEXTSIZE / con_linewidth;
361 memset (con_text, ' ', CON_TEXTSIZE);
365 oldwidth = con_linewidth;
366 con_linewidth = width;
367 oldtotallines = con_totallines;
368 con_totallines = CON_TEXTSIZE / con_linewidth;
369 numlines = oldtotallines;
371 if (con_totallines < numlines)
372 numlines = con_totallines;
376 if (con_linewidth < numchars)
377 numchars = con_linewidth;
379 memcpy (tbuf, con_text, CON_TEXTSIZE);
380 memset (con_text, ' ', CON_TEXTSIZE);
382 for (i=0 ; i<numlines ; i++)
384 for (j=0 ; j<numchars ; j++)
386 con_text[(con_totallines - 1 - i) * con_linewidth + j] =
387 tbuf[((con_current - i + oldtotallines) %
388 oldtotallines) * oldwidth + j];
396 con_current = con_totallines - 1;
406 console_mempool = Mem_AllocPool("console", 0, NULL);
407 con_text = Mem_Alloc(console_mempool, CON_TEXTSIZE);
408 memset (con_text, ' ', CON_TEXTSIZE);
412 // register our cvars
413 Cvar_RegisterVariable (&con_notifytime);
414 Cvar_RegisterVariable (&con_notify);
416 // register our commands
417 Cmd_AddCommand ("toggleconsole", Con_ToggleConsole_f);
418 Cmd_AddCommand ("messagemode", Con_MessageMode_f);
419 Cmd_AddCommand ("messagemode2", Con_MessageMode2_f);
420 Cmd_AddCommand ("clear", Con_Clear_f);
422 con_initialized = true;
423 Con_Print("Console initialized.\n");
432 void Con_Linefeed (void)
439 memset (&con_text[(con_current%con_totallines)*con_linewidth], ' ', con_linewidth);
446 Handles cursor positioning, line wrapping, etc
447 All console printing must go through this in order to be displayed
448 If no console is visible, the notify window will pop up.
451 void Con_PrintToHistory(const char *txt)
458 mask = 128; // go to colored text
459 S_LocalSound ("sound/misc/talk.wav");
463 else if (txt[0] == 2)
465 mask = 128; // go to colored text
475 for (l=0 ; l< con_linewidth ; l++)
480 if (l != con_linewidth && (con_x + l > con_linewidth) )
495 // mark time for transparent overlay
496 if (con_current >= 0)
498 if (con_notify.integer < 0)
499 Cvar_SetValueQuick(&con_notify, 0);
500 if (con_notify.integer > MAX_NOTIFYLINES)
501 Cvar_SetValueQuick(&con_notify, MAX_NOTIFYLINES);
502 if (con_notify.integer > 0)
503 con_times[con_current % con_notify.integer] = cl.time;
518 default: // display character and advance
519 y = con_current % con_totallines;
520 con_text[y*con_linewidth+con_x] = c | mask;
522 if (con_x >= con_linewidth)
530 /* The translation table between the graphical font and plain ASCII --KB */
531 static char qfont_table[256] = {
532 '\0', '#', '#', '#', '#', '.', '#', '#',
533 '#', 9, 10, '#', ' ', 13, '.', '.',
534 '[', ']', '0', '1', '2', '3', '4', '5',
535 '6', '7', '8', '9', '.', '<', '=', '>',
536 ' ', '!', '"', '#', '$', '%', '&', '\'',
537 '(', ')', '*', '+', ',', '-', '.', '/',
538 '0', '1', '2', '3', '4', '5', '6', '7',
539 '8', '9', ':', ';', '<', '=', '>', '?',
540 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
541 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
542 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
543 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
544 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
545 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
546 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
547 'x', 'y', 'z', '{', '|', '}', '~', '<',
549 '<', '=', '>', '#', '#', '.', '#', '#',
550 '#', '#', ' ', '#', ' ', '>', '.', '.',
551 '[', ']', '0', '1', '2', '3', '4', '5',
552 '6', '7', '8', '9', '.', '<', '=', '>',
553 ' ', '!', '"', '#', '$', '%', '&', '\'',
554 '(', ')', '*', '+', ',', '-', '.', '/',
555 '0', '1', '2', '3', '4', '5', '6', '7',
556 '8', '9', ':', ';', '<', '=', '>', '?',
557 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
558 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
559 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
560 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
561 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
562 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
563 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
564 'x', 'y', 'z', '{', '|', '}', '~', '<'
571 Prints to all appropriate console targets, and adds timestamps
574 extern cvar_t timestamps;
575 extern cvar_t timeformat;
576 extern qboolean sys_nostdout;
577 void Con_Print(const char *msg)
579 static int index = 0;
580 static char line[16384];
586 // if this is the beginning of a new line, print timestamp
587 char *timestamp = timestamps.integer ? Sys_TimeString(timeformat.string) : "";
588 // special color codes for chat messages must always come first
589 // for Con_PrintToHistory to work properly
591 line[index++] = *msg++;
593 for (;*timestamp;index++, timestamp++)
594 if (index < sizeof(line) - 2)
595 line[index] = *timestamp;
597 // append the character
598 line[index++] = *msg;
599 // if this is a newline character, we have a complete line to print
600 if (*msg == '\n' || index >= 16000)
602 // terminate the line
606 // send to scrollable buffer
607 if (con_initialized && cls.state != ca_dedicated)
608 Con_PrintToHistory(line);
609 // send to terminal or dedicated server window
613 for (p = (unsigned char *) line;*p; p++)
614 *p = qfont_table[*p];
615 Sys_PrintToTerminal(line);
617 // empty the line buffer
624 // LordHavoc: increased from 4096 to 16384
625 #define MAXPRINTMSG 16384
631 Prints to all appropriate console targets
634 void Con_Printf(const char *fmt, ...)
637 char msg[MAXPRINTMSG];
639 va_start(argptr,fmt);
640 vsnprintf(msg,sizeof(msg),fmt,argptr);
650 A Con_Print that only shows up if the "developer" cvar is set
653 void Con_DPrint(const char *msg)
655 if (!developer.integer)
656 return; // don't confuse non-developers with techie stuff...
664 A Con_Printf that only shows up if the "developer" cvar is set
667 void Con_DPrintf(const char *fmt, ...)
670 char msg[MAXPRINTMSG];
672 if (!developer.integer)
673 return; // don't confuse non-developers with techie stuff...
675 va_start(argptr,fmt);
676 vsnprintf(msg,sizeof(msg),fmt,argptr);
684 ==============================================================================
688 ==============================================================================
696 The input line scrolls horizontally if typing goes beyond the right edge
698 Modified by EvilTypeGuy eviltypeguy@qeradiant.com
701 void Con_DrawInput (void)
705 char editlinecopy[257], *text;
707 if (!key_consoleactive)
708 return; // don't draw anything
710 text = strcpy(editlinecopy, key_lines[edit_line]);
712 // Advanced Console Editing by Radix radix@planetquake.com
713 // Added/Modified by EvilTypeGuy eviltypeguy@qeradiant.com
714 // use strlen of edit_line instead of key_linepos to allow editing
715 // of early characters w/o erasing
719 // fill out remainder with spaces
720 for (i = y; i < 256; i++)
723 // add the cursor frame
724 if ((int)(realtime*con_cursorspeed) & 1) // cursor is visible
725 text[key_linepos] = 11 + 130 * key_insert; // either solid or triangle facing right
727 // text[key_linepos + 1] = 0;
729 // prestep if horizontally scrolling
730 if (key_linepos >= con_linewidth)
731 text += 1 + key_linepos - con_linewidth;
734 DrawQ_String(0, con_vislines - 16, text, con_linewidth, 8, 8, 1, 1, 1, 1, 0);
737 // key_lines[edit_line][key_linepos] = 0;
745 Draws the last few lines of output transparently over the game top
748 void Con_DrawNotify (void)
754 extern char chat_buffer[];
757 if (con_notify.integer < 0)
758 Cvar_SetValueQuick(&con_notify, 0);
759 if (con_notify.integer > MAX_NOTIFYLINES)
760 Cvar_SetValueQuick(&con_notify, MAX_NOTIFYLINES);
761 if (gamemode == GAME_TRANSFUSION)
765 for (i= con_current-con_notify.integer+1 ; i<=con_current ; i++)
769 time = con_times[i % con_notify.integer];
772 time = cl.time - time;
773 if (time > con_notifytime.value)
775 text = con_text + (i % con_totallines)*con_linewidth;
777 if (gamemode == GAME_NEXUIZ) {
780 for (linewidth = con_linewidth; linewidth && text[linewidth-1] == ' '; linewidth--);
781 x = (vid.conwidth - linewidth * 8) / 2;
785 DrawQ_String(x, v, text, con_linewidth, 8, 8, 1, 1, 1, 1, 0);
791 if (key_dest == key_message)
795 // LordHavoc: speedup, and other improvements
797 sprintf(temptext, "say_team:%s%c", chat_buffer, (int) 10+((int)(realtime*con_cursorspeed)&1));
799 sprintf(temptext, "say:%s%c", chat_buffer, (int) 10+((int)(realtime*con_cursorspeed)&1));
800 while (strlen(temptext) >= (size_t) con_linewidth)
802 DrawQ_String (0, v, temptext, con_linewidth, 8, 8, 1, 1, 1, 1, 0);
803 strcpy(temptext, &temptext[con_linewidth]);
806 if (strlen(temptext) > 0)
808 DrawQ_String (0, v, temptext, 0, 8, 8, 1, 1, 1, 1, 0);
818 Draws the console with the solid background
819 The typing input line at the bottom should only be drawn if typing is allowed
822 extern char engineversion[40];
823 void Con_DrawConsole (int lines)
831 // draw the background
832 if (scr_conbrightness.value >= 0.01f)
833 DrawQ_Pic(0, lines - vid.conheight, "gfx/conback", vid.conwidth, vid.conheight, scr_conbrightness.value, scr_conbrightness.value, scr_conbrightness.value, scr_conalpha.value, 0);
835 DrawQ_Fill(0, lines - vid.conheight, vid.conwidth, vid.conheight, 0, 0, 0, scr_conalpha.value, 0);
836 DrawQ_String(vid.conwidth - strlen(engineversion) * 8 - 8, lines - 8, engineversion, 0, 8, 8, 1, 0, 0, 1, 0);
839 con_vislines = lines;
841 rows = (lines-16)>>3; // rows of text to draw
842 y = lines - 16 - (rows<<3); // may start slightly negative
844 for (i = con_current - rows + 1;i <= con_current;i++, y += 8)
846 j = max(i - con_backscroll, 0);
847 text = con_text + (j % con_totallines)*con_linewidth;
849 DrawQ_String(0, y, text, con_linewidth, 8, 8, 1, 1, 1, 1, 0);
852 // draw the input prompt, user text, and cursor if desired
859 New function for tab-completion system
861 MEGA Thanks to Taniwha
864 void Con_DisplayList(const char **list)
866 int i = 0, pos = 0, len = 0, maxlen = 0, width = (con_linewidth - 4);
867 const char **walk = list;
879 if (pos + maxlen >= width) {
885 for (i = 0; i < (maxlen - len); i++)
897 Con_CompleteCommandLine
899 New function for tab-completion system
901 Thanks to Fett erich@heintz.com
905 void Con_CompleteCommandLine (void)
907 const char *cmd = "", *s;
908 const char **list[3] = {0, 0, 0};
909 int c, v, a, i, cmd_len;
911 s = key_lines[edit_line] + 1;
912 // Count number of possible matches
913 c = Cmd_CompleteCountPossible(s);
914 v = Cvar_CompleteCountPossible(s);
915 a = Cmd_CompleteAliasCountPossible(s);
917 if (!(c + v + a)) // No possible matches
920 if (c + v + a == 1) {
922 list[0] = Cmd_CompleteBuildList(s);
924 list[0] = Cvar_CompleteBuildList(s);
926 list[0] = Cmd_CompleteAliasBuildList(s);
928 cmd_len = strlen (cmd);
931 cmd = *(list[0] = Cmd_CompleteBuildList(s));
933 cmd = *(list[1] = Cvar_CompleteBuildList(s));
935 cmd = *(list[2] = Cmd_CompleteAliasBuildList(s));
937 cmd_len = strlen (s);
939 for (i = 0; i < 3; i++) {
940 char ch = cmd[cmd_len];
941 const char **l = list[i];
943 while (*l && (*l)[cmd_len] == ch)
954 for (i = 0; i < con_linewidth - 4; i++)
958 // Print Possible Commands
960 Con_Printf("%i possible command%s\n", c, (c > 1) ? "s: " : ":");
961 Con_DisplayList(list[0]);
965 Con_Printf("%i possible variable%s\n", v, (v > 1) ? "s: " : ":");
966 Con_DisplayList(list[1]);
970 Con_Printf("%i possible aliases%s\n", a, (a > 1) ? "s: " : ":");
971 Con_DisplayList(list[2]);
976 strncpy(key_lines[edit_line] + 1, cmd, cmd_len);
977 key_linepos = cmd_len + 1;
978 if (c + v + a == 1) {
979 key_lines[edit_line][key_linepos] = ' ';
982 key_lines[edit_line][key_linepos] = 0;
984 for (i = 0; i < 3; i++)
986 Mem_Free((void *)list[i]);