yaft: convert into c++ class

This commit is contained in:
Stefan Seyfried
2018-01-24 22:36:02 +01:00
committed by Thilo Graf
parent 2551dfd994
commit 6f821d04cb
5 changed files with 1892 additions and 0 deletions

View File

@@ -0,0 +1,415 @@
/*
* yaft framebuffer terminal as C++ class for embedding in neutrino-MP
* (C) 2018 Stefan Seyfried
* License: GPL-2.0
*
* 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.
*
* derived from yaft/ctrlseq/csi.h
* original code
* Copyright (c) 2012 haru <uobikiemukot at gmail dot com>
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*/
#include "yaft_priv.h"
#include <cstring>
/* function for csi sequence */
void YaFT_p::insert_blank(struct parm_t *parm)
{
int i, num = sum(parm);
if (num <= 0)
num = 1;
for (i = cols - 1; cursor.x <= i; i--) {
if (cursor.x <= (i - num))
copy_cell(cursor.y, i, cursor.y, i - num);
else
erase_cell(cursor.y, i);
}
}
void YaFT_p::curs_up(struct parm_t *parm)
{
int num = sum(parm);
if (num <= 0)
num = 1;
move_cursor(-num, 0);
}
void YaFT_p::curs_down(struct parm_t *parm)
{
int num = sum(parm);
if (num <= 0)
num = 1;
move_cursor(num, 0);
}
void YaFT_p::curs_forward(struct parm_t *parm)
{
int num = sum(parm);
if (num <= 0)
num = 1;
move_cursor(0, num);
}
void YaFT_p::curs_back(struct parm_t *parm)
{
int num = sum(parm);
if (num <= 0)
num = 1;
move_cursor(0, -num);
}
void YaFT_p::curs_nl(struct parm_t *parm)
{
int num = sum(parm);
if (num <= 0)
num = 1;
move_cursor(num, 0);
cr();
}
void YaFT_p::curs_pl(struct parm_t *parm)
{
int num = sum(parm);
if (num <= 0)
num = 1;
move_cursor(-num, 0);
cr();
}
void YaFT_p::curs_col(struct parm_t *parm)
{
int num;
num = (parm->argc <= 0) ? 0 : dec2num(parm->argv[parm->argc - 1]) - 1;
set_cursor(cursor.y, num);
}
void YaFT_p::curs_pos(struct parm_t *parm)
{
int line, col;
if (parm->argc <= 0) {
line = col = 0;
} else if (parm->argc == 2) {
line = dec2num(parm->argv[0]) - 1;
col = dec2num(parm->argv[1]) - 1;
} else {
return;
}
if (line < 0)
line = 0;
if (col < 0)
col = 0;
set_cursor(line, col);
}
void YaFT_p::curs_line(struct parm_t *parm)
{
int num;
num = (parm->argc <= 0) ? 0 : dec2num(parm->argv[parm->argc - 1]) - 1;
set_cursor(num, cursor.x);
}
void YaFT_p::erase_display(struct parm_t *parm)
{
int i, j, pmode;
pmode = (parm->argc <= 0) ? 0 : dec2num(parm->argv[parm->argc - 1]);
if (pmode < 0 || 2 < pmode)
return;
if (pmode == 0) {
for (i = cursor.y; i < lines; i++)
for (j = 0; j < cols; j++)
if (i > cursor.y || (i == cursor.y && j >= cursor.x))
erase_cell(i, j);
} else if (pmode == 1) {
for (i = 0; i <= cursor.y; i++)
for (j = 0; j < cols; j++)
if (i < cursor.y || (i == cursor.y && j <= cursor.x))
erase_cell(i, j);
} else if (pmode == 2) {
for (i = 0; i < lines; i++)
for (j = 0; j < cols; j++)
erase_cell(i, j);
}
}
void YaFT_p::erase_line(struct parm_t *parm)
{
int i, pmode;
pmode = (parm->argc <= 0) ? 0 : dec2num(parm->argv[parm->argc - 1]);
if (pmode < 0 || 2 < pmode)
return;
if (pmode == 0) {
for (i = cursor.x; i < cols; i++)
erase_cell(cursor.y, i);
} else if (pmode == 1) {
for (i = 0; i <= cursor.x; i++)
erase_cell(cursor.y, i);
} else if (pmode == 2) {
for (i = 0; i < cols; i++)
erase_cell(cursor.y, i);
}
}
void YaFT_p::insert_line(struct parm_t *parm)
{
int num = sum(parm);
if (mode & MODE_ORIGIN) {
if (cursor.y < scrollm.top
|| cursor.y > scrollm.bottom)
return;
}
if (num <= 0)
num = 1;
scroll(cursor.y, scrollm.bottom, -num);
}
void YaFT_p::delete_line(struct parm_t *parm)
{
int num = sum(parm);
if (mode & MODE_ORIGIN) {
if (cursor.y < scrollm.top
|| cursor.y > scrollm.bottom)
return;
}
if (num <= 0)
num = 1;
scroll(cursor.y, scrollm.bottom, num);
}
void YaFT_p::delete_char(struct parm_t *parm)
{
int i, num = sum(parm);
if (num <= 0)
num = 1;
for (i = cursor.x; i < cols; i++) {
if ((i + num) < cols)
copy_cell(cursor.y, i, cursor.y, i + num);
else
erase_cell(cursor.y, i);
}
}
void YaFT_p::erase_char(struct parm_t *parm)
{
int i, num = sum(parm);
if (num <= 0)
num = 1;
else if (num + cursor.x > cols)
num = cols - cursor.x;
for (i = cursor.x; i < cursor.x + num; i++)
erase_cell(cursor.y, i);
}
void YaFT_p::set_attr(struct parm_t *parm)
{
int i, num;
if (parm->argc <= 0) {
attribute = ATTR_RESET;
color_pair.fg = DEFAULT_FG;
color_pair.bg = DEFAULT_BG;
return;
}
for (i = 0; i < parm->argc; i++) {
num = dec2num(parm->argv[i]);
if (num == 0) { /* reset all attribute and color */
attribute = ATTR_RESET;
color_pair.fg = DEFAULT_FG;
color_pair.bg = DEFAULT_BG;
} else if (1 <= num && num <= 7) { /* set attribute */
attribute |= attr_mask[num];
} else if (21 <= num && num <= 27) { /* reset attribute */
attribute &= ~attr_mask[num - 20];
} else if (30 <= num && num <= 37) { /* set foreground */
color_pair.fg = (num - 30);
} else if (num == 38) { /* set 256 color to foreground */
if ((i + 2) < parm->argc && dec2num(parm->argv[i + 1]) == 5) {
color_pair.fg = dec2num(parm->argv[i + 2]);
i += 2;
}
} else if (num == 39) { /* reset foreground */
color_pair.fg = DEFAULT_FG;
} else if (40 <= num && num <= 47) { /* set background */
color_pair.bg = (num - 40);
} else if (num == 48) { /* set 256 color to background */
if ((i + 2) < parm->argc && dec2num(parm->argv[i + 1]) == 5) {
color_pair.bg = dec2num(parm->argv[i + 2]);
i += 2;
}
} else if (num == 49) { /* reset background */
color_pair.bg = DEFAULT_BG;
} else if (90 <= num && num <= 97) { /* set bright foreground */
color_pair.fg = (num - 90) + BRIGHT_INC;
} else if (100 <= num && num <= 107) { /* set bright background */
color_pair.bg = (num - 100) + BRIGHT_INC;
}
}
}
void YaFT_p::status_report(struct parm_t *parm)
{
int i, num;
char buf[BUFSIZE];
for (i = 0; i < parm->argc; i++) {
num = dec2num(parm->argv[i]);
if (num == 5) { /* terminal response: ready */
write(fd, "\033[0n", 4);
} else if (num == 6) { /* cursor position report */
snprintf(buf, BUFSIZE, "\033[%d;%dR", cursor.y + 1, cursor.x + 1);
write(fd, buf, strlen(buf));
} else if (num == 15) { /* terminal response: printer not connected */
write(fd, "\033[?13n", 6);
}
}
}
void YaFT_p::device_attribute(struct parm_t *parm)
{
/* TODO: refer VT525 DA */
(void) parm;
write(fd, "\033[?6c", 5); /* "I am a VT102" */
}
void YaFT_p::set_mode(struct parm_t *parm)
{
int i, pmode;
for (i = 0; i < parm->argc; i++) {
pmode = dec2num(parm->argv[i]);
if (esc.buf[1] != '?')
continue; /* not supported */
if (pmode == 6) { /* private mode */
mode |= MODE_ORIGIN;
set_cursor(0, 0);
} else if (pmode == 7) {
mode |= MODE_AMRIGHT;
} else if (pmode == 25) {
mode |= MODE_CURSOR;
} else if (pmode == 8901) {
mode |= MODE_VWBS;
}
}
}
void YaFT_p::reset_mode(struct parm_t *parm)
{
int i, pmode;
for (i = 0; i < parm->argc; i++) {
pmode = dec2num(parm->argv[i]);
if (esc.buf[1] != '?')
continue; /* not supported */
if (pmode == 6) { /* private mode */
mode &= ~MODE_ORIGIN;
set_cursor(0, 0);
} else if (pmode == 7) {
mode &= ~MODE_AMRIGHT;
wrap_occured = false;
} else if (pmode == 25) {
mode &= ~MODE_CURSOR;
} else if (pmode == 8901) {
mode &= ~MODE_VWBS;
}
}
}
void YaFT_p::set_margin(struct parm_t *parm)
{
int top, bottom;
if (parm->argc <= 0) { /* CSI r */
top = 0;
bottom = lines - 1;
} else if (parm->argc == 2) { /* CSI ; r -> use default value */
top = parm->argv[0].empty() ? 0 : dec2num(parm->argv[0]) - 1;
bottom = parm->argv[1].empty() ? lines - 1 : dec2num(parm->argv[1]) - 1;
} else {
return;
}
if (top < 0 || top >= lines)
top = 0;
if (bottom < 0 || bottom >= lines)
bottom = lines - 1;
if (top >= bottom)
return;
scrollm.top = top;
scrollm.bottom = bottom;
set_cursor(0, 0); /* move cursor to home */
}
void YaFT_p::clear_tabstop(struct parm_t *parm)
{
int i, j, num;
if (parm->argc <= 0) {
tabstop[cursor.x] = false;
} else {
for (i = 0; i < parm->argc; i++) {
num = dec2num(parm->argv[i]);
if (num == 0) {
tabstop[cursor.x] = false;
} else if (num == 3) {
for (j = 0; j < cols; j++)
tabstop[j] = false;
return;
}
}
}
}

View File

@@ -0,0 +1,127 @@
/*
* yaft framebuffer terminal as C++ class for embedding in neutrino-MP
* (C) 2018 Stefan Seyfried
* License: GPL-2.0
*
* 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.
*
* derived from yaft/ctrlseq/esc.h
* original code
* Copyright (c) 2012 haru <uobikiemukot at gmail dot com>
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*/
#include "yaft_priv.h"
/* function for control character */
void YaFT_p::bs(void)
{
if (mode & MODE_VWBS
&& cursor.x - 1 >= 0
&& cells[cursor.y][cursor.x - 1].width == NEXT_TO_WIDE)
move_cursor(0, -2);
else
move_cursor(0, -1);
}
void YaFT_p::tab(void)
{
int i;
txt.back().append(" ");
for (i = cursor.x + 1; i < cols; i++) {
if (tabstop[i]) {
set_cursor(cursor.y, i);
return;
}
}
set_cursor(cursor.y, cols - 1);
}
void YaFT_p::nl(void)
{
nlseen = true;
txt.push("");
lines_available++;
move_cursor(1, 0);
}
void YaFT_p::cr(void)
{
set_cursor(cursor.y, 0);
}
void YaFT_p::enter_esc(void)
{
esc.state = STATE_ESC;
}
/* function for escape sequence */
void YaFT_p::save_state(void)
{
state.mode = mode & MODE_ORIGIN;
state.cursor = cursor;
state.attribute = attribute;
}
void YaFT_p::restore_state(void)
{
/* restore state */
if (state.mode & MODE_ORIGIN)
mode |= MODE_ORIGIN;
else
mode &= ~MODE_ORIGIN;
cursor = state.cursor;
attribute = state.attribute;
}
void YaFT_p::crnl(void)
{
cr();
nl();
}
void YaFT_p::set_tabstop(void)
{
tabstop[cursor.x] = true;
}
void YaFT_p::reverse_nl(void)
{
move_cursor(-1, 0);
}
void YaFT_p::identify(void)
{
write(fd, "\033[?6c", 5); /* "I am a VT102" */
}
void YaFT_p::enter_csi(void)
{
esc.state = STATE_CSI;
}
void YaFT_p::enter_osc(void)
{
esc.state = STATE_OSC;
}
#if 0
void YaFT_p::enter_dcs(void)
{
esc.state = STATE_DCS;
}
#endif
void YaFT_p::ris(void)
{
reset();
}

View File

@@ -0,0 +1,211 @@
/*
* yaft framebuffer terminal as C++ class for embedding in neutrino-MP
* (C) 2018 Stefan Seyfried
* License: GPL-2.0
*
* 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.
*
* derived from yaft/ctrlseq/osc.h,
* original code
* Copyright (c) 2012 haru <uobikiemukot at gmail dot com>
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*/
#include "yaft_priv.h"
#include <cstring>
/* function for osc sequence */
int32_t YaFT_p::parse_color1(std::string &seq)
{
/*
format
rgb:r/g/b
rgb:rr/gg/bb
rgb:rrr/ggg/bbb
rgb:rrrr/gggg/bbbb
*/
int i, length, value;
int32_t color;
uint32_t rgb[3];
struct parm_t parm;
reset_parm(&parm);
parse_arg(seq, &parm, '/', isalnum);
for (i = 0; i < parm.argc; i++)
logging(DEBUG, "parm.argv[%d]: %s\n", i, parm.argv[i].c_str());
if (parm.argc != 3)
return -1;
length = parm.argv[0].length();
for (i = 0; i < 3; i++) {
value = hex2num(parm.argv[i]);
logging(DEBUG, "value:%d\n", value);
if (length == 1) /* r/g/b/ */
rgb[i] = 0xFF & (value * 0xFF / 0x0F);
else if (length == 2) /* rr/gg/bb */
rgb[i] = 0xFF & value;
else if (length == 3) /* rrr/ggg/bbb */
rgb[i] = 0xFF & (value * 0xFF / 0xFFF);
else if (length == 4) /* rrrr/gggg/bbbb */
rgb[i] = 0xFF & (value * 0xFF / 0xFFFF);
else
return -1;
}
color = (rgb[0] << 16) + (rgb[1] << 8) + rgb[2];
logging(DEBUG, "color:0x%.6X\n", color);
return color;
}
int32_t YaFT_p::parse_color2(std::string &seq)
{
/*
format
#rgb
#rrggbb
#rrrgggbbb
#rrrrggggbbbb
*/
int i, length;
uint32_t rgb[3];
int32_t color;
char buf[BUFSIZE];
length = seq.length();
memset(buf, '\0', BUFSIZE);
if (length == 3) { /* rgb */
for (i = 0; i < 3; i++) {
rgb[i] = 0xFF & hex2num(seq.substr(i, 1)) * 0xFF / 0x0F;
}
} else if (length == 6) { /* rrggbb */
for (i = 0; i < 3; i++) { /* rrggbb */
//strncpy(buf, seq + i * 2, 2);
rgb[i] = 0xFF & hex2num(seq.substr(i * 2, 2));
}
} else if (length == 9) { /* rrrgggbbb */
for (i = 0; i < 3; i++) {
//strncpy(buf, seq + i * 3, 3);
rgb[i] = 0xFF & hex2num(seq.substr(i * 3, 3)) * 0xFF / 0xFFF;
}
} else if (length == 12) { /* rrrrggggbbbb */
for (i = 0; i < 3; i++) {
//strncpy(buf, seq + i * 4, 4);
rgb[i] = 0xFF & hex2num(seq.substr(i * 4, 4)) * 0xFF / 0xFFFF;
}
} else {
return -1;
}
color = (rgb[0] << 16) + (rgb[1] << 8) + rgb[2];
logging(DEBUG, "color:0x%.6X\n", color);
return color;
}
void YaFT_p::set_palette(struct parm_t *pt)
{
/*
OSC Ps ; Pt ST
ref: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
ref: http://ttssh2.sourceforge.jp/manual/ja/about/ctrlseq.html#OSC
only recognize change color palette:
Ps: 4
Pt: c ; spec
c: color index (from 0 to 255)
spec:
rgb:r/g/b
rgb:rr/gg/bb
rgb:rrr/ggg/bbb
rgb:rrrr/gggg/bbbb
#rgb
#rrggbb
#rrrgggbbb
#rrrrggggbbbb
this rgb format is "RGB Device String Specification"
see http://xjman.dsl.gr.jp/X11R6/X11/CH06.html
Pt: c ; ?
response rgb color
OSC 4 ; c ; rgb:rr/gg/bb ST
TODO: this function only works in 32bpp mode
*/
int i, argc = pt->argc, index;
int32_t color;
uint8_t rgb[3];
std::string *argv = pt->argv;
char buf[BUFSIZE];
if (argc != 3)
return;
index = dec2num(argv[1]);
if (index < 0 || index >= COLORS)
return;
if (argv[2].compare(0, 4, "rgb:") == 0) {
std::string tmp = argv[2].substr(4); /* skip "rgb:" */
if ((color = parse_color1(tmp)) != -1) {
virtual_palette[index] = (uint32_t) color;
palette_modified = true;
}
} else if (argv[2][0] == '#') {
std::string tmp = argv[2].substr(1); /* skip "#" */
if ((color = parse_color2(tmp)) != -1) {
virtual_palette[index] = (uint32_t) color;
palette_modified = true;
}
} else if (argv[2][0] == '?') {
for (i = 0; i < 3; i++)
rgb[i] = 0xFF & (virtual_palette[index] >> (8 * (2 - i)));
snprintf(buf, BUFSIZE, "\033]4;%d;rgb:%.2X/%.2X/%.2X\033\\",
index, rgb[0], rgb[1], rgb[2]);
write(fd, buf, strlen(buf));
}
}
void YaFT_p::reset_palette(struct parm_t *pt)
{
/*
reset color c
OSC 104 ; c ST
c: index of color
ST: BEL or ESC \
reset all color
OSC 104 ST
ST: BEL or ESC \
terminfo: oc=\E]104\E\\
*/
int i, argc = pt->argc, c;
std::string *argv = pt->argv;
if (argc < 2) { /* reset all color palette */
for (i = 0; i < COLORS; i++)
virtual_palette[i] = color_list[i];
palette_modified = true;
} else if (argc == 2) { /* reset color_palette[c] */
c = dec2num(argv[1]);
if (0 <= c && c < COLORS) {
virtual_palette[c] = color_list[c];
palette_modified = true;
}
}
}