1ecc16c8dSEd Schouten.\" Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org> 2ecc16c8dSEd Schouten.\" All rights reserved. 3ecc16c8dSEd Schouten.\" 4ecc16c8dSEd Schouten.\" Redistribution and use in source and binary forms, with or without 5ecc16c8dSEd Schouten.\" modification, are permitted provided that the following conditions 6ecc16c8dSEd Schouten.\" are met: 7ecc16c8dSEd Schouten.\" 1. Redistributions of source code must retain the above copyright 8ecc16c8dSEd Schouten.\" notice, this list of conditions and the following disclaimer. 9ecc16c8dSEd Schouten.\" 2. Redistributions in binary form must reproduce the above copyright 10ecc16c8dSEd Schouten.\" notice, this list of conditions and the following disclaimer in the 11ecc16c8dSEd Schouten.\" documentation and/or other materials provided with the distribution. 12ecc16c8dSEd Schouten.\" 13ecc16c8dSEd Schouten.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14ecc16c8dSEd Schouten.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15ecc16c8dSEd Schouten.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16ecc16c8dSEd Schouten.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17ecc16c8dSEd Schouten.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18ecc16c8dSEd Schouten.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19ecc16c8dSEd Schouten.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20ecc16c8dSEd Schouten.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21ecc16c8dSEd Schouten.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22ecc16c8dSEd Schouten.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23ecc16c8dSEd Schouten.\" SUCH DAMAGE. 24ecc16c8dSEd Schouten.\" 25ecc16c8dSEd Schouten.\" $FreeBSD$ 26ecc16c8dSEd Schouten.\" 272610c9f2SBruce Evans.Dd Mar 13, 2017 28ecc16c8dSEd Schouten.Dt TEKEN 3 29ecc16c8dSEd Schouten.Os 30ecc16c8dSEd Schouten.Sh NAME 31ecc16c8dSEd Schouten.Nm teken 32ecc16c8dSEd Schouten.Nd xterm-like terminal emulation interface 33ecc16c8dSEd Schouten.Sh LIBRARY 34ecc16c8dSEd Schouten.Lb libteken 35ecc16c8dSEd Schouten.Sh SYNOPSIS 36ecc16c8dSEd Schouten.In teken.h 37ecc16c8dSEd Schouten.Ft void 38ecc16c8dSEd Schouten.Fn teken_init "teken_t *t" "const teken_funcs_t *funcs" "void *thunk" 39ecc16c8dSEd Schouten.Ft void 40ecc16c8dSEd Schouten.Fn teken_input "teken_t *t" "const void *buf" "size_t nbytes" 41ecc16c8dSEd Schouten.Ft const teken_pos_t * 42ecc16c8dSEd Schouten.Fn teken_get_winsize "teken_t *t" 43ecc16c8dSEd Schouten.Ft void 44ecc16c8dSEd Schouten.Fn teken_set_winsize "teken_t *t" "const teken_pos_t *size" 45ecc16c8dSEd Schouten.Ft const teken_pos_t * 46*92223bddSPoul-Henning Kamp.Fn teken_get_cursor "const teken_t *t" 47ecc16c8dSEd Schouten.Ft void 48ecc16c8dSEd Schouten.Fn teken_set_cursor "teken_t *t" "const teken_pos_t *pos" 49ecc16c8dSEd Schouten.Ft const teken_attr_t * 50*92223bddSPoul-Henning Kamp.Fn teken_get_curattr "const teken_t *t" 51ecc16c8dSEd Schouten.Ft void 52ecc16c8dSEd Schouten.Fn teken_set_curattr "teken_t *t" "const teken_attr_t *attr" 53ecc16c8dSEd Schouten.Ft const teken_attr_t * 54*92223bddSPoul-Henning Kamp.Fn teken_get_defattr "const teken_t *t" 55ecc16c8dSEd Schouten.Ft void 56ecc16c8dSEd Schouten.Fn teken_set_defattr "teken_t *t" "const teken_attr_t *attr" 57ecc16c8dSEd Schouten.Ft const char * 58*92223bddSPoul-Henning Kamp.Fn teken_get_sequence "const teken_t *t" "unsigned int id" 59ecc16c8dSEd Schouten.Ft teken_color_t 602610c9f2SBruce Evans.Fn teken_256to16 "teken_color_t color" 612610c9f2SBruce Evans.Ft teken_color_t 62ecc16c8dSEd Schouten.Fn teken_256to8 "teken_color_t color" 63ecc16c8dSEd Schouten.Ft void 64*92223bddSPoul-Henning Kamp.Fn teken_get_defattr_cons25 "const teken_t *t" "int *fg" "int *bg" 65ecc16c8dSEd Schouten.Ft void 66ecc16c8dSEd Schouten.Fn teken_set_8bit "teken_t *t" 67ecc16c8dSEd Schouten.Ft void 68ecc16c8dSEd Schouten.Fn teken_set_cons25 "teken_t *t" 69ecc16c8dSEd Schouten.Sh DESCRIPTION 70ecc16c8dSEd SchoutenThe 71ecc16c8dSEd Schouten.Nm 72ecc16c8dSEd Schoutenlibrary implements the input parser of a 256-color xterm-like terminal. 73ecc16c8dSEd SchoutenIt converts a stream of UTF-8 encoded characters into a series of 74ecc16c8dSEd Schoutenprimitive drawing instructions that can be used by a console driver or 75ecc16c8dSEd Schoutenterminal emulator to render a terminal application. 76ecc16c8dSEd Schouten.Pp 77ecc16c8dSEd SchoutenThe 78ecc16c8dSEd Schouten.Fn teken_init 79ecc16c8dSEd Schoutenfunction is used to initialize terminal state object 80ecc16c8dSEd Schouten.Fa t , 81ecc16c8dSEd Schoutenhaving type 82ecc16c8dSEd Schouten.Vt teken_t . 83ecc16c8dSEd SchoutenThe supplied 84ecc16c8dSEd Schouten.Vt teken_funcs_t 85ecc16c8dSEd Schoutenstructure 86ecc16c8dSEd Schouten.Fa funcs 87ecc16c8dSEd Schoutencontains a set of callback functions, which are called when supplying 88ecc16c8dSEd Schoutendata to 89ecc16c8dSEd Schouten.Fn teken_input . 90ecc16c8dSEd SchoutenThe 91ecc16c8dSEd Schouten.Fa thunk 92ecc16c8dSEd Schoutenargument stores an arbitrary pointer, which is passed to each invocation 93ecc16c8dSEd Schoutenof the callback functions. 94ecc16c8dSEd Schouten.Pp 95ecc16c8dSEd SchoutenThe 96ecc16c8dSEd Schouten.Vt teken_funcs_t 97ecc16c8dSEd Schoutenstructure stores the following callbacks: 98ecc16c8dSEd Schouten.Bd -literal -offset indent 99ecc16c8dSEd Schoutentypedef struct { 100ecc16c8dSEd Schouten tf_bell_t *tf_bell; /* Audible/visible bell. */ 101ecc16c8dSEd Schouten tf_cursor_t *tf_cursor; /* Move cursor to x/y. */ 102ecc16c8dSEd Schouten tf_putchar_t *tf_putchar; /* Put Unicode character at x/y. */ 103ecc16c8dSEd Schouten tf_fill_t *tf_fill; /* Fill rectangle with character. */ 104ecc16c8dSEd Schouten tf_copy_t *tf_copy; /* Copy rectangle to new location. */ 105ecc16c8dSEd Schouten tf_param_t *tf_param; /* Miscellaneous options. */ 106ecc16c8dSEd Schouten tf_respond_t *tf_respond; /* Send response string to user. */ 107ecc16c8dSEd Schouten} teken_funcs_t; 108ecc16c8dSEd Schouten.Ed 109ecc16c8dSEd Schouten.Pp 110ecc16c8dSEd SchoutenAll callbacks must be provided, though unimplemented callbacks may some 111ecc16c8dSEd Schoutentimes be sufficient. 112ecc16c8dSEd SchoutenThe actual types of these callbacks can be found in 113ecc16c8dSEd Schouten.In teken.h . 114ecc16c8dSEd Schouten.Pp 115ecc16c8dSEd SchoutenBy default, 116ecc16c8dSEd Schouten.Fn teken_init 117ecc16c8dSEd Schouteninitializes the 118ecc16c8dSEd Schouten.Vt teken_t 119ecc16c8dSEd Schoutenstructure to emulate a terminal having 24 rows and 80 columns. 120ecc16c8dSEd SchoutenThe 121ecc16c8dSEd Schouten.Fn teken_get_winsize 122ecc16c8dSEd Schoutenand 123ecc16c8dSEd Schouten.Fn teken_set_winsize 124ecc16c8dSEd Schoutenfunctions can be used to obtain and modify the dimensions of the 125ecc16c8dSEd Schoutenterminal. 126ecc16c8dSEd Schouten.Pp 127ecc16c8dSEd SchoutenEven though the cursor position is normally controlled by input of data 128ecc16c8dSEd Schoutenthrough 129ecc16c8dSEd Schouten.Fn teken_input 130ecc16c8dSEd Schoutenand returned by the 131ecc16c8dSEd Schouten.Fn tf_cursor 132ecc16c8dSEd Schoutencallback, it can be obtained and modified manually using the 133ecc16c8dSEd Schouten.Fn teken_get_cursor 134ecc16c8dSEd Schoutenand 135ecc16c8dSEd Schouten.Fn teken_set_cursor 136ecc16c8dSEd Schoutenfunctions. 137ecc16c8dSEd SchoutenThe same holds for 138ecc16c8dSEd Schouten.Fn teken_get_curattr 139ecc16c8dSEd Schoutenand 140ecc16c8dSEd Schouten.Fn teken_set_curattr , 141ecc16c8dSEd Schoutenwhich can be used to change the currently selected font attributes and 142ecc16c8dSEd Schoutenforeground and background color. 143ecc16c8dSEd Schouten.Pp 144ecc16c8dSEd SchoutenBy default, 145ecc16c8dSEd Schouten.Nm 146ecc16c8dSEd Schoutenemulates a white-on-black terminal, which means the default foreground 147ecc16c8dSEd Schoutencolor is white, while the background color is black. 148ecc16c8dSEd SchoutenThese defaults can be modified using 149ecc16c8dSEd Schouten.Fn teken_get_defattr 150ecc16c8dSEd Schoutenand 151ecc16c8dSEd Schouten.Fn teken_set_defattr . 152ecc16c8dSEd Schouten.Pp 153ecc16c8dSEd SchoutenThe 154ecc16c8dSEd Schouten.Fn teken_get_sequence 155ecc16c8dSEd Schoutenfunction is a utility function that can be used to obtain escape 156ecc16c8dSEd Schoutensequences of special keyboard keys, generated by user input. 157ecc16c8dSEd SchoutenThe 158ecc16c8dSEd Schouten.Fa id 159ecc16c8dSEd Schoutenparameter must be one of the 160ecc16c8dSEd Schouten.Dv TKEY_* 161ecc16c8dSEd Schoutenparameters listed in 162ecc16c8dSEd Schouten.In teken.h . 163ecc16c8dSEd Schouten.Sh LEGACY FEATURES 164ecc16c8dSEd SchoutenThis library also provides a set of functions that shouldn't be used in 165ecc16c8dSEd Schoutenany modern applications. 166ecc16c8dSEd Schouten.Pp 167ecc16c8dSEd SchoutenThe 1682610c9f2SBruce Evans.Fn teken_256to16 1692610c9f2SBruce Evansfunction converts an xterm-256 256-color code to an xterm 16-color code 1702610c9f2SBruce Evanswhose color with default palettes is as similar as possible (not very 1712610c9f2SBruce Evanssimilar). 1722610c9f2SBruce EvansThe lower 3 bits of the result are the ANSI color and the next lowest 1732610c9f2SBruce Evansbit is brightness. 1742610c9f2SBruce EvansOther layers (hardare and software) that only support 16 colors can use 1752610c9f2SBruce Evansthis to avoid knowing the details of 256-color codes. 1762610c9f2SBruce Evans.Pp 1772610c9f2SBruce EvansThe 178ecc16c8dSEd Schouten.Fn teken_256to8 1792610c9f2SBruce Evansfunction is similar to 1802610c9f2SBruce Evans.Fn teken_256to16 1812610c9f2SBruce Evansexcept it converts to an ANSI 8-color code. 1822610c9f2SBruce EvansThis is more accurate than discarding the brigtness bit in the result of 1832610c9f2SBruce Evans.Fn teken_256to16 . 184ecc16c8dSEd Schouten.Pp 185ecc16c8dSEd SchoutenThe 186ecc16c8dSEd Schouten.Fn teken_get_defattr_cons25 187ecc16c8dSEd Schoutenfunction obtains the default terminal attributes as a pair of foreground 188ecc16c8dSEd Schoutenand background colors, using ANSI color numbering. 189ecc16c8dSEd Schouten.Pp 190ecc16c8dSEd SchoutenThe 191ecc16c8dSEd Schouten.Fn teken_set_8bit 192ecc16c8dSEd Schoutenfunction disables UTF-8 processing and switches to 8-bit character mode, 193ecc16c8dSEd Schoutenwhich can be used to support character sets like CP437 and ISO-8859-1. 194ecc16c8dSEd Schouten.Pp 195ecc16c8dSEd SchoutenThe 196ecc16c8dSEd Schouten.Fn teken_set_cons25 197ecc16c8dSEd Schoutenfunction switches terminal emulation to 198ecc16c8dSEd Schouten.Dv cons25 , 199ecc16c8dSEd Schoutenwhich is used by versions of 200ecc16c8dSEd Schouten.Fx 201ecc16c8dSEd Schoutenprior to 9.0. 202ecc16c8dSEd Schouten.Sh SEE ALSO 203ecc16c8dSEd Schouten.Xr ncurses 3 , 204ecc16c8dSEd Schouten.Xr termcap 3 , 205068b48d6SJoel Dahl.Xr syscons 4 206ecc16c8dSEd Schouten.Sh HISTORY 207ecc16c8dSEd SchoutenThe 208ecc16c8dSEd Schouten.Nm 209ecc16c8dSEd Schoutenlibrary appeared in 210ecc16c8dSEd Schouten.Fx 8.0 , 211ecc16c8dSEd Schoutenthough it was only available and used inside the kernel. 212ecc16c8dSEd SchoutenIn 213ecc16c8dSEd Schouten.Fx 9.0 , 214ecc16c8dSEd Schoutenthe 215ecc16c8dSEd Schouten.Nm 216ecc16c8dSEd Schoutenlibrary appeared in userspace. 217ecc16c8dSEd Schouten.Sh AUTHORS 218ecc16c8dSEd Schouten.An Ed Schouten Aq ed@FreeBSD.org 219135bce2aSUlrich Spörlein.Sh SECURITY CONSIDERATIONS 220135bce2aSUlrich SpörleinThe 221135bce2aSUlrich Spörlein.Fn tf_respond 222135bce2aSUlrich Spörleincallback is used to respond to device status requests commands generated 223135bce2aSUlrich Spörleinby an application. 224135bce2aSUlrich SpörleinIn the past, there have been various security issues, where a malicious 225135bce2aSUlrich Spörleinapplication sends a device status request before termination, causing 226135bce2aSUlrich Spörleinthe generated response to be interpreted by applications such as 227135bce2aSUlrich Spörlein.Xr sh 1 . 228135bce2aSUlrich Spörlein.Pp 229135bce2aSUlrich Spörlein.Nm 230135bce2aSUlrich Spörleinonly implements a small subset of responses which are unlikely to cause 231135bce2aSUlrich Spörleinany harm. 232135bce2aSUlrich SpörleinStill, it is advised to leave 233135bce2aSUlrich Spörlein.Fn tf_respond 234135bce2aSUlrich Spörleinunimplemented. 235