From 22190faeace11d58b452d0c5dea8b7c613c08705 Mon Sep 17 00:00:00 2001 From: Sebastian Reichel Date: Fri, 5 Oct 2012 22:26:47 +0200 Subject: play audio on shutdown (GH-13) --- src/audio.vala | 14 ++++++++++++++ src/main.vala | 32 ++++++++++++++++++++++++++------ src/web.vala | 2 +- 3 files changed, 41 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/audio.vala b/src/audio.vala index 8e36c70..e30f3b1 100644 --- a/src/audio.vala +++ b/src/audio.vala @@ -16,11 +16,25 @@ public class AudioPlayer { private dynamic Gst.Element p; + public signal void end_of_stream(); + + private bool bus_callback(Gst.Bus bus, Gst.Message message) { + switch (message.type) { + case Gst.MessageType.EOS: + end_of_stream(); + break; + } + + return true; + } + public AudioPlayer() { p = Gst.ElementFactory.make("playbin", "play"); + p.get_bus().add_watch(bus_callback); } public void play(string file) { + p.set_state(Gst.State.NULL); p.uri = "file://"+Environment.get_current_dir()+"/sounds/"+file; p.set_state(Gst.State.PLAYING); } diff --git a/src/main.vala b/src/main.vala index eccd171..eff3a9d 100644 --- a/src/main.vala +++ b/src/main.vala @@ -17,6 +17,7 @@ public Device dev; public Database db; public AudioPlayer audio; public CSVMemberFile csvimport; +public MainLoop loop; public static int main(string[] args) { Gst.init(ref args); @@ -26,9 +27,15 @@ public static int main(string[] args) { return 1; } - dev = new Device(args[1], 9600, 8, 1); - db = new Database("shop.db"); + /* handle unix signals */ + Unix.signal_add(Posix.SIGTERM, handle_signals); + Unix.signal_add(Posix.SIGINT, handle_signals); + + + dev = new Device(args[1], 9600, 8, 1); + db = new Database("shop.db"); audio = new AudioPlayer(); + loop = new MainLoop(); dev.received_barcode.connect((data) => { if(interpret(data)) @@ -42,11 +49,19 @@ public static int main(string[] args) { new WebServer(); /* run mainloop */ - new MainLoop().run(); + loop.run(); + + write_to_log("Stopping Shop System"); + audio.play("system/shutdown.ogg"); - /* call destructors */ - dev = null; - db = null; + /* we need to run the mainloop to play audio */ + audio.end_of_stream.connect(() => { loop.quit(); }); + loop.run(); + + /* explicitly call destructors */ + dev = null; + db = null; + audio = null; return 0; } @@ -117,3 +132,8 @@ public static bool interpret(string data) { } } } + +bool handle_signals() { + loop.quit(); + return false; +} diff --git a/src/web.vala b/src/web.vala index 2e2e13f..9a77369 100644 --- a/src/web.vala +++ b/src/web.vala @@ -728,6 +728,6 @@ public class WebServer { srv.add_handler("/users", handler_users); srv.add_handler("/users/import", handler_user_import); - srv.run(); + srv.run_async(); } } -- cgit v1.2.3