1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:47:44 +00:00

Piano: Use CEventLoop::wake() to trigger repaint from sound thread.

In order to repaint the GUI after the sound thread has produced some sweet
new waves, we post a CCustomEvent to the main thread's event loop and then
wake up that event loop via CEventLoop::wake().
This commit is contained in:
Andreas Kling 2019-07-14 10:22:28 +02:00
parent 17ee548bcd
commit 1ecb7462b7
3 changed files with 16 additions and 19 deletions

View file

@ -354,3 +354,11 @@ void PianoWidget::render_knobs(GPainter& painter)
painter.draw_rect(wave_knob_rect, Color(r, g, b)); painter.draw_rect(wave_knob_rect, Color(r, g, b));
painter.draw_text(wave_knob_rect, wave_name, TextAlignment::Center, Color(r, g, b)); painter.draw_text(wave_knob_rect, wave_name, TextAlignment::Center, Color(r, g, b));
} }
void PianoWidget::event(CEvent& event)
{
if (event.type() == CEvent::Custom) {
update();
}
GWidget::event(event);
}

View file

@ -16,6 +16,7 @@ private:
virtual void paint_event(GPaintEvent&) override; virtual void paint_event(GPaintEvent&) override;
virtual void keydown_event(GKeyEvent&) override; virtual void keydown_event(GKeyEvent&) override;
virtual void keyup_event(GKeyEvent&) override; virtual void keyup_event(GKeyEvent&) override;
virtual void event(CEvent&) override;
double w_sine(size_t); double w_sine(size_t);
double w_saw(size_t); double w_saw(size_t);

View file

@ -1,40 +1,27 @@
#include "Music.h" #include "Music.h"
#include "PianoWidget.h" #include "PianoWidget.h"
#include <LibCore/CFile.h>
#include <LibCore/CNotifier.h>
#include <LibAudio/AClientConnection.h> #include <LibAudio/AClientConnection.h>
#include <LibCore/CFile.h>
#include <LibCore/CThread.h>
#include <LibGUI/GApplication.h> #include <LibGUI/GApplication.h>
#include <LibGUI/GEventLoop.h> #include <LibGUI/GEventLoop.h>
#include <LibGUI/GWindow.h> #include <LibGUI/GWindow.h>
static int s_pipefds[2];
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
AClientConnection audio_connection; AClientConnection audio_connection;
GApplication app(argc, argv); GApplication app(argc, argv);
pipe(s_pipefds);
auto* window = new GWindow; auto* window = new GWindow;
window->set_title("Piano"); window->set_title("Piano");
window->set_rect(100, 100, 512, 512); window->set_rect(100, 100, 512, 512);
auto* piano_widget = new PianoWidget; auto* piano_widget = new PianoWidget;
window->set_main_widget(piano_widget); window->set_main_widget(piano_widget);
window->show(); window->show();
window->set_icon_path("/res/icons/16x16/app-piano.png"); window->set_icon_path("/res/icons/16x16/app-piano.png");
CNotifier notifier(s_pipefds[0], CNotifier::Read); CThread sound_thread([](void* context) -> int {
notifier.on_ready_to_read = [&] {
char buffer[32];
read(s_pipefds[1], buffer, sizeof(buffer));
piano_widget->update();
};
create_thread([](void* context) -> int {
auto* piano_widget = (PianoWidget*)context; auto* piano_widget = (PianoWidget*)context;
CFile audio("/dev/audio"); CFile audio("/dev/audio");
@ -47,10 +34,11 @@ int main(int argc, char** argv)
u8 buffer[4096]; u8 buffer[4096];
piano_widget->fill_audio_buffer(buffer, sizeof(buffer)); piano_widget->fill_audio_buffer(buffer, sizeof(buffer));
audio.write(buffer, sizeof(buffer)); audio.write(buffer, sizeof(buffer));
char ch = '!'; GEventLoop::current().post_event(*piano_widget, make<CCustomEvent>(0));
write(s_pipefds[0], &ch, 1); GEventLoop::current().wake();
} }
}, piano_widget); },
piano_widget);
return app.exec(); return app.exec();
} }