summaryrefslogtreecommitdiff
path: root/app/xterm/scrollback.c
blob: 782afe49496017e9306de1f803db5a5becf8e593 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* $XTermId: scrollback.c,v 1.15 2011/09/11 14:59:40 tom Exp $ */

/*
 * Copyright 2009-2010,2011 by Thomas E. Dickey
 *
 *                         All Rights Reserved
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name(s) of the above copyright
 * holders shall not be used in advertising or otherwise to promote the
 * sale, use or other dealings in this Software without prior written
 * authorization.
 */

#include <xterm.h>

#define REAL_ROW(screen, row) ((row) + 1 + (screen)->saved_fifo)
#define ROW2FIFO(screen, row) \
	(unsigned) (REAL_ROW(screen, row) % (screen)->savelines)

/*
 * Given a row-number, find the corresponding data for the line in the VT100
 * widget's saved-line FIFO.  The row-number (from getLineData) is negative.
 * So we just count backwards from the last saved line.
 */
LineData *
getScrollback(TScreen * screen, int row)
{
    LineData *result = 0;

    if (screen->saved_fifo > 0 && REAL_ROW(screen, row) >= 0) {
	unsigned which = ROW2FIFO(screen, row);
	ScrnBuf where = scrnHeadAddr(screen, screen->saveBuf_index, which);
	result = (LineData *) where;
    }

    TRACE(("getScrollback %d -> %d -> %p\n",
	   row, ROW2FIFO(screen, row),
	   (void *) result));
    return result;
}

/*
 * Allocate a new row in the scrollback FIFO, returning a pointer to it.
 */
LineData *
addScrollback(TScreen * screen)
{
    ScrnBuf where = 0;
    unsigned which;
    unsigned ncols = (unsigned) MaxCols(screen);
    Char *block;

    if (screen->saveBuf_index != 0) {
	screen->saved_fifo++;
	TRACE(("addScrollback %lu\n", screen->saved_fifo));

	/* first, see which index we'll use */
	which = (unsigned) (screen->saved_fifo % screen->savelines);
	where = scrnHeadAddr(screen, screen->saveBuf_index, which);

	/* discard any obsolete index data */
	if (screen->saved_fifo > screen->savelines) {
	    LineData *prior = (LineData *) where;
	    /*
	     * setupLineData uses the attribs as the first address used from the
	     * data block.
	     */
	    if (prior->attribs != 0) {
		TRACE(("...freeing prior FIFO data in slot %d: %p->%p\n",
		       which, (void *) prior, prior->attribs));
		free(prior->attribs);
		prior->attribs = 0;
	    }
	}

	/* allocate the new data */
	block = allocScrnData(screen, 1, ncols);

	/* record the new data in the index */
	setupLineData(screen, where, (Char *) block, 1, ncols);

	TRACE(("...storing new FIFO data in slot %d: %p->%p\n",
	       which, (void *) where, block));

    }
    return (LineData *) where;
}

void
deleteScrollback(TScreen * screen, int row)
{
    unsigned which = ROW2FIFO(screen, row);
    ScrnBuf where = scrnHeadAddr(screen, screen->saveBuf_index, which);
    LineData *prior = (LineData *) where;
    /*
     * setupLineData uses the attribs as the first address used from the
     * data block.
     */
    if (prior->attribs != 0) {
	TRACE(("...freeing prior FIFO data in slot %d: %p->%p\n",
	       which, (void *) prior, prior->attribs));
	free(prior->attribs);
	prior->attribs = 0;
    }
}