Code

screen_outputs: new screen to control MPD's output devices
[ncmpc.git] / src / screen_outputs.c
1 /*
2  * (c) 2008 by Mikael Svantesson <mikael@distopic.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  *
17  */
19 #include "i18n.h"
20 #include "screen.h"
21 #include "list_window.h"
23 #include <glib.h>
25 static list_window_t *lw = NULL;
27 static GPtrArray *mpd_outputs = NULL;
29 static void
30 outputs_paint(void);
32 static void
33 outputs_repaint(void)
34 {
35         outputs_paint();
36         wrefresh(lw->w);
37 }
39 static int
40 toggle_output(mpdclient_t *c, unsigned int output_index)
41 {
42         int return_value;
43         mpd_OutputEntity *output;
45         assert(mpd_outputs != NULL);
47         if (output_index >= mpd_outputs->len)
48                 return -1;
50         output = g_ptr_array_index(mpd_outputs, output_index);
52         if (output->enabled == 0) {
53                 mpd_sendEnableOutputCommand(c->connection, output->id);
55                 output->enabled = 1;
57                 screen_status_printf(_("Output '%s' enabled"), output->name);
58         } else {
59                 mpd_sendDisableOutputCommand(c->connection, output->id);
61                 output->enabled = 0;
63                 screen_status_printf(_("Output '%s' disabled"), output->name);
64         }
66         return_value = mpdclient_finish_command(c);
68         outputs_repaint();
70         return return_value;
71 }
73 static void
74 clear_output_element(gpointer data, G_GNUC_UNUSED gpointer user_data)
75 {
76         mpd_freeOutputElement(data);
77 }
79 static void
80 clear_outputs_list(void)
81 {
82         assert(mpd_outputs != NULL);
84         if (mpd_outputs->len <= 0)
85                 return;
87         g_ptr_array_foreach(mpd_outputs, clear_output_element, NULL);
88         g_ptr_array_remove_range(mpd_outputs, 0, mpd_outputs->len);
89 }
91 static void
92 fill_outputs_list(mpdclient_t *c)
93 {
94         mpd_OutputEntity *output;
96         assert(mpd_outputs != NULL);
98         mpd_sendOutputsCommand(c->connection);
99         while ((output = mpd_getNextOutput(c->connection)) != NULL) {
100                 g_ptr_array_add(mpd_outputs, output);
101         }
104 static const char *
105 outputs_list_callback(unsigned int output_index, bool *highlight,
106                       G_GNUC_UNUSED void *data)
108         mpd_OutputEntity *output;
110         assert(mpd_outputs != NULL);
112         if (output_index >= mpd_outputs->len)
113                 return NULL;
115         output = g_ptr_array_index(mpd_outputs, output_index);
117         if (output->enabled)
118                 *highlight = true;
120         return output->name;
123 static void
124 outputs_init(WINDOW *w, int cols, int rows)
126         lw = list_window_init(w, cols, rows);
128         mpd_outputs = g_ptr_array_new();
131 static void
132 outputs_resize(int cols, int rows)
134         lw->cols = cols;
135         lw->rows = rows;
138 static void
139 outputs_exit(void)
141         list_window_free(lw);
143         g_ptr_array_free(mpd_outputs, TRUE);
146 static void
147 outputs_open(mpdclient_t *c)
149         fill_outputs_list(c);
152 static void
153 outputs_close(void)
155         clear_outputs_list();
158 static const char *
159 outputs_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
161         return _("Outputs");
164 static void
165 outputs_paint(void)
167         list_window_paint(lw, outputs_list_callback, NULL);
170 static bool
171 outputs_cmd(mpdclient_t *c, command_t cmd)
173         assert(mpd_outputs != NULL);
175         if (list_window_cmd(lw, mpd_outputs->len, cmd)) {
176                 outputs_repaint();
177                 return true;
178         }
180         if (cmd == CMD_PLAY) {
181                 toggle_output(c, lw->selected);
182                 return true;
183         }
185         return false;
188 const struct screen_functions screen_outputs = {
189         .init      = outputs_init,
190         .exit      = outputs_exit,
191         .open      = outputs_open,
192         .close     = outputs_close,
193         .resize    = outputs_resize,
194         .paint     = outputs_paint,
195         .cmd       = outputs_cmd,
196         .get_title = outputs_title,
197 };