1 /**************************************************************************** 2 * Copyright (c) 2002-2009,2010 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.14 2010/12/19 01:44:24 tom Exp $") 40 41 #define doPut(mode) TPUTS_TRACE(#mode); NCURSES_SP_NAME(tputs)(NCURSES_SP_ARGx mode, 1, outc) 42 43 #define TurnOn(mask,mode) \ 44 if ((turn_on & mask) && mode) { doPut(mode); } 45 46 #define TurnOff(mask,mode) \ 47 if ((turn_off & mask) && mode) { doPut(mode); turn_off &= ~mask; } 48 49 /* if there is no current screen, assume we *can* do color */ 50 #define SetColorsIf(why, old_attr, old_pair) \ 51 if (can_color && (why)) { \ 52 TR(TRACE_ATTRS, ("old pair = %d -- new pair = %d", old_pair, pair)); \ 53 if ((pair != old_pair) \ 54 || (fix_pair0 && (pair == 0)) \ 55 || (reverse ^ ((old_attr & A_REVERSE) != 0))) { \ 56 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_ARGx \ 57 old_pair, pair, \ 58 reverse, outc); \ 59 } \ 60 } 61 62 #define set_color(mode, pair) \ 63 mode &= ALL_BUT_COLOR; \ 64 mode |= (attr_t) ColorPair(pair) 65 66 NCURSES_EXPORT(int) 67 NCURSES_SP_NAME(vid_puts) (NCURSES_SP_DCLx 68 attr_t newmode, 69 short pair, 70 void *opts GCC_UNUSED, 71 NCURSES_SP_OUTC outc) 72 { 73 #if NCURSES_EXT_COLORS 74 static attr_t previous_attr = A_NORMAL; 75 static int previous_pair = 0; 76 77 attr_t turn_on, turn_off; 78 bool reverse = FALSE; 79 bool can_color = (SP_PARM == 0 || SP_PARM->_coloron); 80 #if NCURSES_EXT_FUNCS 81 bool fix_pair0 = (SP_PARM != 0 && SP_PARM->_coloron && !SP_PARM->_default_color); 82 #else 83 #define fix_pair0 FALSE 84 #endif 85 86 newmode &= A_ATTRIBUTES; 87 T((T_CALLED("vid_puts(%s,%d)"), _traceattr(newmode), pair)); 88 89 /* this allows us to go on whether or not newterm() has been called */ 90 if (SP_PARM) { 91 previous_attr = AttrOf(SCREEN_ATTRS(SP_PARM)); 92 previous_pair = GetPair(SCREEN_ATTRS(SP_PARM)); 93 } 94 95 TR(TRACE_ATTRS, ("previous attribute was %s, %d", 96 _traceattr(previous_attr), previous_pair)); 97 98 #if !USE_XMC_SUPPORT 99 if ((SP_PARM != 0) 100 && (magic_cookie_glitch > 0)) 101 newmode &= ~(SP_PARM->_xmc_suppress); 102 #endif 103 104 /* 105 * If we have a terminal that cannot combine color with video 106 * attributes, use the colors in preference. 107 */ 108 if ((pair != 0 109 || fix_pair0) 110 && (no_color_video > 0)) { 111 /* 112 * If we had chosen the A_xxx definitions to correspond to the 113 * no_color_video mask, we could simply shift it up and mask off the 114 * attributes. But we did not (actually copied Solaris' definitions). 115 * However, this is still simpler/faster than a lookup table. 116 * 117 * The 63 corresponds to A_STANDOUT, A_UNDERLINE, A_REVERSE, A_BLINK, 118 * A_DIM, A_BOLD which are 1:1 with no_color_video. The bits that 119 * correspond to A_INVIS, A_PROTECT (192) must be shifted up 1 and 120 * A_ALTCHARSET (256) down 2 to line up. We use the NCURSES_BITS 121 * macro so this will work properly for the wide-character layout. 122 */ 123 unsigned value = no_color_video; 124 attr_t mask = NCURSES_BITS((value & 63) 125 | ((value & 192) << 1) 126 | ((value & 256) >> 2), 8); 127 128 if ((mask & A_REVERSE) != 0 129 && (newmode & A_REVERSE) != 0) { 130 reverse = TRUE; 131 mask &= ~A_REVERSE; 132 } 133 newmode &= ~mask; 134 } 135 136 if (newmode == previous_attr 137 && pair == previous_pair) 138 returnCode(OK); 139 140 if (reverse) { 141 newmode &= ~A_REVERSE; 142 } 143 144 turn_off = (~newmode & previous_attr) & ALL_BUT_COLOR; 145 turn_on = (newmode & ~previous_attr) & ALL_BUT_COLOR; 146 147 SetColorsIf(((pair == 0) && !fix_pair0), previous_attr, previous_pair); 148 149 if (newmode == A_NORMAL) { 150 if ((previous_attr & A_ALTCHARSET) && exit_alt_charset_mode) { 151 doPut(exit_alt_charset_mode); 152 previous_attr &= ~A_ALTCHARSET; 153 } 154 if (previous_attr) { 155 if (exit_attribute_mode) { 156 doPut(exit_attribute_mode); 157 } else { 158 if (!SP_PARM || SP_PARM->_use_rmul) { 159 TurnOff(A_UNDERLINE, exit_underline_mode); 160 } 161 if (!SP_PARM || SP_PARM->_use_rmso) { 162 TurnOff(A_STANDOUT, exit_standout_mode); 163 } 164 } 165 previous_attr &= ALL_BUT_COLOR; 166 previous_pair = 0; 167 } 168 169 SetColorsIf((pair != 0) || fix_pair0, previous_attr, previous_pair); 170 } else if (set_attributes) { 171 if (turn_on || turn_off) { 172 TPUTS_TRACE("set_attributes"); 173 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx 174 TPARM_9(set_attributes, 175 (newmode & A_STANDOUT) != 0, 176 (newmode & A_UNDERLINE) != 0, 177 (newmode & A_REVERSE) != 0, 178 (newmode & A_BLINK) != 0, 179 (newmode & A_DIM) != 0, 180 (newmode & A_BOLD) != 0, 181 (newmode & A_INVIS) != 0, 182 (newmode & A_PROTECT) != 0, 183 (newmode & A_ALTCHARSET) != 0), 184 1, outc); 185 previous_attr &= ALL_BUT_COLOR; 186 previous_pair = 0; 187 } 188 SetColorsIf((pair != 0) || fix_pair0, previous_attr, previous_pair); 189 } else { 190 191 TR(TRACE_ATTRS, ("turning %s off", _traceattr(turn_off))); 192 193 TurnOff(A_ALTCHARSET, exit_alt_charset_mode); 194 195 if (!SP_PARM || SP_PARM->_use_rmul) { 196 TurnOff(A_UNDERLINE, exit_underline_mode); 197 } 198 199 if (!SP_PARM || SP_PARM->_use_rmso) { 200 TurnOff(A_STANDOUT, exit_standout_mode); 201 } 202 203 if (turn_off && exit_attribute_mode) { 204 doPut(exit_attribute_mode); 205 turn_on |= (newmode & ALL_BUT_COLOR); 206 previous_attr &= ALL_BUT_COLOR; 207 previous_pair = 0; 208 } 209 SetColorsIf((pair != 0) || fix_pair0, previous_attr, previous_pair); 210 211 TR(TRACE_ATTRS, ("turning %s on", _traceattr(turn_on))); 212 /* *INDENT-OFF* */ 213 TurnOn(A_ALTCHARSET, enter_alt_charset_mode); 214 TurnOn(A_BLINK, enter_blink_mode); 215 TurnOn(A_BOLD, enter_bold_mode); 216 TurnOn(A_DIM, enter_dim_mode); 217 TurnOn(A_REVERSE, enter_reverse_mode); 218 TurnOn(A_STANDOUT, enter_standout_mode); 219 TurnOn(A_PROTECT, enter_protected_mode); 220 TurnOn(A_INVIS, enter_secure_mode); 221 TurnOn(A_UNDERLINE, enter_underline_mode); 222 #if USE_WIDEC_SUPPORT 223 TurnOn(A_HORIZONTAL, enter_horizontal_hl_mode); 224 TurnOn(A_LEFT, enter_left_hl_mode); 225 TurnOn(A_LOW, enter_low_hl_mode); 226 TurnOn(A_RIGHT, enter_right_hl_mode); 227 TurnOn(A_TOP, enter_top_hl_mode); 228 TurnOn(A_VERTICAL, enter_vertical_hl_mode); 229 #endif 230 /* *INDENT-ON* */ 231 232 } 233 234 if (reverse) 235 newmode |= A_REVERSE; 236 237 if (SP_PARM) { 238 SetAttr(SCREEN_ATTRS(SP_PARM), newmode); 239 SetPair(SCREEN_ATTRS(SP_PARM), pair); 240 } else { 241 previous_attr = newmode; 242 previous_pair = pair; 243 } 244 245 returnCode(OK); 246 #else 247 T((T_CALLED("vid_puts(%s,%d)"), _traceattr(newmode), pair)); 248 set_color(newmode, pair); 249 returnCode(NCURSES_SP_NAME(vidputs) (NCURSES_SP_ARGx newmode, outc)); 250 #endif 251 } 252 253 #if NCURSES_SP_FUNCS 254 NCURSES_EXPORT(int) 255 vid_puts(attr_t newmode, 256 short pair, 257 void *opts GCC_UNUSED, 258 NCURSES_OUTC outc) 259 { 260 SetSafeOutcWrapper(outc); 261 return NCURSES_SP_NAME(vid_puts) (CURRENT_SCREEN, 262 newmode, 263 pair, 264 opts, 265 _nc_outc_wrapper); 266 } 267 #endif 268 269 #undef vid_attr 270 NCURSES_EXPORT(int) 271 NCURSES_SP_NAME(vid_attr) (NCURSES_SP_DCLx 272 attr_t newmode, 273 short pair, 274 void *opts) 275 { 276 T((T_CALLED("vid_attr(%s,%d)"), _traceattr(newmode), pair)); 277 returnCode(NCURSES_SP_NAME(vid_puts) (NCURSES_SP_ARGx 278 newmode, 279 pair, 280 opts, 281 NCURSES_SP_NAME(_nc_outch))); 282 } 283 284 #if NCURSES_SP_FUNCS 285 NCURSES_EXPORT(int) 286 vid_attr(attr_t newmode, short pair, void *opts) 287 { 288 return NCURSES_SP_NAME(vid_attr) (CURRENT_SCREEN, newmode, pair, opts); 289 } 290 #endif 291 292 /* 293 * This implementation uses the same mask values for A_xxx and WA_xxx, so 294 * we can use termattrs() for part of the logic. 295 */ 296 NCURSES_EXPORT(attr_t) 297 NCURSES_SP_NAME(term_attrs) (NCURSES_SP_DCL0) 298 { 299 attr_t attrs; 300 301 T((T_CALLED("term_attrs()"))); 302 attrs = SP_PARM ? NCURSES_SP_NAME(termattrs) (NCURSES_SP_ARG) : 0; 303 304 /* these are only supported for wide-character mode */ 305 if (enter_horizontal_hl_mode) 306 attrs |= WA_HORIZONTAL; 307 if (enter_left_hl_mode) 308 attrs |= WA_LEFT; 309 if (enter_low_hl_mode) 310 attrs |= WA_LOW; 311 if (enter_right_hl_mode) 312 attrs |= WA_RIGHT; 313 if (enter_top_hl_mode) 314 attrs |= WA_TOP; 315 if (enter_vertical_hl_mode) 316 attrs |= WA_VERTICAL; 317 318 returnAttr(attrs); 319 } 320 321 #if NCURSES_SP_FUNCS 322 NCURSES_EXPORT(attr_t) 323 term_attrs(void) 324 { 325 return NCURSES_SP_NAME(term_attrs) (CURRENT_SCREEN); 326 } 327 #endif 328