]> git.sesse.net Git - ccbs/blob - bigscreen/rotatescreen.cpp
Support odd/even theming for top5/top10.
[ccbs] / bigscreen / rotatescreen.cpp
1 /* NOTE: this class will _NOT_ handle resolution changes cleanly. You have been warned. :-) */
2
3 #include <cstdio>
4 #include <cstring>
5 #include "rotatescreen.h"
6
7 RotateScreen::RotateScreen()
8         : fadefrom_buf(NULL), valid(false), current_screen(0), in_fade(false), fade_to_new_info(false)
9 {
10 }
11
12 RotateScreen::~RotateScreen()
13 {
14         delete[] fadefrom_buf;
15         for (unsigned i = 0; i < subscreens.size(); ++i) {
16                 delete[] subscreens[i].redbuf;
17                 delete[] subscreens[i].buf;
18         }
19 }
20
21 bool RotateScreen::check_invalidated()
22 {
23         if (!valid)
24                 return true;
25         if (in_fade)
26                 return true;
27         if (needs_update())
28                 return true;
29         if (!can_update())
30                 return false;
31         
32         for (unsigned i = 0; i < subscreens.size(); ++i) {
33                 if (subscreens[i].buf == NULL || subscreens[i].screen->check_invalidated())
34                         return true;
35         }
36
37         return false;
38 }
39
40 void RotateScreen::draw(unsigned char *buf, unsigned width, unsigned height)
41 {
42         // see line 1 for all of this
43         if (fadefrom_buf == NULL) {
44                 fadefrom_buf = new unsigned char[width * height * 4];
45         }
46         for (std::vector<Subscreen>::iterator i = subscreens.begin(); i != subscreens.end(); ++i) {
47                 if (i->redbuf == NULL) {
48                         i->redbuf = new unsigned char[width * height * 4];
49                 }
50                 if (i->buf == NULL) {
51                         i->buf = new unsigned char[width * height * 4];
52                         i->screen->draw(i->buf, width, height);
53                 }
54         }
55         // end of "line 1"-code :-)
56         
57         bool force = false;
58
59         if (subscreens.size() == 0) {
60                 valid = true;
61                 gettimeofday(&last_update, NULL);
62                 return;
63         }
64         
65         // if we're in a fade, complete it before doing anything else
66         if (in_fade) {
67                 struct timeval now;
68                 gettimeofday(&now, NULL);
69
70                 if (!fade_found_start_time) {
71                         fade_found_start_time = true;
72                         fade_started = now;
73                 }
74                 
75                 double elapsed_fade = double(now.tv_sec - fade_started.tv_sec) +
76                         double(now.tv_usec - fade_started.tv_usec) * 1.0e-6;
77                 
78                 if (elapsed_fade > 5.5 || (!fade_to_new_info && elapsed_fade > 0.5)) {
79                         in_fade = false;
80
81                         memcpy(buf, subscreens[current_screen].buf, width * height * 4);
82                 } else {
83                         unsigned char *sptr1, *sptr2;
84                         unsigned char *dptr = buf;
85                         int f;
86                         if (fade_to_new_info) {
87                                 if (elapsed_fade < 0.5) {
88                                         sptr1 = fadefrom_buf;
89                                         sptr2 = subscreens[current_screen].redbuf;
90                                         f = unsigned(elapsed_fade/0.5 * 256.0);
91                                 } else {
92                                         sptr1 = subscreens[current_screen].redbuf;
93                                         sptr2 = subscreens[current_screen].buf;
94                                         f = unsigned((elapsed_fade-0.5)/5.0 * 256.0);
95                                 }
96                         } else {
97                                 sptr1 = fadefrom_buf;
98                                 sptr2 = subscreens[current_screen].buf;
99                                 f = unsigned(elapsed_fade/0.5 * 256.0);
100                         }
101
102                         for (unsigned i = 0; i < width * height; ++i) {
103                                 dptr[0] = sptr1[0] + (((int(sptr2[0]) - int(sptr1[0])) * f) >> 8);
104                                 dptr[1] = sptr1[1] + (((int(sptr2[1]) - int(sptr1[1])) * f) >> 8);
105                                 dptr[2] = sptr1[2] + (((int(sptr2[2]) - int(sptr1[2])) * f) >> 8);
106
107                                 sptr1 += 4, sptr2 += 4, dptr += 4;
108                         }
109                 }
110         } else {
111                 // determine if we want to switch screens
112         
113                 unsigned old_current_screen = current_screen;
114                 int priority = -9999;  // bah :-P
115                 
116                 // push any invalidated screen first (for now)
117                 for (unsigned i = 0; i < subscreens.size(); ++i) {
118                         if (subscreens[i].screen->check_invalidated() && subscreens[i].screen->get_priority() > priority) {
119                                 current_screen = i;
120                                 force = true;
121                                 priority = subscreens[i].screen->get_priority();
122                         }
123                 }
124
125                 // check if we want to go to the next screen
126                 if (valid && !force && needs_update()) {
127                         current_screen = (current_screen + 1) % subscreens.size();
128                         gettimeofday(&last_update, NULL);
129                 }
130
131                 if (!valid || current_screen != old_current_screen || subscreens[current_screen].screen->check_invalidated()) {
132                         // initialize a fade
133                         in_fade = true;
134                         fade_found_start_time = false;
135                         fade_to_new_info = force;
136                         
137                         memcpy(fadefrom_buf, subscreens[old_current_screen].buf, width * height * 4);
138
139                         if (subscreens[current_screen].screen->check_invalidated()) {
140                                 if (fade_to_new_info) {
141                                         subscreens[current_screen].screen->draw(subscreens[current_screen].redbuf, width, height);
142                                 }
143                                 subscreens[current_screen].screen->draw(subscreens[current_screen].buf, width, height);
144                         }
145                 }
146         }
147
148         if (!valid) {
149                 valid = true;
150                 gettimeofday(&last_update, NULL);
151         }
152 }
153         
154 // note: makes no sense if valid=false!
155 bool RotateScreen::needs_update()
156 {
157         struct timeval now;
158         gettimeofday(&now, NULL);
159
160         double since = double(now.tv_sec - last_update.tv_sec) +
161                 double(now.tv_usec - last_update.tv_usec) * 1.0e-6;
162
163         return (since >= 10.0);
164 }
165
166 /*
167  * Hold all screens for minimum three seconds, hold all screens with new info
168  * for at minimum eight seconds. There's a slight hack here; a screen with new
169  * info _might_ already be outdated (for instance, if we do an update to a group
170  * with a mistake, then correct it quick afterwards). If it's already outdated,
171  * we ignore the "eight second" rule; the results will be somewhat ugly, though,
172  * but it's hard to do much better without too much special code.
173  */
174 bool RotateScreen::can_update()
175 {
176         struct timeval now;
177         gettimeofday(&now, NULL);
178
179         double since = double(now.tv_sec - last_update.tv_sec) +
180                 double(now.tv_usec - last_update.tv_usec) * 1.0e-6;
181
182         if (since < 3.0)
183                 return false;
184         if (fade_to_new_info && subscreens.size() > 0 && !subscreens[current_screen].screen->check_invalidated() && since < 8.0)
185                 return false;
186         return true;
187 }
188
189 void RotateScreen::add_screen(GenericScreen *screen)
190 {
191         Subscreen ss;
192         ss.redbuf = NULL;
193         ss.buf = NULL;
194         ss.screen = screen;
195
196         subscreens.push_back(ss);
197 }
198