2 Copyright 2004-2005 Chris Tallon
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 BoxStack* BoxStack::instance = NULL;
42 BoxStack* BoxStack::getInstance()
49 if (initted) return 0;
55 int BoxStack::shutdown()
57 if (!initted) return 0;
59 // FIXME don't think this can work properly, removeAll leaves the wallpaper there!
66 int BoxStack::addVideoDisplay(Boxx* box,VideoDisplay vd)
68 videoStack.push(pair<Boxx*,VideoDisplay>(box,vd));
69 Video::getInstance()->setVideoDisplay(vd);
73 int BoxStack::add(Boxx* v)
75 if (!initted) return 0;
76 Log::getInstance()->log("BoxStack", Log::DEBUG, "add called");
78 Log::getInstance()->log("BoxStack", Log::DEBUG, "Locked for add");
82 Log::getInstance()->log("BoxStack", Log::ERR, "More than 16 boxes! Unlocked for add");
86 boxes[numBoxes++] = v;
88 if (v->getVideoDisplay(vd)) {
89 Log::getInstance()->log("BoxStack", Log::DEBUG, "Add video display");
90 addVideoDisplay(v,vd);
95 Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for add");
100 // ---------------------------------------------------- REMOVE CODE
102 int BoxStack::remove(Boxx* toDelete)
104 if (!initted) return 0;
105 VideoDisplay *display = NULL;
108 Log::getInstance()->log("BoxStack", Log::DEBUG, "Locked for remove");
113 Log::getInstance()->log("BoxStack", Log::ERR, "Unlocked for remove numBoxes == 0");
117 // Log::getInstance()->log("BoxStack", Log::DEBUG, "entering remove, numBoxes=%i", numBoxes);
121 if (toDelete == NULL)
123 toDelete = boxes[numBoxes-1];
128 // to be deleted box is more likely to be at the top
129 for (i = numBoxes-1; i >= 0; i--)
131 // Log::getInstance()->log("BoxStack", Log::DEBUG, "todel: %p, i=%i, boxes[i]=%p", toDelete, i, boxes[i]);
132 if (boxes[i] == toDelete) break;
137 // not a Box we have!
140 Log::getInstance()->log("BoxStack", Log::ERR, "Unlocked for remove - no boxx deleted");
147 toDelete->preDelete();
151 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Starting deleteBox");
153 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Done deleteBox");
155 // Shift the boxes on top down one
157 for(int j = i; j < numBoxes; j++) boxes[j] = boxes[j+1];
159 // If there is only the wallpaper left signal command
162 Message* m = new Message();
163 m->to = Command::getInstance();
164 m->message = Message::LAST_VIEW_CLOSE;
165 Command::getInstance()->postMessageNoLock(m);
168 if (!videoStack.empty() && videoStack.top().first==toDelete) {
170 if (!videoStack.empty()) display=&videoStack.top().second;
173 Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for remove");
176 //AVO: do this delete outside the lock to allow for recursive calls within the destructor
177 // as this box is not in the stack any more, there is no chance for a second delete
178 Log::getInstance()->log("BoxStack", Log::DEBUG, "remove: going to delete boxx %p, num %d", toDelete, numBoxes);
181 Log::getInstance()->log("BoxStack", Log::DEBUG, "setVideoDisplay %d %d %d %d %d %d", display->mode, display->fallbackMode,
182 display->x, display->y, display->width, display->height);
183 Video::getInstance()->setVideoDisplay(*display);
189 /////////////////////////////////////////////////////////////////////////////
191 /////////////////////////////////////////////////////////////////////////////
193 void BoxStack::deleteBox(int z)
195 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Delete box %i of %i", z, numBoxes);
197 boxSplit(boxes[z]->area, z + 1, numBoxes, 1, rl);
200 repaintRevealed(z, rl.front());
205 void BoxStack::redrawAllBoxes()
209 for (int z = 0; z < numBoxes; z++)
216 update(NULL,NULL); // should blt all
219 void BoxStack::update(Boxx* toUpdate, Region* regionToUpdate)
221 Log::getInstance()->log("BoxStack", Log::DEBUG, "Update called");
222 if (!initted) return; // it is allowed to call this before init
224 Log::getInstance()->log("BoxStack", Log::DEBUG, "Locked for update");
226 // Get the z index of the box
230 for (z = 0; z < numBoxes; z++)
232 if (boxes[z] == toUpdate) break;
237 // not a Box we have!
239 Log::getInstance()->log("BoxStack", Log::ERR, "Unlocked for update! box not inside boxstack");
250 Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for update, no box present");
254 // get the region for the whole box, could be less than that
255 // for smaller updates
257 Region r = toUpdate->area;
261 // Can be null if the whole box should be updated
262 // If this is given the numbers are relative to the size of the box, not the screen
264 r.x += regionToUpdate->x;
265 r.y += regionToUpdate->y;
266 r.w = regionToUpdate->w;
267 r.h = regionToUpdate->h;
273 boxSplit(r, z+1, numBoxes, 1, rl);
283 Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for update");
286 void BoxStack::repaintRevealed(int x, Region r)
289 boxSplit(r, x - 1, -1, -1, rl);
295 boxes[r2.z]->blt(r2);
300 void BoxStack::boxSplit(Region r, int start, int end, int direction, RegionList& rl)
302 // printf("Y= S=%i E=%i D=%i: Boxsplit: %i %i %i %i\n", start, end, direction, r.x, r.y, r.w, r.h);
304 for(int z = start; z != end; z += direction)
306 if (r.overlappedBy(boxes[z]->area))
308 // printf("Z=%i S=%i E=%i D=%i: %i overlaps\n", z, start, end, direction, z);
313 if (boxes[z]->area.y > r.y)
315 // printf("Z=%i S=%i E=%i D=%i: Case 1 for %i %i %i %i split by %i: %i %i %i %i\n", z, start, end, direction, r.x, r.y, r.w, r.h, z, boxes[z]->area.x, boxes[z]->area.y, boxes[z]->area.w, boxes[z]->area.h);
316 top = boxes[z]->area.y;
321 newR.h = boxes[z]->area.y - r.y;
322 boxSplit(newR, z + direction, end, direction, rl);
328 newR2.y = boxes[z]->area.y;
330 newR2.h = r.h - newR.h;
331 boxSplit(newR2, z, end, -1, rl);
336 if (boxes[z]->area.y2() < r.y2())
338 // printf("Z=%i S=%i E=%i D=%i: Case 2 for %i %i %i %i split by %i: %i %i %i %i\n", z, start, end, direction, r.x, r.y, r.w, r.h, z, boxes[z]->area.x, boxes[z]->area.y, boxes[z]->area.w, boxes[z]->area.h);
339 btm = boxes[z]->area.y2();
342 newR.y = boxes[z]->area.y2() + 1;
344 newR.h = r.y2() - newR.y + 1;
345 boxSplit(newR, z + direction, end, direction, rl);
353 newR2.h = r.h - newR.h;
354 boxSplit(newR2, z, end, -1, rl);
359 if (boxes[z]->area.x > r.x)
361 // printf("Z=%i S=%i E=%i D=%i: Case 3 for %i %i %i %i split by %i: %i %i %i %i\n", z, start, end, direction, r.x, r.y, r.w, r.h, z, boxes[z]->area.x, boxes[z]->area.y, boxes[z]->area.w, boxes[z]->area.h);
365 newR.w = boxes[z]->area.x - r.x;
366 newR.h = btm - top + 1;
367 boxSplit(newR, z + direction, end, direction, rl);
372 newR2.x = r.x + newR.w;
374 newR2.w = r.w - newR.w;
376 boxSplit(newR2, z, end, -1, rl);
381 if (boxes[z]->area.x2() < r.x2())
383 // printf("Z=%i S=%i E=%i D=%i: Case 4 for %i %i %i %i split by %i: %i %i %i %i\n", z, start, end, direction, r.x, r.y, r.w, r.h, z, boxes[z]->area.x, boxes[z]->area.y, boxes[z]->area.w, boxes[z]->area.h);
385 newR.x = boxes[z]->area.x2() + 1;
387 newR.w = r.x2() - newR.x + 1;
388 newR.h = btm - top + 1;
389 boxSplit(newR, z + direction, end, direction, rl);
396 newR2.w = r.w - newR.w;
398 boxSplit(newR2, z, end, -1, rl);
405 // we are going down the stack
406 // r is underlapped by boxes[z]
407 // but we have not split
408 // Therefore this region under test is
409 // completely covering boxes[z]
411 // don't go any further down, generate a region and quit
413 // printf("Repaint region: %i %i %i %i\n", r.x, r.y, r.w, r.h);
418 // printf("Returning from Z=%i\n", z);
423 // printf("Z=%i S=%i E=%i D=%i: %i does not overlap\n", z, start, end, direction, z);
427 // if direction = 1 then we have come to a region that is
428 // entirely clear of higher boxes and needs to be redrawn
430 // if direction = -1 then we have come to a region that is on
431 // the very bottom with nothing below it to repaint.
432 // do nothing. stale window data will be left on screen?
440 /////////////////////////////////////////////////////////////////////////////
442 /////////////////////////////////////////////////////////////////////////////
444 // ---------------------------------------------------- END OF REMOVE CODE
447 void BoxStack::removeAll()
449 // 1.. Don't delete wallpaper. No point.
451 // Need locking on this one??
453 // This is pretty silly now that preDelete needs mutex unlocked
456 VideoDisplay *display = NULL;
468 toDel = boxes[numBoxes - 1];
476 // If boxes[numBoxes - 1] isn't toDel then there's a problem
477 if (boxes[numBoxes - 1] == toDel)
483 Log::getInstance()->log("BoxStack", Log::ERR, "Can this actually happen? Why?");
487 if (!videoStack.empty() && videoStack.top().first==toDel) {
489 if (!videoStack.empty()) display=&videoStack.top().second;
493 //AVO: do the delete outside the lock to allow for recursive deletes
494 Log::getInstance()->log("BoxStack", Log::DEBUG, "going to delete boxx %p, num=%d", toDel, numBoxes);
495 if (display) Video::getInstance()->setVideoDisplay(*display);
498 if (toDel) delete toDel;
504 int BoxStack::handleCommand(int command)
510 if (command != Remote::NA_NONE)
512 // handle command return values
513 // 0 - drop through to next box
514 // 1 - dont drop to next box, but not handled
515 // 2 - handled - stop command here
516 // 4 - handled - delete this box
518 for (i=numBoxes-1; i>=0; i--)
520 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Giving command to i=%i", i);
521 retVal = boxes[i]->handleCommand(command);
524 // not handled but don't give to any more boxes
534 else if (retVal == 4)
536 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Return 4: i=%i, boxes[i]=%p", i, boxes[i]);
545 // fake the return code
552 void BoxStack::processMessage(Message* m)
556 for (int i = numBoxes-1; i >= 0; i--)
558 if (boxes[i] == m->to)
560 Log::getInstance()->log("BoxStack", Log::DEBUG, "sending message from box %p to box %p %lu", m->from, m->to, m->message);
561 boxes[i]->processMessage(m);
568 /* Handle mouse events*/
569 // They come in with m->to = NULL? and just need to be delivered to top box?
570 if ((numBoxes > 1) && ((m->message == Message::MOUSE_MOVE) || (m->message == Message::MOUSE_LBDOWN)
571 || (m->message == Message::MOUSE_ANDROID_SCROLL)))
573 boxes[numBoxes-1]->processMessage(m);
577 Log::getInstance()->log("BoxStack", Log::DEBUG, "it's for meeee!");
581 case Message::CLOSE_ME:
583 remove((Boxx*)m->from);
586 case Message::ADD_VIEW: // currently not used by anything but it might come in useful again
588 Boxx* toAdd = (Boxx*)m->parameter;