replace timeval and time_t with boost::posix_time::ptime

This commit is contained in:
Andrzej Rybczak
2014-08-27 11:14:07 +02:00
parent 7fdace835b
commit b7ceae7273
17 changed files with 75 additions and 78 deletions

View File

@@ -2593,7 +2593,7 @@ void seek()
Statusbar::lock();
unsigned songpos = Status::State::elapsedTime();
timeval t = Timer;
auto t = Timer;
int old_timeout = wFooter->getTimeout();
wFooter->setTimeout(500);
@@ -2605,9 +2605,10 @@ void seek()
while (true)
{
Status::trace();
myPlaylist->UpdateTimer();
unsigned howmuch = Config.incremental_seeking ? (Timer.tv_sec-t.tv_sec)/2+Config.seek_time : Config.seek_time;
unsigned howmuch = Config.incremental_seeking
? (Timer-t).seconds()/2+Config.seek_time
: Config.seek_time;
Key input = Key::read(*wFooter);
auto k = Bindings.get(input);

View File

@@ -25,7 +25,6 @@
#ifdef ENABLE_CLOCK
#include <cstring>
#include <sys/time.h>
#include "global.h"
#include "playlist.h"
@@ -115,20 +114,20 @@ void Clock::update()
myPlaylist->switchTo();
}
std::tm *time = std::localtime(&Global::Timer.tv_sec);
auto time = boost::posix_time::to_tm(Global::Timer);
mask = 0;
Set(time->tm_sec % 10, 0);
Set(time->tm_sec / 10, 4);
Set(time->tm_min % 10, 10);
Set(time->tm_min / 10, 14);
Set(time->tm_hour % 10, 20);
Set(time->tm_hour / 10, 24);
Set(time.tm_sec % 10, 0);
Set(time.tm_sec / 10, 4);
Set(time.tm_min % 10, 10);
Set(time.tm_min / 10, 14);
Set(time.tm_hour % 10, 20);
Set(time.tm_hour / 10, 24);
Set(10, 7);
Set(10, 17);
char buf[64];
std::strftime(buf, 64, "%x", time);
std::strftime(buf, 64, "%x", &time);
attron(COLOR_PAIR(int(Config.main_color)));
mvprintw(w.getStarty()+w.getHeight(), w.getStartX()+(w.getWidth()-strlen(buf))/2, "%s", buf);
attroff(COLOR_PAIR(int(Config.main_color)));

View File

@@ -36,6 +36,6 @@ bool ShowMessages = false;
bool SeekingInProgress = false;
std::string VolumeState;
timeval Timer;
boost::posix_time::ptime Timer;
}

View File

@@ -21,8 +21,7 @@
#ifndef NCMPCPP_GLOBAL_H
#define NCMPCPP_GLOBAL_H
#include <sys/time.h>
#include <boost/date_time/posix_time/posix_time.hpp>
#include "mpdpp.h"
#include "screen.h"
@@ -57,7 +56,7 @@ extern bool SeekingInProgress;
extern std::string VolumeState;
// global timer
extern timeval Timer;
extern boost::posix_time::ptime Timer;
}

View File

@@ -22,7 +22,6 @@
#include <clocale>
#include <csignal>
#include <cstring>
#include <sys/time.h>
#include <boost/locale.hpp>
#include <iostream>
@@ -160,15 +159,14 @@ int main(int argc, char **argv)
wFooter->setGetStringHelper(Statusbar::Helpers::getString);
// initialize global timer
gettimeofday(&Timer, 0);
Timer = boost::posix_time::microsec_clock::local_time();
// go to playlist
myPlaylist->switchTo();
myPlaylist->UpdateTimer();
// local variables
Key input(0, Key::Standard);
timeval past = { 0, 0 };
boost::posix_time::ptime past = boost::posix_time::from_time_t(0);
// local variables end
mouseinterval(0);
@@ -237,14 +235,13 @@ int main(int argc, char **argv)
}
// header stuff
if (((Timer.tv_sec == past.tv_sec && Timer.tv_usec >= past.tv_usec+500000) || Timer.tv_sec > past.tv_sec)
&& (myScreen == myPlaylist || myScreen == myBrowser || myScreen == myLyrics)
if ((myScreen == myPlaylist || myScreen == myBrowser || myScreen == myLyrics)
&& (Timer - past > boost::posix_time::milliseconds(500))
)
{
drawHeader();
past = Timer;
}
// header stuff end
if (input != Key::noOp)
myScreen->refreshWindow();
@@ -272,7 +269,7 @@ int main(int argc, char **argv)
# ifdef ENABLE_VISUALIZER
// visualizer sets timeout to 40ms, but since only it needs such small
// value, we should restore defalt one after switching to another screen.
// value, we should restore default one after switching to another screen.
if (wFooter->getTimeout() < 500
&& !(myScreen == myVisualizer || myLockedScreen == myVisualizer || myInactiveScreen == myVisualizer)
)

View File

@@ -97,6 +97,17 @@ std::wstring Playlist::title()
return result;
}
void Playlist::update()
{
if (Config.playlist_disable_highlight_delay.time_duration::seconds() > 0
&& w.isHighlighted()
&& Global::Timer - itsTimer > Config.playlist_disable_highlight_delay)
{
w.setHighlighting(false);
w.refresh();
}
}
void Playlist::enterPressed()
{
if (!w.empty())
@@ -260,12 +271,7 @@ void Playlist::Reverse()
void Playlist::EnableHighlighting()
{
w.setHighlighting(true);
UpdateTimer();
}
void Playlist::UpdateTimer()
{
std::time(&itsTimer);
itsTimer = Global::Timer;
}
std::string Playlist::TotalLength()

View File

@@ -38,7 +38,7 @@ struct Playlist: Screen<NC::Menu<MPD::Song>>, Filterable, HasSongs, Searchable,
virtual std::wstring title() OVERRIDE;
virtual ScreenType type() OVERRIDE { return ScreenType::Playlist; }
virtual void update() OVERRIDE { }
virtual void update() OVERRIDE;
virtual void enterPressed() OVERRIDE;
virtual void spacePressed() OVERRIDE;
@@ -71,8 +71,6 @@ struct Playlist: Screen<NC::Menu<MPD::Song>>, Filterable, HasSongs, Searchable,
void Reverse();
void EnableHighlighting();
void UpdateTimer();
time_t Timer() const { return itsTimer; }
void SetSelectedItemsPriority(int prio);
@@ -101,7 +99,7 @@ private:
size_t itsRemainingTime;
size_t itsScrollBegin;
time_t itsTimer;
boost::posix_time::ptime itsTimer;
MPD::Status m_status;
unsigned m_old_playlist_version;

View File

@@ -18,7 +18,6 @@
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <sys/time.h>
#include <iomanip>
#include "global.h"
@@ -75,10 +74,10 @@ std::wstring ServerInfo::title()
void ServerInfo::update()
{
static timeval past = { 0, 0 };
if (Global::Timer.tv_sec <= past.tv_sec)
Statusbar::printf("%1%, %2%", Global::Timer, m_timer);
if (Global::Timer - m_timer < boost::posix_time::seconds(1))
return;
past = Global::Timer;
m_timer = Global::Timer;
MPD::Statistics stats = Mpd.getStatistics();
if (stats.empty())

View File

@@ -48,6 +48,8 @@ protected:
private:
void SetDimensions();
boost::posix_time::ptime m_timer;
MPD::StringList itsURLHandlers;
MPD::StringList itsTagTypes;

View File

@@ -188,13 +188,13 @@ void Configuration::SetDefaults()
crossfade_time = 5;
seek_time = 1;
volume_change_step = 1;
playlist_disable_highlight_delay = 5;
playlist_disable_highlight_delay = boost::posix_time::seconds(5);
message_delay_time = 4;
lyrics_db = 0;
regex_type = boost::regex::literal | boost::regex::icase;
lines_scrolled = 2;
search_engine_default_search_mode = 0;
visualizer_sync_interval = 30;
visualizer_sync_interval = boost::posix_time::seconds(30);
locked_screen_width_part = 0.5;
selected_item_prefix_length = 0;
selected_item_suffix_length = 0;
@@ -212,6 +212,7 @@ void Configuration::SetDefaults()
}
Configuration::Configuration()
: playlist_disable_highlight_delay(0), visualizer_sync_interval(0)
{
# ifdef WIN32
ncmpcpp_directory = GetHomeDirectory() + "ncmpcpp/";
@@ -338,8 +339,7 @@ void Configuration::Read()
}
else if (name == "playlist_disable_highlight_delay")
{
if (boost::lexical_cast<int>(v) >= 0)
playlist_disable_highlight_delay = boost::lexical_cast<int>(v);
playlist_disable_highlight_delay = boost::posix_time::seconds(boost::lexical_cast<int>(v));
}
else if (name == "message_delay_time")
{
@@ -762,7 +762,7 @@ void Configuration::Read()
{
unsigned interval = boost::lexical_cast<unsigned>(v);
if (interval)
visualizer_sync_interval = interval;
visualizer_sync_interval = boost::posix_time::seconds(interval);
}
else if (name == "browser_sort_mode")
{

View File

@@ -21,6 +21,7 @@
#ifndef NCMPCPP_SETTINGS_H
#define NCMPCPP_SETTINGS_H
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/regex.hpp>
#include <cassert>
#include <vector>
@@ -171,7 +172,6 @@ struct Configuration
int crossfade_time;
int seek_time;
int volume_change_step;
int playlist_disable_highlight_delay;
int message_delay_time;
int lyrics_db;
@@ -179,7 +179,9 @@ struct Configuration
unsigned lines_scrolled;
unsigned search_engine_default_search_mode;
unsigned visualizer_sync_interval;
boost::posix_time::seconds playlist_disable_highlight_delay;
boost::posix_time::seconds visualizer_sync_interval;
double locked_screen_width_part;

View File

@@ -18,8 +18,6 @@
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <sys/time.h>
#include "browser.h"
#include "charset.h"
#include "global.h"
@@ -49,7 +47,7 @@ using Global::VolumeState;
namespace {//
timeval past = { 0, 0 };
boost::posix_time::ptime past = boost::posix_time::from_time_t(0);
size_t playing_song_scroll_begin = 0;
size_t first_line_scroll_begin = 0;
@@ -135,10 +133,11 @@ void Status::handleServerError(MPD::ServerError &e)
void Status::trace()
{
gettimeofday(&Timer, 0);
Timer = boost::posix_time::microsec_clock::local_time();
if (Mpd.Connected())
{
if (State::player() == MPD::psPlay && Global::Timer.tv_sec > past.tv_sec)
if (State::player() == MPD::psPlay
&& Global::Timer - past > boost::posix_time::seconds(1))
{
// update elapsed time/bitrate of the current song
Status::Changes::elapsedTime(true);
@@ -147,16 +146,6 @@ void Status::trace()
}
applyToVisibleWindows(&BaseScreen::update);
if (isVisible(myPlaylist)
&& Timer.tv_sec == myPlaylist->Timer()+Config.playlist_disable_highlight_delay
&& myPlaylist->main().isHighlighted()
&& Config.playlist_disable_highlight_delay)
{
myPlaylist->main().setHighlighting(false);
myPlaylist->main().refresh();
}
Statusbar::tryRedraw();
Mpd.idle();

View File

@@ -30,8 +30,8 @@ using Global::wFooter;
namespace {//
timeval statusbarLockTime;
int statusbarLockDelay = -1;
boost::posix_time::ptime statusbarLockTime;
boost::posix_time::seconds statusbarLockDelay(-1);
bool statusbarBlockUpdate = false;
bool progressbarBlockUpdate = false;
@@ -97,7 +97,7 @@ void Statusbar::lock()
void Statusbar::unlock()
{
statusbarAllowUnlock = true;
if (statusbarLockDelay < 0)
if (statusbarLockDelay.is_negative())
{
if (Config.statusbar_visibility)
statusbarBlockUpdate = false;
@@ -122,10 +122,10 @@ bool Statusbar::isUnlocked()
void Statusbar::tryRedraw()
{
using Global::Timer;
if (statusbarLockDelay > 0
&& Timer.tv_sec >= statusbarLockTime.tv_sec+statusbarLockDelay)
if (statusbarLockDelay > boost::posix_time::seconds(0)
&& Timer - statusbarLockTime > statusbarLockDelay)
{
statusbarLockDelay = -1;
statusbarLockDelay = boost::posix_time::seconds(-1);
if (Config.statusbar_visibility)
statusbarBlockUpdate = !statusbarAllowUnlock;
@@ -149,12 +149,12 @@ NC::Window &Statusbar::put()
return *wFooter;
}
void Statusbar::print(int time, const std::string &message)
void Statusbar::print(int delay, const std::string &message)
{
if (statusbarAllowUnlock)
{
statusbarLockTime = Global::Timer;
statusbarLockDelay = time;
statusbarLockDelay = boost::posix_time::seconds(delay);
if (Config.statusbar_visibility)
statusbarBlockUpdate = true;
else

View File

@@ -94,7 +94,7 @@ private:
}
/// displays message in statusbar for a given period of time
void print(int time, const std::string &message);
void print(int delay, const std::string& message);
/// displays message in statusbar for period of time set in configuration file
inline void print(const std::string &message)
@@ -118,14 +118,14 @@ void printf(FormatT &&fmt, ArgT &&arg, Args&&... args)
/// displays formatted message in statusbar for a given period of time
template <typename FormatT>
void printf(int time, FormatT &&fmt)
void printf(int delay, FormatT &&fmt)
{
print(time, boost::format(std::forward<FormatT>(fmt)).str());
print(delay, boost::format(std::forward<FormatT>(fmt)).str());
}
template <typename FormatT, typename ArgT, typename... Args>
void printf(int time, FormatT &&fmt, ArgT &&arg, Args&&... args)
void printf(int delay, FormatT &&fmt, ArgT &&arg, Args&&... args)
{
printf(time, boost::format(std::forward<FormatT>(fmt)) % std::forward<ArgT>(arg),
printf(delay, boost::format(std::forward<FormatT>(fmt)) % std::forward<ArgT>(arg),
std::forward<Args>(args)...
);
}

View File

@@ -28,8 +28,6 @@
#include <fstream>
#include <limits>
#include <fcntl.h>
#include <sys/time.h>
#include <unistd.h>
#include "global.h"
#include "settings.h"
@@ -64,7 +62,6 @@ void Visualizer::switchTo()
SwitchTo::execute(this);
w.clear();
SetFD();
m_timer = { 0, 0 };
if (m_fifo >= 0)
Global::wFooter->setTimeout(WindowTimeout);
drawHeader();
@@ -95,7 +92,7 @@ void Visualizer::update()
if (data < 0) // no data available in fifo
return;
if (m_output_id != -1 && Global::Timer.tv_sec > m_timer.tv_sec+Config.visualizer_sync_interval)
if (m_output_id != -1 && Global::Timer - m_timer > Config.visualizer_sync_interval)
{
Mpd.DisableOutput(m_output_id);
usleep(50000);

View File

@@ -25,6 +25,7 @@
#ifdef ENABLE_VISUALIZER
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include "interfaces.h"
#include "screen.h"
#include "window.h"
@@ -69,7 +70,7 @@ private:
# endif // HAVE_FFTW3_H
int m_output_id;
timeval m_timer;
boost::posix_time::ptime m_timer;
int m_fifo;
unsigned m_samples;