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 }
117 }
119 static const char *
120 outputs_list_callback(unsigned int output_index, bool *highlight,
121 G_GNUC_UNUSED char **sc, G_GNUC_UNUSED void *data)
122 {
123 struct mpd_output *output;
125 assert(mpd_outputs != NULL);
127 if (output_index >= mpd_outputs->len)
128 return NULL;
130 output = g_ptr_array_index(mpd_outputs, output_index);
132 if (mpd_output_get_enabled(output))
133 *highlight = true;
135 return mpd_output_get_name(output);
136 }
138 static void
139 outputs_init(WINDOW *w, int cols, int rows)
140 {
141 lw = list_window_init(w, cols, rows);
143 mpd_outputs = g_ptr_array_new();
144 }
146 static void
147 outputs_resize(int cols, int rows)
148 {
149 lw->cols = cols;
150 lw->rows = rows;
151 }
153 static void
154 outputs_exit(void)
155 {
156 list_window_free(lw);
158 g_ptr_array_free(mpd_outputs, TRUE);
159 }
161 static void
162 outputs_open(struct mpdclient *c)
163 {
164 fill_outputs_list(c);
165 }
167 static void
168 outputs_close(void)
169 {
170 clear_outputs_list();
171 }
173 static const char *
174 outputs_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
175 {
176 return _("Outputs");
177 }
179 static void
180 outputs_paint(void)
181 {
182 list_window_paint(lw, outputs_list_callback, NULL);
183 }
185 static bool
186 outputs_cmd(struct mpdclient *c, command_t cmd)
187 {
188 assert(mpd_outputs != NULL);
190 if (list_window_cmd(lw, mpd_outputs->len, cmd)) {
191 outputs_repaint();
192 return true;
193 }
195 if (cmd == CMD_PLAY) {
196 toggle_output(c, lw->selected);
197 return true;
198 }
200 return false;
201 }
203 const struct screen_functions screen_outputs = {
204 .init = outputs_init,
205 .exit = outputs_exit,
206 .open = outputs_open,
207 .close = outputs_close,
208 .resize = outputs_resize,
209 .paint = outputs_paint,
210 .cmd = outputs_cmd,
211 .get_title = outputs_title,
212 };