Code

mpdclient: add "playing" attribute
authorMax Kellermann <max.kellermann@gmail.com>
Fri, 17 Mar 2017 22:24:27 +0000 (23:24 +0100)
committerMax Kellermann <max.kellermann@gmail.com>
Fri, 17 Mar 2017 22:28:05 +0000 (23:28 +0100)
Cache the mpd_status_get_state()==MPD_STATE_PLAY value which is used
pretty often.

src/main.c
src/mpdclient.c
src/mpdclient.h

index 88e6f221fc89afb891470a29027fe2afccd8e044..177f1b529be07e0506fe8d245bf82b5d78c94ceb 100644 (file)
@@ -74,12 +74,10 @@ static guint check_key_bindings_source_id;
 static void
 update_xterm_title(void)
 {
-       struct mpd_status *status = mpd->status;
        const struct mpd_song *song = mpd->song;
 
        char tmp[BUFSIZE];
-       if (options.xterm_title_format && status && song &&
-           mpd_status_get_state(status) == MPD_STATE_PLAY)
+       if (options.xterm_title_format && mpd->playing && song)
                strfsong(tmp, BUFSIZE, options.xterm_title_format, song);
        else
                g_strlcpy(tmp, PACKAGE " version " VERSION, BUFSIZE);
@@ -177,9 +175,7 @@ disable_update_timer(void)
 static bool
 should_enable_update_timer(void)
 {
-       return (mpdclient_is_connected(mpd) &&
-               mpd->status != NULL &&
-               mpd_status_get_state(mpd->status) == MPD_STATE_PLAY)
+       return mpd->playing
 #ifndef NCMPC_MINI
                || options.display_time
 #endif
@@ -202,9 +198,7 @@ static void
 do_mpd_update(void)
 {
        if (mpdclient_is_connected(mpd) &&
-           (mpd->events != 0 ||
-            (mpd->status != NULL &&
-             mpd_status_get_state(mpd->status) == MPD_STATE_PLAY)))
+           (mpd->events != 0 || mpd->playing))
                mpdclient_update(mpd);
 
 #ifndef NCMPC_MINI
index 13937516897838b43b2f883758af8d966562d272..0209d6464bf8287ab4294ded44d35995ab05043b 100644 (file)
@@ -104,6 +104,7 @@ mpdclient_new(void)
        playlist_init(&c->playlist);
        c->volume = -1;
        c->events = 0;
+       c->playing = false;
 
        return c;
 }
@@ -128,6 +129,7 @@ mpdclient_status_free(struct mpdclient *c)
        c->status = NULL;
 
        c->volume = -1;
+       c->playing = false;
 }
 
 void
@@ -210,6 +212,7 @@ mpdclient_update(struct mpdclient *c)
                return mpdclient_handle_error(c);
 
        c->volume = mpd_status_get_volume(c->status);
+       c->playing = mpd_status_get_state(c->status) == MPD_STATE_PLAY;
 
        /* check if the playlist needs an update */
        if (c->playlist.version != mpd_status_get_queue_version(c->status)) {
index 52088151dba89287f4bf57219005bbf386010f52..7756710173a368116e2165f161bf346919bde78f 100644 (file)
@@ -41,6 +41,11 @@ struct mpdclient {
         * "idle" mode, and the #mpd_glib_source waits for an event.
         */
        bool idle;
+
+       /**
+        * Is MPD currently playing?
+        */
+       bool playing;
 };
 
 enum {