Bugfix: memory and thread-deleting problems
[monky] / src / mpd.c
index 44d0abe..ec929b7 100644 (file)
--- a/src/mpd.c
+++ b/src/mpd.c
@@ -1,4 +1,5 @@
 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
+ * vim: ts=4 sw=4 noet ai cindent syntax=c
  *
  * Conky, a system monitor, based on torsmo
  *
@@ -8,7 +9,7 @@
  *
  * Please see COPYING for details
  *
- * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
+ * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
  *     (see AUTHORS)
  * All rights reserved.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
- * vim: ts=4 sw=4 noet ai cindent syntax=c
- *
  */
 
 #include "conky.h"
 #include "logging.h"
 #include "timed_thread.h"
+#include "timeinfo.h"
 #include "libmpdclient.h"
 #include "mpd.h"
 
@@ -81,9 +81,7 @@ int mpd_set_port(const char *port)
 void init_mpd(void)
 {
        if (!(refcount++))      /* first client */
-               memset(&mpd_info, 0, sizeof(struct mpd_s));
-
-       refcount++;
+               memset(&mpd_info, 0, sizeof(mpd_info));
 }
 
 struct mpd_s *mpd_get_info(void)
@@ -115,23 +113,24 @@ void free_mpd(void)
 
 static void *update_mpd_thread(void *) __attribute__((noreturn));
 
-void update_mpd(void)
+int update_mpd(void)
 {
        int interval;
        static timed_thread *thread = NULL;
 
        if (thread)
-               return;
+               return 0;
 
        interval = info.music_player_interval * 1000000;
        thread = timed_thread_create(&update_mpd_thread, &thread, interval);
        if (!thread) {
-               ERR("Failed to create MPD timed thread");
-               return;
+               NORM_ERR("Failed to create MPD timed thread");
+               return 0;
        }
        timed_thread_register(thread, &thread);
        if (timed_thread_run(thread))
-               ERR("Failed to run MPD timed thread");
+               NORM_ERR("Failed to run MPD timed thread");
+       return 0;
 }
 
 /* stringMAXdup dups at most text_buffer_size bytes */
@@ -157,7 +156,7 @@ static void *update_mpd_thread(void *arg)
                timed_thread_lock(me);
 
                if (conn->error || conn == NULL) {
-                       ERR("MPD error: %s\n", conn->errorStr);
+                       NORM_ERR("MPD error: %s\n", conn->errorStr);
                        mpd_closeConnection(conn);
                        conn = 0;
                        clear_mpd();
@@ -172,7 +171,7 @@ static void *update_mpd_thread(void *arg)
 
                mpd_sendStatusCommand(conn);
                if ((status = mpd_getStatus(conn)) == NULL) {
-                       ERR("MPD error: %s\n", conn->errorStr);
+                       NORM_ERR("MPD error: %s\n", conn->errorStr);
                        mpd_closeConnection(conn);
                        conn = 0;
                        clear_mpd();
@@ -196,7 +195,21 @@ static void *update_mpd_thread(void *arg)
                        continue;
                }
 
-               mpd_info.volume = status->volume;
+               mpd_info.vol = status->volume;
+               if (status->random == 0) {
+                       mpd_info.random = "Off";
+               } else if (status->random == 1) {
+                       mpd_info.random = "On";
+               } else {
+                       mpd_info.random = "";
+               }
+               if (status->repeat == 0) {
+                       mpd_info.repeat = "Off";
+               } else if (status->repeat == 1) {
+                       mpd_info.repeat = "On";
+               } else {
+                       mpd_info.repeat = "";
+               }
                /* if (status->error) {
                        printf("error: %s\n", status->error);
                } */
@@ -217,11 +230,7 @@ static void *update_mpd_thread(void *arg)
                                break;
                }
 
-               if (status->state == MPD_STATUS_STATE_STOP) {
-                       mpd_info.progress = (float) status->elapsedTime /
-                               status->totalTime;
-                       mpd_info.elapsed = status->elapsedTime;
-               } else if (status->state == MPD_STATUS_STATE_PLAY ||
+               if (status->state == MPD_STATUS_STATE_PLAY ||
                    status->state == MPD_STATUS_STATE_PAUSE) {
                        mpd_info.is_playing = 1;
                        mpd_info.bitrate = status->bitRate;
@@ -229,22 +238,10 @@ static void *update_mpd_thread(void *arg)
                                status->totalTime;
                        mpd_info.elapsed = status->elapsedTime;
                        mpd_info.length = status->totalTime;
-                       if (status->random == 0) {
-                               mpd_info.random = "Off";
-                       } else if (status->random == 1) {
-                               mpd_info.random = "On";
-                       } else {
-                               mpd_info.random = "";
-                       }
-                       if (status->repeat == 0) {
-                               mpd_info.repeat = "Off";
-                       } else if (status->repeat == 1) {
-                               mpd_info.repeat = "On";
-                       } else {
-                               mpd_info.repeat = "";
-                       }
                } else {
+                       mpd_info.progress = 0;
                        mpd_info.is_playing = 0;
+                       mpd_info.elapsed = 0;
                }
 
                if (conn->error) {
@@ -321,3 +318,98 @@ static void *update_mpd_thread(void *arg)
        /* never reached */
 }
 
+static inline void format_media_player_time(char *buf, const int size,
+               int seconds)
+{
+       int days, hours, minutes;
+
+       if (times_in_seconds()) {
+               snprintf(buf, size, "%d", seconds);
+               return;
+       }
+
+       days = seconds / (24 * 60 * 60);
+       seconds %= (24 * 60 * 60);
+       hours = seconds / (60 * 60);
+       seconds %= (60 * 60);
+       minutes = seconds / 60;
+       seconds %= 60;
+
+       if (days > 0) {
+               snprintf(buf, size, "%i days %i:%02i:%02i", days,
+                               hours, minutes, seconds);
+       } else if (hours > 0) {
+               snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
+                               seconds);
+       } else {
+               snprintf(buf, size, "%i:%02i", minutes, seconds);
+       }
+}
+
+void print_mpd_elapsed(struct text_object *obj, char *p, int p_max_size)
+{
+       (void)obj;
+       format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
+}
+
+void print_mpd_length(struct text_object *obj, char *p, int p_max_size)
+{
+       (void)obj;
+       format_media_player_time(p, p_max_size, mpd_get_info()->length);
+}
+
+void print_mpd_percent(struct text_object *obj, char *p, int p_max_size)
+{
+       (void)obj;
+       percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
+}
+
+void print_mpd_bar(struct text_object *obj, char *p, int p_max_size)
+{
+       new_bar(obj, p, p_max_size, (int) (mpd_get_info()->progress * 255.0f));
+}
+
+void print_mpd_smart(struct text_object *obj, char *p, int p_max_size)
+{
+       struct mpd_s *mpd = mpd_get_info();
+       int len = obj->data.i;
+       if (len == 0 || len > p_max_size)
+               len = p_max_size;
+
+       memset(p, 0, p_max_size);
+       if (mpd->artist && *mpd->artist &&
+                       mpd->title && *mpd->title) {
+               snprintf(p, len, "%s - %s", mpd->artist,
+                               mpd->title);
+       } else if (mpd->title && *mpd->title) {
+               snprintf(p, len, "%s", mpd->title);
+       } else if (mpd->artist && *mpd->artist) {
+               snprintf(p, len, "%s", mpd->artist);
+       } else if (mpd->file && *mpd->file) {
+               snprintf(p, len, "%s", mpd->file);
+       } else {
+               *p = 0;
+       }
+}
+
+#define MPD_PRINT_GENERATOR(name, fmt) \
+void print_mpd_##name(struct text_object *obj, char *p, int p_max_size) \
+{ \
+       if (obj->data.i && obj->data.i < p_max_size) \
+               p_max_size = obj->data.i; \
+       snprintf(p, p_max_size, fmt, mpd_get_info()->name); \
+}
+
+MPD_PRINT_GENERATOR(title, "%s")
+MPD_PRINT_GENERATOR(artist, "%s")
+MPD_PRINT_GENERATOR(album, "%s")
+MPD_PRINT_GENERATOR(random, "%s")
+MPD_PRINT_GENERATOR(repeat, "%s")
+MPD_PRINT_GENERATOR(track, "%s")
+MPD_PRINT_GENERATOR(name, "%s")
+MPD_PRINT_GENERATOR(file, "%s")
+MPD_PRINT_GENERATOR(vol, "%d")
+MPD_PRINT_GENERATOR(bitrate, "%d")
+MPD_PRINT_GENERATOR(status, "%s")
+
+#undef MPD_PRINT_GENERATOR