1 /***************************************************************************
2 * Copyright (C) 2008-2017 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
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. *
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. *
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 "screens/clock.h"
27 #include <boost/date_time/posix_time/posix_time.hpp>
31 #include "screens/playlist.h"
34 #include "statusbar.h"
36 #include "screens/screen_switcher.h"
38 using Global::MainHeight
;
39 using Global::MainStartY
;
40 using Global::myScreen
;
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
;
54 const size_t Clock::Height
= 8;
58 Width
= Config
.clock_display_seconds
? 60 : 40;
60 m_pane
= NC::Window(0, MainStartY
, COLS
, MainHeight
, "", Config
.main_color
, NC::Border());
61 w
= NC::Window((COLS
-Width
)/2, (MainHeight
-Height
)/2+MainStartY
, Width
, Height
-1, "", Config
.main_color
, Config
.main_color
);
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
);
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");
86 SwitchTo::execute(this);
90 // clearing screen apparently fixes the problem with last digits being misrendered
96 std::wstring
Clock::title()
103 if (Width
> m_pane
.getWidth() || Height
> MainHeight
)
105 using Global::myLockedScreen
;
106 using Global::myInactiveScreen
;
110 if (myInactiveScreen
!= myLockedScreen
)
111 myScreen
= myInactiveScreen
;
112 myLockedScreen
->switchTo();
115 myPlaylist
->switchTo();
118 auto time
= boost::posix_time::to_tm(Global::Timer
);
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);
131 std::strftime(buf
, 64, "%x", &time
);
132 color_set(Config
.main_color
.pairNumber(), nullptr);
133 mvprintw(w
.getStarty()+w
.getHeight(), w
.getStartX()+(w
.getWidth()-strlen(buf
))/2, "%s", buf
);
137 for (int k
= 0; k
< 6; ++k
)
139 newer
[k
] = (newer
[k
] & ~mask
) | (next
[k
] & mask
);
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
];
150 for (int j
= 0; t
; t
>>= 1, ++j
)
158 if (Config
.clock_display_seconds
|| j
< 18)
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
)
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
;
191 #endif // ENABLE_CLOCK