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;
75 Log::getInstance()->log("BoxStack", Log::DEBUG, "add called");
77 pthread_mutex_lock(&boxLock);
79 WaitForSingleObject(boxLock, INFINITE);
81 Log::getInstance()->log("BoxStack", Log::DEBUG, "Locked for add");
85 Log::getInstance()->log("BoxStack", Log::ERR, "More than 16 boxes! Unlocked for add");
87 pthread_mutex_unlock(&boxLock);
89 ReleaseMutex(boxLock);
93 boxes[numBoxes++] = v;
96 pthread_mutex_unlock(&boxLock);
98 ReleaseMutex(boxLock);
101 Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for add");
106 // ---------------------------------------------------- REMOVE CODE
108 int BoxStack::remove(Boxx* toDelete)
110 if (!initted) return 0;
113 pthread_mutex_lock(&boxLock);
115 WaitForSingleObject(boxLock, INFINITE);
117 Log::getInstance()->log("BoxStack", Log::DEBUG, "Locked for remove");
122 pthread_mutex_unlock(&boxLock);
124 ReleaseMutex(boxLock);
126 Log::getInstance()->log("BoxStack", Log::ERR, "Unlocked for remove numBoxes == 0");
130 // Log::getInstance()->log("BoxStack", Log::DEBUG, "entering remove, numBoxes=%i", numBoxes);
134 if (toDelete == NULL)
136 toDelete = boxes[numBoxes-1];
141 // to be deleted box is more likely to be at the top
142 for (i = numBoxes-1; i >= 0; i--)
144 // Log::getInstance()->log("BoxStack", Log::DEBUG, "todel: %p, i=%i, boxes[i]=%p", toDelete, i, boxes[i]);
145 if (boxes[i] == toDelete) break;
150 // not a Box we have!
153 pthread_mutex_unlock(&boxLock);
155 ReleaseMutex(boxLock);
157 Log::getInstance()->log("BoxStack", Log::ERR, "Unlocked for remove - no boxx deleted");
163 pthread_mutex_unlock(&boxLock);
165 ReleaseMutex(boxLock);
168 toDelete->preDelete();
171 pthread_mutex_lock(&boxLock);
173 WaitForSingleObject(boxLock, INFINITE);
176 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Starting deleteBox");
178 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Done deleteBox");
180 // Shift the boxes on top down one
182 for(int j = i; j < numBoxes; j++) boxes[j] = boxes[j+1];
184 // If there is only the wallpaper left signal command
187 Message* m = new Message();
188 m->to = Command::getInstance();
189 m->message = Message::LAST_VIEW_CLOSE;
190 Command::getInstance()->postMessageNoLock(m);
194 pthread_mutex_unlock(&boxLock);
196 ReleaseMutex(boxLock);
198 Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for remove");
201 //AVO: do this delete outside the lock to allow for recursive calls within the destructor
202 // as this box is not in the stack any more, there is no chance for a second delete
203 Log::getInstance()->log("BoxStack", Log::DEBUG, "remove: going to delete boxx %p, num %d", toDelete, numBoxes);
209 /////////////////////////////////////////////////////////////////////////////
211 /////////////////////////////////////////////////////////////////////////////
213 void BoxStack::deleteBox(int z)
215 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Delete box %i of %i", z, numBoxes);
217 boxSplit(boxes[z]->area, z + 1, numBoxes, 1, rl);
220 repaintRevealed(z, rl.front());
225 void BoxStack::redrawAllBoxes()
228 pthread_mutex_lock(&boxLock);
230 WaitForSingleObject(boxLock, INFINITE);
233 for (int z = 0; z < numBoxes; z++)
240 pthread_mutex_unlock(&boxLock);
242 ReleaseMutex(boxLock);
244 update(NULL,NULL); // should blt all
247 void BoxStack::update(Boxx* toUpdate, Region* regionToUpdate)
249 Log::getInstance()->log("BoxStack", Log::DEBUG, "Update called");
250 if (!initted) return; // it is allowed to call this before init
252 pthread_mutex_lock(&boxLock);
254 WaitForSingleObject(boxLock, INFINITE);
256 Log::getInstance()->log("BoxStack", Log::DEBUG, "Locked for update");
258 // Get the z index of the box
262 for (z = 0; z < numBoxes; z++)
264 if (boxes[z] == toUpdate) break;
269 // not a Box we have!
271 pthread_mutex_unlock(&boxLock);
273 ReleaseMutex(boxLock);
275 Log::getInstance()->log("BoxStack", Log::ERR, "Unlocked for update! box not inside boxstack");
286 pthread_mutex_unlock(&boxLock);
288 ReleaseMutex(boxLock);
290 Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for update, no box present");
294 // get the region for the whole box, could be less than that
295 // for smaller updates
297 Region r = toUpdate->area;
301 // Can be null if the whole box should be updated
302 // If this is given the numbers are relative to the size of the box, not the screen
304 r.x += regionToUpdate->x;
305 r.y += regionToUpdate->y;
306 r.w = regionToUpdate->w;
307 r.h = regionToUpdate->h;
313 boxSplit(r, z+1, numBoxes, 1, rl);
323 pthread_mutex_unlock(&boxLock);
325 ReleaseMutex(boxLock);
327 Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for update");
330 void BoxStack::repaintRevealed(int x, Region r)
333 boxSplit(r, x - 1, -1, -1, rl);
339 boxes[r2.z]->blt(r2);
344 void BoxStack::boxSplit(Region r, int start, int end, int direction, RegionList& rl)
346 // 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);
348 for(int z = start; z != end; z += direction)
350 if (r.overlappedBy(boxes[z]->area))
352 // printf("Z=%i S=%i E=%i D=%i: %i overlaps\n", z, start, end, direction, z);
357 if (boxes[z]->area.y > r.y)
359 // 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);
360 top = boxes[z]->area.y;
365 newR.h = boxes[z]->area.y - r.y;
366 boxSplit(newR, z + direction, end, direction, rl);
372 newR2.y = boxes[z]->area.y;
374 newR2.h = r.h - newR.h;
375 boxSplit(newR2, z, end, -1, rl);
380 if (boxes[z]->area.y2() < r.y2())
382 // 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);
383 btm = boxes[z]->area.y2();
386 newR.y = boxes[z]->area.y2() + 1;
388 newR.h = r.y2() - newR.y + 1;
389 boxSplit(newR, z + direction, end, direction, rl);
397 newR2.h = r.h - newR.h;
398 boxSplit(newR2, z, end, -1, rl);
403 if (boxes[z]->area.x > r.x)
405 // 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);
409 newR.w = boxes[z]->area.x - r.x;
410 newR.h = btm - top + 1;
411 boxSplit(newR, z + direction, end, direction, rl);
416 newR2.x = r.x + newR.w;
418 newR2.w = r.w - newR.w;
420 boxSplit(newR2, z, end, -1, rl);
425 if (boxes[z]->area.x2() < r.x2())
427 // 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);
429 newR.x = boxes[z]->area.x2() + 1;
431 newR.w = r.x2() - newR.x + 1;
432 newR.h = btm - top + 1;
433 boxSplit(newR, z + direction, end, direction, rl);
440 newR2.w = r.w - newR.w;
442 boxSplit(newR2, z, end, -1, rl);
449 // we are going down the stack
450 // r is underlapped by boxes[z]
451 // but we have not split
452 // Therefore this region under test is
453 // completely covering boxes[z]
455 // don't go any further down, generate a region and quit
457 // printf("Repaint region: %i %i %i %i\n", r.x, r.y, r.w, r.h);
462 // printf("Returning from Z=%i\n", z);
467 // printf("Z=%i S=%i E=%i D=%i: %i does not overlap\n", z, start, end, direction, z);
471 // if direction = 1 then we have come to a region that is
472 // entirely clear of higher boxes and needs to be redrawn
474 // if direction = -1 then we have come to a region that is on
475 // the very bottom with nothing below it to repaint.
476 // do nothing. stale window data will be left on screen?
484 /////////////////////////////////////////////////////////////////////////////
486 /////////////////////////////////////////////////////////////////////////////
488 // ---------------------------------------------------- END OF REMOVE CODE
491 void BoxStack::removeAll()
493 // 1.. Don't delete wallpaper. No point.
495 // Need locking on this one??
497 // This is pretty silly now that preDelete needs mutex unlocked
504 pthread_mutex_lock(&boxLock);
506 WaitForSingleObject(boxLock, INFINITE);
512 pthread_mutex_unlock(&boxLock);
514 ReleaseMutex(boxLock);
519 toDel = boxes[numBoxes - 1];
522 pthread_mutex_unlock(&boxLock);
524 ReleaseMutex(boxLock);
530 pthread_mutex_lock(&boxLock);
532 WaitForSingleObject(boxLock, INFINITE);
535 // If boxes[numBoxes - 1] isn't toDel then there's a problem
536 if (boxes[numBoxes - 1] == toDel)
542 Log::getInstance()->log("BoxStack", Log::ERR, "Can this actually happen? Why?");
547 pthread_mutex_unlock(&boxLock);
549 ReleaseMutex(boxLock);
552 //AVO: do the delete outside the lock to allow for recursive deletes
553 Log::getInstance()->log("BoxStack", Log::DEBUG, "going to delete boxx %p, num=%d", toDel, numBoxes);
554 if (toDel) delete toDel;
558 int BoxStack::handleCommand(int command)
564 if (command != Remote::NA_NONE)
566 // handle command return values
567 // 0 - drop through to next box
568 // 1 - dont drop to next box, but not handled
569 // 2 - handled - stop command here
570 // 4 - handled - delete this box
572 for (i=numBoxes-1; i>=0; i--)
574 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Giving command to i=%i", i);
575 retVal = boxes[i]->handleCommand(command);
578 // not handled but don't give to any more boxes
588 else if (retVal == 4)
590 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Return 4: i=%i, boxes[i]=%p", i, boxes[i]);
599 // fake the return code
606 void BoxStack::processMessage(Message* m)
610 for (int i = numBoxes-1; i >= 0; i--)
612 if (boxes[i] == m->to)
614 Log::getInstance()->log("BoxStack", Log::DEBUG, "sending message from box %p to box %p %lu", m->from, m->to, m->message);
615 boxes[i]->processMessage(m);
622 /* Handle mouse events*/
623 // They come in with m->to = NULL? and just need to be delivered to top box?
624 if ((numBoxes > 1) && ((m->message == Message::MOUSE_MOVE) || (m->message == Message::MOUSE_LBDOWN)
625 || (m->message == Message::MOUSE_ANDROID_SCROLL)))
627 boxes[numBoxes-1]->processMessage(m);
631 Log::getInstance()->log("BoxStack", Log::DEBUG, "it's for meeee!");
635 case Message::CLOSE_ME:
637 remove((Boxx*)m->from);
640 case Message::ADD_VIEW: // currently not used by anything but it might come in useful again
642 Boxx* toAdd = (Boxx*)m->parameter;