From: Max Kellermann Date: Wed, 30 Sep 2009 19:05:48 +0000 (+0200) Subject: mpdclient: added function mpdclient_get_current_song() X-Git-Tag: release-0.16~256 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=aee38701de0938343fa23222c9b9aa876dbe8603;p=ncmpc.git mpdclient: added function mpdclient_get_current_song() --- diff --git a/src/mpdclient.h b/src/mpdclient.h index 1b83feb..f363aa9 100644 --- a/src/mpdclient.h +++ b/src/mpdclient.h @@ -40,6 +40,16 @@ mpdclient_is_connected(const struct mpdclient *c) return c->connection != NULL; } +static inline const struct mpd_song * +mpdclient_get_current_song(const struct mpdclient *c) +{ + return c->song != NULL && c->status != NULL && + (mpd_status_get_state(c->status) == MPD_STATE_PLAY || + mpd_status_get_state(c->status) == MPD_STATE_PAUSE) + ? c->song + : NULL; +} + bool mpdclient_connect(struct mpdclient *c, const gchar *host, gint port, gfloat timeout_, const gchar *password); diff --git a/src/player_command.c b/src/player_command.c index 04256eb..085ffa1 100644 --- a/src/player_command.c +++ b/src/player_command.c @@ -24,9 +24,7 @@ #include "i18n.h" #include "screen_client.h" -#define IS_PLAYING(s) (s==MPD_STATE_PLAY) #define IS_PAUSED(s) (s==MPD_STATE_PAUSE) -#define IS_STOPPED(s) (!(IS_PLAYING(s) | IS_PAUSED(s))) int seek_id = -1; int seek_target_time; @@ -80,6 +78,8 @@ cancel_seek_timer(void) bool handle_player_command(struct mpdclient *c, command_t cmd) { + const struct mpd_song *song; + if (c->connection == NULL || c->status == NULL) return false; @@ -104,10 +104,10 @@ handle_player_command(struct mpdclient *c, command_t cmd) mpdclient_cmd_crop(c); break; case CMD_SEEK_FORWARD: - if (!IS_STOPPED(mpd_status_get_state(c->status))) { - if (c->song != NULL && - seek_id != (int)mpd_song_get_id(c->song)) { - seek_id = mpd_song_get_id(c->song); + song = mpdclient_get_current_song(c); + if (song != NULL) { + if (seek_id != (int)mpd_song_get_id(song)) { + seek_id = mpd_song_get_id(song); seek_target_time = mpd_status_get_elapsed_time(c->status); } seek_target_time+=options.seek_time; @@ -122,8 +122,9 @@ handle_player_command(struct mpdclient *c, command_t cmd) mpdclient_handle_error(c); break; case CMD_SEEK_BACKWARD: - if (!IS_STOPPED(mpd_status_get_state(c->status))) { - if (seek_id != (int)mpd_song_get_id(c->song)) { + song = mpdclient_get_current_song(c); + if (song != NULL) { + if (seek_id != (int)mpd_song_get_id(song)) { seek_id = mpd_song_get_id(c->song); seek_target_time = mpd_status_get_elapsed_time(c->status); } diff --git a/src/screen_play.c b/src/screen_play.c index 0f31032..5cc3457 100644 --- a/src/screen_play.c +++ b/src/screen_play.c @@ -182,11 +182,12 @@ list_callback(unsigned idx, bool *highlight, char **second_column, G_GNUC_UNUSED static void center_playing_item(struct mpdclient *c, bool center_cursor) { + const struct mpd_song *song; unsigned length = c->playlist.list->len; int idx; - if (!c->song || c->status == NULL || - IS_STOPPED(mpd_status_get_state(c->status))) + song = mpdclient_get_current_song(c); + if (song == NULL) return; /* try to center the song that are playing */