1 /* NOTE: this class will _NOT_ handle resolution changes cleanly. You have been warned. :-) */
5 #include "rotatescreen.h"
7 RotateScreen::RotateScreen()
8 : fadefrom_buf(NULL), valid(false), current_screen(0), in_fade(false)
12 RotateScreen::~RotateScreen()
17 bool RotateScreen::check_invalidated()
28 for (unsigned i = 0; i < subscreens.size(); ++i) {
29 if (subscreens[i].buf == NULL || subscreens[i].screen->check_invalidated())
36 void RotateScreen::draw(unsigned char *buf, unsigned width, unsigned height)
38 // see line 1 for all of this
39 if (fadefrom_buf == NULL) {
40 fadefrom_buf = new unsigned char[width * height * 4];
42 for (std::vector<Subscreen>::iterator i = subscreens.begin(); i != subscreens.end(); ++i) {
44 i->buf = new unsigned char[width * height * 4];
45 i->screen->draw(i->buf, width, height);
48 // end of "line 1"-code :-)
52 if (subscreens.size() == 0) {
54 gettimeofday(&last_update, NULL);
58 // if we're in a fade, complete it before doing anything else
61 gettimeofday(&now, NULL);
63 if (!fade_found_start_time) {
64 fade_found_start_time = true;
68 double elapsed_fade = double(now.tv_sec - fade_started.tv_sec) +
69 double(now.tv_usec - fade_started.tv_usec) * 1.0e-6;
71 if (elapsed_fade > 5.5 || (!fade_to_new_info && elapsed_fade > 0.5)) {
74 // ugly hack here? :-)
75 subscreens[current_screen].screen->draw(subscreens[current_screen].buf, width, height);
77 memcpy(buf, subscreens[current_screen].buf, width * height * 4);
79 // find the fade factors
81 if (fade_to_new_info) {
82 if (elapsed_fade < 0.5) {
83 fr = fg = fb = fa = unsigned(elapsed_fade/0.5 * 256.0);
85 fr = fg = fb = fa = unsigned((elapsed_fade-0.5)/5.0 * 256.0);
88 fr = fg = fb = fa = unsigned(elapsed_fade/0.5 * 256.0);
91 unsigned char *sptr1 = fadefrom_buf;
92 unsigned char *sptr2 = subscreens[current_screen].buf;
93 unsigned char *dptr = buf;
95 if (fade_to_new_info && elapsed_fade >= 0.5) {
97 for (unsigned i = 0; i < width * height; ++i) {
98 dptr[0] = sptr2[0] + (((int(sptr2[2]) - int(sptr2[0])) * fb) >> 8);
99 dptr[1] = sptr2[1] + (((int(sptr2[2]) - int(sptr2[1])) * fg) >> 8);
103 sptr1 += 4, sptr2 += 4, dptr += 4;
106 for (unsigned i = 0; i < width * height; ++i) {
107 dptr[0] = sptr1[0] + (((int(sptr2[0]) - int(sptr1[0])) * fb) >> 8);
108 dptr[1] = sptr1[1] + (((int(sptr2[1]) - int(sptr1[1])) * fg) >> 8);
109 dptr[2] = sptr1[2] + (((int(sptr2[2]) - int(sptr1[2])) * fr) >> 8);
110 dptr[3] = sptr1[3] + (((int(sptr2[3]) - int(sptr1[3])) * fa) >> 8);
112 sptr1 += 4, sptr2 += 4, dptr += 4;
117 // determine if we want to switch screens
119 unsigned old_current_screen = current_screen;
120 int priority = -9999; // bah :-P
122 // push any invalidated screen first (for now)
123 for (unsigned i = 0; i < subscreens.size(); ++i) {
124 if (subscreens[i].screen->check_invalidated() && subscreens[i].screen->get_priority() > priority) {
127 priority = subscreens[i].screen->get_priority();
131 // check if we want to go to the next screen
132 if (valid && !force && needs_update()) {
133 current_screen = (current_screen + 1) % subscreens.size();
134 gettimeofday(&last_update, NULL);
137 if (!valid || current_screen != old_current_screen || subscreens[current_screen].screen->check_invalidated()) {
140 fade_found_start_time = false;
141 fade_to_new_info = force;
143 memcpy(fadefrom_buf, subscreens[old_current_screen].buf, width * height * 4);
145 if (subscreens[current_screen].screen->check_invalidated()) {
146 subscreens[current_screen].screen->draw(subscreens[current_screen].buf, width, height);
153 gettimeofday(&last_update, NULL);
157 // note: makes no sense if valid=false!
158 bool RotateScreen::needs_update()
161 gettimeofday(&now, NULL);
163 double since = double(now.tv_sec - last_update.tv_sec) +
164 double(now.tv_usec - last_update.tv_usec) * 1.0e-6;
166 return (since >= 10.0);
170 * Hold all screens for minimum three seconds, hold all screens with new info
171 * for at minimum eight seconds. There's a slight hack here; a screen with new
172 * info _might_ already be outdated (for instance, if we do an update to a group
173 * with a mistake, then correct it quick afterwards). If it's already outdated,
174 * we ignore the "eight second" rule; the results will be somewhat ugly, though,
175 * but it's hard to do much better without too much special code.
177 bool RotateScreen::can_update()
180 gettimeofday(&now, NULL);
182 double since = double(now.tv_sec - last_update.tv_sec) +
183 double(now.tv_usec - last_update.tv_usec) * 1.0e-6;
187 if (fade_to_new_info && subscreens.size() > 0 && !subscreens[current_screen].screen->check_invalidated() && since < 8.0)
192 void RotateScreen::add_screen(GenericScreen *screen)
198 subscreens.push_back(ss);