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 "i18n.h"
21 #include "screen.h"
22 #include "list_window.h"
24 #include <glib.h>
26 static list_window_t *lw = NULL;
28 static GPtrArray *mpd_outputs = NULL;
30 static void
31 outputs_paint(void);
33 static void
34 outputs_repaint(void)
35 {
36 outputs_paint();
37 wrefresh(lw->w);
38 }
40 static int
41 toggle_output(mpdclient_t *c, unsigned int output_index)
42 {
43 int return_value;
44 mpd_OutputEntity *output;
46 assert(mpd_outputs != NULL);
48 if (output_index >= mpd_outputs->len)
49 return -1;
51 output = g_ptr_array_index(mpd_outputs, output_index);
53 if (output->enabled == 0) {
54 mpd_sendEnableOutputCommand(c->connection, output->id);
56 output->enabled = 1;
58 screen_status_printf(_("Output '%s' enabled"), output->name);
59 } else {
60 mpd_sendDisableOutputCommand(c->connection, output->id);
62 output->enabled = 0;
64 screen_status_printf(_("Output '%s' disabled"), output->name);
65 }
67 return_value = mpdclient_finish_command(c);
69 outputs_repaint();
71 return return_value;
72 }
74 static void
75 clear_output_element(gpointer data, G_GNUC_UNUSED gpointer user_data)
76 {
77 mpd_freeOutputElement(data);
78 }
80 static void
81 clear_outputs_list(void)
82 {
83 assert(mpd_outputs != NULL);
85 if (mpd_outputs->len <= 0)
86 return;
88 g_ptr_array_foreach(mpd_outputs, clear_output_element, NULL);
89 g_ptr_array_remove_range(mpd_outputs, 0, mpd_outputs->len);
90 }
92 static void
93 fill_outputs_list(mpdclient_t *c)
94 {
95 mpd_OutputEntity *output;
97 assert(mpd_outputs != NULL);
99 mpd_sendOutputsCommand(c->connection);
100 while ((output = mpd_getNextOutput(c->connection)) != NULL) {
101 g_ptr_array_add(mpd_outputs, output);
102 }
103 }
105 static const char *
106 outputs_list_callback(unsigned int output_index, bool *highlight,
107 G_GNUC_UNUSED void *data)
108 {
109 mpd_OutputEntity *output;
111 assert(mpd_outputs != NULL);
113 if (output_index >= mpd_outputs->len)
114 return NULL;
116 output = g_ptr_array_index(mpd_outputs, output_index);
118 if (output->enabled)
119 *highlight = true;
121 return output->name;
122 }
124 static void
125 outputs_init(WINDOW *w, int cols, int rows)
126 {
127 lw = list_window_init(w, cols, rows);
129 mpd_outputs = g_ptr_array_new();
130 }
132 static void
133 outputs_resize(int cols, int rows)
134 {
135 lw->cols = cols;
136 lw->rows = rows;
137 }
139 static void
140 outputs_exit(void)
141 {
142 list_window_free(lw);
144 g_ptr_array_free(mpd_outputs, TRUE);
145 }
147 static void
148 outputs_open(mpdclient_t *c)
149 {
150 fill_outputs_list(c);
151 }
153 static void
154 outputs_close(void)
155 {
156 clear_outputs_list();
157 }
159 static const char *
160 outputs_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
161 {
162 return _("Outputs");
163 }
165 static void
166 outputs_paint(void)
167 {
168 list_window_paint(lw, outputs_list_callback, NULL);
169 }
171 static bool
172 outputs_cmd(mpdclient_t *c, command_t cmd)
173 {
174 assert(mpd_outputs != NULL);
176 if (list_window_cmd(lw, mpd_outputs->len, cmd)) {
177 outputs_repaint();
178 return true;
179 }
181 if (cmd == CMD_PLAY) {
182 toggle_output(c, lw->selected);
183 return true;
184 }
186 return false;
187 }
189 const struct screen_functions screen_outputs = {
190 .init = outputs_init,
191 .exit = outputs_exit,
192 .open = outputs_open,
193 .close = outputs_close,
194 .resize = outputs_resize,
195 .paint = outputs_paint,
196 .cmd = outputs_cmd,
197 .get_title = outputs_title,
198 };