17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright (c) 1995, by Sun Microsystems, Inc.
247c478bd9Sstevel@tonic-gate  * All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * color.c
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  * XCurses Library
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #ifdef M_RCSID
377c478bd9Sstevel@tonic-gate #ifndef lint
387c478bd9Sstevel@tonic-gate static char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/color.c 1.2 1995/10/02 15:15:02 ant Exp $";
397c478bd9Sstevel@tonic-gate #endif
407c478bd9Sstevel@tonic-gate #endif
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <private.h>
437c478bd9Sstevel@tonic-gate #include <stdlib.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate int
start_color()467c478bd9Sstevel@tonic-gate start_color()
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate 	int code = ERR;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #ifdef M_CURSES_TRACE
517c478bd9Sstevel@tonic-gate 	__m_trace("start_color(void)");
527c478bd9Sstevel@tonic-gate #endif
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	COLORS = max_colors;
55*1da57d55SToomas Soome 	COLOR_PAIRS = max_pairs;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	if (orig_colors != (char *) 0)
587c478bd9Sstevel@tonic-gate 		(void) tputs(orig_colors, 1, __m_outc);
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	if (orig_pair != (char *) 0)
617c478bd9Sstevel@tonic-gate 		(void) tputs(orig_pair, 1, __m_outc);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	if (0 < max_colors) {
647c478bd9Sstevel@tonic-gate 		cur_term->_color = calloc(max_colors, sizeof *cur_term->_color);
657c478bd9Sstevel@tonic-gate 		if (cur_term->_color == (short (*)[3]) 0)
667c478bd9Sstevel@tonic-gate 			goto error1;
677c478bd9Sstevel@tonic-gate 	}
68*1da57d55SToomas Soome 
697c478bd9Sstevel@tonic-gate 	if (0 < max_pairs) {
707c478bd9Sstevel@tonic-gate 		cur_term->_pair = calloc(max_pairs, sizeof *cur_term->_pair);
71*1da57d55SToomas Soome 		if (cur_term->_pair == (short (*)[2]) 0)
727c478bd9Sstevel@tonic-gate 			goto error2;
737c478bd9Sstevel@tonic-gate 	}
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	return __m_return_code("start_color", OK);
767c478bd9Sstevel@tonic-gate error2:
777c478bd9Sstevel@tonic-gate 	if (cur_term->_color != (short (*)[3]) 0)
787c478bd9Sstevel@tonic-gate 		free(cur_term->_color);
797c478bd9Sstevel@tonic-gate error1:
807c478bd9Sstevel@tonic-gate 	return __m_return_code("start_color", ERR);
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate int
init_color(short color,short r,short g,short b)847c478bd9Sstevel@tonic-gate init_color(short color, short r, short g, short b)
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	int code = ERR;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #ifdef M_CURSES_TRACE
897c478bd9Sstevel@tonic-gate 	__m_trace("init_color(%d, %d, %d, %d)", color, r, g, b);
907c478bd9Sstevel@tonic-gate #endif
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	if (!can_change || color < 0 || max_colors <= color
937c478bd9Sstevel@tonic-gate 	|| r < 0 || 1000 < r
947c478bd9Sstevel@tonic-gate 	|| g < 0 || 1000 < g
957c478bd9Sstevel@tonic-gate 	|| b < 0 || 1000 < b)
967c478bd9Sstevel@tonic-gate 		goto error;
97*1da57d55SToomas Soome 
987c478bd9Sstevel@tonic-gate 	/* Remember color settings for future queries. */
997c478bd9Sstevel@tonic-gate 	cur_term->_color[color][0] = r;
1007c478bd9Sstevel@tonic-gate 	cur_term->_color[color][1] = g;
1017c478bd9Sstevel@tonic-gate 	cur_term->_color[color][2] = b;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	code = OK;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	/* Set the color. */
1067c478bd9Sstevel@tonic-gate 	if (initialize_color != (char *) 0) {
1077c478bd9Sstevel@tonic-gate 		code = tputs(
1087c478bd9Sstevel@tonic-gate 			tparm(
1097c478bd9Sstevel@tonic-gate 				initialize_color, (long) color,
110*1da57d55SToomas Soome 				(long) r, (long) g, (long) b,
1117c478bd9Sstevel@tonic-gate 				0L, 0L, 0L, 0L, 0L
1127c478bd9Sstevel@tonic-gate 			), 1, __m_outc
1137c478bd9Sstevel@tonic-gate 		);
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate error:
1167c478bd9Sstevel@tonic-gate 	return __m_return_code("init_color", code);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate int
init_pair(short pair,short f,short b)1207c478bd9Sstevel@tonic-gate init_pair(short pair, short f, short b)
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate 	int code = ERR;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate #ifdef M_CURSES_TRACE
1257c478bd9Sstevel@tonic-gate 	__m_trace("init_pair(%d, %d, %d)", pair, f, b);
1267c478bd9Sstevel@tonic-gate #endif
1277c478bd9Sstevel@tonic-gate 
128*1da57d55SToomas Soome 	if (pair < 0 || max_pairs <= pair
1297c478bd9Sstevel@tonic-gate 	|| f < 0 || max_colors <= f
1307c478bd9Sstevel@tonic-gate 	|| b < 0 || max_colors <= b)
1317c478bd9Sstevel@tonic-gate 		goto error;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	/* Remember color-pair settings for future queries. */
1347c478bd9Sstevel@tonic-gate 	cur_term->_pair[pair][0] = f;
1357c478bd9Sstevel@tonic-gate 	cur_term->_pair[pair][1] = b;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	code = OK;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	/* Set color-pair (foreground-background). */
1407c478bd9Sstevel@tonic-gate 	if (initialize_pair == (char *) 0) {
1417c478bd9Sstevel@tonic-gate 		code = tputs(
1427c478bd9Sstevel@tonic-gate 			tparm(
143*1da57d55SToomas Soome 				initialize_pair,
144*1da57d55SToomas Soome 				(long) cur_term->_pair[f][0],
145*1da57d55SToomas Soome 				(long) cur_term->_pair[f][1],
146*1da57d55SToomas Soome 				(long) cur_term->_pair[f][2],
1477c478bd9Sstevel@tonic-gate 				(long) cur_term->_pair[b][0],
1487c478bd9Sstevel@tonic-gate 				(long) cur_term->_pair[b][1],
1497c478bd9Sstevel@tonic-gate 				(long) cur_term->_pair[b][2],
1507c478bd9Sstevel@tonic-gate 				0L, 0L, 0L
1517c478bd9Sstevel@tonic-gate 			), 1, __m_outc
1527c478bd9Sstevel@tonic-gate 		);
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate error:
1557c478bd9Sstevel@tonic-gate 	return __m_return_code("init_pair", code);
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate int
color_content(short color,short * r,short * g,short * b)1597c478bd9Sstevel@tonic-gate color_content(short color, short *r, short *g, short *b)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate #ifdef M_CURSES_TRACE
1627c478bd9Sstevel@tonic-gate 	__m_trace("color_content(%d, %p, %p, %p)", color, r, g, b);
1637c478bd9Sstevel@tonic-gate #endif
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	if (color < 0 || max_colors <= color)
1667c478bd9Sstevel@tonic-gate 		return __m_return_code("color_content", ERR);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	/* There does not appear to be a terminfo entry to query the
1697c478bd9Sstevel@tonic-gate 	 * color settings, so we retain them in an array for quick
1707c478bd9Sstevel@tonic-gate 	 * access.
1717c478bd9Sstevel@tonic-gate 	 */
1727c478bd9Sstevel@tonic-gate 	*r = cur_term->_color[color][0];
1737c478bd9Sstevel@tonic-gate 	*g = cur_term->_color[color][1];
1747c478bd9Sstevel@tonic-gate 	*b = cur_term->_color[color][2];
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	return __m_return_code("color_content", OK);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate int
pair_content(short pair,short * f,short * b)1807c478bd9Sstevel@tonic-gate pair_content(short pair, short *f, short *b)
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate #ifdef M_CURSES_TRACE
1837c478bd9Sstevel@tonic-gate 	__m_trace("pair_content(%d, %p, %p)", pair, f, b);
1847c478bd9Sstevel@tonic-gate #endif
1857c478bd9Sstevel@tonic-gate 	if (pair < 0 || max_pairs <= pair)
1867c478bd9Sstevel@tonic-gate 		return __m_return_code("pair_content", ERR);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	/* There does not appear to be a terminfo entry to query the
1897c478bd9Sstevel@tonic-gate 	 * color-pair settings, so we retain them in an array for quick
1907c478bd9Sstevel@tonic-gate 	 * access.
1917c478bd9Sstevel@tonic-gate 	 */
1927c478bd9Sstevel@tonic-gate 	*f = cur_term->_pair[pair][0];
1937c478bd9Sstevel@tonic-gate 	*b = cur_term->_pair[pair][1];
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	return __m_return_code("pair_content", OK);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate 
198