yascreen - Yet Another Screen Library (curses replacement for daemons and embedded apps)

NAME  SYNOPSIS  DESCRIPTION  Main features  Architecture  Example initialization for terminal and handling of SIGWINCH  Example initialization for socket with external event loop and telnetsequences processing  API Reference  Predefined constants and Helper macros  Style codes  Color codes  Helper macros  Key codes  Functions  yascreen_init  yascreen_ver  yascreen_setout  yascreen_set_unicode  yascreen_set_telnet  yascreen_init_telnet  yascreen_resize  yascreen_free  yascreen_term_save  yascreen_term_restore  yascreen_term_push  yascreen_term_pop  yascreen_term_set  mode is a bitmask, containing one of  yascreen_printxy  yascreen_putsxy  yascreen_printxyu  yascreen_putsxyu  yascreen_update  yascreen_redraw  yascreen_clear_mem  yascreen_cursor  yascreen_cursor_xy  yascreen_altbuf  yascreen_clear  yascreen_clearln  yascreen_update_attr  yascreen_set_attr  yascreen_print  yascreen_write  yascreen_puts  yascreen_clearln_s  yascreen_sx  yascreen_sy  yascreen_x  yascreen_y  yascreen_esc_to  yascreen_ckto  yascreen_getch_to  yascreen_getch  yascreen_getch_nowait  yascreen_ungetch  yascreen_pushch  yascreen_feed  yascreen_peekch  yascreen_getsize  yascreen_reqsize  yascreen_set_hint_i  yascreen_get_hint_i  yascreen_set_hint_p  yascreen_get_hint_p  yascreen_line_flush  yascreen_getwch_to  yascreen_getwch  yascreen_getwch_nowait  yascreen_ungetwch  yascreen_peekwch 

NAME

yascreen - Yet Another Screen Library (curses replacement for daemons and embedded apps)

SYNOPSIS

#include <yascreen.h>

DESCRIPTION

Main features

small footprint

does not have external dependencies

allows both internal and external event loop

allows stdin/stdout or external input/output (can work over socket)

supports basic set of telnet sequences, making it suitable for built-in terminal interfaces for daemons

supports a limited set of input keystroke sequences

fully unicode compatible (parts of this depend on wcwidth in libc)

supports utf8 verification of input

supports utf8 input and wide character input

supports non-utf8 input mode

relies only on a limited subset of ansi/xterm ESC sequences, making it compatible with mostly all modern terminals (inspired by linenoise ⟨https://github.com/antirez/linenoise⟩)

there is no curses API and ancient terminal compatibility, hence less bloat

there is no autoconf - there is no need to have one

clean API with opaque private data, usable from C/C++

easy cross compilation setup (by setting CC, AR, STRIP and RANLIB)

Current development is done on Linux, with additional testing on OpenBSD/FreeBSD; other platforms may need minimal fixes.

On *BSD a gmake is required to build.

Architecture

yascreen uses an opaque data structure, allocated by the library and dynamically resized when needed - yascreen_init(int sx, int sy) / yascreen_resize(yascreen *s, int sx, int sy). An application may specify (0,0) for both calls to let yascreen detect the size or use a fixed size.

There are two modes of operation - telnet protocol over socket or running in terminal. For sockets the event loop would typically be handled outside of the library while for terminals a built-in event loop may be used.

Modes of operation can be modified at runtime.

For terminal use signal handling (SIGWINCH) should always be handled by the application.

Example initialization for terminal and handling of SIGWINCH

yascreen *s;
int winch=0;

void sigwinch(int sign) {

winch=1;

}

s=yascreen_init(0,0); // let yascreen get term size
yascreen_term_set(s,YAS_NOBUFF|YAS_NOSIGN|YAS_NOECHO);
signal(SIGWINCH,sigwinch);

for (;;) { // main loop

if (winch) {

winch=0;

if (yascreen_resize(s,0,0))

// handle a fatal error - no memory

// get the new sizes and redraw

newsizex=yascreen_sx(s);

newsizey=yascreen_sy(s);

}

â¦

// option 1

// input is handled in external event loop and fed to yascreen via yascreen_feed

if (FD_ISSET(STDIN_FILENO,&r)&&sizeof c==read(STDIN_FILENO,&c,sizeof c))

yascreen_feed(s,c); // pump state machine with bytestream

// keys are processed only when available without delay/blocking

while ((ch=yascreen_getch_nowait(s))!=-1) {

// handle processed keys

}

â¦

// option 2

// input is handled by yascreen and key or -1 is returned not longer than TIMEOUT ms

// note: if screen update is based on this, keypresses will force it

while ((ch=yascreen_getch_to(s,TIMEOUT))!=-1) {

// handle processed keys

}

â¦

// option 3

// input is handled by yascreen and the following call will block until a key is pressed

if ((ch=yascreen_getch(s))!=-1) {

// handle processed key

}

}

For sockets input is handled like option 1 in the example above and yascreen needs to be provided with a callback for output.

In multiprocess mode daemons where stdin/stdout are redirected to a socket the same model from the example above can be used. Obviously SIGWINCH will work only for terminals and for sockets the screen size can get to be known either via telnet or ASNI sequences.

Example initialization for socket with external event loop and telnetsequences processing

yascreen *s;

s=yascreen_init(80,25); // there is no guarantee that screen size detection is supported on the remote end
yascreen_setout(s,output_cb); // set callback for output
yascreen_set_telnet(s,1); // enable processing of telnet sequences
yascreen_init_telnet(s); // try to negotiate telnet options (regardless if telnet processing is enabled)
yascreen_reqsize(s); // request initial screen size
for (;;) { // main loop

â¦

yascreen_feed(s,c); // feed input from the socket to yascreen

// keys are processed only when available without delay/blocking

while ((ch=yascreen_getch_nowait(s))!=-1) {

// handle processed keys

// screen size change is reported as a special keypress code:

if (ch==YAS_TELNET_SIZE||ch==YAS_SCREEN_SIZE) // screen size change reported via telnet or ANSI sequence

// redraw

}

}

API Reference

Predefined constants and Helper macros

Internally style is kept into bitfields in a single integer variable - that includes foreground/background colors, style modifiers (bold, italic, underline, inverse and blink.

Style codes

Color codes

Helper macros

All of the above can be or’ed into attribute, provided that the bits for foreground/background color are all zeroes.

Key codes

Special, generated internally

Previous versions of the library used -1 and 0x100+ for these codes. In order to achieve unicode wide character compatibility and simpler API, the reserved Unicode range 0xf0000-0xffffd is used for the special codes both in narrow and wide character input modes.

There is a macro YAS_IS_CC(code) that will evaluate to non-zero for the special codes and to zero for normal characters. Note that all ASCII control characters in the range 0x00-0x7f are treated as normal ones.

Normal keys

Extended keys, parsed from ANSI sequences

Alt-<letter>

These codes are generated by a helper macro - YAS_K_ALT(keycode).

Functions

All functions in the API work with a pointer to an opaque yascreen structure.

The structure is allocated internally in the library by yascreen_init and it is the job of the user program to keep track of it.

The library is thread safe, as long as each struct yascreen object is accessed by a single thread.

yascreen_init

inline yascreen *yascreen_init(int sx,int sy);

allocate and initialize screen data

output defaults to stdout

in case output is a terminal and initial size is (0,0), the screen size is autodetected

in case of error, returns NULL

yascreen_ver

inline const char *yascreen_ver(void);

returns a string with the library version

yascreen_setout

inline int yascreen_setout(yascreen *s,ssize_t (*out)(yascreen *s,const void *data,size_t len));

set callback that handles output

if out=NULL, the output goes to stdout

the callback may implement internal buffering, a flush is signalled by calling out with len=0

yascreen_set_unicode

inline void yascreen_set_unicode(yascreen *s,int on);

enable (on is non-zero) or disable (on=0) unicode input processing

by default unicode mode is on

changing the unicode input processing will flush all pending input

yascreen_set_telnet

inline void yascreen_set_telnet(yascreen *s,int on);

enable (on is non-zero) or disable (on=0) telnet sequence processing

by default telnet mode is off

yascreen_init_telnet

inline void yascreen_init_telnet(yascreen *s);

depending on telnet sequence processing, sends a set of telnet initialization sequences

yascreen_resize

inline int yascreen_resize(yascreen *s,int sx,int sy);

resize screen

should redraw afterwards

since allocation is involved, this may fail and return -1

yascreen_free

inline void yascreen_free(yascreen *s);

finish the lifecycle of struct yascreen - all internally allocated memory is freed

yascreen_term_save

inline void yascreen_term_save(yascreen *s);

save current terminal state on top of state stack

yascreen_term_restore

inline void yascreen_term_restore(yascreen *s);

restore previously saved terminal state from top of state stack

yascreen_term_push

inline void yascreen_term_push(yascreen *s);

push current terminal state to state stack

yascreen_term_pop

inline void yascreen_term_pop(yascreen *s);

pop and restore previously saved terminal state from state stack

yascreen_term_set

inline void yascreen_term_set(yascreen *s,int mode);

set terminal for proper screen operation

mode is a bitmask, containing one of

yascreen_printxy

inline int yascreen_printxy(yascreen *s,int x,int y,uint32_t attr,const char *format,...) __attribute__((format(printf,5,6)));

yascreen_putsxy

inline int yascreen_putsxy(yascreen *s,int x,int y,uint32_t attr,const char *str);

print at position, if data exceeds buffer, then it gets truncated

yascreen_printxyu

inline int yascreen_printxyu(yascreen *s,int x,int y,uint32_t attr,const char *format,...) __attribute__((format(printf,5,6)));

yascreen_putsxyu

inline int yascreen_putsxyu(yascreen *s,int x,int y,uint32_t attr,const char *str);

print at position, if data exceeds buffer, then it gets truncated

screen is immediately updated

yascreen_update

inline int yascreen_update(yascreen *s);

sync memory state to screen

since allocation is involved, this may fail and return -1

yascreen_redraw

inline void yascreen_redraw(yascreen *s);

set next update to be a full redraw

yascreen_clear_mem

inline void yascreen_clear_mem(yascreen *s,uint32_t attr);

clear memory buffer

all cells in the screen are set to Space, using attr for colors and style

yascreen_cursor

inline void yascreen_cursor(yascreen *s,int on);

hide (on=0) or show (on is non-zero) cusror

screen is updated immediately

yascreen_cursor_xy

inline void yascreen_cursor_xy(yascreen *s,int x,int y);

set cursor position

screen is updated immediately

yascreen_altbuf

inline void yascreen_altbuf(yascreen *s,int on);

switch between regular and alternative buffer

screen is updated immediately

yascreen_clear

inline void yascreen_clear(yascreen *s);

clear real screen, no change to memory buffers

yascreen_clearln

inline void yascreen_clearln(yascreen *s);

clear current line, no change to memory buffers

yascreen_update_attr

inline void yascreen_update_attr(yascreen *s,uint32_t oattr,uint32_t nattr);

apply difference between two attrs and output the optimized ANSI sequence to switch from oattr to nattr

if oattr=0xffffffff, the full ANSI sequence will be generated

no change to memory buffers

yascreen_set_attr

yascreen_set_attr(s,attr)

reset all attrs and set specific one (attr)

yascreen_print

inline int yascreen_print(yascreen *s,const char *format,...) __attribute__((format(printf,2,3)));

yascreen_write

inline int yascreen_write(yascreen *s,const char *str,int len);

yascreen_puts

inline int yascreen_puts(yascreen *s,const char *str);

yascreen_clearln_s

inline const char *yascreen_clearln_s(yascreen *s);

print in line mode

yascreen_sx

inline int yascreen_sx(yascreen *s);

get current x size

yascreen_sy

inline int yascreen_sy(yascreen *s);

get current y size

yascreen_x

inline int yascreen_x(yascreen *s);

get current x

yascreen_y

inline int yascreen_y(yascreen *s);

get current y

yascreen_esc_to

inline void yascreen_esc_to(yascreen *s,int timeout);

set timeout for single ESC key press

yascreen_ckto

inline void yascreen_ckto(yascreen *s);

in case of external event loop, this call will check for single ESC key

should be called regularly enough so that the above specified timeout is not extended too much

if not called often enough then single ESC will be yielded after longer timeout

if not called at all then single ESC will be yielded with next key press

yascreen_getch_to

inline int yascreen_getch_to(yascreen *s,int timeout);

wait for a key, return ASCII or extended keycode, wait no more than timeout in milliseconds

yascreen_getch

yascreen_getch(s)

get a key without timeout

this macro expands to yascreen_getch_to(s,0)

zero timeout=wait forever

yascreen_getch_nowait

yascreen_getch_nowait(s)

get a key, if available, return immediately

this macro expands to yascreen_getch_to(s,-1)

negative timeout=do not wait

yascreen_ungetch

inline void yascreen_ungetch(yascreen *s,int key);

put back key value in key buffer

the internal key buffer is dynamically allocated, hence there is no limit of how many key codes may be put back, but in case of memory allocation failure, the error will not be reported and the key will not be put into the buffer

yascreen_pushch

inline void yascreen_pushch(yascreen *s,int key);

push key value at end of key buffer

similar to yascreen_ungetch but the key code will be returned after all other key codes currently in the buffer

the internal key buffer is dynamically allocated, hence there is no limit of how many key codes may be put back, but in case of memory allocation failure, the error will not be reported and the key will not be put into the buffer

yascreen_feed

inline void yascreen_feed(yascreen *s,unsigned char c);

feed key sequence state machine with byte stream

this is useful to implement external event loop and read key codes by yascreen_getch_nowait until it returns -1

yascreen_peekch

inline int yascreen_peekch(yascreen *s);

peek for key without removing it from input queue

yascreen_getsize

inline void yascreen_getsize(yascreen *s,int *sx,int *sy);

get last reported screen size

set both to 0 if there is none

this will yield valid result after YAS_SCREEN_SIZE is returned as keypress

yascreen_reqsize

inline void yascreen_reqsize(yascreen *s);

request terminal to report its size via ANSI sequence

yascreen_set_hint_i

inline void yascreen_set_hint_i(yascreen *s,int hint);

yascreen_get_hint_i

inline int yascreen_get_hint_i(yascreen *s);

yascreen_set_hint_p

inline void yascreen_set_hint_p(yascreen *s,void *hint);

yascreen_get_hint_p

inline void *yascreen_get_hint_p(yascreen *s);

get/set opaque hint values

integer and pointer hints are stored separately and both can be used at the same time

these are useful to link the yascreen instance to user program data

for example a single output callback may output to socket or a terminal, depending on the hint values

yascreen_line_flush

inline void yascreen_line_flush(yascreen *s,int on);

enable/disable auto flush for line and direct screen oriented operations

yascreen versions before 1.77 didn’t use buffered output and would immediately send the output to the screen

disabling internal flush can help an application optimize the number of write calls at the cost of performing explicit flush after each group of operations

explicit flush example:

yascreen_write(s,"",0);

yascreen_getwch_to

inline wchar_t yascreen_getwch_to(yascreen *s,int timeout);

wait for a key, return wide character or extended keycode, wait no more than timeout in milliseconds

yascreen_getwch_to does not work in non-unicode mode and will always return YAS_K_NONE

mixing the utf8 and wide character input modes will break on multibyte utf8 sequences and is not supported

yascreen_getwch

yascreen_getwch(s)

get a key as wide character without timeout

this macro expands to yascreen_getwch_to(s,0)

zero timeout=wait forever

yascreen_getwch does not work in non-unicode mode and will always return YAS_K_NONE

yascreen_getwch_nowait

yascreen_getwch_nowait(s)

get a key as a wide character, if available, return immediately

this macro expands to yascreen_getwch_to(s,-1)

negative timeout=do not wait

yascreen_getwch_nowait does not work in non-unicode mode and will always return YAS_K_NONE

yascreen_ungetwch

inline void yascreen_ungetwch(yascreen *s,wchar_t key);

put back wide character key value in key buffer

the internal key buffer is dynamically allocated, hence there is no limit of how many key codes may be put back, but in case of memory allocation failure, the error will not be reported and the key will not be put into the buffer

the internal key buffer contains utf8 and the wide character will be expanded to the appropriate utf8 sequence

yascreen_ungetwch does not do anything in non-unicode mode

yascreen_peekwch

inline wchar_t yascreen_peekwch(yascreen *s);

peek for key without removing it from input queue

yascreen_peekwch does not work in non-unicode mode and will always return YAS_K_NONE


Updated 2024-01-29 - jenkler.se | uex.se