ocarina/gui/gst.cpp

176 lines
4.7 KiB
C++
Raw Normal View History

/*
* Copyright 2014 (c) Anna Schumaker.
*
* The gst_init() function parses command line options passed to Ocarina
* through argv. Use the command `gst-inspect-1.0 --help-gst` to find
* what options are supported.
*/
extern "C" {
#include <core/audio.h>
#include <core/playlist.h>
#include <core/string.h>
#include <gui/builder.h>
}
#include <gui/ocarina.h>
#include <gst/gst.h>
static Gtk::Button *o_next;
static Gtk::Button *o_pause;
static Gtk::Button *o_play;
static Gtk::Button *o_prev;
static Gtk::Scale *o_seek;
static Gtk::Button *o_stop;
static Gtk::SpinButton *o_count;
static Gtk::CheckButton *o_enabled;
static Gtk::Label *o_album;
static Gtk::Label *o_artist;
static Gtk::Label *o_duration;
static Gtk::Label *o_position;
static Gtk::Label *o_title;
static Glib::RefPtr<Gtk::Adjustment> o_progress;
static void set_markup(Gtk::Label *label, const std::string &size,
const std::string &text)
{
label->set_markup("<span size='" + size + "'>" +
Glib::Markup::escape_text(text) + "</span>");
}
static void on_load(struct track *track)
{
Gtk::ToggleButton *toggle;
gchar *str = g_strdup_printf("From: %s", track->tr_album->al_name);
set_markup(o_album, "x-large", str);
g_free(str);
str = g_strdup_printf("By: %s", track->tr_artist->ar_name);
set_markup(o_artist, "x-large", str);
g_free(str);
set_markup(o_title, "xx-large", track->tr_title);
str = string_sec2str(track->tr_length);
o_duration->set_text(str);
g_free(str);
toggle = Glib :: wrap(GTK_TOGGLE_BUTTON(gui_builder_widget("o_ban")), false);
toggle->set_active(playlist_has(PL_HIDDEN, track));
toggle = Glib :: wrap(GTK_TOGGLE_BUTTON(gui_builder_widget("o_favorite")), false);
toggle->set_active(playlist_has(PL_FAVORITED, track));
}
static void on_change_state(GstState state)
{
if (state == GST_STATE_PLAYING) {
o_play->hide();
o_pause->show();
} else {
o_play->show();
o_pause->hide();
}
}
static void on_config_pause(int n)
{
if (n == -1)
o_enabled->set_active(false);
else {
o_count->set_value(n);
o_enabled->set_active(true);
}
}
struct audio_ops audio_ops = {
on_load,
on_change_state,
on_config_pause,
};
static bool on_seek(Gtk::ScrollType type, double value)
{
audio_seek(value);
return true;
}
static void on_pause_count()
{
o_enabled->set_active(true);
audio_pause_after(o_count->get_value());
}
static void on_pause_enabled()
{
if (!o_enabled->get_active())
audio_pause_after(-1);
else
audio_pause_after(o_count->get_value());
}
static bool on_timeout()
{
gchar *pos = string_sec2str(audio_position() / GST_SECOND);
o_progress->set_upper(audio_duration());
o_progress->set_value(audio_position());
o_position->set_text(pos);
g_free(pos);
return true;
}
void gst :: play() { audio_play(); }
void gst :: pause() { audio_pause(); }
void gst :: stop() { audio_stop(); }
void gst :: next() { audio_next(); }
void gst :: prev() { audio_prev(); }
void gst :: toggle()
{
if (audio_cur_state() == GST_STATE_PLAYING)
gst :: pause();
else
gst :: play();
}
void gst :: init_pre()
{
o_next = Glib :: wrap(GTK_BUTTON(gui_builder_widget("o_next")), false);
o_pause = Glib :: wrap(GTK_BUTTON(gui_builder_widget("o_pause")), false);
o_play = Glib :: wrap(GTK_BUTTON(gui_builder_widget("o_play")), false);
o_prev = Glib :: wrap(GTK_BUTTON(gui_builder_widget("o_prev")), false);
o_stop = Glib :: wrap(GTK_BUTTON(gui_builder_widget("o_stop")), false);
o_seek = Glib :: wrap(GTK_SCALE(gui_builder_widget("o_seek")), false);
o_count = Glib :: wrap(GTK_SPIN_BUTTON(gui_builder_widget("o_pause_count")), false);
o_enabled = Glib :: wrap(GTK_CHECK_BUTTON(gui_builder_widget("o_pause_enabled")), false);
o_album = Glib :: wrap(GTK_LABEL(gui_builder_widget("o_album")), false);
o_artist = Glib :: wrap(GTK_LABEL(gui_builder_widget("o_artist")), false);
o_duration = Glib :: wrap(GTK_LABEL(gui_builder_widget("o_duration")), false);
o_position = Glib :: wrap(GTK_LABEL(gui_builder_widget("o_position")), false);
o_title = Glib ::wrap(GTK_LABEL(gui_builder_widget("o_title")), false);
o_progress = Glib :: wrap(GTK_ADJUSTMENT(gui_builder_object("o_progress")), false);
o_next->signal_clicked().connect(sigc::ptr_fun(next));
o_pause->signal_clicked().connect(sigc::ptr_fun(gst :: pause));
o_play->signal_clicked().connect(sigc::ptr_fun(gst :: play));
o_prev->signal_clicked().connect(sigc::ptr_fun(gst :: prev));
o_seek->signal_change_value().connect(sigc::ptr_fun(on_seek));
o_stop->signal_clicked().connect(sigc::ptr_fun(gst :: stop));
o_count->signal_changed().connect(sigc::ptr_fun(on_pause_count));
o_enabled->signal_toggled().connect(sigc::ptr_fun(on_pause_enabled));
}
void gst :: init()
{
Glib :: signal_timeout().connect(sigc::ptr_fun(on_timeout), 500);
}