Code

screen_queue: update current song in method open()
[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 (output_index >= mpd_outputs->len)
56                 return false;
58         connection = mpdclient_get_connection(c);
59         if (connection == NULL)
60                 return false;
62         output = g_ptr_array_index(mpd_outputs, output_index);
64         if (!mpd_output_get_enabled(output)) {
65                 if (!mpd_run_enable_output(connection,
66                                            mpd_output_get_id(output))) {
67                         mpdclient_handle_error(c);
68                         return false;
69                 }
71                 c->events |= MPD_IDLE_OUTPUT;
73                 screen_status_printf(_("Output '%s' enabled"),
74                                      mpd_output_get_name(output));
75         } else {
76                 if (!mpd_run_disable_output(connection,
77                                             mpd_output_get_id(output))) {
78                         mpdclient_handle_error(c);
79                         return false;
80                 }
82                 c->events |= MPD_IDLE_OUTPUT;
84                 screen_status_printf(_("Output '%s' disabled"),
85                                      mpd_output_get_name(output));
86         }
88         return true;
89 }
91 static void
92 clear_output_element(gpointer data, G_GNUC_UNUSED gpointer user_data)
93 {
94         mpd_output_free(data);
95 }
97 static void
98 clear_outputs_list(void)
99 {
100         assert(mpd_outputs != NULL);
102         if (mpd_outputs->len <= 0)
103                 return;
105         g_ptr_array_foreach(mpd_outputs, clear_output_element, NULL);
106         g_ptr_array_remove_range(mpd_outputs, 0, mpd_outputs->len);
108         /* not updating the list_window length here, because that
109            would clear the cursor position, and fill_outputs_list()
110            will be called after this function anyway */
111         /* list_window_set_length(lw, 0); */
114 static void
115 fill_outputs_list(struct mpdclient *c)
117         struct mpd_connection *connection;
118         struct mpd_output *output;
120         assert(mpd_outputs != NULL);
122         connection = mpdclient_get_connection(c);
123         if (connection == NULL)
124                 return;
126         mpd_send_outputs(connection);
127         while ((output = mpd_recv_output(connection)) != NULL) {
128                 g_ptr_array_add(mpd_outputs, output);
129         }
131         if (!mpd_response_finish(connection))
132                 mpdclient_handle_error(c);
134         list_window_set_length(lw, mpd_outputs->len);
137 static void
138 outputs_init(WINDOW *w, int cols, int rows)
140         lw = list_window_init(w, cols, rows);
142         mpd_outputs = g_ptr_array_new();
145 static void
146 outputs_resize(int cols, int rows)
148         list_window_resize(lw, cols, rows);
151 static void
152 outputs_exit(void)
154         list_window_free(lw);
156         g_ptr_array_free(mpd_outputs, TRUE);
159 static void
160 outputs_open(struct mpdclient *c)
162         fill_outputs_list(c);
165 static void
166 outputs_close(void)
168         clear_outputs_list();
171 static const char *
172 outputs_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
174         return _("Outputs");
177 static void
178 screen_outputs_paint_callback(WINDOW *w, unsigned i,
179                               G_GNUC_UNUSED unsigned y, unsigned width,
180                               bool selected, G_GNUC_UNUSED void *data)
182         const struct mpd_output *output;
184         assert(mpd_outputs != NULL);
185         assert(i < mpd_outputs->len);
187         output = g_ptr_array_index(mpd_outputs, i);
189         row_color(w, COLOR_LIST, selected);
190         waddstr(w, mpd_output_get_enabled(output) ? "[X] " : "[ ] ");
191         waddstr(w, mpd_output_get_name(output));
192         row_clear_to_eol(w, width, selected);
195 static void
196 outputs_paint(void)
198         list_window_paint2(lw, screen_outputs_paint_callback, NULL);
201 static void
202 screen_outputs_update(struct mpdclient *c)
204         if (c->events & MPD_IDLE_OUTPUT) {
205                 clear_outputs_list();
206                 fill_outputs_list(c);
207                 outputs_repaint();
208         }
211 static bool
212 outputs_cmd(struct mpdclient *c, command_t cmd)
214         assert(mpd_outputs != NULL);
216         if (list_window_cmd(lw, cmd)) {
217                 outputs_repaint();
218                 return true;
219         }
221         switch (cmd) {
222         case CMD_PLAY:
223                 toggle_output(c, lw->selected);
224                 return true;
226         case CMD_SCREEN_UPDATE:
227                 clear_outputs_list();
228                 fill_outputs_list(c);
229                 outputs_repaint();
230                 return true;
232         default:
233                 break;
234         }
236         return false;
239 const struct screen_functions screen_outputs = {
240         .init      = outputs_init,
241         .exit      = outputs_exit,
242         .open      = outputs_open,
243         .close     = outputs_close,
244         .resize    = outputs_resize,
245         .paint     = outputs_paint,
246         .update = screen_outputs_update,
247         .cmd       = outputs_cmd,
248         .get_title = outputs_title,
249 };