1 /**************************************************************************** 2 * Copyright (c) 2002-2013,2014 Free Software Foundation, Inc. * 3 * * 4 * Permission is hereby granted, free of charge, to any person obtaining a * 5 * copy of this software and associated documentation files (the * 6 * "Software"), to deal in the Software without restriction, including * 7 * without limitation the rights to use, copy, modify, merge, publish, * 8 * distribute, distribute with modifications, sublicense, and/or sell * 9 * copies of the Software, and to permit persons to whom the Software is * 10 * furnished to do so, subject to the following conditions: * 11 * * 12 * The above copyright notice and this permission notice shall be included * 13 * in all copies or substantial portions of the Software. * 14 * * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 22 * * 23 * Except as contained in this notice, the name(s) of the above copyright * 24 * holders shall not be used in advertising or otherwise to promote the * 25 * sale, use or other dealings in this Software without prior written * 26 * authorization. * 27 ****************************************************************************/ 28 29 /**************************************************************************** 30 * Author: Thomas E. Dickey * 31 ****************************************************************************/ 32 33 #include <curses.priv.h> 34 35 #ifndef CUR 36 #define CUR SP_TERMTYPE 37 #endif 38 39 MODULE_ID("$Id: lib_vid_attr.c,v 1.23 2014/06/07 22:13:46 tom Exp $") 40 41 #define doPut(mode) \ 42 TPUTS_TRACE(#mode); \ 43 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx mode, 1, outc) 44 45 #define TurnOn(mask, mode) \ 46 if ((turn_on & mask) && mode) { doPut(mode); } 47 48 #define TurnOff(mask, mode) \ 49 if ((turn_off & mask) && mode) { doPut(mode); turn_off &= ~mask; } 50 51 /* if there is no current screen, assume we *can* do color */ 52 #define SetColorsIf(why, old_attr, old_pair) \ 53 if (can_color && (why)) { \ 54 TR(TRACE_ATTRS, ("old pair = %d -- new pair = %d", old_pair, pair)); \ 55 if ((pair != old_pair) \ 56 || (fix_pair0 && (pair == 0)) \ 57 || (reverse ^ ((old_attr & A_REVERSE) != 0))) { \ 58 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_ARGx \ 59 old_pair, pair, \ 60 reverse, outc); \ 61 } \ 62 } 63 64 #define set_color(mode, pair) \ 65 mode &= ALL_BUT_COLOR; \ 66 mode |= (attr_t) ColorPair(pair) 67 68 NCURSES_EXPORT(int) 69 NCURSES_SP_NAME(vid_puts) (NCURSES_SP_DCLx 70 attr_t newmode, 71 NCURSES_PAIRS_T pair, 72 void *opts GCC_UNUSED, 73 NCURSES_SP_OUTC outc) 74 { 75 #if NCURSES_EXT_COLORS 76 static attr_t previous_attr = A_NORMAL; 77 static int previous_pair = 0; 78 79 attr_t turn_on, turn_off; 80 bool reverse = FALSE; 81 bool can_color = (SP_PARM == 0 || SP_PARM->_coloron); 82 #if NCURSES_EXT_FUNCS 83 bool fix_pair0 = (SP_PARM != 0 && SP_PARM->_coloron && !SP_PARM->_default_color); 84 #else 85 #define fix_pair0 FALSE 86 #endif 87 88 newmode &= A_ATTRIBUTES; 89 T((T_CALLED("vid_puts(%s,%d)"), _traceattr(newmode), pair)); 90 91 /* this allows us to go on whether or not newterm() has been called */ 92 if (SP_PARM) { 93 previous_attr = AttrOf(SCREEN_ATTRS(SP_PARM)); 94 previous_pair = GetPair(SCREEN_ATTRS(SP_PARM)); 95 } 96 97 TR(TRACE_ATTRS, ("previous attribute was %s, %d", 98 _traceattr(previous_attr), previous_pair)); 99 100 #if !USE_XMC_SUPPORT 101 if ((SP_PARM != 0) 102 && (magic_cookie_glitch > 0)) 103 newmode &= ~(SP_PARM->_xmc_suppress); 104 #endif 105 106 /* 107 * If we have a terminal that cannot combine color with video 108 * attributes, use the colors in preference. 109 */ 110 if ((pair != 0 111 || fix_pair0) 112 && (no_color_video > 0)) { 113 /* 114 * If we had chosen the A_xxx definitions to correspond to the 115 * no_color_video mask, we could simply shift it up and mask off the 116 * attributes. But we did not (actually copied Solaris' definitions). 117 * However, this is still simpler/faster than a lookup table. 118 * 119 * The 63 corresponds to A_STANDOUT, A_UNDERLINE, A_REVERSE, A_BLINK, 120 * A_DIM, A_BOLD which are 1:1 with no_color_video. The bits that 121 * correspond to A_INVIS, A_PROTECT (192) must be shifted up 1 and 122 * A_ALTCHARSET (256) down 2 to line up. We use the NCURSES_BITS 123 * macro so this will work properly for the wide-character layout. 124 */ 125 unsigned value = (unsigned) no_color_video; 126 attr_t mask = NCURSES_BITS((value & 63) 127 | ((value & 192) << 1) 128 | ((value & 256) >> 2), 8); 129 130 if ((mask & A_REVERSE) != 0 131 && (newmode & A_REVERSE) != 0) { 132 reverse = TRUE; 133 mask &= ~A_REVERSE; 134 } 135 newmode &= ~mask; 136 } 137 138 if (newmode == previous_attr 139 && pair == previous_pair) 140 returnCode(OK); 141 142 if (reverse) { 143 newmode &= ~A_REVERSE; 144 } 145 146 turn_off = (~newmode & previous_attr) & ALL_BUT_COLOR; 147 turn_on = (newmode & ~(previous_attr & TPARM_ATTR)) & ALL_BUT_COLOR; 148 149 SetColorsIf(((pair == 0) && !fix_pair0), previous_attr, previous_pair); 150 151 if (newmode == A_NORMAL) { 152 if ((previous_attr & A_ALTCHARSET) && exit_alt_charset_mode) { 153 doPut(exit_alt_charset_mode); 154 previous_attr &= ~A_ALTCHARSET; 155 } 156 if (previous_attr) { 157 if (exit_attribute_mode) { 158 doPut(exit_attribute_mode); 159 } else { 160 if (!SP_PARM || SP_PARM->_use_rmul) { 161 TurnOff(A_UNDERLINE, exit_underline_mode); 162 } 163 if (!SP_PARM || SP_PARM->_use_rmso) { 164 TurnOff(A_STANDOUT, exit_standout_mode); 165 } 166 #if USE_ITALIC 167 if (!SP_PARM || SP_PARM->_use_ritm) { 168 TurnOff(A_ITALIC, exit_italics_mode); 169 } 170 #endif 171 } 172 previous_attr &= ALL_BUT_COLOR; 173 previous_pair = 0; 174 } 175 176 SetColorsIf((pair != 0) || fix_pair0, previous_attr, previous_pair); 177 } else if (set_attributes) { 178 if (turn_on || turn_off) { 179 TPUTS_TRACE("set_attributes"); 180 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx 181 TPARM_9(set_attributes, 182 (newmode & A_STANDOUT) != 0, 183 (newmode & A_UNDERLINE) != 0, 184 (newmode & A_REVERSE) != 0, 185 (newmode & A_BLINK) != 0, 186 (newmode & A_DIM) != 0, 187 (newmode & A_BOLD) != 0, 188 (newmode & A_INVIS) != 0, 189 (newmode & A_PROTECT) != 0, 190 (newmode & A_ALTCHARSET) != 0), 191 1, outc); 192 previous_attr &= ALL_BUT_COLOR; 193 previous_pair = 0; 194 } 195 #if USE_ITALIC 196 if (!SP_PARM || SP_PARM->_use_ritm) { 197 if (turn_on & A_ITALIC) { 198 TurnOn(A_ITALIC, enter_italics_mode); 199 } else if (turn_off & A_ITALIC) { 200 TurnOff(A_ITALIC, exit_italics_mode); 201 } 202 } 203 #endif 204 SetColorsIf((pair != 0) || fix_pair0, previous_attr, previous_pair); 205 } else { 206 207 TR(TRACE_ATTRS, ("turning %s off", _traceattr(turn_off))); 208 209 TurnOff(A_ALTCHARSET, exit_alt_charset_mode); 210 211 if (!SP_PARM || SP_PARM->_use_rmul) { 212 TurnOff(A_UNDERLINE, exit_underline_mode); 213 } 214 215 if (!SP_PARM || SP_PARM->_use_rmso) { 216 TurnOff(A_STANDOUT, exit_standout_mode); 217 } 218 #if USE_ITALIC 219 if (!SP_PARM || SP_PARM->_use_ritm) { 220 TurnOff(A_ITALIC, exit_italics_mode); 221 } 222 #endif 223 if (turn_off && exit_attribute_mode) { 224 doPut(exit_attribute_mode); 225 turn_on |= (newmode & ALL_BUT_COLOR); 226 previous_attr &= ALL_BUT_COLOR; 227 previous_pair = 0; 228 } 229 SetColorsIf((pair != 0) || fix_pair0, previous_attr, previous_pair); 230 231 TR(TRACE_ATTRS, ("turning %s on", _traceattr(turn_on))); 232 /* *INDENT-OFF* */ 233 TurnOn(A_ALTCHARSET, enter_alt_charset_mode); 234 TurnOn(A_BLINK, enter_blink_mode); 235 TurnOn(A_BOLD, enter_bold_mode); 236 TurnOn(A_DIM, enter_dim_mode); 237 TurnOn(A_REVERSE, enter_reverse_mode); 238 TurnOn(A_STANDOUT, enter_standout_mode); 239 TurnOn(A_PROTECT, enter_protected_mode); 240 TurnOn(A_INVIS, enter_secure_mode); 241 TurnOn(A_UNDERLINE, enter_underline_mode); 242 #if USE_ITALIC 243 TurnOn(A_ITALIC, enter_italics_mode); 244 #endif 245 #if USE_WIDEC_SUPPORT 246 TurnOn(A_HORIZONTAL, enter_horizontal_hl_mode); 247 TurnOn(A_LEFT, enter_left_hl_mode); 248 TurnOn(A_LOW, enter_low_hl_mode); 249 TurnOn(A_RIGHT, enter_right_hl_mode); 250 TurnOn(A_TOP, enter_top_hl_mode); 251 TurnOn(A_VERTICAL, enter_vertical_hl_mode); 252 #endif 253 /* *INDENT-ON* */ 254 255 } 256 257 if (reverse) 258 newmode |= A_REVERSE; 259 260 if (SP_PARM) { 261 SetAttr(SCREEN_ATTRS(SP_PARM), newmode); 262 SetPair(SCREEN_ATTRS(SP_PARM), pair); 263 } else { 264 previous_attr = newmode; 265 previous_pair = pair; 266 } 267 268 returnCode(OK); 269 #else 270 T((T_CALLED("vid_puts(%s,%d)"), _traceattr(newmode), (int) pair)); 271 set_color(newmode, pair); 272 returnCode(NCURSES_SP_NAME(vidputs) (NCURSES_SP_ARGx newmode, outc)); 273 #endif 274 } 275 276 #if NCURSES_SP_FUNCS 277 NCURSES_EXPORT(int) 278 vid_puts(attr_t newmode, 279 NCURSES_PAIRS_T pair, 280 void *opts GCC_UNUSED, 281 NCURSES_OUTC outc) 282 { 283 SetSafeOutcWrapper(outc); 284 return NCURSES_SP_NAME(vid_puts) (CURRENT_SCREEN, 285 newmode, 286 pair, 287 opts, 288 _nc_outc_wrapper); 289 } 290 #endif 291 292 #undef vid_attr 293 NCURSES_EXPORT(int) 294 NCURSES_SP_NAME(vid_attr) (NCURSES_SP_DCLx 295 attr_t newmode, 296 NCURSES_PAIRS_T pair, 297 void *opts) 298 { 299 T((T_CALLED("vid_attr(%s,%d)"), _traceattr(newmode), (int) pair)); 300 returnCode(NCURSES_SP_NAME(vid_puts) (NCURSES_SP_ARGx 301 newmode, 302 pair, 303 opts, 304 NCURSES_SP_NAME(_nc_putchar))); 305 } 306 307 #if NCURSES_SP_FUNCS 308 NCURSES_EXPORT(int) 309 vid_attr(attr_t newmode, NCURSES_PAIRS_T pair, void *opts) 310 { 311 return NCURSES_SP_NAME(vid_attr) (CURRENT_SCREEN, newmode, pair, opts); 312 } 313 #endif 314 315 /* 316 * This implementation uses the same mask values for A_xxx and WA_xxx, so 317 * we can use termattrs() for part of the logic. 318 */ 319 NCURSES_EXPORT(attr_t) 320 NCURSES_SP_NAME(term_attrs) (NCURSES_SP_DCL0) 321 { 322 attr_t attrs = 0; 323 324 T((T_CALLED("term_attrs()"))); 325 if (SP_PARM) { 326 attrs = NCURSES_SP_NAME(termattrs) (NCURSES_SP_ARG); 327 328 /* these are only supported for wide-character mode */ 329 if (enter_horizontal_hl_mode) 330 attrs |= WA_HORIZONTAL; 331 if (enter_left_hl_mode) 332 attrs |= WA_LEFT; 333 if (enter_low_hl_mode) 334 attrs |= WA_LOW; 335 if (enter_right_hl_mode) 336 attrs |= WA_RIGHT; 337 if (enter_top_hl_mode) 338 attrs |= WA_TOP; 339 if (enter_vertical_hl_mode) 340 attrs |= WA_VERTICAL; 341 } 342 343 returnAttr(attrs); 344 } 345 346 #if NCURSES_SP_FUNCS 347 NCURSES_EXPORT(attr_t) 348 term_attrs(void) 349 { 350 return NCURSES_SP_NAME(term_attrs) (CURRENT_SCREEN); 351 } 352 #endif 353