Skip to content

Commit

Permalink
screen: defer Paint() call using an IdleEvent
Browse files Browse the repository at this point in the history
This way, we avoid the assertion failure in
FileListPage::PaintListItem(), caused by FileBrowserPage::Reload()
resetting the FileList but not updating the ListWindow just yet; but
the mpdclient::GetConnection() could return an idle event which would
then trigger a synchronous repaint.

We avoid lots of undefined behavior by deferring all Paint() calls
with an IdleEvent, and maybe we save a few redundant Paint() calls.

Fixes https://bugs.debian.org/981152
Closes #87
  • Loading branch information
MaxKellermann committed Jan 27, 2021
1 parent a7adc05 commit e1c9840
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ncmpc 0.43 - not yet released
* show "conductor" and "work" on song page
* playlist editor (work in progress)
* file page: handle mouse double clicks
* fix crash bug
* fix build failure on macOS
* add azlyrics plugin

Expand Down
2 changes: 1 addition & 1 deletion src/screen.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ ScreenManager::Update(struct mpdclient &c, const DelayedSeek &seek) noexcept
/* update the main window */
current_page->second->Update(c);

Paint();
SchedulePaint();
}

void
Expand Down
16 changes: 13 additions & 3 deletions src/screen.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "StatusBar.hxx"
#include "History.hxx"
#include "Point.hxx"
#include "event/IdleEvent.hxx"
#include "util/Compiler.h"

#include <curses.h>
Expand All @@ -44,7 +45,11 @@ class DelayedSeek;
class EventLoop;

class ScreenManager {
EventLoop &event_loop;
/**
* This event defers Paint() calls until after the EventLoop
* has handled all other events.
*/
IdleEvent paint_event;

struct Layout {
Size size;
Expand Down Expand Up @@ -105,7 +110,7 @@ public:
~ScreenManager() noexcept;

auto &GetEventLoop() const noexcept {
return event_loop;
return paint_event.GetEventLoop();
}

void Init(struct mpdclient *c) noexcept;
Expand All @@ -132,7 +137,6 @@ public:
void Swap(struct mpdclient &c, const struct mpd_song *song) noexcept;

void PaintTopWindow() noexcept;
void Paint() noexcept;

void Update(struct mpdclient &c, const DelayedSeek &seek) noexcept;
void OnCommand(struct mpdclient &c, DelayedSeek &seek, Command cmd);
Expand All @@ -144,6 +148,12 @@ public:

private:
void NextMode(struct mpdclient &c, int offset) noexcept;

void SchedulePaint() noexcept {
paint_event.Schedule();
}

void Paint() noexcept;
};

#endif
6 changes: 3 additions & 3 deletions src/screen_init.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
static const unsigned SCREEN_MIN_COLS = 14;
static const unsigned SCREEN_MIN_ROWS = 5;

ScreenManager::ScreenManager(EventLoop &_event_loop) noexcept
:event_loop(_event_loop),
ScreenManager::ScreenManager(EventLoop &event_loop) noexcept
:paint_event(event_loop, BIND_THIS_METHOD(Paint)),
layout({std::max<unsigned>(COLS, SCREEN_MIN_COLS),
std::max<unsigned>(LINES, SCREEN_MIN_ROWS)}),
title_bar({layout.title_x, layout.title_y}, layout.size.width),
Expand Down Expand Up @@ -104,7 +104,7 @@ ScreenManager::OnResize() noexcept
curs_set(1);
curs_set(0);

Paint();
SchedulePaint();
}

void
Expand Down

0 comments on commit e1c9840

Please sign in to comment.