2 Copyright 2004-2020 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, see <https://www.gnu.org/licenses/>.
22 #include "messagequeue.h"
26 BoxStack* BoxStack::instance = NULL;
41 BoxStack* BoxStack::getInstance()
48 if (initted) return 0;
54 int BoxStack::shutdown()
56 if (!initted) return 0;
62 void BoxStack::removeAll()
64 removeAllExceptWallpaper();
65 if (numBoxes == 1) remove(boxes[0]);
68 int BoxStack::addVideoDisplay(Boxx* box,VideoDisplay vd)
71 videoStack.push(std::pair<Boxx*,VideoDisplay>(box,vd));
73 Video::getInstance()->setVideoDisplay(vd);
77 int BoxStack::add(Boxx* v)
79 if (!initted) return 0;
80 Log::getInstance()->log("BoxStack", Log::DEBUG, "add called");
82 Log::getInstance()->log("BoxStack", Log::DEBUG, "Locked for add");
86 Log::getInstance()->log("BoxStack", Log::ERR, "More than 16 boxes! Unlocked for add");
90 boxes[numBoxes++] = v;
93 if (v->getVideoDisplay(vd)) {
94 Log::getInstance()->log("BoxStack", Log::DEBUG, "Add video display");
95 addVideoDisplay(v,vd);
100 Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for add");
105 // ---------------------------------------------------- REMOVE CODE
107 int BoxStack::remove(Boxx* toDelete)
109 if (!initted) return 0;
110 VideoDisplay *display = NULL;
113 Log::getInstance()->log("BoxStack", Log::DEBUG, "Locked for remove");
118 Log::getInstance()->log("BoxStack", Log::ERR, "Unlocked for remove numBoxes == 0");
122 // Log::getInstance()->log("BoxStack", Log::DEBUG, "entering remove, numBoxes=%i", numBoxes);
126 if (toDelete == NULL)
128 toDelete = boxes[numBoxes-1];
133 // to be deleted box is more likely to be at the top
134 for (i = numBoxes-1; i >= 0; i--)
136 // Log::getInstance()->log("BoxStack", Log::DEBUG, "todel: %p, i=%i, boxes[i]=%p", toDelete, i, boxes[i]);
137 if (boxes[i] == toDelete) break;
142 // not a Box we have!
145 Log::getInstance()->log("BoxStack", Log::ERR, "Unlocked for remove - no boxx deleted");
152 toDelete->preDelete();
156 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Starting deleteBox");
158 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Done deleteBox");
160 // Shift the boxes on top down one
162 for(int j = i; j < numBoxes; j++) boxes[j] = boxes[j+1];
164 // If there is only the wallpaper left signal command
167 Message* m = new Message();
168 m->p_to = Message::CONTROL;
169 m->message = Message::LAST_VIEW_CLOSE;
170 MessageQueue::getInstance()->postMessage(m);
173 if (!videoStack.empty() && videoStack.top().first==toDelete) {
175 if (!videoStack.empty()) display=&videoStack.top().second;
178 Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for remove");
181 //AVO: do this delete outside the lock to allow for recursive calls within the destructor
182 // as this box is not in the stack any more, there is no chance for a second delete
183 Log::getInstance()->log("BoxStack", Log::DEBUG, "remove: going to delete boxx %p, num %d", toDelete, numBoxes);
186 Log::getInstance()->log("BoxStack", Log::DEBUG, "setVideoDisplay %d %d %d %d %d %d", display->mode, display->fallbackMode,
187 display->x, display->y, display->width, display->height);
188 Video::getInstance()->setVideoDisplay(*display);
194 /////////////////////////////////////////////////////////////////////////////
196 /////////////////////////////////////////////////////////////////////////////
198 void BoxStack::deleteBox(int z)
200 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Delete box %i of %i", z, numBoxes);
202 boxSplit(boxes[z]->area, z + 1, numBoxes, 1, rl);
205 repaintRevealed(z, rl.front());
210 void BoxStack::redrawAllBoxes()
214 for (int z = 0; z < numBoxes; z++)
221 update(NULL,NULL); // should blt all
224 void BoxStack::update(Boxx* toUpdate, Region* regionToUpdate)
226 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Update called");
227 if (!initted) return; // it is allowed to call this before init
229 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Locked for update");
231 // Get the z index of the box
235 for (z = 0; z < numBoxes; z++)
237 if (boxes[z] == toUpdate) break;
242 // not a Box we have!
244 Log::getInstance()->log("BoxStack", Log::ERR, "Unlocked for update! box not inside boxstack");
255 Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for update, no box present");
259 // get the region for the whole box, could be less than that
260 // for smaller updates
262 Region r = toUpdate->area;
266 // Can be null if the whole box should be updated
267 // If this is given the numbers are relative to the size of the box, not the screen
269 r.x += regionToUpdate->x;
270 r.y += regionToUpdate->y;
271 r.w = regionToUpdate->w;
272 r.h = regionToUpdate->h;
278 boxSplit(r, z+1, numBoxes, 1, rl);
288 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for update");
291 void BoxStack::repaintRevealed(int x, Region r)
294 boxSplit(r, x - 1, -1, -1, rl);
300 boxes[r2.z]->blt(r2);
305 void BoxStack::boxSplit(Region r, int start, int end, int direction, RegionList& rl)
307 // 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);
309 for(int z = start; z != end; z += direction)
311 if (r.overlappedBy(boxes[z]->area))
313 // printf("Z=%i S=%i E=%i D=%i: %i overlaps\n", z, start, end, direction, z);
318 if (boxes[z]->area.y > r.y)
320 // 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);
321 top = boxes[z]->area.y;
326 newR.h = boxes[z]->area.y - r.y;
327 boxSplit(newR, z + direction, end, direction, rl);
333 newR2.y = boxes[z]->area.y;
335 newR2.h = r.h - newR.h;
336 boxSplit(newR2, z, end, -1, rl);
341 if (boxes[z]->area.y2() < r.y2())
343 // 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);
344 btm = boxes[z]->area.y2();
347 newR.y = boxes[z]->area.y2() + 1;
349 newR.h = r.y2() - newR.y + 1;
350 boxSplit(newR, z + direction, end, direction, rl);
358 newR2.h = r.h - newR.h;
359 boxSplit(newR2, z, end, -1, rl);
364 if (boxes[z]->area.x > r.x)
366 // 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);
370 newR.w = boxes[z]->area.x - r.x;
371 newR.h = btm - top + 1;
372 boxSplit(newR, z + direction, end, direction, rl);
377 newR2.x = r.x + newR.w;
379 newR2.w = r.w - newR.w;
381 boxSplit(newR2, z, end, -1, rl);
386 if (boxes[z]->area.x2() < r.x2())
388 // 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);
390 newR.x = boxes[z]->area.x2() + 1;
392 newR.w = r.x2() - newR.x + 1;
393 newR.h = btm - top + 1;
394 boxSplit(newR, z + direction, end, direction, rl);
401 newR2.w = r.w - newR.w;
403 boxSplit(newR2, z, end, -1, rl);
410 // we are going down the stack
411 // r is underlapped by boxes[z]
412 // but we have not split
413 // Therefore this region under test is
414 // completely covering boxes[z]
416 // don't go any further down, generate a region and quit
418 // printf("Repaint region: %i %i %i %i\n", r.x, r.y, r.w, r.h);
423 // printf("Returning from Z=%i\n", z);
428 // printf("Z=%i S=%i E=%i D=%i: %i does not overlap\n", z, start, end, direction, z);
432 // if direction = 1 then we have come to a region that is
433 // entirely clear of higher boxes and needs to be redrawn
435 // if direction = -1 then we have come to a region that is on
436 // the very bottom with nothing below it to repaint.
437 // do nothing. stale window data will be left on screen?
445 /////////////////////////////////////////////////////////////////////////////
447 /////////////////////////////////////////////////////////////////////////////
449 // ---------------------------------------------------- END OF REMOVE CODE
452 void BoxStack::removeAllExceptWallpaper()
454 // 1.. Don't delete wallpaper. No point.
456 // Need locking on this one??
458 // This is pretty silly now that preDelete needs mutex unlocked
461 VideoDisplay *display = NULL;
473 toDel = boxes[numBoxes - 1];
481 // If boxes[numBoxes - 1] isn't toDel then there's a problem
482 if (boxes[numBoxes - 1] == toDel)
488 Log::getInstance()->log("BoxStack", Log::ERR, "Can this actually happen? Why?");
492 if (!videoStack.empty() && videoStack.top().first==toDel) {
494 if (!videoStack.empty()) display=&videoStack.top().second;
498 //AVO: do the delete outside the lock to allow for recursive deletes
499 Log::getInstance()->log("BoxStack", Log::DEBUG, "going to delete boxx %p, num=%d", toDel, numBoxes);
500 if (display) Video::getInstance()->setVideoDisplay(*display);
503 if (toDel) delete toDel;
509 int BoxStack::handleCommand(int command)
515 if (command != Input::NA_NONE)
517 // handle command return values
518 // 0 - drop through to next box
519 // 1 - dont drop to next box, but not handled
520 // 2 - handled - stop command here
521 // 4 - handled - delete this box
523 for (i=numBoxes-1; i>=0; i--)
525 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Giving command to i=%i", i);
526 retVal = boxes[i]->handleCommand(command);
529 // not handled but don't give to any more boxes
539 else if (retVal == 4)
541 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Return 4: i=%i, boxes[i]=%p", i, boxes[i]);
550 // fake the return code
557 void BoxStack::processMessage(Message* m)
559 if ((m->p_to != Message::BOXSTACK) && (m->to != this))
561 for (int i = numBoxes-1; i >= 0; i--)
563 if (boxes[i] == m->to)
565 Log::getInstance()->log("BoxStack", Log::DEBUG, "sending message from box %p to box %p %lu", m->from, m->to, m->message);
566 boxes[i]->processMessage(m);
573 // Handle mouse events
574 // They just need to be delivered to top box
575 if (m->p_to == Message::MOUSE_RECEIVER)
577 if (numBoxes > 1) boxes[numBoxes-1]->processMessage(m);
581 Log::getInstance()->log("BoxStack", Log::DEBUG, "it's for meeee!");
585 case Message::CLOSE_ME:
587 remove(static_cast<Boxx*>(m->from));
590 case Message::ADD_VIEW:
592 Boxx* toAdd = reinterpret_cast<Boxx*>(m->data);