seamus

commit de1c54b70ea163ff19f30815557fe2a33aab2334

Author: Pedro Lucas Porcellis <porcellis@eletrotupi.com>

status: fetch and display current mpd status

 configure | 1 
 include/seamus.h | 2 +
 include/status.h | 6 +++
 include/ui.h | 1 
 src/status.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++


diff --git a/configure b/configure
index bc602f7940baa4372b23e97d44511e1992a4ec08..d0f4d965eaaf10ec361c797a4f15cd20fb72d938 100755
--- a/configure
+++ b/configure
@@ -9,6 +9,7 @@ 	genrules seamus \
 		src/seamus.c \
 		src/log.c \
 		src/queue.c \
+		src/status.c \
 		src/ui.c
 }
 




diff --git a/include/seamus.h b/include/seamus.h
index a4f47b7b04587a0c747deddbb075ddc4319d3fb1..7f9cb0cc4a24e8719d69f403822a398d23294466 100644
--- a/include/seamus.h
+++ b/include/seamus.h
@@ -15,7 +15,9 @@ struct seamus_frontend {
 	struct mpd_connection *conn;
 	struct seamus_song *queue;
 	size_t queue_size;
+	char *current_status;
 	TickitWindow *main_window;
+	TickitWindow *status_window;
 	Tickit *t;
 };
 




diff --git a/include/status.h b/include/status.h
new file mode 100644
index 0000000000000000000000000000000000000000..7c2b643129d27a6a278835809ecc6dcca5dcd07c
--- /dev/null
+++ b/include/status.h
@@ -0,0 +1,6 @@
+#ifndef SEAMUS_STATUS
+#define SEAMUS_STATUS
+#include "seamus.h"
+
+int fetch_current_status(struct seamus_frontend *seamus);
+#endif




diff --git a/include/ui.h b/include/ui.h
index 32148ba1baf553882029c32bd03344e3650c6ea4..f6e80fc602618455f9afc12acbda8bf34b951325 100644
--- a/include/ui.h
+++ b/include/ui.h
@@ -10,5 +10,6 @@ int tickit_finish(struct seamus_frontend *s);
 
 
 static int render_main_window(TickitWindow *win, TickitEventFlags flags, void *_info, void *data);
+static int render_status_window(TickitWindow *win, TickitEventFlags flags, void *_info, void *data);
 static int render_root(TickitWindow *win, TickitEventFlags flags, void *_info, void *data);
 #endif




diff --git a/src/status.c b/src/status.c
new file mode 100644
index 0000000000000000000000000000000000000000..0c7b33b27767118bb23b033743a260768b8993a7
--- /dev/null
+++ b/src/status.c
@@ -0,0 +1,92 @@
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+#include "seamus.h"
+
+int
+fetch_current_status(struct seamus_frontend *s)
+{
+	assert(s->conn != NULL);
+
+	struct mpd_status *status = mpd_run_status(s->conn);
+
+	if (status == NULL) {
+		const char *message = mpd_connection_get_error_message(s->conn);
+		log_error("MPD Error - No Status: %s", message);
+
+		return 1;
+	}
+
+	if (mpd_status_get_state(status) == MPD_STATE_PLAY ||
+		mpd_status_get_state(status) == MPD_STATE_PAUSE) {
+
+		struct mpd_song *song = mpd_run_current_song(s->conn);
+
+		const char *state;
+
+		if (mpd_status_get_state(status) == MPD_STATE_PLAY) {
+			state = "[playing]";
+		} else {
+			state = "[paused]";
+		}
+
+		if (song != NULL) {
+			enum mpd_tag_type tag_artist = mpd_tag_name_iparse("Artist");
+			enum mpd_tag_type tag_title = mpd_tag_name_iparse("Title");
+
+			const char *title = mpd_song_get_tag(song, tag_title, 0);
+			const char *artist = mpd_song_get_tag(song, tag_artist, 0);
+
+			char *elapsed_time = (char*) malloc(13 * sizeof(char));
+
+			sprintf(elapsed_time, "%3i:%02i",
+					mpd_status_get_elapsed_time(status) / 60,
+					mpd_status_get_elapsed_time(status) % 60);
+
+			char *total_time = (char*) malloc(13 * sizeof(char));
+
+			sprintf(total_time, "%i:%02i",
+					mpd_status_get_total_time(status) / 60,
+					mpd_status_get_total_time(status) % 60);
+
+			// TODO: This looks kind of bad. Probably have some
+			// less stupid way to do it
+			size_t strsz = (
+				sizeof(char) * (
+					strlen(state) +
+					strlen(artist) +
+					strlen(title) +
+					strlen(elapsed_time) +
+					strlen(total_time) +
+					20
+				)
+			);
+
+			char *str = malloc(strsz);
+			if (s->current_status == NULL) {
+				s->current_status = malloc(strsz);
+			} else {
+				free(s->current_status);
+			}
+
+			sprintf(str, "%s %s - %s: %s/%s",
+				state,
+				artist,
+				title,
+				elapsed_time,
+				total_time
+			);
+
+			strcpy(s->current_status, str);
+
+			free(total_time);
+			free(elapsed_time);
+			free(str);
+			mpd_song_free(song);
+
+			return 0;
+		}
+	}
+
+	mpd_status_free(status);
+}