ripoffline.c revision 1.1 1 1.1 roy /* $NetBSD: ripoffline.c,v 1.1 2017/01/11 20:43:03 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.1 roy __RCSID("$NetBSD: ripoffline.c,v 1.1 2017/01/11 20:43:03 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.1 roy __CTRACE(__CTRACE_SCREEN, "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.1 roy __rippedlines(const SCREEN *screen)
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.1 roy if (rip->nlines < 0)
82 1.1 roy n += -rip->nlines;
83 1.1 roy else
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.1 roy __ripoffscreen(SCREEN *screen, int *rtop)
97 1.1 roy {
98 1.1 roy int i, nlines;
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 *rtop = 0;
104 1.1 roy rip = screen->ripped;
105 1.1 roy for (i = 0, srip = ripoffs; i < nrips; i++, srip++) {
106 1.1 roy nlines = srip->nlines < 0 ? -srip->nlines : srip->nlines;
107 1.1 roy w = __newwin(screen, nlines, 0,
108 1.1 roy srip->nlines < 0 ? LINES - nlines : *rtop,
109 1.1 roy 0, FALSE);
110 1.1 roy if (w != NULL) {
111 1.1 roy rip->win = w;
112 1.1 roy rip->nlines = srip->nlines;
113 1.1 roy rip++;
114 1.1 roy screen->nripped++;
115 1.1 roy if (rip->nlines > 0)
116 1.1 roy (*rtop) += rip->nlines;
117 1.1 roy LINES -= nlines;
118 1.1 roy }
119 1.1 roy if (srip->init(w, COLS) == ERR)
120 1.1 roy return ERR;
121 1.1 roy #ifdef DEBUG
122 1.1 roy if (w != NULL)
123 1.1 roy __CTRACE(__CTRACE_SCREEN,
124 1.1 roy "newterm: ripped %d lines from the %s\n",
125 1.1 roy nlines, srip->nlines < 0 ? "bottom" : "top");
126 1.1 roy #endif
127 1.1 roy }
128 1.1 roy nrips = 0; /* Reset the stack. */
129 1.1 roy return OK;
130 1.1 roy }
131 1.1 roy
132 1.1 roy /*
133 1.1 roy * __ripoffresize --
134 1.1 roy * Called from resizeterm to ensure the ripped off lines are correctly
135 1.1 roy * placed and refreshed.
136 1.1 roy */
137 1.1 roy void
138 1.1 roy __ripoffresize(SCREEN *screen)
139 1.1 roy {
140 1.1 roy int rbot = _cursesi_screen->LINES, i;
141 1.1 roy struct __ripoff *rip;
142 1.1 roy
143 1.1 roy for (i = 0, rip = _cursesi_screen->ripped;
144 1.1 roy i < _cursesi_screen->nripped;
145 1.1 roy i++, rip++)
146 1.1 roy {
147 1.1 roy if (rip->nlines > 0)
148 1.1 roy touchwin(rip->win);
149 1.1 roy else {
150 1.1 roy /* Reposition the lower windows. */
151 1.1 roy mvwin(rip->win, rbot + rip->nlines, 0);
152 1.1 roy rbot += rip->nlines;
153 1.1 roy }
154 1.1 roy wnoutrefresh(rip->win);
155 1.1 roy }
156 1.1 roy }
157