1 /* ncmpc (Ncurses MPD Client)
2 (c) 2004-2018 The Music Player Daemon Project
3 Project homepage: http://musicpd.org
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
20 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "util/Compiler.h"
32 #include <mpd/async.h>
33 #include <mpd/parser.h>
41 MpdIdleSource::MpdIdleSource(struct mpd_connection &_connection,
42 mpd_glib_callback_t _callback, void *_callback_ctx)
43 :connection(&_connection),
44 async(mpd_connection_get_async(connection)),
45 parser(mpd_parser_new()),
46 callback(_callback), callback_ctx(_callback_ctx),
47 channel(g_io_channel_unix_new(mpd_async_get_fd(async)))
49 /* TODO check parser!=nullptr */
52 MpdIdleSource::~MpdIdleSource()
57 g_io_channel_unref(channel);
59 mpd_parser_free(parser);
64 mpd_glib_invoke(const MpdIdleSource *source)
66 assert(source->id == 0);
68 if (source->idle_events != 0)
69 source->callback(MPD_ERROR_SUCCESS, (enum mpd_server_error)0,
71 source->idle_events, source->callback_ctx);
75 mpd_glib_invoke_error(const MpdIdleSource *source,
76 enum mpd_error error, enum mpd_server_error server_error,
79 assert(source->id == 0);
81 source->callback(error, server_error, message,
82 0, source->callback_ctx);
86 mpd_glib_invoke_async_error(const MpdIdleSource *source)
88 assert(source->id == 0);
90 mpd_glib_invoke_error(source, mpd_async_get_error(source->async),
91 (enum mpd_server_error)0,
92 mpd_async_get_error_message(source->async));
96 * Converts a GIOCondition bit mask to #mpd_async_event.
98 static enum mpd_async_event
99 g_io_condition_to_mpd_async_event(GIOCondition condition)
103 if (condition & G_IO_IN)
104 events |= MPD_ASYNC_EVENT_READ;
106 if (condition & G_IO_OUT)
107 events |= MPD_ASYNC_EVENT_WRITE;
109 if (condition & G_IO_HUP)
110 events |= MPD_ASYNC_EVENT_HUP;
112 if (condition & G_IO_ERR)
113 events |= MPD_ASYNC_EVENT_ERROR;
115 return (enum mpd_async_event)events;
119 * Converts a #mpd_async_event bit mask to GIOCondition.
122 mpd_async_events_to_g_io_condition(enum mpd_async_event events)
124 unsigned condition = 0;
126 if (events & MPD_ASYNC_EVENT_READ)
127 condition |= G_IO_IN;
129 if (events & MPD_ASYNC_EVENT_WRITE)
130 condition |= G_IO_OUT;
132 if (events & MPD_ASYNC_EVENT_HUP)
133 condition |= G_IO_HUP;
135 if (events & MPD_ASYNC_EVENT_ERROR)
136 condition |= G_IO_ERR;
138 return GIOCondition(condition);
142 * Parses a response line from MPD.
144 * @return true on success, false on error
147 mpd_glib_feed(MpdIdleSource *source, char *line)
149 enum mpd_parser_result result;
151 result = mpd_parser_feed(source->parser, line);
153 case MPD_PARSER_MALFORMED:
155 source->io_events = 0;
157 mpd_glib_invoke_error(source, MPD_ERROR_MALFORMED,
158 (enum mpd_server_error)0,
159 "Malformed MPD response");
162 case MPD_PARSER_SUCCESS:
164 source->io_events = 0;
166 mpd_glib_invoke(source);
169 case MPD_PARSER_ERROR:
171 source->io_events = 0;
173 mpd_glib_invoke_error(source, MPD_ERROR_SERVER,
174 mpd_parser_get_server_error(source->parser),
175 mpd_parser_get_message(source->parser));
178 case MPD_PARSER_PAIR:
179 if (strcmp(mpd_parser_get_name(source->parser),
181 source->idle_events |=
182 mpd_idle_name_parse(mpd_parser_get_value(source->parser));
191 * Receives and evaluates a portion of the MPD response.
193 * @return true on success, false on error
196 mpd_glib_recv(MpdIdleSource *source)
199 while ((line = mpd_async_recv_line(source->async)) != nullptr) {
200 if (!mpd_glib_feed(source, line))
204 if (mpd_async_get_error(source->async) != MPD_ERROR_SUCCESS) {
206 source->io_events = 0;
208 mpd_glib_invoke_async_error(source);
216 mpd_glib_source_callback(gcc_unused GIOChannel *_source,
217 GIOCondition condition, gpointer data)
219 auto *source = (MpdIdleSource *)data;
221 assert(source->id != 0);
222 assert(source->io_events != 0);
224 /* let libmpdclient do some I/O */
226 if (!mpd_async_io(source->async,
227 g_io_condition_to_mpd_async_event(condition))) {
229 source->io_events = 0;
231 mpd_glib_invoke_async_error(source);
235 /* receive the response */
237 if ((condition & G_IO_IN) != 0) {
238 if (!mpd_glib_recv(source))
242 /* continue polling? */
244 enum mpd_async_event events = mpd_async_events(source->async);
246 /* no events - disable watch */
248 source->io_events = 0;
251 } else if (events != source->io_events) {
252 /* different event mask: make new watch */
254 g_source_remove(source->id);
256 condition = mpd_async_events_to_g_io_condition(events);
257 source->id = g_io_add_watch(source->channel, condition,
258 mpd_glib_source_callback, source);
259 source->io_events = events;
263 /* same event mask as before, enable the old watch */
268 mpd_glib_add_watch(MpdIdleSource *source)
270 enum mpd_async_event events = mpd_async_events(source->async);
272 assert(source->io_events == 0);
273 assert(source->id == 0);
275 GIOCondition condition = mpd_async_events_to_g_io_condition(events);
276 source->id = g_io_add_watch(source->channel, condition,
277 mpd_glib_source_callback, source);
278 source->io_events = events;
282 MpdIdleSource::Enter()
284 assert(io_events == 0);
289 if (!mpd_async_send_command(async, "idle", nullptr)) {
290 mpd_glib_invoke_async_error(this);
294 mpd_glib_add_watch(this);
299 MpdIdleSource::Leave()
302 /* already left, callback was invoked */
309 enum mpd_idle events = idle_events == 0
310 ? mpd_run_noidle(connection)
311 : mpd_recv_idle(connection, false);
314 mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS) {
315 enum mpd_error error =
316 mpd_connection_get_error(connection);
317 enum mpd_server_error server_error =
318 error == MPD_ERROR_SERVER
319 ? mpd_connection_get_server_error(connection)
320 : (enum mpd_server_error)0;
322 mpd_glib_invoke_error(this, error, server_error,
323 mpd_connection_get_error_message(connection));
327 idle_events |= events;
328 mpd_glib_invoke(this);