1 /* $NetBSD: ripoffline.c,v 1.6 2021/09/06 07:03:50 rin Exp $ */ 2 3 /*- 4 * Copyright (c) 2017 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Roy Marples. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 __RCSID("$NetBSD: ripoffline.c,v 1.6 2021/09/06 07:03:50 rin Exp $"); 35 #endif /* not lint */ 36 37 #include "curses.h" 38 #include "curses_private.h" 39 40 /* List of ripoffline calls */ 41 static struct ripoff { 42 int nlines; 43 int (*init)(WINDOW *, int); 44 } ripoffs[MAX_RIPS]; 45 static int nrips; 46 47 /* 48 * ripoffline -- 49 * Ripoff a line from the top of bottom of stdscr. 50 * Must be called before initscr or newterm. 51 */ 52 int 53 ripoffline(int line, int (*init)(WINDOW *, int)) 54 { 55 56 __CTRACE(__CTRACE_WINDOW, "ripoffline: %d\n", line); 57 58 if (nrips >= MAX_RIPS || init == NULL) 59 return ERR; /* This makes sense, but not standards compliant. */ 60 if (line == 0) 61 return OK; 62 ripoffs[nrips].nlines = line < 0 ? -1 : 1; 63 ripoffs[nrips++].init = init; 64 return OK; 65 } 66 67 /* 68 * __rippedlines -- 69 * Returns the number of ripped lines from the screen. 70 */ 71 int 72 __rippedlines(const SCREEN *screen, int line) 73 { 74 const struct __ripoff *rip; 75 int i, n; 76 77 n = 0; 78 for (i = 0, rip = screen->ripped; i < screen->nripped; i++, rip++) { 79 if (line < 1 && rip->nlines < 0) 80 n += -rip->nlines; 81 else if (line > 0 && rip->nlines > 0) 82 n += rip->nlines; 83 } 84 return n; 85 } 86 87 /* 88 * __ripoffscreen -- 89 * Rips lines from the screen by creating a WINDOW per ripoffline call. 90 * Although the POSIX API only allows for one line WINDOWS to be created, 91 * this implemenation allows for N lines if needed. 92 */ 93 int 94 __ripoffscreen(SCREEN *screen) 95 { 96 int i, nlines, rbot, rtop; 97 const struct ripoff *srip; 98 struct __ripoff *rip; 99 WINDOW *w; 100 101 rip = screen->ripped; 102 rbot = LINES; 103 rtop = 0; 104 for (i = 0, srip = ripoffs; i < nrips; i++, srip++) { 105 if (srip->nlines == 0) 106 continue; 107 nlines = srip->nlines < 0 ? -srip->nlines : srip->nlines; 108 w = __newwin(screen, nlines, 0, 109 srip->nlines < 0 ? rbot - nlines : rtop, 110 0, FALSE, FALSE); 111 if (w != NULL) { 112 rip->win = w; 113 rip->nlines = srip->nlines; 114 rip++; 115 screen->nripped++; 116 if (srip->nlines > 0) 117 rtop += nlines; 118 else 119 rbot -= nlines; 120 } 121 if (srip->init(w, COLS) == ERR) 122 return ERR; 123 #ifdef DEBUG 124 if (w != NULL) 125 __CTRACE(__CTRACE_WINDOW, 126 "newterm: %p ripped %d line(s) from the %s\n", 127 w, nlines, srip->nlines < 0 ? "bottom" : "top"); 128 #endif 129 } 130 nrips = 0; /* Reset the stack. */ 131 return OK; 132 } 133 134 /* 135 * __ripoffresize -- 136 * Called from resizeterm to ensure the ripped off lines are correctly 137 * placed and refreshed. 138 */ 139 int 140 __ripoffresize(SCREEN *screen) 141 { 142 int rbot = screen->LINES, i, nlines, ret = OK; 143 struct __ripoff *rip; 144 145 for (i = 0, rip = screen->ripped; i < screen->nripped; i++, rip++) { 146 if (rip->nlines == 0) 147 continue; 148 nlines = rip->nlines < 0 ? -rip->nlines : rip->nlines; 149 if (wresize(rip->win, nlines, screen->COLS) == ERR) 150 ret = ERR; 151 if (rip->nlines < 0) { 152 /* Reposition the lower windows. */ 153 if (mvwin(rip->win, rbot + rip->nlines, 0) == ERR) 154 ret = ERR; 155 else 156 rbot += rip->nlines; 157 } 158 } 159 160 return ret; 161 } 162 163 /* 164 * __ripofftouch -- 165 * Displays the ripped off lines from initscr. 166 */ 167 void 168 __ripofftouch(SCREEN *screen) 169 { 170 int i; 171 struct __ripoff *rip; 172 173 for (i = 0, rip = screen->ripped; i < screen->nripped; i++, rip++) { 174 touchwin(rip->win); 175 wnoutrefresh(rip->win); 176 } 177 } 178 179 /* 180 * __unripoffline -- 181 * Used by __slk_init to remove the ripoffline reservation it made 182 * because the terminal natively supports soft label keys. 183 */ 184 int 185 __unripoffline(int (*init)(WINDOW *, int)) 186 { 187 struct ripoff *rip; 188 int i, unripped = 0; 189 190 for (i = 0, rip = ripoffs; i < nrips; i++, rip++) { 191 if (rip->init == init) { 192 rip->nlines = 0; 193 unripped++; 194 } 195 } 196 return unripped; 197 } 198