1 /****************************************************************************
2 * Copyright 2020,2021 Thomas E. Dickey *
3 * Copyright 1998-2015,2016 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Thomas E. Dickey *
32 * and: Juergen Pfeifer *
33 ****************************************************************************/
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.
43 #include <curses.priv.h>
46 #define CUR SP_TERMTYPE
49 MODULE_ID("$Id: resizeterm.c,v 1.52 2021/10/23 17:12:33 tom Exp $")
52 * If we're trying to be reentrant, do not want any local statics.
55 #define EXTRA_ARGS , CurLines, CurCols
56 #define EXTRA_DCLS , int CurLines, int CurCols
58 static int current_lines;
59 static int current_cols;
60 #define CurLines current_lines
61 #define CurCols current_cols
62 #define EXTRA_ARGS /* nothing */
63 #define EXTRA_DCLS /* nothing */
66 #if NCURSES_SP_FUNCS && !defined(USE_SP_WINDOWLIST)
67 #define UNUSED_SP (void) sp
69 #define UNUSED_SP /* nothing */
74 show_window_sizes(const char *name)
79 _nc_lock_global(curses);
80 for (each_screen(sp)) {
81 _tracef("%s resizing: %p: %2d x %2d (%2d x %2d)", name, (void *) sp,
84 screen_lines(sp), screen_columns(sp));
85 for (each_window(sp, wp)) {
86 _tracef(" window %p is %2ld x %2ld at %2ld,%2ld",
88 (long) wp->win._maxy + 1,
89 (long) wp->win._maxx + 1,
91 (long) wp->win._begx);
94 _nc_unlock_global(curses);
99 * Return true if the given dimensions do not match the internal terminal
103 NCURSES_SP_NAME(is_term_resized) (NCURSES_SP_DCLx int ToLines, int ToCols)
105 T((T_CALLED("is_term_resized(%p, %d, %d)"), (void *) SP_PARM, ToLines, ToCols));
106 returnCode(ToLines > 0
108 && (ToLines != screen_lines(SP_PARM)
109 || ToCols != screen_columns(SP_PARM)));
114 is_term_resized(int ToLines, int ToCols)
116 return NCURSES_SP_NAME(is_term_resized) (CURRENT_SCREEN, ToLines, ToCols);
123 ripped_window(WINDOW *win)
125 ripoff_t *result = 0;
130 SCREEN *sp = _nc_screen_of(win);
132 for (each_ripoff(rop)) {
133 if (rop->win == win && rop->line != 0) {
143 * Returns the number of lines from the bottom for the beginning of a ripped
147 ripped_bottom(WINDOW *win)
155 SCREEN *sp = _nc_screen_of(win);
157 for (each_ripoff(rop)) {
160 if (rop->win == win) {
170 * Return the number of levels of child-windows under the current window.
173 child_depth(WINDOW *cmp)
178 #ifdef USE_SP_WINDOWLIST
179 SCREEN *sp = _nc_screen_of(cmp);
183 for (each_window(sp, wp)) {
184 WINDOW *tst = &(wp->win);
185 if (tst->_parent == cmp) {
186 depth = 1 + child_depth(tst);
195 * Return the number of levels of parent-windows above the current window.
198 parent_depth(WINDOW *cmp)
204 while ((tst = cmp->_parent) != 0) {
213 * FIXME: must adjust position so it is within the parent!
216 adjust_window(WINDOW *win, int ToLines, int ToCols, int stolen EXTRA_DCLS)
219 int bottom = CurLines + _nc_screen_of(win)->_topstolen - stolen;
220 int myLines = win->_maxy + 1;
221 int myCols = win->_maxx + 1;
222 ripoff_t *rop = ripped_window(win);
224 T((T_CALLED("adjust_window(%p,%d,%d)%s depth %d/%d currently %ldx%ld at %ld,%ld"),
225 (void *) win, ToLines, ToCols,
226 (rop != 0) ? " (rip)" : "",
229 (long) getmaxy(win), (long) getmaxx(win),
230 (long) getbegy(win) + win->_yoffset, (long) getbegx(win)));
232 if (rop != 0 && rop->line < 0) {
234 * If it is a ripped-off window at the bottom of the screen, simply
235 * move it to the same relative position.
237 win->_begy = (NCURSES_SIZE_T) (ToLines - ripped_bottom(win) - 0 - win->_yoffset);
238 if (rop->hook == _nc_slk_initialize)
244 } else if (win->_begy >= bottom) {
246 * If it is below the bottom of the new screen, move up by the same
247 * amount that the screen shrank.
249 win->_begy = (NCURSES_SIZE_T) (win->_begy + (ToLines - CurLines));
251 if (myLines == (CurLines - stolen)
252 && ToLines != CurLines) {
253 myLines = ToLines - stolen;
254 } else if (myLines == CurLines
255 && ToLines != CurLines) {
260 if (myLines > ToLines) {
267 if (myCols == CurCols
268 && ToCols != CurCols)
271 result = wresize(win, myLines, myCols);
276 * If we're decreasing size, recursively search for windows that have no
277 * children, decrease those to fit, then decrease the containing window, etc.
280 decrease_size(NCURSES_SP_DCLx int ToLines, int ToCols, int stolen EXTRA_DCLS)
286 T((T_CALLED("decrease_size(%p, %d, %d)"), (void *) SP_PARM, ToLines, ToCols));
291 TR(TRACE_UPDATE, ("decreasing size of windows to %dx%d, depth=%d",
292 ToLines, ToCols, depth));
293 for (each_window(SP_PARM, wp)) {
294 WINDOW *win = &(wp->win);
297 if (child_depth(win) == depth) {
299 if (adjust_window(win, ToLines, ToCols,
300 stolen EXTRA_ARGS) != OK)
311 * If we're increasing size, recursively search for windows that have no
312 * parent, increase those to fit, then increase the contained window, etc.
315 increase_size(NCURSES_SP_DCLx int ToLines, int ToCols, int stolen EXTRA_DCLS)
321 T((T_CALLED("increase_size(%p, %d, %d)"), (void *) SP_PARM, ToLines, ToCols));
326 TR(TRACE_UPDATE, ("increasing size of windows to %dx%d, depth=%d",
327 ToLines, ToCols, depth));
328 for (each_window(SP_PARM, wp)) {
329 WINDOW *win = &(wp->win);
332 if (parent_depth(win) == depth) {
334 if (adjust_window(win, ToLines, ToCols,
335 stolen EXTRA_ARGS) != OK)
346 * This function reallocates NCURSES window structures, with no side-effects
350 NCURSES_SP_NAME(resize_term) (NCURSES_SP_DCLx int ToLines, int ToCols)
352 int result = OK EXTRA_ARGS;
355 T((T_CALLED("resize_term(%p,%d,%d) old(%d,%d)"),
356 (void *) SP_PARM, ToLines, ToCols,
357 (SP_PARM == 0) ? -1 : screen_lines(SP_PARM),
358 (SP_PARM == 0) ? -1 : screen_columns(SP_PARM)));
360 if (SP_PARM == 0 || ToLines <= 0 || ToCols <= 0) {
364 _nc_nonsp_lock_global(curses);
366 was_stolen = (screen_lines(SP_PARM) - SP_PARM->_lines_avail);
367 if (NCURSES_SP_NAME(is_term_resized) (NCURSES_SP_ARGx ToLines, ToCols)) {
368 int myLines = CurLines = screen_lines(SP_PARM);
369 int myCols = CurCols = screen_columns(SP_PARM);
372 if (USE_TRACEF(TRACE_UPDATE)) {
373 show_window_sizes("before");
374 _nc_unlock_global(tracef);
377 if (ToLines > screen_lines(SP_PARM)) {
378 result = increase_size(NCURSES_SP_ARGx
381 was_stolen EXTRA_ARGS);
387 && (ToCols > screen_columns(SP_PARM))) {
388 result = increase_size(NCURSES_SP_ARGx
391 was_stolen EXTRA_ARGS);
397 && (ToLines < myLines ||
399 result = decrease_size(NCURSES_SP_ARGx
402 was_stolen EXTRA_ARGS);
406 screen_lines(SP_PARM) = (NCURSES_SIZE_T) ToLines;
407 screen_columns(SP_PARM) = (NCURSES_SIZE_T) ToCols;
409 #ifdef USE_TERM_DRIVER
410 CallDriver_2(SP_PARM, td_setsize, ToLines, ToCols);
412 lines = (NCURSES_SIZE_T) ToLines;
413 columns = (NCURSES_SIZE_T) ToCols;
416 SP_PARM->_lines_avail = (NCURSES_SIZE_T) (ToLines - was_stolen);
418 if (SP_PARM->oldhash) {
419 FreeAndNull(SP_PARM->oldhash);
421 if (SP_PARM->newhash) {
422 FreeAndNull(SP_PARM->newhash);
425 if (USE_TRACEF(TRACE_UPDATE)) {
426 SET_LINES(ToLines - was_stolen);
428 show_window_sizes("after");
429 _nc_unlock_global(tracef);
437 * Always update LINES, to allow for call from lib_doupdate.c which
438 * needs to have the count adjusted by the stolen (ripped off) lines.
440 SET_LINES(ToLines - was_stolen);
444 _nc_nonsp_unlock_global(curses);
451 resize_term(int ToLines, int ToCols)
454 _nc_sp_lock_global(curses);
455 res = NCURSES_SP_NAME(resize_term) (CURRENT_SCREEN, ToLines, ToCols);
456 _nc_sp_unlock_global(curses);
462 * This function reallocates NCURSES window structures. It is invoked in
463 * response to a SIGWINCH interrupt. Other user-defined windows may also need
466 * Because this performs memory allocation, it should not (in general) be
467 * invoked directly from the signal handler.
470 NCURSES_SP_NAME(resizeterm) (NCURSES_SP_DCLx int ToLines, int ToCols)
474 T((T_CALLED("resizeterm(%p, %d,%d) old(%d,%d)"),
475 (void *) SP_PARM, ToLines, ToCols,
476 (SP_PARM == 0) ? -1 : screen_lines(SP_PARM),
477 (SP_PARM == 0) ? -1 : screen_columns(SP_PARM)));
479 if (SP_PARM != 0 && ToLines > 0 && ToCols > 0) {
481 SP_PARM->_sig_winch = FALSE;
483 if (NCURSES_SP_NAME(is_term_resized) (NCURSES_SP_ARGx ToLines, ToCols)) {
486 bool slk_visible = (SP_PARM != 0
487 && SP_PARM->_slk != 0
488 && !(SP_PARM->_slk->hidden));
494 result = NCURSES_SP_NAME(resize_term) (NCURSES_SP_ARGx ToLines, ToCols);
497 clearok(CurScreen(SP_PARM), TRUE); /* screen contents are unknown */
499 /* ripped-off lines are a special case: if we did not lengthen
500 * them, we haven't moved them either. repaint them, too.
502 * for the rest - stdscr and other windows - the client has to
503 * decide which to repaint, since without panels, ncurses does
504 * not know which are really on top.
506 for (each_ripoff(rop)) {
507 if (rop->win != StdScreen(SP_PARM)
511 if (rop->hook != _nc_slk_initialize) {
513 wnoutrefresh(rop->win);
518 /* soft-keys are a special case: we _know_ how to repaint them */
520 NCURSES_SP_NAME(slk_restore) (NCURSES_SP_ARG);
521 NCURSES_SP_NAME(slk_touch) (NCURSES_SP_ARG);
522 NCURSES_SP_NAME(slk_refresh) (NCURSES_SP_ARG);
527 safe_ungetch(SP_PARM, KEY_RESIZE); /* so application can know this */
536 resizeterm(int ToLines, int ToCols)
538 return NCURSES_SP_NAME(resizeterm) (CURRENT_SCREEN, ToLines, ToCols);