summaryrefslogtreecommitdiffstats
path: root/src/c_mpdview.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/c_mpdview.c')
-rw-r--r--src/c_mpdview.c103
1 files changed, 75 insertions, 28 deletions
diff --git a/src/c_mpdview.c b/src/c_mpdview.c
index cfad0be..aa0c008 100644
--- a/src/c_mpdview.c
+++ b/src/c_mpdview.c
@@ -59,6 +59,7 @@
#include <mpd/song.h>
#include <mpd/status.h>
+#include "c_defines.h"
#include "c_mpdview.h"
static struct mpdview_s *
@@ -74,16 +75,71 @@ c_create_view(void)
return (view);
}
-static unsigned char
-c_connect_mpd(struct mpdview_s* view)
+static u8_t
+c_connect_mpd(struct mpdview_s* v)
{
- view->conn = mpd_connection_new(NULL, 0, 3000);
- if (mpd_connection_get_error(view->conn) != MPD_ERROR_SUCCESS) {
+ v->conn = mpd_connection_new(NULL, 0, 3000);
+ if (mpd_connection_get_error(v->conn) != MPD_ERROR_SUCCESS) {
dprintf(
STDERR_FILENO,
"%s: %s\n",
PROGNAME,
- mpd_connection_get_error_message(view->conn)
+ mpd_connection_get_error_message(v->conn)
+ );
+ return (RET_ERR);
+ }
+ return (RET_OK);
+}
+
+static void
+c_destroy_mpdview(struct mpdview_s** v)
+{
+ (*v)->state = MPD_STATE_UNKNOWN;
+ if ((*v)->song != NULL) {
+ mpd_song_free((*v)->song);
+ }
+ (*v)->song = NULL;
+ if ((*v)->status != NULL) {
+ mpd_status_free((*v)->status);
+ }
+ (*v)->status = NULL;
+ if ((*v)->conn != NULL) {
+ mpd_connection_free((*v)->conn);
+ }
+ (*v)->conn = NULL;
+ free(*v);
+ *v = NULL;
+}
+
+static u8_t
+c_get_mpd_status(struct mpdview_s* v)
+{
+ v->status = mpd_run_status(v->conn);
+ if (v->status == NULL) {
+ dprintf(
+ STDERR_FILENO,
+ "%s: %s\n",
+ PROGNAME,
+ mpd_status_get_error(v->status)
+ );
+ return (RET_ERR);
+ }
+ v->state = mpd_status_get_state(v->status);
+ mpd_status_free(v->status);
+ v->status = NULL;
+ return (RET_OK);
+}
+
+static u8_t
+c_get_mpd_song(struct mpdview_s* v)
+{
+ v->song = mpd_run_current_song(v->conn);
+ if (v->song == NULL) {
+ dprintf(
+ STDERR_FILENO,
+ "%s: %s\n",
+ PROGNAME,
+ "Could not get song"
);
return (RET_ERR);
}
@@ -97,7 +153,7 @@ main
const char* envp[])
{
struct mpdview_s* view;
- int read_size;
+ i32_t read_size;
char buff[BUFF_SIZE];
(void)argc;
@@ -108,25 +164,21 @@ main
return (EXIT_FAILURE);
}
if (c_connect_mpd(view) != RET_OK) {
- if (view->conn != NULL) {
- mpd_connection_free(view->conn);
- }
- view->conn = NULL;
- free(view);
- view = NULL;
+ c_destroy_mpdview(&view);
+ return (EXIT_FAILURE);
+ }
+ if (c_get_mpd_status(view) == RET_ERR) {
+ c_destroy_mpdview(&view);
return (EXIT_FAILURE);
}
- mpd_command_list_begin(view->conn, TRUE);
- mpd_send_status(view->conn);
- mpd_send_current_song(view->conn);
- mpd_command_list_end(view->conn);
- view->status = mpd_recv_status(view->conn);
- view->state = mpd_status_get_state(view->status);
- mpd_status_free(view->status);
- view->status = NULL;
if (view->state == MPD_STATE_PLAY || view->state == MPD_STATE_PAUSE) {
- mpd_response_next(view->conn);
- view->song = mpd_recv_song(view->conn);
+ if (c_get_mpd_song(view) == RET_ERR) {
+ c_destroy_mpdview(&view);
+ return (EXIT_FAILURE);
+ }
+ dprintf(STDOUT_FILENO, "%s\n", mpd_song_get_uri(view->song));
+ c_destroy_mpdview(&view);
+ return (EXIT_SUCCESS);
mpd_response_finish(view->conn);
bzero(buff, BUFF_SIZE);
read_size = mpd_run_albumart(view->conn, mpd_song_get_uri(view->song), 0, buff, BUFF_SIZE);
@@ -139,11 +191,6 @@ main
mpd_song_free(view->song);
view->song = NULL;
}
- if (view->conn != NULL) {
- mpd_connection_free(view->conn);
- }
- view->conn = NULL;
- free(view);
- view = NULL;
+ c_destroy_mpdview(&view);
return (EXIT_SUCCESS);
}