1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2017 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
4 *
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.
9 *
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.
14 *
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_status.h"
23 #include "paint.h"
24 #include "i18n.h"
25 #include "list_window.h"
26 #include "mpdclient.h"
28 #include <mpd/client.h>
30 #include <glib.h>
31 #include <assert.h>
33 static struct list_window *lw;
35 static GPtrArray *mpd_outputs = NULL;
37 static void
38 outputs_paint(void);
40 static void
41 outputs_repaint(void)
42 {
43 outputs_paint();
44 wrefresh(lw->w);
45 }
47 static bool
48 toggle_output(struct mpdclient *c, unsigned int output_index)
49 {
50 assert(mpd_outputs != NULL);
52 if (output_index >= mpd_outputs->len)
53 return false;
55 struct mpd_connection *connection = mpdclient_get_connection(c);
56 if (connection == NULL)
57 return false;
59 struct mpd_output *output =
60 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, gcc_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);
105 /* not updating the list_window length here, because that
106 would clear the cursor position, and fill_outputs_list()
107 will be called after this function anyway */
108 /* list_window_set_length(lw, 0); */
109 }
111 static void
112 fill_outputs_list(struct mpdclient *c)
113 {
114 assert(mpd_outputs != NULL);
116 struct mpd_connection *connection = mpdclient_get_connection(c);
117 if (connection == NULL) {
118 list_window_set_length(lw, 0);
119 return;
120 }
122 mpd_send_outputs(connection);
124 struct mpd_output *output;
125 while ((output = mpd_recv_output(connection)) != NULL) {
126 g_ptr_array_add(mpd_outputs, output);
127 }
129 mpdclient_finish_command(c);
131 list_window_set_length(lw, mpd_outputs->len);
132 }
134 static void
135 outputs_init(WINDOW *w, int cols, int rows)
136 {
137 lw = list_window_init(w, cols, rows);
139 mpd_outputs = g_ptr_array_new();
140 }
142 static void
143 outputs_resize(int cols, int rows)
144 {
145 list_window_resize(lw, cols, rows);
146 }
148 static void
149 outputs_exit(void)
150 {
151 list_window_free(lw);
153 g_ptr_array_free(mpd_outputs, TRUE);
154 }
156 static void
157 outputs_open(struct mpdclient *c)
158 {
159 fill_outputs_list(c);
160 }
162 static void
163 outputs_close(void)
164 {
165 clear_outputs_list();
166 }
168 static const char *
169 outputs_title(gcc_unused char *str, gcc_unused size_t size)
170 {
171 return _("Outputs");
172 }
174 static void
175 screen_outputs_paint_callback(WINDOW *w, unsigned i,
176 gcc_unused unsigned y, unsigned width,
177 bool selected, gcc_unused const void *data)
178 {
179 const struct mpd_output *output;
181 assert(mpd_outputs != NULL);
182 assert(i < mpd_outputs->len);
184 output = g_ptr_array_index(mpd_outputs, i);
186 row_color(w, COLOR_LIST, selected);
187 waddstr(w, mpd_output_get_enabled(output) ? "[X] " : "[ ] ");
188 waddstr(w, mpd_output_get_name(output));
189 row_clear_to_eol(w, width, selected);
190 }
192 static void
193 outputs_paint(void)
194 {
195 list_window_paint2(lw, screen_outputs_paint_callback, NULL);
196 }
198 static void
199 screen_outputs_update(struct mpdclient *c)
200 {
201 if (c->events & MPD_IDLE_OUTPUT) {
202 clear_outputs_list();
203 fill_outputs_list(c);
204 outputs_repaint();
205 }
206 }
208 static bool
209 outputs_cmd(struct mpdclient *c, command_t cmd)
210 {
211 assert(mpd_outputs != NULL);
213 if (list_window_cmd(lw, cmd)) {
214 outputs_repaint();
215 return true;
216 }
218 switch (cmd) {
219 case CMD_PLAY:
220 toggle_output(c, lw->selected);
221 return true;
223 case CMD_SCREEN_UPDATE:
224 clear_outputs_list();
225 fill_outputs_list(c);
226 outputs_repaint();
227 return true;
229 default:
230 break;
231 }
233 return false;
234 }
236 const struct screen_functions screen_outputs = {
237 .init = outputs_init,
238 .exit = outputs_exit,
239 .open = outputs_open,
240 .close = outputs_close,
241 .resize = outputs_resize,
242 .paint = outputs_paint,
243 .update = screen_outputs_update,
244 .cmd = outputs_cmd,
245 .get_title = outputs_title,
246 };