From 89de2efebc22b3754c18ede10dc07bfc397fc2d0 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Wed, 2 Jul 2008 19:18:44 +0000 Subject: initial server ncurses console --- src/server/console.cc | 132 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 131 insertions(+), 1 deletion(-) (limited to 'src/server/console.cc') diff --git a/src/server/console.cc b/src/server/console.cc index 4b7992e..ea82dff 100644 --- a/src/server/console.cc +++ b/src/server/console.cc @@ -4,28 +4,158 @@ 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" -#include +#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 } -- cgit v1.2.3