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"
25 #include "list_window.h"
26 #include "mpdclient.h"
28 #include <mpd/client.h>
33 static struct list_window
*lw
;
35 static GPtrArray
*mpd_outputs
= NULL
;
38 toggle_output(struct mpdclient
*c
, unsigned int output_index
)
40 assert(mpd_outputs
!= NULL
);
42 if (output_index
>= mpd_outputs
->len
)
45 struct mpd_connection
*connection
= mpdclient_get_connection(c
);
46 if (connection
== NULL
)
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
);
58 c
->events
|= MPD_IDLE_OUTPUT
;
60 screen_status_printf(_("Output '%s' enabled"),
61 mpd_output_get_name(output
));
63 if (!mpd_run_disable_output(connection
,
64 mpd_output_get_id(output
))) {
65 mpdclient_handle_error(c
);
69 c
->events
|= MPD_IDLE_OUTPUT
;
71 screen_status_printf(_("Output '%s' disabled"),
72 mpd_output_get_name(output
));
79 clear_output_element(gpointer data
, gcc_unused gpointer user_data
)
81 mpd_output_free(data
);
85 clear_outputs_list(void)
87 assert(mpd_outputs
!= NULL
);
89 if (mpd_outputs
->len
<= 0)
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); */
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);
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
);
125 outputs_init(WINDOW
*w
, unsigned cols
, unsigned rows
)
127 lw
= list_window_init(w
, cols
, rows
);
129 mpd_outputs
= g_ptr_array_new();
133 outputs_resize(unsigned cols
, unsigned rows
)
135 list_window_resize(lw
, cols
, rows
);
141 list_window_free(lw
);
143 g_ptr_array_free(mpd_outputs
, TRUE
);
147 outputs_open(struct mpdclient
*c
)
149 fill_outputs_list(c
);
155 clear_outputs_list();
159 outputs_title(gcc_unused
char *str
, gcc_unused
size_t size
)
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
);
185 list_window_paint2(lw
, screen_outputs_paint_callback
, NULL
);
189 screen_outputs_update(struct mpdclient
*c
)
191 if (c
->events
& MPD_IDLE_OUTPUT
) {
192 clear_outputs_list();
193 fill_outputs_list(c
);
199 outputs_cmd(struct mpdclient
*c
, command_t cmd
)
201 assert(mpd_outputs
!= NULL
);
203 if (list_window_cmd(lw
, cmd
)) {
210 toggle_output(c
, lw
->selected
);
213 case CMD_SCREEN_UPDATE
:
214 clear_outputs_list();
215 fill_outputs_list(c
);
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
,
235 .get_title
= outputs_title
,