summaryrefslogtreecommitdiff
path: root/Applications/Piano/main.cpp
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-07-14 10:22:28 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-07-14 10:22:28 +0200
commit1ecb7462b74ff44af618b49ea71615108bbba312 (patch)
tree68cb617f73cef43594cf1936f9f36d9431ccbabd /Applications/Piano/main.cpp
parent17ee548bcd8d9111addcc291457ad316e620d5e6 (diff)
downloadserenity-1ecb7462b74ff44af618b49ea71615108bbba312.zip
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().
Diffstat (limited to 'Applications/Piano/main.cpp')
-rw-r--r--Applications/Piano/main.cpp26
1 files changed, 7 insertions, 19 deletions
diff --git a/Applications/Piano/main.cpp b/Applications/Piano/main.cpp
index 0e25bb423e..029cfbb2b4 100644
--- a/Applications/Piano/main.cpp
+++ b/Applications/Piano/main.cpp
@@ -1,40 +1,27 @@
#include "Music.h"
#include "PianoWidget.h"
-#include <LibCore/CFile.h>
-#include <LibCore/CNotifier.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>
-static int s_pipefds[2];
-
int main(int argc, char** argv)
{
AClientConnection audio_connection;
GApplication app(argc, argv);
- pipe(s_pipefds);
-
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");
- CNotifier notifier(s_pipefds[0], CNotifier::Read);
- notifier.on_ready_to_read = [&] {
- char buffer[32];
- read(s_pipefds[1], buffer, sizeof(buffer));
- piano_widget->update();
- };
-
- create_thread([](void* context) -> int {
+ CThread sound_thread([](void* context) -> int {
auto* piano_widget = (PianoWidget*)context;
CFile audio("/dev/audio");
@@ -47,10 +34,11 @@ int main(int argc, char** argv)
u8 buffer[4096];
piano_widget->fill_audio_buffer(buffer, sizeof(buffer));
audio.write(buffer, sizeof(buffer));
- char ch = '!';
- write(s_pipefds[0], &ch, 1);
+ GEventLoop::current().post_event(*piano_widget, make<CCustomEvent>(0));
+ GEventLoop::current().wake();
}
- }, piano_widget);
+ },
+ piano_widget);
return app.exec();
}