ocarina/gui/audio.c

102 lines
2.6 KiB
C

/*
* Copyright 2014 (c) Anna Schumaker.
*/
#include <core/audio.h>
#include <core/playlist.h>
#include <core/string.h>
#include <gui/audio.h>
#include <gui/builder.h>
static inline void __audio_set_label(const gchar *label, const gchar *size,
const gchar *prefix, const gchar *text)
{
gchar *markup = g_markup_printf_escaped("<span size='%s'>%s%s</span>",
size, prefix, text);
gtk_label_set_markup(GTK_LABEL(gui_builder_widget(label)), markup);
g_free(markup);
}
static inline void __audio_set_time_label(const gchar *label, unsigned int time)
{
gchar *str = string_sec2str(time);
__audio_set_label(label, "large", "", str);
g_free(str);
}
static void __audio_load(struct track *track)
{
__audio_set_label("o_title", "xx-large", "", track->tr_title);
__audio_set_label("o_artist", "x-large", "By: ", track->tr_artist->ar_name);
__audio_set_label("o_album", "x-large", "From: ", track->tr_album->al_name);
__audio_set_time_label("o_duration", track->tr_length);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gui_builder_widget("o_hide")),
playlist_has(PL_HIDDEN, track));
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gui_builder_widget("o_favorite")),
playlist_has(PL_FAVORITED, track));
}
static void __audio_change_state(GstState state)
{
if (state == GST_STATE_PLAYING) {
gtk_widget_hide(gui_builder_widget("o_play"));
gtk_widget_show(gui_builder_widget("o_pause"));
} else {
gtk_widget_show(gui_builder_widget("o_play"));
gtk_widget_hide(gui_builder_widget("o_pause"));
}
}
static void __audio_config_pause(int n)
{
GtkToggleButton *enabled = GTK_TOGGLE_BUTTON(gui_builder_widget("o_pause_enabled"));
if (n >= 0)
gtk_spin_button_set_value(GTK_SPIN_BUTTON(gui_builder_widget("o_pause_count")), n);
gtk_toggle_button_set_active(enabled, n >= 0);
}
void __audio_pause_count(GtkSpinButton *count)
{
audio_pause_after(gtk_spin_button_get_value(count));
}
void __audio_pause_enabled(GtkToggleButton *enabled, GtkSpinButton *count)
{
int val = -1;
if (gtk_toggle_button_get_active(enabled))
val = gtk_spin_button_get_value(count);
audio_pause_after(val);
}
void __audio_seek(GtkScrollType type, double value)
{
audio_seek(value);
}
static int __audio_timeout(gpointer data)
{
GtkAdjustment *progress = data;
gtk_adjustment_set_upper(progress, audio_duration());
gtk_adjustment_set_value(progress, audio_position());
__audio_set_time_label("o_position", audio_position() / GST_SECOND);
return G_SOURCE_CONTINUE;
}
struct audio_ops audio_ops = {
__audio_load,
__audio_change_state,
__audio_config_pause,
};
void gui_audio_init()
{
g_timeout_add(500, __audio_timeout, gui_builder_object("o_progress"));
}