Retro68/Console/retro/Console.cc

543 lines
12 KiB
C++
Raw Normal View History

2019-01-08 23:12:47 +00:00
/*
2020-01-12 21:09:04 +00:00
Copyright 2012-2020 Wolfgang Thaller, Davide Bucci
2019-01-08 23:12:47 +00:00
This file is part of Retro68.
Retro68 is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Retro68 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
2019-01-08 23:12:47 +00:00
You should have received a copy of the GNU General Public License
along with Retro68. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Console.h"
#include "MacUtils.h"
#include "Fonts.h"
#include "Processes.h"
#include <algorithm>
using namespace retro;
Console *Console::currentInstance = NULL;
2020-01-12 21:09:04 +00:00
Attributes::Attributes(void)
{
reset();
}
void Attributes::reset(void)
{
cBold=false;
cUnderline=false;
cItalic=false;
}
bool Attributes::isBold(void) const
{
return cBold;
}
bool Attributes::isUnderline(void) const
{
return cUnderline;
}
bool Attributes::isItalic(void) const
{
return cItalic;
}
void Attributes::setBold(const bool v)
{
cBold=v;
}
void Attributes::setItalic(const bool v)
{
cItalic=v;
}
void Attributes::setUnderline(const bool v)
{
cUnderline=v;
}
inline bool operator==(const Attributes& lhs, const Attributes& rhs)
{
2020-01-12 21:09:04 +00:00
return lhs.isBold()==rhs.isBold() && lhs.isUnderline()==rhs.isUnderline() && lhs.isItalic()==rhs.isItalic();
}
inline bool operator!=(const Attributes& lhs, const Attributes& rhs)
{
return !(lhs == rhs);
}
inline bool operator==(const AttributedChar& lhs, const AttributedChar& rhs)
{
2020-01-12 21:09:04 +00:00
return lhs.c==rhs.c && lhs.attrs==rhs.attrs;
}
inline bool operator!=(const AttributedChar& lhs, const AttributedChar& rhs)
{
return !(lhs == rhs);
}
2019-01-08 23:12:47 +00:00
namespace
{
class FontSetup
{
2020-01-12 21:09:04 +00:00
short saveFont, saveSize, saveFace;
2019-01-08 23:12:47 +00:00
public:
FontSetup()
{
#if TARGET_API_MAC_CARBON
GrafPtr port;
GetPort(&port);
saveFont = GetPortTextFont(port);
saveSize = GetPortTextSize(port);
#else
saveFont = qd.thePort->txFont;
saveSize = qd.thePort->txSize;
2020-01-12 21:09:04 +00:00
saveFace = qd.thePort->txFace;
2019-01-08 23:12:47 +00:00
#endif
TextFont(kFontIDMonaco);
TextSize(9);
2020-01-12 21:09:04 +00:00
TextFace(normal);
2019-01-08 23:12:47 +00:00
}
~FontSetup()
{
TextFont(saveFont);
TextSize(saveSize);
2020-01-12 21:09:04 +00:00
TextFace(saveFace);
2019-01-08 23:12:47 +00:00
}
};
}
Console::Console()
{
}
Console::Console(GrafPtr port, Rect r)
{
Init(port, r);
}
Console::~Console()
{
if(currentInstance == this)
currentInstance = NULL;
}
void Console::Init(GrafPtr port, Rect r)
{
consolePort = port;
bounds = r;
2019-01-08 23:12:47 +00:00
PortSetter setport(consolePort);
FontSetup fontSetup;
InsetRect(&bounds, 2,2);
2019-01-08 23:12:47 +00:00
cellSizeY = 12;
cellSizeX = CharWidth('M');
2019-01-08 23:12:47 +00:00
rows = (bounds.bottom - bounds.top) / cellSizeY;
cols = (bounds.right - bounds.left) / cellSizeX;
2020-01-12 21:09:04 +00:00
chars = std::vector<AttributedChar>(rows*cols, AttributedChar(' ',currentAttr));
2019-01-08 23:12:47 +00:00
onscreen = chars;
cursorX = cursorY = 0;
2020-01-12 21:09:04 +00:00
isProcessingEscSequence=false;
}
void Console::SetAttributes(Attributes aa)
{
TextFace(aa.isBold()?bold+condense:0 + aa.isUnderline()?underline:0 + aa.isItalic()?italic:0);
2019-01-08 23:12:47 +00:00
}
Rect Console::CellRect(short x, short y)
{
return { (short) (bounds.top + y * cellSizeY), (short) (bounds.left + x * cellSizeX),
(short) (bounds.top + (y+1) * cellSizeY), (short) (bounds.left + (x+1) * cellSizeX) };
}
void Console::DrawCell(short x, short y, bool erase)
{
Rect r = CellRect(x,y);
if(cursorDrawn)
{
if(y == cursorY && x == cursorX)
{
erase = true;
cursorDrawn = false;
}
}
if(erase)
EraseRect(&r);
MoveTo(r.left, r.bottom - 2);
2020-01-12 21:09:04 +00:00
DrawChar(chars[y * cols + x].c);
2019-01-08 23:12:47 +00:00
}
void Console::DrawCells(short x1, short x2, short y, bool erase)
{
Rect r = { (short) (bounds.top + y * cellSizeY), (short) (bounds.left + x1 * cellSizeX),
2020-01-12 21:09:04 +00:00
(short) (bounds.top + (y+1) * cellSizeY), (short) (bounds.left + x2 * cellSizeX) };
2019-01-08 23:12:47 +00:00
if(cursorDrawn)
{
if(y == cursorY && x1 <= cursorX && x2 > cursorX)
{
erase = true;
cursorDrawn = false;
}
}
2019-01-08 23:12:47 +00:00
if(erase)
EraseRect(&r);
MoveTo(r.left, r.bottom - 2);
2020-01-12 21:09:04 +00:00
Attributes a=chars[y * cols + x1].attrs;
SetAttributes(a);
for(int i=x1; i<x2; ++i)
{
if(a!=chars[y * cols + i].attrs) {
a=chars[y * cols + i].attrs;
SetAttributes(a);
}
DrawChar(chars[y * cols + i].c);
}
2019-01-08 23:12:47 +00:00
}
void Console::Draw(Rect r)
{
if(!consolePort)
return;
PortSetter setport(consolePort);
FontSetup fontSetup;
SectRect(&r, &bounds, &r);
short minRow = std::max(0, (r.top - bounds.top) / cellSizeY);
short maxRow = std::min((int)rows, (r.bottom - bounds.top + cellSizeY - 1) / cellSizeY);
2020-01-12 21:09:04 +00:00
short minCol = 0;// std::max(0, (r.left - bounds.left) / cellSizeX);
2020-01-12 21:09:04 +00:00
short maxCol = cols; //;std::min((int)cols, (r.right - bounds.left + cellSizeX - 1) / cellSizeX);
2019-01-08 23:12:47 +00:00
EraseRect(&r);
for(short row = minRow; row < maxRow; ++row)
{
DrawCells(minCol, maxCol, row, false);
}
if(cursorDrawn)
{
Rect cursor = CellRect(cursorX, cursorY);
InvertRect(&cursor);
}
onscreen = chars;
}
void Console::ScrollUp(short n)
{
cursorY--;
std::copy(chars.begin() + cols, chars.end(), chars.begin());
2020-01-12 21:09:04 +00:00
std::fill(chars.end() - cols, chars.end(), AttributedChar(' ', currentAttr));
2019-01-08 23:12:47 +00:00
std::copy(onscreen.begin() + cols, onscreen.end(), onscreen.begin());
2020-01-12 21:09:04 +00:00
std::fill(onscreen.end() - cols, onscreen.end(), AttributedChar(' ', currentAttr));
2019-01-08 23:12:47 +00:00
RgnHandle rgn = NewRgn();
ScrollRect(&bounds, 0, -cellSizeY, rgn);
DisposeRgn(rgn);
dirtyRect.top = dirtyRect.top > 0 ? dirtyRect.top - 1 : 0;
dirtyRect.bottom = dirtyRect.bottom > 0 ? dirtyRect.bottom - 1 : 0;
}
2020-01-12 21:09:04 +00:00
void Console::ProcessEscSequence(char c)
{
switch(sequenceStep)
{
case 0:
if(c=='[')
++sequenceStep;
else
isProcessingEscSequence=false;
break;
case 1:
++sequenceStep;
switch(c)
{
case '0': // Normal character
currentAttr.reset();
break;
case '1': // Bold
currentAttr.setBold(true);
break;
case '3': // Italic
currentAttr.setItalic(true);
break;
case '4': // Underline
currentAttr.setUnderline(true);
break;
default:
isProcessingEscSequence=false;
}
break;
case 2:
if(c=='m')
isProcessingEscSequence=false;
else if(c==';')
sequenceStep=1;
else
isProcessingEscSequence=false;
break;
default:
sequenceStep=0;
}
}
2019-01-08 23:12:47 +00:00
void Console::PutCharNoUpdate(char c)
{
2020-01-12 21:09:04 +00:00
if(isProcessingEscSequence)
{
ProcessEscSequence(c);
return;
}
2019-01-08 23:12:47 +00:00
InvalidateCursor();
switch(c)
{
2020-01-12 21:09:04 +00:00
case '\033': // Begin of an ANSI escape sequence
isProcessingEscSequence=true;
sequenceStep=0;
break;
2019-01-08 23:12:47 +00:00
case '\r':
cursorX = 0;
break;
case '\n':
cursorY++;
cursorX = 0;
if(cursorY >= rows)
ScrollUp();
break;
default:
2020-01-12 21:09:04 +00:00
chars[cursorY * cols + cursorX].c = c;
chars[cursorY * cols + cursorX].attrs = currentAttr;
2019-01-08 23:12:47 +00:00
if(dirtyRect.right == 0)
{
dirtyRect.right = (dirtyRect.left = cursorX) + 1;
dirtyRect.bottom = (dirtyRect.top = cursorY) + 1;
}
else
{
dirtyRect.left = std::min(dirtyRect.left, cursorX);
dirtyRect.top = std::min(dirtyRect.top, cursorY);
dirtyRect.right = std::max(dirtyRect.right, short(cursorX + 1));
dirtyRect.bottom = std::max(dirtyRect.bottom, short(cursorY + 1));
}
cursorX++;
if(cursorX >= cols)
PutCharNoUpdate('\n');
2020-01-12 21:09:04 +00:00
// This is to make sure the cursor width is calculated correctly
chars[cursorY * cols + cursorX].attrs = currentAttr;
2019-01-08 23:12:47 +00:00
}
}
void Console::Update()
{
PortSetter setport(consolePort);
FontSetup fontSetup;
for(short row = dirtyRect.top; row < dirtyRect.bottom; ++row)
{
short start = -1;
bool needclear = false;
for(short col = dirtyRect.left; col < dirtyRect.right; ++col)
{
2020-01-12 21:09:04 +00:00
AttributedChar old = onscreen[row * cols + col];
2019-01-08 23:12:47 +00:00
if(chars[row * cols + col] != old)
{
if(start == -1)
start = col;
2020-01-12 21:09:04 +00:00
if(old.c != ' ')
2019-01-08 23:12:47 +00:00
needclear = true;
onscreen[row * cols + col] = chars[row * cols + col];
}
else
{
if(start != -1)
DrawCells(start, col, row, needclear);
start = -1;
needclear = false;
}
}
if(start != -1)
DrawCells(start, dirtyRect.right, row, needclear);
}
dirtyRect = Rect();
2019-01-08 23:12:47 +00:00
if(cursorVisible != cursorDrawn)
{
Rect r = CellRect(cursorX, cursorY);
if(cursorDrawn)
DrawCell(cursorX, cursorY, true);
else
InvertRect(&r);
cursorDrawn = !cursorDrawn;
}
2019-01-08 23:12:47 +00:00
#if TARGET_API_MAC_CARBON
QDFlushPortBuffer(consolePort,NULL);
#endif
}
void Console::putch(char c)
{
if(!rows)
return;
PutCharNoUpdate(c);
Update();
}
void Console::write(const char *p, int n)
{
if(!rows)
return;
2019-01-08 23:12:47 +00:00
for(int i = 0; i < n; i++)
Console::currentInstance->PutCharNoUpdate(*p++);
Update();
}
std::string Console::ReadLine()
{
if(!consolePort)
return "";
std::string buffer;
char c;
2019-01-08 23:12:47 +00:00
do
{
2019-01-08 23:12:47 +00:00
c = WaitNextChar();
if(!c)
{
eof = true;
return "";
}
if(c == '\r')
c = '\n';
2019-01-08 23:12:47 +00:00
if(c == '\b')
{
if(buffer.size())
{
InvalidateCursor();
cursorX--;
PutCharNoUpdate(' ');
cursorX--;
Update();
buffer.resize(buffer.size()-1);
}
continue;
}
2019-01-08 23:12:47 +00:00
putch(c);
buffer.append(1,c);
} while(c != '\n');
return buffer;
}
void Console::InvalidateCursor()
{
if(cursorDrawn)
{
PortSetter setport(consolePort);
DrawCell(cursorX, cursorY, true);
cursorDrawn = false;
}
}
void Console::Idle()
{
long ticks = TickCount();
if(ticks - blinkTicks > 60)
{
cursorVisible = !cursorVisible;
blinkTicks = ticks;
Update();
}
}
void Console::Reshape(Rect newBounds)
{
if(!consolePort)
return;
InsetRect(&newBounds, 2,2);
bounds = newBounds;
short newRows = (bounds.bottom - bounds.top) / cellSizeY;
short newCols = (bounds.right - bounds.left) / cellSizeX;
short upshift = 0;
if(cursorY >= newRows)
{
upshift = cursorY - (newRows - 1);
2019-01-08 23:12:47 +00:00
InvalidateCursor();
cursorY = newRows - 1;
}
2020-01-12 21:09:04 +00:00
std::vector<AttributedChar> newChars(newRows*newCols, AttributedChar(' ', currentAttr));
2019-01-08 23:12:47 +00:00
for(short row = 0; row < newRows && row + upshift < rows; row++)
{
2020-01-12 21:09:04 +00:00
AttributedChar *src = &chars[(row+upshift) * cols];
AttributedChar *dst = &newChars[row * newCols];
2019-01-08 23:12:47 +00:00
std::copy(src, src + std::min(cols, newCols), dst);
}
chars.swap(newChars);
/*newChars = std::vector<char>(newRows*newCols, ' ');
for(short row = 0; row < newRows && row < rows; row++)
{
char *src = &chars[row * cols];
char *dst = &newChars[row * newCols];
std::copy(src, src + std::min(cols, newCols), dst);
}
onscreen.swap(newChars);*/
onscreen = newChars;
2019-01-08 23:12:47 +00:00
rows = newRows;
cols = newCols;
if(upshift)
{
//dirtyRect = Rect { 0, 0, rows, cols };
//Update();
Draw();
}
}
char Console::WaitNextChar()
{
return 0;
}