1 /* $OpenBSD: wresize.c,v 1.6 2023/10/17 09:52:09 nicm Exp $ */ 2 3 /**************************************************************************** 4 * Copyright 2019-2020,2021 Thomas E. Dickey * 5 * Copyright 1998-2010,2011 Free Software Foundation, Inc. * 6 * * 7 * Permission is hereby granted, free of charge, to any person obtaining a * 8 * copy of this software and associated documentation files (the * 9 * "Software"), to deal in the Software without restriction, including * 10 * without limitation the rights to use, copy, modify, merge, publish, * 11 * distribute, distribute with modifications, sublicense, and/or sell * 12 * copies of the Software, and to permit persons to whom the Software is * 13 * furnished to do so, subject to the following conditions: * 14 * * 15 * The above copyright notice and this permission notice shall be included * 16 * in all copies or substantial portions of the Software. * 17 * * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 21 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 24 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 25 * * 26 * Except as contained in this notice, the name(s) of the above copyright * 27 * holders shall not be used in advertising or otherwise to promote the * 28 * sale, use or other dealings in this Software without prior written * 29 * authorization. * 30 ****************************************************************************/ 31 32 /**************************************************************************** 33 * Author: Thomas E. Dickey 1996-on * 34 * and: Juergen Pfeifer * 35 ****************************************************************************/ 36 37 #include <curses.priv.h> 38 39 MODULE_ID("$Id: wresize.c,v 1.6 2023/10/17 09:52:09 nicm Exp $") 40 41 static int 42 cleanup_lines(struct ldat *data, int length) 43 { 44 while (--length >= 0) 45 FreeAndNull(data[length].text); 46 free(data); 47 return ERR; 48 } 49 50 /* 51 * If we have reallocated the ldat structs, we will have to repair pointers 52 * used in subwindows. 53 */ 54 static void 55 repair_subwindows(WINDOW *cmp) 56 { 57 WINDOWLIST *wp; 58 struct ldat *pline = cmp->_line; 59 int row; 60 #ifdef USE_SP_WINDOWLIST 61 SCREEN *sp = _nc_screen_of(cmp); 62 #endif 63 64 _nc_lock_global(curses); 65 66 for (each_window(SP_PARM, wp)) { 67 WINDOW *tst = &(wp->win); 68 69 if (tst->_parent == cmp) { 70 71 #define REPAIR1(field, limit) \ 72 if (tst->field > cmp->limit) \ 73 tst->field = cmp->limit 74 75 REPAIR1(_pary, _maxy); 76 REPAIR1(_parx, _maxx); 77 78 #define REPAIR2(field, limit) \ 79 if (tst->limit + tst->field > cmp->limit) \ 80 tst->limit = (NCURSES_SIZE_T) (cmp->limit - tst->field) 81 82 REPAIR2(_pary, _maxy); 83 REPAIR2(_parx, _maxx); 84 85 #define REPAIR3(field, limit) \ 86 if (tst->field > tst->limit) \ 87 tst->field = tst->limit 88 89 REPAIR3(_cury, _maxy); 90 REPAIR3(_curx, _maxx); 91 92 REPAIR3(_regtop, _maxy); 93 REPAIR3(_regbottom, _maxy); 94 95 for (row = 0; row <= tst->_maxy; ++row) { 96 tst->_line[row].text = &pline[tst->_pary + row].text[tst->_parx]; 97 } 98 repair_subwindows(tst); 99 } 100 } 101 _nc_unlock_global(curses); 102 } 103 104 /* 105 * Reallocate a curses WINDOW struct to either shrink or grow to the specified 106 * new lines/columns. If it grows, the new character cells are filled with 107 * blanks. The application is responsible for repainting the blank area. 108 */ 109 NCURSES_EXPORT(int) 110 wresize(WINDOW *win, int ToLines, int ToCols) 111 { 112 int col, row, size_x, size_y; 113 struct ldat *pline; 114 struct ldat *new_lines = 0; 115 116 #ifdef TRACE 117 T((T_CALLED("wresize(%p,%d,%d)"), (void *) win, ToLines, ToCols)); 118 if (win) { 119 TR(TRACE_UPDATE, ("...beg (%ld, %ld), max(%ld,%ld), reg(%ld,%ld)", 120 (long) win->_begy, (long) win->_begx, 121 (long) win->_maxy, (long) win->_maxx, 122 (long) win->_regtop, (long) win->_regbottom)); 123 if (USE_TRACEF(TRACE_UPDATE)) { 124 _tracedump("...before", win); 125 _nc_unlock_global(tracef); 126 } 127 } 128 #endif 129 130 if (!win || --ToLines < 0 || --ToCols < 0) 131 returnCode(ERR); 132 133 size_x = win->_maxx; 134 size_y = win->_maxy; 135 136 if (ToLines == size_y 137 && ToCols == size_x) 138 returnCode(OK); 139 140 if (IS_SUBWIN(win)) { 141 /* 142 * Check if the new limits will fit into the parent window's size. If 143 * not, do not resize. We could adjust the location of the subwindow, 144 * but the application may not like that. 145 */ 146 if (win->_pary + ToLines > win->_parent->_maxy 147 || win->_parx + ToCols > win->_parent->_maxx) { 148 returnCode(ERR); 149 } 150 pline = win->_parent->_line; 151 } else { 152 pline = 0; 153 } 154 155 /* 156 * Allocate new memory as needed. Do the allocations without modifying 157 * the original window, in case an allocation fails. Always allocate 158 * (at least temporarily) the array pointing to the individual lines. 159 */ 160 new_lines = typeCalloc(struct ldat, (unsigned) (ToLines + 1)); 161 if (new_lines == 0) 162 returnCode(ERR); 163 164 /* 165 * For each line in the target, allocate or adjust pointers for the 166 * corresponding text, depending on whether this is a window or a 167 * subwindow. 168 */ 169 for (row = 0; row <= ToLines; ++row) { 170 int begin = (row > size_y) ? 0 : (size_x + 1); 171 int end = ToCols; 172 NCURSES_CH_T *s; 173 174 if (!IS_SUBWIN(win)) { 175 if (row <= size_y) { 176 if (ToCols != size_x) { 177 s = typeMalloc(NCURSES_CH_T, (unsigned) ToCols + 1); 178 if (s == 0) 179 returnCode(cleanup_lines(new_lines, row)); 180 for (col = 0; col <= ToCols; ++col) { 181 bool valid = (col <= size_x); 182 if_WIDEC({ 183 if (col == ToCols 184 && col < size_x 185 && isWidecBase(win->_line[row].text[col])) { 186 valid = FALSE; 187 } 188 }); 189 s[col] = (valid 190 ? win->_line[row].text[col] 191 : win->_nc_bkgd); 192 } 193 } else { 194 s = win->_line[row].text; 195 } 196 } else { 197 s = typeMalloc(NCURSES_CH_T, (unsigned) ToCols + 1); 198 if (s == 0) 199 returnCode(cleanup_lines(new_lines, row)); 200 for (col = 0; col <= ToCols; ++col) 201 s[col] = win->_nc_bkgd; 202 } 203 } else if (pline != 0 && pline[win->_pary + row].text != 0) { 204 s = &pline[win->_pary + row].text[win->_parx]; 205 } else { 206 s = 0; 207 } 208 209 if_USE_SCROLL_HINTS(new_lines[row].oldindex = row); 210 if (row <= size_y) { 211 new_lines[row].firstchar = win->_line[row].firstchar; 212 new_lines[row].lastchar = win->_line[row].lastchar; 213 } 214 if ((ToCols != size_x) || (row > size_y)) { 215 if (end >= begin) { /* growing */ 216 if (new_lines[row].firstchar < begin) 217 new_lines[row].firstchar = (NCURSES_SIZE_T) begin; 218 } else { /* shrinking */ 219 new_lines[row].firstchar = 0; 220 } 221 new_lines[row].lastchar = (NCURSES_SIZE_T) ToCols; 222 } 223 new_lines[row].text = s; 224 } 225 226 /* 227 * Dispose of unwanted memory. 228 */ 229 if (!(win->_flags & _SUBWIN)) { 230 if (ToCols == size_x) { 231 for (row = ToLines + 1; row <= size_y; row++) { 232 FreeAndNull(win->_line[row].text); 233 } 234 } else { 235 for (row = 0; row <= size_y; row++) { 236 FreeAndNull(win->_line[row].text); 237 } 238 } 239 } 240 241 FreeAndNull(win->_line); 242 win->_line = new_lines; 243 244 /* 245 * Finally, adjust the parameters showing screen size and cursor 246 * position: 247 */ 248 win->_maxx = (NCURSES_SIZE_T) ToCols; 249 win->_maxy = (NCURSES_SIZE_T) ToLines; 250 251 if (win->_regtop > win->_maxy) 252 win->_regtop = win->_maxy; 253 if (win->_regbottom > win->_maxy 254 || win->_regbottom == size_y) 255 win->_regbottom = win->_maxy; 256 257 if (win->_curx > win->_maxx) 258 win->_curx = win->_maxx; 259 if (win->_cury > win->_maxy) 260 win->_cury = win->_maxy; 261 262 /* 263 * Check for subwindows of this one, and readjust pointers to our text, 264 * if needed. 265 */ 266 repair_subwindows(win); 267 268 #ifdef TRACE 269 TR(TRACE_UPDATE, ("...beg (%ld, %ld), max(%ld,%ld), reg(%ld,%ld)", 270 (long) win->_begy, (long) win->_begx, 271 (long) win->_maxy, (long) win->_maxx, 272 (long) win->_regtop, (long) win->_regbottom)); 273 if (USE_TRACEF(TRACE_UPDATE)) { 274 _tracedump("...after:", win); 275 _nc_unlock_global(tracef); 276 } 277 #endif 278 returnCode(OK); 279 } 280