/* server/console.cc This file is part of the Osirion project and is distributed under the terms and conditions of the GNU General Public License version 2 */ #include #include "server/console.h" #include "core/core.h" #include "sys/consoleinterface.h" #ifdef HAVE_CURSES #include #endif namespace server { bool console_initialized = false; bool console_updated = false; Console server_console; #ifdef HAVE_CURSES WINDOW *stdwin; #endif Console *console() { return (&server_console); } void Console::init() { #ifdef HAVE_CURSES stdwin = initscr(); // initialize the ncurses window cbreak(); // disable input line buffering noecho(); // don't show typed characters keypad(stdwin, TRUE); // enable special keys nodelay(stdwin, TRUE); // non-blocking input curs_set(0); // disable cursor console_initialized = true; console_updated = true; #endif // HAVE_CURSES con_print << "Initializing console..." << std::endl; #ifdef HAVE_CURSES server_console.console_lastrefresh = 1; server_console.draw(); #endif // HAVE_CURSES } void Console::shutdown() { con_print << "Shutting down console..." << std::endl; #ifdef HAVE_CURSES server_console.draw(); endwin(); console_initialized = false; #endif } #ifdef HAVE_CURSES void Console::resize() { if (!console_initialized) return; endwin(); refresh(); console_updated = true; } void Console::flush() { char line[MAXCMDSIZE]; while(consoleinterface_buffer.getline(line, MAXCMDSIZE-1)) { while (consoleinterface_text.size() >= sys::MAXCONLINES) { consoleinterface_text.pop_front(); } consoleinterface_text.push_back(std::string(line)); console_updated = true; } consoleinterface_buffer.clear(); } void Console::draw_background() { bkgdset(' '); clear(); // draw version string std::string versionstr("The Osirion Project "); versionstr.append(core::version()); mvaddstr(0, stdwin->_maxx - 1 - versionstr.size(), versionstr.c_str()); } void Console::draw_text() { int w = stdwin->_maxx; int h = stdwin->_maxy; if ((w < 3) || (h < 3)) return; int y = h-1; // draw console text std::deque::reverse_iterator rit = consoleinterface_text.rbegin(); while (rit != consoleinterface_text.rend() && y > 0) { mvaddnstr(y, 0, (*rit).c_str(), w); ++rit; y--; } } void Console::draw() { flush(); if (console_lastrefresh < 0.1) { return; } if (console_initialized && console_updated && stdwin->_maxx && stdwin->_maxy) { draw_background(); draw_text(); wrefresh(stdwin); console_updated = false; console_lastrefresh = 0; } } void Console::frame(float seconds) { if (!console_initialized) return; console_lastrefresh += seconds; int key = wgetch(stdwin); while (key != ERR) { key = wgetch(stdwin); } draw(); } #endif // HAVE_CURSES }