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);
82 if (numBoxes == 16) return 0;
83 boxes[numBoxes++] = v;
86 pthread_mutex_unlock(&boxLock);
88 ReleaseMutex(boxLock);
94 // ---------------------------------------------------- REMOVE CODE
96 int BoxStack::remove(Boxx* toDelete)
98 if (!initted) return 0;
101 pthread_mutex_lock(&boxLock);
103 WaitForSingleObject(boxLock, INFINITE);
106 if (numBoxes == 0) return 0;
108 // Log::getInstance()->log("BoxStack", Log::DEBUG, "entering remove, numBoxes=%i", numBoxes);
112 if (toDelete == NULL)
114 toDelete = boxes[numBoxes-1];
119 // to be deleted box is more likely to be at the top
120 for (i = numBoxes-1; i >= 0; i--)
122 // Log::getInstance()->log("BoxStack", Log::DEBUG, "todel: %p, i=%i, boxes[i]=%p", toDelete, i, boxes[i]);
123 if (boxes[i] == toDelete) break;
128 // not a Box we have!
133 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Starting deleteBox");
135 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Done deleteBox");
137 // Shift the boxes on top down one
139 for(int j = i; j < numBoxes; j++) boxes[j] = boxes[j+1];
144 // If there is only the wallpaper left signal command
147 Message* m = new Message();
148 m->to = Command::getInstance();
149 m->message = Message::LAST_VIEW_CLOSE;
150 Command::getInstance()->postMessageNoLock(m);
154 pthread_mutex_unlock(&boxLock);
156 ReleaseMutex(boxLock);
162 /////////////////////////////////////////////////////////////////////////////
164 /////////////////////////////////////////////////////////////////////////////
166 void BoxStack::deleteBox(int z)
168 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Delete box %i of %i", z, numBoxes);
170 boxSplit(boxes[z]->area, z + 1, numBoxes, 1, rl);
173 repaintRevealed(z, rl.front());
178 void BoxStack::update(Boxx* toUpdate, Region* regionToUpdate)
180 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Update called");
181 // Get the z index of the box
184 pthread_mutex_lock(&boxLock);
186 WaitForSingleObject(boxLock, INFINITE);
188 Log::getInstance()->log("BoxStack", Log::DEBUG, "Locked for update");
191 for (z = 0; z < numBoxes; z++)
193 if (boxes[z] == toUpdate) break;
198 // not a Box we have!
202 // get the region for the whole box, could be less than that
203 // for smaller updates
205 Region r = toUpdate->area;
209 // Can be null if the whole box should be updated
210 // If this is given the numbers are relative to the size of the box, not the screen
212 r.x += regionToUpdate->x;
213 r.y += regionToUpdate->y;
214 r.w = regionToUpdate->w;
215 r.h = regionToUpdate->h;
221 boxSplit(r, z+1, numBoxes, 1, rl);
230 Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocking");
232 pthread_mutex_unlock(&boxLock);
234 ReleaseMutex(boxLock);
238 void BoxStack::repaintRevealed(int x, Region r)
241 boxSplit(r, x - 1, -1, -1, rl);
247 boxes[r2.z]->blt(r2);
252 void BoxStack::boxSplit(Region r, int start, int end, int direction, RegionList& rl)
254 // 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);
256 for(int z = start; z != end; z += direction)
258 if (r.overlappedBy(boxes[z]->area))
260 // printf("Z=%i S=%i E=%i D=%i: %i overlaps\n", z, start, end, direction, z);
265 if (boxes[z]->area.y > r.y)
267 // 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);
268 top = boxes[z]->area.y;
273 newR.h = boxes[z]->area.y - r.y;
274 boxSplit(newR, z + direction, end, direction, rl);
280 newR2.y = boxes[z]->area.y;
282 newR2.h = r.h - newR.h;
283 boxSplit(newR2, z, end, -1, rl);
288 if (boxes[z]->area.y2() < r.y2())
290 // 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);
291 btm = boxes[z]->area.y2();
294 newR.y = boxes[z]->area.y2() + 1;
296 newR.h = r.y2() - newR.y + 1;
297 boxSplit(newR, z + direction, end, direction, rl);
305 newR2.h = r.h - newR.h;
306 boxSplit(newR2, z, end, -1, rl);
311 if (boxes[z]->area.x > r.x)
313 // 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);
317 newR.w = boxes[z]->area.x - r.x;
318 newR.h = btm - top + 1;
319 boxSplit(newR, z + direction, end, direction, rl);
324 newR2.x = r.x + newR.w;
326 newR2.w = r.w - newR.w;
328 boxSplit(newR2, z, end, -1, rl);
333 if (boxes[z]->area.x2() < r.x2())
335 // 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);
337 newR.x = boxes[z]->area.x2() + 1;
339 newR.w = r.x2() - newR.x + 1;
340 newR.h = btm - top + 1;
341 boxSplit(newR, z + direction, end, direction, rl);
348 newR2.w = r.w - newR.w;
350 boxSplit(newR2, z, end, -1, rl);
357 // we are going down the stack
358 // r is underlapped by boxes[z]
359 // but we have not split
360 // Therefore this region under test is
361 // completely covering boxes[z]
363 // don't go any further down, generate a region and quit
365 // printf("Repaint region: %i %i %i %i\n", r.x, r.y, r.w, r.h);
370 // printf("Returning from Z=%i\n", z);
375 // printf("Z=%i S=%i E=%i D=%i: %i does not overlap\n", z, start, end, direction, z);
379 // if direction = 1 then we have come to a region that is
380 // entirely clear of higher boxes and needs to be redrawn
382 // if direction = -1 then we have come to a region that is on
383 // the very bottom with nothing below it to repaint.
384 // do nothing. stale window data will be left on screen?
392 /////////////////////////////////////////////////////////////////////////////
394 /////////////////////////////////////////////////////////////////////////////
396 // ---------------------------------------------------- END OF REMOVE CODE
399 void BoxStack::removeAll()
401 // 1.. Don't delete wallpaper. No point.
403 // Need locking on this one??
406 pthread_mutex_lock(&boxLock);
408 WaitForSingleObject(boxLock, INFINITE);
412 for (; numBoxes > 1; --numBoxes)
414 delete boxes[numBoxes-1];
418 pthread_mutex_unlock(&boxLock);
420 ReleaseMutex(boxLock);
425 int BoxStack::handleCommand(int command)
431 if (command != Remote::NA_NONE)
433 // handle command return values
434 // 0 - drop through to next box
435 // 1 - dont drop to next box, but not handled
436 // 2 - handled - stop command here
437 // 4 - handled - delete this box
439 for (i=numBoxes-1; i>=0; i--)
441 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Giving command to i=%i", i);
442 retVal = boxes[i]->handleCommand(command);
445 // not handled but don't give to any more boxes
455 else if (retVal == 4)
457 // Log::getInstance()->log("BoxStack", Log::DEBUG, "Return 4: i=%i, boxes[i]=%p", i, boxes[i]);
466 // fake the return code
473 void BoxStack::processMessage(Message* m)
477 for (int i = numBoxes-1; i >= 0; i--)
479 if (boxes[i] == m->to)
481 Log::getInstance()->log("BoxStack", Log::DEBUG, "sending message from box %p to box %p %lu", m->from, m->to, m->message);
482 boxes[i]->processMessage(m);
489 /* Handle mouse events*/
490 // They come in with m->to = NULL? and just need to be delivered to top box?
491 if ((numBoxes > 1) && ((m->message == Message::MOUSE_MOVE) || (m->message == Message::MOUSE_LBDOWN)))
493 boxes[numBoxes-1]->processMessage(m);
497 Log::getInstance()->log("BoxStack", Log::DEBUG, "it's for meeee!");
501 case Message::CLOSE_ME:
503 remove((Boxx*)m->from);
506 case Message::ADD_VIEW: // currently not used by anything but it might come in useful again
508 Boxx* toAdd = (Boxx*)m->parameter;