xref: /openbsd-src/lib/libcurses/base/resizeterm.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: resizeterm.c,v 1.2 2001/01/22 18:01:48 millert Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998,2000 Free Software Foundation, Inc.                   *
5  *                                                                          *
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:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
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.                               *
24  *                                                                          *
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       *
28  * authorization.                                                           *
29  ****************************************************************************/
30 
31 /****************************************************************************
32  *  Author: Thomas E. Dickey <dickey@clark.net> 1996,1997                   *
33  ****************************************************************************/
34 
35 /*
36  * This is an extension to the curses library.  It provides callers with a hook
37  * into the NCURSES data to resize windows, primarily for use by programs
38  * running in an X Window terminal (e.g., xterm).  I abstracted this module
39  * from my application library for NCURSES because it must be compiled with
40  * the private data structures -- T.Dickey 1995/7/4.
41  */
42 
43 #include <curses.priv.h>
44 #include <term.h>
45 
46 MODULE_ID("$From: resizeterm.c,v 1.9 2000/12/10 02:43:28 tom Exp $")
47 
48 /*
49  * This function reallocates NCURSES window structures.  It is invoked in
50  * response to a SIGWINCH interrupt.  Other user-defined windows may also need
51  * to be reallocated.
52  *
53  * Because this performs memory allocation, it should not (in general) be
54  * invoked directly from the signal handler.
55  */
56 NCURSES_EXPORT(int)
57 resizeterm(int ToLines, int ToCols)
58 {
59     int stolen = screen_lines - SP->_lines_avail;
60     int bottom = screen_lines + SP->_topstolen - stolen;
61 
62     T((T_CALLED("resizeterm(%d,%d) old(%d,%d)"),
63        ToLines, ToCols,
64        screen_lines, screen_columns));
65 
66     SP->_sig_winch = FALSE;
67 
68     if (ToLines != screen_lines
69 	|| ToCols != screen_columns) {
70 	WINDOWLIST *wp;
71 
72 #if USE_SIGWINCH
73 	ungetch(KEY_RESIZE);	/* so application can know this */
74 	clearok(curscr, TRUE);	/* screen contents are unknown */
75 #endif
76 
77 	for (wp = _nc_windows; wp != 0; wp = wp->next) {
78 	    WINDOW *win = wp->win;
79 	    int myLines = win->_maxy + 1;
80 	    int myCols = win->_maxx + 1;
81 
82 	    /* pads aren't treated this way */
83 	    if (win->_flags & _ISPAD)
84 		continue;
85 
86 	    if (win->_begy >= bottom) {
87 		win->_begy += (ToLines - screen_lines);
88 	    } else {
89 		if (myLines == screen_lines - stolen
90 		    && ToLines != screen_lines)
91 		    myLines = ToLines - stolen;
92 		else if (myLines == screen_lines
93 			 && ToLines != screen_lines)
94 		    myLines = ToLines;
95 	    }
96 
97 	    if (myCols == screen_columns
98 		&& ToCols != screen_columns)
99 		myCols = ToCols;
100 
101 	    if (wresize(win, myLines, myCols) != OK)
102 		returnCode(ERR);
103 	}
104 
105 	screen_lines = lines = ToLines;
106 	screen_columns = columns = ToCols;
107 
108 	SP->_lines_avail = lines - stolen;
109 
110 	if (SP->oldhash) {
111 	    FreeAndNull(SP->oldhash);
112 	}
113 	if (SP->newhash) {
114 	    FreeAndNull(SP->newhash);
115 	}
116     }
117 
118     /*
119      * Always update LINES, to allow for call from lib_doupdate.c which
120      * needs to have the count adjusted by the stolen (ripped off) lines.
121      */
122     LINES = ToLines - stolen;
123     COLS = ToCols;
124 
125     returnCode(OK);
126 }
127