-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOutputWindow.cc
More file actions
368 lines (316 loc) · 12 KB
/
OutputWindow.cc
File metadata and controls
368 lines (316 loc) · 12 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Output window implementation
#include "OutputWindow.h"
#include "InputBox.h"
#include "ScrollbackSearch.h"
#include "StatusLine.h"
#include "Config.h"
#include "Color.h"
#include "TTY.h"
#include "Hook.h"
#include "misc.h"
#include <cerrno>
#if __GNUC__ >= 3
#include <locale> // isspace
#else
#include <ctype.h>
#endif
OutputWindow::OutputWindow(Window *_parent)
: Window(_parent, wh_full, _parent->height-1), top_line(0)
{
// Forget the canvas
delete[] canvas;
highlight.line = -1;
scrollback = new attrib[width * config->getOption(opt_scrollback_lines)];
viewpoint = canvas = scrollback; // point at the beginning
fFrozen = false;
clear();
screen->setScrollingRegion(trueX(), trueY(), trueX() + width, trueY() + height);
sb = new ScrollbackController(screen, this); // FIXME
}
OutputWindow::~OutputWindow()
{
canvas = NULL; // Make sure noone delets this
delete scrollback;
delete sb;
}
#define COPY_LINES 250
// 'scroll' one line up
bool OutputWindow::scroll()
{
if (canvas == scrollback + width * (config->getOption(opt_scrollback_lines) - height))
{
// We're at the end of our buffer. Copy some lines up
memmove(scrollback, scrollback + width * COPY_LINES,
(config->getOption(opt_scrollback_lines) - COPY_LINES) * width * sizeof(attrib));
canvas -= width * COPY_LINES;
viewpoint -= width * COPY_LINES;
top_line += COPY_LINES;
if (viewpoint < scrollback)
viewpoint = scrollback;
// OK, now clear one full screen beneath
canvas += width * height; // Cheat :)
clear();
canvas -= width * height;
}
else
{
canvas += width; // advance the canvas
clear_line(height-1); // Make sure the bottom line is cleared
cursor_y--;
if (!fFrozen) // Scroll what we are viewing, too
viewpoint += width;
}
return true;
}
void OutputWindow::moveViewpoint(int amount)
{
bool fQuit = false;
if (amount < 0 && viewpoint == scrollback)
status->setf ("You are already at the beginning of the scrollback buffer");
else if (amount > 0 && viewpoint == canvas)
fQuit = true; // Leave
else
{
hook.enableGroup("ScrollbackController_volatile");
viewpoint += amount * width;
if (viewpoint < scrollback) { // Don't scroll past top
viewpoint = scrollback;
}
if (viewpoint > canvas) { // We're at the end of the scrollback buffer.
viewpoint = canvas;
}
if(viewpoint == canvas) fQuit = true;
status->sticky_status = true;
status->setf("Scrollback: line %d of %d",
(viewpoint - scrollback) / width,
(canvas-scrollback) / width + height);
}
dirty = true;
if(fQuit) sb->close();
}
ScrollbackController::ScrollbackController(Window *_parent, OutputWindow *_output)
: Window(_parent,0,0,None), output(_output) {
vector<string> mygroups;
mygroups.push_back("Dirt keys");
mygroups.push_back("ScrollbackController");
hook.add(KEYPRESS, new KeypressHookStub(-1, 1.0, -1, false, true, true,
"__DIRT_ScrollbackController_page_up", mygroups, "", "",
key_page_up, "", &ScrollbackController::keypress_page_up, (void*)this));
hook.add(KEYPRESS, new KeypressHookStub(-1, 1.0, -1, false, true, true,
"__DIRT_ScrollbackController_page_down", mygroups, "", "",
key_page_down, "", &ScrollbackController::keypress_page_down, (void*)this));
hook.add(KEYPRESS, new KeypressHookStub(-1, 1.0, -1, false, true, true,
"__DIRT_ScrollbackController_home", mygroups, "", "",
key_home, "", &ScrollbackController::keypress_home, (void*)this));
hook.add(KEYPRESS, new KeypressHookStub(-1, 1.0, -1, false, true, true,
"__DIRT_ScrollbackController_pause", mygroups, "", "",
key_pause, "", &ScrollbackController::keypress_pause, (void*)this));
mygroups.push_back("ScrollbackController_volatile");
hook.add(KEYPRESS, new KeypressHookStub(1, 1.0, -1, false, true, true,
"__DIRT_ScrollbackController_end", mygroups, "", "",
key_end, "", &ScrollbackController::keypress_end, (void*)this));
hook.add(KEYPRESS, new KeypressHookStub(1, 1.0, -1, false, false, true,
"__DIRT_ScrollbackController_arrow_up", mygroups, "", "",
key_arrow_up, "", &ScrollbackController::keypress_arrow_up, (void*)this));
hook.add(KEYPRESS, new KeypressHookStub(1, 1.0, -1, false, false, true,
"__DIRT_ScrollbackController_arrow_down", mygroups, "", "",
key_arrow_down, "", &ScrollbackController::keypress_arrow_down, (void*)this));
}
bool ScrollbackController::keypress_page_up(string&, void* mt) {
ScrollbackController* mythis = (ScrollbackController*)mt;
mythis->output->moveViewpoint(-mythis->output->height/2);
mythis->output->freeze();
return true;
}
bool ScrollbackController::keypress_page_down(string&, void* mt) {
ScrollbackController* mythis = (ScrollbackController*)mt;
mythis->output->moveViewpoint(mythis->output->height/2);
return true;
}
// FIXME doesn't do anything.
bool ScrollbackController::keypress_arrow_up(string&, void* mt) {
ScrollbackController* mythis = (ScrollbackController*)mt;
mythis->output->moveViewpoint(-1);
return true;
}
// FIXME doesn't do anything.
bool ScrollbackController::keypress_arrow_down(string&, void* mt) {
ScrollbackController* mythis = (ScrollbackController*)mt;
mythis->output->moveViewpoint(1);
return true;
}
// FIXME doesn't do anything.
bool ScrollbackController::keypress_home(string&, void* mt) {
ScrollbackController* mythis = (ScrollbackController*)mt;
mythis->output->moveViewpoint(-(mythis->output->viewpoint-mythis->output->scrollback)/mythis->output->width);
return true;
}
// FIXME What is this supposed to do?
bool ScrollbackController::keypress_pause(string&, void* mt) {
ScrollbackController* mythis = (ScrollbackController*)mt;
mythis->output->moveViewpoint(0);
return true;
}
// FIXME doesn't do anything.
bool ScrollbackController::keypress_end(string&, void* mt) {
((ScrollbackController*)mt)->close();
return true;
}
// FIXME disable keys here.
void ScrollbackController::close() {
hook.disableGroup("ScrollbackController_volatile"); // Disable my keys.
status->sticky_status = false;
status->setf ("Leaving scrollback.");
output->unfreeze();
show(false);
}
void OutputWindow::search (const char *s, bool forward)
{
attrib *p = viewpoint + (width * (cursor_y-1)), *q, *r;
const char *t;
int len = strlen(s);
bool found = true;
for (;;)
{
// Search current line first
// Start search at beginning and continue until <len> offset from the right margin
for (q = p; q < p + width - len; q++)
{
found = true;
// Now compare the letters
for (r = q, t = s; *t; t++, r++)
{
if ( tolower((*r & 0xFF)) != tolower(*t))
{
found = false;
break;
}
}
if (found)
break;
}
if (found)
break;
// Go forward or backward
if (forward)
{
if (p == canvas + width * cursor_y) // we're searching the very last line!
break;
p += width;
}
else
{
if (p == scrollback)
break;
p -= width;
}
}
if (!found)
status->setf ("Search string '%s' not found", s);
else
{
highlight.line = ((p-scrollback) / width) + top_line;
highlight.x = q - p;
highlight.len = strlen(s);
// Show on the second line rather than under status bar
// FIXME
//viewpoint = scrollback >? p-width;
viewpoint = (scrollback > p-width)?scrollback:(p-width);
viewpoint = (viewpoint < canvas)?viewpoint:canvas;
//viewpoint = viewpoint <? canvas;
status->setf("Found string '%s'", s);
}
}
void OutputWindow::draw_on_parent()
{
// Show what we are viewing (not necessarily the same as what we are drawing on
if (parent)
{
// Now we need to highlight search strings, if any
if (highlight.line >= 0 &&
highlight.line-top_line >= (viewpoint-scrollback) / width &&
highlight.line-top_line <= ((viewpoint-scrollback) / width + height))
{
attrib *a, *b;
a = viewpoint + width * ( (highlight.line - top_line) - (viewpoint-scrollback)/width) + highlight.x;
// Copy the old stuff into a temp array ,ugh
// This seems to be the easiest solution however
attrib old[highlight.len];
memcpy(old, a, highlight.len*sizeof(attrib));
for (b = a; b < a+highlight.len; b++)
{
unsigned char color = ((*b & 0xFF00) >> 8) & ~(fg_bold|fg_blink);
unsigned char bg = (color & 0x0F) << 4;
unsigned char fg = (color & 0xF0) >> 4;
*b = (*b & 0x00FF) | ((bg|fg) << 8);
}
parent->copy (viewpoint, width,height,parent_x,parent_y);
memcpy(a, old, highlight.len*sizeof(attrib));
}
else
parent->copy (viewpoint, width,height,parent_x,parent_y);
}
}
void OutputWindow::move (int x, int y) {
Window::move(x,y);
screen->setScrollingRegion(trueX(), trueY(), trueX() + width, trueY() + height);
}
// Print some version information
void OutputWindow::printVersion()
{
printf (
"\n"
"Dirt version %s\n"
"Copyright (C) 2001 Bob McElrath <mcelrath@users.sourceforge.net>\n"
"Based on MCL Copyright (C) 1997-2000 Erwin S. Andreasen <erwin@andreasen.org>\n"
"Dirt comes with ABSOLUTELY NO WARRANTY; for details, see file COPYING\n"
"This binary compiled: " __DATE__ ", " __TIME__ " by " COMPILED_BY ".\n"
"Binary/source available from http://sourceforge.net/projects/dirt-client/\n",
versionToString(VERSION)
);
if (screen->isVirtual())
printf("Using Virtual Console output routines\n");
else
printf("Using TTY output routines\n");
}
void OutputWindow::saveToFile (const char *fname, bool use_color) {
FILE *fp = fopen(fname, "w");
if (!fp)
status->setf("Cannot open %s for writing: %s", fname, strerror(errno));
else {
fprintf(fp, "Scrollback saved from dirt %s at %s", versionToString(VERSION), ctime(¤t_time));
int color = -1;
for (attrib *line = scrollback; line < canvas + (width * height); line += width) {
for (attrib *a = line; a < line+width; a++) {
if (use_color && (*a >> 8) != color) {
fputs(screen->getColorCode(*a >> 8, true), fp);
color = *a >> 8;
}
fputc(*a & 0xFF, fp);
}
fputc('\n', fp);
}
fclose(fp);
status->setf("Scrollback saved successfully");
}
}
void ScrollbackSearch::execute(const char *text)
{
ScrollbackController *s;
if (text[0]) {
// If the scrollback is not up yet, we need to create it
if (!( s = (ScrollbackController*) (screen->findByName ("ScrollbackController"))))
(s = new ScrollbackController(screen, outputWindow))->keypress(key_pause); // FIXME
outputWindow->search(text, forward);
}
die();
}
ScrollbackController::~ScrollbackController() {
hook.remove("__DIRT_ScrollbackController_page_up");
hook.remove("__DIRT_ScrollbackController_page_down");
hook.remove("__DIRT_ScrollbackController_arrow_up");
hook.remove("__DIRT_ScrollbackController_arrow_down");
hook.remove("__DIRT_ScrollbackController_home");
hook.remove("__DIRT_ScrollbackController_pause");
hook.remove("__DIRT_ScrollbackController_end");
}