1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 12:38:12 +00:00
serenity/Applications/Piano/main.cpp
Andreas Kling 1ecb7462b7 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().
2019-07-14 10:22:28 +02:00

44 lines
1.2 KiB
C++

#include "Music.h"
#include "PianoWidget.h"
#include <LibAudio/AClientConnection.h>
#include <LibCore/CFile.h>
#include <LibCore/CThread.h>
#include <LibGUI/GApplication.h>
#include <LibGUI/GEventLoop.h>
#include <LibGUI/GWindow.h>
int main(int argc, char** argv)
{
AClientConnection audio_connection;
GApplication app(argc, argv);
auto* window = new GWindow;
window->set_title("Piano");
window->set_rect(100, 100, 512, 512);
auto* piano_widget = new PianoWidget;
window->set_main_widget(piano_widget);
window->show();
window->set_icon_path("/res/icons/16x16/app-piano.png");
CThread sound_thread([](void* context) -> int {
auto* piano_widget = (PianoWidget*)context;
CFile audio("/dev/audio");
if (!audio.open(CIODevice::WriteOnly)) {
dbgprintf("Can't open audio device: %s", audio.error_string());
return 1;
}
for (;;) {
u8 buffer[4096];
piano_widget->fill_audio_buffer(buffer, sizeof(buffer));
audio.write(buffer, sizeof(buffer));
GEventLoop::current().post_event(*piano_widget, make<CCustomEvent>(0));
GEventLoop::current().wake();
}
},
piano_widget);
return app.exec();
}