namespace TERM_REC functions (conflict with curses.h)

This commit is contained in:
Ailin Nemui 2025-04-08 08:04:21 +02:00
parent 7bac5eb762
commit 382bde9ab3
3 changed files with 148 additions and 154 deletions

View File

@ -386,14 +386,12 @@ void term_set_color2(TERM_WINDOW *window, int col, unsigned int fgcol24, unsigne
}
/* set foreground color */
if (fg != last_fg &&
(fg != 0 || (col & ATTR_RESETFG) == 0)) {
if (fg != last_fg && (fg != 0 || (col & ATTR_RESETFG) == 0)) {
if (term_use_colors) {
last_fg = fg;
if (fg >> 8)
termctl_set_color_24bit(0,
last_fg == COLOR_BLACK24 ? 0
: last_fg >> 8);
termctl_set_color_24bit(0, last_fg == COLOR_BLACK24 ? 0 :
last_fg >> 8);
else
terminfo_set_fg(last_fg);
}
@ -401,19 +399,17 @@ void term_set_color2(TERM_WINDOW *window, int col, unsigned int fgcol24, unsigne
/* set background color */
if (window && window->term->TI_colors &&
(term_color256map[bg&0xff]&8) == window->term->TI_colors)
(term_color256map[bg & 0xff] & 8) == window->term->TI_colors)
col |= ATTR_BLINK;
if (col & ATTR_BLINK)
current_term->set_blink(current_term);
current_term->tr_set_blink(current_term);
if (bg != last_bg &&
(bg != 0 || (col & ATTR_RESETBG) == 0)) {
if (bg != last_bg && (bg != 0 || (col & ATTR_RESETBG) == 0)) {
if (term_use_colors) {
last_bg = bg;
if (bg >> 8)
termctl_set_color_24bit(1,
last_bg == COLOR_BLACK24 ? 0
: last_bg >> 8);
termctl_set_color_24bit(1, last_bg == COLOR_BLACK24 ? 0 :
last_bg >> 8);
else
terminfo_set_bg(last_bg);
}
@ -425,7 +421,7 @@ void term_set_color2(TERM_WINDOW *window, int col, unsigned int fgcol24, unsigne
/* bold */
if (window && window->term->TI_colors &&
(term_color256map[fg&0xff]&8) == window->term->TI_colors)
(term_color256map[fg & 0xff] & 8) == window->term->TI_colors)
col |= ATTR_BOLD;
if (col & ATTR_BOLD)
terminfo_set_bold();
@ -445,7 +441,7 @@ void term_set_color2(TERM_WINDOW *window, int col, unsigned int fgcol24, unsigne
terminfo_set_italic(FALSE);
/* update the new attribute settings whilst ignoring color values. */
last_attrs = col & ~( BG_MASK | FG_MASK );
last_attrs = col & ~(BG_MASK | FG_MASK);
}
void term_move(TERM_WINDOW *window, int x, int y)

View File

@ -175,17 +175,17 @@ static void _scroll_region(TERM_REC *term, int y1, int y2, int count)
/* setup the scrolling region to wanted area */
scroll_region_setup(term, y1, y2);
term->move(term, 0, y1);
term->tr_move(term, 0, y1);
if (count > 0) {
term->move(term, 0, y2);
term->tr_move(term, 0, y2);
tput(tparm(term->TI_indn, count, count, 0, 0, 0, 0, 0, 0, 0));
} else if (count < 0) {
term->move(term, 0, y1);
term->tr_move(term, 0, y1);
tput(tparm(term->TI_rin, -count, -count, 0, 0, 0, 0, 0, 0, 0));
}
/* reset the scrolling region to full screen */
scroll_region_setup(term, 0, term->height-1);
scroll_region_setup(term, 0, term->height - 1);
}
/* Scroll (change_scroll_region+scroll_reverse+scroll_forward / csr+ri+ind) */
@ -197,17 +197,17 @@ static void _scroll_region_1(TERM_REC *term, int y1, int y2, int count)
scroll_region_setup(term, y1, y2);
if (count > 0) {
term->move(term, 0, y2);
term->tr_move(term, 0, y2);
for (i = 0; i < count; i++)
tput(tparm(term->TI_ind, 0, 0, 0, 0, 0, 0, 0, 0, 0));
} else if (count < 0) {
term->move(term, 0, y1);
term->tr_move(term, 0, y1);
for (i = count; i < 0; i++)
tput(tparm(term->TI_ri, 0, 0, 0, 0, 0, 0, 0, 0, 0));
}
/* reset the scrolling region to full screen */
scroll_region_setup(term, 0, term->height-1);
scroll_region_setup(term, 0, term->height - 1);
}
/* Scroll (parm_insert_line+parm_delete_line / il+dl) */
@ -219,19 +219,19 @@ static void _scroll_line(TERM_REC *term, int y1, int y2, int count)
scroll_region_setup(term, y1, y2);
if (count > 0) {
term->move(term, 0, y1);
term->tr_move(term, 0, y1);
tput(tparm(term->TI_dl, count, count, 0, 0, 0, 0, 0, 0, 0));
term->move(term, 0, y2-count+1);
term->tr_move(term, 0, y2 - count + 1);
tput(tparm(term->TI_il, count, count, 0, 0, 0, 0, 0, 0, 0));
} else if (count < 0) {
term->move(term, 0, y2+count+1);
term->tr_move(term, 0, y2 + count + 1);
tput(tparm(term->TI_dl, -count, -count, 0, 0, 0, 0, 0, 0, 0));
term->move(term, 0, y1);
term->tr_move(term, 0, y1);
tput(tparm(term->TI_il, -count, -count, 0, 0, 0, 0, 0, 0, 0));
}
/* reset the scrolling region to full screen */
scroll_region_setup(term, 0, term->height-1);
scroll_region_setup(term, 0, term->height - 1);
}
/* Scroll (insert_line+delete_line / il1+dl1) */
@ -240,17 +240,17 @@ static void _scroll_line_1(TERM_REC *term, int y1, int y2, int count)
int i;
if (count > 0) {
term->move(term, 0, y1);
term->tr_move(term, 0, y1);
for (i = 0; i < count; i++)
tput(tparm(term->TI_dl1, 0, 0, 0, 0, 0, 0, 0, 0, 0));
term->move(term, 0, y2-count+1);
term->tr_move(term, 0, y2 - count + 1);
for (i = 0; i < count; i++)
tput(tparm(term->TI_il1, 0, 0, 0, 0, 0, 0, 0, 0, 0));
} else if (count < 0) {
term->move(term, 0, y2+count+1);
term->tr_move(term, 0, y2 + count + 1);
for (i = count; i < 0; i++)
tput(tparm(term->TI_dl1, 0, 0, 0, 0, 0, 0, 0, 0, 0));
term->move(term, 0, y1);
term->tr_move(term, 0, y1);
for (i = count; i < 0; i++)
tput(tparm(term->TI_il1, 0, 0, 0, 0, 0, 0, 0, 0, 0));
}
@ -265,14 +265,14 @@ static void _clear_screen(TERM_REC *term)
/* Clear screen (clr_eos / ed) */
static void _clear_eos(TERM_REC *term)
{
term->move(term, 0, 0);
term->tr_move(term, 0, 0);
tput(tparm(term->TI_ed, 0, 0, 0, 0, 0, 0, 0, 0, 0));
}
/* Clear screen (parm_delete_line / dl) */
static void _clear_del(TERM_REC *term)
{
term->move(term, 0, 0);
term->tr_move(term, 0, 0);
tput(tparm(term->TI_dl, term->height, term->height, 0, 0, 0, 0, 0, 0, 0));
}
@ -281,7 +281,7 @@ static void _clear_del_1(TERM_REC *term)
{
int i;
term->move(term, 0, 0);
term->tr_move(term, 0, 0);
for (i = 0; i < term->height; i++)
tput(tparm(term->TI_dl1, 0, 0, 0, 0, 0, 0, 0, 0, 0));
}
@ -455,10 +455,7 @@ static void terminfo_colors_deinit(TERM_REC *term)
terminal capabilities don't contain color codes */
void terminfo_setup_colors(TERM_REC *term, int force)
{
static const char ansitab[16] = {
0, 4, 2, 6, 1, 5, 3, 7,
8, 12, 10, 14, 9, 13, 11, 15
};
static const char ansitab[16] = { 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15 };
unsigned int i, color;
terminfo_colors_deinit(term);
@ -466,16 +463,15 @@ void terminfo_setup_colors(TERM_REC *term, int force)
if (force && term->TI_setf == NULL && term->TI_setaf == NULL)
term->TI_colors = 8;
if ((term->TI_setf || term->TI_setaf || force) &&
term->TI_colors > 0) {
if ((term->TI_setf || term->TI_setaf || force) && term->TI_colors > 0) {
term->TI_fg = g_new0(char *, term->TI_colors);
term->TI_bg = g_new0(char *, term->TI_colors);
term->set_fg = _set_fg;
term->set_bg = _set_bg;
term->tr_set_fg = _set_fg;
term->tr_set_bg = _set_bg;
} else {
/* no colors */
term->TI_colors = 0;
term->set_fg = term->set_bg = _ignore_parm;
term->tr_set_fg = term->tr_set_bg = _ignore_parm;
}
if (term->TI_setaf) {
@ -489,7 +485,7 @@ void terminfo_setup_colors(TERM_REC *term, int force)
term->TI_fg[i] = g_strdup(tparm(term->TI_setf, i, 0, 0, 0, 0, 0, 0, 0, 0));
} else if (force) {
for (i = 0; i < 8; i++)
term->TI_fg[i] = g_strdup_printf("\033[%dm", 30+ansitab[i]);
term->TI_fg[i] = g_strdup_printf("\033[%dm", 30 + ansitab[i]);
}
if (term->TI_setab) {
@ -503,7 +499,7 @@ void terminfo_setup_colors(TERM_REC *term, int force)
term->TI_bg[i] = g_strdup(tparm(term->TI_setb, i, 0, 0, 0, 0, 0, 0, 0, 0));
} else if (force) {
for (i = 0; i < 8; i++)
term->TI_bg[i] = g_strdup_printf("\033[%dm", 40+ansitab[i]);
term->TI_bg[i] = g_strdup_printf("\033[%dm", 40 + ansitab[i]);
}
}
@ -598,40 +594,40 @@ static int term_setup(TERM_REC *term)
/* Cursor movement */
if (term->TI_cup)
term->move = _move_cup;
term->tr_move = _move_cup;
else if (term->TI_hpa && term->TI_vpa)
term->move = _move_pa;
term->tr_move = _move_pa;
else {
fprintf(stderr, "Terminal doesn't support cursor movement\n");
return 0;
}
term->move_relative = _move_relative;
term->set_cursor_visible = term->TI_civis && term->TI_cnorm ?
_set_cursor_visible : _ignore_parm;
term->tr_move_relative = _move_relative;
term->tr_set_cursor_visible =
term->TI_civis && term->TI_cnorm ? _set_cursor_visible : _ignore_parm;
/* Scrolling */
if ((term->TI_csr || term->TI_wind) && term->TI_rin && term->TI_indn)
term->scroll = _scroll_region;
term->tr_scroll = _scroll_region;
else if (term->TI_il && term->TI_dl)
term->scroll = _scroll_line;
term->tr_scroll = _scroll_line;
else if ((term->TI_csr || term->TI_wind) && term->TI_ri && term->TI_ind)
term->scroll = _scroll_region_1;
else if (term->scroll == NULL && (term->TI_il1 && term->TI_dl1))
term->scroll = _scroll_line_1;
else if (term->scroll == NULL) {
term->tr_scroll = _scroll_region_1;
else if (term->tr_scroll == NULL && (term->TI_il1 && term->TI_dl1))
term->tr_scroll = _scroll_line_1;
else if (term->tr_scroll == NULL) {
fprintf(stderr, "Terminal doesn't support scrolling\n");
return 0;
}
/* Clearing screen */
if (term->TI_clear)
term->clear = _clear_screen;
term->tr_clear = _clear_screen;
else if (term->TI_ed)
term->clear = _clear_eos;
term->tr_clear = _clear_eos;
else if (term->TI_dl)
term->clear = _clear_del;
term->tr_clear = _clear_del;
else if (term->TI_dl1)
term->clear = _clear_del_1;
term->tr_clear = _clear_del_1;
else {
/* we could do this by line inserts as well, but don't
bother - if some terminal has insert line it most probably
@ -642,7 +638,7 @@ static int term_setup(TERM_REC *term)
/* Clearing to end of line */
if (term->TI_el)
term->clrtoeol = _clrtoeol;
term->tr_clrtoeol = _clrtoeol;
else {
fprintf(stderr, "Terminal doesn't support clearing to end of line\n");
return 0;
@ -650,37 +646,38 @@ static int term_setup(TERM_REC *term)
/* Repeating character */
if (term->TI_rep)
term->repeat = _repeat;
term->tr_repeat = _repeat;
else
term->repeat = _repeat_manual;
term->tr_repeat = _repeat_manual;
/* Bold, underline, standout, reverse, italics */
term->set_blink = term->TI_blink ? _set_blink : _ignore;
term->set_bold = term->TI_bold ? _set_bold : _ignore;
term->set_reverse = term->TI_rev ? _set_reverse :
term->TI_smso ? _set_standout_on : _ignore;
term->set_uline = term->TI_smul && term->TI_rmul ?
_set_uline : _ignore_parm;
term->set_standout = term->TI_smso && term->TI_rmso ?
_set_standout : _ignore_parm;
term->set_italic = term->TI_sitm && term->TI_ritm ?
_set_italic : _ignore_parm;
term->tr_set_blink = term->TI_blink ? _set_blink : _ignore;
term->tr_set_bold = term->TI_bold ? _set_bold : _ignore;
term->tr_set_reverse = term->TI_rev ? _set_reverse :
term->TI_smso ? _set_standout_on :
_ignore;
term->tr_set_uline = term->TI_smul && term->TI_rmul ? _set_uline : _ignore_parm;
term->tr_set_standout = term->TI_smso && term->TI_rmso ? _set_standout : _ignore_parm;
term->tr_set_italic = term->TI_sitm && term->TI_ritm ? _set_italic : _ignore_parm;
/* Create a string to set all attributes off */
str = g_string_new(NULL);
if (term->TI_sgr0)
g_string_append(str, term->TI_sgr0);
if (term->TI_rmul && (term->TI_sgr0 == NULL || g_strcmp0(term->TI_rmul, term->TI_sgr0) != 0))
if (term->TI_rmul &&
(term->TI_sgr0 == NULL || g_strcmp0(term->TI_rmul, term->TI_sgr0) != 0))
g_string_append(str, term->TI_rmul);
if (term->TI_rmso && (term->TI_sgr0 == NULL || g_strcmp0(term->TI_rmso, term->TI_sgr0) != 0))
if (term->TI_rmso &&
(term->TI_sgr0 == NULL || g_strcmp0(term->TI_rmso, term->TI_sgr0) != 0))
g_string_append(str, term->TI_rmso);
if (term->TI_ritm && (term->TI_sgr0 == NULL || g_strcmp0(term->TI_ritm, term->TI_sgr0) != 0))
if (term->TI_ritm &&
(term->TI_sgr0 == NULL || g_strcmp0(term->TI_ritm, term->TI_sgr0) != 0))
g_string_append(str, term->TI_ritm);
term->TI_normal = str->str;
g_string_free(str, FALSE);
term->set_normal = _set_normal;
term->tr_set_normal = _set_normal;
term->beep = term->TI_bel ? _beep : _ignore;
term->tr_beep = term->TI_bel ? _beep : _ignore;
terminfo_setup_colors(term, FALSE);
terminfo_input_init0(term);
@ -731,7 +728,7 @@ void terminfo_core_deinit(TERM_REC *term)
old_term = current_term;
current_term = term;
term->set_normal(term);
term->tr_set_normal(term);
current_term = old_term;
terminfo_stop(term);

View File

@ -3,48 +3,49 @@
#include <termios.h>
#define terminfo_move(x, y) current_term->move(current_term, x, y)
#define terminfo_move_relative(oldx, oldy, x, y) current_term->move_relative(current_term, oldx, oldy, x, y)
#define terminfo_set_cursor_visible(set) current_term->set_cursor_visible(current_term, set)
#define terminfo_scroll(y1, y2, count) current_term->scroll(current_term, y1, y2, count)
#define terminfo_clear() current_term->clear(current_term)
#define terminfo_clrtoeol() current_term->clrtoeol(current_term)
#define terminfo_repeat(chr, count) current_term->repeat(current_term, chr, count)
#define terminfo_set_fg(color) current_term->set_fg(current_term, color)
#define terminfo_set_bg(color) current_term->set_bg(current_term, color)
#define terminfo_set_normal() current_term->set_normal(current_term)
#define terminfo_set_bold() current_term->set_bold(current_term)
#define terminfo_set_uline(set) current_term->set_uline(current_term, set)
#define terminfo_set_standout(set) current_term->set_standout(current_term, set)
#define terminfo_set_reverse() current_term->set_reverse(current_term)
#define terminfo_set_italic(set) current_term->set_italic(current_term, set)
#define terminfo_move(x, y) current_term->tr_move(current_term, x, y)
#define terminfo_move_relative(oldx, oldy, x, y) \
current_term->tr_move_relative(current_term, oldx, oldy, x, y)
#define terminfo_set_cursor_visible(set) current_term->tr_set_cursor_visible(current_term, set)
#define terminfo_scroll(y1, y2, count) current_term->tr_scroll(current_term, y1, y2, count)
#define terminfo_clear() current_term->tr_clear(current_term)
#define terminfo_clrtoeol() current_term->tr_clrtoeol(current_term)
#define terminfo_repeat(chr, count) current_term->tr_repeat(current_term, chr, count)
#define terminfo_set_fg(color) current_term->tr_set_fg(current_term, color)
#define terminfo_set_bg(color) current_term->tr_set_bg(current_term, color)
#define terminfo_set_normal() current_term->tr_set_normal(current_term)
#define terminfo_set_bold() current_term->tr_set_bold(current_term)
#define terminfo_set_uline(set) current_term->tr_set_uline(current_term, set)
#define terminfo_set_standout(set) current_term->tr_set_standout(current_term, set)
#define terminfo_set_reverse() current_term->tr_set_reverse(current_term)
#define terminfo_set_italic(set) current_term->tr_set_italic(current_term, set)
#define terminfo_is_colors_set(term) (term->TI_fg != NULL)
#define terminfo_beep(term) current_term->beep(current_term)
#define terminfo_beep(term) current_term->tr_beep(current_term)
typedef struct _TERM_REC TERM_REC;
struct _TERM_REC {
/* Functions */
void (*move)(TERM_REC *term, int x, int y);
void (*move_relative)(TERM_REC *term, int oldx, int oldy, int x, int y);
void (*set_cursor_visible)(TERM_REC *term, int set);
void (*scroll)(TERM_REC *term, int y1, int y2, int count);
void (*tr_move)(TERM_REC *term, int x, int y);
void (*tr_move_relative)(TERM_REC *term, int oldx, int oldy, int x, int y);
void (*tr_set_cursor_visible)(TERM_REC *term, int set);
void (*tr_scroll)(TERM_REC *term, int y1, int y2, int count);
void (*clear)(TERM_REC *term);
void (*clrtoeol)(TERM_REC *term);
void (*repeat)(TERM_REC *term, char chr, int count);
void (*tr_clear)(TERM_REC *term);
void (*tr_clrtoeol)(TERM_REC *term);
void (*tr_repeat)(TERM_REC *term, char chr, int count);
void (*set_fg)(TERM_REC *term, int color);
void (*set_bg)(TERM_REC *term, int color);
void (*set_normal)(TERM_REC *term);
void (*set_blink)(TERM_REC *term);
void (*set_bold)(TERM_REC *term);
void (*set_reverse)(TERM_REC *term);
void (*set_uline)(TERM_REC *term, int set);
void (*set_standout)(TERM_REC *term, int set);
void (*set_italic)(TERM_REC *term, int set);
void (*tr_set_fg)(TERM_REC *term, int color);
void (*tr_set_bg)(TERM_REC *term, int color);
void (*tr_set_normal)(TERM_REC *term);
void (*tr_set_blink)(TERM_REC *term);
void (*tr_set_bold)(TERM_REC *term);
void (*tr_set_reverse)(TERM_REC *term);
void (*tr_set_uline)(TERM_REC *term, int set);
void (*tr_set_standout)(TERM_REC *term, int set);
void (*tr_set_italic)(TERM_REC *term, int set);
void (*beep)(TERM_REC *term);
void (*tr_beep)(TERM_REC *term);
#ifndef HAVE_TERMINFO
char buffer1[1024], buffer2[1024];