window: support extended urxvt mouse support if applicable
[ncmpcpp.git] / src / ncmpcpp.cpp
blobb01c13d17e3447344344eba8c5630977876239d2
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>
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 "browser.h"
37 #include "charset.h"
38 #include "configuration.h"
39 #include "global.h"
40 #include "helpers.h"
41 #include "lyrics.h"
42 #include "outputs.h"
43 #include "playlist.h"
44 #include "settings.h"
45 #include "status.h"
46 #include "statusbar.h"
47 #include "visualizer.h"
48 #include "title.h"
49 #include "utility/conversion.h"
51 namespace
53 std::ofstream errorlog;
54 std::streambuf *cerr_buffer;
55 bool run_resize_screen = false;
57 void sighandler(int sig)
59 if (sig == SIGWINCH)
61 run_resize_screen = true;
63 # if defined(__sun) && defined(__SVR4)
64 // in solaris it is needed to reinstall the handler each time it's executed
65 signal(sig, sighandler);
66 # endif // __sun && __SVR4
69 void do_at_exit()
71 // restore old cerr buffer
72 std::cerr.rdbuf(cerr_buffer);
73 errorlog.close();
74 Mpd.Disconnect();
75 NC::destroyScreen();
76 windowTitle("");
80 int main(int argc, char **argv)
82 using Global::myScreen;
84 using Global::wHeader;
85 using Global::wFooter;
87 using Global::VolumeState;
88 using Global::Timer;
90 srand(time(nullptr));
91 std::setlocale(LC_ALL, "");
92 std::locale::global(Charset::internalLocale());
94 if (!configure(argc, argv))
95 return 0;
97 // always execute these commands, even if ncmpcpp use exit function
98 atexit(do_at_exit);
100 // redirect std::cerr output to ~/.ncmpcpp/error.log file
101 errorlog.open((Config.ncmpcpp_directory + "error.log").c_str(), std::ios::app);
102 cerr_buffer = std::cerr.rdbuf();
103 std::cerr.rdbuf(errorlog.rdbuf());
105 # ifndef WIN32
106 signal(SIGPIPE, sighandler);
107 signal(SIGWINCH, sighandler);
108 // ignore Ctrl-C
109 sigignore(SIGINT);
110 # endif // !WIN32
112 NC::initScreen(Config.colors_enabled, Config.mouse_support);
114 Actions::OriginalStatusbarVisibility = Config.statusbar_visibility;
116 if (Config.design == Design::Alternative)
117 Config.statusbar_visibility = 0;
119 Actions::setWindowsDimensions();
120 Actions::validateScreenSize();
121 Actions::initializeScreens();
123 wHeader = new NC::Window(0, 0, COLS, Actions::HeaderHeight, "", Config.header_color, NC::Border());
124 if (Config.header_visibility || Config.design == Design::Alternative)
125 wHeader->display();
127 wFooter = new NC::Window(0, Actions::FooterStartY, COLS, Actions::FooterHeight, "", Config.statusbar_color, NC::Border());
128 wFooter->setPromptHook(Statusbar::Helpers::mainHook);
130 // initialize global timer
131 Timer = boost::posix_time::microsec_clock::local_time();
133 // initialize playlist
134 myPlaylist->switchTo();
136 // go to startup screen
137 if (Config.startup_screen_type != myScreen->type())
138 toScreen(Config.startup_screen_type)->switchTo();
140 // lock current screen and go to the slave one if applicable
141 if (Config.startup_slave_screen_type)
143 auto slave_screen = *Config.startup_slave_screen_type;
144 bool screen_locked = myScreen->lock();
145 if (screen_locked && slave_screen != myScreen->type())
146 toScreen(slave_screen)->switchTo();
149 // local variables
150 bool key_pressed = false;
151 Key input = Key::noOp;
152 auto connect_attempt = boost::posix_time::from_time_t(0);
153 auto past = boost::posix_time::from_time_t(0);
155 while (!Actions::ExitMainLoop)
159 if (!Mpd.Connected() && Timer - connect_attempt > boost::posix_time::seconds(1))
161 connect_attempt = Timer;
162 // reset local status info
163 Status::clear();
164 // clear mpd callback
165 wFooter->clearFDCallbacksList();
168 Mpd.Connect();
169 if (Mpd.Version() < 16)
171 Mpd.Disconnect();
172 throw MPD::ClientError(MPD_ERROR_STATE, "MPD < 0.16.0 is not supported", false);
175 catch (MPD::ClientError &e)
177 Status::handleClientError(e);
181 // update timer, status if necessary etc.
182 Status::trace(!key_pressed, true);
184 if (run_resize_screen)
186 Actions::resizeScreen(true);
187 run_resize_screen = false;
190 // header stuff
191 if ((myScreen == myPlaylist || myScreen == myBrowser || myScreen == myLyrics)
192 && (Timer - past > boost::posix_time::milliseconds(500))
195 drawHeader();
196 past = Timer;
199 if (key_pressed)
200 myScreen->refreshWindow();
201 input = Key::read(*wFooter);
202 key_pressed = input != Key::noOp;
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, boost::bind(&Binding::execute, _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;