Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: ea82dff09ef7fb7267241e391db8eeba8f0d03ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
   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 <iostream>

#include "server/console.h"
#include "core/core.h"
#include "sys/consoleinterface.h"

#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

}