1 /***************************************************************************
2 * Copyright (C) 2008-2016 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
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. *
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. *
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 ***************************************************************************/
26 #include <boost/date_time/posix_time/posix_time.hpp>
27 #include <boost/locale.hpp>
38 #include "configuration.h"
46 #include "statusbar.h"
47 #include "visualizer.h"
49 #include "utility/conversion.h"
51 namespace ph
= std::placeholders
;
55 std::ofstream errorlog
;
56 std::streambuf
*cerr_buffer
;
57 bool run_resize_screen
= false;
59 void sighandler(int sig
)
63 run_resize_screen
= true;
65 # if defined(__sun) && defined(__SVR4)
66 // in solaris it is needed to reinstall the handler each time it's executed
67 signal(sig
, sighandler
);
68 # endif // __sun && __SVR4
73 // restore old cerr buffer
74 std::cerr
.rdbuf(cerr_buffer
);
82 int main(int argc
, char **argv
)
84 using Global::myScreen
;
86 using Global::wHeader
;
87 using Global::wFooter
;
89 using Global::VolumeState
;
92 std::setlocale(LC_ALL
, "");
93 std::locale::global(Charset::internalLocale());
95 if (!configure(argc
, argv
))
98 // always execute these commands, even if ncmpcpp use exit function
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());
107 signal(SIGPIPE
, sighandler
);
108 signal(SIGWINCH
, sighandler
);
113 NC::initScreen(Config
.colors_enabled
, Config
.mouse_support
);
115 Actions::OriginalStatusbarVisibility
= Config
.statusbar_visibility
;
117 if (Config
.design
== Design::Alternative
)
118 Config
.statusbar_visibility
= 0;
120 Actions::setWindowsDimensions();
121 Actions::validateScreenSize();
122 Actions::initializeScreens();
124 wHeader
= new NC::Window(0, 0, COLS
, Actions::HeaderHeight
, "", Config
.header_color
, NC::Border());
125 if (Config
.header_visibility
|| Config
.design
== Design::Alternative
)
128 wFooter
= new NC::Window(0, Actions::FooterStartY
, COLS
, Actions::FooterHeight
, "", Config
.statusbar_color
, NC::Border());
129 wFooter
->setPromptHook(Statusbar::Helpers::mainHook
);
131 // initialize global timer
132 Timer
= boost::posix_time::microsec_clock::local_time();
134 // initialize global random number generator
135 Global::RNG
.seed(std::random_device()());
137 // initialize playlist
138 myPlaylist
->switchTo();
140 // go to startup screen
141 if (Config
.startup_screen_type
!= myScreen
->type())
143 auto startup_screen
= toScreen(Config
.startup_screen_type
);
144 assert(startup_screen
!= nullptr);
145 startup_screen
->switchTo();
148 // lock current screen and go to the slave one if applicable
149 if (Config
.startup_slave_screen_type
)
151 auto slave_screen_type
= *Config
.startup_slave_screen_type
;
152 bool screen_locked
= myScreen
->lock();
153 if (screen_locked
&& slave_screen_type
!= myScreen
->type())
155 auto slave_screen
= toScreen(slave_screen_type
);
156 assert(slave_screen
!= nullptr);
157 slave_screen
->switchTo();
158 if (!Config
.startup_slave_screen_focus
)
159 Actions::get(Actions::Type::MasterScreen
).execute();
164 bool key_pressed
= false;
165 auto input
= NC::Key::None
;
166 auto connect_attempt
= boost::posix_time::from_time_t(0);
167 auto update_environment
= static_cast<Actions::UpdateEnvironment
&>(
168 Actions::get(Actions::Type::UpdateEnvironment
)
171 while (!Actions::ExitMainLoop
)
175 if (!Mpd
.Connected() && Timer
- connect_attempt
> boost::posix_time::seconds(1))
177 connect_attempt
= Timer
;
178 // reset local status info
180 // clear mpd callback
181 wFooter
->clearFDCallbacksList();
185 if (Mpd
.Version() < 16)
188 throw MPD::ClientError(MPD_ERROR_STATE
, "MPD < 0.16.0 is not supported", false);
191 catch (MPD::ClientError
&e
)
193 Status::handleClientError(e
);
197 if (run_resize_screen
)
199 Actions::resizeScreen(true);
200 run_resize_screen
= false;
203 update_environment
.run(!key_pressed
, key_pressed
);
205 input
= readKey(*wFooter
);
206 key_pressed
= input
!= NC::Key::None
;
210 //Statusbar::print(ToString(keyToWString(input)));
212 // The reason we want to update timer here is that if the timer is updated
213 // in Status::trace, then Key::read usually blocks for 500ms and if key is
214 // pressed 400ms after Key::read was called, we end up with Timer that is
215 // ~400ms inaccurate. On the other hand, if keys are being pressed, we don't
216 // want to update timer in both Status::trace and here. Therefore we update
217 // timer in Status::trace only if there was no recent input.
218 Timer
= boost::posix_time::microsec_clock::local_time();
222 auto k
= Bindings
.get(input
);
223 std::any_of(k
.first
, k
.second
, std::bind(&Binding::execute
, ph::_1
));
225 catch (ConversionError
&e
)
227 Statusbar::printf("Invalid value: %1%", e
.value());
229 catch (OutOfBounds
&e
)
231 Statusbar::printf("Error: %1%", e
.errorMessage());
233 catch (NC::PromptAborted
&)
235 Statusbar::printf("Action aborted");
238 if (myScreen
== myPlaylist
)
239 myPlaylist
->enableHighlighting();
241 catch (MPD::ClientError
&e
)
243 Status::handleClientError(e
);
245 catch (MPD::ServerError
&e
)
247 Status::handleServerError(e
);
249 catch (std::exception
&e
)
251 Statusbar::printf("Unexpected error: %1%", e
.what());