ocarina/gui/gst.cpp

173 lines
4.4 KiB
C++

/*
* 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>
static GtkButton *o_pause;
static GtkButton *o_play;
static GtkButton *o_prev;
static GtkScale *o_seek;
static GtkButton *o_stop;
static GtkSpinButton *o_count;
static GtkToggleButton *o_enabled;
static GtkLabel *o_album;
static GtkLabel *o_artist;
static GtkLabel *o_duration;
static GtkLabel *o_position;
static GtkLabel *o_title;
static GtkAdjustment *o_progress;
static void set_markup(GtkLabel *label, const std::string &size,
const std::string &text)
{
gchar *markup = g_markup_printf_escaped("<span size='%s'>%s</span>",
size.c_str(), text.c_str());
gtk_label_set_markup(label, markup);
g_free(markup);
}
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);
gtk_label_set_text(o_duration, 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) {
gtk_widget_hide(GTK_WIDGET(o_play));
gtk_widget_show(GTK_WIDGET(o_pause));
} else {
gtk_widget_show(GTK_WIDGET(o_play));
gtk_widget_hide(GTK_WIDGET(o_pause));
}
}
static void on_config_pause(int n)
{
if (n == -1)
gtk_toggle_button_set_active(o_enabled, false);
else {
gtk_spin_button_set_value(o_count, n);
gtk_toggle_button_set_active(o_enabled, 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()
{
gtk_toggle_button_set_active(o_enabled, true);
audio_pause_after(gtk_spin_button_get_value(o_count));
}
static void on_pause_enabled()
{
if (!gtk_toggle_button_get_active(o_enabled))
audio_pause_after(-1);
else
audio_pause_after(gtk_spin_button_get_value(o_count));
}
static bool on_timeout()
{
gchar *pos = string_sec2str(audio_position() / GST_SECOND);
gtk_adjustment_set_upper(o_progress, audio_duration());
gtk_adjustment_set_value(o_progress, audio_position());
gtk_label_set_text(o_position, pos);
g_free(pos);
return true;
}
void gst :: play() { audio_play(); }
void gst :: pause() { audio_pause(); }
void gst :: stop() { audio_stop(); }
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_pause = GTK_BUTTON(gui_builder_widget("o_pause"));
o_play = GTK_BUTTON(gui_builder_widget("o_play"));
o_prev = GTK_BUTTON(gui_builder_widget("o_prev"));
o_stop = GTK_BUTTON(gui_builder_widget("o_stop"));
o_seek = GTK_SCALE(gui_builder_widget("o_seek"));
o_count = GTK_SPIN_BUTTON(gui_builder_widget("o_pause_count"));
o_enabled = GTK_TOGGLE_BUTTON(gui_builder_widget("o_pause_enabled"));
o_album = GTK_LABEL(gui_builder_widget("o_album"));
o_artist = GTK_LABEL(gui_builder_widget("o_artist"));
o_duration = GTK_LABEL(gui_builder_widget("o_duration"));
o_position = GTK_LABEL(gui_builder_widget("o_position"));
o_title = GTK_LABEL(gui_builder_widget("o_title"));
o_progress = GTK_ADJUSTMENT(gui_builder_object("o_progress"));
g_signal_connect(o_pause, "clicked", gst :: pause, NULL);
g_signal_connect(o_play, "clicked", gst :: play, NULL);
g_signal_connect(o_prev, "clicked", gst :: prev, NULL);
g_signal_connect(o_seek, "change-value", G_CALLBACK(on_seek), NULL);
g_signal_connect(o_stop, "clicked", gst :: stop, NULL);
g_signal_connect(o_count, "changed", G_CALLBACK(on_pause_count), NULL);
g_signal_connect(o_enabled, "toggled", G_CALLBACK(on_pause_enabled), NULL);
}
void gst :: init()
{
Glib :: signal_timeout().connect(sigc::ptr_fun(on_timeout), 500);
}