plugin: convert plugin_fd_add() to method
[ncmpc-debian.git] / src / Instance.hxx
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2019 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 #ifndef NCMPC_INSTANCE_HXX
21 #define NCMPC_INSTANCE_HXX
22
23 #include "config.h"
24 #include "AsyncUserInput.hxx"
25 #include "mpdclient.hxx"
26 #include "DelayedSeek.hxx"
27 #include "screen.hxx"
28
29 #ifdef ENABLE_LIRC
30 #include "lirc.hxx"
31 #endif
32
33 #include <boost/asio/io_service.hpp>
34 #include <boost/asio/steady_timer.hpp>
35 #ifndef _WIN32
36 #include <boost/asio/signal_set.hpp>
37 #endif
38
39 /**
40  * A singleton holding global instance variables.
41  */
42 class Instance {
43         boost::asio::io_service io_service;
44
45 #ifndef _WIN32
46         boost::asio::signal_set sigterm, sigwinch;
47 #endif
48
49         struct mpdclient client;
50
51         DelayedSeek seek;
52
53         /**
54          * This timer is installed when the connection to the MPD
55          * server is broken.  It tries to recover by reconnecting
56          * periodically.
57          */
58         boost::asio::steady_timer reconnect_timer;
59
60         boost::asio::steady_timer update_timer;
61         bool pending_update_timer = false;
62
63 #ifndef NCMPC_MINI
64         boost::asio::steady_timer check_key_bindings_timer;
65 #endif
66
67         ScreenManager screen_manager;
68
69 #ifdef ENABLE_LIRC
70         LircInput lirc_input;
71 #endif
72
73         AsyncUserInput user_input;
74
75 public:
76         Instance();
77         ~Instance();
78
79         Instance(const Instance &) = delete;
80         Instance &operator=(const Instance &) = delete;
81
82         auto &get_io_service() {
83                 return io_service;
84         }
85
86         auto &GetClient() {
87                 return client;
88         }
89
90         auto &GetSeek() noexcept {
91                 return seek;
92         }
93
94         auto &GetScreenManager() {
95                 return screen_manager;
96         }
97
98         void UpdateClient() noexcept;
99
100         void Run();
101
102         template<typename D>
103         void ScheduleReconnect(const D &expiry_time) {
104                 boost::system::error_code error;
105                 reconnect_timer.expires_from_now(expiry_time, error);
106                 reconnect_timer.async_wait(std::bind(&Instance::OnReconnectTimer,
107                                                      this,
108                                                      std::placeholders::_1));
109         }
110
111         void EnableUpdateTimer() noexcept {
112                 if (!pending_update_timer)
113                         ScheduleUpdateTimer();
114         }
115
116         void DisableUpdateTimer() noexcept {
117                 if (pending_update_timer) {
118                         pending_update_timer = false;
119                         update_timer.cancel();
120                 }
121         }
122
123 #ifndef NCMPC_MINI
124         void ScheduleCheckKeyBindings() noexcept {
125                 boost::system::error_code error;
126                 check_key_bindings_timer.expires_from_now(std::chrono::seconds(10),
127                                                           error);
128                 check_key_bindings_timer.async_wait(std::bind(&Instance::OnCheckKeyBindings,
129                                                               this,
130                                                               std::placeholders::_1));
131         }
132 #endif
133
134 private:
135 #ifndef _WIN32
136         void InitSignals();
137         void OnSigwinch();
138         void AsyncWaitSigwinch();
139 #endif
140
141         void OnReconnectTimer(const boost::system::error_code &error) noexcept;
142
143         void OnUpdateTimer(const boost::system::error_code &error) noexcept;
144
145         void ScheduleUpdateTimer() noexcept {
146                 pending_update_timer = true;
147                 boost::system::error_code error;
148                 update_timer.expires_from_now(std::chrono::milliseconds(500),
149                                               error);
150                 update_timer.async_wait(std::bind(&Instance::OnUpdateTimer,
151                                                   this,
152                                                   std::placeholders::_1));
153         }
154
155 #ifndef NCMPC_MINI
156         void OnCheckKeyBindings(const boost::system::error_code &error) noexcept;
157 #endif
158 };
159
160 #endif