1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 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_message.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
;
48 toggle_output(struct mpdclient
*c
, unsigned int output_index
)
50 struct mpd_connection
*connection
;
51 struct mpd_output
*output
;
53 assert(mpd_outputs
!= NULL
);
55 if (output_index
>= mpd_outputs
->len
)
58 connection
= mpdclient_get_connection(c
);
59 if (connection
== NULL
)
62 output
= g_ptr_array_index(mpd_outputs
, output_index
);
64 if (!mpd_output_get_enabled(output
)) {
65 if (!mpd_run_enable_output(connection
,
66 mpd_output_get_id(output
))) {
67 mpdclient_handle_error(c
);
71 c
->events
|= MPD_IDLE_OUTPUT
;
73 screen_status_printf(_("Output '%s' enabled"),
74 mpd_output_get_name(output
));
76 if (!mpd_run_disable_output(connection
,
77 mpd_output_get_id(output
))) {
78 mpdclient_handle_error(c
);
82 c
->events
|= MPD_IDLE_OUTPUT
;
84 screen_status_printf(_("Output '%s' disabled"),
85 mpd_output_get_name(output
));
92 clear_output_element(gpointer data
, G_GNUC_UNUSED gpointer user_data
)
94 mpd_output_free(data
);
98 clear_outputs_list(void)
100 assert(mpd_outputs
!= NULL
);
102 if (mpd_outputs
->len
<= 0)
105 g_ptr_array_foreach(mpd_outputs
, clear_output_element
, NULL
);
106 g_ptr_array_remove_range(mpd_outputs
, 0, mpd_outputs
->len
);
108 /* not updating the list_window length here, because that
109 would clear the cursor position, and fill_outputs_list()
110 will be called after this function anyway */
111 /* list_window_set_length(lw, 0); */
115 fill_outputs_list(struct mpdclient
*c
)
117 struct mpd_connection
*connection
;
118 struct mpd_output
*output
;
120 assert(mpd_outputs
!= NULL
);
122 connection
= mpdclient_get_connection(c
);
123 if (connection
== NULL
) {
124 list_window_set_length(lw
, 0);
128 mpd_send_outputs(connection
);
129 while ((output
= mpd_recv_output(connection
)) != NULL
) {
130 g_ptr_array_add(mpd_outputs
, output
);
133 if (!mpd_response_finish(connection
))
134 mpdclient_handle_error(c
);
136 list_window_set_length(lw
, mpd_outputs
->len
);
140 outputs_init(WINDOW
*w
, int cols
, int rows
)
142 lw
= list_window_init(w
, cols
, rows
);
144 mpd_outputs
= g_ptr_array_new();
148 outputs_resize(int cols
, int rows
)
150 list_window_resize(lw
, cols
, rows
);
156 list_window_free(lw
);
158 g_ptr_array_free(mpd_outputs
, TRUE
);
162 outputs_open(struct mpdclient
*c
)
164 fill_outputs_list(c
);
170 clear_outputs_list();
174 outputs_title(G_GNUC_UNUSED
char *str
, G_GNUC_UNUSED
size_t size
)
180 screen_outputs_paint_callback(WINDOW
*w
, unsigned i
,
181 G_GNUC_UNUSED
unsigned y
, unsigned width
,
182 bool selected
, G_GNUC_UNUSED
void *data
)
184 const struct mpd_output
*output
;
186 assert(mpd_outputs
!= NULL
);
187 assert(i
< mpd_outputs
->len
);
189 output
= g_ptr_array_index(mpd_outputs
, i
);
191 row_color(w
, COLOR_LIST
, selected
);
192 waddstr(w
, mpd_output_get_enabled(output
) ? "[X] " : "[ ] ");
193 waddstr(w
, mpd_output_get_name(output
));
194 row_clear_to_eol(w
, width
, selected
);
200 list_window_paint2(lw
, screen_outputs_paint_callback
, NULL
);
204 screen_outputs_update(struct mpdclient
*c
)
206 if (c
->events
& MPD_IDLE_OUTPUT
) {
207 clear_outputs_list();
208 fill_outputs_list(c
);
214 outputs_cmd(struct mpdclient
*c
, command_t cmd
)
216 assert(mpd_outputs
!= NULL
);
218 if (list_window_cmd(lw
, cmd
)) {
225 toggle_output(c
, lw
->selected
);
228 case CMD_SCREEN_UPDATE
:
229 clear_outputs_list();
230 fill_outputs_list(c
);
241 const struct screen_functions screen_outputs
= {
242 .init
= outputs_init
,
243 .exit
= outputs_exit
,
244 .open
= outputs_open
,
245 .close
= outputs_close
,
246 .resize
= outputs_resize
,
247 .paint
= outputs_paint
,
248 .update
= screen_outputs_update
,
250 .get_title
= outputs_title
,