scrollback.c revision 956cc18d
1/* $XTermId: scrollback.c,v 1.11 2009/08/06 08:34:30 tom Exp $ */
2
3/************************************************************
4
5Copyright 2009 by Thomas E. Dickey
6
7                        All Rights Reserved
8
9Permission is hereby granted, free of charge, to any person obtaining a
10copy of this software and associated documentation files (the
11"Software"), to deal in the Software without restriction, including
12without limitation the rights to use, copy, modify, merge, publish,
13distribute, sublicense, and/or sell copies of the Software, and to
14permit persons to whom the Software is furnished to do so, subject to
15the following conditions:
16
17The above copyright notice and this permission notice shall be included
18in all copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
24CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28Except as contained in this notice, the name(s) of the above copyright
29holders shall not be used in advertising or otherwise to promote the
30sale, use or other dealings in this Software without prior written
31authorization.
32
33********************************************************/
34
35#include <xterm.h>
36
37#define ROW2FIFO(screen, row) \
38	(unsigned) (((row) + 1 + (screen)->saved_fifo) % (screen)->savelines)
39
40/*
41 * Given a row-number, find the corresponding data for the line in the VT100
42 * widget's saved-line FIFO.  The row-number (from getLineData) is negative.
43 * So we just count backwards from the last saved line.
44 */
45LineData *
46getScrollback(TScreen * screen, int row)
47{
48    unsigned which = ROW2FIFO(screen, row);
49    ScrnBuf where = scrnHeadAddr(screen, screen->saveBuf_index, which);
50
51    TRACE(("getScrollback %d -> %d -> %p\n", row, which, where));
52    return (LineData *) where;
53}
54
55/*
56 * Allocate a new row in the scrollback FIFO, returning a pointer to it.
57 */
58LineData *
59addScrollback(TScreen * screen)
60{
61    ScrnBuf where = 0;
62    unsigned which;
63    unsigned ncols = (unsigned) MaxCols(screen);
64    Char *block;
65
66    if (screen->saveBuf_index != 0) {
67	screen->saved_fifo++;
68	TRACE(("addScrollback %lu\n", screen->saved_fifo));
69
70	/* first, see which index we'll use */
71	which = (unsigned) (screen->saved_fifo % screen->savelines);
72	where = scrnHeadAddr(screen, screen->saveBuf_index, which);
73
74	/* discard any obsolete index data */
75	if (screen->saved_fifo > screen->savelines) {
76	    LineData *prior = (LineData *) where;
77	    /*
78	     * setupLineData uses the attribs as the first address used from the
79	     * data block.
80	     */
81	    if (prior->attribs != 0) {
82		TRACE(("...freeing prior FIFO data in slot %d: %p->%p\n",
83		       which, prior, prior->attribs));
84		free(prior->attribs);
85		prior->attribs = 0;
86	    }
87	}
88
89	/* allocate the new data */
90	block = allocScrnData(screen, 1, ncols);
91
92	/* record the new data in the index */
93	setupLineData(screen, where, (Char *) block, 1, ncols);
94
95	TRACE(("...storing new FIFO data in slot %d: %p->%p\n",
96	       which, where, block));
97
98    }
99    return (LineData *) where;
100}
101
102void
103deleteScrollback(TScreen * screen, int row)
104{
105    unsigned which = ROW2FIFO(screen, row);
106    ScrnBuf where = scrnHeadAddr(screen, screen->saveBuf_index, which);
107    LineData *prior = (LineData *) where;
108    /*
109     * setupLineData uses the attribs as the first address used from the
110     * data block.
111     */
112    if (prior->attribs != 0) {
113	TRACE(("...freeing prior FIFO data in slot %d: %p->%p\n",
114	       which, prior, prior->attribs));
115	free(prior->attribs);
116	prior->attribs = 0;
117    }
118}
119