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;
111 WINDOW *Xinitscr(int argc, char *argv[])
115 PDC_LOG(("Xinitscr() - called\n"));
120 if (PDC_scr_open(argc, argv) == ERR)
122 fprintf(stderr, "initscr(): Unable to create SP\n");
126 SP->autocr = TRUE; /* cr -> lf by default */
127 SP->raw_out = FALSE; /* tty I/O modes */
128 SP->raw_inp = FALSE; /* tty I/O modes */
130 SP->save_key_modifiers = FALSE;
131 SP->return_key_modifiers = FALSE;
136 SP->_map_mbe_to_key = 0L;
137 SP->linesrippedoff = 0;
138 SP->linesrippedoffontop = 0;
142 SP->orig_cursor = PDC_get_cursor_mode();
147 if (LINES < 2 || COLS < 2)
149 fprintf(stderr, "initscr(): LINES=%d COLS=%d: too small.\n",
154 if ((curscr = newwin(LINES, COLS, 0, 0)) == (WINDOW *)NULL)
156 fprintf(stderr, "initscr(): Unable to create curscr.\n");
160 if ((pdc_lastscr = newwin(LINES, COLS, 0, 0)) == (WINDOW *)NULL)
162 fprintf(stderr, "initscr(): Unable to create pdc_lastscr.\n");
166 wattrset(pdc_lastscr, (chtype)(-1));
169 PDC_slk_initialize();
170 LINES -= SP->slklines;
172 /* We have to sort out ripped off lines here, and reduce the height
173 of stdscr by the number of lines ripped off */
175 for (i = 0; i < linesrippedoff; i++)
177 if (linesripped[i].line < 0)
178 (*linesripped[i].init)(newwin(1, COLS, LINES - 1, 0), COLS);
180 (*linesripped[i].init)(newwin(1, COLS,
181 SP->linesrippedoffontop++, 0), COLS);
183 SP->linesrippedoff++;
189 if (!(stdscr = newwin(LINES, COLS, SP->linesrippedoffontop, 0)))
191 fprintf(stderr, "initscr(): Unable to create stdscr.\n");
197 /* If preserving the existing screen, don't allow a screen clear */
203 stdscr->_clear = FALSE;
204 curscr->_clear = FALSE;
207 curscr->_clear = TRUE;
209 PDC_init_atrtab(); /* set up default colors */
211 MOUSE_X_POS = MOUSE_Y_POS = -1;
212 BUTTON_STATUS(1) = BUTTON_RELEASED;
213 BUTTON_STATUS(2) = BUTTON_RELEASED;
214 BUTTON_STATUS(3) = BUTTON_RELEASED;
215 Mouse_status.changes = 0;
221 sprintf(ttytype, "pdcurses|PDCurses for %s", PDC_sysname());
226 WINDOW *initscr(void)
228 PDC_LOG(("initscr() - called\n"));
230 return Xinitscr(0, NULL);
235 PDC_LOG(("endwin() - called\n"));
237 /* Allow temporary exit from curses using endwin() */
249 PDC_LOG(("isendwin() - called\n"));
251 return SP ? !(SP->alive) : FALSE;
254 SCREEN *newterm(const char *type, FILE *outfd, FILE *infd)
256 PDC_LOG(("newterm() - called\n"));
258 return Xinitscr(0, NULL) ? SP : NULL;
261 SCREEN *set_term(SCREEN *new)
263 PDC_LOG(("set_term() - called\n"));
265 /* We only support one screen */
267 return (new == SP) ? SP : NULL;
270 void delscreen(SCREEN *sp)
272 PDC_LOG(("delscreen() - called\n"));
277 PDC_slk_free(); /* free the soft label keys, if needed */
282 stdscr = (WINDOW *)NULL;
283 curscr = (WINDOW *)NULL;
284 pdc_lastscr = (WINDOW *)NULL;
288 PDC_scr_free(); /* free SP and pdc_atrtab */
293 int resize_term(int nlines, int ncols)
295 PDC_LOG(("resize_term() - called: nlines %d\n", nlines));
297 if (!stdscr || PDC_resize_screen(nlines, ncols) == ERR)
300 SP->lines = PDC_get_rows();
301 LINES = SP->lines - SP->linesrippedoff - SP->slklines;
302 SP->cols = COLS = PDC_get_columns();
304 if (wresize(curscr, SP->lines, SP->cols) == ERR ||
305 wresize(stdscr, LINES, COLS) == ERR ||
306 wresize(pdc_lastscr, SP->lines, SP->cols) == ERR)
310 curscr->_clear = TRUE;
314 if (wresize(SP->slk_winptr, SP->slklines, COLS) == ERR)
317 wmove(SP->slk_winptr, 0, 0);
318 wclrtobot(SP->slk_winptr);
319 PDC_slk_initialize();
324 wnoutrefresh(stdscr);
329 bool is_termresized(void)
331 PDC_LOG(("is_termresized() - called\n"));
336 const char *curses_version(void)
338 return _curses_notice;