6f951dd3fc1a1dcfb2ff51a0f6e1e3a2445ab7e2
[ncmpc-debian.git] / src / screen_outputs.cxx
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2018 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  *
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.
9  *
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.
14  *
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  */
19
20 #include "screen_outputs.hxx"
21 #include "screen_interface.hxx"
22 #include "ListPage.hxx"
23 #include "screen_status.hxx"
24 #include "paint.hxx"
25 #include "i18n.h"
26 #include "mpdclient.hxx"
27
28 #include <mpd/client.h>
29
30 #include <vector>
31 #include <memory>
32
33 #include <assert.h>
34
35 struct OutputDeleter {
36         void operator()(struct mpd_output *o) const {
37                 mpd_output_free(o);
38         }
39 };
40
41 class OutputsPage final : public ListPage {
42         std::vector<std::unique_ptr<struct mpd_output, OutputDeleter>> items;
43
44 public:
45         OutputsPage(WINDOW *w, Size size)
46                 :ListPage(w, size) {}
47
48 private:
49         void Clear();
50
51         bool Toggle(struct mpdclient &c, unsigned output_index);
52
53 public:
54         /* virtual methods from class Page */
55         void Paint() const override;
56         void Update(struct mpdclient &c, unsigned events) override;
57         bool OnCommand(struct mpdclient &c, command_t cmd) override;
58         const char *GetTitle(char *s, size_t size) const override;
59 };
60
61 bool
62 OutputsPage::Toggle(struct mpdclient &c, unsigned output_index)
63 {
64         if (output_index >= items.size())
65                 return false;
66
67         struct mpd_connection *connection = mpdclient_get_connection(&c);
68         if (connection == nullptr)
69                 return false;
70
71         const auto &output = *items[output_index];
72         if (!mpd_output_get_enabled(&output)) {
73                 if (!mpd_run_enable_output(connection,
74                                            mpd_output_get_id(&output))) {
75                         mpdclient_handle_error(&c);
76                         return false;
77                 }
78
79                 c.events |= MPD_IDLE_OUTPUT;
80
81                 screen_status_printf(_("Output '%s' enabled"),
82                                      mpd_output_get_name(&output));
83         } else {
84                 if (!mpd_run_disable_output(connection,
85                                             mpd_output_get_id(&output))) {
86                         mpdclient_handle_error(&c);
87                         return false;
88                 }
89
90                 c.events |= MPD_IDLE_OUTPUT;
91
92                 screen_status_printf(_("Output '%s' disabled"),
93                                      mpd_output_get_name(&output));
94         }
95
96         return true;
97 }
98
99 void
100 OutputsPage::Clear()
101 {
102         if (items.empty())
103                 return;
104
105         items.clear();
106
107         /* not updating the list_window length here, because that
108            would clear the cursor position, and fill_outputs_list()
109            will be called after this function anyway */
110         /* lw.SetLength(0); */
111 }
112
113 template<typename O>
114 static void
115 fill_outputs_list(struct mpdclient *c, O &items)
116 {
117         struct mpd_connection *connection = mpdclient_get_connection(c);
118         if (connection == nullptr)
119                 return;
120
121         mpd_send_outputs(connection);
122
123         struct mpd_output *output;
124         while ((output = mpd_recv_output(connection)) != nullptr)
125                 items.emplace_back(output);
126
127         mpdclient_finish_command(c);
128 }
129
130 static Page *
131 outputs_init(ScreenManager &, WINDOW *w, Size size)
132 {
133         return new OutputsPage(w, size);
134 }
135
136 const char *
137 OutputsPage::GetTitle(gcc_unused char *str, gcc_unused size_t size) const
138 {
139         return _("Outputs");
140 }
141
142 static void
143 screen_outputs_paint_callback(WINDOW *w, unsigned i,
144                               gcc_unused unsigned y, unsigned width,
145                               bool selected, const void *data)
146 {
147         const auto &items = *(const std::vector<std::unique_ptr<struct mpd_output, OutputDeleter>> *)data;
148         assert(i < items.size());
149         const auto *output = items[i].get();
150
151         row_color(w, COLOR_LIST, selected);
152         waddstr(w, mpd_output_get_enabled(output) ? "[X] " : "[ ] ");
153         waddstr(w, mpd_output_get_name(output));
154         row_clear_to_eol(w, width, selected);
155 }
156
157 void
158 OutputsPage::Paint() const
159 {
160         lw.Paint(screen_outputs_paint_callback, &items);
161 }
162
163 void
164 OutputsPage::Update(struct mpdclient &c, unsigned events)
165 {
166         if (events & MPD_IDLE_OUTPUT) {
167                 Clear();
168                 fill_outputs_list(&c, items);
169                 lw.SetLength(items.size());
170                 SetDirty();
171         }
172 }
173
174 bool
175 OutputsPage::OnCommand(struct mpdclient &c, command_t cmd)
176 {
177         if (ListPage::OnCommand(c, cmd))
178                 return true;
179
180         switch (cmd) {
181         case CMD_PLAY:
182                 Toggle(c, lw.selected);
183                 return true;
184
185         case CMD_SCREEN_UPDATE:
186                 Clear();
187                 fill_outputs_list(&c, items);
188                 lw.SetLength(items.size());
189                 SetDirty();
190                 return true;
191
192         default:
193                 break;
194         }
195
196         return false;
197 }
198
199 const struct screen_functions screen_outputs = {
200         "outputs",
201         outputs_init,
202 };