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)
69 videoStack.push(pair<Boxx*,VideoDisplay>(box,vd));
71 Video::getInstance()->setVideoDisplay(vd);
75 int BoxStack::add(Boxx* v)
77 if (!initted) return 0;
78 Log::getInstance()->log("BoxStack", Log::DEBUG, "add called");
80 Log::getInstance()->log("BoxStack", Log::DEBUG, "Locked for add");
84 Log::getInstance()->log("BoxStack", Log::ERR, "More than 16 boxes! Unlocked for add");
88 boxes[numBoxes++] = v;
91 if (v->getVideoDisplay(vd)) {
92 Log::getInstance()->log("BoxStack", Log::DEBUG, "Add video display");
93 addVideoDisplay(v,vd);
98 Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for add");
103 // ---------------------------------------------------- REMOVE CODE
105 int BoxStack::remove(Boxx* toDelete)
107 if (!initted) return 0;
108 VideoDisplay *display = NULL;
111 Log::getInstance()->log("BoxStack", Log::DEBUG, "Locked for remove");
116 Log::getInstance()->log("BoxStack", Log::ERR, "Unlocked for remove numBoxes == 0");
120 // Log::getInstance()->log("BoxStack", Log::DEBUG, "entering remove, numBoxes=%i", numBoxes);
124 if (toDelete == NULL)
126 toDelete = boxes[numBoxes-1];
131 // to be deleted box is more likely to be at the top
132 for (i = numBoxes-1; i >= 0; i--)
134 // Log::getInstance()->log("BoxStack", Log::DEBUG, "todel: %p, i=%i, boxes[i]=%p", toDelete, i, boxes[i]);
135 if (boxes[i] == toDelete) break;
140 // not a Box we have!
143 Log::getInstance()->log("BoxStack", Log::ERR, "Unlocked for remove - no boxx deleted");
150 toDelete->preDelete();
154 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Starting deleteBox");
156 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Done deleteBox");
158 // Shift the boxes on top down one
160 for(int j = i; j < numBoxes; j++) boxes[j] = boxes[j+1];
162 // If there is only the wallpaper left signal command
165 Message* m = new Message();
166 m->to = Command::getInstance();
167 m->message = Message::LAST_VIEW_CLOSE;
168 Command::getInstance()->postMessageNoLock(m);
171 if (!videoStack.empty() && videoStack.top().first==toDelete) {
173 if (!videoStack.empty()) display=&videoStack.top().second;
176 Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for remove");
179 //AVO: do this delete outside the lock to allow for recursive calls within the destructor
180 // as this box is not in the stack any more, there is no chance for a second delete
181 Log::getInstance()->log("BoxStack", Log::DEBUG, "remove: going to delete boxx %p, num %d", toDelete, numBoxes);
184 Log::getInstance()->log("BoxStack", Log::DEBUG, "setVideoDisplay %d %d %d %d %d %d", display->mode, display->fallbackMode,
185 display->x, display->y, display->width, display->height);
186 Video::getInstance()->setVideoDisplay(*display);
192 /////////////////////////////////////////////////////////////////////////////
194 /////////////////////////////////////////////////////////////////////////////
196 void BoxStack::deleteBox(int z)
198 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Delete box %i of %i", z, numBoxes);
200 boxSplit(boxes[z]->area, z + 1, numBoxes, 1, rl);
203 repaintRevealed(z, rl.front());
208 void BoxStack::redrawAllBoxes()
212 for (int z = 0; z < numBoxes; z++)
219 update(NULL,NULL); // should blt all
222 void BoxStack::update(Boxx* toUpdate, Region* regionToUpdate)
224 Log::getInstance()->log("BoxStack", Log::DEBUG, "Update called");
225 if (!initted) return; // it is allowed to call this before init
227 Log::getInstance()->log("BoxStack", Log::DEBUG, "Locked for update");
229 // Get the z index of the box
233 for (z = 0; z < numBoxes; z++)
235 if (boxes[z] == toUpdate) break;
240 // not a Box we have!
242 Log::getInstance()->log("BoxStack", Log::ERR, "Unlocked for update! box not inside boxstack");
253 Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for update, no box present");
257 // get the region for the whole box, could be less than that
258 // for smaller updates
260 Region r = toUpdate->area;
264 // Can be null if the whole box should be updated
265 // If this is given the numbers are relative to the size of the box, not the screen
267 r.x += regionToUpdate->x;
268 r.y += regionToUpdate->y;
269 r.w = regionToUpdate->w;
270 r.h = regionToUpdate->h;
276 boxSplit(r, z+1, numBoxes, 1, rl);
286 Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for update");
289 void BoxStack::repaintRevealed(int x, Region r)
292 boxSplit(r, x - 1, -1, -1, rl);
298 boxes[r2.z]->blt(r2);
303 void BoxStack::boxSplit(Region r, int start, int end, int direction, RegionList& rl)
305 // 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);
307 for(int z = start; z != end; z += direction)
309 if (r.overlappedBy(boxes[z]->area))
311 // printf("Z=%i S=%i E=%i D=%i: %i overlaps\n", z, start, end, direction, z);
316 if (boxes[z]->area.y > r.y)
318 // 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);
319 top = boxes[z]->area.y;
324 newR.h = boxes[z]->area.y - r.y;
325 boxSplit(newR, z + direction, end, direction, rl);
331 newR2.y = boxes[z]->area.y;
333 newR2.h = r.h - newR.h;
334 boxSplit(newR2, z, end, -1, rl);
339 if (boxes[z]->area.y2() < r.y2())
341 // 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);
342 btm = boxes[z]->area.y2();
345 newR.y = boxes[z]->area.y2() + 1;
347 newR.h = r.y2() - newR.y + 1;
348 boxSplit(newR, z + direction, end, direction, rl);
356 newR2.h = r.h - newR.h;
357 boxSplit(newR2, z, end, -1, rl);
362 if (boxes[z]->area.x > r.x)
364 // 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);
368 newR.w = boxes[z]->area.x - r.x;
369 newR.h = btm - top + 1;
370 boxSplit(newR, z + direction, end, direction, rl);
375 newR2.x = r.x + newR.w;
377 newR2.w = r.w - newR.w;
379 boxSplit(newR2, z, end, -1, rl);
384 if (boxes[z]->area.x2() < r.x2())
386 // 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);
388 newR.x = boxes[z]->area.x2() + 1;
390 newR.w = r.x2() - newR.x + 1;
391 newR.h = btm - top + 1;
392 boxSplit(newR, z + direction, end, direction, rl);
399 newR2.w = r.w - newR.w;
401 boxSplit(newR2, z, end, -1, rl);
408 // we are going down the stack
409 // r is underlapped by boxes[z]
410 // but we have not split
411 // Therefore this region under test is
412 // completely covering boxes[z]
414 // don't go any further down, generate a region and quit
416 // printf("Repaint region: %i %i %i %i\n", r.x, r.y, r.w, r.h);
421 // printf("Returning from Z=%i\n", z);
426 // printf("Z=%i S=%i E=%i D=%i: %i does not overlap\n", z, start, end, direction, z);
430 // if direction = 1 then we have come to a region that is
431 // entirely clear of higher boxes and needs to be redrawn
433 // if direction = -1 then we have come to a region that is on
434 // the very bottom with nothing below it to repaint.
435 // do nothing. stale window data will be left on screen?
443 /////////////////////////////////////////////////////////////////////////////
445 /////////////////////////////////////////////////////////////////////////////
447 // ---------------------------------------------------- END OF REMOVE CODE
450 void BoxStack::removeAll()
452 // 1.. Don't delete wallpaper. No point.
454 // Need locking on this one??
456 // This is pretty silly now that preDelete needs mutex unlocked
459 VideoDisplay *display = NULL;
471 toDel = boxes[numBoxes - 1];
479 // If boxes[numBoxes - 1] isn't toDel then there's a problem
480 if (boxes[numBoxes - 1] == toDel)
486 Log::getInstance()->log("BoxStack", Log::ERR, "Can this actually happen? Why?");
490 if (!videoStack.empty() && videoStack.top().first==toDel) {
492 if (!videoStack.empty()) display=&videoStack.top().second;
496 //AVO: do the delete outside the lock to allow for recursive deletes
497 Log::getInstance()->log("BoxStack", Log::DEBUG, "going to delete boxx %p, num=%d", toDel, numBoxes);
498 if (display) Video::getInstance()->setVideoDisplay(*display);
501 if (toDel) delete toDel;
507 int BoxStack::handleCommand(int command)
513 if (command != Remote::NA_NONE)
515 // handle command return values
516 // 0 - drop through to next box
517 // 1 - dont drop to next box, but not handled
518 // 2 - handled - stop command here
519 // 4 - handled - delete this box
521 for (i=numBoxes-1; i>=0; i--)
523 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Giving command to i=%i", i);
524 retVal = boxes[i]->handleCommand(command);
527 // not handled but don't give to any more boxes
537 else if (retVal == 4)
539 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Return 4: i=%i, boxes[i]=%p", i, boxes[i]);
548 // fake the return code
555 void BoxStack::processMessage(Message* m)
559 for (int i = numBoxes-1; i >= 0; i--)
561 if (boxes[i] == m->to)
563 Log::getInstance()->log("BoxStack", Log::DEBUG, "sending message from box %p to box %p %lu", m->from, m->to, m->message);
564 boxes[i]->processMessage(m);
571 /* Handle mouse events*/
572 // They come in with m->to = NULL? and just need to be delivered to top box?
573 if ((numBoxes > 1) && ((m->message == Message::MOUSE_MOVE) || (m->message == Message::MOUSE_LBDOWN)
574 || (m->message == Message::MOUSE_SCROLL)))
576 boxes[numBoxes-1]->processMessage(m);
580 Log::getInstance()->log("BoxStack", Log::DEBUG, "it's for meeee!");
584 case Message::CLOSE_ME:
586 remove((Boxx*)m->from);
589 case Message::ADD_VIEW: // currently not used by anything but it might come in useful again
591 Boxx* toAdd = (Boxx*)m->parameter;