]> git.vomp.tv Git - vompclient.git/blob - boxstack.cc
*** empty log message ***
[vompclient.git] / boxstack.cc
1 /*
2     Copyright 2004-2005 Chris Tallon
3
4     This file is part of VOMP.
5
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.
10
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.
15
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.
19 */
20
21 #include "boxstack.h"
22
23 #include "command.h"
24 #include "remote.h"
25 #include "log.h"
26
27 BoxStack* BoxStack::instance = NULL;
28
29 BoxStack::BoxStack()
30 {
31   if (instance) return;
32   instance = this;
33   initted = 0;
34   numBoxes = 0;
35 }
36
37 BoxStack::~BoxStack()
38 {
39   instance = NULL;
40 }
41
42 BoxStack* BoxStack::getInstance()
43 {
44   return instance;
45 }
46
47 int BoxStack::init()
48 {
49   if (initted) return 0;
50   initted = 1;
51   
52 #ifndef WIN32
53   pthread_mutex_init(&boxLock, NULL);
54 #else
55   boxLock = CreateMutex(NULL,FALSE,NULL);
56 #endif
57
58   return 1;
59 }
60
61 int BoxStack::shutdown()
62 {
63   if (!initted) return 0;
64
65   // FIXME don't think this can work properly, removeAll leaves the wallpaper there!
66   removeAll();
67
68   initted = 0;
69   return 1;
70 }
71
72 int BoxStack::add(Boxx* v)
73 {
74   if (!initted) return 0;
75   Log::getInstance()->log("BoxStack", Log::DEBUG, "add called"); 
76 #ifndef WIN32
77   pthread_mutex_lock(&boxLock);
78 #else
79   WaitForSingleObject(boxLock, INFINITE);
80 #endif
81   Log::getInstance()->log("BoxStack", Log::DEBUG, "Locked for add");  
82   
83   if (numBoxes == 16)
84   { //Error
85     Log::getInstance()->log("BoxStack", Log::ERR, "More than 16 boxes! Unlocked for add"); 
86 #ifndef WIN32
87     pthread_mutex_unlock(&boxLock);
88 #else
89     ReleaseMutex(boxLock);
90 #endif
91     return 0;
92   }
93   boxes[numBoxes++] = v;
94
95 #ifndef WIN32
96   pthread_mutex_unlock(&boxLock);
97 #else
98   ReleaseMutex(boxLock);
99 #endif
100
101   Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for add");
102   
103   return 1;
104 }
105
106 // ---------------------------------------------------- REMOVE CODE
107
108 int BoxStack::remove(Boxx* toDelete)
109 {
110   if (!initted) return 0;
111
112   #ifndef WIN32
113   pthread_mutex_lock(&boxLock);
114   #else
115   WaitForSingleObject(boxLock, INFINITE);
116   #endif
117   Log::getInstance()->log("BoxStack", Log::DEBUG, "Locked for remove");  
118   
119   if (numBoxes == 0)
120   {
121     #ifndef WIN32
122     pthread_mutex_unlock(&boxLock);
123     #else
124     ReleaseMutex(boxLock);
125     #endif
126     Log::getInstance()->log("BoxStack", Log::ERR, "Unlocked for remove numBoxes == 0");  
127     return 0;
128   }
129
130 //  Log::getInstance()->log("BoxStack", Log::DEBUG, "entering remove, numBoxes=%i", numBoxes);
131
132   int i;
133
134   if (toDelete == NULL)
135   {
136     toDelete = boxes[numBoxes-1];
137     i = numBoxes - 1;
138   }
139   else
140   {
141     // to be deleted box is more likely to be at the top
142     for (i = numBoxes-1; i >= 0; i--)
143     {
144 //      Log::getInstance()->log("BoxStack", Log::DEBUG, "todel: %p, i=%i, boxes[i]=%p", toDelete, i, boxes[i]);
145       if (boxes[i] == toDelete) break;
146     }
147
148     if (i == -1)
149     {
150       // not a Box we have!
151       // FIXME
152       #ifndef WIN32
153       pthread_mutex_unlock(&boxLock);
154       #else
155       ReleaseMutex(boxLock);
156       #endif
157       Log::getInstance()->log("BoxStack", Log::ERR, "Unlocked for remove - no boxx deleted");  
158       return 0;
159     }
160   }
161
162 #ifndef WIN32
163   pthread_mutex_unlock(&boxLock);
164 #else
165   ReleaseMutex(boxLock);
166 #endif
167
168 toDelete->preDelete();
169
170 #ifndef WIN32
171   pthread_mutex_lock(&boxLock);
172 #else
173   WaitForSingleObject(boxLock, INFINITE);
174 #endif
175
176 //  Log::getInstance()->log("BoxStack", Log::DEBUG, "Starting deleteBox");
177   deleteBox(i);
178 //  Log::getInstance()->log("BoxStack", Log::DEBUG, "Done deleteBox");
179
180   // Shift the boxes on top down one
181   --numBoxes;
182   for(int j = i; j < numBoxes; j++) boxes[j] = boxes[j+1];
183
184   // If there is only the wallpaper left signal command
185   if (numBoxes == 1)
186   {
187     Message* m = new Message();
188     m->to = Command::getInstance();
189     m->message = Message::LAST_VIEW_CLOSE;
190     Command::getInstance()->postMessageNoLock(m);
191   }
192
193 #ifndef WIN32
194   pthread_mutex_unlock(&boxLock);
195 #else
196   ReleaseMutex(boxLock);
197 #endif
198   Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for remove");  
199  
200   // Delete the box
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);  
204   delete toDelete;
205
206   return 1;
207 }
208
209 /////////////////////////////////////////////////////////////////////////////
210 // NEW STUFF
211 /////////////////////////////////////////////////////////////////////////////
212
213 void BoxStack::deleteBox(int z)
214 {
215 //  Log::getInstance()->log("BoxStack", Log::DEBUG, "Delete box %i of %i", z, numBoxes);
216   RegionList rl;
217   boxSplit(boxes[z]->area, z + 1, numBoxes, 1, rl);
218   while(!rl.empty())
219   {
220     repaintRevealed(z, rl.front());
221     rl.pop_front();
222   }
223 }
224
225 void BoxStack::update(Boxx* toUpdate, Region* regionToUpdate)
226 {
227   Log::getInstance()->log("BoxStack", Log::DEBUG, "Update called");
228
229 #ifndef WIN32
230   pthread_mutex_lock(&boxLock);
231 #else
232   WaitForSingleObject(boxLock, INFINITE);
233 #endif
234   Log::getInstance()->log("BoxStack", Log::DEBUG, "Locked for update");
235
236   // Get the z index of the box
237   int z;
238   for (z = 0; z < numBoxes; z++)
239   {
240     if (boxes[z] == toUpdate) break;
241   }
242
243   if (z == numBoxes)
244   {
245     // not a Box we have!
246 #ifndef WIN32
247     pthread_mutex_unlock(&boxLock);
248 #else
249     ReleaseMutex(boxLock);
250 #endif 
251     Log::getInstance()->log("BoxStack", Log::ERR, "Unlocked for update! box not inside boxstack");
252     return;
253   }
254
255   // get the region for the whole box, could be less than that
256   // for smaller updates
257
258   Region r = toUpdate->area;
259
260   if (regionToUpdate)
261   {
262     // Can be null if the whole box should be updated
263     // If this is given the numbers are relative to the size of the box, not the screen
264
265     r.x += regionToUpdate->x;
266     r.y += regionToUpdate->y;
267     r.w = regionToUpdate->w;
268     r.h = regionToUpdate->h;
269   }
270
271   RegionList rl;
272
273   Region r2;
274   boxSplit(r, z+1, numBoxes, 1, rl);
275   while(!rl.empty())
276   {
277     r2 = rl.front();
278     r2.z = z;
279     boxes[z]->blt(r2);
280     rl.pop_front();
281   }
282   
283 #ifndef WIN32
284   pthread_mutex_unlock(&boxLock);
285 #else
286   ReleaseMutex(boxLock);
287 #endif  
288   Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for update");
289 }
290
291 void BoxStack::repaintRevealed(int x, Region r)
292 {
293   RegionList rl;
294   boxSplit(r, x - 1, -1, -1, rl);
295
296   Region r2;
297   while(!rl.empty())
298   {
299     r2 = rl.front();
300     boxes[r2.z]->blt(r2);
301     rl.pop_front();
302   }
303 }
304
305 void BoxStack::boxSplit(Region r, int start, int end, int direction, RegionList& rl)
306 {
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);
308
309   for(int z = start; z != end; z += direction)
310   {
311     if (r.overlappedBy(boxes[z]->area))
312     {
313 //      printf("Z=%i S=%i E=%i D=%i: %i overlaps\n", z, start, end, direction, z);
314
315       int top = r.y;
316       int btm = r.y2();
317
318       if (boxes[z]->area.y > r.y)
319       {
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;
322         Region newR;
323         newR.x = r.x;
324         newR.y = r.y;
325         newR.w = r.w;
326         newR.h = boxes[z]->area.y - r.y;
327         boxSplit(newR, z + direction, end, direction, rl);
328
329         if (direction == -1)
330         {
331           Region newR2;
332           newR2.x = r.x;
333           newR2.y = boxes[z]->area.y;
334           newR2.w = r.w;
335           newR2.h = r.h - newR.h;
336           boxSplit(newR2, z, end, -1, rl);
337           return;
338         }
339       }
340
341       if (boxes[z]->area.y2() < r.y2())
342       {
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();
345         Region newR;
346         newR.x = r.x;
347         newR.y = boxes[z]->area.y2() + 1;
348         newR.w = r.w;
349         newR.h = r.y2() - newR.y + 1;
350         boxSplit(newR, z + direction, end, direction, rl);
351
352         if (direction == -1)
353         {
354           Region newR2;
355           newR2.x = r.x;
356           newR2.y = r.y;
357           newR2.w = r.w;
358           newR2.h = r.h - newR.h;
359           boxSplit(newR2, z, end, -1, rl);
360           return;
361         }
362       }
363
364       if (boxes[z]->area.x > r.x)
365       {
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);
367         Region newR;
368         newR.x = r.x;
369         newR.y = top;
370         newR.w = boxes[z]->area.x - r.x;
371         newR.h = btm - top + 1;
372         boxSplit(newR, z + direction, end, direction, rl);
373
374         if (direction == -1)
375         {
376           Region newR2;
377           newR2.x = r.x + newR.w;
378           newR2.y = r.y;
379           newR2.w = r.w - newR.w;
380           newR2.h = r.h;
381           boxSplit(newR2, z, end, -1, rl);
382           return;
383         }
384       }
385
386       if (boxes[z]->area.x2() < r.x2())
387       {
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);
389         Region newR;
390         newR.x = boxes[z]->area.x2() + 1;
391         newR.y = top;
392         newR.w = r.x2() - newR.x + 1;
393         newR.h = btm - top + 1;
394         boxSplit(newR, z + direction, end, direction, rl);
395
396         if (direction == -1)
397         {
398           Region newR2;
399           newR2.x = r.x;
400           newR2.y = r.y;
401           newR2.w = r.w - newR.w;
402           newR2.h = r.h;
403           boxSplit(newR2, z, end, -1, rl);
404           return;
405         }
406       }
407
408       if (direction == -1)
409       {
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]
415
416         // don't go any further down, generate a region and quit
417
418 //        printf("Repaint region: %i %i %i %i\n", r.x, r.y, r.w, r.h);
419         r.z = z;
420         rl.push_front(r);
421       }
422
423 //      printf("Returning from Z=%i\n", z);
424       return;
425     }
426     else
427     {
428 //      printf("Z=%i S=%i E=%i D=%i: %i does not overlap\n", z, start, end, direction, z);
429     }
430   }
431
432   // if direction = 1 then we have come to a region that is
433   // entirely clear of higher boxes and needs to be redrawn
434
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?
438
439   if (direction == 1)
440   {
441     rl.push_front(r);
442   }
443 }
444
445 /////////////////////////////////////////////////////////////////////////////
446 // END NEW STUFF
447 /////////////////////////////////////////////////////////////////////////////
448
449 // ---------------------------------------------------- END OF REMOVE CODE
450
451
452 void BoxStack::removeAll()
453 {
454   // 1.. Don't delete wallpaper. No point.
455
456   // Need locking on this one??
457   
458   // This is pretty silly now that preDelete needs mutex unlocked
459   
460   Boxx* toDel = NULL;
461   
462   while(numBoxes > 1)
463   {
464     #ifndef WIN32
465       pthread_mutex_lock(&boxLock);
466     #else
467       WaitForSingleObject(boxLock, INFINITE);
468     #endif  
469
470     if (numBoxes == 1)
471     {
472       #ifndef WIN32
473         pthread_mutex_unlock(&boxLock);
474       #else
475         ReleaseMutex(boxLock);
476       #endif
477       break;
478     }    
479   
480     toDel = boxes[numBoxes - 1];
481
482     #ifndef WIN32
483       pthread_mutex_unlock(&boxLock);
484     #else
485       ReleaseMutex(boxLock);
486     #endif
487
488     toDel->preDelete();
489     
490     #ifndef WIN32
491       pthread_mutex_lock(&boxLock);
492     #else
493       WaitForSingleObject(boxLock, INFINITE);
494     #endif  
495
496     // If boxes[numBoxes - 1] isn't toDel then there's a problem
497     if (boxes[numBoxes - 1] == toDel)
498     {
499       --numBoxes;
500     }
501     else
502     {
503       Log::getInstance()->log("BoxStack", Log::ERR, "Can this actually happen? Why?");
504       toDel = NULL;
505     }
506
507     #ifndef WIN32
508       pthread_mutex_unlock(&boxLock);
509     #else
510       ReleaseMutex(boxLock);
511     #endif
512
513     //AVO: do the delete outside the lock to allow for recursive deletes
514     Log::getInstance()->log("BoxStack", Log::DEBUG, "going to delete boxx %p, num=%d", toDel, numBoxes);
515     if (toDel) delete toDel;
516   }
517 }
518
519 int BoxStack::handleCommand(int command)
520 {
521   int retVal;
522   int retVal2 = 0;
523   int i;
524
525   if (command != Remote::NA_NONE)
526   {
527     // handle command return values
528     // 0 - drop through to next box
529     // 1 - dont drop to next box, but not handled
530     // 2 - handled - stop command here
531     // 4 - handled - delete this box
532
533     for (i=numBoxes-1; i>=0; i--)
534     {
535 //      Log::getInstance()->log("BoxStack", Log::DEBUG, "Giving command to i=%i", i);
536       retVal = boxes[i]->handleCommand(command);
537       if (retVal == 1)
538       {
539         // not handled but don't give to any more boxes
540         return 0;
541       }
542
543       if (retVal == 2)
544       {
545         // command handled
546         retVal2 = 1;
547         break;
548       }
549       else if (retVal == 4)
550       {
551 //        Log::getInstance()->log("BoxStack", Log::DEBUG, "Return 4: i=%i, boxes[i]=%p", i, boxes[i]);
552         remove(boxes[i]);
553         retVal2 = 1;
554         break;
555       }
556     }
557   }
558   else
559   {
560     // fake the return code
561     retVal2 = 2;
562   }
563
564   return retVal2;
565 }
566
567 void BoxStack::processMessage(Message* m)
568 {
569   if (m->to != this)
570   {
571     for (int i = numBoxes-1; i >= 0; i--)
572     {
573       if (boxes[i] == m->to)
574       {
575         Log::getInstance()->log("BoxStack", Log::DEBUG, "sending message from box %p to box %p %lu", m->from, m->to, m->message);
576         boxes[i]->processMessage(m);
577         return;
578       }
579     }
580     return;
581   }
582
583   /* Handle mouse events*/
584   // They come in with m->to = NULL? and just need to be delivered to top box?
585   if ((numBoxes > 1) && ((m->message == Message::MOUSE_MOVE) || (m->message == Message::MOUSE_LBDOWN)))
586   {
587     boxes[numBoxes-1]->processMessage(m);
588     return;
589   }
590
591   Log::getInstance()->log("BoxStack", Log::DEBUG, "it's for meeee!");
592
593   switch(m->message)
594   {
595     case Message::CLOSE_ME:
596     {
597       remove((Boxx*)m->from);
598       break;
599     }
600     case Message::ADD_VIEW: // currently not used by anything but it might come in useful again
601     {
602       Boxx* toAdd = (Boxx*)m->parameter;
603       add(toAdd);
604       toAdd->draw();
605       update(toAdd);
606       break;
607     }
608   }
609 }