1 /* $OpenBSD: lib_vidattr.c,v 1.5 2000/06/19 03:53:54 millert Exp $ */ 2 3 /**************************************************************************** 4 * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc. * 5 * * 6 * Permission is hereby granted, free of charge, to any person obtaining a * 7 * copy of this software and associated documentation files (the * 8 * "Software"), to deal in the Software without restriction, including * 9 * without limitation the rights to use, copy, modify, merge, publish, * 10 * distribute, distribute with modifications, sublicense, and/or sell * 11 * copies of the Software, and to permit persons to whom the Software is * 12 * furnished to do so, subject to the following conditions: * 13 * * 14 * The above copyright notice and this permission notice shall be included * 15 * in all copies or substantial portions of the Software. * 16 * * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 20 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 23 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 24 * * 25 * Except as contained in this notice, the name(s) of the above copyright * 26 * holders shall not be used in advertising or otherwise to promote the * 27 * sale, use or other dealings in this Software without prior written * 28 * authorization. * 29 ****************************************************************************/ 30 31 /**************************************************************************** 32 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 33 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 34 ****************************************************************************/ 35 36 /* 37 * vidputs(newmode, outc) 38 * 39 * newmode is taken to be the logical 'or' of the symbols in curses.h 40 * representing graphic renditions. The terminal is set to be in all of 41 * the given modes, if possible. 42 * 43 * if the new attribute is normal 44 * if exit-alt-char-set exists 45 * emit it 46 * emit exit-attribute-mode 47 * else if set-attributes exists 48 * use it to set exactly what you want 49 * else 50 * if exit-attribute-mode exists 51 * turn off everything 52 * else 53 * turn off those which can be turned off and aren't in 54 * newmode. 55 * turn on each mode which should be on and isn't, one by one 56 * 57 * NOTE that this algorithm won't achieve the desired mix of attributes 58 * in some cases, but those are probably just those cases in which it is 59 * actually impossible, anyway, so... 60 * 61 * NOTE that we cannot assume that there's no interaction between color 62 * and other attribute resets. So each time we reset color (or other 63 * attributes) we'll have to be prepared to restore the other. 64 */ 65 66 #include <curses.priv.h> 67 #include <term.h> 68 69 MODULE_ID("$From: lib_vidattr.c,v 1.27 2000/04/29 23:25:27 tom Exp $") 70 71 #define doPut(mode) TPUTS_TRACE(#mode); tputs(mode, 1, outc) 72 73 #define TurnOn(mask,mode) \ 74 if ((turn_on & mask) && mode) { doPut(mode); } 75 76 #define TurnOff(mask,mode) \ 77 if ((turn_off & mask) && mode) { doPut(mode); turn_off &= ~mask; } 78 79 /* if there is no current screen, assume we *can* do color */ 80 #define SetColorsIf(why,old_attr) \ 81 if (can_color && (why)) { \ 82 int old_pair = PAIR_NUMBER(old_attr); \ 83 T(("old pair = %d -- new pair = %d", old_pair, pair)); \ 84 if ((pair != old_pair) \ 85 || (fix_pair0 && (pair == 0)) \ 86 || (reverse ^ ((old_attr & A_REVERSE) != 0))) { \ 87 _nc_do_color(old_pair, pair, reverse, outc); \ 88 } \ 89 } 90 91 int 92 vidputs(attr_t newmode, int (*outc) (int)) 93 { 94 static attr_t previous_attr = A_NORMAL; 95 attr_t turn_on, turn_off; 96 int pair; 97 bool reverse = FALSE; 98 bool used_ncv = FALSE; 99 bool can_color = (SP == 0 || SP->_coloron); 100 #ifdef NCURSES_EXT_FUNCS 101 bool fix_pair0 = (SP != 0 && SP->_coloron && !SP->_default_color); 102 #else 103 #define fix_pair0 FALSE 104 #endif 105 106 T((T_CALLED("vidputs(%s)"), _traceattr(newmode))); 107 108 /* this allows us to go on whether or not newterm() has been called */ 109 if (SP) 110 previous_attr = SP->_current_attr; 111 112 T(("previous attribute was %s", _traceattr(previous_attr))); 113 114 #if !USE_XMC_SUPPORT 115 if ((SP != 0) 116 && (magic_cookie_glitch > 0)) 117 newmode &= ~(SP->_xmc_suppress); 118 #endif 119 120 /* 121 * If we have a terminal that cannot combine color with video 122 * attributes, use the colors in preference. 123 */ 124 if (((newmode & A_COLOR) != 0 125 || fix_pair0) 126 && (no_color_video > 0)) { 127 /* *INDENT-OFF* */ 128 static const struct { 129 attr_t video; 130 unsigned bit; 131 } table[] = { 132 { A_STANDOUT, 1 }, 133 { A_UNDERLINE, 2 }, 134 { A_REVERSE, 4 }, 135 { A_BLINK, 8 }, 136 { A_DIM, 16 }, 137 { A_BOLD, 32 }, 138 { A_INVIS, 64 }, 139 { A_PROTECT, 128 }, 140 { A_ALTCHARSET, 256 }, 141 }; 142 /* *INDENT-ON* */ 143 144 size_t n; 145 for (n = 0; n < SIZEOF(table); n++) { 146 if ((table[n].bit & no_color_video) 147 && (table[n].video & newmode)) { 148 used_ncv = TRUE; 149 if (table[n].video == A_REVERSE) 150 reverse = TRUE; 151 else 152 newmode &= ~table[n].video; 153 } 154 } 155 } 156 157 if (newmode == previous_attr) 158 returnCode(OK); 159 160 pair = PAIR_NUMBER(newmode); 161 162 if (reverse) { 163 newmode &= ~A_REVERSE; 164 } 165 166 turn_off = (~newmode & previous_attr) & ALL_BUT_COLOR; 167 turn_on = (newmode & ~previous_attr) & ALL_BUT_COLOR; 168 169 SetColorsIf(((pair == 0) && !fix_pair0), previous_attr); 170 171 if (newmode == A_NORMAL) { 172 if ((previous_attr & A_ALTCHARSET) && exit_alt_charset_mode) { 173 doPut(exit_alt_charset_mode); 174 previous_attr &= ~A_ALTCHARSET; 175 } 176 if (previous_attr) { 177 doPut(exit_attribute_mode); 178 previous_attr &= ~A_COLOR; 179 } 180 181 SetColorsIf((pair != 0) || fix_pair0, previous_attr); 182 } else if (set_attributes && !used_ncv) { 183 if (turn_on || turn_off) { 184 TPUTS_TRACE("set_attributes"); 185 tputs(tparm(set_attributes, 186 (newmode & A_STANDOUT) != 0, 187 (newmode & A_UNDERLINE) != 0, 188 (newmode & A_REVERSE) != 0, 189 (newmode & A_BLINK) != 0, 190 (newmode & A_DIM) != 0, 191 (newmode & A_BOLD) != 0, 192 (newmode & A_INVIS) != 0, 193 (newmode & A_PROTECT) != 0, 194 (newmode & A_ALTCHARSET) != 0), 1, outc); 195 previous_attr &= ~A_COLOR; 196 } 197 SetColorsIf((pair != 0) || fix_pair0, previous_attr); 198 } else { 199 200 T(("turning %s off", _traceattr(turn_off))); 201 202 TurnOff(A_ALTCHARSET, exit_alt_charset_mode); 203 204 if (!SP || SP->_use_rmul) { 205 TurnOff(A_UNDERLINE, exit_underline_mode); 206 } 207 208 if (!SP || SP->_use_rmso) { 209 TurnOff(A_STANDOUT, exit_standout_mode); 210 } 211 212 if (turn_off && exit_attribute_mode) { 213 doPut(exit_attribute_mode); 214 turn_on |= (newmode & (chtype) (~A_COLOR)); 215 previous_attr &= ~A_COLOR; 216 } 217 SetColorsIf((pair != 0) || fix_pair0, previous_attr); 218 219 T(("turning %s on", _traceattr(turn_on))); 220 /* *INDENT-OFF* */ 221 TurnOn(A_ALTCHARSET, enter_alt_charset_mode); 222 TurnOn(A_BLINK, enter_blink_mode); 223 TurnOn(A_BOLD, enter_bold_mode); 224 TurnOn(A_DIM, enter_dim_mode); 225 TurnOn(A_REVERSE, enter_reverse_mode); 226 TurnOn(A_STANDOUT, enter_standout_mode); 227 TurnOn(A_PROTECT, enter_protected_mode); 228 TurnOn(A_INVIS, enter_secure_mode); 229 TurnOn(A_UNDERLINE, enter_underline_mode); 230 TurnOn(A_HORIZONTAL, enter_horizontal_hl_mode); 231 TurnOn(A_LEFT, enter_left_hl_mode); 232 TurnOn(A_LOW, enter_low_hl_mode); 233 TurnOn(A_RIGHT, enter_right_hl_mode); 234 TurnOn(A_TOP, enter_top_hl_mode); 235 TurnOn(A_VERTICAL, enter_vertical_hl_mode); 236 /* *INDENT-ON* */ 237 238 } 239 240 if (reverse) 241 newmode |= A_REVERSE; 242 243 if (SP) 244 SP->_current_attr = newmode; 245 else 246 previous_attr = newmode; 247 248 returnCode(OK); 249 } 250 251 int 252 vidattr(attr_t newmode) 253 { 254 T((T_CALLED("vidattr(%s)"), _traceattr(newmode))); 255 256 returnCode(vidputs(newmode, _nc_outch)); 257 } 258 259 chtype 260 termattrs(void) 261 { 262 chtype attrs = A_NORMAL; 263 264 if (enter_alt_charset_mode) 265 attrs |= A_ALTCHARSET; 266 267 if (enter_blink_mode) 268 attrs |= A_BLINK; 269 270 if (enter_bold_mode) 271 attrs |= A_BOLD; 272 273 if (enter_dim_mode) 274 attrs |= A_DIM; 275 276 if (enter_reverse_mode) 277 attrs |= A_REVERSE; 278 279 if (enter_standout_mode) 280 attrs |= A_STANDOUT; 281 282 if (enter_protected_mode) 283 attrs |= A_PROTECT; 284 285 if (enter_secure_mode) 286 attrs |= A_INVIS; 287 288 if (enter_underline_mode) 289 attrs |= A_UNDERLINE; 290 291 if (SP->_coloron) 292 attrs |= A_COLOR; 293 294 return (attrs); 295 } 296