scrollpad: explicitely include iostream
[ncmpcpp.git] / src / clock.cpp
blob13762a475ca37b02d6cd468cb48d0a7b8453eec8
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 /// NOTICE: Major part of this code is ported from ncmpc's clock screen
23 #include "clock.h"
25 #ifdef ENABLE_CLOCK
27 #include <boost/date_time/posix_time/posix_time.hpp>
28 #include <cstring>
30 #include "global.h"
31 #include "playlist.h"
32 #include "settings.h"
33 #include "status.h"
34 #include "statusbar.h"
35 #include "title.h"
36 #include "screen_switcher.h"
38 using Global::MainHeight;
39 using Global::MainStartY;
40 using Global::myScreen;
42 Clock *myClock;
44 short Clock::disp[11] =
46 075557, 011111, 071747, 071717,
47 055711, 074717, 074757, 071111,
48 075757, 075717, 002020
51 long Clock::older[6], Clock::next[6], Clock::newer[6], Clock::mask;
53 size_t Clock::Width;
54 const size_t Clock::Height = 8;
56 Clock::Clock()
58 Width = Config.clock_display_seconds ? 60 : 40;
60 m_pane = NC::Window(0, MainStartY, COLS, MainHeight, "", Config.main_color, NC::Border::None);
61 w = NC::Window((COLS-Width)/2, (MainHeight-Height)/2+MainStartY, Width, Height-1, "", Config.main_color, NC::Border(Config.main_color));
64 void Clock::resize()
66 size_t x_offset, width;
67 getWindowResizeParams(x_offset, width);
69 // used for clearing area out of clock window while resizing terminal
70 m_pane.resize(width, MainHeight);
71 m_pane.moveTo(x_offset, MainStartY);
72 m_pane.refresh();
74 if (Width <= width && Height <= MainHeight)
75 w.moveTo(x_offset+(width-Width)/2, MainStartY+(MainHeight-Height)/2);
78 void Clock::switchTo()
80 size_t x_offset, width;
81 getWindowResizeParams(x_offset, width, false);
82 if (Width > width || Height > MainHeight)
83 Statusbar::print("Screen is too small to display clock");
84 else
86 SwitchTo::execute(this);
87 drawHeader();
88 Prepare();
89 m_pane.refresh();
90 // clearing screen apparently fixes the problem with last digits being misrendered
91 w.clear();
92 w.display();
96 std::wstring Clock::title()
98 return L"Clock";
101 void Clock::update()
103 if (Width > m_pane.getWidth() || Height > MainHeight)
105 using Global::myLockedScreen;
106 using Global::myInactiveScreen;
108 if (myLockedScreen)
110 if (myInactiveScreen != myLockedScreen)
111 myScreen = myInactiveScreen;
112 myLockedScreen->switchTo();
114 else
115 myPlaylist->switchTo();
118 auto time = boost::posix_time::to_tm(Global::Timer);
120 mask = 0;
121 Set(time.tm_sec % 10, 0);
122 Set(time.tm_sec / 10, 4);
123 Set(time.tm_min % 10, 10);
124 Set(time.tm_min / 10, 14);
125 Set(time.tm_hour % 10, 20);
126 Set(time.tm_hour / 10, 24);
127 Set(10, 7);
128 Set(10, 17);
130 char buf[64];
131 std::strftime(buf, 64, "%x", &time);
132 attron(COLOR_PAIR(int(Config.main_color)));
133 mvprintw(w.getStarty()+w.getHeight(), w.getStartX()+(w.getWidth()-strlen(buf))/2, "%s", buf);
134 attroff(COLOR_PAIR(int(Config.main_color)));
135 refresh();
137 for (int k = 0; k < 6; ++k)
139 newer[k] = (newer[k] & ~mask) | (next[k] & mask);
140 next[k] = 0;
141 for (int s = 1; s >= 0; --s)
143 w << (s ? NC::Format::Reverse : NC::Format::NoReverse);
144 for (int i = 0; i < 6; ++i)
146 long a = (newer[i] ^ older[i]) & (s ? newer : older)[i];
147 if (a != 0)
149 long t = 1 << 26;
150 for (int j = 0; t; t >>= 1, ++j)
152 if (a & t)
154 if (!(a & (t << 1)))
156 w.goToXY(2*j+2, i);
158 if (Config.clock_display_seconds || j < 18)
159 w << " ";
163 if (!s)
165 older[i] = newer[i];
170 w.refresh();
173 void Clock::Prepare()
175 for (int i = 0; i < 5; ++i)
176 older[i] = newer[i] = next[i] = 0;
179 void Clock::Set(int t, int n)
181 int m = 7 << n;
182 for (int i = 0; i < 5; ++i)
184 next[i] |= ((disp[t] >> ((4 - i) * 3)) & 07) << n;
185 mask |= (next[i] ^ older[i]) & m;
187 if (mask & m)
188 mask |= m;
191 #endif // ENABLE_CLOCK