/* Copyright (C) 1996-1997 Id Software, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ // console.c #include "quakedef.h" #include "shader.h" console_t *con_head; // first console in the list console_t *con_curwindow; // the (window) console that's currently got focus. console_t *con_current; // points to whatever is the active console (the one that has focus ONLY when kdm_console) console_t *con_mouseover; // points to whichever console's title is currently mouseovered, or null console_t *con_main; // the default console that text will be thrown at. recreated as needed. console_t *con_chat; // points to a chat console #define Font_ScreenWidth() (vid.pixelwidth) static int Con_DrawProgress(int left, int right, int y); static int Con_DrawConsoleLines(console_t *con, conline_t *l, float displayscroll, int sx, int ex, int y, int top, int selactive, int selsx, int selex, int selsy, int seley, float lineagelimit); #ifdef QTERM #include typedef struct qterm_s { console_t *console; qboolean running; HANDLE process; HANDLE pipein; HANDLE pipeout; HANDLE pipeinih; HANDLE pipeoutih; struct qterm_s *next; } qterm_t; qterm_t *qterms; qterm_t *activeqterm; #endif //int con_linewidth; // characters across screen //int con_totallines; // total lines in console scrollback static float con_cursorspeed = 4; static cvar_t con_numnotifylines = CVAR("con_notifylines","4"); //max lines to show static cvar_t con_notifytime = CVAR("con_notifytime","3"); //seconds static cvar_t con_notify_x = CVAR("con_notify_x","0"); static cvar_t con_notify_y = CVAR("con_notify_y","0"); static cvar_t con_notify_w = CVAR("con_notify_w","1"); static cvar_t con_centernotify = CVAR("con_centernotify", "0"); static cvar_t con_displaypossibilities = CVAR("con_displaypossibilities", "1"); static cvar_t con_showcompletion = CVAR("con_showcompletion", "1"); static cvar_t con_maxlines = CVAR("con_maxlines", "1024"); cvar_t cl_chatmode = CVARD("cl_chatmode", "2", "0(nq) - everything is assumed to be a console command. prefix with 'say', or just use a messagemode bind\n1(q3) - everything is assumed to be chat, unless its prefixed with a /\n2(qw) - anything explicitly recognised as a command will be used as a command, anything unrecognised will be a chat message.\n/ prefix is supported in all cases.\nctrl held when pressing enter always makes any implicit chat into team chat instead."); static cvar_t con_numnotifylines_chat = CVAR("con_numnotifylines_chat", "8"); static cvar_t con_notifytime_chat = CVAR("con_notifytime_chat", "8"); cvar_t con_separatechat = CVAR("con_separatechat", "0"); static cvar_t con_timestamps = CVAR("con_timestamps", "0"); static cvar_t con_timeformat = CVAR("con_timeformat", "(%H:%M:%S) "); cvar_t con_textsize = CVARD("con_textsize", "8", "Resize the console text to be a different height, scaled separately from the hud. The value is the height in (virtual) pixels."); static cvar_t con_savehistory = CVARD("con_savehistory", "1", "Write/update conhistory.txt"); extern cvar_t log_developer; void con_window_cb(cvar_t *var, char *oldval) { if (!con_main) return; //doesn't matter right now. if (var->ival) { con_main->flags &= ~CONF_NOTIFY; if (!(con_main->flags & CONF_ISWINDOW)) { con_main->flags |= CONF_ISWINDOW; if (con_current == con_main) Con_SetActive(con_main); } } else { con_main->flags |= CONF_NOTIFY; if (con_main->flags & CONF_ISWINDOW) { con_main->flags &= ~CONF_ISWINDOW; if (con_curwindow == con_main) Con_SetActive(con_main); } } } static cvar_t con_window = CVARCD("con_window", "0", con_window_cb, "States whether the console should be a floating window as in source engine games, or a top-of-the-screen-only thing."); #define NUM_CON_TIMES 24 qboolean con_initialized; /*makes sure the console object works*/ void Con_Finit (console_t *con) { if (con->current == NULL) { con->oldest = con->current = Z_Malloc(sizeof(conline_t)); con->linecount = 0; } if (con->display == NULL) con->display = con->current; con->selstartline = NULL; con->selendline = NULL; con->defaultcharbits = CON_WHITEMASK; con->parseflags = 0; } /*returns a bitmask: 1: currently active 2: has text that has not been seen yet */ int Con_IsActive (console_t *con) { return (con == con_current) | (con->unseentext*2); } /*kills a console_t object. will never destroy the main console (which will only be cleared)*/ void Con_Destroy (console_t *con) { shader_t *shader; console_t **link; conline_t *t; if (con->close) { con->close(con, true); con->close = NULL; } /*purge the lines from the console*/ while (con->current) { t = con->current; con->current = t->older; Z_Free(t); } con->display = con->current = con->oldest = NULL; Con_Footerf(con, false, ""); if (con->completionline) Z_Free(con->completionline); con->completionline = NULL; for (link = &con_head; *link; link = &(*link)->next) { if (*link == con) { (*link) = con->next; break; } } shader = con->backshader; BZ_Free(con); //make sure any special references are fixed up now that its gone if (con_mouseover == con) con_mouseover = NULL; if (con_current == con) con_current = con_head; if (con_curwindow == con) { for (con_curwindow = con_head; con_curwindow; con_curwindow = con_curwindow->next) { if (con_curwindow->flags & CONF_ISWINDOW) break; } if (!con_curwindow) Key_Dest_Remove(kdm_cwindows); } con_mouseover = NULL; if (shader) R_UnloadShader(shader); } /*just purges the background images for various consoles on restart/shutdown*/ void Con_FlushBackgrounds(void) { console_t *con; //fixme: we really need to handle videomaps differently here, for vid_restarts. for (con = con_head; con; con = con->next) { if (con->backshader) R_UnloadShader(con->backshader); con->backshader = NULL; } } /*obtains a console_t without creating*/ console_t *Con_FindConsole(const char *name) { console_t *con; if (!strcmp(name, "current") && con_current) return con_current; if (!strcmp(name, "head") && con_current) return con_head; for (con = con_head; con; con = con->next) { if (!strcmp(con->name, name)) return con; } return NULL; } /*creates a potentially duplicate console_t - please use Con_FindConsole first, as its confusing otherwise*/ console_t *Con_Create(const char *name, unsigned int flags) { console_t *con, *p; if (!name) { static unsigned long seq; name = va("c%lu", seq++); } if (!strcmp(name, "current")) return NULL; if (!strcmp(name, "head")) return NULL; con = Z_Malloc(sizeof(console_t)); Q_strncpyz(con->name, name, sizeof(con->name)); Q_strncpyz(con->title, name, sizeof(con->title)); Q_strncpyz(con->prompt, "]", sizeof(con->prompt)); con->flags = flags; Con_Finit(con); //insert at end. make it active if you must. if (!con_head) con_head = con; else { for (p = con_head; p->next; p = p->next) ; p->next = con; } return con; } static qboolean Con_Main_BlockClose(console_t *con, qboolean force) { if (!force) { //trying to close it just hides it (this is to avoid it getting cleared). if (con_curwindow == con) Key_Dest_Remove(kdm_cwindows); return false; } con_main = NULL; //its forced to die. and don't forget it. return true; } console_t *Con_GetMain(void) { if (!con_main) { con_main = Con_Create("", 0); con_main->linebuffered = Con_ExecuteLine; con_main->commandcompletion = true; con_main->wnd_w = 640; con_main->wnd_h = 480; con_main->wnd_x = 0; con_main->wnd_y = 0; con_main->close = Con_Main_BlockClose; Q_strncpyz(con_main->title, "MAIN", sizeof(con_main->title)); Q_strncpyz(con_main->prompt, "]", sizeof(con_main->prompt)); Cvar_ForceCallback(&con_window); } return con_main; } /*sets a console as the active one*/ void Con_SetActive (console_t *con) { if (con->flags & CONF_ISWINDOW) { console_t *prev; Key_Dest_Add(kdm_cwindows); Key_Dest_Remove(kdm_console); if (con_curwindow == con) return; for (prev = con_head; prev; prev = prev->next) { if (prev->next == con) { prev->next = con->next; while(prev->next) { prev = prev->next; } prev->next = con; con->next = NULL; break; } } con_curwindow = con; } else { if (con_curwindow == con) con_curwindow = NULL; Key_Dest_Add(kdm_console); Key_Dest_Remove(kdm_cwindows); con_current = con; } Con_Footerf(con, false, ""); con->buttonsdown = CB_NONE; } /*for enumerating consoles*/ qboolean Con_NameForNum(int num, char *buffer, int buffersize) { console_t *con; for (con = con_head; con; con = con->next, num--) { if (num <= 0) { Q_strncpyz(buffer, con->name, buffersize); return true; } } if (buffersize>0) *buffer = '\0'; return false; } #ifdef QTERM void QT_Kill(qterm_t *qt, qboolean killconsole) { qterm_t **link; qt->console->close = NULL; qt->console->userdata = NULL; qt->console->redirect = NULL; if (killconsole) Con_Destroy(qt->console); //yes this loop will crash if you're not careful. it makes it easier to debug. for (link = &qterms; ; link = &(*link)->next) { if (*link == qt) { *link = qt->next; break; } } CloseHandle(qt->pipein); CloseHandle(qt->pipeout); CloseHandle(qt->pipeinih); CloseHandle(qt->pipeoutih); CloseHandle(qt->process); Z_Free(qt); } void QT_Update(void) { char buffer[2048]; DWORD ret; qterm_t *qt, *n; for (qt = qterms; qt; ) { if (qt->running) { if (WaitForSingleObject(qt->process, 0) == WAIT_TIMEOUT) { if ((ret=GetFileSize(qt->pipeout, NULL))) { if (ret!=INVALID_FILE_SIZE) { ReadFile(qt->pipeout, buffer, sizeof(buffer)-32, &ret, NULL); buffer[ret] = '\0'; Con_PrintCon(qt->console, buffer, PFS_NOMARKUP); } } } else { Con_PrintCon(qt->console, "Process ended\n", PFS_NOMARKUP); qt->running = false; } } n = qt->next; if (!qt->running) { if (!Con_IsActive(qt->console)) QT_Kill(qt, true); } qt = n; } } qboolean QT_KeyPress(console_t *con, unsigned int unicode, int key) { qbyte k[2]; qterm_t *qt = con->userdata; DWORD send = key; //get around a gcc warning k[0] = key; k[1] = '\0'; if (qt->running) { if (*k == '\r') { // *k = '\r'; // WriteFile(qt->pipein, k, 1, &key, NULL); // Con_PrintCon(k, &qt->console, PFS_NOMARKUP); *k = '\n'; } // if (GetFileSize(qt->pipein, NULL)<512) { WriteFile(qt->pipein, k, 1, &send, NULL); Con_PrintCon(qt->console, k, PFS_NOMARKUP); } } return true; } qboolean QT_Close(struct console_s *con, qboolean force) { qterm_t *qt = con->userdata; QT_Kill(qt, false); return true; } void QT_Create(char *command) { HANDLE StdIn[2]; HANDLE StdOut[2]; qterm_t *qt; SECURITY_ATTRIBUTES sa; STARTUPINFO SUInf; PROCESS_INFORMATION ProcInfo; int ret; qt = Z_Malloc(sizeof(*qt)); memset(&sa,0,sizeof(sa)); sa.nLength=sizeof(sa); sa.bInheritHandle=true; CreatePipe(&StdOut[0], &StdOut[1], &sa, 1024); CreatePipe(&StdIn[1], &StdIn[0], &sa, 1024); memset(&SUInf, 0, sizeof(SUInf)); SUInf.cb = sizeof(SUInf); SUInf.dwFlags = STARTF_USESTDHANDLES; /* qt->pipeout = StdOut[0]; qt->pipein = StdIn[0]; */ qt->pipeoutih = StdOut[1]; qt->pipeinih = StdIn[1]; if (!DuplicateHandle(GetCurrentProcess(), StdIn[0], GetCurrentProcess(), &qt->pipein, 0, FALSE, // not inherited DUPLICATE_SAME_ACCESS)) qt->pipein = StdIn[0]; else CloseHandle(StdIn[0]); if (!DuplicateHandle(GetCurrentProcess(), StdOut[0], GetCurrentProcess(), &qt->pipeout, 0, FALSE, // not inherited DUPLICATE_SAME_ACCESS)) qt->pipeout = StdOut[0]; else CloseHandle(StdOut[0]); SUInf.hStdInput = qt->pipeinih; SUInf.hStdOutput = qt->pipeoutih; SUInf.hStdError = qt->pipeoutih; //we don't want to have to bother working out which one was written to first. if (!SetStdHandle(STD_OUTPUT_HANDLE, SUInf.hStdOutput)) Con_Printf("Windows sucks\n"); if (!SetStdHandle(STD_ERROR_HANDLE, SUInf.hStdError)) Con_Printf("Windows sucks\n"); if (!SetStdHandle(STD_INPUT_HANDLE, SUInf.hStdInput)) Con_Printf("Windows sucks\n"); printf("Started app\n"); ret = CreateProcess(NULL, command, NULL, NULL, true, CREATE_NO_WINDOW, NULL, NULL, &SUInf, &ProcInfo); qt->process = ProcInfo.hProcess; CloseHandle(ProcInfo.hThread); qt->running = true; qt->console = Con_Create("QTerm", 0); qt->console->redirect = QT_KeyPress; qt->console->close = QT_Close; qt->console->userdata = qt; Con_PrintCon(qt->console, "Started Process\n", PFS_NOMARKUP); Con_SetActive(qt->console); qt->next = qterms; qterms = activeqterm = qt; } void Con_QTerm_f(void) { if(Cmd_IsInsecure()) Con_Printf("Server tried stuffcmding a restricted command: qterm %s\n", Cmd_Args()); else QT_Create(Cmd_Args()); } #endif void Key_ClearTyping (void) { key_lines[edit_line] = BZ_Realloc(key_lines[edit_line], 1); key_lines[edit_line][0] = 0; // clear any typing key_linepos = 0; } void Con_History_Load(void) { char line[8192]; char *cr; vfsfile_t *file = FS_OpenVFS("conhistory.txt", "rb", FS_ROOT); for (edit_line=0 ; edit_line<=CON_EDIT_LINES_MASK ; edit_line++) { key_lines[edit_line] = BZ_Realloc(key_lines[edit_line], 1); key_lines[edit_line][0] = 0; } edit_line = 0; key_linepos = 0; if (file) { while (VFS_GETS(file, line, sizeof(line)-1)) { //strip a trailing \r if its from windows. cr = line + strlen(line); if (cr > line && cr[-1] == '\r') cr[-1] = '\0'; key_lines[edit_line] = BZ_Realloc(key_lines[edit_line], strlen(line)+1); strcpy(key_lines[edit_line], line); edit_line = (edit_line+1) & CON_EDIT_LINES_MASK; } VFS_CLOSE(file); } history_line = edit_line; } void Con_History_Save(void) { vfsfile_t *file; int line; if (!FS_GameIsInitialised()) return; if (!con_savehistory.ival) return; file = FS_OpenVFS("conhistory.txt", "wb", FS_ROOT); if (file) { line = edit_line - CON_EDIT_LINES_MASK; if (line < 0) line = 0; for(; line < edit_line; line++) { VFS_PUTS(file, key_lines[line]); #ifdef _WIN32 //use an \r\n for readability with notepad. VFS_PUTS(file, "\r\n"); #else VFS_PUTS(file, "\n"); #endif } VFS_CLOSE(file); } } /* ================ Con_ToggleConsole_f ================ */ void Con_ToggleConsole_Force(void) { console_t *con = Con_GetMain(); SCR_EndLoadingPlaque(); Key_ClearTyping (); if (con->flags & CONF_ISWINDOW) { if (con_curwindow == con && Key_Dest_Has(kdm_cwindows)) { con_curwindow = NULL; Key_Dest_Remove(kdm_cwindows); } else { con_curwindow = con; Key_Dest_Add(kdm_cwindows); VRUI_SnapAngle(); } } else { if (Key_Dest_Has(kdm_console)) Key_Dest_Remove(kdm_console); else { Key_Dest_Add(kdm_console); VRUI_SnapAngle(); } } } void Con_ToggleConsole_f (void) { extern cvar_t con_stayhidden; Con_GetMain(); if (!con_curwindow) { for (con_curwindow = con_head; con_curwindow; con_curwindow = con_curwindow->next) if (con_curwindow->flags & CONF_ISWINDOW) break; } if (con_curwindow && !Key_Dest_Has(kdm_cwindows|kdm_console)) { Key_Dest_Add(kdm_cwindows); return; } #ifdef CSQC_DAT if (!editormodal && CSQC_ConsoleCommand(-1, "toggleconsole")) { Key_Dest_Remove(kdm_console); return; } #endif if (con_stayhidden.ival >= 3) { Key_Dest_Remove(kdm_cwindows); return; //its hiding! } Con_ToggleConsole_Force(); } void Con_ClearCon(console_t *con) { conline_t *t; while (con->current) { t = con->current; con->current = t->older; Z_Free(t); } con->display = con->current = con->oldest = NULL; con->selstartline = NULL; con->selendline = NULL; /*reset the line pointers, create an active line*/ Con_Finit(con); } /* ================ Con_Clear_f ================ */ void Con_Clear_f (void) { console_t *con = Con_FindConsole(Cmd_Argv(1)); if (!con || Cmd_IsInsecure()) return; Con_ClearCon(con); } void Cmd_ConEchoCenter_f(void) { console_t *con; con = Con_FindConsole(Cmd_Argv(1)); if (!con) con = Con_Create(Cmd_Argv(1), 0); if (con) { Cmd_ShiftArgs(1, false); Con_PrintCon(con, Cmd_Args(), con->parseflags|PFS_NONOTIFY|PFS_CENTERED ); Con_PrintCon(con, "\n", con->parseflags|PFS_NONOTIFY|PFS_CENTERED); } } void Cmd_ConEcho_f(void) { console_t *con; con = Con_FindConsole(Cmd_Argv(1)); if (!con) con = Con_Create(Cmd_Argv(1), 0); if (con) { Cmd_ShiftArgs(1, false); Con_PrintCon(con, Cmd_Args(), con->parseflags); Con_PrintCon(con, "\n", con->parseflags); } } void Cmd_ConClear_f(void) { console_t *con; con = Con_FindConsole(Cmd_Argv(1)); if (con) Con_ClearCon(con); } void Cmd_ConClose_f(void) { console_t *con; con = Con_FindConsole(Cmd_Argv(1)); if (con) Con_Destroy(con); } void Cmd_ConActivate_f(void) { console_t *con; con = Con_FindConsole(Cmd_Argv(1)); if (con) Con_SetActive(con); } /* ================ Con_MessageMode_f ================ */ void Con_MessageMode_f (void) { chat_team = false; Key_Dest_Add(kdm_message); Key_Dest_Remove(kdm_console); } /* ================ Con_MessageMode2_f ================ */ void Con_MessageMode2_f (void) { chat_team = true; Key_Dest_Add(kdm_message); Key_Dest_Remove(kdm_console); } void Con_ForceActiveNow(void) { Key_Dest_Add(kdm_console); scr_con_target = scr_con_current = vid.height; } /* ================ Con_Init ================ */ void Log_Init (void); void Con_Init (void) { con_current = NULL; con_head = NULL; con_main = Con_GetMain(); con_initialized = true; // Con_TPrintf ("Console initialized.\n"); // // register our commands // Cvar_Register (&con_centernotify, "Console controls"); Cvar_Register (&con_notifytime, "Console controls"); Cvar_Register (&con_notify_x, "Console controls"); Cvar_Register (&con_notify_y, "Console controls"); Cvar_Register (&con_notify_w, "Console controls"); Cvar_Register (&con_numnotifylines, "Console controls"); Cvar_Register (&con_displaypossibilities, "Console controls"); Cvar_Register (&con_showcompletion, "Console controls"); Cvar_Register (&cl_chatmode, "Console controls"); Cvar_Register (&con_maxlines, "Console controls"); Cvar_Register (&con_numnotifylines_chat, "Console controls"); Cvar_Register (&con_notifytime_chat, "Console controls"); Cvar_Register (&con_separatechat, "Console controls"); Cvar_Register (&con_timestamps, "Console controls"); Cvar_Register (&con_timeformat, "Console controls"); Cvar_Register (&con_textsize, "Console controls"); Cvar_Register (&con_window, "Console controls"); Cvar_Register (&con_savehistory, "Console controls"); Cvar_ForceCallback(&con_window); Cmd_AddCommand ("toggleconsole", Con_ToggleConsole_f); Cmd_AddCommand ("messagemode", Con_MessageMode_f); Cmd_AddCommand ("messagemode2", Con_MessageMode2_f); Cmd_AddCommand ("clear", Con_Clear_f); #ifdef QTERM Cmd_AddCommand ("qterm", Con_QTerm_f); #endif Cmd_AddCommandD ("conecho_center", Cmd_ConEchoCenter_f, "conecho_center consolename The Text To Echo\nUse \"\" for the main console.\nAny added lines will be aligned to the middle of the console."); Cmd_AddCommandD ("conecho", Cmd_ConEcho_f, "conecho consolename The Text To Echo\nEchos text to a named console instead of just the main one."); Cmd_AddCommandD ("conclear", Cmd_ConClear_f, "Clears a named console (instead of just the main one)"); Cmd_AddCommandD ("conclose", Cmd_ConClose_f, "Destroys a named console"); Cmd_AddCommandD ("conactivate", Cmd_ConActivate_f, "Brings focus to the named console. Will not do anything if the named console is not created yet (so be sure to do any echos before using this command)"); Log_Init(); } void Con_Shutdown(void) { int i; for (i = 0; i <= CON_EDIT_LINES_MASK; i++) { BZ_Free(key_lines[i]); } while(con_head) Con_Destroy(con_head); con_initialized = false; } void TTS_SayConString(conchar_t *stringtosay); /* ================ Con_Print Handles cursor positioning, line wrapping, etc All console printing must go through this in order to be logged to disk If no console is visible, the notify window will pop up. ================ */ //reallocates a line (with its buffer), and updates its links. if shrinking, be sure to reduce the length conline_t *Con_ResizeLineBuffer(console_t *con, conline_t *old, unsigned int length) { conline_t *l; old->maxlength = length & 0xffff; if (old->maxlength < old->length) return NULL; //overflow. l = BZ_Realloc(old, sizeof(*l)+(old->maxlength)*sizeof(conchar_t)); if (l->newer) l->newer->older = l; if (l->older) l->older->newer = l; if (con->selstartline == old) con->selstartline = l; if (con->selendline == old) con->selendline = l; if (con->display == old) con->display = l; if (con->oldest == old) con->oldest = l; if (con->current == old) con->current = l; if (con->footerline == old) con->footerline = l; if (con->userline == old) con->userline = l; if (con->highlightline == old) con->highlightline = old; return l; } qboolean Con_InsertConChars (console_t *con, conline_t *line, int offset, conchar_t *c, int len) { conchar_t *o; if (line->length+len > line->maxlength) { line = Con_ResizeLineBuffer(con, line, line->length+len + 8); if (!line) return false; //overflowed! } o = (conchar_t *)(line+1); if (line->length-offset) memmove(o+offset+len, o+offset, (line->length-offset)*sizeof(conchar_t)); memcpy(o+offset, c, sizeof(*o) * len); line->length+=len; return true; } void Con_PrintCon (console_t *con, const char *txt, unsigned int parseflags) { conchar_t expanded[4096]; conchar_t *c, *n; conline_t *reuse; int maxlines; unsigned flags, codepoint; if (con->maxlines) maxlines = con->maxlines; else maxlines = con_maxlines.ival; COM_ParseFunString(con->defaultcharbits, txt, expanded, sizeof(expanded), parseflags); c = expanded; if (*c) con->unseentext = true; for (;*c; c=n) { n = Font_Decode(c, &flags, &codepoint); if (codepoint=='\r' && !(flags&CON_HIDDEN)) con->cr = true; else if (codepoint=='\n' && !(flags&CON_HIDDEN)) { con->cr = false; reuse = NULL; while (con->linecount >= maxlines) { if (con->oldest == con->current) break; if (con->selstartline == con->oldest) con->selstartline = NULL; if (con->selendline == con->oldest) con->selendline = NULL; if (con->display == con->oldest) con->display = con->oldest->newer; con->oldest = con->oldest->newer; if (reuse) Z_Free(con->oldest->older); else reuse = con->oldest->older; con->oldest->older = NULL; con->linecount--; } con->linecount++; con->current->time = realtime; con->current->flags = 0; if (parseflags & PFS_CENTERED) con->current->flags |= CONL_CENTERED; if (parseflags & PFS_NONOTIFY) con->current->flags |= CONL_NONOTIFY; else if (!Key_Dest_Has(~kdm_game) && (con->flags & CONF_NOTIFY)) VRUI_SnapAngle(); #if defined(HAVE_SPEECHTOTEXT) if (con->current) TTS_SayConString((conchar_t*)(con->current+1)); #endif if (!reuse) { reuse = Z_Malloc(sizeof(conline_t) + sizeof(conchar_t)); reuse->maxlength = 1; } else { reuse->newer = NULL; reuse->older = NULL; } reuse->id = (++con->nextlineid) & 0xffff; reuse->older = con->current; con->current->newer = reuse; con->current = reuse; con->current->length = 0; if (con->display == con->current->older && con->displayscroll==0) con->display = con->current; } else { if (con->cr) { con->current->length = 0; con->cr = false; } if (!con->current->numlines) con->current->numlines = 1; if (!con->current->length && con_timestamps.ival && !(parseflags & PFS_CENTERED)) { char timeasc[64]; conchar_t timecon[64], *timeconend; time_t rawtime; time (&rawtime); strftime(timeasc, sizeof(timeasc), con_timeformat.string, localtime (&rawtime)); timeconend = COM_ParseFunString(con->defaultcharbits, timeasc, timecon, sizeof(timecon), false); Con_InsertConChars(con, con->current, con->current->length, timecon, timeconend-timecon); } //FIXME: don't do this a char at a time Con_InsertConChars(con, con->current, con->current->length, c, n-c); } } con->current->time = realtime; } void Con_CenterPrint(const char *txt) { console_t *c = Con_GetMain(); int flags = c->parseflags|PFS_NONOTIFY|PFS_CENTERED; Con_PrintCon(c, "^Ue01d^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01f\n", flags); Con_PrintCon(c, txt, flags); //client console Con_PrintCon(c, "\n^Ue01d^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01e^Ue01f\n", flags); } void Con_Print (const char *txt) { console_t *c = Con_GetMain(); Con_PrintCon(c, txt, c->parseflags); //client console } void Con_PrintFlags(const char *txt, unsigned int setflags, unsigned int clearflags) { console_t *c = Con_GetMain(); setflags |= c->parseflags; setflags &= ~clearflags; // also echo to debugging console Sys_Printf ("%s", txt); // also echo to debugging console // log all messages to file Con_Log (txt); if (con_initialized) Con_PrintCon(c, txt, setflags); } void Con_CycleConsole(void) { console_t *first = con_current?con_current:con_head; while(1) { con_current = con_current->next; if (!con_current) con_current = con_head; if (con_current == first) { if (con_current->flags & (CONF_HIDDEN|CONF_ISWINDOW)) con_current = NULL; //no valid consoles break; //we wrapped? oh noes } if (con_current->flags & (CONF_HIDDEN|CONF_ISWINDOW)) continue; //this is a valid choice break; } } /* ================ Con_Printf Handles cursor positioning, line wrapping, etc ================ */ #ifdef HAVE_SERVER extern redirect_t sv_redirected; extern char sv_redirected_buf[8000]; void SV_FlushRedirect (void); #endif vfsfile_t *con_pipe; #define MAXPRINTMSG 4096 static void Con_PrintFromThread (void *ctx, void *data, size_t a, size_t b) { Con_Printf("%s", (char*)data); BZ_Free(data); } vfsfile_t *Con_POpen(const char *conname) { if (!conname || !*conname) { if (con_pipe) VFS_CLOSE(con_pipe); con_pipe = VFSPIPE_Open(2, false); return con_pipe; } return NULL; } // FIXME: make a buffer size safe vsprintf? void VARGS Con_Printf (const char *fmt, ...) { va_list argptr; char msg[MAXPRINTMSG]; va_start (argptr,fmt); vsnprintf (msg,sizeof(msg), fmt,argptr); va_end (argptr); if (!Sys_IsMainThread()) { COM_AddWork(WG_MAIN, Con_PrintFromThread, NULL, Z_StrDup(msg), 0, 0); return; } #ifdef HAVE_SERVER // add to redirected message if (sv_redirected) { if (strlen (msg) + strlen(sv_redirected_buf) > sizeof(sv_redirected_buf) - 1) SV_FlushRedirect (); strcat (sv_redirected_buf, msg); return; } #endif // also echo to debugging console Sys_Printf ("%s", msg); // also echo to debugging console // log all messages to file Con_Log (msg); if (con_pipe) VFS_PUTS(con_pipe, msg); if (!con_initialized) return; // write it to the scrollable buffer Con_Print (msg); } void VARGS Con_SafePrintf (const char *fmt, ...) { //obsolete version of the function va_list argptr; char msg[MAXPRINTMSG]; va_start (argptr,fmt); vsnprintf (msg,sizeof(msg)-1, fmt,argptr); va_end (argptr); // write it to the scrollable buffer Con_Printf ("%s", msg); } void VARGS Con_TPrintf (translation_t text, ...) { va_list argptr; char msg[MAXPRINTMSG]; const char *fmt = localtext(text); va_start (argptr,text); vsnprintf (msg,sizeof(msg), fmt,argptr); va_end (argptr); // write it to the scrollable buffer Con_Printf ("%s", msg); } void VARGS Con_SafeTPrintf (translation_t text, ...) { va_list argptr; char msg[MAXPRINTMSG]; const char *fmt = localtext(text); va_start (argptr,text); vsnprintf (msg,sizeof(msg), fmt,argptr); va_end (argptr); // write it to the scrollable buffer Con_Printf ("%s", msg); } static void Con_DPrintFromThread (void *ctx, void *data, size_t a, size_t b) { if (log_developer.ival || !a) Con_Log(data); if (developer.ival >= (int)a) { console_t *c = Con_GetMain(); Sys_Printf ("%s", (const char*)data); // also echo to debugging console Con_PrintCon(c, data, c->parseflags); } BZ_Free(data); } /* ================ Con_DPrintf A Con_Printf that only shows up if the "developer" cvar is set ================ */ void VARGS Con_DPrintf (const char *fmt, ...) { va_list argptr; char msg[MAXPRINTMSG]; #ifdef CRAZYDEBUGGING va_start (argptr,fmt); vsnprintf (msg,sizeof(msg)-1, fmt,argptr); va_end (argptr); Sys_Printf("%s", msg); return; #else if (!developer.ival && !log_developer.ival) return; // early exit #endif va_start (argptr,fmt); vsnprintf (msg,sizeof(msg)-1, fmt,argptr); va_end (argptr); if (!Sys_IsMainThread()) { COM_AddWork(WG_MAIN, Con_DPrintFromThread, NULL, Z_StrDup(msg), 1, 0); return; } if (log_developer.ival) Con_Log(msg); if (developer.ival) { console_t *c = Con_GetMain(); Sys_Printf ("%s", msg); // also echo to debugging console Con_PrintCon(c, msg, c->parseflags); } } void VARGS Con_DLPrintf (int level, const char *fmt, ...) { va_list argptr; char msg[MAXPRINTMSG]; #ifdef CRAZYDEBUGGING va_start (argptr,fmt); vsnprintf (msg,sizeof(msg)-1, fmt,argptr); va_end (argptr); Sys_Printf("%s", msg); return; #else if (developer.ival= level) { Sys_Printf ("%s", msg); // also echo to debugging console if (con_initialized) { console_t *c = Con_GetMain(); Con_PrintCon(c, msg, c->parseflags); } } } void VARGS Con_ThrottlePrintf (float *timer, int developerlevel, const char *fmt, ...) { va_list argptr; char msg[MAXPRINTMSG]; float now = realtime; if (*timer > now) ; //in the future? zomg else if (*timer >= now-1) return; //within the last second *timer = now; //in the future? zomg va_start (argptr,fmt); vsnprintf (msg,sizeof(msg)-1, fmt,argptr); va_end (argptr); if (developerlevel) Con_DLPrintf(developerlevel, "%s", msg); else Con_Printf("%s", msg); } static void Con_FooterMarked(console_t *con, qboolean append, conchar_t *marked, conchar_t *markedend) { int oldlen, newlen; conline_t *newf = NULL, *l; unsigned fl, cp; conchar_t *nl, *n; if (!append) { while(con->footerline) { l = con->footerline; con->footerline = l->older; if (con->selstartline == l) con->selstartline = NULL; if (con->selendline == l) con->selendline = NULL; Z_Free(l); } con->footerline = NULL; } for (append = true; marked < markedend; marked = n, append = false) { n = markedend; for (nl = marked; nl < markedend; nl=n) { n = Font_Decode(nl, &fl, &cp); if (cp == '\n' && !(fl&CONF_HIDDEN)) break; } newlen = nl - marked; if (append && con->footerline) oldlen = con->footerline->length; else oldlen = 0; if (newlen || !append) { newf = Z_Malloc(sizeof(*newf) + (oldlen + newlen) * sizeof(conchar_t)); if (append && con->footerline) { memcpy(newf, con->footerline, sizeof(*con->footerline)+oldlen*sizeof(conchar_t)); Z_Free(con->footerline); } else newf->older = con->footerline; if (newf->older) newf->older->newer = newf; memcpy((conchar_t*)(newf+1)+oldlen, marked, newlen*sizeof(conchar_t)); newf->length = oldlen + newlen; con->footerline = newf; } } } /*description text at the bottom of the console*/ void Con_Footerf(console_t *con, qboolean append, const char *fmt, ...) { va_list argptr; char msg[MAXPRINTMSG]; conchar_t marked[MAXPRINTMSG], *markedend; if (!con) con = con_current; if (!con) return; va_start (argptr,fmt); vsnprintf (msg,sizeof(msg)-1, fmt,argptr); va_end (argptr); markedend = COM_ParseFunString((COLOR_YELLOW << CON_FGSHIFT)|(con->backshader?CON_NONCLEARBG:0), msg, marked, sizeof(marked), false); Con_FooterMarked(con, append, marked, markedend); } /* ============================================================================== DRAWING ============================================================================== */ qboolean COM_InsertIME(conchar_t *buffer, size_t buffersize, conchar_t **cursor, conchar_t **textend) { conchar_t *in = vid.ime_preview; if (in && *in && *textend+vid.ime_previewlen < buffer+buffersize) { memmove(buffer + (*cursor-buffer) + vid.ime_previewlen, *cursor, ((*textend-*cursor)+1)*sizeof(conchar_t)); memcpy(buffer + (*cursor-buffer), in, vid.ime_previewlen*sizeof(conchar_t)); *cursor += vid.ime_caret; *textend += vid.ime_previewlen; return true; } return false; } /* ================ Con_DrawInput The input line scrolls horizontally if typing goes beyond the right edge y is the bottom of the input return value is the top of the region ================ */ int Con_DrawInput (console_t *con, qboolean focused, int left, int right, int y, qboolean selactive, int selsx, int selex, int selsy, int seley) { int i; int lhs, rhs; int p; unsigned char *text, *fname = NULL; extern int con_commandmatch; conchar_t maskedtext[2048]; conchar_t *endmtext; conchar_t *cursor; conchar_t *cchar; conchar_t *textstart; size_t textsize; qboolean cursorframe; unsigned int codeflags, codepoint; int cursorpos; qboolean hidecomplete; int x; if (focused) { vid.ime_allow = true; vid.ime_position[0] = ((float)left/vid.pixelwidth)*vid.width; vid.ime_position[1] = ((float)y/vid.pixelheight)*vid.height; } if (!con->linebuffered || con->linebuffered == Con_Navigate) { if (con->footerline) { y = Con_DrawConsoleLines(con, con->footerline, 0, left, right, y, 0, selactive, selsx, selex, selsy, seley, 0); } return y; //fixme: draw any unfinished lines of the current console instead. } y -= Font_CharHeight(); if (!focused) return y; // don't draw anything (always draw if not active) text = key_lines[edit_line]; cursorpos = key_linepos; //copy it to an alternate buffer and fill in text colouration escape codes. //if it's recognised as a command, colour it yellow. //if it's not a command, and the cursor is at the end of the line, leave it as is, // but add to the end to show what the compleation will be. textstart = COM_ParseFunString(CON_WHITEMASK, con->prompt, maskedtext, sizeof(maskedtext) - sizeof(maskedtext[0]), PFS_FORCEUTF8); textsize = (countof(maskedtext) - (textstart-maskedtext) - 1) * sizeof(maskedtext[0]); i = text[cursorpos]; text[cursorpos] = 0; cursor = COM_ParseFunString(CON_WHITEMASK, text, textstart, textsize, PFS_KEEPMARKUP | PFS_FORCEUTF8); //okay, so that's where the cursor is. heal the input string and reparse (so we don't mess up escapes) text[cursorpos] = i; endmtext = COM_ParseFunString(CON_WHITEMASK, text, textstart, textsize, PFS_KEEPMARKUP | PFS_FORCEUTF8); // endmtext = COM_ParseFunString(CON_WHITEMASK, text+key_linepos, cursor, ((char*)maskedtext)+sizeof(maskedtext) - (char*)(cursor+1), PFS_KEEPMARKUP | PFS_FORCEUTF8); hidecomplete = COM_InsertIME(maskedtext, countof(maskedtext), &cursor, &endmtext); /* if (cursorpos == strlen(text) && vid.ime_preview) { endmtext = COM_ParseFunString(COLOR_MAGENTA<