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