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 struct list_window *lw;
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_connection *connection;
50 struct mpd_output *output;
52 assert(mpd_outputs != NULL);
54 if (!mpdclient_is_connected(c) ||
55 output_index >= mpd_outputs->len)
56 return false;
58 connection = mpdclient_get_connection(c);
59 output = g_ptr_array_index(mpd_outputs, output_index);
61 if (!mpd_output_get_enabled(output)) {
62 if (!mpd_run_enable_output(connection,
63 mpd_output_get_id(output))) {
64 mpdclient_handle_error(c);
65 return false;
66 }
68 c->events |= MPD_IDLE_OUTPUT;
70 screen_status_printf(_("Output '%s' enabled"),
71 mpd_output_get_name(output));
72 } else {
73 if (!mpd_run_disable_output(connection,
74 mpd_output_get_id(output))) {
75 mpdclient_handle_error(c);
76 return false;
77 }
79 c->events |= MPD_IDLE_OUTPUT;
81 screen_status_printf(_("Output '%s' disabled"),
82 mpd_output_get_name(output));
83 }
85 return true;
86 }
88 static void
89 clear_output_element(gpointer data, G_GNUC_UNUSED gpointer user_data)
90 {
91 mpd_output_free(data);
92 }
94 static void
95 clear_outputs_list(void)
96 {
97 assert(mpd_outputs != NULL);
99 if (mpd_outputs->len <= 0)
100 return;
102 g_ptr_array_foreach(mpd_outputs, clear_output_element, NULL);
103 g_ptr_array_remove_range(mpd_outputs, 0, mpd_outputs->len);
104 }
106 static void
107 fill_outputs_list(struct mpdclient *c)
108 {
109 struct mpd_connection *connection;
110 struct mpd_output *output;
112 assert(mpd_outputs != NULL);
114 if (!mpdclient_is_connected(c))
115 return;
117 connection = mpdclient_get_connection(c);
118 mpd_send_outputs(connection);
119 while ((output = mpd_recv_output(connection)) != NULL) {
120 g_ptr_array_add(mpd_outputs, output);
121 }
123 if (!mpd_response_finish(connection))
124 mpdclient_handle_error(c);
126 list_window_set_length(lw, mpd_outputs->len);
127 }
129 static const char *
130 outputs_list_callback(unsigned int output_index, bool *highlight,
131 G_GNUC_UNUSED char **sc, G_GNUC_UNUSED void *data)
132 {
133 struct mpd_output *output;
135 assert(mpd_outputs != NULL);
136 assert(output_index < mpd_outputs->len);
138 output = g_ptr_array_index(mpd_outputs, output_index);
140 if (mpd_output_get_enabled(output))
141 *highlight = true;
143 return mpd_output_get_name(output);
144 }
146 static void
147 outputs_init(WINDOW *w, int cols, int rows)
148 {
149 lw = list_window_init(w, cols, rows);
151 mpd_outputs = g_ptr_array_new();
152 }
154 static void
155 outputs_resize(int cols, int rows)
156 {
157 list_window_resize(lw, cols, rows);
158 }
160 static void
161 outputs_exit(void)
162 {
163 list_window_free(lw);
165 g_ptr_array_free(mpd_outputs, TRUE);
166 }
168 static void
169 outputs_open(struct mpdclient *c)
170 {
171 fill_outputs_list(c);
172 }
174 static void
175 outputs_close(void)
176 {
177 clear_outputs_list();
178 }
180 static const char *
181 outputs_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
182 {
183 return _("Outputs");
184 }
186 static void
187 outputs_paint(void)
188 {
189 list_window_paint(lw, outputs_list_callback, NULL);
190 }
192 static void
193 screen_outputs_update(struct mpdclient *c)
194 {
195 if (c->events & MPD_IDLE_OUTPUT) {
196 clear_outputs_list();
197 fill_outputs_list(c);
198 outputs_repaint();
199 }
200 }
202 static bool
203 outputs_cmd(struct mpdclient *c, command_t cmd)
204 {
205 assert(mpd_outputs != NULL);
207 if (list_window_cmd(lw, cmd)) {
208 outputs_repaint();
209 return true;
210 }
212 switch (cmd) {
213 case CMD_PLAY:
214 toggle_output(c, lw->selected);
215 return true;
217 case CMD_SCREEN_UPDATE:
218 clear_outputs_list();
219 fill_outputs_list(c);
220 outputs_repaint();
221 return true;
223 default:
224 break;
225 }
227 return false;
228 }
230 const struct screen_functions screen_outputs = {
231 .init = outputs_init,
232 .exit = outputs_exit,
233 .open = outputs_open,
234 .close = outputs_close,
235 .resize = outputs_resize,
236 .paint = outputs_paint,
237 .update = screen_outputs_update,
238 .cmd = outputs_cmd,
239 .get_title = outputs_title,
240 };