ncursesw-morphos/ncurses/tty/lib_vidattr.c

338 lines
10 KiB
C
Raw Normal View History

1998-03-01 12:21:12 +08:00
/****************************************************************************
2007-04-22 08:57:08 +08:00
* Copyright (c) 1998-2006,2007 Free Software Foundation, Inc. *
1998-03-01 12:21:12 +08:00
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the *
* "Software"), to deal in the Software without restriction, including *
* without limitation the rights to use, copy, modify, merge, publish, *
* distribute, distribute with modifications, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included *
* in all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
* IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
* THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
* *
* Except as contained in this notice, the name(s) of the above copyright *
* holders shall not be used in advertising or otherwise to promote the *
* sale, use or other dealings in this Software without prior written *
* authorization. *
****************************************************************************/
/****************************************************************************
* Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
* and: Eric S. Raymond <esr@snark.thyrsus.com> *
2005-10-10 02:41:57 +08:00
* and: Thomas E. Dickey 1996 on *
1998-03-01 12:21:12 +08:00
****************************************************************************/
1997-05-15 12:00:00 +08:00
/*
* vidputs(newmode, outc)
*
* newmode is taken to be the logical 'or' of the symbols in curses.h
* representing graphic renditions. The terminal is set to be in all of
* the given modes, if possible.
*
* if the new attribute is normal
* if exit-alt-char-set exists
* emit it
* emit exit-attribute-mode
* else if set-attributes exists
* use it to set exactly what you want
* else
* if exit-attribute-mode exists
* turn off everything
* else
* turn off those which can be turned off and aren't in
* newmode.
* turn on each mode which should be on and isn't, one by one
*
* NOTE that this algorithm won't achieve the desired mix of attributes
* in some cases, but those are probably just those cases in which it is
* actually impossible, anyway, so...
*
* NOTE that we cannot assume that there's no interaction between color
* and other attribute resets. So each time we reset color (or other
* attributes) we'll have to be prepared to restore the other.
*/
#include <curses.priv.h>
#include <term.h>
2007-04-22 08:57:08 +08:00
MODULE_ID("$Id: lib_vidattr.c,v 1.48 2007/04/21 23:25:38 tom Exp $")
1997-05-15 12:00:00 +08:00
#define doPut(mode) TPUTS_TRACE(#mode); tputs(mode, 1, outc)
#define TurnOn(mask,mode) \
if ((turn_on & mask) && mode) { doPut(mode); }
#define TurnOff(mask,mode) \
if ((turn_off & mask) && mode) { doPut(mode); turn_off &= ~mask; }
1999-10-24 12:32:42 +08:00
/* if there is no current screen, assume we *can* do color */
#define SetColorsIf(why,old_attr) \
2000-07-09 10:46:08 +08:00
if (can_color && (why)) { \
1999-10-24 12:32:42 +08:00
int old_pair = PAIR_NUMBER(old_attr); \
2000-10-21 12:42:11 +08:00
TR(TRACE_ATTRS, ("old pair = %d -- new pair = %d", old_pair, pair)); \
1999-10-24 12:32:42 +08:00
if ((pair != old_pair) \
2000-07-09 10:46:08 +08:00
|| (fix_pair0 && (pair == 0)) \
1999-10-24 12:32:42 +08:00
|| (reverse ^ ((old_attr & A_REVERSE) != 0))) { \
2000-07-09 10:46:08 +08:00
_nc_do_color(old_pair, pair, reverse, outc); \
1999-10-24 12:32:42 +08:00
} \
}
2007-04-22 08:57:08 +08:00
#define PreviousAttr _nc_prescreen.previous_attr
2002-10-13 11:35:53 +08:00
NCURSES_EXPORT(int)
2005-10-10 02:41:57 +08:00
vidputs(chtype newmode, int (*outc) (int))
1997-05-15 12:00:00 +08:00
{
2000-07-09 10:46:08 +08:00
attr_t turn_on, turn_off;
int pair;
bool reverse = FALSE;
bool can_color = (SP == 0 || SP->_coloron);
2000-10-21 12:42:11 +08:00
#if NCURSES_EXT_FUNCS
2000-07-09 10:46:08 +08:00
bool fix_pair0 = (SP != 0 && SP->_coloron && !SP->_default_color);
#else
#define fix_pair0 FALSE
#endif
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
T((T_CALLED("vidputs(%s)"), _traceattr(newmode)));
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
/* this allows us to go on whether or not newterm() has been called */
if (SP)
2007-04-22 08:57:08 +08:00
PreviousAttr = AttrOf(SCREEN_ATTRS(SP));
1997-05-15 12:00:00 +08:00
2007-04-22 08:57:08 +08:00
TR(TRACE_ATTRS, ("previous attribute was %s", _traceattr(PreviousAttr)));
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
if ((SP != 0)
2006-12-18 12:32:42 +08:00
&& (magic_cookie_glitch > 0)) {
#if USE_XMC_SUPPORT
2007-04-22 08:57:08 +08:00
static const chtype table[] =
2006-12-18 12:32:42 +08:00
{
A_STANDOUT,
A_UNDERLINE,
A_REVERSE,
A_BLINK,
A_DIM,
A_BOLD,
A_INVIS,
A_PROTECT,
};
unsigned n;
int used = 0;
int limit = (max_attributes <= 0) ? 1 : max_attributes;
chtype retain = 0;
/*
* Limit the number of attribute bits set in the newmode according to
* the terminfo max_attributes value.
*/
for (n = 0; n < SIZEOF(table); ++n) {
if ((table[n] & SP->_ok_attributes) == 0) {
newmode &= ~table[n];
} else if ((table[n] & newmode) != 0) {
if (used++ >= limit) {
newmode &= ~table[n];
if (newmode == retain)
break;
} else {
retain = newmode;
}
}
}
#else
2000-07-09 10:46:08 +08:00
newmode &= ~(SP->_xmc_suppress);
1998-03-01 12:21:12 +08:00
#endif
2006-12-18 12:32:42 +08:00
TR(TRACE_ATTRS, ("suppressed attribute is %s", _traceattr(newmode)));
}
1998-03-01 12:21:12 +08:00
2000-07-09 10:46:08 +08:00
/*
* If we have a terminal that cannot combine color with video
* attributes, use the colors in preference.
*/
if (((newmode & A_COLOR) != 0
2000-10-21 12:42:11 +08:00
|| fix_pair0)
2000-07-09 10:46:08 +08:00
&& (no_color_video > 0)) {
2000-10-21 12:42:11 +08:00
/*
* If we had chosen the A_xxx definitions to correspond to the
* no_color_video mask, we could simply shift it up and mask off the
* attributes. But we did not (actually copied Solaris' definitions).
* However, this is still simpler/faster than a lookup table.
*
* The 63 corresponds to A_STANDOUT, A_UNDERLINE, A_REVERSE, A_BLINK,
* A_DIM, A_BOLD which are 1:1 with no_color_video. The bits that
* correspond to A_INVIS, A_PROTECT (192) must be shifted up 1 and
* A_ALTCHARSET (256) down 2 to line up. We use the NCURSES_BITS
* macro so this will work properly for the wide-character layout.
*/
2002-10-13 11:35:53 +08:00
unsigned value = no_color_video;
attr_t mask = NCURSES_BITS((value & 63)
| ((value & 192) << 1)
| ((value & 256) >> 2), 8);
2000-10-21 12:42:11 +08:00
2002-10-13 11:35:53 +08:00
if ((mask & A_REVERSE) != 0
&& (newmode & A_REVERSE) != 0) {
2000-10-21 12:42:11 +08:00
reverse = TRUE;
mask &= ~A_REVERSE;
1998-03-01 12:21:12 +08:00
}
2000-10-21 12:42:11 +08:00
newmode &= ~mask;
2000-07-09 10:46:08 +08:00
}
2007-04-22 08:57:08 +08:00
if (newmode == PreviousAttr)
2000-07-09 10:46:08 +08:00
returnCode(OK);
pair = PAIR_NUMBER(newmode);
1998-03-01 12:21:12 +08:00
2000-07-09 10:46:08 +08:00
if (reverse) {
newmode &= ~A_REVERSE;
}
1997-05-15 12:00:00 +08:00
2007-04-22 08:57:08 +08:00
turn_off = (~newmode & PreviousAttr) & ALL_BUT_COLOR;
turn_on = (newmode & ~PreviousAttr) & ALL_BUT_COLOR;
1997-05-15 12:00:00 +08:00
2007-04-22 08:57:08 +08:00
SetColorsIf(((pair == 0) && !fix_pair0), PreviousAttr);
2000-07-09 10:46:08 +08:00
if (newmode == A_NORMAL) {
2007-04-22 08:57:08 +08:00
if ((PreviousAttr & A_ALTCHARSET) && exit_alt_charset_mode) {
2000-07-09 10:46:08 +08:00
doPut(exit_alt_charset_mode);
2007-04-22 08:57:08 +08:00
PreviousAttr &= ~A_ALTCHARSET;
2000-07-09 10:46:08 +08:00
}
2007-04-22 08:57:08 +08:00
if (PreviousAttr) {
2000-10-21 12:42:11 +08:00
if (exit_attribute_mode) {
doPut(exit_attribute_mode);
} else {
if (!SP || SP->_use_rmul) {
TurnOff(A_UNDERLINE, exit_underline_mode);
}
if (!SP || SP->_use_rmso) {
TurnOff(A_STANDOUT, exit_standout_mode);
}
}
2007-04-22 08:57:08 +08:00
PreviousAttr &= ALL_BUT_COLOR;
1998-03-01 12:21:12 +08:00
}
2007-04-22 08:57:08 +08:00
SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
2002-10-13 11:35:53 +08:00
} else if (set_attributes) {
2000-07-09 10:46:08 +08:00
if (turn_on || turn_off) {
TPUTS_TRACE("set_attributes");
tputs(tparm(set_attributes,
2000-10-21 12:42:11 +08:00
(newmode & A_STANDOUT) != 0,
(newmode & A_UNDERLINE) != 0,
(newmode & A_REVERSE) != 0,
(newmode & A_BLINK) != 0,
(newmode & A_DIM) != 0,
(newmode & A_BOLD) != 0,
(newmode & A_INVIS) != 0,
(newmode & A_PROTECT) != 0,
(newmode & A_ALTCHARSET) != 0), 1, outc);
2007-04-22 08:57:08 +08:00
PreviousAttr &= ALL_BUT_COLOR;
1997-05-15 12:00:00 +08:00
}
2007-04-22 08:57:08 +08:00
SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
2000-07-09 10:46:08 +08:00
} else {
1997-05-15 12:00:00 +08:00
2000-10-21 12:42:11 +08:00
TR(TRACE_ATTRS, ("turning %s off", _traceattr(turn_off)));
1998-03-01 12:21:12 +08:00
2000-07-09 10:46:08 +08:00
TurnOff(A_ALTCHARSET, exit_alt_charset_mode);
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
if (!SP || SP->_use_rmul) {
TurnOff(A_UNDERLINE, exit_underline_mode);
}
if (!SP || SP->_use_rmso) {
TurnOff(A_STANDOUT, exit_standout_mode);
}
if (turn_off && exit_attribute_mode) {
doPut(exit_attribute_mode);
2005-10-10 02:41:57 +08:00
turn_on |= (newmode & ALL_BUT_COLOR);
2007-04-22 08:57:08 +08:00
PreviousAttr &= ALL_BUT_COLOR;
2000-07-09 10:46:08 +08:00
}
2007-04-22 08:57:08 +08:00
SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
2000-07-09 10:46:08 +08:00
2000-10-21 12:42:11 +08:00
TR(TRACE_ATTRS, ("turning %s on", _traceattr(turn_on)));
2000-07-09 10:46:08 +08:00
/* *INDENT-OFF* */
TurnOn(A_ALTCHARSET, enter_alt_charset_mode);
TurnOn(A_BLINK, enter_blink_mode);
TurnOn(A_BOLD, enter_bold_mode);
TurnOn(A_DIM, enter_dim_mode);
TurnOn(A_REVERSE, enter_reverse_mode);
TurnOn(A_STANDOUT, enter_standout_mode);
TurnOn(A_PROTECT, enter_protected_mode);
TurnOn(A_INVIS, enter_secure_mode);
TurnOn(A_UNDERLINE, enter_underline_mode);
2005-10-10 02:41:57 +08:00
#if USE_WIDEC_SUPPORT
2000-07-09 10:46:08 +08:00
TurnOn(A_HORIZONTAL, enter_horizontal_hl_mode);
TurnOn(A_LEFT, enter_left_hl_mode);
TurnOn(A_LOW, enter_low_hl_mode);
TurnOn(A_RIGHT, enter_right_hl_mode);
TurnOn(A_TOP, enter_top_hl_mode);
TurnOn(A_VERTICAL, enter_vertical_hl_mode);
2002-10-13 11:35:53 +08:00
#endif
2000-07-09 10:46:08 +08:00
/* *INDENT-ON* */
}
if (reverse)
newmode |= A_REVERSE;
if (SP)
2005-10-10 02:41:57 +08:00
SetAttr(SCREEN_ATTRS(SP), newmode);
2000-07-09 10:46:08 +08:00
else
2007-04-22 08:57:08 +08:00
PreviousAttr = newmode;
2000-07-09 10:46:08 +08:00
returnCode(OK);
1997-05-15 12:00:00 +08:00
}
2002-10-13 11:35:53 +08:00
NCURSES_EXPORT(int)
vidattr(chtype newmode)
1997-05-15 12:00:00 +08:00
{
2000-07-09 10:46:08 +08:00
T((T_CALLED("vidattr(%s)"), _traceattr(newmode)));
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
returnCode(vidputs(newmode, _nc_outch));
1997-05-15 12:00:00 +08:00
}
2002-10-13 11:35:53 +08:00
NCURSES_EXPORT(chtype)
2000-07-09 10:46:08 +08:00
termattrs(void)
1997-05-15 12:00:00 +08:00
{
2000-07-09 10:46:08 +08:00
chtype attrs = A_NORMAL;
1997-05-15 12:00:00 +08:00
2000-10-21 12:42:11 +08:00
T((T_CALLED("termattrs()")));
2000-07-09 10:46:08 +08:00
if (enter_alt_charset_mode)
attrs |= A_ALTCHARSET;
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
if (enter_blink_mode)
attrs |= A_BLINK;
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
if (enter_bold_mode)
attrs |= A_BOLD;
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
if (enter_dim_mode)
attrs |= A_DIM;
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
if (enter_reverse_mode)
attrs |= A_REVERSE;
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
if (enter_standout_mode)
attrs |= A_STANDOUT;
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
if (enter_protected_mode)
attrs |= A_PROTECT;
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
if (enter_secure_mode)
attrs |= A_INVIS;
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
if (enter_underline_mode)
attrs |= A_UNDERLINE;
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
if (SP->_coloron)
attrs |= A_COLOR;
1997-05-15 12:00:00 +08:00
2000-10-21 12:42:11 +08:00
returnChar(attrs);
1997-05-15 12:00:00 +08:00
}