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