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;
63 void BoxStack::removeAll()
65 removeAllExceptWallpaper();
66 if (numBoxes == 1) remove(boxes[0]);
69 int BoxStack::addVideoDisplay(Boxx* box,VideoDisplay vd)
72 videoStack.push(pair<Boxx*,VideoDisplay>(box,vd));
74 Video::getInstance()->setVideoDisplay(vd);
78 int BoxStack::add(Boxx* v)
80 if (!initted) return 0;
81 Log::getInstance()->log("BoxStack", Log::DEBUG, "add called");
83 Log::getInstance()->log("BoxStack", Log::DEBUG, "Locked for add");
87 Log::getInstance()->log("BoxStack", Log::ERR, "More than 16 boxes! Unlocked for add");
91 boxes[numBoxes++] = v;
94 if (v->getVideoDisplay(vd)) {
95 Log::getInstance()->log("BoxStack", Log::DEBUG, "Add video display");
96 addVideoDisplay(v,vd);
101 Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for add");
106 // ---------------------------------------------------- REMOVE CODE
108 int BoxStack::remove(Boxx* toDelete)
110 if (!initted) return 0;
111 VideoDisplay *display = NULL;
114 Log::getInstance()->log("BoxStack", Log::DEBUG, "Locked for remove");
119 Log::getInstance()->log("BoxStack", Log::ERR, "Unlocked for remove numBoxes == 0");
123 // Log::getInstance()->log("BoxStack", Log::DEBUG, "entering remove, numBoxes=%i", numBoxes);
127 if (toDelete == NULL)
129 toDelete = boxes[numBoxes-1];
134 // to be deleted box is more likely to be at the top
135 for (i = numBoxes-1; i >= 0; i--)
137 // Log::getInstance()->log("BoxStack", Log::DEBUG, "todel: %p, i=%i, boxes[i]=%p", toDelete, i, boxes[i]);
138 if (boxes[i] == toDelete) break;
143 // not a Box we have!
146 Log::getInstance()->log("BoxStack", Log::ERR, "Unlocked for remove - no boxx deleted");
153 toDelete->preDelete();
157 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Starting deleteBox");
159 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Done deleteBox");
161 // Shift the boxes on top down one
163 for(int j = i; j < numBoxes; j++) boxes[j] = boxes[j+1];
165 // If there is only the wallpaper left signal command
168 Message* m = new Message();
169 m->to = Command::getInstance();
170 m->message = Message::LAST_VIEW_CLOSE;
171 Command::getInstance()->postMessageNoLock(m);
174 if (!videoStack.empty() && videoStack.top().first==toDelete) {
176 if (!videoStack.empty()) display=&videoStack.top().second;
179 Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for remove");
182 //AVO: do this delete outside the lock to allow for recursive calls within the destructor
183 // as this box is not in the stack any more, there is no chance for a second delete
184 Log::getInstance()->log("BoxStack", Log::DEBUG, "remove: going to delete boxx %p, num %d", toDelete, numBoxes);
187 Log::getInstance()->log("BoxStack", Log::DEBUG, "setVideoDisplay %d %d %d %d %d %d", display->mode, display->fallbackMode,
188 display->x, display->y, display->width, display->height);
189 Video::getInstance()->setVideoDisplay(*display);
195 /////////////////////////////////////////////////////////////////////////////
197 /////////////////////////////////////////////////////////////////////////////
199 void BoxStack::deleteBox(int z)
201 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Delete box %i of %i", z, numBoxes);
203 boxSplit(boxes[z]->area, z + 1, numBoxes, 1, rl);
206 repaintRevealed(z, rl.front());
211 void BoxStack::redrawAllBoxes()
215 for (int z = 0; z < numBoxes; z++)
222 update(NULL,NULL); // should blt all
225 void BoxStack::update(Boxx* toUpdate, Region* regionToUpdate)
227 Log::getInstance()->log("BoxStack", Log::DEBUG, "Update called");
228 if (!initted) return; // it is allowed to call this before init
230 Log::getInstance()->log("BoxStack", Log::DEBUG, "Locked for update");
232 // Get the z index of the box
236 for (z = 0; z < numBoxes; z++)
238 if (boxes[z] == toUpdate) break;
243 // not a Box we have!
245 Log::getInstance()->log("BoxStack", Log::ERR, "Unlocked for update! box not inside boxstack");
256 Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for update, no box present");
260 // get the region for the whole box, could be less than that
261 // for smaller updates
263 Region r = toUpdate->area;
267 // Can be null if the whole box should be updated
268 // If this is given the numbers are relative to the size of the box, not the screen
270 r.x += regionToUpdate->x;
271 r.y += regionToUpdate->y;
272 r.w = regionToUpdate->w;
273 r.h = regionToUpdate->h;
279 boxSplit(r, z+1, numBoxes, 1, rl);
289 Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for update");
292 void BoxStack::repaintRevealed(int x, Region r)
295 boxSplit(r, x - 1, -1, -1, rl);
301 boxes[r2.z]->blt(r2);
306 void BoxStack::boxSplit(Region r, int start, int end, int direction, RegionList& rl)
308 // 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);
310 for(int z = start; z != end; z += direction)
312 if (r.overlappedBy(boxes[z]->area))
314 // printf("Z=%i S=%i E=%i D=%i: %i overlaps\n", z, start, end, direction, z);
319 if (boxes[z]->area.y > r.y)
321 // 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);
322 top = boxes[z]->area.y;
327 newR.h = boxes[z]->area.y - r.y;
328 boxSplit(newR, z + direction, end, direction, rl);
334 newR2.y = boxes[z]->area.y;
336 newR2.h = r.h - newR.h;
337 boxSplit(newR2, z, end, -1, rl);
342 if (boxes[z]->area.y2() < r.y2())
344 // 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);
345 btm = boxes[z]->area.y2();
348 newR.y = boxes[z]->area.y2() + 1;
350 newR.h = r.y2() - newR.y + 1;
351 boxSplit(newR, z + direction, end, direction, rl);
359 newR2.h = r.h - newR.h;
360 boxSplit(newR2, z, end, -1, rl);
365 if (boxes[z]->area.x > r.x)
367 // 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);
371 newR.w = boxes[z]->area.x - r.x;
372 newR.h = btm - top + 1;
373 boxSplit(newR, z + direction, end, direction, rl);
378 newR2.x = r.x + newR.w;
380 newR2.w = r.w - newR.w;
382 boxSplit(newR2, z, end, -1, rl);
387 if (boxes[z]->area.x2() < r.x2())
389 // 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);
391 newR.x = boxes[z]->area.x2() + 1;
393 newR.w = r.x2() - newR.x + 1;
394 newR.h = btm - top + 1;
395 boxSplit(newR, z + direction, end, direction, rl);
402 newR2.w = r.w - newR.w;
404 boxSplit(newR2, z, end, -1, rl);
411 // we are going down the stack
412 // r is underlapped by boxes[z]
413 // but we have not split
414 // Therefore this region under test is
415 // completely covering boxes[z]
417 // don't go any further down, generate a region and quit
419 // printf("Repaint region: %i %i %i %i\n", r.x, r.y, r.w, r.h);
424 // printf("Returning from Z=%i\n", z);
429 // printf("Z=%i S=%i E=%i D=%i: %i does not overlap\n", z, start, end, direction, z);
433 // if direction = 1 then we have come to a region that is
434 // entirely clear of higher boxes and needs to be redrawn
436 // if direction = -1 then we have come to a region that is on
437 // the very bottom with nothing below it to repaint.
438 // do nothing. stale window data will be left on screen?
446 /////////////////////////////////////////////////////////////////////////////
448 /////////////////////////////////////////////////////////////////////////////
450 // ---------------------------------------------------- END OF REMOVE CODE
453 void BoxStack::removeAllExceptWallpaper()
455 // 1.. Don't delete wallpaper. No point.
457 // Need locking on this one??
459 // This is pretty silly now that preDelete needs mutex unlocked
462 VideoDisplay *display = NULL;
474 toDel = boxes[numBoxes - 1];
482 // If boxes[numBoxes - 1] isn't toDel then there's a problem
483 if (boxes[numBoxes - 1] == toDel)
489 Log::getInstance()->log("BoxStack", Log::ERR, "Can this actually happen? Why?");
493 if (!videoStack.empty() && videoStack.top().first==toDel) {
495 if (!videoStack.empty()) display=&videoStack.top().second;
499 //AVO: do the delete outside the lock to allow for recursive deletes
500 Log::getInstance()->log("BoxStack", Log::DEBUG, "going to delete boxx %p, num=%d", toDel, numBoxes);
501 if (display) Video::getInstance()->setVideoDisplay(*display);
504 if (toDel) delete toDel;
510 int BoxStack::handleCommand(int command)
516 if (command != Remote::NA_NONE)
518 // handle command return values
519 // 0 - drop through to next box
520 // 1 - dont drop to next box, but not handled
521 // 2 - handled - stop command here
522 // 4 - handled - delete this box
524 for (i=numBoxes-1; i>=0; i--)
526 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Giving command to i=%i", i);
527 retVal = boxes[i]->handleCommand(command);
530 // not handled but don't give to any more boxes
540 else if (retVal == 4)
542 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Return 4: i=%i, boxes[i]=%p", i, boxes[i]);
551 // fake the return code
558 void BoxStack::processMessage(Message* m)
562 for (int i = numBoxes-1; i >= 0; i--)
564 if (boxes[i] == m->to)
566 Log::getInstance()->log("BoxStack", Log::DEBUG, "sending message from box %p to box %p %lu", m->from, m->to, m->message);
567 boxes[i]->processMessage(m);
574 /* Handle mouse events*/
575 // They come in with m->to = NULL? and just need to be delivered to top box?
576 if ((numBoxes > 1) && ((m->message == Message::MOUSE_MOVE) || (m->message == Message::MOUSE_LBDOWN)
577 || (m->message == Message::MOUSE_SCROLL)))
579 boxes[numBoxes-1]->processMessage(m);
583 Log::getInstance()->log("BoxStack", Log::DEBUG, "it's for meeee!");
587 case Message::CLOSE_ME:
589 remove((Boxx*)m->from);
592 case Message::ADD_VIEW: // currently not used by anything but it might come in useful again
594 Boxx* toAdd = (Boxx*)m->parameter.num;