ocarina: Listen for text written to a named pipe
If the text is "play", "pause", or "next" then I call the appropriate libsaria function. I eventually plan on adding more commands so bash can act as a generic remote control, but for now this is a good start! To use: `echo play > ~/.config/saria[-debug]/saria.pipe` after creating the fifo (see scripts/makepipe). Signed-off-by: Bryan Schumaker <bjschuma@gmail.com>
This commit is contained in:
parent
82b9974448
commit
41467524db
|
@ -7,6 +7,7 @@ namespace ocarina
|
|||
void quit();
|
||||
void idle_add();
|
||||
string full_path(string);
|
||||
void init_pipe();
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include <ocarina/callback.h>
|
||||
#include <ocarina/gtk.h>
|
||||
#include <ocarina/body.h>
|
||||
#include <ocarina/ocarina.h>
|
||||
#include <ocarina/settings.h>
|
||||
#include <ocarina/window.h>
|
||||
#include <ocarina/library.h>
|
||||
|
@ -81,6 +82,7 @@ int main(int argc, char **argv)
|
|||
|
||||
gtk_init(&argc, &argv);
|
||||
ocarina::init(argc, argv);
|
||||
ocarina::init_pipe();
|
||||
|
||||
if (argc > 1)
|
||||
libsaria::audio::load(argv[1]);
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
// Copyright (c) Bryan Schumaker 2012.
|
||||
#include <libsaria/path.h>
|
||||
#include <libsaria/print.h>
|
||||
#include <libsaria/audio.h>
|
||||
#include <libsaria/controls.h>
|
||||
#include <ocarina/ocarina.h>
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
static GIOChannel *pipe_in;
|
||||
|
||||
void perform_action(string action)
|
||||
{
|
||||
if (action == "play")
|
||||
libsaria::audio::play();
|
||||
else if (action == "pause")
|
||||
libsaria::audio::pause();
|
||||
else if (action == "next")
|
||||
libsaria::next();
|
||||
}
|
||||
|
||||
string pipe_read_text(GIOChannel *source)
|
||||
{
|
||||
gchar *str = NULL;
|
||||
GError *error = NULL;
|
||||
gsize size, end;
|
||||
string text;
|
||||
|
||||
g_io_channel_read_line(source, &str, &size, &end, &error);
|
||||
text = str;
|
||||
|
||||
/* g_io_channel_read_line allocates memory */
|
||||
g_free(str);
|
||||
return text.substr(0, size-1);
|
||||
}
|
||||
|
||||
gboolean pipe_read(GIOChannel *source, GIOCondition condition, gpointer data)
|
||||
{
|
||||
string action = pipe_read_text(source);
|
||||
perform_action(action);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
namespace ocarina
|
||||
{
|
||||
void init_pipe()
|
||||
{
|
||||
GError *error = NULL;
|
||||
string pipe = get_pipe_file();
|
||||
if (pipe == "")
|
||||
return;
|
||||
|
||||
pipe_in = g_io_channel_new_file(pipe.c_str(), "r+", &error);
|
||||
if (!pipe_in)
|
||||
return;
|
||||
g_io_add_watch(pipe_in, G_IO_IN, pipe_read, NULL);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue