void Default_CmdFunction(ConsoleInformation * console, char *command) { CON_Out(console, " No CommandFunction registered"); CON_Out(console, " use 'CON_SetExecuteFunction' to register one"); CON_Out(console, " "); CON_Out(console, "Unknown Command \"%s\"", command); }
char *Default_TabFunction(char *command) { CON_Out(Topmost, " No TabFunction registered"); CON_Out(Topmost, " use 'CON_SetTabCompletion' to register one"); CON_Out(Topmost, " "); return NULL; }
void Console::ExecuteCommand(const std::string& cmd, const std::vector<std::string>& args) { const auto it = mCmdCallbacks.find(cmd); if (it == mCmdCallbacks.end()) { CON_Out(mSdlConsole, (std::string("Unknown command: \"") + cmd + "\"").c_str()); return; } it->second(*this, cmd, args); }
/* Adds background image to the console, x and y based on consoles x and y */ int CON_Background(ConsoleInformation * console, const char *image, int x, int y) { SDL_Surface *temp; SDL_Rect backgroundsrc, backgrounddest; if (!console) return 1; /* Free the background from the console */ if (image == NULL) { if (console->BackgroundImage == NULL) SDL_FreeSurface(console->BackgroundImage); console->BackgroundImage = NULL; SDL_FillRect(console->InputBackground, NULL, SDL_MapRGBA(console->ConsoleSurface->format, 0, 0, 0, SDL_ALPHA_OPAQUE)); return 0; } /* Load a new background */ #ifdef HAVE_SDLIMAGE temp = IMG_Load(image); #else temp = SDL_LoadBMP(image); #endif if (!temp) { CON_Out(console, "Cannot load background %s.", image); return 1; } console->BackgroundImage = SDL_DisplayFormat(temp); SDL_FreeSurface(temp); console->BackX = x; console->BackY = y; backgroundsrc.x = 0; backgroundsrc.y = console->ConsoleSurface->h - console->FontHeight - console->BackY; backgroundsrc.w = console->BackgroundImage->w; backgroundsrc.h = console->InputBackground->h; backgrounddest.x = console->BackX; backgrounddest.y = 0; backgrounddest.w = console->BackgroundImage->w; backgrounddest.h = console->FontHeight; SDL_FillRect(console->InputBackground, NULL, SDL_MapRGBA(console->ConsoleSurface->format, 0, 0, 0, SDL_ALPHA_OPAQUE)); SDL_BlitSurface(console->BackgroundImage, &backgroundsrc, console->InputBackground, &backgrounddest); CON_UpdateConsole(console); return 0; }
/* Sets the Prompt for console */ void CON_SetPrompt(ConsoleInformation *console, char* newprompt) { if(!console) return; /* check length so we can still see at least 1 char :-) */ if(strlen(newprompt) < console->VChars) console->Prompt = strdup(newprompt); else CON_Out(console, "prompt too long. (max. %i chars)", console->VChars - 1); }
void _CDECL_ con_printf(int priority, const char *fmt, ...) { va_list arglist; if (!(fmt && *fmt)) return; if (priority <= ((int)con_threshold.value)) { va_start (arglist, fmt); vsprintf (buffer, fmt, arglist); va_end (arglist); if (fErr) { fprintf(fErr, buffer); fflush(fErr); } #ifdef CONSOLE if (con_initialized) { CON_Out(Console, buffer); atexit(con_free); } #endif if (text_console_enabled) { /* Produce a sanitised version and send it to the console */ char *p1, *p2; p1 = p2 = buffer; do { switch (*p1) { case CC_COLOR: case CC_LSPACING: p1++; case CC_UNDERLINE: p1++; break; default: *p2++ = *p1++; } } while (*p1); *p2 = 0; if (priority == CON_NORMAL) printf(buffer); } } }
/* Takes keys from the keyboard and inputs them to the console If the event was not handled (i.e. WM events or unknown ctrl-shift sequences) the function returns the event for further processing. */ SDL_Event* CON_Events(SDL_Event *event) { if(Topmost == NULL) return event; if(!CON_isVisible(Topmost)) return event; if(event->type == SDL_KEYDOWN) { if(event->key.keysym.mod & KMOD_CTRL) { /* CTRL pressed */ switch(event->key.keysym.sym) { case SDLK_a: Cursor_Home(Topmost); break; case SDLK_e: Cursor_End(Topmost); break; case SDLK_c: Clear_Command(Topmost); break; case SDLK_l: Clear_History(Topmost); CON_UpdateConsole(Topmost); break; default: return event; } } else if(event->key.keysym.mod & KMOD_ALT) { /* the console does not handle ALT combinations! */ return event; } else { /* first of all, check if the console hide key was pressed */ if(event->key.keysym.sym == Topmost->HideKey) { CON_Hide(Topmost); return NULL; } switch (event->key.keysym.sym) { case SDLK_HOME: if(event->key.keysym.mod & KMOD_SHIFT) { Topmost->ConsoleScrollBack = Topmost->LineBuffer-1; CON_UpdateConsole(Topmost); } else { Cursor_Home(Topmost); } break; case SDLK_END: if(event->key.keysym.mod & KMOD_SHIFT) { Topmost->ConsoleScrollBack = 0; CON_UpdateConsole(Topmost); } else { Cursor_End(Topmost); } break; case SDLK_PAGEUP: Topmost->ConsoleScrollBack += CON_LINE_SCROLL; if(Topmost->ConsoleScrollBack > Topmost->LineBuffer-1) Topmost->ConsoleScrollBack = Topmost->LineBuffer-1; CON_UpdateConsole(Topmost); break; case SDLK_PAGEDOWN: Topmost->ConsoleScrollBack -= CON_LINE_SCROLL; if(Topmost->ConsoleScrollBack < 0) Topmost->ConsoleScrollBack = 0; CON_UpdateConsole(Topmost); break; case SDLK_UP: Command_Up(Topmost); break; case SDLK_DOWN: Command_Down(Topmost); break; case SDLK_LEFT: Cursor_Left(Topmost); break; case SDLK_RIGHT: Cursor_Right(Topmost); break; case SDLK_BACKSPACE: Cursor_BSpace(Topmost); break; case SDLK_DELETE: Cursor_Del(Topmost); break; case SDLK_INSERT: Topmost->InsMode = 1-Topmost->InsMode; break; case SDLK_TAB: CON_TabCompletion(Topmost); break; case SDLK_RETURN: if(strlen(Topmost->Command) > 0) { CON_NewLineCommand(Topmost); /* copy the input into the past commands strings */ strcpy(Topmost->CommandLines[0], Topmost->Command); /* display the command including the prompt */ CON_Out(Topmost, "%s%s", Topmost->Prompt, Topmost->Command); CON_Execute(Topmost, Topmost->Command); /* printf("Command: %s\n", Topmost->Command); */ Clear_Command(Topmost); Topmost->CommandScrollBack = -1; } break; case SDLK_ESCAPE: /* deactivate Console */ CON_Hide(Topmost); return NULL; default: if(Topmost->InsMode) Cursor_Add(Topmost, event); else { Cursor_Add(Topmost, event); Cursor_Del(Topmost); } } } return NULL; } return event; }
/* Initializes the console */ ConsoleInformation *CON_Init(SDL_Surface *Surface, SDL_Surface *DisplayScreen, int lines, SDL_Rect rect) { int loop; SDL_Surface *Temp; ConsoleInformation *newinfo; /* Create a new console struct and init it. */ if((newinfo = (ConsoleInformation *) malloc(sizeof(ConsoleInformation))) == NULL) { PRINT_ERROR("Could not allocate the space for a new console info struct.\n"); return NULL; } newinfo->Visible = CON_CLOSED; newinfo->WasUnicode = 0; newinfo->RaiseOffset = 0; newinfo->ConsoleLines = NULL; newinfo->CommandLines = NULL; newinfo->TotalConsoleLines = 0; newinfo->ConsoleScrollBack = 0; newinfo->TotalCommands = 0; newinfo->BackgroundImage = NULL; newinfo->ConsoleAlpha = SDL_ALPHA_OPAQUE; newinfo->Offset = 0; newinfo->InsMode = 1; newinfo->CursorPos = 0; newinfo->CommandScrollBack = 0; newinfo->OutputScreen = DisplayScreen; newinfo->Prompt = CON_DEFAULT_PROMPT; newinfo->HideKey = CON_DEFAULT_HIDEKEY; CON_SetExecuteFunction(newinfo, Default_CmdFunction); CON_SetTabCompletion(newinfo, Default_TabFunction); /* Load the consoles font */ if (-1 == (newinfo->FontNumber = DT_LoadFontFromSurface(Surface, TRANS_FONT, DisplayScreen))) { PRINT_ERROR("Could not load the font "); PRINT_ERROR("for the console!"); return NULL; } newinfo->FontHeight = DT_FontHeight(newinfo->FontNumber); newinfo->FontWidth = DT_FontWidth(newinfo->FontNumber); /* make sure that the size of the console is valid */ if(rect.w > newinfo->OutputScreen->w || rect.w < newinfo->FontWidth * 32) rect.w = newinfo->OutputScreen->w; if(rect.h > newinfo->OutputScreen->h || rect.h < newinfo->FontHeight) rect.h = newinfo->OutputScreen->h; if(rect.x < 0 || rect.x > newinfo->OutputScreen->w - rect.w) newinfo->DispX = 0; else newinfo->DispX = rect.x; if(rect.y < 0 || rect.y > newinfo->OutputScreen->h - rect.h) newinfo->DispY = 0; else newinfo->DispY = rect.y; /* load the console surface */ Temp = SDL_CreateRGBSurface(SDL_SWSURFACE, rect.w, rect.h, newinfo->OutputScreen->format->BitsPerPixel, 0, 0, 0, 0); if(Temp == NULL) { PRINT_ERROR("Couldn't create the ConsoleSurface\n"); return NULL; } newinfo->ConsoleSurface = SDL_ConvertSurfaceFormat(Temp, DisplayScreen->format->format, DisplayScreen->flags); SDL_FreeSurface(Temp); SDL_FillRect(newinfo->ConsoleSurface, NULL, SDL_MapRGBA(newinfo->ConsoleSurface->format, 0, 0, 0, newinfo->ConsoleAlpha)); /* Load the dirty rectangle for user input */ Temp = SDL_CreateRGBSurface(SDL_SWSURFACE, rect.w, newinfo->FontHeight, newinfo->OutputScreen->format->BitsPerPixel, 0, 0, 0, SDL_ALPHA_OPAQUE); if(Temp == NULL) { PRINT_ERROR("Couldn't create the InputBackground\n"); return NULL; } newinfo->InputBackground = SDL_ConvertSurfaceFormat(Temp, DisplayScreen->format->format, DisplayScreen->flags); SDL_FreeSurface(Temp); SDL_FillRect(newinfo->InputBackground, NULL, SDL_MapRGBA(newinfo->ConsoleSurface->format, 0, 0, 0, SDL_ALPHA_OPAQUE)); /* calculate the number of visible characters in the command line */ newinfo->VChars = (rect.w - CON_CHAR_BORDER) / newinfo->FontWidth; if(newinfo->VChars > CON_CHARS_PER_LINE) newinfo->VChars = CON_CHARS_PER_LINE; /* deprecated! Memory errors disabled by C.Wacha :-) We would like to have a minumum # of lines to guarentee we don't create a memory error */ /* if(rect.h / newinfo->FontHeight > lines) newinfo->LineBuffer = rect.h / newinfo->FontHeight; else newinfo->LineBuffer = lines; */ newinfo->LineBuffer = lines; newinfo->ConsoleLines = (char **)malloc(sizeof(char *) * newinfo->LineBuffer); newinfo->CommandLines = (char **)malloc(sizeof(char *) * newinfo->LineBuffer); for(loop = 0; loop <= newinfo->LineBuffer - 1; loop++) { newinfo->ConsoleLines[loop] = (char *)calloc(CON_CHARS_PER_LINE+1, sizeof(char)); newinfo->CommandLines[loop] = (char *)calloc(CON_CHARS_PER_LINE+1, sizeof(char)); } memset(newinfo->Command, 0, CON_CHARS_PER_LINE+1); memset(newinfo->LCommand, 0, CON_CHARS_PER_LINE+1); memset(newinfo->RCommand, 0, CON_CHARS_PER_LINE+1); memset(newinfo->VCommand, 0, CON_CHARS_PER_LINE+1); CON_Out(newinfo, "Console initialised."); CON_NewLineConsole(newinfo); return newinfo; }
/* Takes keys from the keyboard and inputs them to the console If the event was not handled (i.e. WM events or unknown ctrl-shift sequences) the function returns the event for further processing. */ SDL_Event *CON_Events(SDL_Event * event) { if (Topmost == NULL) return event; if (!CON_isVisible(Topmost)) return event; if (event->type == SDL_KEYDOWN) { if (event->key.keysym.mod & KMOD_CTRL) { /* CTRL pressed */ /* kps - please modify this to work like in talk.c */ switch (event->key.keysym.sym) { case SDLK_a: Cursor_Home(Topmost); break; case SDLK_b: Cursor_Left(Topmost); break; case SDLK_f: Cursor_Right(Topmost); break; case SDLK_e: Cursor_End(Topmost); break; /* * kps - Ctrl-k should really just clear from current * cursor position to end of line. */ case SDLK_k: case SDLK_u: Clear_Command(Topmost); break; case SDLK_l: Clear_History(Topmost); CON_UpdateConsole(Topmost); break; default: return event; } #if 0 } else if (event->key.keysym.mod & KMOD_ALT) { /* the console does not handle ALT combinations! */ return event; #endif } else { switch (event->key.keysym.sym) { case SDLK_HOME: if (event->key.keysym.mod & KMOD_SHIFT) { Topmost->ConsoleScrollBack = Topmost->LineBuffer - 1; CON_UpdateConsole(Topmost); } else { Cursor_Home(Topmost); } break; case SDLK_END: if (event->key.keysym.mod & KMOD_SHIFT) { Topmost->ConsoleScrollBack = 0; CON_UpdateConsole(Topmost); } else { Cursor_End(Topmost); } break; case SDLK_PAGEUP: Topmost->ConsoleScrollBack += CON_LINE_SCROLL; if (Topmost->ConsoleScrollBack > Topmost->LineBuffer - 1) Topmost->ConsoleScrollBack = Topmost->LineBuffer - 1; CON_UpdateConsole(Topmost); break; case SDLK_PAGEDOWN: Topmost->ConsoleScrollBack -= CON_LINE_SCROLL; if (Topmost->ConsoleScrollBack < 0) Topmost->ConsoleScrollBack = 0; CON_UpdateConsole(Topmost); break; case SDLK_UP: Command_Up(Topmost); break; case SDLK_DOWN: Command_Down(Topmost); break; case SDLK_LEFT: Cursor_Left(Topmost); break; case SDLK_RIGHT: Cursor_Right(Topmost); break; case SDLK_BACKSPACE: Cursor_BSpace(Topmost); break; case SDLK_DELETE: Cursor_Del(Topmost); break; case SDLK_INSERT: Topmost->InsMode = 1 - Topmost->InsMode; break; case SDLK_TAB: CON_TabCompletion(Topmost); break; case SDLK_RETURN: if (strlen(Topmost->Command) > 0) { CON_NewLineCommand(Topmost); /* copy the input into the past commands strings */ strcpy(Topmost->CommandLines[0], Topmost->Command); /* display the command including the prompt */ CON_Out(Topmost, "%s%s", Topmost->Prompt, Topmost->Command); CON_Execute(Topmost, Topmost->Command); /* printf("Command: %s\n", Topmost->Command); */ Clear_Command(Topmost); Topmost->CommandScrollBack = -1; } else /* deactivate Console if return is pressed on empty line */ /* was: CON_Hide(Topmost); */ Talk_set_state(false); break; case SDLK_ESCAPE: if (strlen(Topmost->Command) > 0) { CON_NewLineCommand(Topmost); /* copy the input into the past commands strings */ strcpy(Topmost->CommandLines[0], Topmost->Command); Clear_Command(Topmost); Topmost->CommandScrollBack = -1; } /* deactivate Console */ /*was: CON_Hide(Topmost);*/ Talk_set_state(false); return NULL; default: if (Topmost->InsMode) Cursor_Add(Topmost, event); else { Cursor_Add(Topmost, event); Cursor_Del(Topmost); } } } return NULL; } return event; }
void Console::Print(const std::string& msg) { CON_Out(mSdlConsole, msg.c_str()); }
/* Takes keys from the keyboard and inputs them to the console If the event was not handled (i.e. WM events or unknown ctrl-shift sequences) the function returns the event for further processing. */ int CON_Events(int event) { if(Topmost == NULL) return event; if(!CON_isVisible(Topmost)) return event; if(event & KEY_CTRLED) { //CTRL pressed switch(event & ~KEY_CTRLED) { case KEY_A: Cursor_Home(Topmost); break; case KEY_E: Cursor_End(Topmost); break; case KEY_C: Clear_Command(Topmost); break; case KEY_L: Clear_History(Topmost); CON_UpdateConsole(Topmost); break; default: return event; } } else if(event & KEY_ALTED) { //the console does not handle ALT combinations! return event; } else { //first of all, check if the console hide key was pressed if(event == Topmost->HideKey) { CON_Hide(Topmost); return 0; } switch (event & 0xff) { case KEY_LSHIFT: case KEY_RSHIFT: return event; case KEY_HOME: if(event & KEY_SHIFTED) { Topmost->ConsoleScrollBack = Topmost->LineBuffer-1; CON_UpdateConsole(Topmost); } else { Cursor_Home(Topmost); } break; case KEY_END: if(event & KEY_SHIFTED) { Topmost->ConsoleScrollBack = 0; CON_UpdateConsole(Topmost); } else { Cursor_End(Topmost); } break; case KEY_PAGEUP: Topmost->ConsoleScrollBack += CON_LINE_SCROLL; if(Topmost->ConsoleScrollBack > Topmost->LineBuffer-1) Topmost->ConsoleScrollBack = Topmost->LineBuffer-1; CON_UpdateConsole(Topmost); break; case KEY_PAGEDOWN: Topmost->ConsoleScrollBack -= CON_LINE_SCROLL; if(Topmost->ConsoleScrollBack < 0) Topmost->ConsoleScrollBack = 0; CON_UpdateConsole(Topmost); break; case KEY_UP: Command_Up(Topmost); break; case KEY_DOWN: Command_Down(Topmost); break; case KEY_LEFT: Cursor_Left(Topmost); break; case KEY_RIGHT: Cursor_Right(Topmost); break; case KEY_BACKSP: Cursor_BSpace(Topmost); break; case KEY_DELETE: Cursor_Del(Topmost); break; case KEY_INSERT: Topmost->InsMode = 1-Topmost->InsMode; break; case KEY_TAB: CON_TabCompletion(Topmost); break; case KEY_ENTER: if(strlen(Topmost->Command) > 0) { CON_NewLineCommand(Topmost); // copy the input into the past commands strings strcpy(Topmost->CommandLines[0], Topmost->Command); // display the command including the prompt CON_Out(Topmost, "%s%s", Topmost->Prompt, Topmost->Command); CON_UpdateConsole(Topmost); CON_Execute(Topmost, Topmost->Command); //printf("Command: %s\n", Topmost->Command); Clear_Command(Topmost); Topmost->CommandScrollBack = -1; } break; case KEY_ESC: //deactivate Console if (event & KEY_SHIFTED) { CON_Hide(Topmost); return 0; } break; default: if(Topmost->InsMode) Cursor_Add(Topmost, event); else { Cursor_Add(Topmost, event); Cursor_Del(Topmost); } } } return 0; }