2 .\"***************************************************************************
3 .\" Copyright 2018-2023,2024 Thomas E. Dickey *
4 .\" Copyright 1998-2015,2017 Free Software Foundation, Inc. *
6 .\" Permission is hereby granted, free of charge, to any person obtaining a *
7 .\" copy of this software and associated documentation files (the *
8 .\" "Software"), to deal in the Software without restriction, including *
9 .\" without limitation the rights to use, copy, modify, merge, publish, *
10 .\" distribute, distribute with modifications, sublicense, and/or sell *
11 .\" copies of the Software, and to permit persons to whom the Software is *
12 .\" furnished to do so, subject to the following conditions: *
14 .\" The above copyright notice and this permission notice shall be included *
15 .\" in all copies or substantial portions of the Software. *
17 .\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
18 .\" OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
19 .\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
20 .\" IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
21 .\" DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
22 .\" OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
23 .\" THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
25 .\" Except as contained in this notice, the name(s) of the above copyright *
26 .\" holders shall not be used in advertising or otherwise to promote the *
27 .\" sale, use or other dealings in this Software without prior written *
29 .\"***************************************************************************
31 .\" $Id: curs_util.3x,v 1.112 2024/06/22 21:25:23 tom Exp $
32 .TH curs_util 3X 2024-06-22 "ncurses @NCURSES_MAJOR@.@NCURSES_MINOR@" "Library calls"
61 miscellaneous \fIcurses\fR utility routines
64 \fB#include <curses.h>
66 \fBconst char *unctrl(chtype \fIch\fP);
67 \fBwchar_t *wunctrl(cchar_t *\fIwch\fP);
69 \fBconst char *keyname(int \fIc\fP);
70 \fBconst char *key_name(wchar_t \fIwc\fP);
74 \fBvoid use_env(bool \fIbf\fP);
76 \fBint putwin(WINDOW *\fIwin\fP, FILE *\fIfilep\fP);
77 \fBWINDOW *getwin(FILE *\fIfilep\fP);
79 \fBint delay_output(int \fIms\fP);
80 \fBint flushinp(void);
83 \fBvoid nofilter(void);
84 \fBvoid use_tioctl(bool \fIbf\fP);
88 The \fBunctrl\fP routine returns a character string as a printable
89 representation of the character \fIch\fP:
91 Printable characters are displayed as themselves,
93 a one-character string containing the key.
95 Control characters are displayed in the \fB^\fIX\fR notation.
97 Printing characters are displayed as is.
101 is displayed as \fB^?\fP.
103 Values above 128 are either meta characters
104 (if the screen has not been initialized,
105 or if \fBmeta\fP(3X) has been called with a \fBTRUE\fP parameter),
106 shown in the \fBM\-\fIX\fR notation,
107 or are displayed as themselves.
109 the values may not be printable;
110 this follows the X/Open specification.
112 The corresponding \fBwunctrl\fP returns a printable representation of
113 a complex character \fIwch\fP.
115 In both \fBunctrl\fP and \fBwunctrl\fP the attributes
117 with the character parameter are ignored.
118 .SS "keyname, key_name"
119 The \fBkeyname\fP routine returns a character string
120 corresponding to the key \fIc\fP.
121 Key codes are different from character codes.
123 Key codes below 256 are characters.
124 They are displayed using \fBunctrl\fP.
126 Values above 256 may be the codes for function keys.
127 The function key name is displayed.
130 (if there is no corresponding name and the key is not a character)
131 the function returns null,
133 X/Open also lists an \*(``UNKNOWN KEY\*('' return value,
134 which some implementations return rather than null.
136 The corresponding \fBkey_name\fP returns
137 a multibyte character string corresponding
138 to the wide-character value \fIwc\fP.
140 (\fBkeyname\fP and \fBkey_name\fP)
141 do not return the same set of strings:
143 \fBkeyname\fP returns null where \fBkey_name\fP
144 would display a meta character.
146 \fBkey_name\fP does not return the name of a function key.
147 .SS "filter, nofilter"
148 The \fBfilter\fP routine,
150 must be called before \fBinitscr\fP or
151 \fBnewterm\fP are called.
152 Calling \fBfilter\fP causes these changes in initialization:
154 \fBLINES\fP is set to 1;
166 the capability \fBed\fP is disabled if \fBbce\fP is set;
168 and the \fBhome\fP string is set to the value of \fBcr\fP.
170 The \fBnofilter\fP routine cancels the effect
171 of a preceding \fBfilter\fP call.
172 That allows the caller to initialize a screen on a different device,
173 using a different value of \fB$TERM\fP.
174 The limitation arises because the \fBfilter\fP routine modifies the
175 in-memory copy of the terminal information.
177 The \fBuse_env\fP routine,
179 should be called before \fBinitscr\fP or
180 \fBnewterm\fP are called
181 (because those compute the screen size).
182 It modifies the way \fI\%ncurses\fP treats environment variables
183 when determining the screen size.
185 Normally \fI\%ncurses\fP looks first at the terminal database for the
188 If \fBuse_env\fP was called with \fBFALSE\fP for parameter,
190 \fBuse_tioctl\fP was also called with \fBTRUE\fP for parameter.
192 Then it asks for the screen size via operating system calls.
194 it overrides the values from the terminal database.
197 (unless \fBuse_env\fP was called with \fBFALSE\fP parameter),
198 \fI\%ncurses\fP examines the \fILINES\fP or \fI\%COLUMNS\fP environment
200 using a value in those to override the results
201 from the operating system or terminal database.
203 \fI\%curses\fP also updates the screen size in response to
205 unless overridden by the \fILINES\fP or \fI\%COLUMNS\fP environment
208 The \fBuse_tioctl\fP routine,
210 should be called before \fBinitscr\fP or \fBnewterm\fP are called
211 (because those compute the screen size).
212 After \fBuse_tioctl\fP is called with \fBTRUE\fP as an argument,
213 \fI\%ncurses\fP modifies the last step in its computation
214 of screen size as follows:
216 checks if the \fILINES\fP and \fI\%COLUMNS\fP environment variables
217 are set to a number greater than zero.
220 \fI\%ncurses\fP updates the corresponding environment variable
221 with the value that it has obtained via operating system call
222 or from the terminal database.
224 \fI\%ncurses\fP re-fetches the value of the environment variables so
225 that it is still the environment variables that set the screen size.
227 The \fB\%use_env\fP and \fB\%use_tioctl\fP routines combine as follows.
232 use_env use_tioctl Summary
235 \fI\%ncurses\fP uses operating system calls
236 unless overridden by \fILINES\fP or \fI\%COLUMNS\fP environment
241 \fI\%ncurses\fP updates \fILINES\fP and \fI\%COLUMNS\fP based on
242 operating system calls.
245 \fI\%ncurses\fP ignores \fILINES\fP and \fI\%COLUMNS\fP,
246 using operating system calls to obtain size.
250 The \fBputwin\fP routine writes all data associated
254 the file to which \fIfilep\fP points.
255 This information can be later retrieved
256 using the \fBgetwin\fP function.
258 The \fBgetwin\fP routine reads window related data stored in the file by
260 The routine then creates and initializes a new window using that
262 It returns a pointer to the new window.
263 There are a few caveats:
265 the data written is a copy of the \fI\%WINDOW\fP structure,
266 and its associated character cells.
267 The format differs between the wide-character \%(\fIncursesw\fP) and
268 non-wide \%(\fIncurses\fP) libraries.
269 You can transfer data between the two,
272 the retrieved window is always created as a top-level window
274 rather than a subwindow.
276 the window's character cells contain the color pair \fIvalue\fP,
277 but not the actual color \fInumbers\fP.
278 If cells in the retrieved window use color pairs that have not been
279 created in the application using \fBinit_pair\fP,
280 they will not be colored when the window is refreshed.
282 The \fBdelay_output\fP routine inserts an \fIms\fP millisecond pause
284 Employ this function judiciously when terminal output uses padding,
285 because \fI\%ncurses\fP transmits null characters
286 (consuming CPU and I/O resources)
287 instead of sleeping and requesting resumption from the operating system.
288 Padding is used unless:
290 the terminal description has \fBnpc\fP (\fBno_pad_char\fP) capability,
293 the environment variable \fB\%NCURSES_NO_PADDING\fP is set.
295 If padding is not in use,
296 \fI\%ncurses\fP uses \fBnapms\fP to perform the delay.
297 If the value of \fIms\fP exceeds 30,000
299 it is capped at that value.
301 The \fBflushinp\fP routine throws away any typeahead
302 that has been typed by the user
303 and has not yet been read by the program.
305 Except for \fBflushinp\fP,
306 routines that return an integer
307 return \fBERR\fP upon failure and \fBOK\fP
308 (SVr4 specifies only "an integer value other than \fBERR\fP")
309 upon successful completion.
311 Routines that return pointers return \fBNULL\fP on error.
313 X/Open Curses does not specify any error conditions.
314 In this implementation
320 if the terminal was not initialized.
325 if the associated \fIwrite\fP(2) calls return
330 The SVr4 documentation describes the action of \fBfilter\fP
331 only in the vaguest terms.
332 The description here is adapted from X/Open Curses
333 (which erroneously fails to describe the disabling of \fBcuu\fP).
334 .SS "delay_output padding"
335 The limitation to 30 seconds
336 and the use of \fBnapms\fP
337 differ from other implementations.
339 SVr4 curses does not delay if no padding character is available.
341 NetBSD curses uses \fBnapms\fP when no padding character is available,
342 but does not take timing into account when using the padding character.
344 Neither limits the delay.
346 The \fBkeyname\fP function may return the names of user-defined
347 string capabilities that are defined in the terminfo entry
349 option of \fB@TIC@\fP.
350 This implementation automatically assigns at run-time key codes to
351 user-defined strings that begin with \*(``k\*(''.
352 The key codes start at KEY_MAX,
353 but are not guaranteed to be the same value for different runs
354 because user-defined codes are merged
355 from all terminal descriptions that have been loaded.
356 The \fBuse_extended_names\fP(3X) function controls whether this data is
357 loaded when the terminal description is read by the library.
358 .SS "nofilter, use_tioctl"
359 The \fBnofilter\fP and \fBuse_tioctl\fP routines are specific to
361 They were not supported on Version 7,
362 BSD or System V implementations.
363 It is recommended that any code depending on \fI\%ncurses\fP extensions
364 be conditioned using \fBNCURSES_VERSION\fP.
365 .SS "putwin/getwin file-format"
366 The \fBputwin\fP and \fBgetwin\fP functions have several issues with
369 The files written and read by these functions
370 use an implementation-specific format.
371 Although the format is an obvious target for standardization,
372 it has been overlooked.
374 Interestingly enough,
375 according to the copyright dates in Solaris source,
377 (along with \fBscr_init\fP,
379 originated with the University of California,
384 incorporated into SVr4.
386 there are no such functions in the 4.3BSD curses sources.
388 Most implementations simply dump the binary \fI\%WINDOW\fP structure
390 These include SVr4 curses,
392 as well as older \fI\%ncurses\fP versions.
394 (as well as the X/Open variant of Solaris curses,
398 The implementations that use binary dumps use block-I/O
399 (\fIwrite\fP(2) and \fIread\fP(2) functions).
400 Those that use textual dumps use buffered-I/O.
401 A few applications may happen to write extra data in the file using
403 Doing that can run into problems mixing block- and buffered-I/O.
404 This implementation reduces the problem on writes by flushing the
407 reading from a file written using mixed schemes may not be successful.
408 .SS "unctrl, wunctrl"
410 Issue 4 describes these functions.
411 It states that \fBunctrl\fP and \fBwunctrl\fP will return a null pointer
413 but does not define any error conditions.
414 This implementation checks for three cases:
416 the parameter is a 7-bit US\-ASCII code.
417 This is the case that X/Open Curses documented.
419 the parameter is in the range 128\-159,
422 If \fBuse_legacy_coding\fP(3X) has been called with a \fB2\fP parameter,
423 \fBunctrl\fP returns the parameter,
425 a one-character string with
426 the parameter as the first character.
428 it returns \*(``~@\*('',
431 analogous to \*(``^@\*('',
435 X/Open Curses does not document whether \fBunctrl\fP can be called
436 before initializing curses.
437 This implementation permits that,
438 and returns the \*(``~@\*('',
442 parameter values outside the 0 to 255 range.
443 \fBunctrl\fP returns a null pointer.
445 The strings returned by \fBunctrl\fP in this implementation
446 are determined at compile time,
447 showing C1 controls from the upper-128 codes
448 with a \*(``~\*('' prefix rather than \*(``^\*(''.
449 Other implementations have different conventions.
451 they may show both sets of control characters with \*(``^\*('',
452 and strip the parameter to 7 bits.
453 Or they may ignore C1 controls and treat all of the upper-128 codes as
455 This implementation uses 8 bits
456 but does not modify the string to reflect locale.
457 The \fBuse_legacy_coding\fP(3X) function allows the caller to
458 change the output of \fBunctrl\fP.
461 the \fBmeta\fP(3X) function allows the caller to change the output
464 it determines whether to use the \*(``M\-\*('' prefix
465 for \*(``meta\*('' keys (codes in the range 128 to 255).
466 Both \fBuse_legacy_coding\fP(3X) and \fBmeta\fP(3X) succeed only after
467 curses is initialized.
468 X/Open Curses does not document the treatment of codes 128 to 159.
469 When treating them as \*(``meta\*('' keys
470 (or if \fBkeyname\fP is called before initializing curses),
471 this implementation returns strings \*(``M\-^@\*('',
475 X/Open Curses documents \fBunctrl\fP as declared in \fB<unctrl.h>\fP,
476 which \fI\%ncurses\fP does.
478 \fI\%ncurses\fP' \fB<curses.h>\fP includes \fB<unctrl.h>\fP,
479 matching the behavior of SVr4 curses.
480 Other implementations may not do that.
481 .SS "use_env, use_tioctl"
482 If \fI\%ncurses\fP is configured to provide the sp-functions extension,
483 the state of \fBuse_env\fP and \fBuse_tioctl\fP may be updated before
484 creating each \fIscreen\fP rather than once only
485 (\fBcurs_sp_funcs\fP(3X)).
486 This feature of \fBuse_env\fP
487 is not provided by other implementations of curses.
496 SVr2 (1984) introduced
497 .IR \%delay_output ","
509 reading and writing window dumps
510 with \fI\%fread\fP(3) and \fI\%fwrite\fP(3),
516 X/Open Curses Issue 4 (1995) specified
523 .IR \%nofilter "," \" 20060107
525 .IR \%use_tioctl "." \" 20120714
528 \fB\%curs_initscr\fP(3X),
529 \fB\%curs_inopts\fP(3X),
530 \fB\%curs_kernel\fP(3X),
531 \fB\%curs_scr_dump\fP(3X),
532 \fB\%curs_sp_funcs\fP(3X),
533 \fB\%curs_variables\fP(3X),
534 \fB\%legacy_coding\fP(3X)