screen: move code to paint_top_window()
[ncmpc.git] / src / screen_outputs.c
blob67cc4b86fbc0073e0d986f19e95b8f0d71d373cf
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2017 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.
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 bool
38 toggle_output(struct mpdclient *c, unsigned int output_index)
40 assert(mpd_outputs != NULL);
42 if (output_index >= mpd_outputs->len)
43 return false;
45 struct mpd_connection *connection = mpdclient_get_connection(c);
46 if (connection == NULL)
47 return false;
49 struct mpd_output *output =
50 g_ptr_array_index(mpd_outputs, output_index);
51 if (!mpd_output_get_enabled(output)) {
52 if (!mpd_run_enable_output(connection,
53 mpd_output_get_id(output))) {
54 mpdclient_handle_error(c);
55 return false;
58 c->events |= MPD_IDLE_OUTPUT;
60 screen_status_printf(_("Output '%s' enabled"),
61 mpd_output_get_name(output));
62 } else {
63 if (!mpd_run_disable_output(connection,
64 mpd_output_get_id(output))) {
65 mpdclient_handle_error(c);
66 return false;
69 c->events |= MPD_IDLE_OUTPUT;
71 screen_status_printf(_("Output '%s' disabled"),
72 mpd_output_get_name(output));
75 return true;
78 static void
79 clear_output_element(gpointer data, gcc_unused gpointer user_data)
81 mpd_output_free(data);
84 static void
85 clear_outputs_list(void)
87 assert(mpd_outputs != NULL);
89 if (mpd_outputs->len <= 0)
90 return;
92 g_ptr_array_foreach(mpd_outputs, clear_output_element, NULL);
93 g_ptr_array_remove_range(mpd_outputs, 0, mpd_outputs->len);
95 /* not updating the list_window length here, because that
96 would clear the cursor position, and fill_outputs_list()
97 will be called after this function anyway */
98 /* list_window_set_length(lw, 0); */
101 static void
102 fill_outputs_list(struct mpdclient *c)
104 assert(mpd_outputs != NULL);
106 struct mpd_connection *connection = mpdclient_get_connection(c);
107 if (connection == NULL) {
108 list_window_set_length(lw, 0);
109 return;
112 mpd_send_outputs(connection);
114 struct mpd_output *output;
115 while ((output = mpd_recv_output(connection)) != NULL) {
116 g_ptr_array_add(mpd_outputs, output);
119 mpdclient_finish_command(c);
121 list_window_set_length(lw, mpd_outputs->len);
124 static void
125 outputs_init(WINDOW *w, unsigned cols, unsigned rows)
127 lw = list_window_init(w, cols, rows);
129 mpd_outputs = g_ptr_array_new();
132 static void
133 outputs_resize(unsigned cols, unsigned rows)
135 list_window_resize(lw, cols, rows);
138 static void
139 outputs_exit(void)
141 list_window_free(lw);
143 g_ptr_array_free(mpd_outputs, TRUE);
146 static void
147 outputs_open(struct mpdclient *c)
149 fill_outputs_list(c);
152 static void
153 outputs_close(void)
155 clear_outputs_list();
158 static const char *
159 outputs_title(gcc_unused char *str, gcc_unused size_t size)
161 return _("Outputs");
164 static void
165 screen_outputs_paint_callback(WINDOW *w, unsigned i,
166 gcc_unused unsigned y, unsigned width,
167 bool selected, gcc_unused const void *data)
169 const struct mpd_output *output;
171 assert(mpd_outputs != NULL);
172 assert(i < mpd_outputs->len);
174 output = g_ptr_array_index(mpd_outputs, i);
176 row_color(w, COLOR_LIST, selected);
177 waddstr(w, mpd_output_get_enabled(output) ? "[X] " : "[ ] ");
178 waddstr(w, mpd_output_get_name(output));
179 row_clear_to_eol(w, width, selected);
182 static void
183 outputs_paint(void)
185 list_window_paint2(lw, screen_outputs_paint_callback, NULL);
188 static void
189 screen_outputs_update(struct mpdclient *c)
191 if (c->events & MPD_IDLE_OUTPUT) {
192 clear_outputs_list();
193 fill_outputs_list(c);
194 outputs_paint();
198 static bool
199 outputs_cmd(struct mpdclient *c, command_t cmd)
201 assert(mpd_outputs != NULL);
203 if (list_window_cmd(lw, cmd)) {
204 outputs_paint();
205 return true;
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_paint();
217 return true;
219 default:
220 break;
223 return false;
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,