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 "i18n.h"
21 #include "screen.h"
22 #include "list_window.h"
23 #include "mpdclient.h"
25 #include <mpd/client.h>
27 #include <glib.h>
28 #include <assert.h>
30 static list_window_t *lw = NULL;
32 static GPtrArray *mpd_outputs = NULL;
34 static void
35 outputs_paint(void);
37 static void
38 outputs_repaint(void)
39 {
40 outputs_paint();
41 wrefresh(lw->w);
42 }
44 static int
45 toggle_output(struct mpdclient *c, unsigned int output_index)
46 {
47 struct mpd_output *output;
49 assert(mpd_outputs != NULL);
51 if (output_index >= mpd_outputs->len)
52 return -1;
54 output = g_ptr_array_index(mpd_outputs, output_index);
56 if (!mpd_output_get_enabled(output)) {
57 if (!mpd_run_enable_output(c->connection,
58 mpd_output_get_id(output))) {
59 mpdclient_handle_error(c);
60 return -1;
61 }
63 /* XXX reload */
65 screen_status_printf(_("Output '%s' enabled"),
66 mpd_output_get_name(output));
67 } else {
68 if (!mpd_run_disable_output(c->connection,
69 mpd_output_get_id(output))) {
70 mpdclient_handle_error(c);
71 return -1;
72 }
74 /* XXX reload */
76 screen_status_printf(_("Output '%s' disabled"),
77 mpd_output_get_name(output));
78 }
80 outputs_repaint();
82 return 0;
83 }
85 static void
86 clear_output_element(gpointer data, G_GNUC_UNUSED gpointer user_data)
87 {
88 mpd_output_free(data);
89 }
91 static void
92 clear_outputs_list(void)
93 {
94 assert(mpd_outputs != NULL);
96 if (mpd_outputs->len <= 0)
97 return;
99 g_ptr_array_foreach(mpd_outputs, clear_output_element, NULL);
100 g_ptr_array_remove_range(mpd_outputs, 0, mpd_outputs->len);
101 }
103 static void
104 fill_outputs_list(struct mpdclient *c)
105 {
106 struct mpd_output *output;
108 assert(mpd_outputs != NULL);
110 if (c->connection == NULL)
111 return;
113 mpd_send_outputs(c->connection);
114 while ((output = mpd_recv_output(c->connection)) != NULL) {
115 g_ptr_array_add(mpd_outputs, output);
116 }
118 if (!mpd_response_finish(c->connection))
119 mpdclient_handle_error(c);
120 }
122 static const char *
123 outputs_list_callback(unsigned int output_index, bool *highlight,
124 G_GNUC_UNUSED char **sc, G_GNUC_UNUSED void *data)
125 {
126 struct mpd_output *output;
128 assert(mpd_outputs != NULL);
130 if (output_index >= mpd_outputs->len)
131 return NULL;
133 output = g_ptr_array_index(mpd_outputs, output_index);
135 if (mpd_output_get_enabled(output))
136 *highlight = true;
138 return mpd_output_get_name(output);
139 }
141 static void
142 outputs_init(WINDOW *w, int cols, int rows)
143 {
144 lw = list_window_init(w, cols, rows);
146 mpd_outputs = g_ptr_array_new();
147 }
149 static void
150 outputs_resize(int cols, int rows)
151 {
152 lw->cols = cols;
153 lw->rows = rows;
154 }
156 static void
157 outputs_exit(void)
158 {
159 list_window_free(lw);
161 g_ptr_array_free(mpd_outputs, TRUE);
162 }
164 static void
165 outputs_open(struct mpdclient *c)
166 {
167 fill_outputs_list(c);
168 }
170 static void
171 outputs_close(void)
172 {
173 clear_outputs_list();
174 }
176 static const char *
177 outputs_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
178 {
179 return _("Outputs");
180 }
182 static void
183 outputs_paint(void)
184 {
185 list_window_paint(lw, outputs_list_callback, NULL);
186 }
188 static bool
189 outputs_cmd(struct mpdclient *c, command_t cmd)
190 {
191 assert(mpd_outputs != NULL);
193 if (list_window_cmd(lw, mpd_outputs->len, cmd)) {
194 outputs_repaint();
195 return true;
196 }
198 if (cmd == CMD_PLAY) {
199 toggle_output(c, lw->selected);
200 return true;
201 }
203 return false;
204 }
206 const struct screen_functions screen_outputs = {
207 .init = outputs_init,
208 .exit = outputs_exit,
209 .open = outputs_open,
210 .close = outputs_close,
211 .resize = outputs_resize,
212 .paint = outputs_paint,
213 .cmd = outputs_cmd,
214 .get_title = outputs_title,
215 };