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), fade_to_new_info(false)
12 RotateScreen::~RotateScreen()
14 delete[] fadefrom_buf;
15 for (unsigned i = 0; i < subscreens.size(); ++i) {
16 delete[] subscreens[i].redbuf;
17 delete[] subscreens[i].buf;
21 bool RotateScreen::check_invalidated()
32 for (unsigned i = 0; i < subscreens.size(); ++i) {
33 if (subscreens[i].buf == NULL || subscreens[i].screen->check_invalidated())
40 void RotateScreen::draw(unsigned char *buf, unsigned width, unsigned height)
42 // see line 1 for all of this
43 if (fadefrom_buf == NULL) {
44 fadefrom_buf = new unsigned char[width * height * 4];
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];
51 i->buf = new unsigned char[width * height * 4];
52 i->screen->draw(i->buf, width, height);
55 // end of "line 1"-code :-)
59 if (subscreens.size() == 0) {
61 gettimeofday(&last_update, NULL);
65 // if we're in a fade, complete it before doing anything else
68 gettimeofday(&now, NULL);
70 if (!fade_found_start_time) {
71 fade_found_start_time = true;
75 double elapsed_fade = double(now.tv_sec - fade_started.tv_sec) +
76 double(now.tv_usec - fade_started.tv_usec) * 1.0e-6;
78 if (elapsed_fade > 5.5 || (!fade_to_new_info && elapsed_fade > 0.5)) {
81 memcpy(buf, subscreens[current_screen].buf, width * height * 4);
83 unsigned char *sptr1, *sptr2;
84 unsigned char *dptr = buf;
86 if (fade_to_new_info) {
87 if (elapsed_fade < 0.5) {
89 sptr2 = subscreens[current_screen].redbuf;
90 f = unsigned(elapsed_fade/0.5 * 256.0);
92 sptr1 = subscreens[current_screen].redbuf;
93 sptr2 = subscreens[current_screen].buf;
94 f = unsigned((elapsed_fade-0.5)/5.0 * 256.0);
98 sptr2 = subscreens[current_screen].buf;
99 f = unsigned(elapsed_fade/0.5 * 256.0);
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);
107 sptr1 += 4, sptr2 += 4, dptr += 4;
111 // determine if we want to switch screens
113 unsigned old_current_screen = current_screen;
114 int priority = -9999; // bah :-P
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) {
121 priority = subscreens[i].screen->get_priority();
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);
131 if (!valid || current_screen != old_current_screen || subscreens[current_screen].screen->check_invalidated()) {
134 fade_found_start_time = false;
135 fade_to_new_info = force;
137 memcpy(fadefrom_buf, subscreens[old_current_screen].buf, width * height * 4);
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);
143 subscreens[current_screen].screen->draw(subscreens[current_screen].buf, width, height);
150 gettimeofday(&last_update, NULL);
154 // note: makes no sense if valid=false!
155 bool RotateScreen::needs_update()
158 gettimeofday(&now, NULL);
160 double since = double(now.tv_sec - last_update.tv_sec) +
161 double(now.tv_usec - last_update.tv_usec) * 1.0e-6;
163 return (since >= 10.0);
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.
174 bool RotateScreen::can_update()
177 gettimeofday(&now, NULL);
179 double since = double(now.tv_sec - last_update.tv_sec) +
180 double(now.tv_usec - last_update.tv_usec) * 1.0e-6;
184 if (fade_to_new_info && subscreens.size() > 0 && !subscreens[current_screen].screen->check_invalidated() && since < 8.0)
189 void RotateScreen::add_screen(GenericScreen *screen)
196 subscreens.push_back(ss);