Skip to content

Commit

Permalink
gh-903: Crash on exit
Browse files Browse the repository at this point in the history
  • Loading branch information
alabuzhev committed Feb 5, 2025
1 parent f0be4e3 commit c499ced
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 9 deletions.
5 changes: 5 additions & 0 deletions far/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
--------------------------------------------------------------------------------
drkns 2025-02-05 01:37:50+00:00 - build 6431

1. gh-903: Crash on exit.

--------------------------------------------------------------------------------
drkns 2025-02-04 20:18:36+00:00 - build 6430

Expand Down
18 changes: 12 additions & 6 deletions far/colormix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Common:
#include "common/enum_tokens.hpp"
#include "common/from_string.hpp"
#include "common/function_ref.hpp"
#include "common/view/zip.hpp"

// External:
Expand Down Expand Up @@ -566,7 +567,13 @@ namespace colors
return Result;
}

static index_color_256 color_to_palette_index(FarColor Color, std::optional<colors_mapping> const& Last, std::span<COLORREF const> const Palette, std::unordered_map<COLORREF, uint8_t>& Map)
enum class palette_type
{
nt,
vt,
};

static index_color_256 color_to_palette_index(FarColor Color, std::optional<colors_mapping> const& Last, palette_type const PaletteType, function_ref<std::span<COLORREF const>()> const PaletteGetter, std::unordered_map<COLORREF, uint8_t>& Map)
{
Color = resolve_defaults(Color);

Expand All @@ -583,9 +590,8 @@ static index_color_256 color_to_palette_index(FarColor Color, std::optional<colo
if (From.IsIndex)
{
const auto CurrentIndex = index_value(From.Value);
const auto IsNtPalette = Palette.size() == index::nt_size;

if ((IsNtPalette && CurrentIndex <= index::nt_last) || (!IsNtPalette && CurrentIndex > index::nt_last))
if ((PaletteType == palette_type::nt && CurrentIndex <= index::nt_last) || (PaletteType == palette_type::vt && CurrentIndex > index::nt_last))
return CurrentIndex;

CurrentColorValue = ConsoleIndexToTrueColor(CurrentIndex);
Expand All @@ -595,7 +601,7 @@ static index_color_256 color_to_palette_index(FarColor Color, std::optional<colo
CurrentColorValue = color_value(From.Value);
}

return get_closest_palette_index(CurrentColorValue, Palette, Map);
return get_closest_palette_index(CurrentColorValue, PaletteGetter(), Map);
};

return
Expand Down Expand Up @@ -630,7 +636,7 @@ WORD FarColorToConsoleColor(const FarColor& Color)
}
else
{
Result = color_to_palette_index(Color, Last, { ColorsCache.palette().data(), index::nt_size }, ColorsCache.closest_index_16());
Result = color_to_palette_index(Color, Last, palette_type::nt, []{ return std::span{ ColorsCache.palette().data(), index::nt_size }; }, ColorsCache.closest_index_16());

if (
Result.ForegroundIndex == Result.BackgroundIndex &&
Expand Down Expand Up @@ -663,7 +669,7 @@ index_color_256 FarColorToConsole256Color(const FarColor& Color)
}
else
{
Result = color_to_palette_index(Color, Last, ColorsCache.palette(), ColorsCache.closest_index_256());
Result = color_to_palette_index(Color, Last, palette_type::vt, []{ return std::span{ ColorsCache.palette() }; }, ColorsCache.closest_index_256());
Last.emplace(Color, Result);
// This conversion is only used to select the closest color in the picker, no need to care about readability
}
Expand Down
5 changes: 5 additions & 0 deletions far/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,13 @@ namespace console_detail
// Fortunately, it seems that user input is queued before and/or after the responses,
// but does not interlace with them.

// We also need to disable line input, otherwise we will hang.
// It's usually disabled anyway, but just in case if we get here too early or too late.
// Are you not entertained?

const auto CurrentConsoleMode = ::console.UpdateMode(::console.GetInputHandle(), 0, ENABLE_LINE_INPUT);
SCOPE_EXIT{ if (CurrentConsoleMode) ::console.SetMode(::console.GetInputHandle(), *CurrentConsoleMode);};

const auto Dummy = CSI L"0c"sv;

if (!::console.Write(concat(Dummy, Command, Dummy)))
Expand Down
5 changes: 5 additions & 0 deletions far/interf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ static BOOL control_handler(DWORD CtrlType)
return TRUE;

case CTRL_CLOSE_EVENT:
if (!Global)
return FALSE;

Global->CloseFAR = true;
Global->AllowCancelExit = false;
main_loop_process_messages();
Expand Down Expand Up @@ -402,6 +405,8 @@ void CloseConsole()
MoveRealCursor(0, ScrY);
console.SetCursorInfo(InitialCursorInfo);

SetRealColor(colors::default_color());

if (InitialConsoleMode)
{
ChangeConsoleMode(console.GetInputHandle(), InitialConsoleMode->Input);
Expand Down
2 changes: 0 additions & 2 deletions far/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -805,8 +805,6 @@ static int mainImpl(std::span<const wchar_t* const> const Args)
if (FarColor InitAttributes; console.GetTextAttributes(InitAttributes))
colors::store_default_color(InitAttributes);

SCOPE_EXIT{ console.SetTextAttributes(colors::default_color()); };

SCOPED_ACTION(global);

std::optional<elevation::suppress> NoElevationDuringBoot(std::in_place);
Expand Down
2 changes: 1 addition & 1 deletion far/vbuild.m4
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6430
6431

0 comments on commit c499ced

Please sign in to comment.