strbuffer: change basic_buffer to BasicBuffer
[ncmpcpp.git] / src / clock.cpp
blob73dd32c93cbfe29180f223f93327e10f9ef3792a
1 /***************************************************************************
2 * Copyright (C) 2008-2012 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 <cstring>
28 #include <sys/time.h>
30 #include "global.h"
31 #include "playlist.h"
32 #include "settings.h"
33 #include "status.h"
34 #include "statusbar.h"
35 #include "title.h"
37 using Global::MainHeight;
38 using Global::MainStartY;
39 using Global::myScreen;
41 Clock *myClock = new Clock;
43 short Clock::disp[11] =
45 075557, 011111, 071747, 071717,
46 055711, 074717, 074757, 071111,
47 075757, 075717, 002020
50 long Clock::older[6], Clock::next[6], Clock::newer[6], Clock::mask;
52 size_t Clock::Width;
53 const size_t Clock::Height = 8;
55 void Clock::Init()
57 Width = Config.clock_display_seconds ? 60 : 40;
59 itsPane = new NC::Window(0, MainStartY, COLS, MainHeight, "", Config.main_color, NC::brNone);
60 w = new NC::Window((COLS-Width)/2, (MainHeight-Height)/2+MainStartY, Width, Height-1, "", Config.main_color, NC::Border(Config.main_color));
61 isInitialized = 1;
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 itsPane->resize(width, MainHeight);
71 itsPane->moveTo(x_offset, MainStartY);
72 itsPane->refresh();
74 if (Width <= width && Height <= MainHeight)
75 w->moveTo(x_offset+(width-Width)/2, MainStartY+(MainHeight-Height)/2);
78 void Clock::SwitchTo()
80 using Global::myLockedScreen;
82 if (myScreen == this)
83 return;
85 if (!isInitialized)
86 Init();
88 if (myLockedScreen)
89 UpdateInactiveScreen(this);
91 size_t x_offset, width;
92 GetWindowResizeParams(x_offset, width, false);
93 if (Width > width || Height > MainHeight)
95 Statusbar::msg("Screen is too small to display clock");
96 if (myLockedScreen)
97 UpdateInactiveScreen(myLockedScreen);
98 return;
101 if (hasToBeResized || myLockedScreen)
102 Resize();
104 if (myScreen != this && myScreen->isTabbable())
105 Global::myPrevScreen = myScreen;
106 myScreen = this;
107 drawHeader();
108 Prepare();
109 itsPane->refresh();
110 // clearing screen apparently fixes the problem with last digits being misrendered
111 w->clear();
112 w->display();
115 std::wstring Clock::Title()
117 return L"Clock";
120 void Clock::Update()
122 if (Width > itsPane->getWidth() || Height > MainHeight)
124 using Global::myLockedScreen;
125 using Global::myInactiveScreen;
127 if (myLockedScreen)
129 if (myInactiveScreen != myLockedScreen)
130 myScreen = myInactiveScreen;
131 myLockedScreen->SwitchTo();
133 else
134 myPlaylist->SwitchTo();
137 tm *time = localtime(&Global::Timer.tv_sec);
139 mask = 0;
140 Set(time->tm_sec % 10, 0);
141 Set(time->tm_sec / 10, 4);
142 Set(time->tm_min % 10, 10);
143 Set(time->tm_min / 10, 14);
144 Set(time->tm_hour % 10, 20);
145 Set(time->tm_hour / 10, 24);
146 Set(10, 7);
147 Set(10, 17);
149 char buf[64];
150 strftime(buf, 64, "%x", time);
151 attron(COLOR_PAIR(Config.main_color));
152 mvprintw(w->getStarty()+w->getHeight(), w->getStartX()+(w->getWidth()-strlen(buf))/2, "%s", buf);
153 attroff(COLOR_PAIR(Config.main_color));
154 refresh();
156 for (int k = 0; k < 6; ++k)
158 newer[k] = (newer[k] & ~mask) | (next[k] & mask);
159 next[k] = 0;
160 for (int s = 1; s >= 0; --s)
162 *w << (s ? NC::fmtReverse : NC::fmtReverseEnd);
163 for (int i = 0; i < 6; ++i)
165 long a = (newer[i] ^ older[i]) & (s ? newer : older)[i];
166 if (a != 0)
168 long t = 1 << 26;
169 for (int j = 0; t; t >>= 1, ++j)
171 if (a & t)
173 if (!(a & (t << 1)))
175 w->goToXY(2*j+2, i);
177 if (Config.clock_display_seconds || j < 18)
178 *w << " ";
182 if (!s)
184 older[i] = newer[i];
189 w->refresh();
192 void Clock::Prepare()
194 for (int i = 0; i < 5; ++i)
195 older[i] = newer[i] = next[i] = 0;
198 void Clock::Set(int t, int n)
200 int m = 7 << n;
201 for (int i = 0; i < 5; ++i)
203 next[i] |= ((disp[t] >> ((4 - i) * 3)) & 07) << n;
204 mask |= (next[i] ^ older[i]) & m;
206 if (mask & m)
207 mask |= m;
210 #endif // ENABLE_CLOCK