ocarina/gui/audio.c
Anna Schumaker 257ef3612f gui/audio: Enable idle polling when the track changes
Unplayed, Most Played, and Least Played tracks playlists update
themselves with an idle task when tracks have been played.  It looks
like we haven't been processing these tasks, so the queue was just
building up after every track.  Fix this by enabling the GTK idle
callback whenever a track is played.

Fixes #89: Dynamic playlists aren't updating
Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
2016-09-19 14:45:11 -04:00

129 lines
3.5 KiB
C

/*
* Copyright 2014 (c) Anna Schumaker.
*/
#include <core/audio.h>
#include <core/playlist.h>
#include <core/string.h>
#include <gui/artwork.h>
#include <gui/audio.h>
#include <gui/builder.h>
#include <gui/idle.h>
#include <gui/view.h>
static inline void __audio_set_label(const gchar *label, const gchar *size,
const gchar *text)
{
gchar *markup = g_markup_printf_escaped("<span size='%s'>%s</span>",
size, 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", track->tr_artist->ar_name);
__audio_set_label("o_album", "x-large", 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_SYSTEM, "Hidden", track));
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gui_builder_widget("o_favorite")),
playlist_has(PL_SYSTEM, "Favorites", track));
gui_view_scroll();
gui_artwork_set_cover();
gui_idle_enable();
}
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)
{
GtkComboBox *combo = GTK_COMBO_BOX(gui_builder_widget("o_pause_after"));
gtk_combo_box_set_active(combo, n + 1);
}
void __audio_pause_changed(GtkComboBox *combo, gpointer data)
{
int val = gtk_combo_box_get_active(combo) - 1;
audio_pause_after(val);
}
void __audio_seek(GtkRange *range, GtkScrollType type, double value, gpointer data)
{
audio_seek(value * GST_SECOND);
}
void __audio_favorite(GtkToggleButton *toggle, gpointer data)
{
if (gtk_toggle_button_get_active(toggle))
playlist_add(PL_SYSTEM, "Favorites", audio_cur_track());
else
playlist_remove(PL_SYSTEM, "Favorites", audio_cur_track());
}
void __audio_hide(GtkToggleButton *toggle, gpointer data)
{
if (gtk_toggle_button_get_active(toggle)) {
if (playlist_add(PL_SYSTEM, "Hidden", audio_cur_track()))
audio_next();
} else
playlist_remove(PL_SYSTEM, "Hidden", audio_cur_track());
}
static int __audio_timeout(gpointer data)
{
GtkAdjustment *progress = data;
gtk_adjustment_set_upper(progress, audio_duration() / GST_SECOND);
gtk_adjustment_set_value(progress, audio_position() / GST_SECOND);
__audio_set_time_label("o_position", audio_position() / GST_SECOND);
return G_SOURCE_CONTINUE;
}
gboolean __audio_can_accel(GtkWidget *widget, guint signal_id)
{
GtkWindow *window = GTK_WINDOW(gui_builder_widget("o_window"));
g_signal_stop_emission_by_name(widget, "can-activate-accel");
return !GTK_IS_ENTRY(gtk_window_get_focus(window)) &&
gtk_widget_is_visible(widget) &&
gtk_widget_is_sensitive(widget);
}
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"));
}
#ifdef CONFIG_TESTING
void test_gui_audio_timeout()
{
__audio_timeout(gui_builder_object("o_progress"));
}
#endif /* CONFIG_TESTING */