Do mpd status update after every call to noidle if appropriate
[ncmpcpp.git] / src / ncmpcpp.cpp
blob6b609d7c51a0474791af47995b5afbe0a12dde13
1 /***************************************************************************
2 * Copyright (C) 2008-2016 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
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 *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include <cerrno>
22 #include <clocale>
23 #include <csignal>
24 #include <cstring>
26 #include <boost/date_time/posix_time/posix_time.hpp>
27 #include <boost/locale.hpp>
28 #include <iostream>
29 #include <fstream>
30 #include <stdexcept>
32 #include "mpdpp.h"
34 #include "actions.h"
35 #include "bindings.h"
36 #include "screens/browser.h"
37 #include "charset.h"
38 #include "configuration.h"
39 #include "global.h"
40 #include "helpers.h"
41 #include "screens/lyrics.h"
42 #include "screens/outputs.h"
43 #include "screens/playlist.h"
44 #include "settings.h"
45 #include "status.h"
46 #include "statusbar.h"
47 #include "screens/visualizer.h"
48 #include "title.h"
49 #include "utility/conversion.h"
51 namespace ph = std::placeholders;
53 namespace {
55 std::ofstream errorlog;
56 std::streambuf *cerr_buffer;
58 volatile bool run_resize_screen = false;
60 void sighandler(int sig)
62 if (sig == SIGWINCH)
63 run_resize_screen = true;
64 #if defined(__sun) && defined(__SVR4)
65 // in solaris it is needed to reinstall the handler each time it's executed
66 signal(sig, sighandler);
67 #endif // __sun && __SVR4
70 void do_at_exit()
72 // restore old cerr buffer
73 std::cerr.rdbuf(cerr_buffer);
74 errorlog.close();
75 Mpd.Disconnect();
76 NC::destroyScreen();
77 windowTitle("");
82 int main(int argc, char **argv)
84 using Global::myScreen;
86 using Global::wHeader;
87 using Global::wFooter;
89 using Global::VolumeState;
90 using Global::Timer;
92 std::setlocale(LC_ALL, "");
93 std::locale::global(Charset::internalLocale());
95 if (!configure(argc, argv))
96 return 0;
98 // always execute these commands, even if ncmpcpp use exit function
99 atexit(do_at_exit);
101 // redirect std::cerr output to ~/.ncmpcpp/error.log file
102 errorlog.open((Config.ncmpcpp_directory + "error.log").c_str(), std::ios::app);
103 cerr_buffer = std::cerr.rdbuf();
104 std::cerr.rdbuf(errorlog.rdbuf());
106 sigignore(SIGPIPE);
107 signal(SIGWINCH, sighandler);
109 Mpd.setNoidleCallback(Status::update);
111 NC::initScreen(Config.colors_enabled, Config.mouse_support);
113 Actions::OriginalStatusbarVisibility = Config.statusbar_visibility;
115 if (Config.design == Design::Alternative)
116 Config.statusbar_visibility = 0;
118 Actions::setWindowsDimensions();
119 Actions::validateScreenSize();
120 Actions::initializeScreens();
122 wHeader = new NC::Window(0, 0, COLS, Actions::HeaderHeight, "", Config.header_color, NC::Border());
123 if (Config.header_visibility || Config.design == Design::Alternative)
124 wHeader->display();
126 wFooter = new NC::Window(0, Actions::FooterStartY, COLS, Actions::FooterHeight, "", Config.statusbar_color, NC::Border());
127 wFooter->setPromptHook(Statusbar::Helpers::mainHook);
129 // initialize global timer
130 Timer = boost::posix_time::microsec_clock::local_time();
132 // initialize global random number generator
133 Global::RNG.seed(std::random_device()());
135 // initialize playlist
136 myPlaylist->switchTo();
138 // go to startup screen
139 if (Config.startup_screen_type != myScreen->type())
141 auto startup_screen = toScreen(Config.startup_screen_type);
142 assert(startup_screen != nullptr);
143 startup_screen->switchTo();
146 // lock current screen and go to the slave one if applicable
147 if (Config.startup_slave_screen_type)
149 auto slave_screen_type = *Config.startup_slave_screen_type;
150 bool screen_locked = myScreen->lock();
151 if (screen_locked && slave_screen_type != myScreen->type())
153 auto slave_screen = toScreen(slave_screen_type);
154 assert(slave_screen != nullptr);
155 slave_screen->switchTo();
156 if (!Config.startup_slave_screen_focus)
157 Actions::get(Actions::Type::MasterScreen).execute();
161 // local variables
162 bool key_pressed = false;
163 auto input = NC::Key::None;
164 auto connect_attempt = boost::posix_time::from_time_t(0);
165 auto update_environment = static_cast<Actions::UpdateEnvironment &>(
166 Actions::get(Actions::Type::UpdateEnvironment));
168 while (!Actions::ExitMainLoop)
172 if (!Mpd.Connected() && Timer - connect_attempt > boost::posix_time::seconds(1))
174 connect_attempt = Timer;
175 // reset local status info
176 Status::clear();
177 // clear mpd callback
178 wFooter->clearFDCallbacksList();
181 Mpd.Connect();
182 if (Mpd.Version() < 16)
184 Mpd.Disconnect();
185 throw MPD::ClientError(MPD_ERROR_STATE, "MPD < 0.16.0 is not supported", false);
188 catch (MPD::ClientError &e)
190 Status::handleClientError(e);
194 if (run_resize_screen)
196 Actions::resizeScreen(true);
197 run_resize_screen = false;
200 update_environment.run(!key_pressed, key_pressed, false);
202 input = readKey(*wFooter);
203 key_pressed = input != NC::Key::None;
204 if (!key_pressed)
205 continue;
207 // The reason we want to update timer here is that if the timer is updated
208 // in Status::trace, then Key::read usually blocks for 500ms and if key is
209 // pressed 400ms after Key::read was called, we end up with Timer that is
210 // ~400ms inaccurate. On the other hand, if keys are being pressed, we don't
211 // want to update timer in both Status::trace and here. Therefore we update
212 // timer in Status::trace only if there was no recent input.
213 Timer = boost::posix_time::microsec_clock::local_time();
217 auto k = Bindings.get(input);
218 std::any_of(k.first, k.second, std::bind(&Binding::execute, ph::_1));
220 catch (ConversionError &e)
222 Statusbar::printf("Invalid value: %1%", e.value());
224 catch (OutOfBounds &e)
226 Statusbar::printf("Error: %1%", e.errorMessage());
228 catch (NC::PromptAborted &)
230 Statusbar::printf("Action aborted");
233 if (myScreen == myPlaylist)
234 myPlaylist->enableHighlighting();
236 catch (MPD::ClientError &e)
238 Status::handleClientError(e);
240 catch (MPD::ServerError &e)
242 Status::handleServerError(e);
244 catch (std::exception &e)
246 Statusbar::printf("Unexpected error: %1%", e.what());
249 return 0;