From 9bb22e7d7b53c693039bd457457f320a5de4988a Mon Sep 17 00:00:00 2001 From: Markus Gans Date: Wed, 3 Jan 2018 20:06:22 +0100 Subject: [PATCH] Refactoring attribute settings in FOptiAttr --- ChangeLog | 1 + include/final/foptiattr.h | 9 +- src/foptiattr.cpp | 302 ++++++++++++++++++++++---------------- src/fterm.cpp | 2 +- 4 files changed, 188 insertions(+), 126 deletions(-) diff --git a/ChangeLog b/ChangeLog index b3a822dd..64c7efb6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,5 @@ 2017-01-02 Markus Gans + * Refactoring attribute settings in FOptiAttr * Refactoring FTerm::parseKeyString and timeout settings 2017-01-02 Markus Gans diff --git a/include/final/foptiattr.h b/include/final/foptiattr.h index e45ac9e3..2a917d8a 100644 --- a/include/final/foptiattr.h +++ b/include/final/foptiattr.h @@ -3,7 +3,7 @@ * * * This file is part of the Final Cut widget toolkit * * * -* Copyright 2016-2017 Markus Gans * +* Copyright 2016-2018 Markus Gans * * * * The Final Cut is free software; you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public License * @@ -270,7 +270,14 @@ class FOptiAttr bool colorChange (char_data*&, char_data*&); void resetColor (char_data*&); void prevent_no_color_video_attributes (char_data*&); + void preProcessing_cygwin_quirks (char_data*&); + void postProcessing_cygwin_quirks (char_data*&, char_data*&); + void deactivateAttributes (char_data*&, char_data*&); + void changeAttributeSGR (char_data*&, char_data*&); + void changeAttributeSeparately (char_data*&, char_data*&); void change_color (char_data*&, char_data*&); + void change_to_default_color (char_data*&, char_data*&, short&, short&); + void change_current_color (char_data*&, short, short); void resetAttribute (char_data*&); void reset (char_data*&); bool caused_reset_attributes (char[], uChar = all_tests); diff --git a/src/foptiattr.cpp b/src/foptiattr.cpp index 1cb5b9ef..980d96da 100644 --- a/src/foptiattr.cpp +++ b/src/foptiattr.cpp @@ -539,98 +539,23 @@ char* FOptiAttr::changeAttribute (char_data*& term, char_data*& next) if ( ! F_enter_secure_mode.cap && next->attr.bit.invisible ) next->code = ' '; - // look for no changes + // Look for no changes if ( ! ( switchOn() || switchOff() || colorChange(term, next) ) ) return 0; - if ( cygwin_terminal && (term->fg_color > 7 || term->bg_color > 7) ) - { - // reset blink and bold mode from colors > 7 - char rst[] = CSI "m"; - append_sequence (rst); - reset(term); - } + preProcessing_cygwin_quirks(term); if ( hasNoAttribute(next) ) { - if ( hasAttribute(term) ) - { - if ( F_exit_attribute_mode.cap ) - { - unsetTermAttributes(term); - - if ( off.attr.bit.pc_charset ) - unsetTermPCcharset(term); - - if ( off.attr.bit.alt_charset ) - unsetTermAltCharset(term); - } - else - setAttributesOff(term); - } - - if ( colorChange(term, next) ) - change_color (term, next); + deactivateAttributes (term, next); } else if ( F_set_attributes.cap && ! term->attr.bit.pc_charset ) { - if ( switchOn() || switchOff() ) - setTermAttributes ( term - , next->attr.bit.standout - , next->attr.bit.underline - , next->attr.bit.reverse - , next->attr.bit.blink - , next->attr.bit.dim - , next->attr.bit.bold - , next->attr.bit.invisible - , next->attr.bit.protect - , next->attr.bit.alt_charset ); - - if ( off.attr.bit.pc_charset ) - unsetTermPCcharset(term); - - if ( next->attr.bit.italic ) - setTermItalic(term); - - if ( next->attr.bit.crossed_out ) - setTermCrossedOut(term); - - if ( next->attr.bit.dbl_underline ) - setTermDoubleUnderline(term); - - if ( next->attr.bit.pc_charset ) - setTermPCcharset(term); - - if ( colorChange(term, next) ) - { - change_color(term, next); - - if ( cygwin_terminal ) - { - if ( next->attr.bit.bold ) - setTermBold(term); - - if ( next->attr.bit.reverse ) - setTermReverse(term); - - if ( next->attr.bit.standout ) - setTermStandout(term); - } - } + changeAttributeSGR (term, next); } else { - setAttributesOff(term); - detectSwitchOff (term, next); - - if ( switchOff() ) - unsetTermAttributes(term); - - if ( colorChange(term, next) ) - change_color (term, next); - - detectSwitchOn (term, next); - setAttributesOn(term); + changeAttributeSeparately (term, next); } if ( term && fake_reverse ) @@ -1265,15 +1190,124 @@ inline void FOptiAttr::prevent_no_color_video_attributes (char_data*& attr) } } +//---------------------------------------------------------------------- +inline void FOptiAttr::preProcessing_cygwin_quirks (char_data*& term) +{ + // Cygwin bold color fix pre processing + + if ( ! cygwin_terminal ) + return; + + if ( term->fg_color > 7 || term->bg_color > 7 ) + { + // Reset blink and bold mode from colors > 7 + char rst[] = CSI "m"; + append_sequence (rst); + reset(term); + } +} + +//---------------------------------------------------------------------- +inline void FOptiAttr::postProcessing_cygwin_quirks ( char_data*& term + , char_data*& next ) +{ + // Cygwin bold color fix post processing + + if ( ! cygwin_terminal ) + return; + + if ( next->attr.bit.bold ) + setTermBold(term); + + if ( next->attr.bit.reverse ) + setTermReverse(term); + + if ( next->attr.bit.standout ) + setTermStandout(term); +} + +//---------------------------------------------------------------------- +inline void FOptiAttr::deactivateAttributes ( char_data*& term + , char_data*& next ) +{ + if ( hasAttribute(term) ) + { + if ( F_exit_attribute_mode.cap ) + { + unsetTermAttributes(term); + + if ( off.attr.bit.pc_charset ) + unsetTermPCcharset(term); + + if ( off.attr.bit.alt_charset ) + unsetTermAltCharset(term); + } + else + setAttributesOff(term); + } + + if ( colorChange(term, next) ) + change_color (term, next); +} + +//---------------------------------------------------------------------- +inline void FOptiAttr::changeAttributeSGR ( char_data*& term + , char_data*& next ) +{ + if ( switchOn() || switchOff() ) + setTermAttributes ( term + , next->attr.bit.standout + , next->attr.bit.underline + , next->attr.bit.reverse + , next->attr.bit.blink + , next->attr.bit.dim + , next->attr.bit.bold + , next->attr.bit.invisible + , next->attr.bit.protect + , next->attr.bit.alt_charset ); + + if ( off.attr.bit.pc_charset ) + unsetTermPCcharset(term); + + if ( next->attr.bit.italic ) + setTermItalic(term); + + if ( next->attr.bit.crossed_out ) + setTermCrossedOut(term); + + if ( next->attr.bit.dbl_underline ) + setTermDoubleUnderline(term); + + if ( next->attr.bit.pc_charset ) + setTermPCcharset(term); + + if ( colorChange(term, next) ) + { + change_color(term, next); + postProcessing_cygwin_quirks(term, next); + } +} + +//---------------------------------------------------------------------- +inline void FOptiAttr::changeAttributeSeparately ( char_data*& term + , char_data*& next ) +{ + setAttributesOff(term); + detectSwitchOff (term, next); + + if ( switchOff() ) + unsetTermAttributes(term); + + if ( colorChange(term, next) ) + change_color (term, next); + + detectSwitchOn (term, next); + setAttributesOn(term); +} + //---------------------------------------------------------------------- void FOptiAttr::change_color (char_data*& term, char_data*& next) { - char* color_str; - char* AF = F_set_a_foreground.cap; - char* AB = F_set_a_background.cap; - char* Sf = F_set_foreground.cap; - char* Sb = F_set_background.cap; - char* sp = F_set_color_pair.cap; short fg, bg; if ( monochron || ! (term && next) ) @@ -1283,41 +1317,7 @@ void FOptiAttr::change_color (char_data*& term, char_data*& next) bg = next->bg_color; if ( fg == Default || bg == Default ) - { - if ( ansi_default_color ) - { - if ( fg == Default && term->fg_color != Default - && bg == Default && term->bg_color != Default ) - { - setTermDefaultColor(term); - } - else if ( fg == Default && term->fg_color != Default ) - { - char sgr_39[] = CSI "39m"; - append_sequence (sgr_39); - term->fg_color = Default; - } - else if ( bg == Default && term->bg_color != Default ) - { - char* sgr_49; - char* op = F_orig_pair.cap; - - if ( op && std::strncmp (op, CSI "39;49;25m", 11) == 0 ) - sgr_49 = C_STR(CSI "49;25m"); - else - sgr_49 = C_STR(CSI "49m"); - - append_sequence (sgr_49); - term->bg_color = Default; - } - } - else if ( ! setTermDefaultColor(term) ) - { - // fallback to gray on black - fg = next->fg_color = LightGray; - bg = next->bg_color = Black; - } - } + change_to_default_color (term, next, fg, bg); if ( ! fake_reverse && fg < 0 && bg < 0 ) return; @@ -1330,6 +1330,63 @@ void FOptiAttr::change_color (char_data*& term, char_data*& next) setTermDefaultColor(term); } + change_current_color (term, fg, bg); + + term->fg_color = next->fg_color; + term->bg_color = next->bg_color; +} + +//---------------------------------------------------------------------- +inline void FOptiAttr::change_to_default_color ( char_data*& term + , char_data*& next + , short& fg, short& bg ) +{ + if ( ansi_default_color ) + { + if ( fg == Default && term->fg_color != Default + && bg == Default && term->bg_color != Default ) + { + setTermDefaultColor(term); + } + else if ( fg == Default && term->fg_color != Default ) + { + char sgr_39[] = CSI "39m"; + append_sequence (sgr_39); + term->fg_color = Default; + } + else if ( bg == Default && term->bg_color != Default ) + { + char* sgr_49; + char* op = F_orig_pair.cap; + + if ( op && std::strncmp (op, CSI "39;49;25m", 11) == 0 ) + sgr_49 = C_STR(CSI "49;25m"); + else + sgr_49 = C_STR(CSI "49m"); + + append_sequence (sgr_49); + term->bg_color = Default; + } + } + else if ( ! setTermDefaultColor(term) ) + { + // Fallback to gray on black + fg = next->fg_color = LightGray; + bg = next->bg_color = Black; + } +} + +//---------------------------------------------------------------------- +inline void FOptiAttr::change_current_color ( char_data*& term + , short fg, short bg ) +{ + char* color_str; + char* AF = F_set_a_foreground.cap; + char* AB = F_set_a_background.cap; + char* Sf = F_set_foreground.cap; + char* Sb = F_set_background.cap; + char* sp = F_set_color_pair.cap; + if ( AF && AB ) { short ansi_fg = vga2ansi(fg); @@ -1361,9 +1418,6 @@ void FOptiAttr::change_color (char_data*& term, char_data*& next) if ( (color_str = tparm(sp, fg, bg, 0, 0, 0, 0, 0, 0, 0)) ) append_sequence (color_str); } - - term->fg_color = next->fg_color; - term->bg_color = next->bg_color; } //---------------------------------------------------------------------- diff --git a/src/fterm.cpp b/src/fterm.cpp index 8f68e311..cf496bf0 100644 --- a/src/fterm.cpp +++ b/src/fterm.cpp @@ -4867,7 +4867,7 @@ inline int FTerm::getMetaKey ( char buffer[] } //---------------------------------------------------------------------- -int FTerm::getSingleKey (char buffer[], int buf_size) +inline int FTerm::getSingleKey (char buffer[], int buf_size) { register uChar firstchar = uChar(buffer[0]); int key, n, len;