erebus

view src/console.cc @ 40:9d6368850fe1

minor enhancements and bugfixes to the console stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 10 Jun 2014 10:53:19 +0300
parents 5e27c85e79ca
children 2e817711d0f6
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdarg.h>
5 #include <assert.h>
6 #include <algorithm>
7 #include "opengl.h"
8 #include "console.h"
10 Console::Console()
11 {
12 visible = false;
13 nlines = 16;
14 ncolumns = 80;
16 inpq_front = inpq_back = 0;
18 max_hist_lines = 64;
19 max_output_lines = 128;
21 echo = true;
22 font = 0;
23 font_size = 0;
25 cursor = 0;
26 }
28 void Console::set_font(dtx_font *font, int sz)
29 {
30 this->font = font;
31 font_size = sz;
32 }
34 void Console::set_history_size(int hsz)
35 {
36 max_hist_lines = hsz;
38 int drop = hist.size() - hsz;
39 if(drop > 0) {
40 auto it = hist.begin();
41 for(int i=0; i<drop; i++) {
42 ++it;
43 }
44 hist.erase(hist.begin(), it);
45 }
46 }
48 void Console::set_output_buffer_size(int sz)
49 {
50 max_output_lines = sz;
52 int drop = output.size() - sz;
53 if(drop > 0) {
54 output.erase(output.begin(), output.begin() + drop);
55 }
56 }
58 void Console::set_command_func(std::function<void (const char*)> func)
59 {
60 cmd_handler = func;
61 }
63 void Console::set_echo(bool echo)
64 {
65 this->echo = echo;
66 }
68 bool Console::get_echo() const
69 {
70 return echo;
71 }
73 void Console::set_visible(bool v)
74 {
75 visible = v;
76 }
78 bool Console::is_visible() const
79 {
80 return visible;
81 }
83 void Console::set_size(int lines, int columns)
84 {
85 nlines = lines;
86 ncolumns = columns;
87 }
89 int Console::get_size_lines() const
90 {
91 return nlines;
92 }
94 int Console::get_size_columns() const
95 {
96 return ncolumns;
97 }
99 bool Console::update()
100 {
101 bool must_redraw = false;
103 while(inpq_front != inpq_back) {
104 int c = keybuf[inpq_front];
105 inpq_front = (inpq_front + 1) % INPUTQ_SIZE;
107 switch(c) {
108 case '\n':
109 case '\r':
110 if(!input.empty() && cmd_handler) {
111 cmd_handler(input.c_str());
112 }
114 if(echo) {
115 puts(input.c_str());
116 putchar('\n');
117 }
119 hist.push_back(std::move(input)); // move the input string into the history buffer
120 if((int)hist.size() > max_hist_lines) {
121 hist.pop_front();
122 }
123 cursor = 0;
124 must_redraw = true;
125 break;
127 case '\t':
128 // TODO: completion
129 break;
131 case '\b':
132 if(!input.empty()) {
133 if(cursor == (int)input.size()) {
134 input.pop_back();
135 --cursor;
136 must_redraw = true;
137 } else if(cursor > 0) {
138 input.erase(cursor - 1, 1);
139 --cursor;
140 must_redraw = true;
141 }
142 }
143 break;
145 case KEY_LEFT:
146 if(cursor > 0) {
147 --cursor;
148 must_redraw = true;
149 }
150 break;
152 case KEY_RIGHT:
153 if(cursor < (int)input.size()) {
154 ++cursor;
155 must_redraw = true;
156 }
157 break;
159 case KEY_HOME:
160 cursor = 0;
161 must_redraw = true;
162 break;
164 case KEY_END:
165 cursor = input.size();
166 must_redraw = true;
167 break;
169 case KEY_PGUP:
170 case KEY_PGDOWN:
171 // TODO history
172 break;
174 default:
175 if(c < 256 && isprint(c)) {
176 if(cursor == (int)input.size()) {
177 input.push_back(c);
178 } else {
179 input.insert(cursor, 1, c);
180 }
181 ++cursor;
182 must_redraw = true;
183 }
184 }
185 }
186 return must_redraw;
187 }
189 void Console::draw() const
190 {
191 if(!font || !visible) return;
192 dtx_use_font(font, font_size);
194 int vp[4];
195 glGetIntegerv(GL_VIEWPORT, vp);
197 glMatrixMode(GL_PROJECTION);
198 glPushMatrix();
199 glLoadIdentity();
200 glOrtho(vp[0], vp[0] + vp[2], vp[1], vp[1] + vp[3], -1, 1);
202 glMatrixMode(GL_MODELVIEW);
203 glPushMatrix();
204 glLoadIdentity();
205 glTranslatef(0, vp[3] - vp[1], 0);
207 std::string buflines;
209 int num_lines = std::min(nlines, (int)output.size());
210 auto it = output.cbegin() + (output.size() - num_lines);
212 float max_width = dtx_glyph_width('Q') * ncolumns;
214 while(it != output.cend()) {
215 const std::string &line = *it++;
216 buflines += line + std::string("\n");
218 max_width = std::max(max_width, dtx_string_width(line.c_str()));
219 }
221 // draw the output box
222 float line_height = dtx_line_height();
223 float outbox_height = nlines * line_height;
225 glPushAttrib(GL_ENABLE_BIT);
226 glEnable(GL_BLEND);
227 glDisable(GL_TEXTURE_2D);
228 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
230 glBegin(GL_QUADS);
231 glColor4f(0.1, 0.2, 0.5, 0.7);
232 glVertex2f(0, -outbox_height);
233 glVertex2f(max_width, -outbox_height);
234 glVertex2f(max_width, 0);
235 glVertex2f(0, 0);
237 glColor4f(0.5, 0.2, 0.1, 0.7);
238 glVertex2f(0, -(outbox_height + line_height * 1.5));
239 glVertex2f(max_width, -(outbox_height + line_height * 1.5));
240 glVertex2f(max_width, -outbox_height);
241 glVertex2f(0, -outbox_height);
242 glEnd();
244 // draw the output text
245 glPushMatrix();
246 glTranslatef(0, -line_height, 0);
247 glColor4f(1, 1, 1, 0.7);
248 dtx_string(buflines.c_str());
249 glPopMatrix();
251 // draw the input line
252 glTranslatef(0, -outbox_height - line_height, 0);
254 std::string inpline = std::string("> ") + input;
255 dtx_string(inpline.c_str());
256 glDisable(GL_TEXTURE_2D);
258 float cursor_x = dtx_char_pos(inpline.c_str(), cursor + 2);
259 glBegin(GL_QUADS);
260 glColor4f(1, 1, 1, 0.7);
261 glVertex2f(cursor_x, 0);
262 glVertex2f(cursor_x + 2, 0);
263 glVertex2f(cursor_x + 2, line_height - 2);
264 glVertex2f(cursor_x, line_height - 2);
265 glEnd();
267 glPopAttrib();
269 glMatrixMode(GL_PROJECTION);
270 glPopMatrix();
271 glMatrixMode(GL_MODELVIEW);
272 glPopMatrix();
273 }
275 void Console::input_key(int key)
276 {
277 keybuf[inpq_back] = key;
278 inpq_back = (inpq_back + 1) % INPUTQ_SIZE;
279 }
281 void Console::putchar(char c)
282 {
283 if(output.empty()) {
284 output.push_back(std::string(""));
285 }
287 if(c == '\n' || c == '\r') {
288 output.push_back(std::string(""));
289 if((int)output.size() > max_output_lines) {
290 output.erase(output.begin());
291 }
292 } else {
293 output.back().push_back(c);
294 }
295 }
297 void Console::puts(const char *str)
298 {
299 while(*str) {
300 putchar(*str++);
301 }
302 }
304 void Console::printf(const char *fmt, ...)
305 {
306 static char buf[1024];
307 va_list ap;
309 va_start(ap, fmt);
310 vsnprintf(buf, sizeof buf, fmt, ap);
311 va_end(ap);
313 puts(buf);
314 }
316 void Console::debug()
317 {
318 fprintf(stderr, "visible: %s\n", visible ? "true" : "false");
319 fprintf(stderr, "size: %d lines x %d columns\n", nlines, ncolumns);
320 fprintf(stderr, "cursor: %d\n", cursor);
322 int qsize = 0;
323 int qidx = inpq_front;
324 while(qidx != inpq_back) {
325 qsize++;
326 qidx = (qidx + 1) % INPUTQ_SIZE;
327 }
329 fprintf(stderr, "input queue size: %d\n", qsize);
330 fprintf(stderr, "current input line: \"%s\"\n", input.c_str());
331 fprintf(stderr, "%d saved inputs in the history (max %d)\n", (int)hist.size(), max_hist_lines);
332 auto it = hist.begin();
333 int idx = 0;
334 while(it != hist.end()) {
335 fprintf(stderr, " h(%d): \"%s\"\n", idx++, it++->c_str());
336 }
338 fprintf(stderr, "output buffer (lines: %d/%d):\n", (int)output.size(), max_output_lines);
339 for(size_t i=0; i<output.size(); i++) {
340 fprintf(stderr, "o(%d): \"%s\"\n", (int)i, output[i].c_str());
341 }
342 }