Debian Codegraph @

upstream/opensuse-leap-16-1/man3/initscr.3ncurses

raw
1 .\"***************************************************************************
2 .\" Copyright 2018-2024,2025 Thomas E. Dickey *
3 .\" Copyright 1998-2016,2017 Free Software Foundation, Inc. *
4 .\" *
5 .\" Permission is hereby granted, free of charge, to any person obtaining a *
6 .\" copy of this software and associated documentation files (the *
7 .\" "Software"), to deal in the Software without restriction, including *
8 .\" without limitation the rights to use, copy, modify, merge, publish, *
9 .\" distribute, distribute with modifications, sublicense, and/or sell *
10 .\" copies of the Software, and to permit persons to whom the Software is *
11 .\" furnished to do so, subject to the following conditions: *
12 .\" *
13 .\" The above copyright notice and this permission notice shall be included *
14 .\" in all copies or substantial portions of the Software. *
15 .\" *
16 .\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 .\" OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 .\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 .\" IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 .\" DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 .\" OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 .\" THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 .\" *
24 .\" Except as contained in this notice, the name(s) of the above copyright *
25 .\" holders shall not be used in advertising or otherwise to promote the *
26 .\" sale, use or other dealings in this Software without prior written *
27 .\" authorization. *
28 .\"***************************************************************************
29 .\"
30 .\" $Id: curs_initscr.3x,v 1.102 2025/03/15 21:11:24 tom Exp $
31 .TH initscr 3NCURSES 2025-03-15 "ncurses 6.5" "Library calls"
32 .ie \n(.g \{\
33 .ds `` \(lq
34 .ds '' \(rq
35 .\}
36 .el \{\
37 .ie t .ds `` ``
38 .el .ds `` ""
39 .ie t .ds '' ''
40 .el .ds '' ""
41 .\}
42 .
43 .de bP
44 .ie n .IP \(bu 4
45 .el .IP \(bu 2
46 ..
47 .SH NAME
48 \fB\%initscr\fP,
49 \fB\%newterm\fP,
50 \fB\%endwin\fP,
51 \fB\%isendwin\fP,
52 \fB\%set_term\fP,
53 \fB\%delscreen\fP \-
54 initialize, manipulate, or tear down \fIcurses\fR terminal interface
55 .SH SYNOPSIS
56 .nf
57 \fB#include <ncursesw/curses.h>
58 .PP
59 \fBWINDOW * initscr(void);
60 \fBint endwin(void);
61 .PP
62 \fBbool isendwin(void);
63 .PP
64 \fBSCREEN * newterm(const char * \fItype\fP, FILE * \fIoutf\fP, FILE * \fIinf\fP);
65 \fBSCREEN * set_term(SCREEN * \fInew\fP);
66 \fBvoid delscreen(SCREEN * \fIsp\fP);
67 .fi
68 .SH DESCRIPTION
69 .SS initscr
70 .B \%initscr
71 determines the terminal type and initializes the library's
72 .IR SCREEN ","
73 .IR WINDOW ","
74 and other data structures.
75 It is normally the first
76 .I curses
77 function call a program performs.
78 However,
79 an application with unusual needs might employ a few other
80 .I curses
81 functions beforehand:
82 .bP
83 \fB\%slk_init\fP(3NCURSES) to set up soft-label keys;
84 .bP
85 \fB\%filter\fP(3NCURSES) if the program is designed to operate in a process
86 pipeline;
87 .bP
88 \fB\%ripoffline\fP(3NCURSES) to reserve up to five lines at the top and/or
89 bottom of the screen from management by
90 .BR \%stdscr ","
91 the standard
92 .I curses
93 window;
94 and
95 .bP
96 \fB\%use_env\fP(3NCURSES) and/or \fB\%use_tioctl\fP(3NCURSES) to configure use of
97 the process environment and operating system's terminal driver,
98 respectively,
99 when determining the dimensions of the terminal display.
100 .PP
101 Further,
102 a
103 .I curses
104 program might call
105 .B \%newterm
106 prior to or instead of
107 .B \%initscr
108 in two specialized cases described in its subsection below.
109 .PP
110 .B \%initscr
111 causes the first \fB\%refresh\fP(3NCURSES) call to clear the screen.
112 If errors occur,
113 .B \%initscr
114 writes an appropriate diagnostic message to the standard error stream
115 and exits;
116 otherwise,
117 it returns a pointer to
118 .BR stdscr "."
119 .SS newterm
120 An application that manages multiple terminals should call
121 .B \%newterm
122 once for each such device
123 .I instead
124 of
125 .BR \%initscr "."
126 .BR \%newterm 's
127 arguments are
128 .bP
129 the
130 .I type
131 of the associated terminal,
132 or a null pointer to use the
133 .I TERM
134 environment variable;
135 .bP
136 an output stream
137 .I outf
138 connected to the terminal;
139 and
140 .bP
141 an input stream
142 .I inf
143 connected to the terminal.
144 It returns a variable of
145 .RI pointer-to- SCREEN
146 type,
147 which should be saved for later use with
148 .B \%set_term
149 and
150 .BR \%delscreen "."
151 .PP
152 .B \%newterm
153 passes the file descriptor of the output stream to the
154 .I \%term\%info
155 function \fB\%setupterm\fP(3NCURSES),
156 which returns a pointer to a
157 .I \%TERMINAL
158 structure that
159 .B \%newterm
160 stores in the
161 .I SCREEN
162 it returns to the application.
163 .PP
164 An application that needs to inspect a terminal type's capabilities,
165 so that it can continue to run in a line-oriented mode
166 if the terminal cannot support a screen-oriented program,
167 would also use
168 .BR \%newterm "."
169 If at most one terminal connection is needed,
170 the programmer could perform such a capability test,
171 decide which mode in which to operate,
172 then call
173 .B \%delscreen
174 on the pointer returned by
175 .BR \%newterm ","
176 and proceed with either
177 .B \%initscr
178 or a
179 .RI non- curses
180 interface.
181 .SS endwin
182 The program must also call
183 .B \%endwin
184 for each terminal being used before exiting from
185 .IR curses "."
186 If
187 .B \%newterm
188 is called more than once for the same terminal,
189 the first terminal referred to must be the last one for which
190 .B \%endwin
191 is called.
192 .PP
193 A program should always call
194 .B \%endwin
195 before exiting the application
196 or temporarily suspending
197 .IR curses "'s"
198 management of the terminal.
199 .BR \%endwin ":"
200 .bP
201 resets colors to correspond with the default color pair 0,
202 .bP
203 moves the cursor to the lower left-hand corner of the screen,
204 .bP
205 clears the remainder of the line so that it uses the default colors,
206 .bP
207 sets the cursor to normal visibility
208 (see \fB\%curs_set\fP(3NCURSES)),
209 .bP
210 if applicable,
211 stops cursor-addressing mode using the
212 .B \%exit_ca_mode
213 .RB \%( rmcup )
214 terminal capability,
215 and
216 .bP
217 restores terminal modes (see \fB\%reset_shell_mode\fP(3NCURSES)).
218 .PP
219 Calling \fB\%refresh\fP(3NCURSES) or \fB\%doupdate\fP(3NCURSES) after a
220 temporary suspension causes
221 .I curses
222 to resume managing the terminal.
223 .SS isendwin
224 .B \%isendwin
225 returns
226 .B TRUE
227 if
228 .B \%endwin
229 has been called without any subsequent calls to \fB\%wrefresh\fP(3NCURSES),
230 and
231 .B FALSE
232 otherwise.
233 .SS set_term
234 .B \%set_term
235 re-orients the
236 .I curses
237 library's operations to another terminal
238 when the application has arranged to manage more than one with
239 .BR \%newterm "."
240 .B \%set_term
241 expects a
242 .I SCREEN
243 pointer previously returned by
244 .B \%newterm
245 as an argument,
246 and returns the previous one.
247 .B \%set_term
248 is the only
249 .I curses
250 API function that manipulates
251 .I SCREEN
252 pointers;
253 all others affect only the current terminal.
254 .SS delscreen
255 .B \%delscreen
256 frees the storage backing the supplied
257 .I SCREEN
258 pointer argument.
259 .B \%endwin
260 does not,
261 so that an application can resume managing a terminal with
262 .I curses
263 after a
264 (possibly conditional or temporary)
265 suspension;
266 see \fB\%kernel\fP(3NCURSES).
267 Call
268 .B \%delscreen
269 after
270 .B \%endwin
271 when a particular
272 .I SCREEN
273 structure
274 is no longer needed.
275 .SH RETURN VALUE
276 .B \%delscreen
277 returns no value.
278 .B \%endwin
279 returns
280 .B OK
281 on success and
282 .B ERR
283 on failure.
284 .B \%isendwin
285 returns
286 .B TRUE
287 or
288 .B FALSE
289 as described above.
290 .PP
291 In
292 .IR \%ncurses ","
293 .bP
294 .B \%endwin
295 returns
296 .B ERR
297 if
298 .RS
299 .bP
300 the terminal was not initialized,
301 .bP
302 it is called more than once without updating the screen,
303 or
304 .bP
305 its call of \fB\%reset_shell_mode\fP(3NCURSES) returns
306 .BR ERR "."
307 .RE
308 .bP
309 .B \%newterm
310 returns
311 .B ERR
312 if it cannot allocate storage for the
313 .I SCREEN
314 structure
315 or the
316 .I WINDOW
317 structures automatically associated with it:
318 .BR \%curscr ","
319 .BR \%newscr ","
320 and
321 .BR \%stdscr "."
322 .PP
323 Functions that return pointers return null pointers on error.
324 In
325 .IR \%ncurses ","
326 .B \%set_term
327 does not fail,
328 and
329 .B \%initscr
330 exits the application if it does not operate successfully.
331 .SH NOTES
332 .I \%ncurses
333 establishes signal handlers when a function that initializes a
334 .IR SCREEN ","
335 either
336 .B \%initscr
337 or
338 .BR \%newterm ","
339 is first called.
340 Applications that wish to handle the following signals themselves
341 should set up their corresponding handlers
342 .I after
343 initializing the screen.
344 .TP
345 .I SIGINT
346 .IR \%ncurses 's
347 handler
348 .I attempts
349 to clean up the screen on exit.
350 Although it
351 .I usually
352 works as expected,
353 there are limitations.
354 .RS
355 .bP
356 Walking the
357 .I SCREEN
358 list is unsafe, since all list management
359 is done without any signal blocking.
360 .bP
361 When an application has been built with the
362 .I \%_REENTRANT
363 macro defined
364 (and corresponding system support),
365 .B \%set_term
366 uses functions that could deadlock or misbehave in other ways.
367 .bP
368 .B \%endwin
369 calls other functions,
370 many of which use \fI\%stdio\fP(3) or other library functions that are
371 clearly unsafe.
372 .RE
373 .TP
374 .I SIGTERM
375 .I \%ncurses
376 uses the same handler as for
377 .IR \%SIGINT ","
378 with the same limitations.
379 It is not mentioned in X/Open Curses,
380 but is more suitable for this purpose than
381 .I \%SIGQUIT
382 (which is used in debugging).
383 .TP
384 .I SIGTSTP
385 .IR \%ncurses 's
386 handler manages the terminal-generated stop signal,
387 used in job control.
388 When resuming the process,
389 .I \%ncurses
390 discards pending input with \fB\%flushinp\fP(3NCURSES)
391 and repaints the screen,
392 assuming that it has been completely altered.
393 It also updates the saved terminal modes with
394 \fB\%def_shell_mode\fP(3NCURSES).
395 .TP
396 .I SIGWINCH
397 .I \%ncurses
398 handles changes to the terminal's window size,
399 a phenomenon ignored in standardization efforts.
400 It sets a (signal-safe) variable
401 that is later tested by \fB\%wgetch\fP(3NCURSES) and \fB\%wget_wch\fP(3NCURSES).
402 .RS
403 .bP
404 .B \%wgetch
405 returns the key code
406 .BR \%KEY_RESIZE "."
407 .bP
408 .B \%wget_wch
409 returns
410 .B \%KEY_CODE_YES
411 and sets its
412 .I wch
413 parameter to
414 .BR \%KEY_RESIZE "."
415 .RE
416 .IP
417 At the same time,
418 .I \%ncurses
419 calls \fB\%resizeterm\fP(3NCURSES)
420 to adjust the standard screen
421 .BR \%stdscr ","
422 and update global variables such as
423 .B LINES
424 and
425 .BR COLS "."
426 .SH PORTABILITY
427 X/Open Curses Issue\ 4 describes these functions.
428 It specifies no error conditions for them.
429 .SS Differences
430 X/Open Curses specifies that portable applications must not
431 call
432 .I \%initscr
433 more than once.
434 .bP
435 The portable way to use
436 .I \%initscr
437 is once only,
438 using
439 .I \%refresh
440 to restore the screen after
441 .IR \%endwin "."
442 .bP
443 .I \%ncurses
444 permits use of
445 .I \%initscr
446 after
447 .IR \%endwin "."
448 .PP
449 .I \%initscr
450 in BSD,
451 from its inception (1980) through the Net/2 release (1991) returned
452 .I ERR
453 cast to a
454 .I \%WINDOW
455 pointer when detecting an error.
456 .\" https://minnie.tuhs.org/cgi-bin/utree.pl?file=4BSD/usr/src/lib/libcurses/initscr.c
457 .\" https://minnie.tuhs.org/cgi-bin/utree.pl?file=Net2/usr/src/lib/libcurses/initscr.c
458 4.4BSD (1995)
459 instead returned a null pointer.
460 .\" https://minnie.tuhs.org/cgi-bin/utree.pl?file=4.4BSD/usr/src/lib/libcurses/initscr.c
461 Neither exited the application.
462 It is safe but redundant to check the return value of
463 .I \%initscr
464 in X/Open Curses.
465 .PP
466 Calling
467 .I \%endwin
468 does not dispose of the memory allocated by
469 .I \%initscr
470 or
471 .IR \%newterm "."
472 Deleting a
473 .I SCREEN
474 provides a way to do this.
475 .bP
476 X/Open Curses does not say what happens to
477 .IR \%WINDOW s
478 when
479 .I \%delscreen
480 \*(``frees storage associated with the
481 .IR SCREEN "\*(''"
482 nor does the SVr4 documentation help,
483 adding that it should be called after
484 .I \%endwin
485 if a
486 .I SCREEN
487 is no longer needed.
488 .bP
489 However,
490 .IR \%WINDOW s
491 are implicitly associated with a
492 .IR SCREEN ","
493 so it is reasonable to expect
494 .I \%delscreen
495 to dispose of them.
496 .bP
497 SVr4 deletes the standard
498 .I \%WINDOW
499 structures
500 .I \%stdscr
501 and
502 .I \%curscr
503 as well as a work area
504 .IR \%newscr "."
505 It ignores other windows.
506 .bP
507 Since version 4.0 (1996),
508 .I \%ncurses
509 has maintained a list of all windows for each screen,
510 using that information to delete those windows when
511 .I \%delscreen
512 is
513 called.
514 .bP
515 NetBSD copied this feature of
516 .I \%ncurses
517 in 2001.
518 .I \%PDCurses
519 follows the SVr4 model,
520 deleting only the standard
521 .I \%WINDOW
522 structures.
523 .SS "High-level versus Low-level"
524 Different implementations may disagree regarding the level of some functions.
525 For example,
526 .I SCREEN
527 (returned by
528 .IR \%newterm ")"
529 and
530 .I \%TERMINAL
531 (returned by \fB\%setupterm\fP(3NCURSES)) hold file
532 descriptors for the output stream.
533 If an application switches screens using
534 .IR \%set_term ","
535 or switches terminals using \fB\%set_curterm\fP(3NCURSES),
536 applications which use the output file descriptor can have different
537 behavior depending on which structure holds the corresponding descriptor.
538 .bP
539 NetBSD's
540 .I \%baudrate
541 function uses the descriptor in
542 .IR \%TERMINAL "."
543 .I \%ncurses
544 and SVr4 use the descriptor in
545 .IR SCREEN "."
546 .bP
547 NetBSD and
548 .I \%ncurses
549 use the descriptor
550 in
551 .I \%TERMINAL
552 for terminal I/O modes,
553 e.g.,
554 \fB\%def_shell_mode\fP(3NCURSES),
555 \fB\%def_prog_mode\fP(3NCURSES).
556 SVr4 uses the descriptor in
557 .IR SCREEN "."
558 .SS "Unset \fITERM\fP Environment Variable"
559 If the
560 .I TERM
561 variable is not set in the environment or has an empty value,
562 .I \%initscr
563 uses the value \*(``unknown\*('',
564 which normally corresponds to a terminal entry with the
565 .B \%generic
566 .RB ( gn )
567 capability.
568 Generic entries are detected by \fB\%setupterm\fP(3NCURSES)
569 and cannot be used for full-screen operation.
570 Other implementations may handle
571 a missing or empty
572 .I TERM
573 variable differently.
574 .SS "Signal Handlers"
575 Quoting X/Open Curses Issue\ 7,
576 section 3.1.1:
577 .RS 5
578 .PP
579 Curses implementations may provide for special handling of the
580 \%SIGINT,
581 \%SIGQUIT,
582 and \%SIGTSTP signals if their disposition is \%SIG_DFL at the time
583 .IR \%initscr ()
584 is called.\|.\|.
585 .PP
586 Any special handling for these signals may remain in effect for the
587 life of the process or until the process changes the disposition of
588 the signal.
589 .PP
590 None of the Curses functions are required to be safe
591 with respect to signals.\|.\|.
592 .RE
593 .PP
594 Section \*(``NOTES\*('' above discusses
595 .IR \%ncurses 's
596 signal handlers.
597 .SH HISTORY
598 4BSD (1980)
599 introduced
600 .I \%initscr
601 and
602 .IR \%endwin "."
603 .PP
604 SVr2 (1984)
605 added
606 .I \%newterm
607 and
608 .IR \%set_term "."
609 .PP
610 SVr3.1 (1987)
611 supplied
612 .I \%delscreen
613 and
614 .IR \%isendwin "."
615 .SH SEE ALSO
616 \fB\%ncurses\fP(3NCURSES),
617 \fB\%kernel\fP(3NCURSES),
618 \fB\%refresh\fP(3NCURSES),
619 \fB\%slk\fP(3NCURSES),
620 \fB\%terminfo\fP(3NCURSES),
621 \fB\%util\fP(3NCURSES),
622 \fB\%curses_variables\fP(3NCURSES)