testxmc.c revision 894e0ac8
1/* $XTermId: testxmc.c,v 1.52 2014/05/11 14:05:38 tom Exp $ */
2
3/*
4 * Copyright 1997-2011,2014 by Thomas E. Dickey
5 *
6 *                         All Rights Reserved
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sublicense, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 * IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
23 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 * Except as contained in this notice, the name(s) of the above copyright
28 * holders shall not be used in advertising or otherwise to promote the
29 * sale, use or other dealings in this Software without prior written
30 * authorization.
31 */
32
33/*
34 * This module provides test support for curses applications that must work
35 * with terminals that have the xmc (magic cookie) glitch.  The xmc_glitch
36 * resource denotes the number of spaces that are emitted when switching to or
37 * from standout (reverse) mode.  Some terminals implement this by storing the
38 * attribute controls in the character cell that is skipped.  So if the cell is
39 * overwritten by text, then the attribute change in the cell is cancelled,
40 * causing attributes to the left of the change to propagate.
41 *
42 * We implement the glitch by writing a character that won't be mistaken for
43 * other normal characters (and mapping normal writes to that character to a
44 * different one).
45 *
46 * Since xmc isn't normally part of xterm, we document it here rather than in
47 * the man-page.  This module is driven by resources rather than by the
48 * termcap/terminfo description to make it a little more flexible for testing
49 * purposes.
50 *
51 * Resources:
52 *
53 * xmcGlitch (class XmcGlitch)
54 *	When true, enables this extension.  The default is `0', which disables
55 *	the module.  (termcap sg, terminfo xmc).
56 *
57 * xmcAttributes (class XmcAttributes)
58 *	The attributes for which we'll generate a glitch, as a bitmask.
59 *
60 *		INVERSE		1
61 *		UNDERLINE	2
62 *		BOLD		4
63 *		BLINK		8
64 *
65 *	The default is `1' (INVERSE).  Some terminals emit glitches for
66 *	underline.  Just for completeness, we recognize all of the video
67 *	attributes.
68 *
69 * xmcInline (class XmcInline)
70 *	When true, limits the extent of an SGR change to the current line.
71 *	The default is `false'.  (No termcap or terminfo equivalent, though
72 *	there are comments in some entries relating to this issue).
73 *
74 * xmcMoveSGR (class XmcMoveSGR)
75 *	When false, a cursor movement will leave a glitch when SGR's are
76 *	active.  The default is `true'.  (termcap ms, terminfo msgr).
77 *
78 * TODO:
79 *	When xmc is active, the terminfo max_attributes (ma) capability is
80 *	assumed to be 1.
81 *
82 *	The xmcAttributes resource should also apply to alternate character
83 *	sets and to color.
84 */
85
86#include <xterm.h>
87#include <data.h>
88
89#define MARK_ON(a)  (Bool) ((my_attrs & a) != 0 && (xw->flags & (whichone = CharOf(a))) == 0)
90#define MARK_OFF(a) (Bool) ((my_attrs & a) != 0 && (xw->flags & (whichone = CharOf(a))) != 0)
91
92void
93Mark_XMC(XtermWidget xw, int param)
94{
95    static IChar *glitch;
96
97    TScreen *screen = TScreenOf(xw);
98    Bool found = False;
99    unsigned my_attrs = CharOf(screen->xmc_attributes & XMC_FLAGS);
100    unsigned whichone = 0;
101
102    if (glitch == 0) {
103	unsigned len = screen->xmc_glitch;
104	glitch = TypeMallocN(IChar, len);
105	while (len--)
106	    glitch[len] = XMC_GLITCH;
107    }
108    switch (param) {
109    case -1:			/* DEFAULT */
110    case 0:			/* FALLTHRU */
111	found = MARK_OFF((xw->flags & XMC_FLAGS));
112	break;
113    case 1:
114	found = MARK_ON(BOLD);
115	break;
116    case 4:
117	found = MARK_ON(UNDERLINE);
118	break;
119    case 5:
120	found = MARK_ON(BLINK);
121	break;
122    case 7:
123	found = MARK_ON(INVERSE);
124	break;
125    case 22:
126	found = MARK_OFF(BOLD);
127	break;
128    case 24:
129	found = MARK_OFF(UNDERLINE);
130	break;
131    case 25:
132	found = MARK_OFF(BLINK);
133	break;
134    case 27:
135	found = MARK_OFF(INVERSE);
136	break;
137    }
138
139    /*
140     * Write a glitch with the attributes temporarily set to the new(er)
141     * ones.
142     */
143    if (found) {
144	unsigned save = xw->flags;
145	xw->flags ^= whichone;
146	TRACE(("XMC Writing glitch (%d/%d) after SGR %d\n", my_attrs,
147	       whichone, param));
148	dotext(xw, '?', glitch, screen->xmc_glitch);
149	xw->flags = save;
150    }
151}
152
153/*
154 * Force a glitch on cursor movement when we're in standout mode and not at the
155 * end of a line.
156 */
157void
158Jump_XMC(XtermWidget xw)
159{
160    TScreen *screen = TScreenOf(xw);
161    if (!screen->move_sgr_ok
162	&& screen->cur_col <= LineMaxCol(screen,
163					 getLineData(screen, screen->cur_row))) {
164	Mark_XMC(xw, -1);
165    }
166}
167
168/*
169 * After writing text to the screen, resolve mismatch between the current
170 * location and any attributes that would have been set by preceding locations.
171 */
172void
173Resolve_XMC(XtermWidget xw)
174{
175    TScreen *screen = TScreenOf(xw);
176    LineData *ld;
177    Bool changed = False;
178    IAttr start;
179    IAttr my_attrs = CharOf(screen->xmc_attributes & XMC_FLAGS);
180    int row = screen->cur_row;
181    int col = screen->cur_col;
182
183    /* Find the preceding cell.
184     */
185    ld = getLineData(screen, row);
186    if (ld->charData[col] != XMC_GLITCH) {
187	if (col != 0) {
188	    col--;
189	} else if (!screen->xmc_inline && row != 0) {
190	    ld = getLineData(screen, --row);
191	    col = LineMaxCol(screen, ld);
192	}
193    }
194    start = (ld->attribs[col] & my_attrs);
195
196    /* Now propagate the starting state until we reach a cell which holds
197     * a glitch.
198     */
199    for (;;) {
200	if (col < LineMaxCol(screen, ld)) {
201	    col++;
202	} else if (!screen->xmc_inline && row < screen->max_row) {
203	    col = 0;
204	    ld = getLineData(screen, ++row);
205	} else
206	    break;
207	if (ld->charData[col] == XMC_GLITCH)
208	    break;
209	if ((ld->attribs[col] & my_attrs) != start) {
210	    ld->attribs[col] =
211		(IAttr) (start | (ld->attribs[col] & ~my_attrs));
212	    changed = True;
213	}
214    }
215
216    TRACE(("XMC %s (%s:%d/%d) from %d,%d to %d,%d\n",
217	   changed ? "Ripple" : "Nochange",
218	   BtoS(xw->flags & my_attrs),
219	   my_attrs, start,
220	   screen->cur_row, screen->cur_col,
221	   row, col));
222
223    if (changed) {
224	ScrnUpdate(xw, screen->cur_row, 0, row + 1 - screen->cur_row,
225		   MaxCols(screen), True);
226    }
227}
228