Code

screen_search, screen_outputs: added connection==NULL checks
[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 "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         if (c->connection == NULL)
100                 return;
102         mpd_sendOutputsCommand(c->connection);
103         while ((output = mpd_getNextOutput(c->connection)) != NULL) {
104                 g_ptr_array_add(mpd_outputs, output);
105         }
108 static const char *
109 outputs_list_callback(unsigned int output_index, bool *highlight,
110                       G_GNUC_UNUSED char **sc, G_GNUC_UNUSED void *data)
112         mpd_OutputEntity *output;
114         assert(mpd_outputs != NULL);
116         if (output_index >= mpd_outputs->len)
117                 return NULL;
119         output = g_ptr_array_index(mpd_outputs, output_index);
121         if (output->enabled)
122                 *highlight = true;
124         return output->name;
127 static void
128 outputs_init(WINDOW *w, int cols, int rows)
130         lw = list_window_init(w, cols, rows);
132         mpd_outputs = g_ptr_array_new();
135 static void
136 outputs_resize(int cols, int rows)
138         lw->cols = cols;
139         lw->rows = rows;
142 static void
143 outputs_exit(void)
145         list_window_free(lw);
147         g_ptr_array_free(mpd_outputs, TRUE);
150 static void
151 outputs_open(mpdclient_t *c)
153         fill_outputs_list(c);
156 static void
157 outputs_close(void)
159         clear_outputs_list();
162 static const char *
163 outputs_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
165         return _("Outputs");
168 static void
169 outputs_paint(void)
171         list_window_paint(lw, outputs_list_callback, NULL);
174 static bool
175 outputs_cmd(mpdclient_t *c, command_t cmd)
177         assert(mpd_outputs != NULL);
179         if (list_window_cmd(lw, mpd_outputs->len, cmd)) {
180                 outputs_repaint();
181                 return true;
182         }
184         if (cmd == CMD_PLAY) {
185                 toggle_output(c, lw->selected);
186                 return true;
187         }
189         return false;
192 const struct screen_functions screen_outputs = {
193         .init      = outputs_init,
194         .exit      = outputs_exit,
195         .open      = outputs_open,
196         .close     = outputs_close,
197         .resize    = outputs_resize,
198         .paint     = outputs_paint,
199         .cmd       = outputs_cmd,
200         .get_title = outputs_title,
201 };