Code

mpdclient: wrap access in mpdclient_get_connection()
[ncmpc.git] / src / screen_outputs.c
index 883e3c9a9a80886407747432aafe05863e504f98..c1660b3d8d909b18727207d5096b6aa614462932 100644 (file)
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
+#include "screen_outputs.h"
+#include "screen_interface.h"
+#include "screen_message.h"
 #include "i18n.h"
-#include "screen.h"
 #include "list_window.h"
 #include "mpdclient.h"
 
@@ -41,45 +43,46 @@ outputs_repaint(void)
        wrefresh(lw->w);
 }
 
-static int
+static bool
 toggle_output(struct mpdclient *c, unsigned int output_index)
 {
+       struct mpd_connection *connection;
        struct mpd_output *output;
 
        assert(mpd_outputs != NULL);
 
-       if (output_index >= mpd_outputs->len)
-               return -1;
+       if (!mpdclient_is_connected(c) ||
+           output_index >= mpd_outputs->len)
+               return false;
 
+       connection = mpdclient_get_connection(c);
        output = g_ptr_array_index(mpd_outputs, output_index);
 
        if (!mpd_output_get_enabled(output)) {
-               if (!mpd_run_enable_output(c->connection,
+               if (!mpd_run_enable_output(connection,
                                           mpd_output_get_id(output))) {
                        mpdclient_handle_error(c);
-                       return -1;
+                       return false;
                }
 
-               /* XXX reload */
+               c->events |= MPD_IDLE_OUTPUT;
 
                screen_status_printf(_("Output '%s' enabled"),
                                     mpd_output_get_name(output));
        } else {
-               if (!mpd_run_disable_output(c->connection,
+               if (!mpd_run_disable_output(connection,
                                            mpd_output_get_id(output))) {
                        mpdclient_handle_error(c);
-                       return -1;
+                       return false;
                }
 
-               /* XXX reload */
+               c->events |= MPD_IDLE_OUTPUT;
 
                screen_status_printf(_("Output '%s' disabled"),
                                     mpd_output_get_name(output));
        }
 
-       outputs_repaint();
-
-       return 0;
+       return true;
 }
 
 static void
@@ -103,19 +106,21 @@ clear_outputs_list(void)
 static void
 fill_outputs_list(struct mpdclient *c)
 {
+       struct mpd_connection *connection;
        struct mpd_output *output;
 
        assert(mpd_outputs != NULL);
 
-       if (c->connection == NULL)
+       if (!mpdclient_is_connected(c))
                return;
 
-       mpd_send_outputs(c->connection);
-       while ((output = mpd_recv_output(c->connection)) != NULL) {
+       connection = mpdclient_get_connection(c);
+       mpd_send_outputs(connection);
+       while ((output = mpd_recv_output(connection)) != NULL) {
                g_ptr_array_add(mpd_outputs, output);
        }
 
-       if (!mpd_response_finish(c->connection))
+       if (!mpd_response_finish(connection))
                mpdclient_handle_error(c);
 }
 
@@ -185,6 +190,16 @@ outputs_paint(void)
        list_window_paint(lw, outputs_list_callback, NULL);
 }
 
+static void
+screen_outputs_update(struct mpdclient *c)
+{
+       if (c->events & MPD_IDLE_OUTPUT) {
+               clear_outputs_list();
+               fill_outputs_list(c);
+               outputs_repaint();
+       }
+}
+
 static bool
 outputs_cmd(struct mpdclient *c, command_t cmd)
 {
@@ -195,9 +210,19 @@ outputs_cmd(struct mpdclient *c, command_t cmd)
                return true;
        }
 
-       if (cmd == CMD_PLAY) {
+       switch (cmd) {
+       case CMD_PLAY:
                toggle_output(c, lw->selected);
                return true;
+
+       case CMD_SCREEN_UPDATE:
+               clear_outputs_list();
+               fill_outputs_list(c);
+               outputs_repaint();
+               return true;
+
+       default:
+               break;
        }
 
        return false;
@@ -210,6 +235,7 @@ const struct screen_functions screen_outputs = {
        .close     = outputs_close,
        .resize    = outputs_resize,
        .paint     = outputs_paint,
+       .update = screen_outputs_update,
        .cmd       = outputs_cmd,
        .get_title = outputs_title,
 };