]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/resizeterm.c
ncurses 6.1 - patch 20190727
[ncurses.git] / ncurses / base / resizeterm.c
1 /****************************************************************************
2  * Copyright (c) 1998-2015,2016 Free Software Foundation, Inc.              *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Thomas E. Dickey                                                *
31  *     and: Juergen Pfeifer                                                 *
32  ****************************************************************************/
33
34 /*
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.
40  */
41
42 #include <curses.priv.h>
43
44 #ifndef CUR
45 #define CUR SP_TERMTYPE
46 #endif
47
48 MODULE_ID("$Id: resizeterm.c,v 1.49 2016/05/28 23:11:26 tom Exp $")
49
50 /*
51  * If we're trying to be reentrant, do not want any local statics.
52  */
53 #if USE_REENTRANT
54 #define EXTRA_ARGS ,     CurLines,     CurCols
55 #define EXTRA_DCLS , int CurLines, int CurCols
56 #else
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 */
63 #endif
64
65 #if NCURSES_SP_FUNCS && !defined(USE_SP_WINDOWLIST)
66 #define UNUSED_SP  (void) sp
67 #else
68 #define UNUSED_SP               /* nothing */
69 #endif
70
71 #ifdef TRACE
72 static void
73 show_window_sizes(const char *name)
74 {
75     SCREEN *sp;
76     WINDOWLIST *wp;
77
78     _nc_lock_global(curses);
79     for (each_screen(sp)) {
80         _tracef("%s resizing: %p: %2d x %2d (%2d x %2d)", name, (void *) sp,
81                 *(ptrLines(sp)),
82                 *(ptrCols(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",
86                     (void *) &(wp->win),
87                     (long) wp->win._maxy + 1,
88                     (long) wp->win._maxx + 1,
89                     (long) wp->win._begy,
90                     (long) wp->win._begx);
91         }
92     }
93     _nc_unlock_global(curses);
94 }
95 #endif
96
97 /*
98  * Return true if the given dimensions do not match the internal terminal
99  * structure's size.
100  */
101 NCURSES_EXPORT(bool)
102 NCURSES_SP_NAME(is_term_resized) (NCURSES_SP_DCLx int ToLines, int ToCols)
103 {
104     T((T_CALLED("is_term_resized(%p, %d, %d)"), (void *) SP_PARM, ToLines, ToCols));
105     returnCode(ToLines > 0
106                && ToCols > 0
107                && (ToLines != screen_lines(SP_PARM)
108                    || ToCols != screen_columns(SP_PARM)));
109 }
110
111 #if NCURSES_SP_FUNCS
112 NCURSES_EXPORT(bool)
113 is_term_resized(int ToLines, int ToCols)
114 {
115     return NCURSES_SP_NAME(is_term_resized) (CURRENT_SCREEN, ToLines, ToCols);
116 }
117 #endif
118
119 /*
120  */
121 static ripoff_t *
122 ripped_window(WINDOW *win)
123 {
124     ripoff_t *result = 0;
125     ripoff_t *rop;
126
127     if (win != 0) {
128 #ifdef USE_SP_RIPOFF
129         SCREEN *sp = _nc_screen_of(win);
130 #endif
131         for (each_ripoff(rop)) {
132             if (rop->win == win && rop->line != 0) {
133                 result = rop;
134                 break;
135             }
136         }
137     }
138     return result;
139 }
140
141 /*
142  * Returns the number of lines from the bottom for the beginning of a ripped
143  * off window.
144  */
145 static int
146 ripped_bottom(WINDOW *win)
147 {
148     int result = 0;
149
150     if (win != 0) {
151         ripoff_t *rop;
152
153 #ifdef USE_SP_RIPOFF
154         SCREEN *sp = _nc_screen_of(win);
155 #endif
156         for (each_ripoff(rop)) {
157             if (rop->line < 0) {
158                 result -= rop->line;
159                 if (rop->win == win) {
160                     break;
161                 }
162             }
163         }
164     }
165     return result;
166 }
167
168 /*
169  * Return the number of levels of child-windows under the current window.
170  */
171 static int
172 child_depth(WINDOW *cmp)
173 {
174     int depth = 0;
175
176     if (cmp != 0) {
177 #ifdef USE_SP_WINDOWLIST
178         SCREEN *sp = _nc_screen_of(cmp);
179 #endif
180         WINDOWLIST *wp;
181
182         for (each_window(sp, wp)) {
183             WINDOW *tst = &(wp->win);
184             if (tst->_parent == cmp) {
185                 depth = 1 + child_depth(tst);
186                 break;
187             }
188         }
189     }
190     return depth;
191 }
192
193 /*
194  * Return the number of levels of parent-windows above the current window.
195  */
196 static int
197 parent_depth(WINDOW *cmp)
198 {
199     int depth = 0;
200
201     if (cmp != 0) {
202         WINDOW *tst;
203         while ((tst = cmp->_parent) != 0) {
204             ++depth;
205             cmp = tst;
206         }
207     }
208     return depth;
209 }
210
211 /*
212  * FIXME: must adjust position so it's within the parent!
213  */
214 static int
215 adjust_window(WINDOW *win, int ToLines, int ToCols, int stolen EXTRA_DCLS)
216 {
217     int result;
218     int bottom = CurLines + _nc_screen_of(win)->_topstolen - stolen;
219     int myLines = win->_maxy + 1;
220     int myCols = win->_maxx + 1;
221     ripoff_t *rop = ripped_window(win);
222
223     T((T_CALLED("adjust_window(%p,%d,%d)%s depth %d/%d currently %ldx%ld at %ld,%ld"),
224        (void *) win, ToLines, ToCols,
225        (rop != 0) ? " (rip)" : "",
226        parent_depth(win),
227        child_depth(win),
228        (long) getmaxy(win), (long) getmaxx(win),
229        (long) getbegy(win) + win->_yoffset, (long) getbegx(win)));
230
231     if (rop != 0 && rop->line < 0) {
232         /*
233          * If it is a ripped-off window at the bottom of the screen, simply
234          * move it to the same relative position.
235          */
236         win->_begy = (NCURSES_SIZE_T) (ToLines - ripped_bottom(win) - 0 - win->_yoffset);
237         if (rop->hook == _nc_slk_initialize)
238             _nc_format_slks(
239 #if NCURSES_SP_FUNCS
240                                _nc_screen_of(win),
241 #endif
242                                ToCols);
243     } else if (win->_begy >= bottom) {
244         /*
245          * If it is below the bottom of the new screen, move up by the same
246          * amount that the screen shrank.
247          */
248         win->_begy = (NCURSES_SIZE_T) (win->_begy + (ToLines - CurLines));
249     } else {
250         if (myLines == (CurLines - stolen)
251             && ToLines != CurLines) {
252             myLines = ToLines - stolen;
253         } else if (myLines == CurLines
254                    && ToLines != CurLines) {
255             myLines = ToLines;
256         }
257     }
258
259     if (myLines > ToLines) {
260         myLines = ToLines;
261     }
262
263     if (myCols > ToCols)
264         myCols = ToCols;
265
266     if (myCols == CurCols
267         && ToCols != CurCols)
268         myCols = ToCols;
269
270     result = wresize(win, myLines, myCols);
271     returnCode(result);
272 }
273
274 /*
275  * If we're decreasing size, recursively search for windows that have no
276  * children, decrease those to fit, then decrease the containing window, etc.
277  */
278 static int
279 decrease_size(NCURSES_SP_DCLx int ToLines, int ToCols, int stolen EXTRA_DCLS)
280 {
281     bool found;
282     int depth = 0;
283     WINDOWLIST *wp;
284
285     T((T_CALLED("decrease_size(%p, %d, %d)"), (void *) SP_PARM, ToLines, ToCols));
286     UNUSED_SP;
287
288     do {
289         found = FALSE;
290         TR(TRACE_UPDATE, ("decreasing size of windows to %dx%d, depth=%d",
291                           ToLines, ToCols, depth));
292         for (each_window(SP_PARM, wp)) {
293             WINDOW *win = &(wp->win);
294
295             if (!(win->_flags & _ISPAD)) {
296                 if (child_depth(win) == depth) {
297                     found = TRUE;
298                     if (adjust_window(win, ToLines, ToCols,
299                                       stolen EXTRA_ARGS) != OK)
300                         returnCode(ERR);
301                 }
302             }
303         }
304         ++depth;
305     } while (found);
306     returnCode(OK);
307 }
308
309 /*
310  * If we're increasing size, recursively search for windows that have no
311  * parent, increase those to fit, then increase the contained window, etc.
312  */
313 static int
314 increase_size(NCURSES_SP_DCLx int ToLines, int ToCols, int stolen EXTRA_DCLS)
315 {
316     bool found;
317     int depth = 0;
318     WINDOWLIST *wp;
319
320     T((T_CALLED("increase_size(%p, %d, %d)"), (void *) SP_PARM, ToLines, ToCols));
321     UNUSED_SP;
322
323     do {
324         found = FALSE;
325         TR(TRACE_UPDATE, ("increasing size of windows to %dx%d, depth=%d",
326                           ToLines, ToCols, depth));
327         for (each_window(SP_PARM, wp)) {
328             WINDOW *win = &(wp->win);
329
330             if (!(win->_flags & _ISPAD)) {
331                 if (parent_depth(win) == depth) {
332                     found = TRUE;
333                     if (adjust_window(win, ToLines, ToCols,
334                                       stolen EXTRA_ARGS) != OK)
335                         returnCode(ERR);
336                 }
337             }
338         }
339         ++depth;
340     } while (found);
341     returnCode(OK);
342 }
343
344 /*
345  * This function reallocates NCURSES window structures, with no side-effects
346  * such as ungetch().
347  */
348 NCURSES_EXPORT(int)
349 NCURSES_SP_NAME(resize_term) (NCURSES_SP_DCLx int ToLines, int ToCols)
350 {
351     int result = OK EXTRA_ARGS;
352     int was_stolen;
353
354     T((T_CALLED("resize_term(%p,%d,%d) old(%d,%d)"),
355        (void *) SP_PARM, ToLines, ToCols,
356        (SP_PARM == 0) ? -1 : screen_lines(SP_PARM),
357        (SP_PARM == 0) ? -1 : screen_columns(SP_PARM)));
358
359     if (SP_PARM == 0 || ToLines <= 0 || ToCols <= 0) {
360         returnCode(ERR);
361     }
362
363     _nc_nonsp_lock_global(curses);
364
365     was_stolen = (screen_lines(SP_PARM) - SP_PARM->_lines_avail);
366     if (NCURSES_SP_NAME(is_term_resized) (NCURSES_SP_ARGx ToLines, ToCols)) {
367         int myLines = CurLines = screen_lines(SP_PARM);
368         int myCols = CurCols = screen_columns(SP_PARM);
369
370 #ifdef TRACE
371         if (USE_TRACEF(TRACE_UPDATE)) {
372             show_window_sizes("before");
373             _nc_unlock_global(tracef);
374         }
375 #endif
376         if (ToLines > screen_lines(SP_PARM)) {
377             result = increase_size(NCURSES_SP_ARGx
378                                    myLines = ToLines,
379                                    myCols,
380                                    was_stolen EXTRA_ARGS);
381             CurLines = myLines;
382             CurCols = myCols;
383         }
384
385         if ((result == OK)
386             && (ToCols > screen_columns(SP_PARM))) {
387             result = increase_size(NCURSES_SP_ARGx
388                                    myLines,
389                                    myCols = ToCols,
390                                    was_stolen EXTRA_ARGS);
391             CurLines = myLines;
392             CurCols = myCols;
393         }
394
395         if ((result == OK)
396             && (ToLines < myLines ||
397                 ToCols < myCols)) {
398             result = decrease_size(NCURSES_SP_ARGx
399                                    ToLines,
400                                    ToCols,
401                                    was_stolen EXTRA_ARGS);
402         }
403
404         if (result == OK) {
405             screen_lines(SP_PARM) = (NCURSES_SIZE_T) ToLines;
406             screen_columns(SP_PARM) = (NCURSES_SIZE_T) ToCols;
407
408 #ifdef USE_TERM_DRIVER
409             CallDriver_2(SP_PARM, td_setsize, ToLines, ToCols);
410 #else
411             lines = (NCURSES_SIZE_T) ToLines;
412             columns = (NCURSES_SIZE_T) ToCols;
413 #endif
414
415             SP_PARM->_lines_avail = (NCURSES_SIZE_T) (ToLines - was_stolen);
416
417             if (SP_PARM->oldhash) {
418                 FreeAndNull(SP_PARM->oldhash);
419             }
420             if (SP_PARM->newhash) {
421                 FreeAndNull(SP_PARM->newhash);
422             }
423 #ifdef TRACE
424             if (USE_TRACEF(TRACE_UPDATE)) {
425                 SET_LINES(ToLines - was_stolen);
426                 SET_COLS(ToCols);
427                 show_window_sizes("after");
428                 _nc_unlock_global(tracef);
429             }
430 #endif
431         }
432     }
433
434     if (result == OK) {
435         /*
436          * Always update LINES, to allow for call from lib_doupdate.c which
437          * needs to have the count adjusted by the stolen (ripped off) lines.
438          */
439         SET_LINES(ToLines - was_stolen);
440         SET_COLS(ToCols);
441     }
442
443     _nc_nonsp_unlock_global(curses);
444
445     returnCode(result);
446 }
447
448 #if NCURSES_SP_FUNCS
449 NCURSES_EXPORT(int)
450 resize_term(int ToLines, int ToCols)
451 {
452     int res;
453     _nc_sp_lock_global(curses);
454     res = NCURSES_SP_NAME(resize_term) (CURRENT_SCREEN, ToLines, ToCols);
455     _nc_sp_unlock_global(curses);
456     return (res);
457 }
458 #endif
459
460 /*
461  * This function reallocates NCURSES window structures.  It is invoked in
462  * response to a SIGWINCH interrupt.  Other user-defined windows may also need
463  * to be reallocated.
464  *
465  * Because this performs memory allocation, it should not (in general) be
466  * invoked directly from the signal handler.
467  */
468 NCURSES_EXPORT(int)
469 NCURSES_SP_NAME(resizeterm) (NCURSES_SP_DCLx int ToLines, int ToCols)
470 {
471     int result = ERR;
472
473     T((T_CALLED("resizeterm(%p, %d,%d) old(%d,%d)"),
474        (void *) SP_PARM, ToLines, ToCols,
475        (SP_PARM == 0) ? -1 : screen_lines(SP_PARM),
476        (SP_PARM == 0) ? -1 : screen_columns(SP_PARM)));
477
478     if (SP_PARM != 0 && ToLines > 0 && ToCols > 0) {
479         result = OK;
480         SP_PARM->_sig_winch = FALSE;
481
482         if (NCURSES_SP_NAME(is_term_resized) (NCURSES_SP_ARGx ToLines, ToCols)) {
483 #if USE_SIGWINCH
484             ripoff_t *rop;
485             bool slk_visible = (SP_PARM != 0
486                                 && SP_PARM->_slk != 0
487                                 && !(SP_PARM->_slk->hidden));
488
489             if (slk_visible) {
490                 slk_clear();
491             }
492 #endif
493             result = NCURSES_SP_NAME(resize_term) (NCURSES_SP_ARGx ToLines, ToCols);
494
495 #if USE_SIGWINCH
496             clearok(CurScreen(SP_PARM), TRUE);  /* screen contents are unknown */
497
498             /* ripped-off lines are a special case: if we did not lengthen
499              * them, we haven't moved them either.  repaint them, too.
500              *
501              * for the rest - stdscr and other windows - the client has to
502              * decide which to repaint, since without panels, ncurses does
503              * not know which are really on top.
504              */
505             for (each_ripoff(rop)) {
506                 if (rop->win != StdScreen(SP_PARM)
507                     && rop->win != 0
508                     && rop->line < 0) {
509
510                     if (rop->hook != _nc_slk_initialize) {
511                         touchwin(rop->win);
512                         wnoutrefresh(rop->win);
513                     }
514                 }
515             }
516
517             /* soft-keys are a special case: we _know_ how to repaint them */
518             if (slk_visible) {
519                 NCURSES_SP_NAME(slk_restore) (NCURSES_SP_ARG);
520                 NCURSES_SP_NAME(slk_touch) (NCURSES_SP_ARG);
521                 NCURSES_SP_NAME(slk_refresh) (NCURSES_SP_ARG);
522             }
523 #endif
524         }
525 #if USE_SIGWINCH
526         safe_ungetch(SP_PARM, KEY_RESIZE);      /* so application can know this */
527 #endif
528     }
529
530     returnCode(result);
531 }
532
533 #if NCURSES_SP_FUNCS
534 NCURSES_EXPORT(int)
535 resizeterm(int ToLines, int ToCols)
536 {
537     return NCURSES_SP_NAME(resizeterm) (CURRENT_SCREEN, ToLines, ToCols);
538 }
539 #endif