Skip to content

Commit

Permalink
Move playback control to pragha-playback.c
Browse files Browse the repository at this point in the history
* Rename some functions, and remove some duplicate code.
* Remove play_first_*() funcion. Use next() or play() instead.
* Add a pragha_playback_stop() to avoid pragha_backend_*() usage.
  • Loading branch information
matiasdelellis committed Oct 10, 2012
1 parent c7f8691 commit 378db7d
Show file tree
Hide file tree
Showing 13 changed files with 178 additions and 279 deletions.
179 changes: 1 addition & 178 deletions src/current-playlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ static void append_rand_track_refs(GtkTreeRowReference *ref, struct con_win *cwi

/* Remove all nodes and free the list */

static void clear_rand_track_refs(struct con_win *cwin)
void clear_rand_track_refs(struct con_win *cwin)
{
if (cwin->cstate->rand_track_refs) {
g_list_free_full(cwin->cstate->rand_track_refs, (GDestroyNotify) gtk_tree_row_reference_free);
Expand Down Expand Up @@ -2141,183 +2141,6 @@ void save_current_playlist(GtkAction *action, struct con_win *cwin)
}
}

/**********************/
/* Playback functions */
/**********************/

/* Play first track in current playlist */

void play_first_current_playlist(struct con_win *cwin)
{
GtkTreeModel *model;
GtkTreePath *path = NULL;
GtkTreeIter iter;
struct musicobject *mobj;

if(cwin->cstate->playlist_change)
return;

model = gtk_tree_view_get_model(GTK_TREE_VIEW(cwin->current_playlist));

/* Check if tree is empty, if not play first track */

if (gtk_tree_model_get_iter_first(model, &iter)) {
gtk_tree_model_get(model, &iter, P_MOBJ_PTR, &mobj, -1);

clear_rand_track_refs(cwin);
path = gtk_tree_model_get_path(model, &iter);

cwin->cstate->update_playlist_action = PLAYLIST_CURR;
update_current_playlist_state(path, cwin);

pragha_backend_start(cwin->backend, mobj);
gtk_tree_path_free(path);
}
}

/* Play prev track in current playlist */

void play_prev_track(struct con_win *cwin)
{
GtkTreePath *path;
struct musicobject *mobj = NULL;

/* Get the next (prev) track to be played */
path = current_playlist_get_prev(cwin);

/* No more tracks */
if (!path)
return;

/* Stop currently playing track */
pragha_backend_stop(cwin->backend);

/* Start playing new track */
cwin->cstate->update_playlist_action = PLAYLIST_PREV;
update_current_playlist_state(path, cwin);

mobj = current_playlist_mobj_at_path(path, cwin);
pragha_backend_start(cwin->backend, mobj);

gtk_tree_path_free(path);
}

/* Play next track in current_playlist */

void play_next_track(struct con_win *cwin)
{
GtkTreePath *path;
struct musicobject *mobj = NULL;

/* Are we playing right now ? */

if (pragha_backend_get_state (cwin->backend) == ST_STOPPED)
return;

/* Stop currently playing track */
pragha_backend_stop(cwin->backend);

/* Get the next track to be played */
path = current_playlist_get_next(cwin);

/* No more tracks */
if (!path)
return;

/* Start playing new track */
cwin->cstate->update_playlist_action = PLAYLIST_NEXT;
update_current_playlist_state(path, cwin);

mobj = current_playlist_mobj_at_path(path, cwin);
pragha_backend_start(cwin->backend, mobj);

gtk_tree_path_free(path);
}

/* Start playback of a new track, or resume playback of current track */

void play_track(struct con_win *cwin)
{
struct musicobject *mobj = NULL;
GtkTreePath *path = NULL;

/* New action is based on the current state */

/************************************/
/* State Action */
/* */
/* Playing Restart playback */
/* Paused Resume playback */
/* Stopped Start playback */
/************************************/

switch (pragha_backend_get_state (cwin->backend)) {
case ST_PLAYING:
if (cwin->cpref->shuffle && cwin->cstate->curr_rand_ref)
path = gtk_tree_row_reference_get_path(cwin->cstate->curr_rand_ref);
else if (!cwin->cpref->shuffle && cwin->cstate->curr_seq_ref)
path = gtk_tree_row_reference_get_path(cwin->cstate->curr_seq_ref);

if (path) {
cwin->cstate->update_playlist_action = PLAYLIST_CURR;
update_current_playlist_state(path, cwin);

mobj = new_musicobject_from_file(cwin->cstate->curr_mobj->file);
pragha_backend_start(cwin->backend, mobj);

gtk_tree_path_free(path);
}
break;
case ST_PAUSED:
pragha_backend_resume(cwin->backend);
break;
case ST_STOPPED:
if(cwin->cstate->playlist_change)
break;
if(cwin->cstate->queue_track_refs)
path = get_next_queue_track(cwin);
if(!path)
path = current_playlist_get_selection(cwin);
if(!path && cwin->cpref->shuffle)
path = get_first_random_track(cwin);
if(!path) {
play_first_current_playlist(cwin);
break;
}

clear_rand_track_refs(cwin);
current_playlist_clear_dirty_all(cwin);
cwin->cstate->unplayed_tracks = cwin->cstate->tracks_curr_playlist;

cwin->cstate->update_playlist_action = PLAYLIST_CURR;
update_current_playlist_state(path, cwin);

mobj = current_playlist_mobj_at_path(path, cwin);
pragha_backend_start(cwin->backend, mobj);

gtk_tree_path_free(path);
break;
default:
break;
}
}

/* Toggle pause/resume */

void pause_resume_track(struct con_win *cwin)
{
switch(pragha_backend_get_state (cwin->backend)) {
case ST_PAUSED:
pragha_backend_resume(cwin->backend);
break;
case ST_PLAYING:
pragha_backend_pause(cwin->backend);
break;
default:
break;
}
}

/*******************/
/* Event Callbacks */
/*******************/
Expand Down
10 changes: 5 additions & 5 deletions src/dbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,27 @@

static void dbus_play_handler(struct con_win *cwin)
{
play_track(cwin);
pragha_playback_play_pause_resume(cwin);
}

static void dbus_stop_handler(struct con_win *cwin)
{
pragha_backend_stop(cwin->backend);
pragha_playback_stop(cwin);
}

static void dbus_pause_handler(struct con_win *cwin)
{
play_pause_resume(cwin);
pragha_playback_play_pause_resume(cwin);
}

static void dbus_next_handler(struct con_win *cwin)
{
play_next_track(cwin);
pragha_playback_next_track(cwin);
}

static void dbus_prev_handler(struct con_win *cwin)
{
play_prev_track(cwin);
pragha_playback_prev_track(cwin);
}

static void dbus_shuffle_handler(struct con_win *cwin)
Expand Down
8 changes: 4 additions & 4 deletions src/gnome-media-keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ static void on_media_player_key_pressed(struct con_gnome_media_keys *gmk,
return;

if (strcmp("Play", key) == 0)
play_pause_resume(cwin);
pragha_playback_play_pause_resume(cwin);
else if (strcmp("Pause", key) == 0)
pragha_backend_pause(cwin->backend);
else if (strcmp("Stop", key) == 0)
pragha_backend_stop(cwin->backend);
pragha_playback_stop(cwin);
else if (strcmp("Previous", key) == 0)
play_prev_track(cwin);
pragha_playback_prev_track(cwin);
else if (strcmp("Next", key) == 0)
play_next_track(cwin);
pragha_playback_next_track(cwin);
else if (strcmp("Repeat", key) == 0)
repeat_button_handler(GTK_TOGGLE_TOOL_BUTTON(cwin->repeat_button), cwin);
else if (strcmp("Shuffle", key) == 0)
Expand Down
8 changes: 4 additions & 4 deletions src/keybinder.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,31 @@ static void keybind_prev_handler (const char *keystring, gpointer data)
struct con_win *cwin = data;

if (pragha_backend_emitted_error (cwin->backend) == FALSE)
play_prev_track(cwin);
pragha_playback_prev_track(cwin);
}

static void keybind_play_handler (const char *keystring, gpointer data)
{
struct con_win *cwin = data;

if (pragha_backend_emitted_error (cwin->backend) == FALSE)
play_pause_resume(cwin);
pragha_playback_play_pause_resume(cwin);
}

static void keybind_stop_handler (const char *keystring, gpointer data)
{
struct con_win *cwin = data;

if (pragha_backend_emitted_error (cwin->backend) == FALSE)
pragha_backend_stop(cwin->backend);
pragha_playback_stop(cwin);
}

static void keybind_next_handler (const char *keystring, gpointer data)
{
struct con_win *cwin = data;

if (pragha_backend_emitted_error (cwin->backend) == FALSE)
play_next_track(cwin);
pragha_playback_next_track(cwin);
}

static void keybind_media_handler (const char *keystring, gpointer data)
Expand Down
6 changes: 5 additions & 1 deletion src/librarytree.c
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,11 @@ void library_tree_replace_and_play(GtkAction *action, struct con_win *cwin)

g_list_free(list);
}
play_first_current_playlist(cwin);

if(pragha_backend_get_state (cwin->backend) == ST_PLAYING)
pragha_playback_next_track(cwin);
else
pragha_playback_play_pause_resume(cwin);
}

void library_tree_add_to_playlist_action(GtkAction *action, struct con_win *cwin)
Expand Down
8 changes: 4 additions & 4 deletions src/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -747,28 +747,28 @@ void add_location_action(GtkAction *action, struct con_win *cwin)

void prev_action(GtkAction *action, struct con_win *cwin)
{
play_prev_track(cwin);
pragha_playback_prev_track(cwin);
}

/* Handler for the 'Play / Pause' item in the pragha menu */

void play_pause_action(GtkAction *action, struct con_win *cwin)
{
play_pause_resume(cwin);
pragha_playback_play_pause_resume(cwin);
}

/* Handler for the 'Stop' item in the pragha menu */

void stop_action(GtkAction *action, struct con_win *cwin)
{
pragha_backend_stop(cwin->backend);
pragha_playback_stop(cwin);
}

/* Handler for the 'Next' item in the pragha menu */

void next_action (GtkAction *action, struct con_win *cwin)
{
play_next_track(cwin);
pragha_playback_next_track(cwin);
}

void edit_tags_playing_action(GtkAction *action, struct con_win *cwin)
Expand Down
16 changes: 9 additions & 7 deletions src/mpris.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,23 +224,23 @@ static GVariant* mpris_Root_get_SupportedMimeTypes (GError **error, struct con_w
static void mpris_Player_Play (GDBusMethodInvocation *invocation, GVariant* parameters, struct con_win *cwin)
{
if (pragha_backend_emitted_error (cwin->backend) == FALSE)
play_track(cwin);
pragha_playback_play_pause_resume(cwin);

g_dbus_method_invocation_return_value (invocation, NULL);
}

static void mpris_Player_Next (GDBusMethodInvocation *invocation, GVariant* parameters, struct con_win *cwin)
{
if (pragha_backend_emitted_error (cwin->backend) == FALSE)
play_next_track(cwin);
pragha_playback_next_track(cwin);

g_dbus_method_invocation_return_value (invocation, NULL);
}

static void mpris_Player_Previous (GDBusMethodInvocation *invocation, GVariant* parameters, struct con_win *cwin)
{
if (pragha_backend_emitted_error (cwin->backend) == FALSE)
play_prev_track(cwin);
pragha_playback_prev_track(cwin);

g_dbus_method_invocation_return_value (invocation, NULL);
}
Expand All @@ -256,15 +256,15 @@ static void mpris_Player_Pause (GDBusMethodInvocation *invocation, GVariant* par
static void mpris_Player_PlayPause (GDBusMethodInvocation *invocation, GVariant* parameters, struct con_win *cwin)
{
if (pragha_backend_emitted_error (cwin->backend) == FALSE)
play_pause_resume(cwin);
pragha_playback_play_pause_resume(cwin);

g_dbus_method_invocation_return_value (invocation, NULL);
}

static void mpris_Player_Stop (GDBusMethodInvocation *invocation, GVariant* parameters, struct con_win *cwin)
{
if (pragha_backend_emitted_error (cwin->backend) == FALSE)
pragha_backend_stop(cwin->backend);
pragha_playback_stop(cwin);

g_dbus_method_invocation_return_value (invocation, NULL);
}
Expand Down Expand Up @@ -582,8 +582,10 @@ static void mpris_Playlists_ActivatePlaylist (GDBusMethodInvocation *invocation,
clear_current_playlist(NULL, cwin);
add_playlist_current_playlist(NULL, found_playlist, cwin);

pragha_backend_stop(cwin->backend);
play_first_current_playlist (cwin);
if(pragha_backend_get_state (cwin->backend) == ST_PLAYING)
pragha_playback_next_track(cwin);
else
pragha_playback_play_pause_resume(cwin);

g_free(found_playlist);

Expand Down

0 comments on commit 378db7d

Please sign in to comment.