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