1 /* Public Domain Curses */
5 RCSID("$Id: initscr.c,v 1.114 2008/07/13 16:08:18 wmcbrine Exp $")
7 /*man-start**************************************************************
12 WINDOW *initscr(void);
13 WINDOW *Xinitscr(int argc, char *argv[]);
16 SCREEN *newterm(const char *type, FILE *outfd, FILE *infd);
17 SCREEN *set_term(SCREEN *new);
18 void delscreen(SCREEN *sp);
20 int resize_term(int nlines, int ncols);
21 bool is_termresized(void);
22 const char *curses_version(void);
25 initscr() should be the first curses routine called. It will
26 initialize all curses data structures, and arrange that the
27 first call to refresh() will clear the screen. In case of
28 error, initscr() will write a message to standard error and end
31 endwin() should be called before exiting or escaping from curses
32 mode temporarily. It will restore tty modes, move the cursor to
33 the lower left corner of the screen and reset the terminal into
34 the proper non-visual mode. To resume curses after a temporary
35 escape, call refresh() or doupdate().
37 isendwin() returns TRUE if endwin() has been called without a
38 subsequent refresh, unless SP is NULL.
40 In some implementations of curses, newterm() allows the use of
41 multiple terminals. Here, it's just an alternative interface for
42 initscr(). It always returns SP, or NULL.
44 delscreen() frees the memory allocated by newterm() or
45 initscr(), since it's not freed by endwin(). This function is
46 usually not needed. In PDCurses, the parameter must be the
47 value of SP, and delscreen() sets SP to NULL.
49 set_term() does nothing meaningful in PDCurses, but is included
50 for compatibility with other curses implementations.
52 resize_term() is effectively two functions: When called with
53 nonzero values for nlines and ncols, it attempts to resize the
54 screen to the given size. When called with (0, 0), it merely
55 adjusts the internal structures to match the current size after
56 the screen is resized by the user. On the currently supported
57 platforms, this functionality is mutually exclusive: X11 allows
58 user resizing, while DOS, OS/2 and Win32 allow programmatic
59 resizing. If you want to support user resizing, you should check
60 for getch() returning KEY_RESIZE, and/or call is_termresized()
61 at appropriate times; if either condition occurs, call
62 resize_term(0, 0). Then, with either user or programmatic
63 resizing, you'll have to resize any windows you've created, as
64 appropriate; resize_term() only handles stdscr and curscr.
66 is_termresized() returns TRUE if the curses screen has been
67 resized by the user, and a call to resize_term() is needed.
68 Checking for KEY_RESIZE is generally preferable, unless you're
69 not handling the keyboard.
71 curses_version() returns a string describing the version of
75 All functions return NULL on error, except endwin(), which
78 Portability X/Open BSD SYS V
89 **man-end****************************************************************/
95 const char *_curses_notice
= "PDCurses 3.4 - Public Domain 2008";
97 SCREEN
*SP
= (SCREEN
*)NULL
; /* curses variables */
98 WINDOW
*curscr
= (WINDOW
*)NULL
; /* the current screen image */
99 WINDOW
*stdscr
= (WINDOW
*)NULL
; /* the default screen window */
100 WINDOW
*pdc_lastscr
= (WINDOW
*)NULL
; /* the last screen image */
102 int LINES
= 0; /* current terminal height */
103 int COLS
= 0; /* current terminal width */
106 MOUSE_STATUS Mouse_status
, pdc_mouse_status
;
108 extern RIPPEDOFFLINE linesripped
[5];
109 extern char linesrippedoff
;
114 WINDOW
*Xinitscr(int argc
, char *argv
[])
118 PDC_LOG(("Xinitscr() - called\n"));
123 if (PDC_scr_open(argc
, argv
) == ERR
)
125 fprintf(stderr
, "initscr(): Unable to create SP\n");
129 SP
->autocr
= TRUE
; /* cr -> lf by default */
130 SP
->raw_out
= FALSE
; /* tty I/O modes */
131 SP
->raw_inp
= FALSE
; /* tty I/O modes */
133 SP
->save_key_modifiers
= FALSE
;
134 SP
->return_key_modifiers
= FALSE
;
139 SP
->_map_mbe_to_key
= 0L;
140 SP
->linesrippedoff
= 0;
141 SP
->linesrippedoffontop
= 0;
145 SP
->orig_cursor
= PDC_get_cursor_mode();
150 if (LINES
< 2 || COLS
< 2)
152 fprintf(stderr
, "initscr(): LINES=%d COLS=%d: too small.\n",
157 if ((curscr
= newwin(LINES
, COLS
, 0, 0)) == (WINDOW
*)NULL
)
159 fprintf(stderr
, "initscr(): Unable to create curscr.\n");
163 if ((pdc_lastscr
= newwin(LINES
, COLS
, 0, 0)) == (WINDOW
*)NULL
)
165 fprintf(stderr
, "initscr(): Unable to create pdc_lastscr.\n");
169 wattrset(pdc_lastscr
, (chtype
)(-1));
172 PDC_slk_initialize();
173 LINES
-= SP
->slklines
;
175 /* We have to sort out ripped off lines here, and reduce the height
176 of stdscr by the number of lines ripped off */
178 for (i
= 0; i
< linesrippedoff
; i
++)
180 if (linesripped
[i
].line
< 0)
181 (*linesripped
[i
].init
)(newwin(1, COLS
, LINES
- 1, 0), COLS
);
183 (*linesripped
[i
].init
)(newwin(1, COLS
,
184 SP
->linesrippedoffontop
++, 0), COLS
);
186 SP
->linesrippedoff
++;
192 if (!(stdscr
= newwin(LINES
, COLS
, SP
->linesrippedoffontop
, 0)))
194 fprintf(stderr
, "initscr(): Unable to create stdscr.\n");
200 /* If preserving the existing screen, don't allow a screen clear */
206 stdscr
->_clear
= FALSE
;
207 curscr
->_clear
= FALSE
;
210 curscr
->_clear
= TRUE
;
212 PDC_init_atrtab(); /* set up default colors */
214 MOUSE_X_POS
= MOUSE_Y_POS
= -1;
215 BUTTON_STATUS(1) = BUTTON_RELEASED
;
216 BUTTON_STATUS(2) = BUTTON_RELEASED
;
217 BUTTON_STATUS(3) = BUTTON_RELEASED
;
218 Mouse_status
.changes
= 0;
224 sprintf(ttytype
, "pdcurses|PDCurses for %s", PDC_sysname());
229 WINDOW
*initscr(void)
231 PDC_LOG(("initscr() - called\n"));
233 return Xinitscr(0, NULL
);
238 PDC_LOG(("endwin() - called\n"));
240 /* Allow temporary exit from curses using endwin() */
252 PDC_LOG(("isendwin() - called\n"));
254 return SP
? !(SP
->alive
) : FALSE
;
257 SCREEN
*newterm(const char *type
, FILE *outfd
, FILE *infd
)
259 PDC_LOG(("newterm() - called\n"));
261 return Xinitscr(0, NULL
) ? SP
: NULL
;
264 SCREEN
*set_term(SCREEN
*new)
266 PDC_LOG(("set_term() - called\n"));
268 /* We only support one screen */
270 return (new == SP
) ? SP
: NULL
;
273 void delscreen(SCREEN
*sp
)
275 PDC_LOG(("delscreen() - called\n"));
280 PDC_slk_free(); /* free the soft label keys, if needed */
285 stdscr
= (WINDOW
*)NULL
;
286 curscr
= (WINDOW
*)NULL
;
287 pdc_lastscr
= (WINDOW
*)NULL
;
291 PDC_scr_free(); /* free SP and pdc_atrtab */
296 int resize_term(int nlines
, int ncols
)
298 PDC_LOG(("resize_term() - called: nlines %d\n", nlines
));
300 if (!stdscr
|| PDC_resize_screen(nlines
, ncols
) == ERR
)
303 SP
->lines
= PDC_get_rows();
304 LINES
= SP
->lines
- SP
->linesrippedoff
- SP
->slklines
;
305 SP
->cols
= COLS
= PDC_get_columns();
307 if (wresize(curscr
, SP
->lines
, SP
->cols
) == ERR
||
308 wresize(stdscr
, LINES
, COLS
) == ERR
||
309 wresize(pdc_lastscr
, SP
->lines
, SP
->cols
) == ERR
)
313 curscr
->_clear
= TRUE
;
317 if (wresize(SP
->slk_winptr
, SP
->slklines
, COLS
) == ERR
)
320 wmove(SP
->slk_winptr
, 0, 0);
321 wclrtobot(SP
->slk_winptr
);
322 PDC_slk_initialize();
327 wnoutrefresh(stdscr
);
332 bool is_termresized(void)
334 PDC_LOG(("is_termresized() - called\n"));
339 const char *curses_version(void)
341 return _curses_notice
;