Code

screen_outputs: paint "enabled" checkbox
[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);
107 static void
108 fill_outputs_list(struct mpdclient *c)
110         struct mpd_connection *connection;
111         struct mpd_output *output;
113         assert(mpd_outputs != NULL);
115         if (!mpdclient_is_connected(c))
116                 return;
118         connection = mpdclient_get_connection(c);
119         mpd_send_outputs(connection);
120         while ((output = mpd_recv_output(connection)) != NULL) {
121                 g_ptr_array_add(mpd_outputs, output);
122         }
124         if (!mpd_response_finish(connection))
125                 mpdclient_handle_error(c);
127         list_window_set_length(lw, mpd_outputs->len);
130 static void
131 outputs_init(WINDOW *w, int cols, int rows)
133         lw = list_window_init(w, cols, rows);
135         mpd_outputs = g_ptr_array_new();
138 static void
139 outputs_resize(int cols, int rows)
141         list_window_resize(lw, cols, rows);
144 static void
145 outputs_exit(void)
147         list_window_free(lw);
149         g_ptr_array_free(mpd_outputs, TRUE);
152 static void
153 outputs_open(struct mpdclient *c)
155         fill_outputs_list(c);
158 static void
159 outputs_close(void)
161         clear_outputs_list();
164 static const char *
165 outputs_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
167         return _("Outputs");
170 static void
171 screen_outputs_paint_callback(WINDOW *w, unsigned i,
172                               G_GNUC_UNUSED unsigned y, unsigned width,
173                               bool selected, G_GNUC_UNUSED void *data)
175         const struct mpd_output *output;
177         assert(mpd_outputs != NULL);
178         assert(i < mpd_outputs->len);
180         output = g_ptr_array_index(mpd_outputs, i);
182         row_color(w, COLOR_LIST, selected);
183         waddstr(w, mpd_output_get_enabled(output) ? "[X] " : "[ ] ");
184         waddstr(w, mpd_output_get_name(output));
185         row_clear_to_eol(w, width, selected);
188 static void
189 outputs_paint(void)
191         list_window_paint2(lw, screen_outputs_paint_callback, NULL);
194 static void
195 screen_outputs_update(struct mpdclient *c)
197         if (c->events & MPD_IDLE_OUTPUT) {
198                 clear_outputs_list();
199                 fill_outputs_list(c);
200                 outputs_repaint();
201         }
204 static bool
205 outputs_cmd(struct mpdclient *c, command_t cmd)
207         assert(mpd_outputs != NULL);
209         if (list_window_cmd(lw, cmd)) {
210                 outputs_repaint();
211                 return true;
212         }
214         switch (cmd) {
215         case CMD_PLAY:
216                 toggle_output(c, lw->selected);
217                 return true;
219         case CMD_SCREEN_UPDATE:
220                 clear_outputs_list();
221                 fill_outputs_list(c);
222                 outputs_repaint();
223                 return true;
225         default:
226                 break;
227         }
229         return false;
232 const struct screen_functions screen_outputs = {
233         .init      = outputs_init,
234         .exit      = outputs_exit,
235         .open      = outputs_open,
236         .close     = outputs_close,
237         .resize    = outputs_resize,
238         .paint     = outputs_paint,
239         .update = screen_outputs_update,
240         .cmd       = outputs_cmd,
241         .get_title = outputs_title,
242 };