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 "i18n.h"
24 #include "list_window.h"
25 #include "mpdclient.h"
27 #include <mpd/client.h>
29 #include <glib.h>
30 #include <assert.h>
32 static list_window_t *lw = NULL;
34 static GPtrArray *mpd_outputs = NULL;
36 static void
37 outputs_paint(void);
39 static void
40 outputs_repaint(void)
41 {
42 outputs_paint();
43 wrefresh(lw->w);
44 }
46 static bool
47 toggle_output(struct mpdclient *c, unsigned int output_index)
48 {
49 struct mpd_output *output;
51 assert(mpd_outputs != NULL);
53 if (output_index >= mpd_outputs->len)
54 return false;
56 output = g_ptr_array_index(mpd_outputs, output_index);
58 if (!mpd_output_get_enabled(output)) {
59 if (!mpd_run_enable_output(c->connection,
60 mpd_output_get_id(output))) {
61 mpdclient_handle_error(c);
62 return false;
63 }
65 c->events |= MPD_IDLE_OUTPUT;
67 screen_status_printf(_("Output '%s' enabled"),
68 mpd_output_get_name(output));
69 } else {
70 if (!mpd_run_disable_output(c->connection,
71 mpd_output_get_id(output))) {
72 mpdclient_handle_error(c);
73 return false;
74 }
76 c->events |= MPD_IDLE_OUTPUT;
78 screen_status_printf(_("Output '%s' disabled"),
79 mpd_output_get_name(output));
80 }
82 return true;
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 void
189 screen_outputs_update(struct mpdclient *c)
190 {
191 if (c->events & MPD_IDLE_OUTPUT) {
192 clear_outputs_list();
193 fill_outputs_list(c);
194 outputs_repaint();
195 }
196 }
198 static bool
199 outputs_cmd(struct mpdclient *c, command_t cmd)
200 {
201 assert(mpd_outputs != NULL);
203 if (list_window_cmd(lw, mpd_outputs->len, cmd)) {
204 outputs_repaint();
205 return true;
206 }
208 switch (cmd) {
209 case CMD_PLAY:
210 toggle_output(c, lw->selected);
211 return true;
213 case CMD_SCREEN_UPDATE:
214 clear_outputs_list();
215 fill_outputs_list(c);
216 outputs_repaint();
217 return true;
219 default:
220 break;
221 }
223 return false;
224 }
226 const struct screen_functions screen_outputs = {
227 .init = outputs_init,
228 .exit = outputs_exit,
229 .open = outputs_open,
230 .close = outputs_close,
231 .resize = outputs_resize,
232 .paint = outputs_paint,
233 .update = screen_outputs_update,
234 .cmd = outputs_cmd,
235 .get_title = outputs_title,
236 };