Code

Merge remote branches 'jn/cosmetics', 'jn/doxygen' and 'jn/renames'
[ncmpc.git] / src / screen_outputs.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2010 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  *
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.
9  *
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.
14  *
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_status.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                 list_window_set_length(lw, 0);
125                 return;
126         }
128         mpd_send_outputs(connection);
129         while ((output = mpd_recv_output(connection)) != NULL) {
130                 g_ptr_array_add(mpd_outputs, output);
131         }
133         mpdclient_finish_command(c);
135         list_window_set_length(lw, mpd_outputs->len);
138 static void
139 outputs_init(WINDOW *w, int cols, int rows)
141         lw = list_window_init(w, cols, rows);
143         mpd_outputs = g_ptr_array_new();
146 static void
147 outputs_resize(int cols, int rows)
149         list_window_resize(lw, cols, rows);
152 static void
153 outputs_exit(void)
155         list_window_free(lw);
157         g_ptr_array_free(mpd_outputs, TRUE);
160 static void
161 outputs_open(struct mpdclient *c)
163         fill_outputs_list(c);
166 static void
167 outputs_close(void)
169         clear_outputs_list();
172 static const char *
173 outputs_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
175         return _("Outputs");
178 static void
179 screen_outputs_paint_callback(WINDOW *w, unsigned i,
180                               G_GNUC_UNUSED unsigned y, unsigned width,
181                               bool selected, G_GNUC_UNUSED void *data)
183         const struct mpd_output *output;
185         assert(mpd_outputs != NULL);
186         assert(i < mpd_outputs->len);
188         output = g_ptr_array_index(mpd_outputs, i);
190         row_color(w, COLOR_LIST, selected);
191         waddstr(w, mpd_output_get_enabled(output) ? "[X] " : "[ ] ");
192         waddstr(w, mpd_output_get_name(output));
193         row_clear_to_eol(w, width, selected);
196 static void
197 outputs_paint(void)
199         list_window_paint2(lw, screen_outputs_paint_callback, NULL);
202 static void
203 screen_outputs_update(struct mpdclient *c)
205         if (c->events & MPD_IDLE_OUTPUT) {
206                 clear_outputs_list();
207                 fill_outputs_list(c);
208                 outputs_repaint();
209         }
212 static bool
213 outputs_cmd(struct mpdclient *c, command_t cmd)
215         assert(mpd_outputs != NULL);
217         if (list_window_cmd(lw, cmd)) {
218                 outputs_repaint();
219                 return true;
220         }
222         switch (cmd) {
223         case CMD_PLAY:
224                 toggle_output(c, lw->selected);
225                 return true;
227         case CMD_SCREEN_UPDATE:
228                 clear_outputs_list();
229                 fill_outputs_list(c);
230                 outputs_repaint();
231                 return true;
233         default:
234                 break;
235         }
237         return false;
240 const struct screen_functions screen_outputs = {
241         .init      = outputs_init,
242         .exit      = outputs_exit,
243         .open      = outputs_open,
244         .close     = outputs_close,
245         .resize    = outputs_resize,
246         .paint     = outputs_paint,
247         .update    = screen_outputs_update,
248         .cmd       = outputs_cmd,
249         .get_title = outputs_title,
250 };