summaryrefslogtreecommitdiff
path: root/src/clock.cpp
blob: 122fb58c4fbc36953f074be37a39f42bddbd35cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/***************************************************************************
 *   Copyright (C) 2008-2009 by Andrzej Rybczak                            *
 *   electricityispower@gmail.com                                          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.              *
 ***************************************************************************/

/// NOTICE: Major part of this code is ported from ncmpc's clock screen

#include "clock.h"

#ifdef ENABLE_CLOCK

#include <iomanip>
#include <sstream>

#include "settings.h"

namespace
{
	short disp[11] =
	{
		075557, 011111, 071747, 071717,
		055711, 074717, 074757, 071111,
		075757, 075717, 002020
	};
	
	long older[6], next[6], newer[6], mask;
	
	void set(int t, int n)
	{
		int m = 7 << n;
		for (int i = 0; i < 5; i++)
		{
			next[i] |= ((disp[t] >> ((4 - i) * 3)) & 07) << n;
			mask |= (next[i] ^ older[i]) & m;
		}
		if (mask & m)
			mask |= m;
	}
}

void InitClock()
{
	for (int i = 0; i < 5; i++)
		older[i] = newer[i] = next[i] = 0;
}

void DisplayClock(Window &w, const tm *time)
{
	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(10, 7);
	set(10, 17);
	
	char buf[54];
	strftime(buf, 64, "%x", time);
	attron(COLOR_PAIR(Config.main_color));
	mvprintw(w.GetStartY()+w.GetHeight(), w.GetStartX()+(w.GetWidth()-strlen(buf))/2, "%s", buf);
	attroff(COLOR_PAIR(Config.main_color));
	refresh();
	
	for (int k = 0; k < 6; k++)
	{
		newer[k] = (newer[k] & ~mask) | (next[k] & mask);
		next[k] = 0;
		for (int s = 1; s >= 0; s--)
		{
			w.Reverse(s);
			for (int i = 0; i < 6; i++)
			{
				long a = (newer[i] ^ older[i]) & (s ? newer : older)[i];
				if (a != 0)
				{
					long t = 1 << 26;
					for (int j = 0; t; t >>= 1, j++)
					{
						if (a & t)
						{
							if (!(a & (t << 1)))
							{
								w.GotoXY(2*j+2, i);
							}
							if (Config.clock_display_seconds || j < 18)
								w << "  ";
						}
					}
				}
				if (!s)
				{
					older[i] = newer[i];
				}
			}
			if (!s)
			{
				w.Refresh();
			}
		}
	}
}

#endif // ENABLE_CLOCK