options: remove double-semicolon
[ncmpc.git] / src / screen_outputs.c
blobabfc4576353a96b691688ca71eacdcd9002cd813
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"
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)
43 outputs_paint();
44 wrefresh(lw->w);
47 static bool
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)
56 return false;
58 connection = mpdclient_get_connection(c);
59 if (connection == NULL)
60 return false;
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);
68 return false;
71 c->events |= MPD_IDLE_OUTPUT;
73 screen_status_printf(_("Output '%s' enabled"),
74 mpd_output_get_name(output));
75 } else {
76 if (!mpd_run_disable_output(connection,
77 mpd_output_get_id(output))) {
78 mpdclient_handle_error(c);
79 return false;
82 c->events |= MPD_IDLE_OUTPUT;
84 screen_status_printf(_("Output '%s' disabled"),
85 mpd_output_get_name(output));
88 return true;
91 static void
92 clear_output_element(gpointer data, G_GNUC_UNUSED gpointer user_data)
94 mpd_output_free(data);
97 static void
98 clear_outputs_list(void)
100 assert(mpd_outputs != NULL);
102 if (mpd_outputs->len <= 0)
103 return;
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); */
114 static void
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);
125 return;
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);
139 static void
140 outputs_init(WINDOW *w, int cols, int rows)
142 lw = list_window_init(w, cols, rows);
144 mpd_outputs = g_ptr_array_new();
147 static void
148 outputs_resize(int cols, int rows)
150 list_window_resize(lw, cols, rows);
153 static void
154 outputs_exit(void)
156 list_window_free(lw);
158 g_ptr_array_free(mpd_outputs, TRUE);
161 static void
162 outputs_open(struct mpdclient *c)
164 fill_outputs_list(c);
167 static void
168 outputs_close(void)
170 clear_outputs_list();
173 static const char *
174 outputs_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
176 return _("Outputs");
179 static void
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);
197 static void
198 outputs_paint(void)
200 list_window_paint2(lw, screen_outputs_paint_callback, NULL);
203 static void
204 screen_outputs_update(struct mpdclient *c)
206 if (c->events & MPD_IDLE_OUTPUT) {
207 clear_outputs_list();
208 fill_outputs_list(c);
209 outputs_repaint();
213 static bool
214 outputs_cmd(struct mpdclient *c, command_t cmd)
216 assert(mpd_outputs != NULL);
218 if (list_window_cmd(lw, cmd)) {
219 outputs_repaint();
220 return true;
223 switch (cmd) {
224 case CMD_PLAY:
225 toggle_output(c, lw->selected);
226 return true;
228 case CMD_SCREEN_UPDATE:
229 clear_outputs_list();
230 fill_outputs_list(c);
231 outputs_repaint();
232 return true;
234 default:
235 break;
238 return false;
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,
249 .cmd = outputs_cmd,
250 .get_title = outputs_title,