From 05d0dda11b83f675c53d9fdb3ea02123f137df28 Mon Sep 17 00:00:00 2001 From: elfmz Date: Sun, 23 Jul 2023 11:28:03 +0300 Subject: [PATCH] add --norgb command line argument that forces using base 16 colors palette --- WinPort/src/Backend/Backend.h | 3 ++- WinPort/src/Backend/TTY/TTYBackend.cpp | 13 +++++++++---- WinPort/src/Backend/TTY/TTYBackend.h | 3 ++- WinPort/src/Backend/TTY/TTYOutput.cpp | 12 +++++++++--- WinPort/src/Backend/TTY/TTYOutput.h | 4 ++-- WinPort/src/Backend/WX/wxMain.cpp | 3 ++- WinPort/src/Backend/WX/wxWinTranslations.cpp | 26 +++++++++++++++----------- WinPort/src/Backend/WX/wxWinTranslations.h | 1 + WinPort/src/Backend/WinPortMain.cpp | 19 ++++++++++++++----- man/far2l.1 | 3 +++ man/ru/far2l.1 | 3 +++ 11 files changed, 62 insertions(+), 28 deletions(-) diff --git a/WinPort/src/Backend/Backend.h b/WinPort/src/Backend/Backend.h index e4c1fe7d..6f093ee9 100644 --- a/WinPort/src/Backend/Backend.h +++ b/WinPort/src/Backend/Backend.h @@ -8,7 +8,7 @@ /// Something changed in code below. /// "WinCompat.h" changed in a way affecting code below. /// Behavior of backend's code changed in incompatible way. -#define FAR2L_BACKEND_ABI_VERSION 0x05 +#define FAR2L_BACKEND_ABI_VERSION 0x06 class IConsoleOutputBackend { @@ -240,5 +240,6 @@ struct WinPortMainBackendArg IConsoleOutput *winport_con_out; IConsoleInput *winport_con_in; bool ext_clipboard; + bool norgb; }; diff --git a/WinPort/src/Backend/TTY/TTYBackend.cpp b/WinPort/src/Backend/TTY/TTYBackend.cpp index f0b2a8c1..53cdd652 100644 --- a/WinPort/src/Backend/TTY/TTYBackend.cpp +++ b/WinPort/src/Backend/TTY/TTYBackend.cpp @@ -79,11 +79,12 @@ static WORD WChar2WinVKeyCode(WCHAR wc) } -TTYBackend::TTYBackend(const char *full_exe_path, int std_in, int std_out, bool ext_clipboard, const char *nodetect, bool far2l_tty, unsigned int esc_expiration, int notify_pipe, int *result) : +TTYBackend::TTYBackend(const char *full_exe_path, int std_in, int std_out, bool ext_clipboard, bool norgb, const char *nodetect, bool far2l_tty, unsigned int esc_expiration, int notify_pipe, int *result) : _full_exe_path(full_exe_path), _stdin(std_in), _stdout(std_out), _ext_clipboard(ext_clipboard), + _norgb(norgb), _nodetect(nodetect), _far2l_tty(far2l_tty), _esc_expiration(esc_expiration), @@ -367,7 +368,7 @@ void TTYBackend::WriterThread() { bool gone_background = false; try { - TTYOutput tty_out(_stdout, _far2l_tty); + TTYOutput tty_out(_stdout, _far2l_tty, _norgb); DispatchPalette(tty_out); // DispatchTermResized(tty_out); while (!_exiting && !_deadio) { @@ -718,6 +719,10 @@ bool TTYBackend::OnConsoleSetFKeyTitles(const char **titles) BYTE TTYBackend::OnConsoleGetColorPalette() { + if (_norgb) { + return 4; + } + if (_far2l_tty) try { StackSerializer stk_ser; stk_ser.PushNum(FARTTY_INTERRACT_GET_COLOR_PALETTE); @@ -1204,9 +1209,9 @@ static void OnSigHup(int signo) } -bool WinPortMainTTY(const char *full_exe_path, int std_in, int std_out, bool ext_clipboard, const char *nodetect, bool far2l_tty, unsigned int esc_expiration, int notify_pipe, int argc, char **argv, int(*AppMain)(int argc, char **argv), int *result) +bool WinPortMainTTY(const char *full_exe_path, int std_in, int std_out, bool ext_clipboard, bool norgb, const char *nodetect, bool far2l_tty, unsigned int esc_expiration, int notify_pipe, int argc, char **argv, int(*AppMain)(int argc, char **argv), int *result) { - TTYBackend vtb(full_exe_path, std_in, std_out, ext_clipboard, nodetect, far2l_tty, esc_expiration, notify_pipe, result); + TTYBackend vtb(full_exe_path, std_in, std_out, ext_clipboard, norgb, nodetect, far2l_tty, esc_expiration, notify_pipe, result); if (!vtb.Startup()) { return false; diff --git a/WinPort/src/Backend/TTY/TTYBackend.h b/WinPort/src/Backend/TTY/TTYBackend.h index 2b520480..25720253 100644 --- a/WinPort/src/Backend/TTY/TTYBackend.h +++ b/WinPort/src/Backend/TTY/TTYBackend.h @@ -19,6 +19,7 @@ class TTYBackend : IConsoleOutputBackend, ITTYInputSpecialSequenceHandler, IFar2 const char *_full_exe_path; int _stdin = 0, _stdout = 1; bool _ext_clipboard; + bool _norgb; const char *_nodetect = ""; bool _far2l_tty = false; bool _osc52clip_set = false; @@ -144,7 +145,7 @@ protected: DWORD QueryControlKeys(); public: - TTYBackend(const char *full_exe_path, int std_in, int std_out, bool ext_clipboard, const char *nodetect, bool far2l_tty, unsigned int esc_expiration, int notify_pipe, int *result); + TTYBackend(const char *full_exe_path, int std_in, int std_out, bool ext_clipboard, bool norgb, const char *nodetect, bool far2l_tty, unsigned int esc_expiration, int notify_pipe, int *result); ~TTYBackend(); void KickAss(bool flush_input_queue = false); bool Startup(); diff --git a/WinPort/src/Backend/TTY/TTYOutput.cpp b/WinPort/src/Backend/TTY/TTYOutput.cpp index 8d8a0907..a81ea3aa 100644 --- a/WinPort/src/Backend/TTY/TTYOutput.cpp +++ b/WinPort/src/Backend/TTY/TTYOutput.cpp @@ -66,6 +66,9 @@ template void TTYOutput::WriteUpdatedAttributes(DWORD64 attr, bool is_space) { + if (_norgb) { + attr&= ~(FOREGROUND_TRUECOLOR | BACKGROUND_TRUECOLOR); + } const DWORD64 xa = _prev_attr_valid ? attr ^ _prev_attr : (DWORD64)-1; if (xa == 0) { return; @@ -137,7 +140,10 @@ void TTYOutput::WriteUpdatedAttributes(DWORD64 attr, bool is_space) _tmp_attrs+= (attr & COMMON_LVB_REVERSE_VIDEO) ? "7;" : "27;"; } - assert(!_tmp_attrs.empty() && _tmp_attrs.back() == ';'); + if (_tmp_attrs.back() != ';') { + return; + } + _tmp_attrs.back() = 'm'; _prev_attr = attr; _prev_attr_valid = true; @@ -147,9 +153,9 @@ void TTYOutput::WriteUpdatedAttributes(DWORD64 attr, bool is_space) /////////////////////// -TTYOutput::TTYOutput(int out, bool far2l_tty) +TTYOutput::TTYOutput(int out, bool far2l_tty, bool norgb) : - _out(out), _far2l_tty(far2l_tty), _kernel_tty(false) + _out(out), _far2l_tty(far2l_tty), _norgb(norgb), _kernel_tty(false) { #if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) unsigned long int leds = 0; diff --git a/WinPort/src/Backend/TTY/TTYOutput.h b/WinPort/src/Backend/TTY/TTYOutput.h index 9a9bb1b4..f0aa4686 100644 --- a/WinPort/src/Backend/TTY/TTYOutput.h +++ b/WinPort/src/Backend/TTY/TTYOutput.h @@ -38,7 +38,7 @@ class TTYOutput } _true_colors; int _out; - bool _far2l_tty, _kernel_tty; + bool _far2l_tty, _norgb, _kernel_tty; TTYBasePalette _palette; bool _prev_attr_valid{false}; DWORD64 _prev_attr{}; @@ -54,7 +54,7 @@ class TTYOutput void WriteUpdatedAttributes(DWORD64 new_attr, bool is_space); public: - TTYOutput(int out, bool far2l_tty); + TTYOutput(int out, bool far2l_tty, bool norgb); ~TTYOutput(); void Flush(); diff --git a/WinPort/src/Backend/WX/wxMain.cpp b/WinPort/src/Backend/WX/wxMain.cpp index 00d6b2b4..1c11fc93 100644 --- a/WinPort/src/Backend/WX/wxMain.cpp +++ b/WinPort/src/Backend/WX/wxMain.cpp @@ -102,6 +102,7 @@ extern "C" __attribute__ ((visibility("default"))) bool WinPortMainBackend(WinPo return false; } + g_wx_norgb = a->norgb; g_winport_con_out = a->winport_con_out; g_winport_con_in = a->winport_con_in; @@ -619,7 +620,7 @@ bool WinPortPanel::OnConsoleSetFKeyTitles(const char **titles) BYTE WinPortPanel::OnConsoleGetColorPalette() { - return 24; + return g_wx_norgb ? 4 : 24; } void WinPortPanel::OnTouchbarKey(bool alternate, int index) diff --git a/WinPort/src/Backend/WX/wxWinTranslations.cpp b/WinPort/src/Backend/WX/wxWinTranslations.cpp index 5da08b68..0674d83c 100644 --- a/WinPort/src/Backend/WX/wxWinTranslations.cpp +++ b/WinPort/src/Backend/WX/wxWinTranslations.cpp @@ -53,7 +53,7 @@ extern bool g_broadway; extern bool g_remote; WinPortPalette g_wx_palette; - +bool g_wx_norgb = false; static const WinPortRGB &InternalConsoleForeground2RGB(USHORT attributes) { @@ -67,12 +67,14 @@ static const WinPortRGB &InternalConsoleBackground2RGB(USHORT attributes) WinPortRGB ConsoleForeground2RGB(DWORD64 attributes) { - if ( (attributes & (FOREGROUND_TRUECOLOR | COMMON_LVB_REVERSE_VIDEO)) == FOREGROUND_TRUECOLOR) { - return GET_RGB_FORE(attributes); - } + if (!g_wx_norgb) { + if ( (attributes & (FOREGROUND_TRUECOLOR | COMMON_LVB_REVERSE_VIDEO)) == FOREGROUND_TRUECOLOR) { + return GET_RGB_FORE(attributes); + } - if ( (attributes & (BACKGROUND_TRUECOLOR | COMMON_LVB_REVERSE_VIDEO)) == (BACKGROUND_TRUECOLOR | COMMON_LVB_REVERSE_VIDEO)) { - return GET_RGB_BACK(attributes); + if ( (attributes & (BACKGROUND_TRUECOLOR | COMMON_LVB_REVERSE_VIDEO)) == (BACKGROUND_TRUECOLOR | COMMON_LVB_REVERSE_VIDEO)) { + return GET_RGB_BACK(attributes); + } } return (attributes & COMMON_LVB_REVERSE_VIDEO) @@ -82,12 +84,14 @@ WinPortRGB ConsoleForeground2RGB(DWORD64 attributes) WinPortRGB ConsoleBackground2RGB(DWORD64 attributes) { - if ( (attributes & (BACKGROUND_TRUECOLOR | COMMON_LVB_REVERSE_VIDEO)) == BACKGROUND_TRUECOLOR) { - return GET_RGB_BACK(attributes); - } + if (!g_wx_norgb) { + if ( (attributes & (BACKGROUND_TRUECOLOR | COMMON_LVB_REVERSE_VIDEO)) == BACKGROUND_TRUECOLOR) { + return GET_RGB_BACK(attributes); + } - if ( (attributes & (FOREGROUND_TRUECOLOR | COMMON_LVB_REVERSE_VIDEO)) == (FOREGROUND_TRUECOLOR | COMMON_LVB_REVERSE_VIDEO)) { - return GET_RGB_FORE(attributes); + if ( (attributes & (FOREGROUND_TRUECOLOR | COMMON_LVB_REVERSE_VIDEO)) == (FOREGROUND_TRUECOLOR | COMMON_LVB_REVERSE_VIDEO)) { + return GET_RGB_FORE(attributes); + } } return (attributes & COMMON_LVB_REVERSE_VIDEO) diff --git a/WinPort/src/Backend/WX/wxWinTranslations.h b/WinPort/src/Backend/WX/wxWinTranslations.h index 37878d82..a65bf41c 100644 --- a/WinPort/src/Backend/WX/wxWinTranslations.h +++ b/WinPort/src/Backend/WX/wxWinTranslations.h @@ -47,6 +47,7 @@ struct wx2INPUT_RECORD : INPUT_RECORD }; extern WinPortPalette g_wx_palette; +extern bool g_wx_norgb; WinPortRGB ConsoleForeground2RGB(DWORD64 attributes); WinPortRGB ConsoleBackground2RGB(DWORD64 attributes); diff --git a/WinPort/src/Backend/WinPortMain.cpp b/WinPort/src/Backend/WinPortMain.cpp index 89f76562..acba7e81 100644 --- a/WinPort/src/Backend/WinPortMain.cpp +++ b/WinPort/src/Backend/WinPortMain.cpp @@ -39,7 +39,10 @@ IConsoleOutput *g_winport_con_out = nullptr; IConsoleInput *g_winport_con_in = nullptr; const wchar_t *g_winport_backend = L""; -bool WinPortMainTTY(const char *full_exe_path, int std_in, int std_out, bool ext_clipboard, const char *nodetect, bool far2l_tty, unsigned int esc_expiration, int notify_pipe, int argc, char **argv, int(*AppMain)(int argc, char **argv), int *result); +bool WinPortMainTTY(const char *full_exe_path, int std_in, int std_out, + bool ext_clipboard, bool norgb, const char *nodetect, bool far2l_tty, + unsigned int esc_expiration, int notify_pipe, int argc, char **argv, + int(*AppMain)(int argc, char **argv), int *result); extern "C" void WinPortInitRegistry(); @@ -227,6 +230,7 @@ extern "C" void WinPortHelp() printf("\t--tty - force using TTY backend only (disable GUI/TTY autodetection)\n"); printf("\t--notty - don't fallback to TTY backend if GUI backend failed\n"); printf("\t--nodetect or --nodetect=[f|][x|xi] - don't detect if TTY backend supports FAR2L or X11/Xi extensions\n"); + printf("\t--norgb - don't use true (24-bit) colors\n"); printf("\t--mortal - terminate instead of going to background on getting SIGHUP (default if in Linux TTY)\n"); printf("\t--immortal - go to background instead of terminating on getting SIGHUP (default if not in Linux TTY)\n"); printf("\t--ee or --ee=N - ESC expiration in msec (100 if unspecified) to avoid need for double ESC presses (valid only in TTY mode without FAR2L extensions)\n"); @@ -239,7 +243,7 @@ extern "C" void WinPortHelp() struct ArgOptions { const char *nodetect = ""; - bool tty = false, far2l_tty = false, notty = false; + bool tty = false, far2l_tty = false, notty = false, norgb = false; bool mortal = false; std::string ext_clipboard; unsigned int esc_expiration = 0; @@ -261,6 +265,9 @@ struct ArgOptions } else if (strcmp(a, "--tty") == 0) { tty = true; + } else if (strcmp(a, "--norgb") == 0) { + norgb = true; + } else if (strcmp(a, "--nodetect") == 0) { nodetect = "fx"; @@ -411,7 +418,7 @@ extern "C" int WinPortMain(const char *full_exe_path, int argc, char **argv, int SudoAskpassImpl askass_impl; SudoAskpassServer askpass_srv(&askass_impl); WinPortMainBackendArg a{FAR2L_BACKEND_ABI_VERSION, - argc, argv, AppMain, &result, g_winport_con_out, g_winport_con_in, !arg_opts.ext_clipboard.empty()}; + argc, argv, AppMain, &result, g_winport_con_out, g_winport_con_in, !arg_opts.ext_clipboard.empty(), arg_opts.norgb}; if (!WinPortMainBackend_p(&a) ) { fprintf(stderr, "Cannot use GUI backend\n"); arg_opts.tty = !arg_opts.notty; @@ -434,7 +441,8 @@ extern "C" int WinPortMain(const char *full_exe_path, int argc, char **argv, int if (arg_opts.mortal) { SudoAskpassImpl askass_impl; SudoAskpassServer askpass_srv(&askass_impl); - if (!WinPortMainTTY(full_exe_path, std_in, std_out, !arg_opts.ext_clipboard.empty(), arg_opts.nodetect, + if (!WinPortMainTTY(full_exe_path, std_in, std_out, + !arg_opts.ext_clipboard.empty(), arg_opts.norgb, arg_opts.nodetect, arg_opts.far2l_tty, arg_opts.esc_expiration, -1, argc, argv, AppMain, &result)) { fprintf(stderr, "Cannot use TTY backend\n"); } @@ -459,7 +467,8 @@ extern "C" int WinPortMain(const char *full_exe_path, int argc, char **argv, int setsid(); SudoAskpassImpl askass_impl; SudoAskpassServer askpass_srv(&askass_impl); - if (!WinPortMainTTY(full_exe_path, std_in, std_out, !arg_opts.ext_clipboard.empty(), arg_opts.nodetect, + if (!WinPortMainTTY(full_exe_path, std_in, std_out, + !arg_opts.ext_clipboard.empty(), arg_opts.norgb, arg_opts.nodetect, arg_opts.far2l_tty, arg_opts.esc_expiration, new_notify_pipe[1], argc, argv, AppMain, &result)) { fprintf(stderr, "Cannot use TTY backend\n"); } diff --git a/man/far2l.1 b/man/far2l.1 index df26d7a3..3f2fd00d 100644 --- a/man/far2l.1 +++ b/man/far2l.1 @@ -69,6 +69,9 @@ force using TTY backend only (disable GUI/TTY autodetection) \fB\-\-notty\fP don't fallback to TTY backend if GUI backend failed .TP +\fB\-\-norgb\fP +don't use true (24-bit) colors +.TP \fB\-\-nodetect\fP, \fB\-\-nodetect\fP=[f|][x|xi] don't detect if TTY backend supports FAR2L or X11/Xi extensions .TP diff --git a/man/ru/far2l.1 b/man/ru/far2l.1 index 6f689e7c..ada786b7 100644 --- a/man/ru/far2l.1 +++ b/man/ru/far2l.1 @@ -66,6 +66,9 @@ \fB\-\-notty\fP Не использовать терминальный режиме при невозможности использовать GUI режим. .TP +\fB\-\-norgb\fP +Не использовать полноцветную (24-битную) палитру цветов +.TP \fB\-\-nodetect\fP, \fB\-\-nodetect\fP=[f|][x|xi] По умолчанию far2l пытается на запуске определить, не работает ли он в терминале другого far2l. В этом случае far2l автоматически использует режим TTY с расширениями терминала far2l. В случае отсутствия таких расширений терминала, far2l проверяет наличие доступа к X11 сессии или Xi расширении и использует их для улучшения возможностей интерфейса, если был собрать с опцией TTYX. Данный ключ выключает всю перечисленную функциональность, предотвращая авто определение наличия расширений и разрешает к использованию лишь базовые возможности обычного терминала в TTY режиме. -- 2.11.4.GIT