catch all exceptions at lower level
[ncmpcpp.git] / src / ncmpcpp.cpp
blobb46f88445a750d0021b9e4c2f0736992ae400593
1 /***************************************************************************
2 * Copyright (C) 2008-2014 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>
25 #include <netinet/tcp.h>
26 #include <netinet/in.h>
28 #include <boost/locale.hpp>
29 #include <iostream>
30 #include <fstream>
31 #include <stdexcept>
33 #include "mpdpp.h"
35 #include "actions.h"
36 #include "bindings.h"
37 #include "browser.h"
38 #include "charset.h"
39 #include "configuration.h"
40 #include "global.h"
41 #include "error.h"
42 #include "helpers.h"
43 #include "lyrics.h"
44 #include "outputs.h"
45 #include "playlist.h"
46 #include "settings.h"
47 #include "status.h"
48 #include "statusbar.h"
49 #include "visualizer.h"
50 #include "title.h"
51 #include "utility/conversion.h"
53 namespace
55 std::ofstream errorlog;
56 std::streambuf *cerr_buffer;
57 bool run_resize_screen = false;
59 # if !defined(WIN32)
60 void sighandler(int sig)
62 if (sig == SIGPIPE)
64 Statusbar::print("SIGPIPE (broken pipe signal) received");
66 else if (sig == SIGWINCH)
68 run_resize_screen = true;
70 # if defined(__sun) && defined(__SVR4)
71 // in solaris it is needed to reinstall the handler each time it's executed
72 signal(sig, sighandler);
73 # endif // __sun && __SVR4
75 # endif // !WIN32
77 void do_at_exit()
79 // restore old cerr buffer
80 std::cerr.rdbuf(cerr_buffer);
81 errorlog.close();
82 Mpd.Disconnect();
83 # ifndef USE_PDCURSES // destroying screen somehow crashes pdcurses
84 NC::destroyScreen();
85 # endif // USE_PDCURSES
86 windowTitle("");
90 int main(int argc, char **argv)
92 using Global::myScreen;
94 using Global::wHeader;
95 using Global::wFooter;
97 using Global::VolumeState;
98 using Global::Timer;
100 srand(time(nullptr));
101 std::setlocale(LC_ALL, "");
102 std::locale::global(Charset::internalLocale());
104 if (!configure(argc, argv))
105 return 0;
107 // always execute these commands, even if ncmpcpp use exit function
108 atexit(do_at_exit);
110 // redirect std::cerr output to ~/.ncmpcpp/error.log file
111 errorlog.open((Config.ncmpcpp_directory + "error.log").c_str(), std::ios::app);
112 cerr_buffer = std::cerr.rdbuf();
113 std::cerr.rdbuf(errorlog.rdbuf());
115 NC::initScreen("ncmpcpp ver. " VERSION, Config.colors_enabled);
117 Actions::OriginalStatusbarVisibility = Config.statusbar_visibility;
119 // for column separators
120 attron(COLOR_PAIR(int(Config.main_color)));
122 if (Config.design == Design::Alternative)
123 Config.statusbar_visibility = 0;
125 Actions::setWindowsDimensions();
126 Actions::validateScreenSize();
127 Actions::initializeScreens();
129 wHeader = new NC::Window(0, 0, COLS, Actions::HeaderHeight, "", Config.header_color, NC::Border::None);
130 if (Config.header_visibility || Config.design == Design::Alternative)
131 wHeader->display();
133 wFooter = new NC::Window(0, Actions::FooterStartY, COLS, Actions::FooterHeight, "", Config.statusbar_color, NC::Border::None);
134 wFooter->setGetStringHelper(Statusbar::Helpers::getString);
136 // initialize global timer
137 Timer = boost::posix_time::microsec_clock::local_time();
139 // initialize playlist
140 myPlaylist->switchTo();
142 // local variables
143 bool key_pressed = false;
144 Key input = Key::noOp;
145 auto past = boost::posix_time::from_time_t(0);
147 /// enable mouse
148 mouseinterval(0);
149 if (Config.mouse_support)
150 mousemask(ALL_MOUSE_EVENTS, 0);
152 # ifndef WIN32
153 signal(SIGPIPE, sighandler);
154 signal(SIGWINCH, sighandler);
155 # endif // !WIN32
157 while (!Actions::ExitMainLoop)
161 if (!Mpd.Connected())
163 wFooter->clearFDCallbacksList();
166 Mpd.Connect();
167 if (Mpd.Version() < 16)
169 Mpd.Disconnect();
170 throw MPD::ClientError(MPD_ERROR_STATE, "MPD < 0.16.0 is not supported", false);
172 wFooter->addFDCallback(Mpd.GetFD(), Statusbar::Helpers::mpd);
173 Status::update(-1); // we need info about new connection
175 if (Config.jump_to_now_playing_song_at_start)
177 int curr_pos = myPlaylist->currentSongPosition();
178 if (curr_pos >= 0)
179 myPlaylist->main().highlight(curr_pos);
182 // Set TCP_NODELAY on the tcp socket as this significantly speeds up operations.
183 int flag = 1;
184 setsockopt(Mpd.GetFD(), IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag));
186 // go to startup screen
187 if (Config.startup_screen_type != myScreen->type())
188 toScreen(Config.startup_screen_type)->switchTo();
189 myScreen->refresh();
191 myBrowser->fetchSupportedExtensions();
192 # ifdef ENABLE_OUTPUTS
193 myOutputs->FetchList();
194 # endif // ENABLE_OUTPUTS
195 # ifdef ENABLE_VISUALIZER
196 myVisualizer->ResetFD();
197 if (myScreen == myVisualizer)
198 myVisualizer->SetFD();
199 myVisualizer->FindOutputID();
200 # endif // ENABLE_VISUALIZER
202 Statusbar::printf("Connected to %1%", Mpd.GetHostname());
204 catch (MPD::ClientError &e)
206 Status::handleClientError(e);
210 // update timer, status if necessary etc.
211 Status::trace(!key_pressed);
213 if (run_resize_screen)
215 Actions::resizeScreen(true);
216 run_resize_screen = false;
219 // header stuff
220 if ((myScreen == myPlaylist || myScreen == myBrowser || myScreen == myLyrics)
221 && (Timer - past > boost::posix_time::milliseconds(500))
224 drawHeader();
225 past = Timer;
228 if (key_pressed)
229 myScreen->refreshWindow();
230 input = Key::read(*wFooter);
231 key_pressed = input != Key::noOp;
233 if (!key_pressed)
234 continue;
236 // The reason we want to update timer here is that if the timer is updated
237 // in Status::trace, then Key::read usually blocks for 500ms and if key is
238 // pressed 400ms after Key::read was called, we end up with Timer that is
239 // ~400ms inaccurate. On the other hand, if keys are being pressed, we don't
240 // want to update timer in both Status::trace and here. Therefore we update
241 // timer in Status::trace only if there was no recent input.
242 Timer = boost::posix_time::microsec_clock::local_time();
246 auto k = Bindings.get(input);
247 std::any_of(k.first, k.second, boost::bind(&Binding::execute, _1));
249 catch (ConversionError &e)
251 Statusbar::printf("Couldn't convert value \"%1%\" to target type", e.value());
253 catch (OutOfBounds &e)
255 Statusbar::printf("Error: %1%", e.errorMessage());
258 if (myScreen == myPlaylist)
259 myPlaylist->EnableHighlighting();
261 catch (MPD::ClientError &e)
263 Status::handleClientError(e);
265 catch (MPD::ServerError &e)
267 Status::handleServerError(e);
269 catch (std::exception &e)
271 Statusbar::printf("Unexpected error: %1%", e.what());
274 return 0;