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::add(Boxx* v)
68 if (!initted) return 0;
69 Log::getInstance()->log("BoxStack", Log::DEBUG, "add called");
71 Log::getInstance()->log("BoxStack", Log::DEBUG, "Locked for add");
75 Log::getInstance()->log("BoxStack", Log::ERR, "More than 16 boxes! Unlocked for add");
79 boxes[numBoxes++] = v;
83 Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for add");
88 // ---------------------------------------------------- REMOVE CODE
90 int BoxStack::remove(Boxx* toDelete)
92 if (!initted) return 0;
95 Log::getInstance()->log("BoxStack", Log::DEBUG, "Locked for remove");
100 Log::getInstance()->log("BoxStack", Log::ERR, "Unlocked for remove numBoxes == 0");
104 // Log::getInstance()->log("BoxStack", Log::DEBUG, "entering remove, numBoxes=%i", numBoxes);
108 if (toDelete == NULL)
110 toDelete = boxes[numBoxes-1];
115 // to be deleted box is more likely to be at the top
116 for (i = numBoxes-1; i >= 0; i--)
118 // Log::getInstance()->log("BoxStack", Log::DEBUG, "todel: %p, i=%i, boxes[i]=%p", toDelete, i, boxes[i]);
119 if (boxes[i] == toDelete) break;
124 // not a Box we have!
127 Log::getInstance()->log("BoxStack", Log::ERR, "Unlocked for remove - no boxx deleted");
134 toDelete->preDelete();
138 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Starting deleteBox");
140 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Done deleteBox");
142 // Shift the boxes on top down one
144 for(int j = i; j < numBoxes; j++) boxes[j] = boxes[j+1];
146 // If there is only the wallpaper left signal command
149 Message* m = new Message();
150 m->to = Command::getInstance();
151 m->message = Message::LAST_VIEW_CLOSE;
152 Command::getInstance()->postMessageNoLock(m);
156 Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for remove");
159 //AVO: do this delete outside the lock to allow for recursive calls within the destructor
160 // as this box is not in the stack any more, there is no chance for a second delete
161 Log::getInstance()->log("BoxStack", Log::DEBUG, "remove: going to delete boxx %p, num %d", toDelete, numBoxes);
167 /////////////////////////////////////////////////////////////////////////////
169 /////////////////////////////////////////////////////////////////////////////
171 void BoxStack::deleteBox(int z)
173 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Delete box %i of %i", z, numBoxes);
175 boxSplit(boxes[z]->area, z + 1, numBoxes, 1, rl);
178 repaintRevealed(z, rl.front());
183 void BoxStack::redrawAllBoxes()
187 for (int z = 0; z < numBoxes; z++)
194 update(NULL,NULL); // should blt all
197 void BoxStack::update(Boxx* toUpdate, Region* regionToUpdate)
199 Log::getInstance()->log("BoxStack", Log::DEBUG, "Update called");
200 if (!initted) return; // it is allowed to call this before init
202 Log::getInstance()->log("BoxStack", Log::DEBUG, "Locked for update");
204 // Get the z index of the box
208 for (z = 0; z < numBoxes; z++)
210 if (boxes[z] == toUpdate) break;
215 // not a Box we have!
217 Log::getInstance()->log("BoxStack", Log::ERR, "Unlocked for update! box not inside boxstack");
228 Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for update, no box present");
232 // get the region for the whole box, could be less than that
233 // for smaller updates
235 Region r = toUpdate->area;
239 // Can be null if the whole box should be updated
240 // If this is given the numbers are relative to the size of the box, not the screen
242 r.x += regionToUpdate->x;
243 r.y += regionToUpdate->y;
244 r.w = regionToUpdate->w;
245 r.h = regionToUpdate->h;
251 boxSplit(r, z+1, numBoxes, 1, rl);
261 Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for update");
264 void BoxStack::repaintRevealed(int x, Region r)
267 boxSplit(r, x - 1, -1, -1, rl);
273 boxes[r2.z]->blt(r2);
278 void BoxStack::boxSplit(Region r, int start, int end, int direction, RegionList& rl)
280 // 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);
282 for(int z = start; z != end; z += direction)
284 if (r.overlappedBy(boxes[z]->area))
286 // printf("Z=%i S=%i E=%i D=%i: %i overlaps\n", z, start, end, direction, z);
291 if (boxes[z]->area.y > r.y)
293 // 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);
294 top = boxes[z]->area.y;
299 newR.h = boxes[z]->area.y - r.y;
300 boxSplit(newR, z + direction, end, direction, rl);
306 newR2.y = boxes[z]->area.y;
308 newR2.h = r.h - newR.h;
309 boxSplit(newR2, z, end, -1, rl);
314 if (boxes[z]->area.y2() < r.y2())
316 // 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);
317 btm = boxes[z]->area.y2();
320 newR.y = boxes[z]->area.y2() + 1;
322 newR.h = r.y2() - newR.y + 1;
323 boxSplit(newR, z + direction, end, direction, rl);
331 newR2.h = r.h - newR.h;
332 boxSplit(newR2, z, end, -1, rl);
337 if (boxes[z]->area.x > r.x)
339 // 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);
343 newR.w = boxes[z]->area.x - r.x;
344 newR.h = btm - top + 1;
345 boxSplit(newR, z + direction, end, direction, rl);
350 newR2.x = r.x + newR.w;
352 newR2.w = r.w - newR.w;
354 boxSplit(newR2, z, end, -1, rl);
359 if (boxes[z]->area.x2() < r.x2())
361 // 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);
363 newR.x = boxes[z]->area.x2() + 1;
365 newR.w = r.x2() - newR.x + 1;
366 newR.h = btm - top + 1;
367 boxSplit(newR, z + direction, end, direction, rl);
374 newR2.w = r.w - newR.w;
376 boxSplit(newR2, z, end, -1, rl);
383 // we are going down the stack
384 // r is underlapped by boxes[z]
385 // but we have not split
386 // Therefore this region under test is
387 // completely covering boxes[z]
389 // don't go any further down, generate a region and quit
391 // printf("Repaint region: %i %i %i %i\n", r.x, r.y, r.w, r.h);
396 // printf("Returning from Z=%i\n", z);
401 // printf("Z=%i S=%i E=%i D=%i: %i does not overlap\n", z, start, end, direction, z);
405 // if direction = 1 then we have come to a region that is
406 // entirely clear of higher boxes and needs to be redrawn
408 // if direction = -1 then we have come to a region that is on
409 // the very bottom with nothing below it to repaint.
410 // do nothing. stale window data will be left on screen?
418 /////////////////////////////////////////////////////////////////////////////
420 /////////////////////////////////////////////////////////////////////////////
422 // ---------------------------------------------------- END OF REMOVE CODE
425 void BoxStack::removeAll()
427 // 1.. Don't delete wallpaper. No point.
429 // Need locking on this one??
431 // This is pretty silly now that preDelete needs mutex unlocked
445 toDel = boxes[numBoxes - 1];
453 // If boxes[numBoxes - 1] isn't toDel then there's a problem
454 if (boxes[numBoxes - 1] == toDel)
460 Log::getInstance()->log("BoxStack", Log::ERR, "Can this actually happen? Why?");
466 //AVO: do the delete outside the lock to allow for recursive deletes
467 Log::getInstance()->log("BoxStack", Log::DEBUG, "going to delete boxx %p, num=%d", toDel, numBoxes);
468 if (toDel) delete toDel;
472 int BoxStack::handleCommand(int command)
478 if (command != Remote::NA_NONE)
480 // handle command return values
481 // 0 - drop through to next box
482 // 1 - dont drop to next box, but not handled
483 // 2 - handled - stop command here
484 // 4 - handled - delete this box
486 for (i=numBoxes-1; i>=0; i--)
488 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Giving command to i=%i", i);
489 retVal = boxes[i]->handleCommand(command);
492 // not handled but don't give to any more boxes
502 else if (retVal == 4)
504 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Return 4: i=%i, boxes[i]=%p", i, boxes[i]);
513 // fake the return code
520 void BoxStack::processMessage(Message* m)
524 for (int i = numBoxes-1; i >= 0; i--)
526 if (boxes[i] == m->to)
528 Log::getInstance()->log("BoxStack", Log::DEBUG, "sending message from box %p to box %p %lu", m->from, m->to, m->message);
529 boxes[i]->processMessage(m);
536 /* Handle mouse events*/
537 // They come in with m->to = NULL? and just need to be delivered to top box?
538 if ((numBoxes > 1) && ((m->message == Message::MOUSE_MOVE) || (m->message == Message::MOUSE_LBDOWN)
539 || (m->message == Message::MOUSE_ANDROID_SCROLL)))
541 boxes[numBoxes-1]->processMessage(m);
545 Log::getInstance()->log("BoxStack", Log::DEBUG, "it's for meeee!");
549 case Message::CLOSE_ME:
551 remove((Boxx*)m->from);
554 case Message::ADD_VIEW: // currently not used by anything but it might come in useful again
556 Boxx* toAdd = (Boxx*)m->parameter;