1 /****************************************************************************
2 * Copyright (c) 1998-2014,2015 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Thomas E. Dickey *
31 * and: Juergen Pfeifer *
32 ****************************************************************************/
35 * This is an extension to the curses library. It provides callers with a hook
36 * into the NCURSES data to resize windows, primarily for use by programs
37 * running in an X Window terminal (e.g., xterm). I abstracted this module
38 * from my application library for NCURSES because it must be compiled with
39 * the private data structures -- T.Dickey 1995/7/4.
42 #include <curses.priv.h>
45 #define CUR SP_TERMTYPE
48 MODULE_ID("$Id: resizeterm.c,v 1.48 2015/10/10 19:47:25 tom Exp $")
51 * If we're trying to be reentrant, do not want any local statics.
54 #define EXTRA_ARGS , CurLines, CurCols
55 #define EXTRA_DCLS , int CurLines, int CurCols
57 static int current_lines;
58 static int current_cols;
59 #define CurLines current_lines
60 #define CurCols current_cols
61 #define EXTRA_ARGS /* nothing */
62 #define EXTRA_DCLS /* nothing */
65 #if NCURSES_SP_FUNCS && !defined(USE_SP_WINDOWLIST)
66 #define UNUSED_SP (void) sp
68 #define UNUSED_SP /* nothing */
73 show_window_sizes(const char *name)
78 _nc_lock_global(curses);
79 for (each_screen(sp)) {
80 _tracef("%s resizing: %p: %2d x %2d (%2d x %2d)", name, (void *) sp,
83 screen_lines(sp), screen_columns(sp));
84 for (each_window(sp, wp)) {
85 _tracef(" window %p is %2ld x %2ld at %2ld,%2ld",
87 (long) wp->win._maxy + 1,
88 (long) wp->win._maxx + 1,
90 (long) wp->win._begx);
93 _nc_unlock_global(curses);
98 * Return true if the given dimensions do not match the internal terminal
102 NCURSES_SP_NAME(is_term_resized) (NCURSES_SP_DCLx int ToLines, int ToCols)
104 T((T_CALLED("is_term_resized(%p, %d, %d)"), (void *) SP_PARM, ToLines, ToCols));
105 returnCode(ToLines > 0
107 && (ToLines != screen_lines(SP_PARM)
108 || ToCols != screen_columns(SP_PARM)));
113 is_term_resized(int ToLines, int ToCols)
115 return NCURSES_SP_NAME(is_term_resized) (CURRENT_SCREEN, ToLines, ToCols);
122 ripped_window(WINDOW *win)
124 ripoff_t *result = 0;
129 SCREEN *sp = _nc_screen_of(win);
131 for (each_ripoff(rop)) {
132 if (rop->win == win && rop->line != 0) {
142 * Returns the number of lines from the bottom for the beginning of a ripped
146 ripped_bottom(WINDOW *win)
153 SCREEN *sp = _nc_screen_of(win);
155 for (each_ripoff(rop)) {
158 if (rop->win == win) {
168 * Return the number of levels of child-windows under the current window.
171 child_depth(WINDOW *cmp)
176 #ifdef USE_SP_WINDOWLIST
177 SCREEN *sp = _nc_screen_of(cmp);
181 for (each_window(sp, wp)) {
182 WINDOW *tst = &(wp->win);
183 if (tst->_parent == cmp) {
184 depth = 1 + child_depth(tst);
193 * Return the number of levels of parent-windows above the current window.
196 parent_depth(WINDOW *cmp)
202 while ((tst = cmp->_parent) != 0) {
211 * FIXME: must adjust position so it's within the parent!
214 adjust_window(WINDOW *win, int ToLines, int ToCols, int stolen EXTRA_DCLS)
217 int bottom = CurLines + _nc_screen_of(win)->_topstolen - stolen;
218 int myLines = win->_maxy + 1;
219 int myCols = win->_maxx + 1;
220 ripoff_t *rop = ripped_window(win);
222 T((T_CALLED("adjust_window(%p,%d,%d)%s depth %d/%d currently %ldx%ld at %ld,%ld"),
223 (void *) win, ToLines, ToCols,
224 (rop != 0) ? " (rip)" : "",
227 (long) getmaxy(win), (long) getmaxx(win),
228 (long) getbegy(win) + win->_yoffset, (long) getbegx(win)));
230 if (rop != 0 && rop->line < 0) {
232 * If it is a ripped-off window at the bottom of the screen, simply
233 * move it to the same relative position.
235 win->_begy = (NCURSES_SIZE_T) (ToLines - ripped_bottom(win) - 0 - win->_yoffset);
236 if (rop->hook == _nc_slk_initialize)
242 } else if (win->_begy >= bottom) {
244 * If it is below the bottom of the new screen, move up by the same
245 * amount that the screen shrank.
247 win->_begy = (NCURSES_SIZE_T) (win->_begy + (ToLines - CurLines));
249 if (myLines == (CurLines - stolen)
250 && ToLines != CurLines) {
251 myLines = ToLines - stolen;
252 } else if (myLines == CurLines
253 && ToLines != CurLines) {
258 if (myLines > ToLines) {
265 if (myCols == CurCols
266 && ToCols != CurCols)
269 result = wresize(win, myLines, myCols);
274 * If we're decreasing size, recursively search for windows that have no
275 * children, decrease those to fit, then decrease the containing window, etc.
278 decrease_size(NCURSES_SP_DCLx int ToLines, int ToCols, int stolen EXTRA_DCLS)
284 T((T_CALLED("decrease_size(%p, %d, %d)"), (void *) SP_PARM, ToLines, ToCols));
289 TR(TRACE_UPDATE, ("decreasing size of windows to %dx%d, depth=%d",
290 ToLines, ToCols, depth));
291 for (each_window(SP_PARM, wp)) {
292 WINDOW *win = &(wp->win);
294 if (!(win->_flags & _ISPAD)) {
295 if (child_depth(win) == depth) {
297 if (adjust_window(win, ToLines, ToCols,
298 stolen EXTRA_ARGS) != OK)
309 * If we're increasing size, recursively search for windows that have no
310 * parent, increase those to fit, then increase the contained window, etc.
313 increase_size(NCURSES_SP_DCLx int ToLines, int ToCols, int stolen EXTRA_DCLS)
319 T((T_CALLED("increase_size(%p, %d, %d)"), (void *) SP_PARM, ToLines, ToCols));
324 TR(TRACE_UPDATE, ("increasing size of windows to %dx%d, depth=%d",
325 ToLines, ToCols, depth));
326 for (each_window(SP_PARM, wp)) {
327 WINDOW *win = &(wp->win);
329 if (!(win->_flags & _ISPAD)) {
330 if (parent_depth(win) == depth) {
332 if (adjust_window(win, ToLines, ToCols,
333 stolen EXTRA_ARGS) != OK)
344 * This function reallocates NCURSES window structures, with no side-effects
348 NCURSES_SP_NAME(resize_term) (NCURSES_SP_DCLx int ToLines, int ToCols)
350 int result = OK EXTRA_ARGS;
353 T((T_CALLED("resize_term(%p,%d,%d) old(%d,%d)"),
354 (void *) SP_PARM, ToLines, ToCols,
355 (SP_PARM == 0) ? -1 : screen_lines(SP_PARM),
356 (SP_PARM == 0) ? -1 : screen_columns(SP_PARM)));
358 if (SP_PARM == 0 || ToLines <= 0 || ToCols <= 0) {
362 _nc_nonsp_lock_global(curses);
364 was_stolen = (screen_lines(SP_PARM) - SP_PARM->_lines_avail);
365 if (NCURSES_SP_NAME(is_term_resized) (NCURSES_SP_ARGx ToLines, ToCols)) {
366 int myLines = CurLines = screen_lines(SP_PARM);
367 int myCols = CurCols = screen_columns(SP_PARM);
370 if (USE_TRACEF(TRACE_UPDATE)) {
371 show_window_sizes("before");
372 _nc_unlock_global(tracef);
375 if (ToLines > screen_lines(SP_PARM)) {
376 result = increase_size(NCURSES_SP_ARGx
379 was_stolen EXTRA_ARGS);
385 && (ToCols > screen_columns(SP_PARM))) {
386 result = increase_size(NCURSES_SP_ARGx
389 was_stolen EXTRA_ARGS);
395 && (ToLines < myLines ||
397 result = decrease_size(NCURSES_SP_ARGx
400 was_stolen EXTRA_ARGS);
404 screen_lines(SP_PARM) = (NCURSES_SIZE_T) ToLines;
405 screen_columns(SP_PARM) = (NCURSES_SIZE_T) ToCols;
407 #ifdef USE_TERM_DRIVER
408 CallDriver_2(SP_PARM, td_setsize, ToLines, ToCols);
410 lines = (NCURSES_SIZE_T) ToLines;
411 columns = (NCURSES_SIZE_T) ToCols;
414 SP_PARM->_lines_avail = (NCURSES_SIZE_T) (ToLines - was_stolen);
416 if (SP_PARM->oldhash) {
417 FreeAndNull(SP_PARM->oldhash);
419 if (SP_PARM->newhash) {
420 FreeAndNull(SP_PARM->newhash);
423 if (USE_TRACEF(TRACE_UPDATE)) {
424 SET_LINES(ToLines - was_stolen);
426 show_window_sizes("after");
427 _nc_unlock_global(tracef);
435 * Always update LINES, to allow for call from lib_doupdate.c which
436 * needs to have the count adjusted by the stolen (ripped off) lines.
438 SET_LINES(ToLines - was_stolen);
442 _nc_nonsp_unlock_global(curses);
449 resize_term(int ToLines, int ToCols)
452 _nc_sp_lock_global(curses);
453 res = NCURSES_SP_NAME(resize_term) (CURRENT_SCREEN, ToLines, ToCols);
454 _nc_sp_unlock_global(curses);
460 * This function reallocates NCURSES window structures. It is invoked in
461 * response to a SIGWINCH interrupt. Other user-defined windows may also need
464 * Because this performs memory allocation, it should not (in general) be
465 * invoked directly from the signal handler.
468 NCURSES_SP_NAME(resizeterm) (NCURSES_SP_DCLx int ToLines, int ToCols)
472 T((T_CALLED("resizeterm(%p, %d,%d) old(%d,%d)"),
473 (void *) SP_PARM, ToLines, ToCols,
474 (SP_PARM == 0) ? -1 : screen_lines(SP_PARM),
475 (SP_PARM == 0) ? -1 : screen_columns(SP_PARM)));
477 if (SP_PARM != 0 && ToLines > 0 && ToCols > 0) {
479 SP_PARM->_sig_winch = FALSE;
481 if (NCURSES_SP_NAME(is_term_resized) (NCURSES_SP_ARGx ToLines, ToCols)) {
484 bool slk_visible = (SP_PARM != 0
485 && SP_PARM->_slk != 0
486 && !(SP_PARM->_slk->hidden));
492 result = NCURSES_SP_NAME(resize_term) (NCURSES_SP_ARGx ToLines, ToCols);
495 clearok(CurScreen(SP_PARM), TRUE); /* screen contents are unknown */
497 /* ripped-off lines are a special case: if we did not lengthen
498 * them, we haven't moved them either. repaint them, too.
500 * for the rest - stdscr and other windows - the client has to
501 * decide which to repaint, since without panels, ncurses does
502 * not know which are really on top.
504 for (each_ripoff(rop)) {
505 if (rop->win != StdScreen(SP_PARM)
509 if (rop->hook != _nc_slk_initialize) {
511 wnoutrefresh(rop->win);
516 /* soft-keys are a special case: we _know_ how to repaint them */
518 NCURSES_SP_NAME(slk_restore) (NCURSES_SP_ARG);
519 NCURSES_SP_NAME(slk_touch) (NCURSES_SP_ARG);
520 NCURSES_SP_NAME(slk_refresh) (NCURSES_SP_ARG);
525 safe_ungetch(SP_PARM, KEY_RESIZE); /* so application can know this */
534 resizeterm(int ToLines, int ToCols)
536 return NCURSES_SP_NAME(resizeterm) (CURRENT_SCREEN, ToLines, ToCols);