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;
53 pthread_mutex_init(&boxLock, NULL);
55 boxLock = CreateMutex(NULL,FALSE,NULL);
61 int BoxStack::shutdown()
63 if (!initted) return 0;
65 // FIXME don't think this can work properly, removeAll leaves the wallpaper there!
72 int BoxStack::add(Boxx* v)
74 if (!initted) return 0;
77 pthread_mutex_lock(&boxLock);
79 WaitForSingleObject(boxLock, INFINITE);
81 Log::getInstance()->log("BoxStack", Log::DEBUG, "Locked for add");
83 if (numBoxes == 16) return 0;
84 boxes[numBoxes++] = v;
87 pthread_mutex_unlock(&boxLock);
89 ReleaseMutex(boxLock);
92 Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for add");
97 // ---------------------------------------------------- REMOVE CODE
99 int BoxStack::remove(Boxx* toDelete)
101 if (!initted) return 0;
104 pthread_mutex_lock(&boxLock);
106 WaitForSingleObject(boxLock, INFINITE);
108 Log::getInstance()->log("BoxStack", Log::DEBUG, "Locked for remove");
113 pthread_mutex_unlock(&boxLock);
115 ReleaseMutex(boxLock);
117 Log::getInstance()->log("BoxStack", Log::ERR, "Unlocked for remove numBoxes == 0");
121 // Log::getInstance()->log("BoxStack", Log::DEBUG, "entering remove, numBoxes=%i", numBoxes);
125 if (toDelete == NULL)
127 toDelete = boxes[numBoxes-1];
132 // to be deleted box is more likely to be at the top
133 for (i = numBoxes-1; i >= 0; i--)
135 // Log::getInstance()->log("BoxStack", Log::DEBUG, "todel: %p, i=%i, boxes[i]=%p", toDelete, i, boxes[i]);
136 if (boxes[i] == toDelete) break;
141 // not a Box we have!
144 pthread_mutex_unlock(&boxLock);
146 ReleaseMutex(boxLock);
148 Log::getInstance()->log("BoxStack", Log::ERR, "Unlocked for remove - no boxx deleted");
154 pthread_mutex_unlock(&boxLock);
156 ReleaseMutex(boxLock);
159 toDelete->preDelete();
162 pthread_mutex_lock(&boxLock);
164 WaitForSingleObject(boxLock, INFINITE);
167 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Starting deleteBox");
169 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Done deleteBox");
171 // Shift the boxes on top down one
173 for(int j = i; j < numBoxes; j++) boxes[j] = boxes[j+1];
175 // If there is only the wallpaper left signal command
178 Message* m = new Message();
179 m->to = Command::getInstance();
180 m->message = Message::LAST_VIEW_CLOSE;
181 Command::getInstance()->postMessageNoLock(m);
185 pthread_mutex_unlock(&boxLock);
187 ReleaseMutex(boxLock);
189 Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for remove");
192 //AVO: do this delete outside the lock to allow for recursive calls within the destructor
193 // as this box is not in the stack any more, there is no chance for a second delete
194 Log::getInstance()->log("BoxStack", Log::DEBUG, "remove: going to delete boxx %p, num %d", toDelete, numBoxes);
200 /////////////////////////////////////////////////////////////////////////////
202 /////////////////////////////////////////////////////////////////////////////
204 void BoxStack::deleteBox(int z)
206 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Delete box %i of %i", z, numBoxes);
208 boxSplit(boxes[z]->area, z + 1, numBoxes, 1, rl);
211 repaintRevealed(z, rl.front());
216 void BoxStack::update(Boxx* toUpdate, Region* regionToUpdate)
218 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Update called");
219 // Get the z index of the box
222 pthread_mutex_lock(&boxLock);
224 WaitForSingleObject(boxLock, INFINITE);
226 Log::getInstance()->log("BoxStack", Log::DEBUG, "Locked for update");
229 for (z = 0; z < numBoxes; z++)
231 if (boxes[z] == toUpdate) break;
236 // not a Box we have!
240 // get the region for the whole box, could be less than that
241 // for smaller updates
243 Region r = toUpdate->area;
247 // Can be null if the whole box should be updated
248 // If this is given the numbers are relative to the size of the box, not the screen
250 r.x += regionToUpdate->x;
251 r.y += regionToUpdate->y;
252 r.w = regionToUpdate->w;
253 r.h = regionToUpdate->h;
259 boxSplit(r, z+1, numBoxes, 1, rl);
269 pthread_mutex_unlock(&boxLock);
271 ReleaseMutex(boxLock);
273 Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for update");
276 void BoxStack::repaintRevealed(int x, Region r)
279 boxSplit(r, x - 1, -1, -1, rl);
285 boxes[r2.z]->blt(r2);
290 void BoxStack::boxSplit(Region r, int start, int end, int direction, RegionList& rl)
292 // 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);
294 for(int z = start; z != end; z += direction)
296 if (r.overlappedBy(boxes[z]->area))
298 // printf("Z=%i S=%i E=%i D=%i: %i overlaps\n", z, start, end, direction, z);
303 if (boxes[z]->area.y > r.y)
305 // 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);
306 top = boxes[z]->area.y;
311 newR.h = boxes[z]->area.y - r.y;
312 boxSplit(newR, z + direction, end, direction, rl);
318 newR2.y = boxes[z]->area.y;
320 newR2.h = r.h - newR.h;
321 boxSplit(newR2, z, end, -1, rl);
326 if (boxes[z]->area.y2() < r.y2())
328 // 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);
329 btm = boxes[z]->area.y2();
332 newR.y = boxes[z]->area.y2() + 1;
334 newR.h = r.y2() - newR.y + 1;
335 boxSplit(newR, z + direction, end, direction, rl);
343 newR2.h = r.h - newR.h;
344 boxSplit(newR2, z, end, -1, rl);
349 if (boxes[z]->area.x > r.x)
351 // 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);
355 newR.w = boxes[z]->area.x - r.x;
356 newR.h = btm - top + 1;
357 boxSplit(newR, z + direction, end, direction, rl);
362 newR2.x = r.x + newR.w;
364 newR2.w = r.w - newR.w;
366 boxSplit(newR2, z, end, -1, rl);
371 if (boxes[z]->area.x2() < r.x2())
373 // 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);
375 newR.x = boxes[z]->area.x2() + 1;
377 newR.w = r.x2() - newR.x + 1;
378 newR.h = btm - top + 1;
379 boxSplit(newR, z + direction, end, direction, rl);
386 newR2.w = r.w - newR.w;
388 boxSplit(newR2, z, end, -1, rl);
395 // we are going down the stack
396 // r is underlapped by boxes[z]
397 // but we have not split
398 // Therefore this region under test is
399 // completely covering boxes[z]
401 // don't go any further down, generate a region and quit
403 // printf("Repaint region: %i %i %i %i\n", r.x, r.y, r.w, r.h);
408 // printf("Returning from Z=%i\n", z);
413 // printf("Z=%i S=%i E=%i D=%i: %i does not overlap\n", z, start, end, direction, z);
417 // if direction = 1 then we have come to a region that is
418 // entirely clear of higher boxes and needs to be redrawn
420 // if direction = -1 then we have come to a region that is on
421 // the very bottom with nothing below it to repaint.
422 // do nothing. stale window data will be left on screen?
430 /////////////////////////////////////////////////////////////////////////////
432 /////////////////////////////////////////////////////////////////////////////
434 // ---------------------------------------------------- END OF REMOVE CODE
437 void BoxStack::removeAll()
439 // 1.. Don't delete wallpaper. No point.
441 // Need locking on this one??
443 // This is pretty silly now that preDelete needs mutex unlocked
450 pthread_mutex_lock(&boxLock);
452 WaitForSingleObject(boxLock, INFINITE);
458 pthread_mutex_unlock(&boxLock);
460 ReleaseMutex(boxLock);
465 toDel = boxes[numBoxes - 1];
468 pthread_mutex_unlock(&boxLock);
470 ReleaseMutex(boxLock);
476 pthread_mutex_lock(&boxLock);
478 WaitForSingleObject(boxLock, INFINITE);
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?");
493 pthread_mutex_unlock(&boxLock);
495 ReleaseMutex(boxLock);
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 (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)))
572 boxes[numBoxes-1]->processMessage(m);
576 Log::getInstance()->log("BoxStack", Log::DEBUG, "it's for meeee!");
580 case Message::CLOSE_ME:
582 remove((Boxx*)m->from);
585 case Message::ADD_VIEW: // currently not used by anything but it might come in useful again
587 Boxx* toAdd = (Boxx*)m->parameter;