xref: /illumos-gate/usr/src/lib/libcurses/screen/start_col.c (revision 24fe0b3bf671e123467ce1df0b67cadd3614c8e4)
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 2005 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 /*LINTLIBRARY*/
43 
44 #include <sys/types.h>
45 #include <stdlib.h>
46 #include "curses_inc.h"
47 
48 #ifdef PC6300PLUS
49 #include <fcntl.h>
50 #include <sys/console.h>
51 #endif
52 
53 int
54 start_color(void)
55 {
56 	short  i, j;
57 	_Color *color_tbl;
58 
59 #ifdef PC6300PLUS
60 	struct console  con;
61 #endif
62 
63 	/* if not a color terminal, return error    */
64 
65 	if ((COLOR_PAIRS = max_pairs) == -1)
66 		return (ERR);
67 
68 	/* we have only 6 bits to store color-pair info	*/
69 
70 	if (COLOR_PAIRS > 64)
71 		COLOR_PAIRS = 64;
72 
73 #ifdef PC6300PLUS
74 	ioctl(cur_term->Filedes, CONIOGETDATA, &con);
75 	if (!con.color)
76 		return (ERR);
77 #endif
78 
79 	/* allocate pairs_tbl	*/
80 
81 	if ((cur_term->_pairs_tbl =
82 	    (_Color_pair *) malloc((COLOR_PAIRS+1) *
83 	    sizeof (_Color_pair))) == NULL)
84 		goto err2;
85 
86 	COLORS = max_colors;
87 
88 /*  the following is not required because we assume that color 0 is */
89 /*  always a default background.  if this will change, we may want  */
90 /*  to store the default colors in entry 0 of pairs_tbl.	    */
91 /*
92  *	cur_term->_pairs_tbl[0].foreground = 0;
93  *	cur_term->_pairs_tbl[0].background = COLORS;
94  */
95 
96 	/* if terminal can change the definition of the color	*/
97 	/* allocate color_tbl					*/
98 
99 	if (can_change)
100 		if ((color_tbl = (cur_term->_color_tbl =
101 		    (_Color *) malloc(COLORS * sizeof (_Color)))) == NULL)
102 			goto err1;
103 
104 	/* allocate color mark map for cookie terminals */
105 
106 	if (ceol_standout_glitch || (magic_cookie_glitch >= 0)) {
107 		int	i, nc;
108 		char	**marks;
109 
110 		if ((marks = (char **)calloc((unsigned)LINES,
111 		    sizeof (char *))) == NULL)
112 			goto err;
113 		SP->_color_mks = marks;
114 		nc = (COLS / BITSPERBYTE) + (COLS % BITSPERBYTE ? 1 : 0);
115 		if ((*marks = (char *)calloc((unsigned)nc * LINES,
116 		    sizeof (char))) == NULL) {
117 			free(marks);
118 err:			free(color_tbl);
119 			cur_term->_color_tbl = NULL;
120 err1:			free(cur_term->_pairs_tbl);
121 			cur_term->_pairs_tbl = NULL;
122 err2:			return (ERR);
123 		}
124 
125 		for (i = LINES - 1; i-- > 0; ++marks)
126 			*(marks + 1) = *marks + nc;
127 	}
128 
129 	if (can_change) {
130 	/* initialize color_tbl with the following colors: black, blue,	*/
131 	/* green, cyan, red, magenta, yellow, black.  if table has more	*/
132 	/* than 8 entries, use the same 8 colors for the following 8	*/
133 	/* positions, and then again, and again ....  If table has less	*/
134 	/* then 8 entries, use as many colors as will fit in.		*/
135 
136 		for (i = 0; i < COLORS; i++) {
137 			j = i%8;
138 
139 			if (j%2)
140 				color_tbl[i].b = 1000;
141 			else
142 				color_tbl[i].b = 0;
143 
144 			if ((j%4) > 1)
145 				color_tbl[i].g = 1000;
146 			else
147 				color_tbl[i].g = 0;
148 
149 			if (j > 3)
150 				color_tbl[i].r = 1000;
151 			else
152 				color_tbl[i].r = 0;
153 		}
154 
155 		if (orig_colors)
156 			(void) tputs(orig_colors, 1, _outch);
157 	}
158 
159 	if (orig_pair)
160 		(void) tputs(tparm_p0(orig_pair), 1, _outch);
161 
162 	/* for Tek terminals set the background color to zero */
163 
164 	if (set_background) {
165 		(void) tputs(tparm_p1(set_background, 0), 1, _outch);
166 		cur_term->_cur_pair.background = 0;
167 		cur_term->_cur_pair.foreground = -1;
168 	}
169 	return (OK);
170 }
171