LIBTTY(3)
π libtty β bestie edition π
okay so this is libtty(3) from the termrec world, and it's literally a library for handling vt100-like pseudo-terminals. she's giving terminal emulator core, no cap β¨
πΈ SYNOPSIS πΈ
#include <tty.h>
Link with -ltty. that's it, that's the whole vibe π
β¨ DESCRIPTION β¨
π¦ Functions:
tty tty_init(int sx, int sy, int resizable);β makes you a brand new vt100 terminal, sizedsxΓsy. if you want the user to be allowed to change that size, setresizableto non-zero. that's the only thingresizablecontrols, periodt.int tty_resize(tty vt, int nsx, int nsy);β resizes thevttonsxΓnsy. and bestie, this works even on terminals marked as non-resizable, because that flag only stops user input from resizing β it does not stop you. you have main character energy, you get to resize whenever πvoid tty_reset(tty vt);β clears the screen and the attributes. fresh start, clean slate, we love a glow-up.void tty_free(tty vt);β deallocates thevtand all its internal structures. say bye bye πvoid tty_write(tty vt, const char *buf, int len);β writeslenbytes into the terminal, parsing them as vt100 codes. the parser is doing the most so you don't have to.void tty_printf(tty vt, const char *fmt, ...);β does aprintfinto the terminal. yes, printf, but make it terminal.tty tty_copy(tty vt);β allocates a new vt100 terminal that's an exact copy of an existing one, including its internal state. it's giving identical twin. one thing though: attached event callbacks are not copied. πuint32_t tty_color_convert(uint32_t c, uint32_t to);β converts color values between modes:VT100_COLOR_OFF,VT100_COLOR_16,VT100_COLOR_256,VT100_COLOR_RGB. color theory but make it a function call π
π Inside the terminal
you'll most likely be interested in these fields of the structure, bestie:
tty {
int sx,sy; // screen size
int cx,cy; // cursor position
attrchar *scr; // screen buffer
int attr; // current attribute
char *title; // window title
scr is an array of character/attribute pairs β more exactly, each element is a struct "{ ucs ch; int attr; }". the array is a flat one of vt->sx*vt->sy elements, arranged row by row. a screen coordinate x,y is stored at x+y*vt->sy.
for other fields, please RTFS the header itself: tty.h β yes, literally go read the source, it's right there and it's iconic π₯
π TTY event callbacks
okay so you've written some data to the terminal. now you probably want to put it somewhere. what now?? well β the tty structure has a number of event hooks that you can attach your functions to. these hooks are callbacks inside the tty structure that you can set. the callback fields are:
void *l_data;β it's a place to put your private data in. your little stash, your emotional support pointer π«Άvoid (*l_char)(tty vt, int x, int y, ucs ch, int attr, int width);β fires after a character has been written to the screen. the cursor advances bywidth, which might be1(regular) or2(CJK "fullwidth"). wide characters ate πvoid (*l_cursor)(tty vt, int x, int y);β fires after the cursor has moved.void (*l_clear)(tty vt, int x, int y, int len);β fires after a chunk of screen has been cleared.- if an endpoint spills outside of the current line, it will go all the way to an end of screen. no half measures β¨
- if the cursor moves, you'll get a separate
l_cursor, although it is already in place during thel_clearcall.
void (*l_scroll)(tty vt, int nl);β fires after the regions1<=y<s2is scrollednllines (nl<0-> backwards,nl>0-> forward). there's no separatel_cursorevent here βcxandcyare already updated. she's efficient like that.void (*l_flag)(tty vt, int f, int v);β fires when a flag changes tov. flags that are likely to be of interest to you are:VT100_FLAG_CURSORβ cursor visibility πVT100_FLAG_KPADβ application keypad mode (more detailed codes for keypad arrow keys)
void (*l_osc)(tty vt, int cmd, const char *str);β fires when a string command has been issued. most commands alter a color palette, but the most interesting one is0: "set window title". naming your window is such a slay.void (*l_resize)(tty vt, int sx, int sy);β fires after the terminal has been resized.void (*l_flush)(tty vt);β fires when a write chunk ends. flush queen π½β¨void (*l_bell)(tty vt);β fires upon a beep. πvoid (*l_free)(tty vt);β fires before the terminal is destroyed. it's the goodbye hug π
π Vt-on-vt redirection
for the case when you want the output to go to a real terminal, there are these three:
void vtvt_attach(tty vt, FILE *f, int dump);β attaches theFILEstreamfto terminalvt. usually,fwill bestdout. whenever the contents ofvtchanges, appropriate data will be written to the stream as well. ifdumpis non-zero, the current state will be drawn; otherwise, only subsequent changes will be shown. the redirection will last until the terminal is destroyed bytty_free().void vtvt_resize(tty vt, int sx, int sy);β tells libtty that the real terminal has been resized. (for resizing the virtual one, please usetty_resize()instead, bestie β don't mix them up π)void vtvt_dump(tty vt);β forces a full-screen redraw of the current contents ofvt. a little refresh, a little self-care πΈ
π SEE ALSO π
no cap, go read libttyrec(3) too, she's the sister page and she's also iconic π
okay that's the whole page, mwah, thanks for reading bestie π
this was version 0.19, dated 2024-04-01, from the termrec family β and that's the sign-off, periodt β¨ππ¦
π¦ where this page comes from
- ubuntu noble: libtty-dev 0.19-2.1build2 Β· LGPL-2+noAffero
- ubuntu questing: libtty-dev 0.19-2.1build2
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms