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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
/* $XTermId: scrollback.c,v 1.25 2026/01/22 00:53:17 tom Exp $ */
/*
* Copyright 2009-2024,2026 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) + (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 = NULL;
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 with specified width.
*/
static LineData *
addScrollbackWithSize(TScreen *screen, unsigned ncols)
{
ScrnBuf where = NULL;
if (screen->saveBuf_index != NULL && screen->savelines != 0) {
unsigned which;
Char *block;
TRACE(("addScrollback %lu (ncols=%u)\n", screen->saved_fifo, ncols));
/* 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 != NULL) {
TRACE(("...freeing prior FIFO data in slot %d: %p->%p\n",
which, (void *) prior, (void *) prior->attribs));
FreeAndNull(prior->attribs);
}
if (screen->saved_fifo > 2 * screen->savelines) {
screen->saved_fifo -= screen->savelines;
}
}
/* allocate the new data */
block = allocScrnData(screen, 1, ncols, False);
/* record the new data in the index */
setupLineData(screen, where, (Char *) block, 1, ncols, False);
TRACE(("...storing new FIFO data in slot %d: %p->%p\n",
which, (void *) where, block));
screen->saved_fifo++;
}
return (LineData *) where;
}
/*
* Allocate a new row in the scrollback FIFO, returning a pointer to it.
*/
LineData *
addScrollback(TScreen *screen)
{
return addScrollbackWithSize(screen, (unsigned) MaxCols(screen));
}
#if OPT_RESIZE_ADJUST
/*
* Allocate a scrollback row sized to preserve the source line's data.
* Uses max of current width, source's lineSize, and source's maxLineSize.
*/
LineData *
addScrollbackForLine(TScreen *screen, CLineData *src)
{
unsigned ncols = (unsigned) MaxCols(screen);
if (src != NULL) {
if (src->maxLineSize > ncols) {
ncols = src->maxLineSize;
}
if (src->lineSize > ncols) {
ncols = src->lineSize;
}
}
return addScrollbackWithSize(screen, ncols);
}
#endif /* OPT_RESIZE_ADJUST */
void
deleteScrollback(TScreen *screen)
{
unsigned which = ROW2FIFO(screen, -1);
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 != NULL) {
TRACE(("...freeing prior FIFO data in slot %d: %p->%p\n",
which, (void *) prior, (void *) prior->attribs));
FreeAndNull(prior->attribs);
}
screen->saved_fifo--;
}
|