Code

moved struct screen_functions to screen_interface.h
[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 int
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 -1;
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 -1;
62                 }
64                 /* XXX reload */
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 -1;
73                 }
75                 /* XXX reload */
77                 screen_status_printf(_("Output '%s' disabled"),
78                                      mpd_output_get_name(output));
79         }
81         outputs_repaint();
83         return 0;
84 }
86 static void
87 clear_output_element(gpointer data, G_GNUC_UNUSED gpointer user_data)
88 {
89         mpd_output_free(data);
90 }
92 static void
93 clear_outputs_list(void)
94 {
95         assert(mpd_outputs != NULL);
97         if (mpd_outputs->len <= 0)
98                 return;
100         g_ptr_array_foreach(mpd_outputs, clear_output_element, NULL);
101         g_ptr_array_remove_range(mpd_outputs, 0, mpd_outputs->len);
104 static void
105 fill_outputs_list(struct mpdclient *c)
107         struct mpd_output *output;
109         assert(mpd_outputs != NULL);
111         if (c->connection == NULL)
112                 return;
114         mpd_send_outputs(c->connection);
115         while ((output = mpd_recv_output(c->connection)) != NULL) {
116                 g_ptr_array_add(mpd_outputs, output);
117         }
119         if (!mpd_response_finish(c->connection))
120                 mpdclient_handle_error(c);
123 static const char *
124 outputs_list_callback(unsigned int output_index, bool *highlight,
125                       G_GNUC_UNUSED char **sc, G_GNUC_UNUSED void *data)
127         struct mpd_output *output;
129         assert(mpd_outputs != NULL);
131         if (output_index >= mpd_outputs->len)
132                 return NULL;
134         output = g_ptr_array_index(mpd_outputs, output_index);
136         if (mpd_output_get_enabled(output))
137                 *highlight = true;
139         return mpd_output_get_name(output);
142 static void
143 outputs_init(WINDOW *w, int cols, int rows)
145         lw = list_window_init(w, cols, rows);
147         mpd_outputs = g_ptr_array_new();
150 static void
151 outputs_resize(int cols, int rows)
153         lw->cols = cols;
154         lw->rows = rows;
157 static void
158 outputs_exit(void)
160         list_window_free(lw);
162         g_ptr_array_free(mpd_outputs, TRUE);
165 static void
166 outputs_open(struct mpdclient *c)
168         fill_outputs_list(c);
171 static void
172 outputs_close(void)
174         clear_outputs_list();
177 static const char *
178 outputs_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
180         return _("Outputs");
183 static void
184 outputs_paint(void)
186         list_window_paint(lw, outputs_list_callback, NULL);
189 static bool
190 outputs_cmd(struct mpdclient *c, command_t cmd)
192         assert(mpd_outputs != NULL);
194         if (list_window_cmd(lw, mpd_outputs->len, cmd)) {
195                 outputs_repaint();
196                 return true;
197         }
199         if (cmd == CMD_PLAY) {
200                 toggle_output(c, lw->selected);
201                 return true;
202         }
204         return false;
207 const struct screen_functions screen_outputs = {
208         .init      = outputs_init,
209         .exit      = outputs_exit,
210         .open      = outputs_open,
211         .close     = outputs_close,
212         .resize    = outputs_resize,
213         .paint     = outputs_paint,
214         .cmd       = outputs_cmd,
215         .get_title = outputs_title,
216 };