From 9306f56c3400b596b91c4d2515dd5fcd8e948748 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Wed, 7 Dec 2016 19:46:38 +0100 Subject: [PATCH] Exit if stdin is closed --- NEWS | 1 + src/bindings.cpp | 4 +--- src/window.cpp | 36 ++++++++++++++++++++++-------------- src/window.h | 1 + 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/NEWS b/NEWS index 7c6be54f..b925cbd7 100644 --- a/NEWS +++ b/NEWS @@ -11,6 +11,7 @@ ncmpcpp-0.8 (????-??-??) * libcurl dependency is no longer optional. * When an attempt to write tags fails, show detailed error message. * Support for fetching lyrics for selected items in background was added. +* Application will now exit if stdin is closed. ncmpcpp-0.7.7 (2016-10-31) * Fixed compilation on 32bit platforms. diff --git a/src/bindings.cpp b/src/bindings.cpp index a0d1f1cd..4d89c47b 100644 --- a/src/bindings.cpp +++ b/src/bindings.cpp @@ -474,6 +474,7 @@ bool BindingsConfiguration::read(const std::string &file) void BindingsConfiguration::generateDefaults() { NC::Key::Type k = NC::Key::None; + bind(NC::Key::EoF, Actions::Type::Quit); if (notBound(k = stringToKey("mouse"))) bind(k, Actions::Type::MouseEvent); if (notBound(k = stringToKey("up"))) @@ -743,9 +744,6 @@ void BindingsConfiguration::generateDefaults() bind(k, Actions::Type::SetSelectedItemsPriority); if (notBound(k = stringToKey("q"))) bind(k, Actions::Type::Quit); - - if (notBound(k = stringToKey("-"))) - bind(k, Actions::Type::VolumeDown); } const Command *BindingsConfiguration::findCommand(const std::string &name) diff --git a/src/window.cpp b/src/window.cpp index 41edc7f3..fc910458 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -1062,29 +1062,37 @@ Key::Type Window::readKey() return result; } - fd_set fdset; - FD_ZERO(&fdset); - FD_SET(STDIN_FILENO, &fdset); + fd_set fds_read; + FD_ZERO(&fds_read); + FD_SET(STDIN_FILENO, &fds_read); timeval timeout = { m_window_timeout/1000, (m_window_timeout%1000)*1000 }; int fd_max = STDIN_FILENO; - for (FDCallbacks::const_iterator it = m_fds.begin(); it != m_fds.end(); ++it) + for (const auto &fd : m_fds) { - if (it->first > fd_max) - fd_max = it->first; - FD_SET(it->first, &fdset); + if (fd.first > fd_max) + fd_max = fd.first; + FD_SET(fd.first, &fds_read); } - - if (select(fd_max+1, &fdset, 0, 0, m_window_timeout < 0 ? 0 : &timeout) > 0) + + auto tv_addr = m_window_timeout < 0 ? nullptr : &timeout; + int res = select(fd_max+1, &fds_read, nullptr, nullptr, tv_addr); + if (res > 0) { - if (FD_ISSET(STDIN_FILENO, &fdset)) - result = getInputChar(wgetch(m_window)); + if (FD_ISSET(STDIN_FILENO, &fds_read)) + { + int key = wgetch(m_window); + if (key == EOF) + result = Key::EoF; + else + result = getInputChar(key); + } else result = Key::None; - for (FDCallbacks::const_iterator it = m_fds.begin(); it != m_fds.end(); ++it) - if (FD_ISSET(it->first, &fdset)) - it->second(); + for (const auto &fd : m_fds) + if (FD_ISSET(fd.first, &fds_read)) + fd.second(); } else result = Key::None; diff --git a/src/window.h b/src/window.h index 76c69b39..223dad39 100644 --- a/src/window.h +++ b/src/window.h @@ -124,6 +124,7 @@ const Type F10 = Special | 275; const Type F11 = Special | 276; const Type F12 = Special | 277; const Type Mouse = Special | 278; +const Type EoF = Special | 279; }