Code

4a5b2f4cdaf4d9a24d2396ff26a6d0e59f6271ed
[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);
107         list_window_set_length(lw, 0);
110 static void
111 fill_outputs_list(struct mpdclient *c)
113         struct mpd_connection *connection;
114         struct mpd_output *output;
116         assert(mpd_outputs != NULL);
118         connection = mpdclient_get_connection(c);
119         if (connection == NULL)
120                 return;
122         mpd_send_outputs(connection);
123         while ((output = mpd_recv_output(connection)) != NULL) {
124                 g_ptr_array_add(mpd_outputs, output);
125         }
127         if (!mpd_response_finish(connection))
128                 mpdclient_handle_error(c);
130         list_window_set_length(lw, mpd_outputs->len);
133 static void
134 outputs_init(WINDOW *w, int cols, int rows)
136         lw = list_window_init(w, cols, rows);
138         mpd_outputs = g_ptr_array_new();
141 static void
142 outputs_resize(int cols, int rows)
144         list_window_resize(lw, cols, rows);
147 static void
148 outputs_exit(void)
150         list_window_free(lw);
152         g_ptr_array_free(mpd_outputs, TRUE);
155 static void
156 outputs_open(struct mpdclient *c)
158         fill_outputs_list(c);
161 static void
162 outputs_close(void)
164         clear_outputs_list();
167 static const char *
168 outputs_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
170         return _("Outputs");
173 static void
174 screen_outputs_paint_callback(WINDOW *w, unsigned i,
175                               G_GNUC_UNUSED unsigned y, unsigned width,
176                               bool selected, G_GNUC_UNUSED void *data)
178         const struct mpd_output *output;
180         assert(mpd_outputs != NULL);
181         assert(i < mpd_outputs->len);
183         output = g_ptr_array_index(mpd_outputs, i);
185         row_color(w, COLOR_LIST, selected);
186         waddstr(w, mpd_output_get_enabled(output) ? "[X] " : "[ ] ");
187         waddstr(w, mpd_output_get_name(output));
188         row_clear_to_eol(w, width, selected);
191 static void
192 outputs_paint(void)
194         list_window_paint2(lw, screen_outputs_paint_callback, NULL);
197 static void
198 screen_outputs_update(struct mpdclient *c)
200         if (c->events & MPD_IDLE_OUTPUT) {
201                 clear_outputs_list();
202                 fill_outputs_list(c);
203                 outputs_repaint();
204         }
207 static bool
208 outputs_cmd(struct mpdclient *c, command_t cmd)
210         assert(mpd_outputs != NULL);
212         if (list_window_cmd(lw, cmd)) {
213                 outputs_repaint();
214                 return true;
215         }
217         switch (cmd) {
218         case CMD_PLAY:
219                 toggle_output(c, lw->selected);
220                 return true;
222         case CMD_SCREEN_UPDATE:
223                 clear_outputs_list();
224                 fill_outputs_list(c);
225                 outputs_repaint();
226                 return true;
228         default:
229                 break;
230         }
232         return false;
235 const struct screen_functions screen_outputs = {
236         .init      = outputs_init,
237         .exit      = outputs_exit,
238         .open      = outputs_open,
239         .close     = outputs_close,
240         .resize    = outputs_resize,
241         .paint     = outputs_paint,
242         .update = screen_outputs_update,
243         .cmd       = outputs_cmd,
244         .get_title = outputs_title,
245 };