Code

screen_outputs: reset list_window length in clear_outputs_list()
[ncmpc.git] / src / screen_outputs.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "screen_outputs.h"
21 #include "screen_interface.h"
22 #include "screen_message.h"
23 #include "paint.h"
24 #include "i18n.h"
25 #include "list_window.h"
26 #include "mpdclient.h"
28 #include <mpd/client.h>
30 #include <glib.h>
31 #include <assert.h>
33 static struct list_window *lw;
35 static GPtrArray *mpd_outputs = NULL;
37 static void
38 outputs_paint(void);
40 static void
41 outputs_repaint(void)
42 {
43         outputs_paint();
44         wrefresh(lw->w);
45 }
47 static bool
48 toggle_output(struct mpdclient *c, unsigned int output_index)
49 {
50         struct mpd_connection *connection;
51         struct mpd_output *output;
53         assert(mpd_outputs != NULL);
55         if (!mpdclient_is_connected(c) ||
56             output_index >= mpd_outputs->len)
57                 return false;
59         connection = mpdclient_get_connection(c);
60         output = g_ptr_array_index(mpd_outputs, output_index);
62         if (!mpd_output_get_enabled(output)) {
63                 if (!mpd_run_enable_output(connection,
64                                            mpd_output_get_id(output))) {
65                         mpdclient_handle_error(c);
66                         return false;
67                 }
69                 c->events |= MPD_IDLE_OUTPUT;
71                 screen_status_printf(_("Output '%s' enabled"),
72                                      mpd_output_get_name(output));
73         } else {
74                 if (!mpd_run_disable_output(connection,
75                                             mpd_output_get_id(output))) {
76                         mpdclient_handle_error(c);
77                         return false;
78                 }
80                 c->events |= MPD_IDLE_OUTPUT;
82                 screen_status_printf(_("Output '%s' disabled"),
83                                      mpd_output_get_name(output));
84         }
86         return true;
87 }
89 static void
90 clear_output_element(gpointer data, G_GNUC_UNUSED gpointer user_data)
91 {
92         mpd_output_free(data);
93 }
95 static void
96 clear_outputs_list(void)
97 {
98         assert(mpd_outputs != NULL);
100         if (mpd_outputs->len <= 0)
101                 return;
103         g_ptr_array_foreach(mpd_outputs, clear_output_element, NULL);
104         g_ptr_array_remove_range(mpd_outputs, 0, mpd_outputs->len);
105         list_window_set_length(lw, 0);
108 static void
109 fill_outputs_list(struct mpdclient *c)
111         struct mpd_connection *connection;
112         struct mpd_output *output;
114         assert(mpd_outputs != NULL);
116         if (!mpdclient_is_connected(c))
117                 return;
119         connection = mpdclient_get_connection(c);
120         mpd_send_outputs(connection);
121         while ((output = mpd_recv_output(connection)) != NULL) {
122                 g_ptr_array_add(mpd_outputs, output);
123         }
125         if (!mpd_response_finish(connection))
126                 mpdclient_handle_error(c);
128         list_window_set_length(lw, mpd_outputs->len);
131 static void
132 outputs_init(WINDOW *w, int cols, int rows)
134         lw = list_window_init(w, cols, rows);
136         mpd_outputs = g_ptr_array_new();
139 static void
140 outputs_resize(int cols, int rows)
142         list_window_resize(lw, cols, rows);
145 static void
146 outputs_exit(void)
148         list_window_free(lw);
150         g_ptr_array_free(mpd_outputs, TRUE);
153 static void
154 outputs_open(struct mpdclient *c)
156         fill_outputs_list(c);
159 static void
160 outputs_close(void)
162         clear_outputs_list();
165 static const char *
166 outputs_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
168         return _("Outputs");
171 static void
172 screen_outputs_paint_callback(WINDOW *w, unsigned i,
173                               G_GNUC_UNUSED unsigned y, unsigned width,
174                               bool selected, G_GNUC_UNUSED void *data)
176         const struct mpd_output *output;
178         assert(mpd_outputs != NULL);
179         assert(i < mpd_outputs->len);
181         output = g_ptr_array_index(mpd_outputs, i);
183         row_color(w, COLOR_LIST, selected);
184         waddstr(w, mpd_output_get_enabled(output) ? "[X] " : "[ ] ");
185         waddstr(w, mpd_output_get_name(output));
186         row_clear_to_eol(w, width, selected);
189 static void
190 outputs_paint(void)
192         list_window_paint2(lw, screen_outputs_paint_callback, NULL);
195 static void
196 screen_outputs_update(struct mpdclient *c)
198         if (c->events & MPD_IDLE_OUTPUT) {
199                 clear_outputs_list();
200                 fill_outputs_list(c);
201                 outputs_repaint();
202         }
205 static bool
206 outputs_cmd(struct mpdclient *c, command_t cmd)
208         assert(mpd_outputs != NULL);
210         if (list_window_cmd(lw, cmd)) {
211                 outputs_repaint();
212                 return true;
213         }
215         switch (cmd) {
216         case CMD_PLAY:
217                 toggle_output(c, lw->selected);
218                 return true;
220         case CMD_SCREEN_UPDATE:
221                 clear_outputs_list();
222                 fill_outputs_list(c);
223                 outputs_repaint();
224                 return true;
226         default:
227                 break;
228         }
230         return false;
233 const struct screen_functions screen_outputs = {
234         .init      = outputs_init,
235         .exit      = outputs_exit,
236         .open      = outputs_open,
237         .close     = outputs_close,
238         .resize    = outputs_resize,
239         .paint     = outputs_paint,
240         .update = screen_outputs_update,
241         .cmd       = outputs_cmd,
242         .get_title = outputs_title,
243 };