diff options
Diffstat (limited to 'src/server')
-rw-r--r-- | src/server/console.cc | 132 | ||||
-rw-r--r-- | src/server/console.h | 22 | ||||
-rw-r--r-- | src/server/server.cc | 15 |
3 files changed, 157 insertions, 12 deletions
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 <iostream> + #include "server/console.h" #include "core/core.h" +#include "sys/consoleinterface.h" -#include <iostream> +#ifdef HAVE_CURSES +#include <ncurses.h> +#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<std::string>::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 } diff --git a/src/server/console.h b/src/server/console.h index d894bf1..6a42ba5 100644 --- a/src/server/console.h +++ b/src/server/console.h @@ -13,9 +13,29 @@ namespace server { class Console : public sys::ConsoleInterface { public: + /// initialize the server console static void init(); - + /// shutdown the server console static void shutdown(); + +#ifdef HAVE_CURSES + /// flush buffered messages + virtual void flush(); + /// resize the console + virtual void resize(); + /// run one console frame + void frame(float seconds); +protected: + /// draw the ncurses console + void draw(); + /// clear and draw background (ncurses) + void draw_background(); + /// draw the console text (ncurses) + void draw_text(); + +private: + float console_lastrefresh; +#endif }; Console *console(); diff --git a/src/server/server.cc b/src/server/server.cc index 27cdf9b..fce2181 100644 --- a/src/server/server.cc +++ b/src/server/server.cc @@ -83,17 +83,11 @@ void Server::run() while(connected()) { timer.mark(); frame(elapsed); +#ifdef HAVE_CURSES + console()->frame(elapsed); +#endif elapsed = timer.elapsed(); - - /* - if (elapsed < server_framerate) { - sys::sleep(server_framerate - elapsed); - - elapsed = server_framerate; - } - */ } - } void Server::shutdown() @@ -113,9 +107,10 @@ void Server::shutdown() con_debug << " bytes sent " << std::setfill(' ') << std::setw(6) << core::Stats::network_bytes_sent / 1024 << " Kb" << std::endl; con_debug << " bytes received " << std::setw(6) << core::Stats::network_bytes_received / 1024 << " Kb" << std::endl; con_debug << " compression " << std::setw(6) << ratio << " %" << std::endl; - Console::shutdown(); core::Application::shutdown(); + + Console::shutdown(); quit(0); } |