-
Notifications
You must be signed in to change notification settings - Fork 15
/
Display.cpp
383 lines (344 loc) · 9.55 KB
/
Display.cpp
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
*--------------------------------------------------------------------
* Project: VM65 - Virtual Machine/CPU emulator programming
* framework.
*
* File: Display.cpp
*
* Purpose: Implementation of Display class.
* The Display class emulates the character I/O device.
* It defines the abstract device. The actual
* input/output to the screen of the platform on which
* the emulator runs is defined in MemMapDev class
* or on higher level of abstraction.
*
* Date: 8/25/2016
*
* Copyright: (C) by Marek Karcz 2016. All rights reserved.
*
* Contact: [email protected]
*
* License Agreement and Warranty:
This software is provided with No Warranty.
I (Marek Karcz) will not be held responsible for any damage to
computer systems, data or user's health resulting from use.
Please proceed responsibly and apply common sense.
This software is provided in hope that it will be useful.
It is free of charge for non-commercial and educational use.
Distribution of this software in non-commercial and educational
derivative work is permitted under condition that original
copyright notices and comments are preserved. Some 3-rd party work
included with this project may require separate application for
permission from their respective authors/copyright owners.
*--------------------------------------------------------------------
*/
#include <ctype.h>
#include <cstdlib>
#include <iostream>
#include <string.h>
#include "Display.h"
#include "MKGenException.h"
using namespace std;
#if defined(LINUX)
#include <ncurses.h>
extern bool g_initialized;
#endif
/*
*--------------------------------------------------------------------
* Method:
* Purpose:
* Arguments:
* Returns:
*--------------------------------------------------------------------
*/
namespace MKBasic {
/*
*--------------------------------------------------------------------
* Method:
* Purpose:
* Arguments:
* Returns:
*--------------------------------------------------------------------
*/
Display::Display()
{
InitScr();
}
/*
*--------------------------------------------------------------------
* Method:
* Purpose:
* Arguments:
* Returns:
*--------------------------------------------------------------------
*/
Display::~Display()
{
}
/*
*--------------------------------------------------------------------
* Method: InitScr()
* Purpose: Initialize screen.
* Arguments: n/a
* Returns: n/a
*--------------------------------------------------------------------
*/
void Display::InitScr()
{
mpConIO = new ConsoleIO();
if (NULL == mpConIO)
throw MKGenException("Display::InitScr() : Out of memory - ConsoleIO");
mLastChar = 0;
mScrLines = SCREENDIM_ROW;
mScrColumns = SCREENDIM_COL;
mShellConsoleWidth = GetConsoleWidth();
if (mScrColumns > mShellConsoleWidth) {
mScrColumns = mShellConsoleWidth;
}
ClrScr();
}
#if defined(WINDOWS)
#include <windows.h>
#include <conio.h>
/*
*--------------------------------------------------------------------
* Method: GetConsoleWidth()
* Purpose: Obtain the width of shell console (the real one, not
* the emulated one) on Windows.
* Arguments: n/a
* Returns: int - width of the shell console.
*--------------------------------------------------------------------
*/
int Display::GetConsoleWidth()
{
HANDLE hStdOut;
CONSOLE_SCREEN_BUFFER_INFO csbi;
hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
if (hStdOut == INVALID_HANDLE_VALUE) return -1;
if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return -2;
return csbi.dwSize.X;
}
#endif
#if defined(LINUX)
#include <termcap.h>
/*
*--------------------------------------------------------------------
* Method: GetConsoleWidth()
* Purpose: Obtain the width of shell console (the real one, not
* the emulated one) on Linux.
* Arguments: n/a
* Returns: int - width of the shell console.
*--------------------------------------------------------------------
*/
int Display::GetConsoleWidth()
{
unsigned int conwidth = SCREENDIM_COL;
char *termtype = getenv("TERM");
static char termbuf[2048];
if (tgetent(termbuf, termtype) < 0) {
cout << "WARNING: Could not access the termcap data base." << endl;
cout << " Unable to determine console width." << endl;
} else {
conwidth = tgetnum("co");
}
return conwidth;
}
#endif
/*
*--------------------------------------------------------------------
* Method:
* Purpose:
* Arguments:
* Returns:
*--------------------------------------------------------------------
*/
void Display::ScrollUp()
{
for (unsigned int row=0; row<mScrLines-1; row++) {
for (unsigned int col=0; col<mScrColumns; col++) {
mScreen[row][col] = mScreen[row+1][col];
}
}
for (unsigned int col=0; col<mScrColumns; col++) {
mScreen[mScrLines-1][col] = ' ';
}
}
/*
*--------------------------------------------------------------------
* Method: GotoXY()
* Purpose: Move cursor to new coordinates.
* Arguments: col, row - integer values, new cursor coordinates
* Returns: n/a
*--------------------------------------------------------------------
*/
void Display::GotoXY(unsigned int col, unsigned int row)
{
if (col < mScrColumns && row < mScrLines) {
mCursorCoord.col = col;
mCursorCoord.row = row;
}
}
/*
*--------------------------------------------------------------------
* Method:
* Purpose:
* Arguments:
* Returns:
*--------------------------------------------------------------------
*/
bool Display::IsSpecChar(char c)
{
bool ret = false;
char sct[] = {SCREENSPECCHARS_NL,
SCREENSPECCHARS_CR,
SCREENSPECCHARS_TB,
SCREENSPECCHARS_BS,
SCREENSPECCHARS_BE,
0};
for (unsigned int i=0; i<strlen(sct); i++) {
if (c == sct[i]) {
ret = true;
break;
}
}
return ret;
}
/*
*--------------------------------------------------------------------
* Method: PutChar()
* Purpose: Output character to console. If going outside
* lower-right corner, scroll the contents up.
* Arguments: c - character to output.
* Returns: n/a
*--------------------------------------------------------------------
*/
void Display::PutChar(char c)
{
if (isalnum(c) || ispunct(c) || isspace(c) || IsSpecChar(c))
{
if (c == SCREENSPECCHARS_NL) {
mLastChar = SCREENSPECCHARS_NL;
//mCursorCoord.col = 0;
mCursorCoord.row++;
if (mCursorCoord.row >= mScrLines) {
ScrollUp();
mCursorCoord.row = mScrLines-1;
}
} else if (c == SCREENSPECCHARS_CR) {
mLastChar = SCREENSPECCHARS_CR;
mCursorCoord.col = 0;
} else if (c == SCREENSPECCHARS_TB) {
mLastChar = SCREENSPECCHARS_TB;
mCursorCoord.col += DISP_TABSIZE;
if (mCursorCoord.col >= mScrColumns) {
mCursorCoord.col = mScrColumns-1; // must work on it some more
}
} else if (c == SCREENSPECCHARS_BS) {
mLastChar = SCREENSPECCHARS_BS;
if (mCursorCoord.col > 0) mCursorCoord.col--;
} else if (c == SCREENSPECCHARS_BE) {
mLastChar = SCREENSPECCHARS_BE;
// no action
}
else {
mScreen[mCursorCoord.row][mCursorCoord.col] = c;
mLastChar = c;
mCursorCoord.col++;
if (mCursorCoord.col >= mScrColumns) {
mCursorCoord.col = 0;
mCursorCoord.row++;
if (mCursorCoord.row >= mScrLines) {
ScrollUp();
mCursorCoord.row = mScrLines-1;
}
}
}
}
}
/*
*--------------------------------------------------------------------
* Method: ClrScr()
* Purpose: Fill the screen with spaces. Set cursor in left-upper
* corner.
* Arguments: n/a
* Returns: n/a
*--------------------------------------------------------------------
*/
void Display::ClrScr()
{
for (unsigned int col=0; col<mScrColumns; col++) {
for (unsigned int row=0; row<mScrLines; row++) {
mScreen[row][col] = ' ';
}
}
mCursorCoord.col = mCursorCoord.row = 0;
}
/*
*--------------------------------------------------------------------
* Method:
* Purpose:
* Arguments:
* Returns:
*--------------------------------------------------------------------
*/
char Display::GetCharAt(unsigned int col, unsigned int row)
{
char c = -1;
if (col < mScrColumns && row < mScrLines)
c = mScreen[row][col];
return c;
}
/*
*--------------------------------------------------------------------
* Method: ShowScr()
* Purpose: Display contents of the emulated console on a... well,
* real console.
* Arguments: n/a
* Returns: n/a
*--------------------------------------------------------------------
*/
void Display::ShowScr()
{
char buf[SCREENDIM_COL] = {0};
string scr;
scr.clear();
for (unsigned int row=0; row<mScrLines; row++) {
char *linebuf = &(mScreen[row][0]);
memset(buf, 0, mScrColumns);
strncpy(buf, linebuf, mScrColumns);
buf[mScrColumns] = 0;
if (mCursorCoord.row == row) {
buf[mCursorCoord.col] = '_';
}
string line(buf);
// add extra NL if the real console is wider than emulated one
if (mShellConsoleWidth > mScrColumns) line = line + "\n";
scr = scr + line;
}
mpConIO->PrintString(scr);
}
/*
*--------------------------------------------------------------------
* Method: GetCursorCoord()
* Purpose: Get cursor coordinates.
* Arguments: n/a
* Returns: pointer to cursor coordinates
*--------------------------------------------------------------------
*/
CursorCoord *Display::GetCursorCoord()
{
return &mCursorCoord;
}
/*
*--------------------------------------------------------------------
* Method:
* Purpose:
* Arguments:
* Returns:
*--------------------------------------------------------------------
*/
char Display::GetLastChar()
{
return mLastChar;
}
} // namespace MKBasic