seamus

commit 3e1b143e24d1c2937e750d22913106a1900180c6

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

status: initial refactoring of status (wip)

 include/status.h | 1 
 src/seamus.c | 5 +
 src/status.c | 135 ++++++++++++++++++++++++++++---------------------
 src/ui.c | 2 


diff --git a/include/status.h b/include/status.h
index 7c2b643129d27a6a278835809ecc6dcca5dcd07c..35d8778bb41925cc29423383a3b0a8100356c0fa 100644
--- a/include/status.h
+++ b/include/status.h
@@ -2,5 +2,6 @@ #ifndef SEAMUS_STATUS
 #define SEAMUS_STATUS
 #include "seamus.h"
 
+int generate_description(struct seamus_status *seamus, struct mpd_status *status);
 int fetch_current_status(struct seamus_frontend *seamus);
 #endif




diff --git a/src/seamus.c b/src/seamus.c
index 94bc1b6056062ea3b63fc127513b8cb7aa75a24e..86abec87e5209b497d55651dd30c3c7c8bff390f 100755
--- a/src/seamus.c
+++ b/src/seamus.c
@@ -44,7 +44,10 @@ 		seamus->queue_size--;
 	}
 
 	free(seamus->queue);
-	free(seamus->current_status);
+
+	struct seamus_status *sts = seamus->status;
+	free(sts->description);
+	free(sts);
 }
 
 int




diff --git a/src/status.c b/src/status.c
index ef7052db780004a9a801cdc95a46ed46f7401ac3..7f5db31f38ec5d53fb97578f39dfa21bbde4bea6 100644
--- a/src/status.c
+++ b/src/status.c
@@ -17,83 +17,102 @@
 		return 1;
 	}
 
-	if (mpd_status_get_state(status) == MPD_STATE_PLAY ||
-		mpd_status_get_state(status) == MPD_STATE_PAUSE) {
+	// TODO: create or populate this mf
+	if (s->status == NULL) {
+		struct seamus_status *s_status = calloc(1, sizeof(struct seamus_status));
+		memset(&s_status->description, 0, sizeof(char));
+		s->status = s_status;
+	}
 
-		struct mpd_song *song = mpd_run_current_song(s->conn);
+	s->status->repeat = mpd_status_get_repeat(status);
+	s->status->version = mpd_status_get_queue_version(status);
+	s->status->length = mpd_status_get_queue_length(status);
+	s->status->current_song_position = mpd_status_get_song_pos(status);
+	s->status->current_song_id = mpd_status_get_song_id(status);
+	s->status->state = mpd_status_get_state(status);
+	s->status->elapsed_time = mpd_status_get_elapsed_time(status);
+	s->status->total_time = mpd_status_get_total_time(status);
 
-		const char *state;
+	mpd_response_finish(s->conn);
+
+	struct mpd_song *song = mpd_run_current_song(s->conn);
+	if (song != NULL) {
+		int desc = generate_description(s->status, song);
 
-		if (mpd_status_get_state(status) == MPD_STATE_PLAY) {
-			state = "[playing]";
-		} else {
-			state = "[paused]";
+		if (desc != 0) {
+			log_debug("Something went off, when generate description");
 		}
+	}
 
-		if (song != NULL) {
-			const char *title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
-			const char *artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
+	mpd_status_free(status);
+	mpd_response_finish(s->conn);
 
-			char *elapsed_time = (char*) malloc(13 * sizeof(char));
+	return 0;
+}
 
-			sprintf(elapsed_time, "%3i:%02i",
-					mpd_status_get_elapsed_time(status) / 60,
-					mpd_status_get_elapsed_time(status) % 60);
+int
+generate_description(struct seamus_status *status, struct mpd_song *song)
+{
 
-			char *total_time = (char*) malloc(13 * sizeof(char));
+	const char *title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
+	const char *artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
 
-			sprintf(total_time, "%i:%02i",
-					mpd_status_get_total_time(status) / 60,
-					mpd_status_get_total_time(status) % 60);
+	char *elapsed_time = (char*) malloc(13 * sizeof(char));
 
-			// 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
-				)
-			);
+	sprintf(elapsed_time, "%3i:%02i",
+			status->elapsed_time / 60,
+			status->elapsed_time % 60);
 
-			char *str = malloc(strsz);
+	char *total_time = (char*) malloc(13 * sizeof(char));
 
-			if (s->current_status != NULL) {
-				free(s->current_status);
-			}
+	sprintf(total_time, "%i:%02i",
+			status->total_time / 60,
+			status->total_time % 60);
 
-			s->current_status = malloc(strsz);
+	// TODO: This looks kind of bad. Probably have some
+	// less stupid way to do it
+	size_t strsz = (
+			sizeof(char) * (
+				strlen(artist) +
+				strlen(title) +
+				strlen(elapsed_time) +
+				strlen(total_time) +
+				20
+				)
+		       );
 
-			sprintf(str, "%s %s - %s: %s/%s",
-				state,
-				artist,
-				title,
-				elapsed_time,
-				total_time
-			);
+	char *str = malloc(strsz);
 
-			strcpy(s->current_status, str);
+	if (status->description != NULL) {
+		free(status->description);
+	}
 
-			free(total_time);
-			free(elapsed_time);
-			free(str);
-			mpd_song_free(song);
-		}
-	} else {
-		const char *stat = "Stopped";
+	status->description = malloc(strsz);
 
-		// This works like a charm, albeit it's ugly:
-		s->current_status = malloc(sizeof(char) * strlen(stat));
-		strcpy(s->current_status, stat);
+	sprintf(str, "%s - %s: %s/%s",
+			artist,
+			title,
+			elapsed_time,
+			total_time
+	       );
 
-		return 0;
-	}
+	strcpy(status->description, str);
 
-	mpd_status_free(status);
-	mpd_response_finish(s->conn);
+	free(total_time);
+	free(elapsed_time);
+	free(str);
+	mpd_song_free(song);
 
 	return 0;
+//} else {
+//	const char *stat = "Stopped";
+//
+//	// This works like a charm, albeit it's ugly:
+//	status->description = malloc(sizeof(char) * strlen(stat));
+//	strcpy(status->description, stat);
+//
+//	return 0;
+//}
+
+//return 1;
 }




diff --git a/src/ui.c b/src/ui.c
index 632c16f097698eb5901e7467e25a9314a4ab7d08..146e61d26344df33e49ae55af9deafd647dae9ea 100644
--- a/src/ui.c
+++ b/src/ui.c
@@ -161,7 +161,7 @@ 					TICKIT_PEN_BOLD, 1,
 					0);
 
 			tickit_renderbuffer_setpen(render_buffer, pen);
-			tickit_renderbuffer_text(render_buffer, seamus->current_status);
+			tickit_renderbuffer_text(render_buffer, seamus->status->description);
 			tickit_renderbuffer_restore(render_buffer);
 		}
 	}