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