Code

list_window: added attribute "length"
[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 "i18n.h"
24 #include "list_window.h"
25 #include "mpdclient.h"
27 #include <mpd/client.h>
29 #include <glib.h>
30 #include <assert.h>
32 static struct list_window *lw;
34 static GPtrArray *mpd_outputs = NULL;
36 static void
37 outputs_paint(void);
39 static void
40 outputs_repaint(void)
41 {
42         outputs_paint();
43         wrefresh(lw->w);
44 }
46 static bool
47 toggle_output(struct mpdclient *c, unsigned int output_index)
48 {
49         struct mpd_connection *connection;
50         struct mpd_output *output;
52         assert(mpd_outputs != NULL);
54         if (!mpdclient_is_connected(c) ||
55             output_index >= mpd_outputs->len)
56                 return false;
58         connection = mpdclient_get_connection(c);
59         output = g_ptr_array_index(mpd_outputs, output_index);
61         if (!mpd_output_get_enabled(output)) {
62                 if (!mpd_run_enable_output(connection,
63                                            mpd_output_get_id(output))) {
64                         mpdclient_handle_error(c);
65                         return false;
66                 }
68                 c->events |= MPD_IDLE_OUTPUT;
70                 screen_status_printf(_("Output '%s' enabled"),
71                                      mpd_output_get_name(output));
72         } else {
73                 if (!mpd_run_disable_output(connection,
74                                             mpd_output_get_id(output))) {
75                         mpdclient_handle_error(c);
76                         return false;
77                 }
79                 c->events |= MPD_IDLE_OUTPUT;
81                 screen_status_printf(_("Output '%s' disabled"),
82                                      mpd_output_get_name(output));
83         }
85         return true;
86 }
88 static void
89 clear_output_element(gpointer data, G_GNUC_UNUSED gpointer user_data)
90 {
91         mpd_output_free(data);
92 }
94 static void
95 clear_outputs_list(void)
96 {
97         assert(mpd_outputs != NULL);
99         if (mpd_outputs->len <= 0)
100                 return;
102         g_ptr_array_foreach(mpd_outputs, clear_output_element, NULL);
103         g_ptr_array_remove_range(mpd_outputs, 0, mpd_outputs->len);
106 static void
107 fill_outputs_list(struct mpdclient *c)
109         struct mpd_connection *connection;
110         struct mpd_output *output;
112         assert(mpd_outputs != NULL);
114         if (!mpdclient_is_connected(c))
115                 return;
117         connection = mpdclient_get_connection(c);
118         mpd_send_outputs(connection);
119         while ((output = mpd_recv_output(connection)) != NULL) {
120                 g_ptr_array_add(mpd_outputs, output);
121         }
123         if (!mpd_response_finish(connection))
124                 mpdclient_handle_error(c);
126         list_window_set_length(lw, mpd_outputs->len);
129 static const char *
130 outputs_list_callback(unsigned int output_index, bool *highlight,
131                       G_GNUC_UNUSED char **sc, G_GNUC_UNUSED void *data)
133         struct mpd_output *output;
135         assert(mpd_outputs != NULL);
137         if (output_index >= mpd_outputs->len)
138                 return NULL;
140         output = g_ptr_array_index(mpd_outputs, output_index);
142         if (mpd_output_get_enabled(output))
143                 *highlight = true;
145         return mpd_output_get_name(output);
148 static void
149 outputs_init(WINDOW *w, int cols, int rows)
151         lw = list_window_init(w, cols, rows);
153         mpd_outputs = g_ptr_array_new();
156 static void
157 outputs_resize(int cols, int rows)
159         lw->cols = cols;
160         lw->rows = rows;
163 static void
164 outputs_exit(void)
166         list_window_free(lw);
168         g_ptr_array_free(mpd_outputs, TRUE);
171 static void
172 outputs_open(struct mpdclient *c)
174         fill_outputs_list(c);
177 static void
178 outputs_close(void)
180         clear_outputs_list();
183 static const char *
184 outputs_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
186         return _("Outputs");
189 static void
190 outputs_paint(void)
192         list_window_paint(lw, outputs_list_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 };