xref: /illumos-gate/usr/src/lib/libcurses/screen/vidupdate.c (revision ddb365bfc9e868ad24ccdcb0dc91af18b10df082)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 /*LINTLIBRARY*/
41 
42 #include <sys/types.h>
43 #include <stdlib.h>
44 #include "curses_inc.h"
45 
46 #ifdef PC6300PLUS
47 #include <fcntl.h>
48 #include <sys/console.h>
49 #endif
50 
51 #define	NUM_OF_SPECIFIC_TURN_OFFS	3
52 extern	chtype	bit_attributes[];
53 
54 int Oldcolors[] = { COLOR_BLACK, COLOR_BLUE, COLOR_GREEN, COLOR_CYAN,
55 		COLOR_RED, COLOR_MAGENTA, COLOR_YELLOW, COLOR_WHITE };
56 
57 void
58 vidupdate(chtype newmode, chtype oldmode, int (*outc)(char))
59 {
60 	bool color_terminal = (cur_term->_pairs_tbl) ? TRUE : FALSE;
61 	chtype oldvideo = (oldmode & A_ATTRIBUTES) & ~A_COLOR;
62 	chtype newvideo = (newmode & A_ATTRIBUTES) & ~A_COLOR;
63 	int  _change_video(chtype, chtype, int (*)(char));
64 	void _change_color(short, int (*)(char));
65 
66 	/* if colors are used, extract the color related information from */
67 	/* the old and new modes and then erase color-pairs fields in	*/
68 	/* both arguments.						*/
69 
70 	if (color_terminal) {
71 		/* LINTED */
72 		short oldcolor = (short) PAIR_NUMBER(oldmode & A_COLOR);
73 		/* LINTED */
74 		short newcolor = (short) PAIR_NUMBER(newmode & A_COLOR);
75 		chtype turn_off = A_COLOR;
76 
77 		/* erase information about video attributes that could not */
78 		/* have been used with colors				   */
79 
80 		if (oldcolor == 0)
81 			oldvideo &= ~turn_off;
82 
83 		if (no_color_video != -1)
84 			turn_off |= (((chtype) no_color_video) << 16);
85 
86 		if (oldcolor != 0)
87 			oldvideo &= ~turn_off;
88 
89 
90 		/* if the new mode contains color information, then first  */
91 		/* deal with video attributes, and then with colors.  This */
92 		/* way color information will overwrite video information. */
93 
94 		if (newcolor != 0) {
95 			/* erase information about video attributes that */
96 			/* should not be used with colors		 */
97 
98 			newvideo &= ~turn_off;
99 
100 			/* if the new and the old video modes became 	*/
101 			/* the same don't bother with them		*/
102 
103 			if (newvideo != oldvideo) {
104 				if ((_change_video(newvideo, oldvideo,
105 				    outc)) == -1) {
106 					_Color_pair *cur_pair =
107 					    &cur_term->_cur_pair;
108 					oldcolor = -1;
109 					cur_pair->background =
110 					    cur_pair->foreground = -1;
111 				}
112 			}
113 			if (newcolor != oldcolor)
114 				_change_color(newcolor, outc);
115 		}
116 
117 		/* new mode doesn't contain any color information.  Deal */
118 		/* with colors first (possibly turning of the colors that */
119 		/* were contained in the oldmode, and then deal with video. */
120 		/* This way video attributes will overwrite colors.	*/
121 
122 		else {
123 			if (newcolor != oldcolor)
124 				_change_color(newcolor, outc);
125 			if (newvideo != oldvideo)
126 				(void) _change_video(newvideo, oldvideo, outc);
127 		}
128 	} else
129 		(void) _change_video(newvideo, oldvideo, outc);
130 }
131 
132 
133 int
134 _change_video(chtype newmode, chtype oldmode, int (*outc)(char))
135 {
136 	int rc = 0;
137 
138 	/* If you have set_attributes let the terminfo writer */
139 	/* worry about it. */
140 
141 	if (!set_attributes) {
142 	/*
143 	 * The trick is that we want to pre-process the new and oldmode
144 	 * so that we now know what they will really translate to on
145 	 * the physical screen.
146 	 * In the case where some attributes are being faked
147 	 * we get rid of the attributes being asked for and just have
148 	 * STANDOUT mode set.  Therefore, if STANDOUT and UNDERLINE were
149 	 * on the screen but UNDERLINE was being faked to STANDOUT; and
150 	 * the new mode is just UNDERLINE, we will get rid of any faked
151 	 * modes and be left with and oldmode of STANDOUT and a new mode
152 	 * of STANDOUT, in which case the check for newmode and oldmode
153 	 * being equal will be true.
154 	 *
155 	 *
156 	 * This test is similar to the concept explained above.
157 	 * counter is the maximum attributes allowed on a terminal.
158 	 * For instance, on an hp/tvi950 without set_attributes
159 	 * the last video sequence sent will be the one the terminal
160 	 * will be in (on that spot).  Therefore, in setupterm.c
161 	 * if ceol_standout_glitch or magic_cookie_glitch is set
162 	 * max_attributes is set to 1.  This is because on those terminals
163 	 * only one attribute can be on at once.  So, we pre-process the
164 	 * oldmode and the newmode and only leave the bits that are
165 	 * significant.  In other words, if on an hp you ask for STANDOUT
166 	 * and UNDERLINE it will become only STANDOUT since that is the
167 	 * first bit that is looked at.  If then the user goes from
168 	 * STANDOUT and UNDERLINE to STANDOUT and REVERSE the oldmode will
169 	 * become STANDOUT and the newmode will become STANDOUT.
170 	 *
171 	 * This also helps the code below in that on a hp or tvi950 only
172 	 * one bit will ever be set so that no code has to be added to
173 	 * cut out early in case two attributes were asked for.
174 	 */
175 
176 		chtype	check_faked, modes[2];
177 		int	counter = max_attributes, i, j, tempmode;
178 		int	k = (cur_term->sgr_mode == oldmode) ? 1 : 2;
179 
180 		modes[0] = newmode;
181 		modes[1] = oldmode;
182 
183 		while (k-- > 0) {
184 			if ((check_faked = (modes[k] &
185 			    cur_term->sgr_faked)) != A_NORMAL) {
186 				modes[k] &= ~check_faked;
187 				modes[k] |= A_STANDOUT;
188 			}
189 
190 			if ((j = counter) >= 0) {
191 				tempmode = A_NORMAL;
192 				if (j > 0) {
193 					for (i = 0; i < NUM_ATTRIBUTES; i++) {
194 						if (modes[k] &
195 						    bit_attributes[i]) {
196 							tempmode |=
197 							    bit_attributes[i];
198 							if (--j == 0)
199 								break;
200 						}
201 					}
202 				}
203 				modes[k] = tempmode;
204 			}
205 		}
206 		newmode = modes[0];
207 		oldmode = modes[1];
208 	}
209 
210 	if (newmode == oldmode)
211 		return (rc);
212 
213 #ifdef DEBUG
214 	if (outf)
215 		fprintf(outf, "vidupdate oldmode=%o, newmode=%o\n",
216 		    oldmode, newmode);
217 #endif
218 
219 	if (set_attributes) {
220 		(void) tputs(tparm(set_attributes,
221 			newmode & A_STANDOUT,
222 			newmode & A_UNDERLINE,
223 			newmode & A_REVERSE,
224 			newmode & A_BLINK,
225 			newmode & A_DIM,
226 			newmode & A_BOLD,
227 			newmode & A_INVIS,
228 			newmode & A_PROTECT,
229 			newmode & A_ALTCHARSET),
230 			1, outc);
231 		rc = -1;
232 	} else {
233 		chtype	turn_on, turn_off;
234 		int			i;
235 
236 		/*
237 		 * If we are going to turn something on anyway and we are
238 		 * on a glitchy terminal, don't bother turning it off
239 		 * since by turning something on you turn everything else off.
240 		 */
241 
242 		if ((ceol_standout_glitch || magic_cookie_glitch >= 0) &&
243 		    ((turn_on = ((oldmode ^ newmode) & newmode)) !=
244 		    A_NORMAL)) {
245 			goto turn_on_code;
246 	}
247 
248 	if ((turn_off = (oldmode & newmode) ^ oldmode) != A_NORMAL) {
249 		/*
250 		 * Check for things to turn off.
251 		 * First see if we are going to turn off something
252 		 * that doesn't have a specific turn off capability.
253 		 *
254 		 * Then check to see if, even though there may be a specific
255 		 * turn off sequence, this terminal doesn't have one or
256 		 * the turn off sequence also turns off something else.
257 		 */
258 		if ((turn_off & ~(A_ALTCHARSET | A_STANDOUT | A_UNDERLINE)) ||
259 		    (turn_off != (turn_off & cur_term->check_turn_off))) {
260 			(void) tputs(tparm_p0(exit_attribute_mode), 1, outc);
261 			rc = -1;
262 			oldmode = A_NORMAL;
263 		} else {
264 			for (i = 0; i < NUM_OF_SPECIFIC_TURN_OFFS; i++) {
265 				if (turn_off & bit_attributes[i]) {
266 					(void) tputs(tparm_p0
267 					    (cur_term->turn_off_seq[i]),
268 					    1, outc);
269 					oldmode &= ~bit_attributes[i];
270 					rc = -1;
271 				}
272 			}
273 		}
274 	}
275 
276 	if ((turn_on = ((oldmode ^ newmode) & newmode)) != A_NORMAL) {
277 turn_on_code:
278 
279 		/* Check for modes to turn on. */
280 
281 		for (i = 0; i < NUM_ATTRIBUTES; i++)
282 			if (turn_on & bit_attributes[i]) {
283 				(void) tputs(tparm_p0(cur_term->turn_on_seq[i]),
284 				    1, outc);
285 				rc = -1;
286 				/*
287 				 * Keep turning off the bit(s) that we just
288 				 * sent to the screen.  As soon as turn_on
289 				 * reaches A_NORMAL we don't have to turn
290 				 * anything else on and we can
291 				 * break out of the loop.
292 				 */
293 				if ((turn_on &= ~bit_attributes[i]) ==
294 				    A_NORMAL)
295 					break;
296 			}
297 		}
298 
299 		if (magic_cookie_glitch > 0)
300 			(void) tputs(cursor_left, 1, outc);
301 	}
302 	cur_term->sgr_mode = newmode;
303 	return (rc);
304 }
305 
306 
307 void
308 _change_color(short newcolor, int (*outc)(char))
309 {
310 #ifndef PC6300PLUS
311 	{
312 	_Color_pair *ptp = cur_term->_pairs_tbl;
313 	/* pairs table pointer */
314 	_Color_pair *cur_pair = &cur_term->_cur_pair;
315 
316 	/* MORE: we may have to change some stuff, depending on whether */
317 	/* HP terminals  will be changing the background, or not	*/
318 
319 	if (newcolor == 0) {
320 		if (orig_pair)
321 			(void) tputs(tparm_p0(orig_pair), 1, outc);
322 		if (set_a_background || set_a_foreground ||
323 		    set_background || set_foreground) {
324 			cur_pair->background = -1;
325 			cur_pair->foreground = -1;
326 		}
327 		return;
328 	}
329 
330 	/* if we are on HP type terminal, just send an escape sequence	*/
331 	/* to use desired color pair (we could have done some optimization: */
332 	/* check if both the foreground and background of newcolor match    */
333 	/* the ones of cur_term->_cur_pair.  but that will happen only when */
334 	/* two color pairs are defined exacly the same, and probably not    */
335 	/* worth the effort).						    */
336 
337 	if (set_color_pair)
338 		(void) tputs(tparm_p1(set_color_pair, newcolor), 1, outc);
339 
340 		/* on Tek model we can do some optimization.	*/
341 
342 	else {
343 		if (ptp[newcolor].background != cur_pair->background) {
344 			if (set_a_background)
345 				(void) tputs(tparm_p1(set_a_background,
346 				    ptp[newcolor].background), 1, outc);
347 			else if (set_background)
348 				(void) tputs(tparm_p1(set_background,
349 				    Oldcolors[ptp[newcolor].background]),
350 				    1, outc);
351 			cur_pair->background = ptp[newcolor].background;
352 		}
353 		if (ptp[newcolor].foreground != cur_pair->foreground) {
354 			if (set_a_foreground)
355 				(void) tputs(tparm_p1(set_a_foreground,
356 				    ptp[newcolor].foreground), 1, outc);
357 			else if (set_foreground)
358 				(void) tputs(tparm_p1(set_foreground,
359 				    Oldcolors[ptp[newcolor].foreground]),
360 				    1, outc);
361 			cur_pair->foreground = ptp[newcolor].foreground;
362 		}
363 	}
364 	}
365 #else
366 	{
367 	/* the following code is for PC6300 PLUS: it uses BOLD terminfo  */
368 	/* entry for turning on colors, and SGR0 for turning them off.   */
369 	/* Every time a new color-pair is used, we are forced to do an   */
370 	/* ioctl read, and the send 'enter_bold_mode' escape sequence.   */
371 	/* This could be improved  by using   */
372 	/* DIM, UNDERLINE, and REVERSE in addition to BOLD		 */
373 
374 	struct console con;
375 	_Color_pair *ptp = cur_term->_pairs_tbl;
376 	/* pairs table pointer */
377 	back = ptp[newcolor].background;
378 	fore = ptp[newcolor].foreground;
379 
380 	(void) fflush(SP->term_file);
381 	ioctl(cur_term->Filedes, CONIOGETDATA, &con);
382 #define	BOLD	4
383 	con.l[con.page].colors[BOLD] =
384 	    ((back + back + (fore > 5)) * 8 + fore) & 0177;
385 	ioctl(cur_term->Filedes, CONIOSETDATA, &con);
386 	(void) tputs(enter_bold_mode, 1, outc);
387 	}
388 #endif
389 }
390