Code

mpdclient: added function mpdclient_get_current_song()
authorMax Kellermann <max@duempel.org>
Wed, 30 Sep 2009 19:05:48 +0000 (21:05 +0200)
committerMax Kellermann <max@duempel.org>
Wed, 30 Sep 2009 19:05:48 +0000 (21:05 +0200)
src/mpdclient.h
src/player_command.c
src/screen_play.c

index 1b83feb01d2c8e573110bf973ce916a378331d90..f363aa9d2a05c297dfcfe5b262453b27eed7dd6d 100644 (file)
@@ -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);
index 04256eb1438714ef2dcb6e1d6c8a12377c51005c..085ffa14a478f0eba1f211954b7fd3653369c59d 100644 (file)
@@ -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);
                        }
index 0f31032d1175ac78fd6ec0f567cfcf72c657fbaa..5cc3457592878492362a7ff04df9390b759922a9 100644 (file)
@@ -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 */