Code

75b9790b460398e86daaeb5e7f0f5dbe5f1af052
[ncmpc.git] / src / screen_outputs.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 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.
18 */
20 #include "screen_interface.h"
21 #include "i18n.h"
22 #include "screen.h"
23 #include "list_window.h"
24 #include "mpdclient.h"
26 #include <mpd/client.h>
28 #include <glib.h>
29 #include <assert.h>
31 static list_window_t *lw = NULL;
33 static GPtrArray *mpd_outputs = NULL;
35 static void
36 outputs_paint(void);
38 static void
39 outputs_repaint(void)
40 {
41         outputs_paint();
42         wrefresh(lw->w);
43 }
45 static bool
46 toggle_output(struct mpdclient *c, unsigned int output_index)
47 {
48         struct mpd_output *output;
50         assert(mpd_outputs != NULL);
52         if (output_index >= mpd_outputs->len)
53                 return false;
55         output = g_ptr_array_index(mpd_outputs, output_index);
57         if (!mpd_output_get_enabled(output)) {
58                 if (!mpd_run_enable_output(c->connection,
59                                            mpd_output_get_id(output))) {
60                         mpdclient_handle_error(c);
61                         return false;
62                 }
64                 c->events |= MPD_IDLE_OUTPUT;
66                 screen_status_printf(_("Output '%s' enabled"),
67                                      mpd_output_get_name(output));
68         } else {
69                 if (!mpd_run_disable_output(c->connection,
70                                             mpd_output_get_id(output))) {
71                         mpdclient_handle_error(c);
72                         return false;
73                 }
75                 c->events |= MPD_IDLE_OUTPUT;
77                 screen_status_printf(_("Output '%s' disabled"),
78                                      mpd_output_get_name(output));
79         }
81         return true;
82 }
84 static void
85 clear_output_element(gpointer data, G_GNUC_UNUSED gpointer user_data)
86 {
87         mpd_output_free(data);
88 }
90 static void
91 clear_outputs_list(void)
92 {
93         assert(mpd_outputs != NULL);
95         if (mpd_outputs->len <= 0)
96                 return;
98         g_ptr_array_foreach(mpd_outputs, clear_output_element, NULL);
99         g_ptr_array_remove_range(mpd_outputs, 0, mpd_outputs->len);
102 static void
103 fill_outputs_list(struct mpdclient *c)
105         struct mpd_output *output;
107         assert(mpd_outputs != NULL);
109         if (c->connection == NULL)
110                 return;
112         mpd_send_outputs(c->connection);
113         while ((output = mpd_recv_output(c->connection)) != NULL) {
114                 g_ptr_array_add(mpd_outputs, output);
115         }
117         if (!mpd_response_finish(c->connection))
118                 mpdclient_handle_error(c);
121 static const char *
122 outputs_list_callback(unsigned int output_index, bool *highlight,
123                       G_GNUC_UNUSED char **sc, G_GNUC_UNUSED void *data)
125         struct mpd_output *output;
127         assert(mpd_outputs != NULL);
129         if (output_index >= mpd_outputs->len)
130                 return NULL;
132         output = g_ptr_array_index(mpd_outputs, output_index);
134         if (mpd_output_get_enabled(output))
135                 *highlight = true;
137         return mpd_output_get_name(output);
140 static void
141 outputs_init(WINDOW *w, int cols, int rows)
143         lw = list_window_init(w, cols, rows);
145         mpd_outputs = g_ptr_array_new();
148 static void
149 outputs_resize(int cols, int rows)
151         lw->cols = cols;
152         lw->rows = rows;
155 static void
156 outputs_exit(void)
158         list_window_free(lw);
160         g_ptr_array_free(mpd_outputs, TRUE);
163 static void
164 outputs_open(struct mpdclient *c)
166         fill_outputs_list(c);
169 static void
170 outputs_close(void)
172         clear_outputs_list();
175 static const char *
176 outputs_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
178         return _("Outputs");
181 static void
182 outputs_paint(void)
184         list_window_paint(lw, outputs_list_callback, NULL);
187 static void
188 screen_outputs_update(struct mpdclient *c)
190         if (c->events & MPD_IDLE_OUTPUT) {
191                 clear_outputs_list();
192                 fill_outputs_list(c);
193                 outputs_repaint();
194         }
197 static bool
198 outputs_cmd(struct mpdclient *c, command_t cmd)
200         assert(mpd_outputs != NULL);
202         if (list_window_cmd(lw, mpd_outputs->len, cmd)) {
203                 outputs_repaint();
204                 return true;
205         }
207         switch (cmd) {
208         case CMD_PLAY:
209                 toggle_output(c, lw->selected);
210                 return true;
212         case CMD_SCREEN_UPDATE:
213                 clear_outputs_list();
214                 fill_outputs_list(c);
215                 outputs_repaint();
216                 return true;
218         default:
219                 break;
220         }
222         return false;
225 const struct screen_functions screen_outputs = {
226         .init      = outputs_init,
227         .exit      = outputs_exit,
228         .open      = outputs_open,
229         .close     = outputs_close,
230         .resize    = outputs_resize,
231         .paint     = outputs_paint,
232         .update = screen_outputs_update,
233         .cmd       = outputs_cmd,
234         .get_title = outputs_title,
235 };