1 /***************************************************************************
2 * Copyright (C) 2008-2014 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 ***************************************************************************/
25 #include <netinet/tcp.h>
26 #include <netinet/in.h>
28 #include <boost/locale.hpp>
39 #include "configuration.h"
48 #include "statusbar.h"
49 #include "visualizer.h"
51 #include "utility/conversion.h"
55 std::ofstream errorlog
;
56 std::streambuf
*cerr_buffer
;
57 bool run_resize_screen
= false;
60 void sighandler(int sig
)
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
79 // restore old cerr buffer
80 std::cerr
.rdbuf(cerr_buffer
);
83 # ifndef USE_PDCURSES // destroying screen somehow crashes pdcurses
85 # endif // USE_PDCURSES
90 int main(int argc
, char **argv
)
92 using Global::myScreen
;
94 using Global::wHeader
;
95 using Global::wFooter
;
97 using Global::VolumeState
;
100 srand(time(nullptr));
101 std::setlocale(LC_ALL
, "");
102 std::locale::global(Charset::internalLocale());
104 if (!configure(argc
, argv
))
107 // always execute these commands, even if ncmpcpp use exit function
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
)
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();
143 bool key_pressed
= false;
144 Key input
= Key::noOp
;
145 auto past
= boost::posix_time::from_time_t(0);
149 if (Config
.mouse_support
)
150 mousemask(ALL_MOUSE_EVENTS
, 0);
153 signal(SIGPIPE
, sighandler
);
154 signal(SIGWINCH
, sighandler
);
159 while (!Actions::ExitMainLoop
)
163 if (!Mpd
.Connected())
165 wFooter
->clearFDCallbacksList();
169 if (Mpd
.Version() < 16)
172 throw MPD::ClientError(MPD_ERROR_STATE
, "MPD < 0.16.0 is not supported", false);
174 wFooter
->addFDCallback(Mpd
.GetFD(), Statusbar::Helpers::mpd
);
175 Status::update(-1); // we need info about new connection
177 if (Config
.jump_to_now_playing_song_at_start
)
179 int curr_pos
= Status::get().currentSongPosition();
181 myPlaylist
->main().highlight(curr_pos
);
184 // Set TCP_NODELAY on the tcp socket as this significantly speeds up operations.
186 setsockopt(Mpd
.GetFD(), IPPROTO_TCP
, TCP_NODELAY
, &flag
, sizeof(flag
));
188 // go to startup screen
189 if (Config
.startup_screen_type
!= myScreen
->type())
190 toScreen(Config
.startup_screen_type
)->switchTo();
193 myBrowser
->fetchSupportedExtensions();
194 # ifdef ENABLE_OUTPUTS
195 myOutputs
->FetchList();
196 # endif // ENABLE_OUTPUTS
197 # ifdef ENABLE_VISUALIZER
198 myVisualizer
->ResetFD();
199 if (myScreen
== myVisualizer
)
200 myVisualizer
->SetFD();
201 myVisualizer
->FindOutputID();
202 # endif // ENABLE_VISUALIZER
204 Statusbar::printf("Connected to %1%", Mpd
.GetHostname());
206 catch (MPD::ClientError
&e
)
208 Status::handleClientError(e
);
212 // update timer, status if necessary etc.
213 Status::trace(!key_pressed
, true);
215 if (run_resize_screen
)
217 Actions::resizeScreen(true);
218 run_resize_screen
= false;
222 if ((myScreen
== myPlaylist
|| myScreen
== myBrowser
|| myScreen
== myLyrics
)
223 && (Timer
- past
> boost::posix_time::milliseconds(500))
231 myScreen
->refreshWindow();
232 input
= Key::read(*wFooter
);
233 key_pressed
= input
!= Key::noOp
;
238 // The reason we want to update timer here is that if the timer is updated
239 // in Status::trace, then Key::read usually blocks for 500ms and if key is
240 // pressed 400ms after Key::read was called, we end up with Timer that is
241 // ~400ms inaccurate. On the other hand, if keys are being pressed, we don't
242 // want to update timer in both Status::trace and here. Therefore we update
243 // timer in Status::trace only if there was no recent input.
244 Timer
= boost::posix_time::microsec_clock::local_time();
248 auto k
= Bindings
.get(input
);
249 std::any_of(k
.first
, k
.second
, boost::bind(&Binding::execute
, _1
));
251 catch (ConversionError
&e
)
253 Statusbar::printf("Couldn't convert value \"%1%\" to target type", e
.value());
255 catch (OutOfBounds
&e
)
257 Statusbar::printf("Error: %1%", e
.errorMessage());
260 if (myScreen
== myPlaylist
)
261 myPlaylist
->EnableHighlighting();
263 catch (MPD::ClientError
&e
)
265 Status::handleClientError(e
);
267 catch (MPD::ServerError
&e
)
269 Status::handleServerError(e
);
271 catch (std::exception
&e
)
273 Statusbar::printf("Unexpected error: %1%", e
.what());