tree changes

This commit is contained in:
2020-08-19 00:47:05 +03:00
parent c582d8ff46
commit ccd6a9888f
240 changed files with 30 additions and 12 deletions

645
libs/console/piscreen.cpp Normal file
View File

@@ -0,0 +1,645 @@
/*
PIP - Platform Independent Primitives
Console output/input
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piscreen.h"
#include "piincludes_p.h"
#ifndef WINDOWS
# include <sys/ioctl.h>
# include <fcntl.h>
# include <termios.h>
#else
# include <wincon.h>
# ifndef COMMON_LVB_UNDERSCORE
# define COMMON_LVB_UNDERSCORE 0x8000
# endif
#endif
using namespace PIScreenTypes;
PRIVATE_DEFINITION_START(PIScreen::SystemConsole)
#ifdef WINDOWS
void * hOut;
CONSOLE_SCREEN_BUFFER_INFO sbi, csbi;
CONSOLE_CURSOR_INFO curinfo;
COORD ccoord, ulcoord, bs, bc;
SMALL_RECT srect;
WORD dattr;
DWORD smode, written;
PIVector<CHAR_INFO> chars;
#endif
PRIVATE_DEFINITION_END(PIScreen::SystemConsole)
PIScreen::SystemConsole::SystemConsole() {
width = height = pwidth = pheight = 0;
mouse_x = mouse_y = -1;
int w, h;
#ifdef WINDOWS
PRIVATE->ulcoord.X = 0;
PRIVATE->hOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(PRIVATE->hOut, &PRIVATE->sbi);
PRIVATE->dattr = PRIVATE->sbi.wAttributes;
w = PRIVATE->sbi.srWindow.Right - PRIVATE->sbi.srWindow.Left;
h = PRIVATE->sbi.srWindow.Bottom - PRIVATE->sbi.srWindow.Top;
PRIVATE->ulcoord.Y = PRIVATE->sbi.srWindow.Top;
GetConsoleMode(PRIVATE->hOut, &PRIVATE->smode);
GetConsoleCursorInfo(PRIVATE->hOut, &PRIVATE->curinfo);
#else
# ifdef FREERTOS
w = 80;
h = 24;
# else
winsize ws;
ioctl(0, TIOCGWINSZ, &ws);
w = ws.ws_col;
h = ws.ws_row;
# endif
#endif
resize(w, h);
}
PIScreen::SystemConsole::~SystemConsole() {
#ifdef WINDOWS
SetConsoleMode(PRIVATE->hOut, PRIVATE->smode);
SetConsoleTextAttribute(PRIVATE->hOut, PRIVATE->dattr);
#endif
}
void PIScreen::SystemConsole::begin() {
#ifdef WINDOWS
SetConsoleMode(PRIVATE->hOut, ENABLE_WRAP_AT_EOL_OUTPUT);
GetConsoleScreenBufferInfo(PRIVATE->hOut, &PRIVATE->sbi);
PRIVATE->bc.X = 0;
PRIVATE->bc.Y = 0;
#endif
clear();
hideCursor();
}
void PIScreen::SystemConsole::end() {
#ifdef WINDOWS
SetConsoleTextAttribute(PRIVATE->hOut, PRIVATE->dattr);
#else
printf("\e[0m");
#endif
moveTo(0, height);
showCursor();
}
void PIScreen::SystemConsole::prepare() {
int w, h;
#ifdef WINDOWS
GetConsoleScreenBufferInfo(PRIVATE->hOut, &PRIVATE->csbi);
w = PRIVATE->csbi.srWindow.Right - PRIVATE->csbi.srWindow.Left + 1;
h = PRIVATE->csbi.srWindow.Bottom - PRIVATE->csbi.srWindow.Top + 1;
#else
# ifdef FREERTOS
w = 80;
h = 24;
# else
winsize ws;
ioctl(0, TIOCGWINSZ, &ws);
w = ws.ws_col;
h = ws.ws_row;
# endif
#endif
resize(w, h);
}
void PIScreen::SystemConsole::clear() {
for (int i = 0; i < cells.size_s(); ++i)
cells[i].fill(Cell());
}
void PIScreen::SystemConsole::resize(int w, int h) {
if (w == pwidth && h == pheight) return;
width = piMaxi(w, 0);
height = piMaxi(h, 0);
pwidth = width;
pheight = height;
cells.resize(height);
pcells.resize(height);
for (int i = 0; i < height; ++i) {
cells[i].resize(width);
pcells[i].resize(width, Cell(0));
}
#ifdef WINDOWS
PRIVATE->sbi.srWindow = PRIVATE->csbi.srWindow;
PRIVATE->chars.resize(width * height);
#endif
for (int i = 0; i < pcells.size_s(); ++i)
pcells[i].fill(Cell());
clear();
clearScreen();
}
void PIScreen::SystemConsole::print() {
if (mouse_x >= 0 && mouse_x < width && mouse_y >= 0 && mouse_y < height) {
///cells[mouse_y][mouse_x].format.flags ^= Inverse;
}
#ifdef WINDOWS
PRIVATE->srect = PRIVATE->sbi.srWindow;
int dx0 = -1, dx1 = -1, dy0 = -1, dy1 = -1;
for (int j = 0; j < height; ++j) {
PIVector<Cell> & ccv(cells[j]);
PIVector<Cell> & pcv(pcells[j]);
for (int i = 0; i < width; ++i)
if (ccv[i] != pcv[i]) {
if (dx0 < 0) {
dx0 = dx1 = i;
dy0 = dy1 = j;
} else {
dx0 = piMini(dx0, i);
dx1 = piMaxi(dx1, i);
dy0 = piMini(dy0, j);
dy1 = piMaxi(dy1, j);
}
}
}
if (dx0 < 0) return;
int dw = dx1 - dx0 + 1, dh = dy1 - dy0 + 1;
for (int i = 0; i < dw; ++i)
for (int j = 0; j < dh; ++j) {
int k = j * dw + i;
Cell & c(cells[j + dy0][i + dx0]);
PRIVATE->chars[k].Char.UnicodeChar = 0;
PRIVATE->chars[k].Char.AsciiChar = c.symbol.toConsole1Byte();
PRIVATE->chars[k].Attributes = attributes(c);
}
//piCout << "draw" << dw << dh;
PRIVATE->bs.X = dw;
PRIVATE->bs.Y = dh;
PRIVATE->srect.Left += dx0;
PRIVATE->srect.Top += dy0;
PRIVATE->srect.Right -= width - dx1 - 1;
PRIVATE->srect.Bottom -= height - dy1 - 1;
WriteConsoleOutput(PRIVATE->hOut, PRIVATE->chars.data(), PRIVATE->bs, PRIVATE->bc, &PRIVATE->srect);
#else
PIString s;
int si = 0, sj = 0;
CellFormat prf(0xFFFF);
for (int j = 0; j < height; ++j) {
PIVector<Cell> & ccv(cells[j]);
PIVector<Cell> & pcv(pcells[j]);
for (int i = 0; i < width; ++i) {
Cell & cc(ccv[i]);
Cell & pc(pcv[i]);
if (cc != pc) {
if (s.isEmpty()) {
si = i;
sj = j;
}
if (prf != cc.format) {
prf = cc.format;
s += formatString(cc);
}
s += cc.symbol;
} else {
if (!s.isEmpty()) {
moveTo(si, sj);
printf("%s", s.data());
s.clear();
}
}
}
if (!s.isEmpty()) {
moveTo(si, sj);
printf("%s", s.data());
s.clear();
}
}
printf("\e[0m");
fflush(0);
#endif
pcells = cells;
}
#ifdef WINDOWS
#define FOREGROUND_MASK (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
#define BACKGROUND_MASK (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE)
ushort PIScreen::SystemConsole::attributes(const PIScreenTypes::Cell & c) {
WORD attr = PRIVATE->dattr;
if (c.format.flags & Bold) attr |= FOREGROUND_INTENSITY;
else attr &= ~FOREGROUND_INTENSITY;
if (c.format.flags & Underline) attr |= COMMON_LVB_UNDERSCORE;
else attr &= ~COMMON_LVB_UNDERSCORE;
switch (c.format.color_char) {
case Black: attr = (attr & ~FOREGROUND_MASK); break;
case Red: attr = (attr & ~FOREGROUND_MASK) | FOREGROUND_RED; break;
case Green: attr = (attr & ~FOREGROUND_MASK) | FOREGROUND_GREEN; break;
case Blue: attr = (attr & ~FOREGROUND_MASK) | FOREGROUND_BLUE; break;
case Cyan: attr = (attr & ~FOREGROUND_MASK) | FOREGROUND_GREEN | FOREGROUND_BLUE; break;
case Magenta: attr = (attr & ~FOREGROUND_MASK) | FOREGROUND_RED | FOREGROUND_BLUE; break;
case Yellow: attr = (attr & ~FOREGROUND_MASK) | FOREGROUND_RED | FOREGROUND_GREEN; break;
case White: attr = attr | FOREGROUND_MASK; break;
}
switch (c.format.color_back) {
case Black: attr = (attr & ~BACKGROUND_MASK); break;
case Red: attr = (attr & ~BACKGROUND_MASK) | BACKGROUND_RED; break;
case Green: attr = (attr & ~BACKGROUND_MASK) | BACKGROUND_GREEN; break;
case Blue: attr = (attr & ~BACKGROUND_MASK) | BACKGROUND_BLUE; break;
case Cyan: attr = (attr & ~BACKGROUND_MASK) | BACKGROUND_GREEN | BACKGROUND_BLUE; break;
case Magenta: attr = (attr & ~BACKGROUND_MASK) | BACKGROUND_RED | BACKGROUND_BLUE; break;
case Yellow: attr = (attr & ~BACKGROUND_MASK) | BACKGROUND_RED | BACKGROUND_GREEN; break;
case White: attr = attr | BACKGROUND_MASK; break;
}
if ((c.format.flags & Inverse) == Inverse) {
uchar f = attr & 0xFF;
attr &= 0xFFFFFF00;
f = (f << 4) | (f >> 4);
attr |= f;
}
return attr;
}
#undef FOREGROUND_MASK
#undef BACKGROUND_MASK
void PIScreen::SystemConsole::getWinCurCoord() {
GetConsoleScreenBufferInfo(PRIVATE->hOut, &PRIVATE->csbi);
PRIVATE->ccoord = PRIVATE->csbi.dwCursorPosition;
}
void PIScreen::SystemConsole::clearLine() {
getWinCurCoord();
FillConsoleOutputAttribute(PRIVATE->hOut, PRIVATE->dattr, width - PRIVATE->ccoord.X, PRIVATE->ccoord, &PRIVATE->written);
FillConsoleOutputCharacter(PRIVATE->hOut, ' ', width - PRIVATE->ccoord.X, PRIVATE->ccoord, &PRIVATE->written);
}
void PIScreen::SystemConsole::newLine() {
getWinCurCoord();
PRIVATE->ccoord.X = 0; PRIVATE->ccoord.Y++;
SetConsoleCursorPosition(PRIVATE->hOut, PRIVATE->ccoord);
}
#else // WINDOWS
PIString PIScreen::SystemConsole::formatString(const PIScreenTypes::Cell & c) {
PIString ts("\e[0");
switch (c.format.color_char) {
case Black: ts += ";30"; break;
case Red: ts += ";31"; break;
case Green: ts += ";32"; break;
case Blue: ts += ";34"; break;
case Cyan: ts += ";36"; break;
case Magenta: ts += ";35"; break;
case Yellow: ts += ";33"; break;
case White: ts += ";37"; break;
}
switch (c.format.color_back) {
case Black: ts += ";40"; break;
case Red: ts += ";41"; break;
case Green: ts += ";42"; break;
case Blue: ts += ";44"; break;
case Cyan: ts += ";46"; break;
case Magenta: ts += ";45"; break;
case Yellow: ts += ";43"; break;
case White: ts += ";47"; break;
}
if ((c.format.flags & Bold) == Bold) ts += ";1";
if ((c.format.flags & Underline) == Underline) ts += ";4";
if ((c.format.flags & Blink) == Blink) ts += ";5";
if ((c.format.flags & Inverse) == Inverse) ts += ";7";
return ts + "m";
}
#endif // WINDOWS
void PIScreen::SystemConsole::toUpperLeft() {
#ifdef WINDOWS
SetConsoleCursorPosition(PRIVATE->hOut, PRIVATE->ulcoord);
#else
printf("\e[H");
#endif
}
void PIScreen::SystemConsole::moveTo(int x, int y) {
#ifdef WINDOWS
PRIVATE->ccoord.X = x;
PRIVATE->ccoord.Y = PRIVATE->ulcoord.Y + y;
SetConsoleCursorPosition(PRIVATE->hOut, PRIVATE->ccoord);
#else
printf("\e[%d;%dH", y + 1, x + 1);
#endif
}
void PIScreen::SystemConsole::clearScreen() {
#ifdef WINDOWS
toUpperLeft();
FillConsoleOutputAttribute(PRIVATE->hOut, PRIVATE->dattr, width * (height + 1), PRIVATE->ulcoord, &PRIVATE->written);
FillConsoleOutputCharacter(PRIVATE->hOut, ' ', width * (height + 1), PRIVATE->ulcoord, &PRIVATE->written);
#else
printf("\e[0m\e[H\e[J");
#endif
}
void PIScreen::SystemConsole::clearScreenLower() {
#ifdef WINDOWS
getWinCurCoord();
FillConsoleOutputAttribute(PRIVATE->hOut, PRIVATE->dattr, width * height - width * PRIVATE->ccoord.Y + PRIVATE->ccoord.X, PRIVATE->ccoord, &PRIVATE->written);
FillConsoleOutputCharacter(PRIVATE->hOut, ' ', width * height - width * PRIVATE->ccoord.Y + PRIVATE->ccoord.X, PRIVATE->ccoord, &PRIVATE->written);
#else
printf("\e[0m\e[J");
#endif
}
void PIScreen::SystemConsole::hideCursor() {
#ifdef WINDOWS
PRIVATE->curinfo.bVisible = false;
SetConsoleCursorInfo(PRIVATE->hOut, &PRIVATE->curinfo);
#else
printf("\e[?25l");
#endif
}
void PIScreen::SystemConsole::showCursor() {
#ifdef WINDOWS
PRIVATE->curinfo.bVisible = true;
SetConsoleCursorInfo(PRIVATE->hOut, &PRIVATE->curinfo);
#else
printf("\e[?25h");
#endif
}
PIScreen::PIScreen(bool startNow, PIKbdListener::KBFunc slot): PIThread(), drawer_(console.cells), root("rootTile") {
setName("screen");
setPriority(piLow);
needLockRun(true);
mouse_ = false;
ret_func = slot;
tile_focus = tile_dialog = 0;
root.screen = this;
listener = new PIKbdListener(key_eventS, this, startNow);
CONNECTU(listener, mouseEvent, this, mouse_event);
CONNECTU(listener, wheelEvent, this, wheel_event);
if (startNow) start();
}
PIScreen::~PIScreen() {
if (isRunning())
stop();
PIThread::waitForFinish(10);
listener->waitForFinish(10);
delete listener;
}
void PIScreen::setMouseEnabled(bool on) {
mouse_ = on;
console.mouse_x = console.mouse_y = -1;
}
void PIScreen::key_event(PIKbdListener::KeyEvent key) {
/** DEBUG
if (ret_func != 0) ret_func(key, t);
keyPressed(key, t);
return;
*/
PIScreenTile * rtile = rootTile();
if (tile_dialog)
rtile = tile_dialog;
bool used = nextFocus(rtile, key);
if (used) return;
if (!used && tile_focus) {
if (tile_focus->visible) {
if (tile_focus->keyEvent(key))
return;
}
}
if (ret_func != 0) ret_func(key, data_);
keyPressed(key, data_);
}
PIVector<PIScreenTile * > PIScreen::prepareMouse(PIKbdListener::MouseEvent * e) {
PIVector<PIScreenTile * > ret;
if (!mouse_ || !e) return ret;
console.mouse_x = e->x;
console.mouse_y = e->y;
PIVector<PIScreenTile * > tl = tilesUnderMouse(e->x, e->y);
bool ff = false;
piForeachR (PIScreenTile * t, tl) {
if (!ff) {
if (t->focus_flags[FocusOnMouse] && (e->action == PIKbdListener::MouseButtonPress)) {
t->setFocus();
ff = true;
}
if (t->focus_flags[FocusOnWheel] && (e->action == PIKbdListener::MouseWheel)) {
t->setFocus();
ff = true;
}
}
}
return tl;
}
PIVector<PIScreenTile * > PIScreen::tilesUnderMouse(int x, int y) {
PIVector<PIScreenTile * > ret;
if (x < 0 || x >= console.width || y < 0 || y >= console.height) return ret;
PIScreenTile * ct = tile_dialog ? tile_dialog : rootTile();
bool f = true;
while (ct) {
if (!f) ret << ct;
f = false;
ct = ct->childUnderMouse(x, y);
}
return ret;
}
void PIScreen::mouse_event(PIKbdListener::MouseEvent me) {
PIVector<PIScreenTile * > tl = prepareMouse(&me);
if (tl.isEmpty()) return;
piForeachR (PIScreenTile * t, tl)
if (t->mouseEvent(me)) break;
}
void PIScreen::wheel_event(PIKbdListener::WheelEvent we) {
PIVector<PIScreenTile * > tl = prepareMouse(&we);
if (tl.isEmpty()) return;
piForeachR (PIScreenTile * t, tl)
if (t->wheelEvent(we)) break;
}
bool PIScreen::nextFocus(PIScreenTile * rt, PIKbdListener::KeyEvent key) {
PIVector<PIScreenTile*> vtl = rt->children(true), ftl;
piForeach (PIScreenTile * t, vtl) {
if (t->focus_flags[CanHasFocus])
ftl << t;
}
int ind = -1;
for (int i = 0; i < ftl.size_s(); ++i)
if (ftl[i] == tile_focus) {
ind = i;
break;
}
if (ind < 0)
tile_focus = 0;
if (ftl.isEmpty())
tile_focus = 0;
else {
if (tile_focus)
if (!tile_focus->visible)
tile_focus = 0;
int next = tile_focus ? 0 : 1;
if (tile_focus) {
if (tile_focus->focus_flags[NextByTab] && key.key == PIKbdListener::Tab)
next = 1;
if (tile_focus->focus_flags[NextByArrowsHorizontal]) {
if (key.key == PIKbdListener::LeftArrow) next = -1;
if (key.key == PIKbdListener::RightArrow) next = 1;
}
if (tile_focus->focus_flags[NextByArrowsVertical]) {
if (key.key == PIKbdListener::UpArrow) next = -1;
if (key.key == PIKbdListener::DownArrow) next = 1;
}
}
//piCout << ftl.size() << ind << next;
if (next != 0) {
PIVector<PIScreenTile*> tl = rt->children();
piForeach (PIScreenTile * t, tl)
t->has_focus = false;
if (!ftl.isEmpty()) {
ind += next;
if (ind >= ftl.size_s()) ind = 0;
if (ind < 0) ind = ftl.size_s() - 1;
tile_focus = ftl[ind];
tile_focus->has_focus = true;
}
return true;
}
}
return false;
}
void PIScreen::tileEventInternal(PIScreenTile * t, TileEvent e) {
tileEvent(t, e);
}
void PIScreen::tileRemovedInternal(PIScreenTile * t) {
if (tile_dialog == t)
tile_dialog = 0;
}
void PIScreen::tileSetFocusInternal(PIScreenTile * t) {
PIScreenTile * rt = rootTile();
if (tile_dialog)
rt = tile_dialog;
PIVector<PIScreenTile*> tl = rt->children(), ftl;
piForeach (PIScreenTile * i, tl)
i->has_focus = false;
tile_focus = t;
if (!tile_focus) return;
if (tile_focus->focus_flags[CanHasFocus])
tile_focus->has_focus = true;
}
void PIScreen::setDialogTile(PIScreenTile * t) {
tile_dialog = t;
if (!tile_dialog) {
nextFocus(&root);
return;
}
tile_dialog->setScreen(this);
tile_dialog->parent = 0;
nextFocus(tile_dialog);
}
void PIScreen::waitForFinish() {
WAIT_FOR_EXIT
stop();
}
void PIScreen::stop(bool clear) {
PIThread::stop(true);
if (clear) console.clearScreen();
#ifndef WINDOWS
fflush(0);
#endif
}
void PIScreen::begin() {
listener->start();
nextFocus(&root);
console.begin();
}
void PIScreen::run() {
console.prepare();
root.width_ = drawer_.width = console.width;
root.height_ = drawer_.height = console.height;
root.layout();
root.drawEventInternal(&drawer_);
if (tile_dialog) {
int sw(0), sh(0);
tile_dialog->sizeHint(sw, sh);
sw = piClampi(sw, tile_dialog->minimumWidth, tile_dialog->maximumWidth);
sh = piClampi(sh, tile_dialog->minimumHeight, tile_dialog->maximumHeight);
tile_dialog->x_ = (console.width - sw) / 2;
tile_dialog->y_ = (console.height - sh) / 2;
tile_dialog->width_ = sw;
tile_dialog->height_ = sh;
tile_dialog->layout();
int dx = tile_dialog->x_ - 1, dy = tile_dialog->y_ - 1, dw = tile_dialog->width_, dh = tile_dialog->height_;
drawer_.drawFrame(dx, dy, dx + dw + 1, dy + dh + 1, (Color)tile_dialog->back_format.color_char,
(Color)tile_dialog->back_format.color_back, (CharFlags)tile_dialog->back_format.flags);
tile_dialog->drawEventInternal(&drawer_);
}
console.print();
}
void PIScreen::end() {
listener->stop();
console.end();
}
PIScreenTile * PIScreen::tileByName(const PIString & name) {
PIVector<PIScreenTile*> tl(tiles());
piForeach (PIScreenTile * t, tl)
if (t->name() == name)
return t;
return 0;
}

View File

@@ -0,0 +1,42 @@
/*
PIP - Platform Independent Primitives
Tile for PIScreen with PIConsole API
Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piscreenconsole.h"
using namespace PIScreenTypes;
TileVars::TileVars(const PIString &n) : PIScreenTile(n) {
alignment = Left;
}
void TileVars::sizeHint(int &w, int &h) const {
}
void TileVars::drawEvent(PIScreenDrawer *d) {
}
PIScreenConsoleTile::PIScreenConsoleTile() {
}

View File

@@ -0,0 +1,271 @@
/*
PIP - Platform Independent Primitives
Console output/input
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piscreendrawer.h"
// comment for use ascii instead of unicode symbols
#define USE_UNICODE
using namespace PIScreenTypes;
PIScreenDrawer::PIScreenDrawer(PIVector<PIVector<Cell> > & c): cells(c) {
arts_[LineVertical] =
#ifdef USE_UNICODE
PIChar::fromUTF8("");
#else
PIChar('|');
#endif
arts_[LineHorizontal] =
#ifdef USE_UNICODE
PIChar::fromUTF8("");
#else
PIChar('-');
#endif
arts_[Cross] =
#ifdef USE_UNICODE
PIChar::fromUTF8("");
#else
PIChar('+');
#endif
arts_[CornerTopLeft] =
#ifdef USE_UNICODE
PIChar::fromUTF8("");
#else
PIChar('+');
#endif
arts_[CornerTopRight] =
#ifdef USE_UNICODE
PIChar::fromUTF8("");
#else
PIChar('+');
#endif
arts_[CornerBottomLeft] =
#ifdef USE_UNICODE
PIChar::fromUTF8("");
#else
PIChar('+');
#endif
arts_[CornerBottomRight] =
#ifdef USE_UNICODE
PIChar::fromUTF8("");
#else
PIChar('+');
#endif
arts_[Unchecked] =
#ifdef USE_UNICODE
PIChar::fromUTF8("");
#else
PIChar('O');
#endif
arts_[Checked] =
#ifdef USE_UNICODE
PIChar::fromUTF8("");
#else
PIChar('0');
#endif
}
void PIScreenDrawer::clear() {
clear(cells);
}
void PIScreenDrawer::drawPixel(int x, int y, const PIChar & c, Color col_char, Color col_back, CharFlags flags_char) {
if (x < 0 || x >= width || y < 0 || y >= height) return;
cells[y][x].symbol = c;
cells[y][x].format.color_char = col_char;
cells[y][x].format.color_back = col_back;
cells[y][x].format.flags = flags_char;
}
void PIScreenDrawer::drawLine(int x0, int y0, int x1, int y1, const PIChar & c, Color col_char, Color col_back, CharFlags flags_char) {
if (x0 == x1 && y0 == y1) drawPixel(x0, y0, c, col_char, col_back, flags_char);
Cell cc;
cc.symbol = c;
cc.format.color_char = col_char;
cc.format.color_back = col_back;
cc.format.flags = flags_char;
int x = 0, y = 0;
if (piAbsi(x1 - x0) >= piAbsi(y1 - y0)) {
float dy = (y1 - y0) / float(piAbsi(x1 - x0)), cy = y0;
int dx = x0 < x1 ? 1 : -1;
for (int i = x0; i != x1; i += dx) {
x = i; y = piRound(cy);
if (x >= 0 && x < width && y >= 0 && y < height)
cells[y][x] = cc;
cy += dy;
}
y = piRound(cy);
if (x1 >= 0 && x1 < width && y >= 0 && y < height)
cells[y][x1] = cc;
} else {
float dx = (x1 - x0) / float(piAbsi(y1 - y0)), cx = x0;
int dy = y0 < y1 ? 1 : -1;
for (int i = y0; i != y1; i += dy) {
x = piRound(cx); y = i;
if (x >= 0 && x < width && y >= 0 && y < height)
cells[y][x] = cc;
cx += dx;
}
x = piRound(cx);
if (x >= 0 && x < width && y1 >= 0 && y1 < height)
cells[y1][x] = cc;
}
}
void PIScreenDrawer::drawRect(int x0, int y0, int x1, int y1, const PIChar & c, Color col_char, Color col_back, CharFlags flags_char) {
if (x0 == x1 && y0 == y1) drawPixel(x0, y0, c, col_char, col_back, flags_char);
Cell cc;
cc.symbol = c;
cc.format.color_char = col_char;
cc.format.color_back = col_back;
cc.format.flags = flags_char;
int dx = x0 < x1 ? 1 : -1;
int dy = y0 < y1 ? 1 : -1;
int xs[2] = {x0, x1};
int ys[2] = {y0, y1};
for (int k = 0; k < 2; ++k) {
int j = ys[k];
if (j >= 0 && j < height) {
PIVector<Cell> & cv(cells[j]);
for (int i = x0; i != x1; i += dx)
if (i >= 0 && i < width)
cv[i] = cc;
}
j = xs[k];
if (j >= 0 && j < width) {
for (int i = y0; i != y1; i += dy)
if (i >= 0 && i < height)
cells[i][j] = cc;
}
}
int i = x1, j = y1;
if (i >= 0 && i < width && j >= 0 && j < height)
cells[j][i] = cc;
}
void PIScreenDrawer::drawFrame(int x0, int y0, int x1, int y1, Color col_char, Color col_back, CharFlags flags_char) {
if (x0 == x1 && y0 == y1) return;
Cell cc;
cc.format.color_char = col_char;
cc.format.color_back = col_back;
cc.format.flags = flags_char;
int dx = x0 < x1 ? 1 : -1;
int dy = y0 < y1 ? 1 : -1;
int xs[2] = {x0, x1};
int ys[2] = {y0, y1};
for (int k = 0; k < 2; ++k) {
int j = ys[k];
if (j >= 0 && j < height) {
PIVector<Cell> & cv(cells[j]);
cc.symbol = artChar(LineHorizontal);
for (int i = x0 + 1; i != x1; i += dx)
if (i >= 0 && i < width)
cv[i] = cc;
}
j = xs[k];
if (j >= 0 && j < width) {
cc.symbol = artChar(LineVertical);
for (int i = y0 + 1; i != y1; i += dy)
if (i >= 0 && i < height)
cells[i][j] = cc;
}
}
int i = x0, j = y0; cc.symbol = artChar(CornerTopLeft);
if (i >= 0 && i < width && j >= 0 && j < height) cells[j][i] = cc;
i = x1, j = y0; cc.symbol = artChar(CornerTopRight);
if (i >= 0 && i < width && j >= 0 && j < height) cells[j][i] = cc;
i = x0, j = y1; cc.symbol = artChar(CornerBottomLeft);
if (i >= 0 && i < width && j >= 0 && j < height) cells[j][i] = cc;
i = x1, j = y1; cc.symbol = artChar(CornerBottomRight);
if (i >= 0 && i < width && j >= 0 && j < height) cells[j][i] = cc;
}
void PIScreenDrawer::fillRect(int x0, int y0, int x1, int y1, const PIChar & c, Color col_char, Color col_back, CharFlags flags_char) {
if (x0 == x1 && y0 == y1) drawPixel(x0, y0, c, col_char, col_back, flags_char);
Cell cc;
cc.symbol = c;
cc.format.color_char = col_char;
cc.format.color_back = col_back;
cc.format.flags = flags_char;
int dx = x0 < x1 ? 1 : -1;
int dy = y0 < y1 ? 1 : -1;
for (int j = y0; j != y1; j += dy)
if (j >= 0 && j < height) {
PIVector<Cell> & cv(cells[j]);
for (int i = x0; i != x1; i += dx)
if (i >= 0 && i < width)
cv[i] = cc;
}
}
void PIScreenDrawer::fillRect(int x0, int y0, int x1, int y1, PIVector<PIVector<Cell> > & content) {
if (x0 > x1) piSwap(x0, x1);
if (y0 > y1) piSwap(y0, y1);
int w = x1 - x0;
int h = y1 - y0;
for (int j = 0; j < h; ++j)
if (j < piMini(height, content.size_s())) {
if ((j + y0) >= 0 && (j + y0) < height) {
PIVector<Cell> & cv(cells[y0 + j]);
PIVector<Cell> & contv(content[j]);
for (int i = 0; i < piMini(w, contv.size_s()); ++i)
if ((i + x0) >= 0 && (i + x0) < width)
cv[x0 + i] = contv[i];
}
}
}
void PIScreenDrawer::clear(PIVector<PIVector<Cell> > & cells) {
for (int i = 0; i < cells.size_s(); ++i)
cells[i].fill(Cell());
}
void PIScreenDrawer::drawText(int x, int y, const PIString & s, Color col_char, Color col_back, CharFlags flags_char) {
if (x < 0 || x >= width || y < 0 || y >= height) return;
PIVector<Cell> & cv(cells[y]);
Cell cc;
cc.format.color_char = col_char;
cc.format.color_back = col_back;
cc.format.flags = flags_char;
for (int i = 0; i < s.size_s(); ++i) {
int j = i + x;
if (j >= 0 && j < width) {
cc.symbol = s[i];
cv[j] = cc;
}
}
}

View File

@@ -0,0 +1,250 @@
/*
PIP - Platform Independent Primitives
Basic PIScreen tile
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piscreentile.h"
#include "piscreendrawer.h"
using namespace PIScreenTypes;
PIScreenTile::PIScreenTile(const PIString & n, Direction d, SizePolicy p): PIObject(n) {
direction = d;
size_policy = p;
focus_flags = 0;
screen = 0;
minimumWidth = minimumHeight = x_ = y_ = width_ = height_ = pw = ph = 0;
maximumWidth = maximumHeight = 65535;
marginLeft = marginRight = marginTop = marginBottom = spacing = 0;
parent = 0;
back_symbol = ' ';
visible = true;
has_focus = false;
}
PIScreenTile::~PIScreenTile() {
//piCout << this << "~";
if (screen)
screen->tileRemovedInternal(this);
setScreen(0);
deleteChildren();
if (!parent) return;
parent->tiles.removeOne(this);
}
void PIScreenTile::addTile(PIScreenTile * t) {
if (t == 0) return;
if (tiles.contains(t)) return;
tiles << t;
t->parent = this;
t->setScreen(screen);
}
void PIScreenTile::takeTile(PIScreenTile * t) {
if (!tiles.contains(t)) return;
tiles.removeOne(t);
t->parent = 0;
t->setScreen(0);
}
void PIScreenTile::removeTile(PIScreenTile * t) {
if (!tiles.contains(t)) return;
tiles.removeOne(t);
t->parent = 0;
t->setScreen(0);
delete t;
}
PIVector<PIScreenTile * > PIScreenTile::children(bool only_visible) {
PIVector<PIScreenTile * > ret;
piForeach (PIScreenTile * t, tiles)
if (t->visible || !only_visible)
ret << t << t->children(only_visible);
return ret;
}
PIScreenTile * PIScreenTile::childUnderMouse(int x, int y) {
piForeach (PIScreenTile * t, tiles) {
if (!t->visible) continue;
if (x >= t->x_ && (x - t->x_) < t->width_ &&
y >= t->y_ && (y - t->y_) < t->height_) {
return t;
}
}
return 0;
}
void PIScreenTile::raiseEvent(TileEvent e) {
if (!screen) return;
screen->tileEventInternal(this, e);
}
void PIScreenTile::setScreen(PIScreenBase * s) {
screen = s;
piForeach (PIScreenTile * t, tiles)
t->setScreen(s);
}
void PIScreenTile::deleteChildren() {
piForeach (PIScreenTile * t, tiles) {
t->parent = 0;
delete t;
}
tiles.clear();
}
void PIScreenTile::setFocus() {
if (!screen || !focus_flags[CanHasFocus]) return;
screen->tileSetFocusInternal(this);
}
void PIScreenTile::drawEventInternal(PIScreenDrawer * d) {
if (!visible) {
return;
}
d->fillRect(x_, y_, x_ + width_, y_ + height_, back_symbol, (Color)back_format.color_char, (Color)back_format.color_back, back_format.flags);
drawEvent(d);
piForeach (PIScreenTile * t, tiles)
t->drawEventInternal(d);
}
void PIScreenTile::sizeHint(int & w, int & h) const {
w = 0;
h = 0;
if (tiles.isEmpty()) return;
int sl = spacing * (tiles.size_s() - 1);
if (direction == Horizontal) w += sl;
else h += sl;
piForeachC (PIScreenTile * t, tiles) {
if (!t->visible) continue;
int cw(0), ch(0);
t->sizeHint(cw, ch);
cw = piClampi(cw, t->minimumWidth, t->maximumWidth);
ch = piClampi(ch, t->minimumHeight, t->maximumHeight);
if (direction == Horizontal) {
w += cw; h = piMaxi(h, ch);
} else {
h += ch; w = piMaxi(w, cw);
}
}
w += marginLeft + marginRight;
h += marginTop + marginBottom;
}
void PIScreenTile::layout() {
if (tiles.isEmpty() || !visible) return;
int as(0), ts(0), ts2(0), ecnt(0), pcnt(0);
ts = (direction == Horizontal) ? (width_ - marginLeft - marginRight) : (height_ - marginTop - marginBottom);
ts2 = (direction != Horizontal) ? (width_ - marginLeft - marginRight) : (height_ - marginTop - marginBottom);
ts -= spacing * (tiles.size_s() - 1);
PIVector<int> hints(tiles.size_s());
PIVector<float> asizes(tiles.size_s());
for (int i = 0; i < tiles.size_s(); ++i) {
PIScreenTile * t(tiles[i]);
int cw(0), ch(0), cs(0);
if (t->visible && t->needLayout()) {
t->sizeHint(cw, ch);
cw = piClampi(cw, t->minimumWidth, t->maximumWidth);
ch = piClampi(ch, t->minimumHeight, t->maximumHeight);
if (t->size_policy == Expanding) ++ecnt;
if (t->size_policy == Preferred) ++pcnt;
cs = (direction == Horizontal) ? cw : ch;
as += cs;
}
hints[i] = cs;
asizes[i] = 0.f;
}
if (as <= ts) {
int acnt(0);
SizePolicy pol = Fixed;
if (ecnt > 0) {
acnt = ecnt;
pol = Expanding;
} else if (pcnt > 0) {
acnt = pcnt;
pol = Preferred;
}
if (acnt > 0) {
float add_a = float(ts - as), add_s = add_a / acnt, add_da(0.);
asizes.fill(add_s);
PISet<int> max_tl;
for (int i = 0; i < tiles.size_s(); ++i) {
if (tiles[i]->size_policy == pol && tiles[i]->visible && tiles[i]->needLayout()) {
float maxs = (direction == Horizontal) ? tiles[i]->maximumWidth : tiles[i]->maximumHeight;
if (hints[i] + asizes[i] > maxs) {
max_tl << i;
float pas = asizes[i];
asizes[i] = maxs - hints[i];
acnt--;
if (acnt > 0) {
pas = (pas - asizes[i]) / acnt;
for (int j = 0; j < tiles.size_s(); ++j) {
if (i == j) continue;
if (max_tl[j]) continue;
if (tiles[j]->size_policy == pol && tiles[j]->visible && tiles[j]->needLayout())
asizes[j] += pas;
}
}
}
}
}
for (int i = 0; i < tiles.size_s(); ++i) {
if (tiles[i]->size_policy == pol && tiles[i]->visible && tiles[i]->needLayout()) {
int a = piRound(asizes[i] + add_da);
add_da += asizes[i] - a;
hints[i] += a;
}
}
}
}
int cx = x_ + marginLeft, cy = y_ + marginTop;
for (int i = 0; i < tiles.size_s(); ++i) {
PIScreenTile * t = tiles[i];
if (!t->visible || !t->needLayout()) continue;
t->x_ = cx;
t->y_ = cy;
if (direction == Horizontal) {
t->width_ = hints[i];
t->height_ = ts2;
cx += hints[i] + spacing;
} else {
t->width_ = ts2;
t->height_ = hints[i];
cy += hints[i] + spacing;
}
if (t->pw != t->width_ || t->ph != t->height_)
t->resizeEvent(t->width_, t->height_);
t->pw = t->width_;
t->ph = t->height_;
t->layout();
}
}

View File

@@ -0,0 +1,714 @@
/*
PIP - Platform Independent Primitives
Various tiles for PIScreen
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piscreentiles.h"
#include "piscreendrawer.h"
using namespace PIScreenTypes;
TileSimple::TileSimple(const PIString & n): PIScreenTile(n) {
alignment = Left;
}
TileSimple::TileSimple(const TileSimple::Row & r): PIScreenTile() {
alignment = Left;
content << r;
}
void TileSimple::sizeHint(int & w, int & h) const {
w = h = 0;
piForeachC (Row & r, content)
w = piMaxi(w, r.first.size_s());
h = content.size_s();
}
void TileSimple::drawEvent(PIScreenDrawer * d) {
for (int i = 0; i < content.size_s(); ++i) {
Row & r(content[i]);
int rx = 0;
switch (alignment) {
case Left: rx = x_; break;
case Center: rx = x_ + (width_ - r.first.size_s()) / 2; break;
case Right: rx = x_ + width_ - r.first.size_s(); break;
};
d->drawText(rx, y_ + i, r.first, (Color)r.second.color_char, (Color)r.second.color_back, r.second.flags);
}
}
TileScrollBar::TileScrollBar(const PIString & n) {
direction = Vertical;
thickness = 1;
minimum_ = value_ = 0;
maximum_ = 100;
}
void TileScrollBar::setMinimum(int v) {
minimum_ = v;
_check();
}
void TileScrollBar::setMaximum(int v) {
maximum_ = v;
_check();
}
void TileScrollBar::setValue(int v) {
value_ = v;
_check();
}
void TileScrollBar::_check() {
value_ = piClampi(value_, minimum_, maximum_);
}
void TileScrollBar::sizeHint(int & w, int & h) const {
w = h = 0;
if (direction == Vertical) {
w = thickness;
h = 255;
} else {
h = thickness;
w = 255;
}
}
void TileScrollBar::drawEvent(PIScreenDrawer * d) {
line_char = d->artChar(direction == Vertical ? PIScreenDrawer::LineVertical : PIScreenDrawer::LineHorizontal);
d->fillRect(x_, y_, x_ + width_, y_ + height_, line_char, Green);
if (value_ >= minimum_ && value_ <= maximum_) {
if (direction == Vertical) {
int c = piRoundf((float(value_ - minimum_) / (maximum_ - minimum_)) * (height_ - 1));
d->drawLine(x_, y_ + c, x_ + width_ - 1, y_ + c, ' ', Green, Green);
} else {
int c = piRoundf((float(value_ - minimum_) / (maximum_ - minimum_)) * (width_ - 1));
d->drawLine(x_ + c, y_, x_ + c, y_ + height_ - 1, ' ', Green, Green);
}
}
}
bool TileScrollBar::mouseEvent(PIKbdListener::MouseEvent me) {
return true;
}
TileList::TileList(const PIString & n): PIScreenTile(n) {
alignment = Left;
focus_flags = CanHasFocus | NextByArrowsHorizontal | NextByTab | FocusOnMouseOrWheel;
lhei = offset = cur = 0;
mouse_sel = false;
selection_mode = NoSelection;
scroll = new TileScrollBar();
scroll->size_policy = Ignore;
addTile(scroll);
}
void TileList::sizeHint(int & w, int & h) const {
w = h = 0;
piForeachC (Row & r, content)
w = piMaxi(w, r.first.size_s());
h = 3;
}
void TileList::resizeEvent(int w, int h) {
scroll->x_ = x_ + width_ - 1;
scroll->y_ = y_;
scroll->width_ = 1;
scroll->height_ = height_;
}
void TileList::drawEvent(PIScreenDrawer * d) {
lhei = height_ - 2;
int is = piClampi(offset, 0, piMaxi(0, content.size_s() - 1)), ie = piClampi(offset + lhei, 0, content.size_s());
if (is > 0) d->drawText(x_, y_, PIString(" /\\ ").repeat(width_ / 4), Green, Default, Bold);
if (ie < content.size_s()) d->drawText(x_, y_ + height_ - 1, PIString(" \\/ ").repeat(width_ / 4), Green, Default, Bold);
//piCout << is << ie << offset << lhei << content.size_s();
for (int i = is; i < ie; ++i) {
Row & r(content[i]);
bool sel = i == cur && has_focus;
if (sel) {
int cy = y_ + i - is + 1;
d->drawLine(x_, cy, x_ + width_ - 2, cy, ' ', Default, Blue);
}
int rx(0);
switch (alignment) {
case Left: rx = x_; break;
case Center: rx = x_ + (width_ - 1 - r.first.size_s()) / 2; break;
case Right: rx = x_ + width_ - 1 - r.first.size_s(); break;
};
CharFlags cf = r.second.flags;
Color cc = (Color)r.second.color_char;
if (selected[i]) {
cf |= Bold;
cc = Yellow;
}
d->drawText(rx, y_ + i - is + 1, r.first, cc, sel ? Blue : Default, cf);
}
scroll->setMaximum(piMaxi(0, content.size_s() - 1));
scroll->setValue(cur);
}
bool TileList::keyEvent(PIKbdListener::KeyEvent key) {
lhei = height_ - 2;
int oo(0), osp = piMini(3, lhei / 4);
switch (key.key) {
case PIKbdListener::PageUp:
cur -= lhei / 2;
oo -= lhei / 2;
case PIKbdListener::UpArrow:
cur--;
oo--;
if (key.modifiers[PIKbdListener::Ctrl]) {
cur -= 4;
oo -= 4;
}
if (cur < 0) cur = 0;
if (cur - offset < osp) offset += oo;
if (offset < 0) offset = 0;
return true;
case PIKbdListener::Space:
if (cur < 0 || cur >= content.size_s()) return true;
switch (selection_mode) {
case NoSelection: return false;
case SingleSelection:
if (selected.isEmpty()) selected << cur;
else {
bool add = !selected[cur];
selected.clear();
if (add) selected << cur;
}
raiseEvent(TileEvent(SelectionChanged));
return true;
case MultiSelection:
if (selected[cur]) selected.remove(cur);
else selected << cur;
raiseEvent(TileEvent(SelectionChanged));
break;
}
case PIKbdListener::PageDown:
if (key.key == PIKbdListener::PageDown) {
cur += lhei / 2;
oo += lhei / 2;
}
case PIKbdListener::DownArrow:
cur++;
oo++;
if (key.modifiers[PIKbdListener::Ctrl]) {
cur += 4;
oo += 4;
}
if (cur >= content.size_s()) cur = content.size_s() - 1;
if (cur - offset >= lhei - osp) offset += oo;
if (offset >= content.size_s() - lhei) offset = content.size_s() - lhei;
if (offset < 0) offset = 0;
return true;
case PIKbdListener::Home:
cur = offset = 0;
return true;
case PIKbdListener::End:
cur = content.size_s() - 1;
offset = content.size_s() - lhei;
if (offset < 0) offset = 0;
return true;
case PIKbdListener::Return:
if (cur >= 0 && cur < content.size_s())
raiseEvent(TileEvent(RowPressed, cur));
return true;
case '*':
if (selection_mode == TileList::MultiSelection) {
PISet<int> nsel;
for (int i = 0; i < content.size_s(); ++i)
if (!selected[i]) nsel << i;
selected = nsel;
}
raiseEvent(TileEvent(SelectionChanged));
return true;
case 'A':
if (selection_mode == TileList::MultiSelection) {
selected.clear();
for (int i = 0; i < content.size_s(); ++i)
selected << i;
}
raiseEvent(TileEvent(SelectionChanged));
return true;
}
return PIScreenTile::keyEvent(key);
}
bool TileList::mouseEvent(PIKbdListener::MouseEvent me) {
if (me.action == PIKbdListener::MouseButtonRelease) return true;
int mp = me.y - y() - 1 + offset;
if (mp < 0 || mp >= content.size_s()) return true;
cur = mp;
switch (me.action) {
case PIKbdListener::MouseButtonPress:
mouse_sel = !selected.contains(cur);
break;
case PIKbdListener::MouseButtonDblClick:
keyEvent(PIKbdListener::KeyEvent(PIKbdListener::Return));
return true;
default: break;
}
if (me.buttons[PIKbdListener::MouseRight]) {
switch (selection_mode) {
case SingleSelection:
selected.clear();
selected << cur;
raiseEvent(TileEvent(SelectionChanged));
break;
case MultiSelection:
if (mouse_sel) selected << cur;
else selected.remove(cur);
raiseEvent(TileEvent(SelectionChanged));
break;
default: break;
}
}
return true;
}
bool TileList::wheelEvent(PIKbdListener::WheelEvent we) {
keyEvent(PIKbdListener::KeyEvent(we.direction ? PIKbdListener::PageUp : PIKbdListener::PageDown));
return true;
}
TileButton::TileButton(const PIString & n): PIScreenTile(n) {
focus_flags = CanHasFocus | NextByTab | NextByArrowsAll | FocusOnMouse;
}
void TileButton::sizeHint(int & w, int & h) const {
w = text.size_s() + 2;
h = 1;
}
void TileButton::drawEvent(PIScreenDrawer * d) {
Color cb = has_focus ? Blue : Cyan;
Color ct = has_focus ? White : Black;
int ff = has_focus ? Bold : 0;
d->fillRect(x_, y_, x_ + width_, y_ + 1, ' ', Default, cb);
d->drawText(x_, y_, "[", ct, Transparent, ff);
d->drawText(x_ + (width_ - text.size_s()) / 2, y_, text, ct, Transparent, ff);
d->drawText(x_ + width_ - 1, y_, "]", ct, Transparent, ff);
}
bool TileButton::keyEvent(PIKbdListener::KeyEvent key) {
if (key.key == PIKbdListener::Space || key.key == PIKbdListener::Return) {
raiseEvent(TileEvent(ButtonClicked));
return true;
}
return PIScreenTile::keyEvent(key);
}
bool TileButton::mouseEvent(PIKbdListener::MouseEvent me) {
if (me.action != PIKbdListener::MouseButtonRelease) return true;
keyEvent(PIKbdListener::KeyEvent(PIKbdListener::Return));
return true;
}
TileButtons::TileButtons(const PIString & n): PIScreenTile(n) {
focus_flags = CanHasFocus | NextByTab | FocusOnMouse;
direction = Horizontal;
alignment = PIScreenTypes::Center;
cur = 0;
}
void TileButtons::sizeHint(int & w, int & h) const {
w = h = 0;
if (direction == Horizontal) {
piForeachC (Button & b, content)
w += b.first.size_s() + 4;
w += piMaxi(0, content.size_s() - 1) * 2;
h += 1;
} else {
piForeachC (Button & b, content)
w = piMaxi(w, b.first.size_s() + 4);
h += content.size_s();
h += piMaxi(0, content.size_s() - 1);
}
}
void TileButtons::drawEvent(PIScreenDrawer * d) {
int cx = x_, cy = y_, shw, shh;
sizeHint(shw, shh);
btn_rects.resize(content.size());
int dx = 0;
switch (alignment) {
case PIScreenTypes::Center: dx = (width_ - shw) / 2; break;
case PIScreenTypes::Right: dx = width_ - shw; break;
default: break;
}
if (direction == PIScreenTypes::Horizontal)
cx += dx;
for (int i = 0; i < content.size_s(); ++i) {
Color cb = Cyan;
Color ct = Black;
int ff = 0;
if (i == cur && has_focus) {
cb = Blue;
ct = White;
ff = Bold;
}
Button & b(content[i]);
int cw = b.first.size_s() + 2, xo(0);
if (direction == Vertical) {
cw = width_ - 2;
xo = (cw - b.first.size_s()) / 2 - 1;
}
btn_rects[i] = Rect(cx, cy, cx + cw + 2, cy + 1);
d->fillRect(cx, cy, cx + cw + 2, cy + 1, ' ', Default, cb);
d->drawText(cx, cy, "[", ct, Transparent, ff);
d->drawText(cx + xo + 2, cy, b.first, ct, Transparent, ff);
d->drawText(cx + cw + 1, cy, "]", ct, Transparent, ff);
if (direction == Horizontal)
cx += b.first.size_s() + 6;
else
cy += 2;
}
}
bool TileButtons::keyEvent(PIKbdListener::KeyEvent key) {
switch (key.key) {
case PIKbdListener::LeftArrow:
case PIKbdListener::UpArrow:
cur--;
if (cur < 0) cur = 0;
return true;
case PIKbdListener::RightArrow:
case PIKbdListener::DownArrow:
cur++;
if (cur >= content.size_s()) cur = content.size_s() - 1;
return true;
case PIKbdListener::Space:
case PIKbdListener::Return:
raiseEvent(TileEvent(ButtonSelected, cur));
return true;
};
return PIScreenTile::keyEvent(key);
}
bool TileButtons::mouseEvent(PIKbdListener::MouseEvent me) {
if (me.action == PIKbdListener::MouseMove || me.action == PIKbdListener::MouseButtonPress) {
for (int i = 0; i < btn_rects.size_s(); ++i)
if (me.x >= btn_rects[i].x0 && me.x < btn_rects[i].x1 &&
me.y >= btn_rects[i].y0 && me.y < btn_rects[i].y1) {
cur = i;
break;
}
return true;
}
if (me.action != PIKbdListener::MouseButtonRelease) return true;
keyEvent(PIKbdListener::KeyEvent(PIKbdListener::Return));
return true;
}
TileCheck::TileCheck(const PIString & n): PIScreenTile(n) {
focus_flags = CanHasFocus | NextByTab | NextByArrowsAll | FocusOnMouse;
toggled = false;
}
void TileCheck::sizeHint(int & w, int & h) const {
w = text.size_s() + 4;
h = 1;
}
void TileCheck::drawEvent(PIScreenDrawer * d) {
Color cb = has_focus ? Blue : Cyan;
Color ct = has_focus ? White : Black;
int ff = has_focus ? Bold : 0;
PIString cs("[ ]");
if (toggled) cs[1] = '*';
d->fillRect(x_, y_, x_ + width_, y_ + 1, ' ', Default, cb);
d->drawText(x_, y_, cs, ct, Transparent, ff);
d->drawText(x_ + 4, y_, text, ct, Transparent, ff);
}
bool TileCheck::keyEvent(PIKbdListener::KeyEvent key) {
if (key.key == PIKbdListener::Space || key.key == PIKbdListener::Return) {
toggled = !toggled;
raiseEvent(TileEvent(Toggled, toggled));
return true;
}
return PIScreenTile::keyEvent(key);
}
bool TileCheck::mouseEvent(PIKbdListener::MouseEvent me) {
if (me.action == PIKbdListener::MouseButtonPress) {
keyEvent(PIKbdListener::KeyEvent(PIKbdListener::Return));
return true;
}
return false;
}
TileProgress::TileProgress(const PIString & n): PIScreenTile(n) {
maximum = 100.;
value = 0.;
suffix = " %";
}
void TileProgress::sizeHint(int & w, int & h) const {
w = 20;
h = 1;
}
void TileProgress::drawEvent(PIScreenDrawer * d) {
int v = maximum == 0. ? 0 : piClampd(piRoundd(value / maximum * 100.), 0, 100);
PIString s = prefix + PIString::fromNumber(piRoundd(value)) + suffix;
int w = piRoundd(v / 100. * width_), sx = (width_ - s.size_s()) / 2;
d->fillRect(x_, y_, x_ + width_, y_ + 1, ' ', Default, Cyan);
d->fillRect(x_, y_, x_ + w, y_ + 1, ' ', Default, Blue);
if (w < sx)
d->drawText(x_ + sx, y_, s, Black, Transparent);
else if (w >= sx + s.size_s())
d->drawText(x_ + sx, y_, s, White, Transparent);
else {
int fw = w - sx;
d->drawText(x_ + sx, y_, s.left(fw), White, Transparent);
d->drawText(x_ + sx + fw, y_, s.cutLeft(fw), Black, Transparent);
}
}
TilePICout::TilePICout(const PIString & n): TileList(n) {
max_lines = 1024;
selection_mode = TileList::SingleSelection;
PICout::setOutputDevices(PICout::Buffer);
PICout::setBufferActive(true);
}
void TilePICout::drawEvent(PIScreenDrawer * d) {
PIString out = PICout::buffer(true);
if (!out.isEmpty()) {
PIStringList l = out.split("\n");
bool scroll = (cur == content.size_s() - 1) || !has_focus;
piForeachC (PIString & s, l)
content << TileList::Row(s.trimmed(), format);
if (content.size_s() > max_lines)
content.remove(0, content.size_s() - max_lines);
if (scroll) {
offset = piMaxi(0, content.size_s() - lhei);
cur = content.size_s() - 1;
}
}
TileList::drawEvent(d);
}
bool TilePICout::keyEvent(PIKbdListener::KeyEvent key) {
if (key.key == 'C') {
content.clear();
cur = offset = 0;
return true;
}
return TileList::keyEvent(key);
}
TileInput::TileInput(const PIString & n): PIScreenTile(n) {
focus_flags = CanHasFocus | NextByTab | FocusOnMouse;
back_format.color_back = White;
format.color_char = Black;
format.color_back = White;
max_length = 1024;
cur = offset = 0;
inv = false;
}
void TileInput::sizeHint(int & w, int & h) const {
w = 32;
h = 1;
}
void TileInput::drawEvent(PIScreenDrawer * d) {
PIString ps = text.mid(offset, width_ - 2);
d->drawText(x_ + 1, y_, ps, (Color)format.color_char, Transparent, (CharFlags)format.flags);
if (offset > 0)
d->drawText(x_, y_, "<", Green, Black, Bold);
if (text.size_s() - offset >= width_ - 2)
d->drawText(x_ + width_ - 1, y_, ">", Green, Black, Bold);
if (!has_focus) return;
Color cb = (Color)format.color_char, cc = (Color)format.color_back;
if (tm_blink.elapsed_m() >= 650) {
tm_blink.reset();
inv = !inv;
}
if (inv) piSwap(cb, cc);
d->drawText(x_ + 1 + cur - offset, y_, text.mid(cur, 1).expandLeftTo(1, ' '), cc, cb, (CharFlags)format.flags);
}
bool TileInput::keyEvent(PIKbdListener::KeyEvent key) {
int lwid = piMaxi(0, width_ - 2);
int oo(0), osp = piMini(3, lwid / 4);
lwid--;
switch (key.key) {
case PIKbdListener::LeftArrow:
cur--;
oo--;
if (key.modifiers[PIKbdListener::Ctrl]) {
cur -= 4;
oo -= 4;
}
if (cur < 0) cur = 0;
if (cur - offset < osp) offset += oo;
if (offset < 0) offset = 0;
reserCursor();
return true;
case PIKbdListener::RightArrow:
cur++;
oo++;
if (key.modifiers[PIKbdListener::Ctrl]) {
cur += 4;
oo += 4;
}
if (cur > text.size_s()) cur = text.size_s();
if (cur - offset >= lwid - osp) offset += oo;
if (offset >= text.size_s() - lwid) offset = text.size_s() - lwid;
if (offset < 0) offset = 0;
reserCursor();
return true;
case PIKbdListener::Home:
cur = offset = 0;
reserCursor();
return true;
case PIKbdListener::End:
cur = text.size_s();
offset = text.size_s() - lwid;
if (offset < 0) offset = 0;
reserCursor();
return true;
case PIKbdListener::Backspace:
if (cur > text.size_s() || text.isEmpty())
return true;
text.remove(cur - 1, 1);
cur--;
if (cur > text.size_s()) cur = text.size_s();
if (cur - offset >= lwid - osp) offset += oo;
if (offset >= text.size_s() - lwid) offset = text.size_s() - lwid;
if (offset < 0) offset = 0;
reserCursor();
return true;
case PIKbdListener::Delete:
if (cur >= text.size_s() || text.isEmpty())
return true;
text.remove(cur, 1);
if (cur < 0) cur = 0;
if (cur > text.size_s()) cur = text.size_s();
if (cur - offset < osp) offset += oo;
if (offset < 0) offset = 0;
reserCursor();
return true;
case PIKbdListener::UpArrow:
case PIKbdListener::DownArrow:
case PIKbdListener::PageUp:
case PIKbdListener::PageDown:
case PIKbdListener::Insert:
case PIKbdListener::Return:
case PIKbdListener::Esc:
case PIKbdListener::F1:
case PIKbdListener::F2:
case PIKbdListener::F3:
case PIKbdListener::F4:
case PIKbdListener::F5:
case PIKbdListener::F6:
case PIKbdListener::F7:
case PIKbdListener::F8:
case PIKbdListener::F9:
case PIKbdListener::F10:
case PIKbdListener::F11:
case PIKbdListener::F12:
break;
default:
PIChar tc
#ifdef WINDOWS
= PIChar(key.key);
#else
= PIChar::fromUTF8((char *)&(key.key));
#endif
text.insert(cur, tc);
cur++;
oo++;
if (cur - offset >= lwid - osp) offset += oo;
if (offset >= text.size_s() - lwid) offset = text.size_s() - lwid;
if (offset < 0) offset = 0;
reserCursor();
return true;
}
return PIScreenTile::keyEvent(key);
}
void TileInput::reserCursor() {
tm_blink.reset();
inv = false;
}

920
libs/console/piterminal.cpp Normal file
View File

@@ -0,0 +1,920 @@
/*
PIP - Platform Independent Primitives
Virtual terminal
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piincludes_p.h"
#include "piterminal.h"
#include "pisharedmemory.h"
#ifndef FREERTOS
#ifdef WINDOWS
# include <windows.h>
# include <wincon.h>
# include <winuser.h>
#else
# include "piprocess.h"
# include <csignal>
# include <fcntl.h>
# include <sys/ioctl.h>
# if defined(QNX) || defined(BLACKBERRY)
# include <unix.h>
# else
# ifdef MAC_OS
# include <util.h>
# else
# include <pty.h>
# endif
# endif
# ifdef ANDROID
# if __ANDROID_API__ >= 23
# define HAS_FORKPTY
# endif
# else
# define HAS_FORKPTY
# endif
#endif
//extern PIMutex PICout::__mutex__;
#ifdef WINDOWS
# define PIPE_BUFFER_SIZE 1024
enum PITerminalAuxMessageType {
mtKey = 1,
mtResize,
mtScroll
};
struct PITerminalAuxData {
int cursor_x;
int cursor_y;
int size_x;
int size_y;
int cells_size;
};
#else
# define BUFFER_SIZE 4096
enum DECType {
CKM = 1
};
#endif
PRIVATE_DEFINITION_START(PITerminal)
#ifdef WINDOWS
PISharedMemory * shm;
HANDLE hConBuf;
STARTUPINFOA si;
PROCESS_INFORMATION pi;
HANDLE pipe;
#else
PIString shell;
PIByteArray read_buf, tmp_buf;
PIScreenTypes::CellFormat cur_format, line_format;
int term_type;
int fd, cur_x, cur_y;
int save_cur_x, save_cur_y;
int win_y0, win_y1;
pid_t pid;
PIString esc_seq;
bool is_esc_seq, last_read;
PIMap<int, bool> DEC;
PIVector<PIVector<PIScreenTypes::Cell> > cells_save;
#endif
PRIVATE_DEFINITION_END(PITerminal)
#ifdef WINDOWS
int writePipe(HANDLE pipe, const PIByteArray & ba) {
DWORD wrote[2];
int sz = ba.size_s();
WriteFile(pipe, &sz, 4, &(wrote[0]), 0);
WriteFile(pipe, ba.data(), ba.size_s(), &(wrote[1]), 0);
//piCout << "send" << ba.size_s();
return int(wrote[0] + wrote[1]);
}
#endif
PITerminal::PITerminal(): PIThread() {
setName("terminal");
initPrivate();
cursor_blink = false;
cursor_x = cursor_y = 0;
dsize_x = 80;
dsize_y = 24;
#ifdef WINDOWS
PRIVATE->shm = 0;
#endif
}
PITerminal::~PITerminal() {
if (isRunning())
stop();
PIThread::waitForFinish(10);
destroy();
#ifdef WINDOWS
if (PRIVATE->shm) delete PRIVATE->shm;
#endif
}
void PITerminal::write(const PIByteArray & d) {
#ifdef WINDOWS
PIByteArray msg;
PIVector<PIKbdListener::KeyEvent> ke;
for (int i = 0; i < d.size_s(); ++i)
ke << PIKbdListener::KeyEvent(d[i]);
msg << int(mtKey) << ke;
writePipe(PRIVATE->pipe, msg);
#else
# ifdef HAS_FORKPTY
if (PRIVATE->fd == 0) return;
//ssize_t wrote = 0;
//wrote =
::write(PRIVATE->fd, d.data(), d.size_s());
//piCout << "wrote" << wrote << d;
# endif
#endif
cursor_tm.reset();
cursor_blink = true;
}
void PITerminal::write(PIKbdListener::SpecialKey k, PIKbdListener::KeyModifiers m) {
PIByteArray ba;
#ifdef WINDOWS
switch (k) {
case PIKbdListener::Tab: ba << uchar('\t'); break;
case PIKbdListener::Return: ba << uchar('\r') << uchar('\n'); break;
case PIKbdListener::Space: ba << uchar(' '); break;
default: break;
}
//piCout << "write" << ba.size();
if (!ba.isEmpty()) write(ba);
else {
PIByteArray msg;
PIVector<PIKbdListener::KeyEvent> ke;
ke << PIKbdListener::KeyEvent(k, m);
msg << int(mtKey) << ke;
writePipe(PRIVATE->pipe, msg);
}
#else
int term = PRIVATE->term_type;
int flags = 0;
switch (k) {
case PIKbdListener::Tab: ba << uchar('\t'); break;
case PIKbdListener::Return: ba << uchar('\n'); break;
case PIKbdListener::Esc: ba << uchar('\e'); break;
case PIKbdListener::Space: ba << uchar(' '); break;
case PIKbdListener::Backspace: ba << uchar(0x7f); break;
case PIKbdListener::UpArrow:
case PIKbdListener::DownArrow:
case PIKbdListener::RightArrow:
case PIKbdListener::LeftArrow: if (PRIVATE->DEC.value(CKM, false)) flags = 1;
/*case PIKbdListener::Home: //break;
case PIKbdListener::End: //break;
case PIKbdListener::PageUp: //ba << uchar('\e') << uchar('[') << uchar('5') << uchar('~'); break;
case PIKbdListener::PageDown: //ba << uchar('\e') << uchar('[') << uchar('6') << uchar('~'); break;
case PIKbdListener::Insert: //ba << uchar('\e') << uchar('[') << uchar('2') << uchar('~'); break;
case PIKbdListener::Delete: //ba << uchar('\e') << uchar('[') << uchar('3') << uchar('~'); break;
case PIKbdListener::F1: //break;
case PIKbdListener::F2: //break;
case PIKbdListener::F3: //break;
case PIKbdListener::F4: //break;
case PIKbdListener::F5: //break;
case PIKbdListener::F6: //break;
case PIKbdListener::F7: //break;
case PIKbdListener::F8: //break;
case PIKbdListener::F9: //break;
case PIKbdListener::F10: //break;
case PIKbdListener::F11: //break;
case PIKbdListener::F12: //break;
*/
default: {
//piCout << flags;
//int mod = 0;
if (m[PIKbdListener::Shift]) m |= 1;
if (m[PIKbdListener::Alt]) m |= 2;
if (m[PIKbdListener::Ctrl]) m |= 4;
for (int i = 0; ; ++i) {
const PIKbdListener::EscSeq & e(PIKbdListener::esc_seq[i]);
if (!e.seq) break;
//piCout << "search" << rc[1] << esc_seq[i].seq;
if (e.key == k && e.mod == m) {
if (((e.vt & term) == term) || (((e.flags & flags) == flags) && (flags != 0))) {
//piCout << "found key" << PIString(e.seq).replaceAll("\e", "\\e");
PIByteArray d = ("\e" + PIString(e.seq)).toByteArray();
write(d);
break;
}
}
}
} break;
}
//piCout << "write" << ba.size();
if (!ba.isEmpty()) write(ba);
#endif
cursor_tm.reset();
cursor_blink = true;
}
void PITerminal::write(PIKbdListener::KeyEvent ke) {
if (isSpecialKey(ke.key)) write((PIKbdListener::SpecialKey)ke.key, ke.modifiers);
else {
PIByteArray ba;
#ifdef WINDOWS
ba << uchar(PIChar(ke.key).toConsole1Byte());
#else
ba = PIString(PIChar(ke.key)).toUTF8();
#endif
write(ba);
}
}
PIVector<PIVector<PIScreenTypes::Cell> > PITerminal::content() {
readConsole();
PIVector<PIVector<PIScreenTypes::Cell> > ret = cells;
if (cursor_blink && cursor_visible)
if (cursor_x >= 0 && cursor_x < size_x)
if (cursor_y >= 0 && cursor_y < size_y)
ret[cursor_y][cursor_x].format.flags ^= PIScreenTypes::Inverse;
return ret;
}
bool PITerminal::isSpecialKey(int k) {
switch (k) {
case PIKbdListener::Tab:
case PIKbdListener::Return:
case PIKbdListener::Esc:
case PIKbdListener::Space:
case PIKbdListener::Backspace:
case PIKbdListener::UpArrow:
case PIKbdListener::DownArrow:
case PIKbdListener::RightArrow:
case PIKbdListener::LeftArrow:
case PIKbdListener::Home:
case PIKbdListener::End:
case PIKbdListener::PageUp:
case PIKbdListener::PageDown:
case PIKbdListener::Insert:
case PIKbdListener::Delete:
case PIKbdListener::F1:
case PIKbdListener::F2:
case PIKbdListener::F3:
case PIKbdListener::F4:
case PIKbdListener::F5:
case PIKbdListener::F6:
case PIKbdListener::F7:
case PIKbdListener::F8:
case PIKbdListener::F9:
case PIKbdListener::F10:
case PIKbdListener::F11:
case PIKbdListener::F12: return true;
default: return false;
}
return false;
}
void PITerminal::initPrivate() {
#ifdef WINDOWS
PRIVATE->hConBuf = INVALID_HANDLE_VALUE;
PRIVATE->pipe = INVALID_HANDLE_VALUE;
PRIVATE->pi.hProcess = 0;
#else
PRIVATE->shell = "/bin/bash";
PRIVATE->read_buf.reserve(BUFFER_SIZE);
PRIVATE->read_buf.fill(0);
PRIVATE->fd = PRIVATE->cur_x = PRIVATE->cur_y = 0;
PRIVATE->save_cur_x = PRIVATE->save_cur_y = 0;
PRIVATE->pid = 0;
PRIVATE->term_type = 0;
PRIVATE->is_esc_seq = false;
PRIVATE->last_read = true;
PRIVATE->esc_seq.clear();
PRIVATE->cur_format = PIScreenTypes::CellFormat();
#endif
cursor_blink = cursor_visible = true;
size_x = size_y = 0;
}
void PITerminal::readConsole() {
#ifdef WINDOWS
if (!PRIVATE->shm) return;
PITerminalAuxData data;
PRIVATE->shm->read(&data, sizeof(data));
if (data.cells_size <= 4) return;
cursor_x = data.cursor_x;
cursor_y = data.cursor_y;
size_x = data.size_x;
size_y = data.size_y;
PIByteArray ba;
ba.resize(data.cells_size);
PRIVATE->shm->read(ba.data(), ba.size_s(), sizeof(data));
ba >> cells;
#endif
//piCout << cursor_x << cursor_y;
}
void PITerminal::getCursor(int & x, int & y) {
#ifdef WINDOWS
if (!PRIVATE->shm) return;
int sz = 0;
PRIVATE->shm->read(&sz, 4);
#else
x = PRIVATE->cur_x;
y = PRIVATE->cur_y;
#endif
}
uchar PITerminal::invertColor(uchar c) {
switch ((PIScreenTypes::Color)c) {
case PIScreenTypes::Black: return PIScreenTypes::White;
case PIScreenTypes::Red: return PIScreenTypes::Cyan;
case PIScreenTypes::Green: return PIScreenTypes::Magenta;
case PIScreenTypes::Blue: return PIScreenTypes::Yellow;
case PIScreenTypes::Cyan: return PIScreenTypes::Red;
case PIScreenTypes::Magenta: return PIScreenTypes::Green;
case PIScreenTypes::Yellow: return PIScreenTypes::Blue;
case PIScreenTypes::White: return PIScreenTypes::Black;
default: break;
}
return PIScreenTypes::White;
}
void PITerminal::run() {
getCursor(cursor_x, cursor_y);
if (cursor_tm.elapsed_m() >= 500) {
cursor_tm.reset();
cursor_blink = !cursor_blink;
}
#ifndef WINDOWS
# ifdef HAS_FORKPTY
if (PRIVATE->fd == 0) return;
PRIVATE->tmp_buf.resize(BUFFER_SIZE);
int readed = ::read(PRIVATE->fd, PRIVATE->tmp_buf.data(), BUFFER_SIZE - PRIVATE->read_buf.size_s());
bool used = false;
if (readed > 0) {
PRIVATE->last_read = true;
//piCoutObj << "readed" << readed << PIString(PRIVATE->tmp_buf.resized(readed)).replaceAll("\e", "\\e");
//piCoutObj << "readed" << readed << (PRIVATE->tmp_buf.resized(readed));
PRIVATE->read_buf.append(PRIVATE->tmp_buf.resized(readed));
for (;;) {
int ind = -1;
for (int i = PRIVATE->read_buf.size_s() - 1; i >= 0; --i)
if (PRIVATE->read_buf[i] == uchar('\n') || PRIVATE->read_buf[i] == uchar('\e')) {
ind = i;
break;
}
if (ind <= 0) break;
used = true;
parseInput(PIString((const char *)PRIVATE->read_buf.data(), ind));
PRIVATE->read_buf.remove(0, ind);
}
bool parse = PRIVATE->read_buf.size_s() >= BUFFER_SIZE;
if (PRIVATE->read_buf.size_s() == 1)
if (PRIVATE->read_buf[0] < 0x80)
parse = true;
if (parse) {
parseInput(PIString(PRIVATE->read_buf));
PRIVATE->read_buf.clear();
}
//printf("%s", PRIVATE->read_buf.data());
}
if (!used && !PRIVATE->last_read && !PRIVATE->read_buf.isEmpty()) {
parseInput(PIString(PRIVATE->read_buf));
PRIVATE->read_buf.clear();
}
PRIVATE->last_read = false;
# endif
#endif
}
#ifndef WINDOWS
void PITerminal::parseInput(const PIString & s) {
//piCoutObj << s.replaceAll("\e", "\\e");
//printf("%s", s.data());
for (int i = 0; i < s.size_s(); ++i) {
if (s[i].unicode16Code() == 0) break;
if (PRIVATE->is_esc_seq) {
if (s[i] == '\e') {
applyEscSeq(PRIVATE->esc_seq);
PRIVATE->esc_seq.clear();
PRIVATE->is_esc_seq = true;
} else {
PRIVATE->esc_seq += s[i];
if (isCompleteEscSeq(PRIVATE->esc_seq)) {
PRIVATE->is_esc_seq = false;
applyEscSeq(PRIVATE->esc_seq);
//piCoutObj << PRIVATE->esc_seq;
}
}
} else {
if (s[i] == '\e') {
PRIVATE->esc_seq.clear();
PRIVATE->is_esc_seq = true;
} else {
if (s[i] == '\a') continue;
if (s[i] == '\b') {
moveCursor(-1, 0);
continue;
}
if (s[i] == '\r') continue;
if (s[i] == '\n') {
//piCoutObj << "new line";
for (int i = PRIVATE->cur_x; i < size_x; ++i) cells[PRIVATE->cur_y][i].format = PRIVATE->cur_format;
PRIVATE->line_format = PRIVATE->cur_format;
PRIVATE->cur_x = 0;
moveCursor(0, 1);
continue;
}
//piCoutObj << "char" << s[i] << s[i].unicode16Code() << "at" << PRIVATE->cur_x << PRIVATE->cur_y;
cells[PRIVATE->cur_y][PRIVATE->cur_x].symbol = s[i];
cells[PRIVATE->cur_y][PRIVATE->cur_x].format = PRIVATE->cur_format;
moveCursor(1, 0);
}
}
}
}
bool PITerminal::isCompleteEscSeq(const PIString & es) {
if (es.size_s() < 2) return false;
if (es.front() == ']') {
if (es.back().toAscii() == '\\' || es.back().toAscii() == '\a') return true;
} else {
if (es.back().toAscii() >= 64 && es.back().toAscii() <= 126) return true;
}
return false;
}
void PITerminal::applyEscSeq(PIString es) {
piCoutObj << es;
if (es.size_s() < 2) return;
// PIScreenTypes::Cell line_cell = PIScreenTypes::Cell(' ', PRIVATE->line_format);
PIScreenTypes::Cell def_cell = PIScreenTypes::Cell(' ', PRIVATE->cur_format);
if (es[1] == '?' && es.size_s() >= 2) {
char a = es.takeRight(1)[0].toAscii();
bool val = false;
if (a == 'l') val = false;
if (a == 'h') val = true;
int dec = es.mid(2).toInt();
piCoutObj << "DEC" << dec << val;
PRIVATE->DEC[dec] = val;
switch (dec) {
case 25: cursor_visible = val; break;
case 1049:
if (val) {
PRIVATE->save_cur_x = PRIVATE->cur_x;
PRIVATE->save_cur_y = PRIVATE->cur_y;
} else {
PRIVATE->cur_x = PRIVATE->save_cur_x;
PRIVATE->cur_y = PRIVATE->save_cur_y;
}
case 1047:
if (val) {
PRIVATE->cells_save = cells;
for (int i = 0; i < size_y; ++i) cells[i].fill(def_cell);
} else {
cells = PRIVATE->cells_save;
}
break;
}
}
if (es[0] == '[') { // CSI
if (es.back() == 'm') {
es.cutLeft(1).cutRight(1);
if (es.isEmpty()) {
PRIVATE->cur_format = PIScreenTypes::CellFormat();
return;
}
PIStringList args = es.split(";");
piForeachC (PIString & a, args) {
int av = a.toInt();
switch (av) {
case 0: PRIVATE->cur_format = PIScreenTypes::CellFormat(); break;
case 1: PRIVATE->cur_format.flags |= PIScreenTypes::Bold; break;
case 4: PRIVATE->cur_format.flags |= PIScreenTypes::Underline; break;
case 5: PRIVATE->cur_format.flags |= PIScreenTypes::Blink; break;
case 7: PRIVATE->cur_format.flags |= PIScreenTypes::Inverse; break;
default: {
bool col = false, target = false;
int cid = av % 10;
if (av >= 30 && av <= 37) {col = true; target = false;}
if (av >= 40 && av <= 47) {col = true; target = true;}
if (col) {
int cfl = 0;
switch (cid) {
case 0: cfl = PIScreenTypes::Black; break;
case 1: cfl = PIScreenTypes::Red; break;
case 2: cfl = PIScreenTypes::Green; break;
case 3: cfl = PIScreenTypes::Yellow; break;
case 4: cfl = PIScreenTypes::Blue; break;
case 5: cfl = PIScreenTypes::Magenta; break;
case 6: cfl = PIScreenTypes::Cyan; break;
case 7: cfl = PIScreenTypes::White; break;
}
if (target)
PRIVATE->cur_format.color_back = cfl;
else
PRIVATE->cur_format.color_char = cfl;
break;
}
} break;
}
}
/*if ((PRIVATE->cur_format.flags & PIScreenTypes::Inverse) == PIScreenTypes::Inverse) {
uchar t = PRIVATE->cur_format.color_char;
PRIVATE->cur_format.color_char = PRIVATE->cur_format.color_back;
PRIVATE->cur_format.color_back = t;
}*/
}
if (es.back() == 'r') {
piCoutObj << es;
es.cutLeft(1).cutRight(1);
PIStringList args = es.split(";");
args.resize(2);
int y0(0), y1(0);
if (!args[0].isEmpty()) y0 = args[0].toInt() - 1;
if (!args[1].isEmpty()) y1 = args[1].toInt() - 1;
PRIVATE->win_y0 = piClamp(y0, 0, size_y - 1);
PRIVATE->win_y1 = piClamp(y1, 0, size_y - 1);
}
if (es.back() == 's') {
PRIVATE->save_cur_x = PRIVATE->cur_x;
PRIVATE->save_cur_y = PRIVATE->cur_y;
}
if (es.back() == 'u') {
PRIVATE->cur_x = PRIVATE->save_cur_x;
PRIVATE->cur_y = PRIVATE->save_cur_y;
}
if (es.back() == 'H' || es.back() == 'f' || es.back() == 'r') {
es.cutLeft(1).cutRight(1);
PIStringList args = es.split(";");
args.resize(2);
int x(0), y(0);
if (!args[0].isEmpty()) y = args[0].toInt() - 1;
if (!args[1].isEmpty()) x = args[1].toInt() - 1;
//piCoutObj << x << y;
PRIVATE->cur_x = piClamp(x, 0, size_x - 1);
PRIVATE->cur_y = piClamp(y, 0, size_y - 1);
PRIVATE->line_format = PRIVATE->cur_format;
}
if (es.back() == 'A') { // cursor up
es.cutLeft(1).cutRight(1);
int v = es.toInt(); if (v == 0) v = 1;
PRIVATE->cur_y = piClamp(PRIVATE->cur_y - v, 0, size_y - 1);
PRIVATE->line_format = PRIVATE->cur_format;
}
if (es.back() == 'B') { // cursor down
es.cutLeft(1).cutRight(1);
int v = es.toInt(); if (v == 0) v = 1;
PRIVATE->cur_y = piClamp(PRIVATE->cur_y + v, 0, size_y - 1);
PRIVATE->line_format = PRIVATE->cur_format;
}
if (es.back() == 'C' || es.back() == 'a') { // cursor forward, next column
es.cutLeft(1).cutRight(1);
int v = es.toInt(); if (v == 0) v = 1;
PRIVATE->cur_x = piClamp(PRIVATE->cur_x + v, 0, size_x - 1);
PRIVATE->line_format = PRIVATE->cur_format;
}
if (es.back() == 'D') { // cursor back
es.cutLeft(1).cutRight(1);
int v = es.toInt(); if (v == 0) v = 1;
PRIVATE->cur_x = piClamp(PRIVATE->cur_x - v, 0, size_x - 1);
PRIVATE->line_format = PRIVATE->cur_format;
}
if (es.back() == 'G' || es.back() == '`') { // goto column
es.cutLeft(1).cutRight(1);
int v = es.toInt();
if (v) PRIVATE->cur_x = piClamp(v - 1, 0, size_x - 1);
PRIVATE->line_format = PRIVATE->cur_format;
}
if (es.back() == 'd') { // goto line
es.cutLeft(1).cutRight(1);
int v = es.toInt(); if (v == 0) v = 1;
PRIVATE->cur_x = 0;
PRIVATE->cur_y = piClamp(v - 1, 0, size_y - 1);
PRIVATE->line_format = PRIVATE->cur_format;
}
if (es.back() == 'E' || es.back() == 'e') { // next line
es.cutLeft(1).cutRight(1);
int v = es.toInt(); if (v == 0) v = 1;
PRIVATE->cur_x = 0;
PRIVATE->cur_y = piClamp(PRIVATE->cur_y + v, 0, size_y - 1);
PRIVATE->line_format = PRIVATE->cur_format;
}
if (es.back() == 'F') { // previous line
es.cutLeft(1).cutRight(1);
int v = es.toInt(); if (v == 0) v = 1;
PRIVATE->cur_x = 0;
PRIVATE->cur_y = piClamp(PRIVATE->cur_y - v, 0, size_y - 1);
PRIVATE->line_format = PRIVATE->cur_format;
}
if (es.back() == 'L') { // insert lines
es.cutLeft(1).cutRight(1);
int v = es.toInt(); if (v == 0) v = 1;
for (int i = piClamp(size_y - 1, PRIVATE->win_y0, PRIVATE->win_y1); i >= piClamp(PRIVATE->cur_y + v, PRIVATE->win_y0, PRIVATE->win_y1); --i) cells[i] = cells[i - v];
for (int j = piClamp(PRIVATE->cur_y, PRIVATE->win_y0, PRIVATE->win_y1); j < piClamp(PRIVATE->cur_y + v, PRIVATE->win_y0, PRIVATE->win_y1); ++j)
for (int i = 0; i < PRIVATE->cur_x; ++i) cells[j][i] = def_cell;
}
if (es.back() == 'M') { // delete lines
es.cutLeft(1).cutRight(1);
int v = es.toInt(); if (v == 0) v = 1;
for (int i = piClamp(PRIVATE->cur_y, PRIVATE->win_y0, PRIVATE->win_y1); i < piClamp(size_y - v, PRIVATE->win_y0, PRIVATE->win_y1); ++i) cells[i] = cells[i + v];
for (int j = piClamp(size_y - v, PRIVATE->win_y0, PRIVATE->win_y1); j < piClamp(size_y, PRIVATE->win_y0, PRIVATE->win_y1); ++j)
for (int i = 0; i < PRIVATE->cur_x; ++i) cells[j][i] = def_cell;
}
if (es.back() == 'P') { // delete characters
es.cutLeft(1).cutRight(1);
int v = es.toInt(); if (v == 0) v = 1;
for (int i = PRIVATE->cur_x; i < size_x - v; ++i) cells[PRIVATE->cur_y][i] = cells[PRIVATE->cur_y][i + v];
for (int i = size_x - v; i < size_x; ++i) cells[PRIVATE->cur_y][i] = def_cell;
}
if (es.back() == '@') { // delete characters
es.cutLeft(1).cutRight(1);
int v = es.toInt(); if (v == 0) v = 1;
for (int i = size_x - 1; i >= PRIVATE->cur_x + v; --i) cells[PRIVATE->cur_y][i] = cells[PRIVATE->cur_y][i - v];
for (int i = PRIVATE->cur_x; i < PRIVATE->cur_x + v; ++i) cells[PRIVATE->cur_y][i] = def_cell;
}
if (es.back() == 'J') { // erase data
es.cutLeft(1).cutRight(1);
int v = es.toInt();
switch (v) {
case 0:
for (int i = PRIVATE->cur_x; i < size_x; ++i) cells[PRIVATE->cur_y][i] = def_cell;
for (int i = PRIVATE->cur_y + 1; i < size_y; ++i) cells[i].fill(def_cell);
break;
case 1:
for (int i = 0; i <= PRIVATE->cur_x; ++i) cells[PRIVATE->cur_y][i] = def_cell;
for (int i = 0; i < PRIVATE->cur_y; ++i) cells[i].fill(def_cell);
break;
case 2:
for (int i = 0; i < size_y; ++i) cells[i].fill(def_cell);
//PRIVATE->cur_x = PRIVATE->cur_y = 0;
break;
}
}
if (es.back() == 'K') { // erase in line
es.cutLeft(1).cutRight(1);
int v = es.toInt();
switch (v) {
case 0:
for (int i = PRIVATE->cur_x; i < size_x; ++i) cells[PRIVATE->cur_y][i] = def_cell;
break;
case 1:
for (int i = 0; i <= PRIVATE->cur_x; ++i) cells[PRIVATE->cur_y][i] = def_cell;
break;
case 2:
cells[PRIVATE->cur_y].fill(def_cell);
break;
}
}
}
}
void PITerminal::moveCursor(int dx, int dy) {
PRIVATE->cur_x += dx;
PRIVATE->cur_y += dy;
if (PRIVATE->cur_x < 0) PRIVATE->cur_x = 0;
if (PRIVATE->cur_y < 0) PRIVATE->cur_y = 0;
if (PRIVATE->cur_x >= size_x) {
PRIVATE->line_format = PRIVATE->cur_format;
PRIVATE->cur_x = 0;
PRIVATE->cur_y++;
}
if (PRIVATE->cur_y >= size_y) {
int scroll = piMini(PRIVATE->cur_y - size_y + 1, size_y - 1);
//piCout << "scroll" << size_x << size_y << size_y - scroll - 1;
PRIVATE->cur_y = size_y - 1;
for (int y = 0; y < size_y - scroll; ++y)
cells[y] = cells[y + scroll];
for (int y = size_y - scroll; y < size_y; ++y)
cells[y].fill(PIScreenTypes::Cell());
}
}
int PITerminal::termType(const PIString & t) {
if (t == "xterm") return PIKbdListener::vt_xterm;
else if (t == "linux") return PIKbdListener::vt_linux;
return PIKbdListener::vt_none;
}
#endif
bool PITerminal::initialize() {
destroy();
#ifdef WINDOWS
/*SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = true;
sa.lpSecurityDescriptor = 0;
if (!CreatePipe(&(PRIVATE->pipe_in[0]), &(PRIVATE->pipe_in[1]), &sa, 0)) {
piCoutObj << "CreatePipe error," << errorString();
initPrivate();
return false;
}
PRIVATE->hConBuf = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, &sa, CONSOLE_TEXTMODE_BUFFER, 0);
if (PRIVATE->hConBuf == INVALID_HANDLE_VALUE) {
piCoutObj << "CreateConsoleScreenBuffer error," << errorString();
destroy();
return false;
}*/
//CreatePipe(&(PRIVATE->pipe_out[0]), &(PRIVATE->pipe_out[1]), &sa, 0);
//SetHandleInformation(PRIVATE->pipe_in[1], HANDLE_FLAG_INHERIT, 0);
//SetHandleInformation(PRIVATE->hConBuf, HANDLE_FLAG_INHERIT, 0);
//GetStartupInfoA(&PRIVATE->si);
memset(&PRIVATE->si, 0, sizeof(PRIVATE->si));
PRIVATE->si.cb = sizeof(STARTUPINFO);
//PRIVATE->si.dwFlags |= STARTF_USESTDHANDLES;
PRIVATE->si.dwFlags |= STARTF_USESHOWWINDOW;
PRIVATE->si.dwFlags |= STARTF_USECOUNTCHARS;
//PRIVATE->si.hStdInput = PRIVATE->pipe;
//PRIVATE->si.hStdOutput = PRIVATE->hConBuf;
//PRIVATE->si.hStdError = PRIVATE->hConBuf;
PRIVATE->si.wShowWindow = SW_HIDE;
PRIVATE->si.dwXCountChars = 80;
PRIVATE->si.dwYCountChars = 24;
memset(&PRIVATE->pi, 0, sizeof(PRIVATE->pi));
PIString shmh = PIString::fromNumber(randomi() % 10000);
PIString pname = "\\\\.\\pipe\\piterm" + shmh;
PIString cmd = "piterminal \"" + shmh + "\" \"" + pname + "\"";
if(!CreateProcessA(0, (LPSTR)cmd.dataAscii(), 0, 0, false, CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP, 0, 0, &PRIVATE->si, &PRIVATE->pi)) {
piCoutObj << "CreateProcess error," << errorString();
destroy();
return false;
}
PRIVATE->pipe = CreateNamedPipe((LPSTR)pname.dataAscii(), PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, 2, PIPE_BUFFER_SIZE, PIPE_BUFFER_SIZE, 1000, NULL);
if (PRIVATE->pipe == INVALID_HANDLE_VALUE) {
piCoutObj << "CreateNamedPipe error," << errorString();
destroy();
return false;
}
PITimeMeasurer tm;
bool ok = false;
while (tm.elapsed_m() < 1000) {
if (ConnectNamedPipe(PRIVATE->pipe, 0) == TRUE) {
ok = true;
break;
}
}
if (!ok) {
piCoutObj << "ConnectNamedPipe error," << errorString();
destroy();
return false;
}
if (PRIVATE->shm) delete PRIVATE->shm;
PRIVATE->shm = new PISharedMemory("piterm_aux" + shmh, 1024*1024);
CloseHandle(PRIVATE->pi.hThread);
resize(dsize_x, dsize_y);
#else
# ifdef HAS_FORKPTY
char pty[256]; memset(pty, 0, 256);
winsize ws;
ws.ws_col = dsize_x;
ws.ws_row = dsize_y;
PIStringList env = PIProcess::currentEnvironment();
piForeachC (PIString & e, env)
if (e.startsWith("TERM=")) {
PRIVATE->term_type = termType(e.mid(5).trim().toLowerCase());
//piCout << PRIVATE->term_type;
break;
}
pid_t fr = forkpty(&(PRIVATE->fd), pty, 0, &ws);
//piCoutObj << fr << PRIVATE->fd << pty;
if (fr == 0) {
char ** argv = new char*[2];
argv[0] = new char[PRIVATE->shell.lengthAscii() + 1];
memcpy(argv[0], PRIVATE->shell.dataAscii(), PRIVATE->shell.lengthAscii());
argv[0][PRIVATE->shell.lengthAscii()] = 0;
argv[1] = 0;
execvp(PRIVATE->shell.dataAscii(), argv);
delete[] argv[0];
delete[] argv;
exit(0);
} else {
if (fr < 0 || PRIVATE->fd < 0) {
piCoutObj << "forkpty error," << errorString();
initPrivate();
return false;
}
PRIVATE->pid = fr;
fcntl(PRIVATE->fd, F_SETFL, O_NONBLOCK);
/*
tcgetattr(PRIVATE->fd, &PRIVATE->desc);
PRIVATE->desc.c_oflag = PRIVATE->desc.c_lflag = PRIVATE->desc.c_cflag = 0;
PRIVATE->desc.c_iflag = IGNBRK;
PRIVATE->desc.c_cflag = CLOCAL | HUPCL;
PRIVATE->desc.c_cflag |= (CSIZE & CS8);
PRIVATE->desc.c_cflag |= CREAD;
PRIVATE->desc.c_cc[VMIN] = 1;
PRIVATE->desc.c_cc[VTIME] = 1;
cfsetispeed(&PRIVATE->desc, B38400);
cfsetospeed(&PRIVATE->desc, B38400);
if (tcsetattr(PRIVATE->fd, TCSANOW, &PRIVATE->desc) < 0) {
piCoutObj << "Can`t set attributes for \"" << pty << "\"";
destroy();
return false;
}
*/
size_x = dsize_x;
size_y = dsize_y;
resize(size_x, size_y);
}
# endif
#endif
cursor_blink = false;
cursor_tm.reset();
start(40);
return true;
}
void PITerminal::destroy() {
//piCout << "destroy ...";
stop();
waitForFinish(1000);
#ifdef WINDOWS
if (PRIVATE->pi.hProcess) {
//piCout << "term";
//TerminateProcess(PRIVATE->pi.hProcess, 0);
GenerateConsoleCtrlEvent(CTRL_C_EVENT, PRIVATE->pi.dwProcessId);
CloseHandle(PRIVATE->pi.hProcess);
}
if (PRIVATE->pipe != INVALID_HANDLE_VALUE) CloseHandle(PRIVATE->pipe);
if (PRIVATE->hConBuf != INVALID_HANDLE_VALUE) CloseHandle(PRIVATE->hConBuf);
//piCout << "destroy" << size_y;
#else
# ifdef HAS_FORKPTY
if (PRIVATE->pid != 0)
kill(PRIVATE->pid, SIGKILL);
if (PRIVATE->fd != 0)
::close(PRIVATE->fd);
# endif
#endif
initPrivate();
}
bool PITerminal::resize(int cols, int rows) {
bool ret = true;
dsize_x = cols;
dsize_y = rows;
#ifdef WINDOWS
if (PRIVATE->pipe == INVALID_HANDLE_VALUE) return false;
PIByteArray msg;
msg << int(mtResize) << dsize_x << dsize_y;
writePipe(PRIVATE->pipe, msg);
#else
# ifdef HAS_FORKPTY
if (PRIVATE->fd == 0) return false;
size_x = dsize_x;
size_y = dsize_y;
//piCout << "resize" << PRIVATE->fd << size_x << size_y;
winsize ws;
ws.ws_col = cols;
ws.ws_row = rows;
ioctl(PRIVATE->fd, TIOCSWINSZ, &ws);
PRIVATE->win_y0 = 0;
PRIVATE->win_y1 = size_y - 1;
PRIVATE->cells_save.resize(size_y);
for (int i = 0; i < size_y; ++i)
PRIVATE->cells_save[i].resize(size_x);
# endif
#endif
cells.resize(size_y);
for (int i = 0; i < size_y; ++i)
cells[i].resize(size_x);
return ret;
}
#endif // FREERTOS