1 /* ncmpc (Ncurses MPD Client)
2    (c) 2004-2020 The Music Player Daemon Project
3    Project homepage: http://musicpd.org
4 
5    Redistribution and use in source and binary forms, with or without
6    modification, are permitted provided that the following conditions
7    are met:
8 
9    - Redistributions of source code must retain the above copyright
10    notice, this list of conditions and the following disclaimer.
11 
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.
15 
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.
27 */
28 
29 #ifndef MPD_GLIB_SOURCE_H
30 #define MPD_GLIB_SOURCE_H
31 
32 #include "event/SocketEvent.hxx"
33 
34 #include <mpd/client.h>
35 
36 class MpdIdleHandler {
37 public:
38 	virtual void OnIdle(unsigned events) noexcept = 0;
39 	virtual void OnIdleError(enum mpd_error error,
40 				 enum mpd_server_error server_error,
41 				 const char *message) noexcept = 0;
42 };
43 
44 class MpdIdleSource final {
45 	struct mpd_connection *connection;
46 	struct mpd_async *async;
47 	struct mpd_parser *parser;
48 
49 	SocketEvent event;
50 
51 	MpdIdleHandler &handler;
52 
53 	unsigned io_events = 0;
54 
55 	unsigned idle_events;
56 
57 public:
58 	MpdIdleSource(EventLoop &event_loop,
59 		      struct mpd_connection &_connection,
60 		      MpdIdleHandler &_handler) noexcept;
61 	~MpdIdleSource() noexcept;
62 
63 	/**
64 	 * Enters idle mode.
65 	 *
66 	 * @return true if idle mode has been entered, false if not
67 	 * (e.g. I/O error)
68 	 */
69 	bool Enter() noexcept;
70 
71 	/**
72 	 * Leaves idle mode and invokes the callback if there were events.
73 	 */
74 	void Leave() noexcept;
75 
76 private:
InvokeCallback()77 	void InvokeCallback() noexcept {
78 		if (idle_events != 0)
79 			handler.OnIdle(idle_events);
80 	}
81 
InvokeError(enum mpd_error error,enum mpd_server_error server_error,const char * message)82 	void InvokeError(enum mpd_error error,
83 			 enum mpd_server_error server_error,
84 			 const char *message) noexcept {
85 		handler.OnIdleError(error, server_error, message);
86 	}
87 
88 	void InvokeAsyncError() noexcept;
89 
90 	/**
91 	 * Parses a response line from MPD.
92 	 *
93 	 * @return true on success, false on error
94 	 */
95 	bool Feed(char *line) noexcept;
96 
97 	/**
98 	 * Receives and evaluates a portion of the MPD response.
99 	 *
100 	 * @return true on success, false on error
101 	 */
102 	bool Receive() noexcept;
103 
104 	void OnSocketReady(unsigned flags) noexcept;
105 
106 	void UpdateSocket() noexcept;
107 };
108 
109 #endif
110