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 int return_value;
48 struct mpd_output *output;
50 assert(mpd_outputs != NULL);
52 if (output_index >= mpd_outputs->len)
53 return -1;
55 output = g_ptr_array_index(mpd_outputs, output_index);
57 if (!mpd_output_get_enabled(output)) {
58 mpd_send_enable_output(c->connection,
59 mpd_output_get_id(output));
61 /* XXX reload */
63 screen_status_printf(_("Output '%s' enabled"),
64 mpd_output_get_name(output));
65 } else {
66 mpd_send_disable_output(c->connection,
67 mpd_output_get_id(output));
69 /* XXX reload */
71 screen_status_printf(_("Output '%s' disabled"),
72 mpd_output_get_name(output));
73 }
75 return_value = mpdclient_finish_command(c);
77 outputs_repaint();
79 return return_value;
80 }
82 static void
83 clear_output_element(gpointer data, G_GNUC_UNUSED gpointer user_data)
84 {
85 mpd_output_free(data);
86 }
88 static void
89 clear_outputs_list(void)
90 {
91 assert(mpd_outputs != NULL);
93 if (mpd_outputs->len <= 0)
94 return;
96 g_ptr_array_foreach(mpd_outputs, clear_output_element, NULL);
97 g_ptr_array_remove_range(mpd_outputs, 0, mpd_outputs->len);
98 }
100 static void
101 fill_outputs_list(struct mpdclient *c)
102 {
103 struct mpd_output *output;
105 assert(mpd_outputs != NULL);
107 if (c->connection == NULL)
108 return;
110 mpd_send_outputs(c->connection);
111 while ((output = mpd_recv_output(c->connection)) != NULL) {
112 g_ptr_array_add(mpd_outputs, output);
113 }
114 }
116 static const char *
117 outputs_list_callback(unsigned int output_index, bool *highlight,
118 G_GNUC_UNUSED char **sc, G_GNUC_UNUSED void *data)
119 {
120 struct mpd_output *output;
122 assert(mpd_outputs != NULL);
124 if (output_index >= mpd_outputs->len)
125 return NULL;
127 output = g_ptr_array_index(mpd_outputs, output_index);
129 if (mpd_output_get_enabled(output))
130 *highlight = true;
132 return mpd_output_get_name(output);
133 }
135 static void
136 outputs_init(WINDOW *w, int cols, int rows)
137 {
138 lw = list_window_init(w, cols, rows);
140 mpd_outputs = g_ptr_array_new();
141 }
143 static void
144 outputs_resize(int cols, int rows)
145 {
146 lw->cols = cols;
147 lw->rows = rows;
148 }
150 static void
151 outputs_exit(void)
152 {
153 list_window_free(lw);
155 g_ptr_array_free(mpd_outputs, TRUE);
156 }
158 static void
159 outputs_open(struct mpdclient *c)
160 {
161 fill_outputs_list(c);
162 }
164 static void
165 outputs_close(void)
166 {
167 clear_outputs_list();
168 }
170 static const char *
171 outputs_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
172 {
173 return _("Outputs");
174 }
176 static void
177 outputs_paint(void)
178 {
179 list_window_paint(lw, outputs_list_callback, NULL);
180 }
182 static bool
183 outputs_cmd(struct mpdclient *c, command_t cmd)
184 {
185 assert(mpd_outputs != NULL);
187 if (list_window_cmd(lw, mpd_outputs->len, cmd)) {
188 outputs_repaint();
189 return true;
190 }
192 if (cmd == CMD_PLAY) {
193 toggle_output(c, lw->selected);
194 return true;
195 }
197 return false;
198 }
200 const struct screen_functions screen_outputs = {
201 .init = outputs_init,
202 .exit = outputs_exit,
203 .open = outputs_open,
204 .close = outputs_close,
205 .resize = outputs_resize,
206 .paint = outputs_paint,
207 .cmd = outputs_cmd,
208 .get_title = outputs_title,
209 };