discard custom colors of selected items also in classic display mode
[ncmpcpp.git] / src / clock.cpp
blob21d5ce0f35eb11ee5a27b1a39adfdb3beee10a0c
1 /***************************************************************************
2 * Copyright (C) 2008-2010 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"
35 using Global::MainHeight;
36 using Global::MainStartY;
37 using Global::myScreen;
39 Clock *myClock = new Clock;
41 short Clock::disp[11] =
43 075557, 011111, 071747, 071717,
44 055711, 074717, 074757, 071111,
45 075757, 075717, 002020
48 long Clock::older[6], Clock::next[6], Clock::newer[6], Clock::mask;
50 size_t Clock::Width;
51 const size_t Clock::Height = 8;
53 void Clock::Init()
55 Width = Config.clock_display_seconds ? 60 : 40;
57 w = new Window((COLS-Width)/2, (MainHeight-Height)/2+MainStartY, Width, Height-1, "", Config.main_color, Border(Config.main_color));
58 isInitialized = 1;
61 void Clock::Resize()
63 if (Width <= size_t(COLS) && Height <= MainHeight)
65 w->MoveTo((COLS-Width)/2, (MainHeight-Height)/2+MainStartY);
66 if (myScreen == this)
68 if (myPlaylist->hasToBeResized)
69 myPlaylist->Resize();
70 myPlaylist->Items->Hide();
71 w->Display();
76 void Clock::SwitchTo()
78 if (Width > size_t(COLS) || Height > MainHeight)
80 ShowMessage("Screen is too small to display clock!");
81 return;
83 if (myScreen == this)
84 return;
86 if (!isInitialized)
87 Init();
89 if (hasToBeResized)
90 Resize();
92 if (myScreen != this && myScreen->isTabbable())
93 Global::myPrevScreen = myScreen;
94 myScreen = this;
95 myPlaylist->Items->Hide();
96 Global::RedrawHeader = 1;
97 Prepare();
98 // clearing screen apparently fixes the problem with last digits being misrendered
99 w->Clear();
100 w->Display();
103 std::basic_string<my_char_t> Clock::Title()
105 return U("Clock");
108 void Clock::Update()
110 if (Width > size_t(COLS) || Height > MainHeight)
111 myPlaylist->SwitchTo();
113 static timeval past = { 0, 0 };
114 if (Global::Timer.tv_sec <= past.tv_sec)
115 return;
116 gettimeofday(&past, 0);
118 tm *time = localtime(&past.tv_sec);
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 strftime(buf, 64, "%x", time);
132 attron(COLOR_PAIR(Config.main_color));
133 mvprintw(w->GetStartY()+w->GetHeight(), w->GetStartX()+(w->GetWidth()-strlen(buf))/2, "%s", buf);
134 attroff(COLOR_PAIR(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 ? fmtReverse : fmtReverseEnd);
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