Code

list_window: removed the list_window_t typedef
[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_outputs.h"
21 #include "screen_interface.h"
22 #include "screen_message.h"
23 #include "i18n.h"
24 #include "list_window.h"
25 #include "mpdclient.h"
27 #include <mpd/client.h>
29 #include <glib.h>
30 #include <assert.h>
32 static struct list_window *lw;
34 static GPtrArray *mpd_outputs = NULL;
36 static void
37 outputs_paint(void);
39 static void
40 outputs_repaint(void)
41 {
42         outputs_paint();
43         wrefresh(lw->w);
44 }
46 static bool
47 toggle_output(struct mpdclient *c, unsigned int output_index)
48 {
49         struct mpd_connection *connection;
50         struct mpd_output *output;
52         assert(mpd_outputs != NULL);
54         if (!mpdclient_is_connected(c) ||
55             output_index >= mpd_outputs->len)
56                 return false;
58         connection = mpdclient_get_connection(c);
59         output = g_ptr_array_index(mpd_outputs, output_index);
61         if (!mpd_output_get_enabled(output)) {
62                 if (!mpd_run_enable_output(connection,
63                                            mpd_output_get_id(output))) {
64                         mpdclient_handle_error(c);
65                         return false;
66                 }
68                 c->events |= MPD_IDLE_OUTPUT;
70                 screen_status_printf(_("Output '%s' enabled"),
71                                      mpd_output_get_name(output));
72         } else {
73                 if (!mpd_run_disable_output(connection,
74                                             mpd_output_get_id(output))) {
75                         mpdclient_handle_error(c);
76                         return false;
77                 }
79                 c->events |= MPD_IDLE_OUTPUT;
81                 screen_status_printf(_("Output '%s' disabled"),
82                                      mpd_output_get_name(output));
83         }
85         return true;
86 }
88 static void
89 clear_output_element(gpointer data, G_GNUC_UNUSED gpointer user_data)
90 {
91         mpd_output_free(data);
92 }
94 static void
95 clear_outputs_list(void)
96 {
97         assert(mpd_outputs != NULL);
99         if (mpd_outputs->len <= 0)
100                 return;
102         g_ptr_array_foreach(mpd_outputs, clear_output_element, NULL);
103         g_ptr_array_remove_range(mpd_outputs, 0, mpd_outputs->len);
106 static void
107 fill_outputs_list(struct mpdclient *c)
109         struct mpd_connection *connection;
110         struct mpd_output *output;
112         assert(mpd_outputs != NULL);
114         if (!mpdclient_is_connected(c))
115                 return;
117         connection = mpdclient_get_connection(c);
118         mpd_send_outputs(connection);
119         while ((output = mpd_recv_output(connection)) != NULL) {
120                 g_ptr_array_add(mpd_outputs, output);
121         }
123         if (!mpd_response_finish(connection))
124                 mpdclient_handle_error(c);
127 static const char *
128 outputs_list_callback(unsigned int output_index, bool *highlight,
129                       G_GNUC_UNUSED char **sc, G_GNUC_UNUSED void *data)
131         struct mpd_output *output;
133         assert(mpd_outputs != NULL);
135         if (output_index >= mpd_outputs->len)
136                 return NULL;
138         output = g_ptr_array_index(mpd_outputs, output_index);
140         if (mpd_output_get_enabled(output))
141                 *highlight = true;
143         return mpd_output_get_name(output);
146 static void
147 outputs_init(WINDOW *w, int cols, int rows)
149         lw = list_window_init(w, cols, rows);
151         mpd_outputs = g_ptr_array_new();
154 static void
155 outputs_resize(int cols, int rows)
157         lw->cols = cols;
158         lw->rows = rows;
161 static void
162 outputs_exit(void)
164         list_window_free(lw);
166         g_ptr_array_free(mpd_outputs, TRUE);
169 static void
170 outputs_open(struct mpdclient *c)
172         fill_outputs_list(c);
175 static void
176 outputs_close(void)
178         clear_outputs_list();
181 static const char *
182 outputs_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
184         return _("Outputs");
187 static void
188 outputs_paint(void)
190         list_window_paint(lw, outputs_list_callback, NULL);
193 static void
194 screen_outputs_update(struct mpdclient *c)
196         if (c->events & MPD_IDLE_OUTPUT) {
197                 clear_outputs_list();
198                 fill_outputs_list(c);
199                 outputs_repaint();
200         }
203 static bool
204 outputs_cmd(struct mpdclient *c, command_t cmd)
206         assert(mpd_outputs != NULL);
208         if (list_window_cmd(lw, mpd_outputs->len, cmd)) {
209                 outputs_repaint();
210                 return true;
211         }
213         switch (cmd) {
214         case CMD_PLAY:
215                 toggle_output(c, lw->selected);
216                 return true;
218         case CMD_SCREEN_UPDATE:
219                 clear_outputs_list();
220                 fill_outputs_list(c);
221                 outputs_repaint();
222                 return true;
224         default:
225                 break;
226         }
228         return false;
231 const struct screen_functions screen_outputs = {
232         .init      = outputs_init,
233         .exit      = outputs_exit,
234         .open      = outputs_open,
235         .close     = outputs_close,
236         .resize    = outputs_resize,
237         .paint     = outputs_paint,
238         .update = screen_outputs_update,
239         .cmd       = outputs_cmd,
240         .get_title = outputs_title,
241 };