term-internal.h revision 1c9633d669948155455e29b0c6e770995a8b1ca3
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright (C) 2014 David Herrmann <dh.herrmann@gmail.com>
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd 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 systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include "util.h"
typedef struct term_char term_char_t;
typedef struct term_charbuf term_charbuf_t;
typedef struct term_color term_color;
typedef struct term_history term_history;
typedef struct term_parser term_parser;
/*
* Miscellaneous
* Sundry things and external helpers.
*/
/*
* Ageing
* Redrawing terminals is quite expensive. Therefore, we avoid redrawing on
* each single modification and mark modified cells instead. This way, we know
* which cells to redraw on the next frame. However, a single DIRTY flag is not
* each cell. If the cell is modified, we simply increase the age by one. Each
* framebuffer can then remember its last rendered age and request an update of
* all newer cells.
* TERM_AGE_NULL is special. If used as cell age, the cell must always be
* redrawn (forced update). If used as framebuffer age, all cells are drawn.
* This way, we can allow integer wrap-arounds.
*/
typedef uint64_t term_age_t;
#define TERM_AGE_NULL 0
/*
* Characters
* Each cell in a terminal page contains only a single character. This is
* usually a single UCS-4 value. However, Unicode allows combining-characters,
* therefore, the number of UCS-4 characters per cell must be unlimited. The
* term_char_t object wraps the internal combining char API so it can be
* treated as a single object.
*/
struct term_char {
/* never access this value directly */
};
struct term_charbuf {
/* 3 bytes + zero-terminator */
};
#define TERM_CHAR_NULL TERM_CHAR_INIT(0)
/* true if @ch is TERM_CHAR_NULL, otherwise false */
}
/* true if @ch is dynamically allocated and needs to be freed */
}
/* true if (a == b), otherwise false; this is (a == b), NOT (*a == *b) */
}
/* true if (*a == *b), otherwise false; this is implied by (a == b) */
}
/* free @ch in case it is dynamically allocated */
if (term_char_is_allocated(ch))
term_char_set(ch, 0);
return TERM_CHAR_NULL;
}
/* gcc _cleanup_ helpers */
static inline void term_char_freep(term_char_t *p) {
term_char_free(*p);
}
/*
* Attributes
* Each cell in a terminal page can have its own set of attributes. These alter
* the behavior of the renderer for this single cell. We use term_attr to
* specify attributes.
* The only non-obvious field is "ccode" for foreground and background colors.
* This field contains the terminal color-code in case no full RGB information
* was given by the host. It is also required for dynamic color palettes. If it
* is set to TERM_CCODE_RGB, the "red", "green" and "blue" fields contain the
* full RGB color.
*/
enum {
/* special color-codes */
TERM_CCODE_DEFAULT, /* default foreground/background color */
TERM_CCODE_256, /* 256color code */
TERM_CCODE_RGB, /* color is specified as RGB */
/* dark color-codes */
TERM_CCODE_WHITE, /* technically: light grey */
/* light color-codes */
};
struct term_color {
};
struct term_attr {
};
/*
* Cells
* The term_cell structure respresents a single cell in a terminal page. It
* contains the stored character, the age of the cell and all its attributes.
*/
struct term_cell {
unsigned int cwidth; /* cached term_char_lookup_width(cell->ch) */
};
/*
* Lines
* Instead of storing cells in a 2D array, we store them in an array of
* dynamically allocated lines. This way, scrolling can be implemented very
* fast without moving any cells at all. Similarly, the scrollback-buffer is
* much simpler to implement.
* We use term_line to store a single line. It contains an array of cells, a
* fill-state which remembers the amount of blanks on the right side, a
* separate age just for the line which can overwrite the age for all cells,
* and some management data.
*/
struct term_line {
unsigned int width; /* visible width of line */
unsigned int n_cells; /* # of allocated cells */
unsigned int fill; /* # of valid cells; starting left */
};
int term_line_reserve(term_line *line, unsigned int width, const term_attr *attr, term_age_t age, unsigned int protect_width);
void term_line_write(term_line *line, unsigned int pos_x, term_char_t ch, unsigned int cwidth, const term_attr *attr, term_age_t age, bool insert_mode);
void term_line_insert(term_line *line, unsigned int from, unsigned int num, const term_attr *attr, term_age_t age);
void term_line_delete(term_line *line, unsigned int from, unsigned int num, const term_attr *attr, term_age_t age);
void term_line_erase(term_line *line, unsigned int from, unsigned int num, const term_attr *attr, term_age_t age, bool keep_protected);
#define TERM_LINE_LINK(_line, _head) term_line_link((_line), &(_head)->lines_first, &(_head)->lines_last)
#define TERM_LINE_LINK_TAIL(_line, _head) term_line_link_tail((_line), &(_head)->lines_first, &(_head)->lines_last)
#define TERM_LINE_UNLINK(_line, _head) term_line_unlink((_line), &(_head)->lines_first, &(_head)->lines_last)
/*
* Pages
* A page represents the 2D table containing all cells of a terminal. It stores
* lines as an array of pointers so scrolling becomes a simple line-shuffle
* operation.
* Scrolling is always targeted only at the scroll-region defined via scroll_idx
* and scroll_num. The fill-state keeps track of the number of touched lines in
* the scroll-region. @width and @height describe the visible region of the page
* and are guaranteed to be allocated at all times.
*/
struct term_page {
unsigned int n_lines; /* # of allocated lines */
unsigned int width; /* width of visible area */
unsigned int height; /* height of visible area */
unsigned int scroll_idx; /* scrolling-region start index */
unsigned int scroll_num; /* scrolling-region length in lines */
unsigned int scroll_fill; /* # of valid scroll-lines */
};
int term_page_reserve(term_page *page, unsigned int cols, unsigned int rows, const term_attr *attr, term_age_t age);
void term_page_resize(term_page *page, unsigned int cols, unsigned int rows, const term_attr *attr, term_age_t age, term_history *history);
void term_page_write(term_page *page, unsigned int pos_x, unsigned int pos_y, term_char_t ch, unsigned int cwidth, const term_attr *attr, term_age_t age, bool insert_mode);
void term_page_insert_cells(term_page *page, unsigned int from_x, unsigned int from_y, unsigned int num, const term_attr *attr, term_age_t age);
void term_page_delete_cells(term_page *page, unsigned int from_x, unsigned int from_y, unsigned int num, const term_attr *attr, term_age_t age);
void term_page_append_combchar(term_page *page, unsigned int pos_x, unsigned int pos_y, uint32_t ucs4, term_age_t age);
void term_page_erase(term_page *page, unsigned int from_x, unsigned int from_y, unsigned int to_x, unsigned int to_y, const term_attr *attr, term_age_t age, bool keep_protected);
void term_page_scroll_up(term_page *page, unsigned int num, const term_attr *attr, term_age_t age, term_history *history);
void term_page_scroll_down(term_page *page, unsigned int num, const term_attr *attr, term_age_t age, term_history *history);
void term_page_insert_lines(term_page *page, unsigned int pos_y, unsigned int num, const term_attr *attr, term_age_t age);
void term_page_delete_lines(term_page *page, unsigned int pos_y, unsigned int num, const term_attr *attr, term_age_t age);
/*
* Histories
* Scroll-back buffers use term_history objects to store scroll-back lines. A
* page is independent of the history used. All page operations that modify a
* history take it as separate argument. You're free to pass NULL at all times
* if no history should be used.
* Lines are stored in a linked list as no complex operations are ever done on
* guaranteed minimum length. Any kind of line might be stored there. Missing
* cells should be cleared to the background color.
*/
struct term_history {
unsigned int n_lines;
unsigned int max_lines;
};
term_line *term_history_pop(term_history *history, unsigned int reserve_width, const term_attr *attr, term_age_t age);
unsigned int term_history_peek(term_history *history, unsigned int max, unsigned int reserve_width, const term_attr *attr, term_age_t age);
/*
* UTF-8
* The UTF-decoder and encoder are adjusted for terminals and provide proper
* fallbacks for invalid UTF-8. In terminals it's quite usual to use fallbacks
* instead of rejecting invalid input. This way, old legacy applications still
*/
struct term_utf8 {
unsigned int i_bytes : 3;
unsigned int n_bytes : 3;
unsigned int valid : 1;
};
/*
* Parsers
* The term_parser object parses control-sequences for both host and terminal
* side. Based on this parser, there is a set of command-parsers that take a
* term_seq sequence and returns the command it represents. This is different
* for host and terminal side so a different set of parsers is provided.
*/
enum {
TERM_SEQ_NONE, /* placeholder, no sequence parsed */
TERM_SEQ_IGNORE, /* no-op character */
TERM_SEQ_GRAPHIC, /* graphic character */
TERM_SEQ_CONTROL, /* control character */
TERM_SEQ_ESCAPE, /* escape sequence */
TERM_SEQ_CSI, /* control sequence function */
TERM_SEQ_DCS, /* device control string */
TERM_SEQ_OSC, /* operating system control */
};
enum {
/* these must be kept compatible to (1U << (ch - 0x20)) */
/* 16-35 is reserved for numbers; unused */
/* COLON is reserved = (1U << 26), char: : */
/* SEMICOLON is reserved = (1U << 27), char: ; */
};
enum {
TERM_CMD_NONE, /* placeholder */
TERM_CMD_GRAPHIC, /* graphics character */
TERM_CMD_BEL, /* bell */
TERM_CMD_BS, /* backspace */
TERM_CMD_CBT, /* cursor-backward-tabulation */
TERM_CMD_CHA, /* cursor-horizontal-absolute */
TERM_CMD_CHT, /* cursor-horizontal-forward-tabulation */
TERM_CMD_CNL, /* cursor-next-line */
TERM_CMD_CPL, /* cursor-previous-line */
TERM_CMD_CR, /* carriage-return */
TERM_CMD_CUB, /* cursor-backward */
TERM_CMD_CUD, /* cursor-down */
TERM_CMD_CUF, /* cursor-forward */
TERM_CMD_CUP, /* cursor-position */
TERM_CMD_CUU, /* cursor-up */
TERM_CMD_DA1, /* primary-device-attributes */
TERM_CMD_DA2, /* secondary-device-attributes */
TERM_CMD_DA3, /* tertiary-device-attributes */
TERM_CMD_DC1, /* device-control-1 */
TERM_CMD_DC3, /* device-control-3 */
TERM_CMD_DCH, /* delete-character */
TERM_CMD_DECALN, /* screen-alignment-pattern */
TERM_CMD_DECANM, /* ansi-mode */
TERM_CMD_DECBI, /* back-index */
TERM_CMD_DECCARA, /* change-attributes-in-rectangular-area */
TERM_CMD_DECCRA, /* copy-rectangular-area */
TERM_CMD_DECDC, /* delete-column */
TERM_CMD_DECDHL_BH, /* double-width-double-height-line: bottom half */
TERM_CMD_DECDHL_TH, /* double-width-double-height-line: top half */
TERM_CMD_DECDWL, /* double-width-single-height-line */
TERM_CMD_XTERM_CLLHP, /* xterm-cursor-lower-left-hp-bugfix */
TERM_CMD_XTERM_IHMT, /* xterm-initiate-highlight-mouse-tracking*/
TERM_CMD_XTERM_MLHP, /* xterm-memory-lock-hp-bugfix */
TERM_CMD_XTERM_MUHP, /* xterm-memory-unlock-hp-bugfix */
TERM_CMD_XTERM_RPM, /* xterm-restore-private-mode */
TERM_CMD_XTERM_RRV, /* xterm-reset-resource-value */
TERM_CMD_XTERM_RTM, /* xterm-reset-title-mode */
TERM_CMD_XTERM_SACL1, /* xterm-set-ansi-conformance-level-1 */
TERM_CMD_XTERM_SACL2, /* xterm-set-ansi-conformance-level-2 */
TERM_CMD_XTERM_SACL3, /* xterm-set-ansi-conformance-level-3 */
TERM_CMD_XTERM_SDCS, /* xterm-set-default-character-set */
TERM_CMD_XTERM_SGFX, /* xterm-sixel-graphics */
TERM_CMD_XTERM_SPM, /* xterm-set-private-mode */
TERM_CMD_XTERM_SRV, /* xterm-set-resource-value */
TERM_CMD_XTERM_STM, /* xterm-set-title-mode */
TERM_CMD_XTERM_SUCS, /* xterm-set-utf8-character-set */
TERM_CMD_XTERM_WM, /* xterm-window-management */
};
enum {
/*
* Charsets: DEC marks charsets according to "Digital Equ. Corp.".
* NRCS marks charsets according to the "National Replacement
* Character Sets". ISO marks charsets according to ISO-8859.
* The USERDEF charset is special and can be modified by the host.
*/
/* 96-compat charsets */
/* 94-compat charsets */
/* special charsets */
};
extern term_charset term_unicode_lower;
extern term_charset term_unicode_upper;
#define TERM_PARSER_ARG_MAX (16)
#define TERM_PARSER_ST_MAX (4096)
struct term_seq {
unsigned int type;
unsigned int command;
unsigned int intermediates;
unsigned int charset;
unsigned int n_args;
int args[TERM_PARSER_ARG_MAX];
unsigned int n_st;
char *st;
};
struct term_parser {
unsigned int state;
bool is_host : 1;
};