]> git.vomp.tv Git - vompclient.git/blob - boxstack.cc
Teletext
[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     MessageBox(0,"the evil error","error",0);
250     ReleaseMutex(boxLock);
251 #endif 
252     Log::getInstance()->log("BoxStack", Log::ERR, "Unlocked for update! box not inside boxstack");
253     return;
254   }
255
256   // get the region for the whole box, could be less than that
257   // for smaller updates
258
259   Region r = toUpdate->area;
260
261   if (regionToUpdate)
262   {
263     // Can be null if the whole box should be updated
264     // If this is given the numbers are relative to the size of the box, not the screen
265
266     r.x += regionToUpdate->x;
267     r.y += regionToUpdate->y;
268     r.w = regionToUpdate->w;
269     r.h = regionToUpdate->h;
270   }
271
272   RegionList rl;
273
274   Region r2;
275   boxSplit(r, z+1, numBoxes, 1, rl);
276   while(!rl.empty())
277   {
278     r2 = rl.front();
279     r2.z = z;
280     boxes[z]->blt(r2);
281     rl.pop_front();
282   }
283   
284 #ifndef WIN32
285   pthread_mutex_unlock(&boxLock);
286 #else
287   ReleaseMutex(boxLock);
288 #endif  
289   Log::getInstance()->log("BoxStack", Log::DEBUG, "Unlocked for update");
290 }
291
292 void BoxStack::repaintRevealed(int x, Region r)
293 {
294   RegionList rl;
295   boxSplit(r, x - 1, -1, -1, rl);
296
297   Region r2;
298   while(!rl.empty())
299   {
300     r2 = rl.front();
301     boxes[r2.z]->blt(r2);
302     rl.pop_front();
303   }
304 }
305
306 void BoxStack::boxSplit(Region r, int start, int end, int direction, RegionList& rl)
307 {
308 //  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
310   for(int z = start; z != end; z += direction)
311   {
312     if (r.overlappedBy(boxes[z]->area))
313     {
314 //      printf("Z=%i S=%i E=%i D=%i: %i overlaps\n", z, start, end, direction, z);
315
316       int top = r.y;
317       int btm = r.y2();
318
319       if (boxes[z]->area.y > r.y)
320       {
321 //        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);
322         top = boxes[z]->area.y;
323         Region newR;
324         newR.x = r.x;
325         newR.y = r.y;
326         newR.w = r.w;
327         newR.h = boxes[z]->area.y - r.y;
328         boxSplit(newR, z + direction, end, direction, rl);
329
330         if (direction == -1)
331         {
332           Region newR2;
333           newR2.x = r.x;
334           newR2.y = boxes[z]->area.y;
335           newR2.w = r.w;
336           newR2.h = r.h - newR.h;
337           boxSplit(newR2, z, end, -1, rl);
338           return;
339         }
340       }
341
342       if (boxes[z]->area.y2() < r.y2())
343       {
344 //        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);
345         btm = boxes[z]->area.y2();
346         Region newR;
347         newR.x = r.x;
348         newR.y = boxes[z]->area.y2() + 1;
349         newR.w = r.w;
350         newR.h = r.y2() - newR.y + 1;
351         boxSplit(newR, z + direction, end, direction, rl);
352
353         if (direction == -1)
354         {
355           Region newR2;
356           newR2.x = r.x;
357           newR2.y = r.y;
358           newR2.w = r.w;
359           newR2.h = r.h - newR.h;
360           boxSplit(newR2, z, end, -1, rl);
361           return;
362         }
363       }
364
365       if (boxes[z]->area.x > r.x)
366       {
367 //        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);
368         Region newR;
369         newR.x = r.x;
370         newR.y = top;
371         newR.w = boxes[z]->area.x - r.x;
372         newR.h = btm - top + 1;
373         boxSplit(newR, z + direction, end, direction, rl);
374
375         if (direction == -1)
376         {
377           Region newR2;
378           newR2.x = r.x + newR.w;
379           newR2.y = r.y;
380           newR2.w = r.w - newR.w;
381           newR2.h = r.h;
382           boxSplit(newR2, z, end, -1, rl);
383           return;
384         }
385       }
386
387       if (boxes[z]->area.x2() < r.x2())
388       {
389 //        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         Region newR;
391         newR.x = boxes[z]->area.x2() + 1;
392         newR.y = top;
393         newR.w = r.x2() - newR.x + 1;
394         newR.h = btm - top + 1;
395         boxSplit(newR, z + direction, end, direction, rl);
396
397         if (direction == -1)
398         {
399           Region newR2;
400           newR2.x = r.x;
401           newR2.y = r.y;
402           newR2.w = r.w - newR.w;
403           newR2.h = r.h;
404           boxSplit(newR2, z, end, -1, rl);
405           return;
406         }
407       }
408
409       if (direction == -1)
410       {
411         // we are going down the stack
412         // r is underlapped by boxes[z]
413         // but we have not split
414         // Therefore this region under test is
415         // completely covering boxes[z]
416
417         // don't go any further down, generate a region and quit
418
419 //        printf("Repaint region: %i %i %i %i\n", r.x, r.y, r.w, r.h);
420         r.z = z;
421         rl.push_front(r);
422       }
423
424 //      printf("Returning from Z=%i\n", z);
425       return;
426     }
427     else
428     {
429 //      printf("Z=%i S=%i E=%i D=%i: %i does not overlap\n", z, start, end, direction, z);
430     }
431   }
432
433   // if direction = 1 then we have come to a region that is
434   // entirely clear of higher boxes and needs to be redrawn
435
436   // if direction = -1 then we have come to a region that is on
437   // the very bottom with nothing below it to repaint.
438   // do nothing. stale window data will be left on screen?
439
440   if (direction == 1)
441   {
442     rl.push_front(r);
443   }
444 }
445
446 /////////////////////////////////////////////////////////////////////////////
447 // END NEW STUFF
448 /////////////////////////////////////////////////////////////////////////////
449
450 // ---------------------------------------------------- END OF REMOVE CODE
451
452
453 void BoxStack::removeAll()
454 {
455   // 1.. Don't delete wallpaper. No point.
456
457   // Need locking on this one??
458   
459   // This is pretty silly now that preDelete needs mutex unlocked
460   
461   Boxx* toDel = NULL;
462   
463   while(numBoxes > 1)
464   {
465     #ifndef WIN32
466       pthread_mutex_lock(&boxLock);
467     #else
468       WaitForSingleObject(boxLock, INFINITE);
469     #endif  
470
471     if (numBoxes == 1)
472     {
473       #ifndef WIN32
474         pthread_mutex_unlock(&boxLock);
475       #else
476         ReleaseMutex(boxLock);
477       #endif
478       break;
479     }    
480   
481     toDel = boxes[numBoxes - 1];
482
483     #ifndef WIN32
484       pthread_mutex_unlock(&boxLock);
485     #else
486       ReleaseMutex(boxLock);
487     #endif
488
489     toDel->preDelete();
490     
491     #ifndef WIN32
492       pthread_mutex_lock(&boxLock);
493     #else
494       WaitForSingleObject(boxLock, INFINITE);
495     #endif  
496
497     // If boxes[numBoxes - 1] isn't toDel then there's a problem
498     if (boxes[numBoxes - 1] == toDel)
499     {
500       --numBoxes;
501     }
502     else
503     {
504       Log::getInstance()->log("BoxStack", Log::ERR, "Can this actually happen? Why?");
505       toDel = NULL;
506     }
507
508     #ifndef WIN32
509       pthread_mutex_unlock(&boxLock);
510     #else
511       ReleaseMutex(boxLock);
512     #endif
513
514     //AVO: do the delete outside the lock to allow for recursive deletes
515     Log::getInstance()->log("BoxStack", Log::DEBUG, "going to delete boxx %p, num=%d", toDel, numBoxes);
516     if (toDel) delete toDel;
517   }
518 }
519
520 int BoxStack::handleCommand(int command)
521 {
522   int retVal;
523   int retVal2 = 0;
524   int i;
525
526   if (command != Remote::NA_NONE)
527   {
528     // handle command return values
529     // 0 - drop through to next box
530     // 1 - dont drop to next box, but not handled
531     // 2 - handled - stop command here
532     // 4 - handled - delete this box
533
534     for (i=numBoxes-1; i>=0; i--)
535     {
536 //      Log::getInstance()->log("BoxStack", Log::DEBUG, "Giving command to i=%i", i);
537       retVal = boxes[i]->handleCommand(command);
538       if (retVal == 1)
539       {
540         // not handled but don't give to any more boxes
541         return 0;
542       }
543
544       if (retVal == 2)
545       {
546         // command handled
547         retVal2 = 1;
548         break;
549       }
550       else if (retVal == 4)
551       {
552 //        Log::getInstance()->log("BoxStack", Log::DEBUG, "Return 4: i=%i, boxes[i]=%p", i, boxes[i]);
553         remove(boxes[i]);
554         retVal2 = 1;
555         break;
556       }
557     }
558   }
559   else
560   {
561     // fake the return code
562     retVal2 = 2;
563   }
564
565   return retVal2;
566 }
567
568 void BoxStack::processMessage(Message* m)
569 {
570   if (m->to != this)
571   {
572     for (int i = numBoxes-1; i >= 0; i--)
573     {
574       if (boxes[i] == m->to)
575       {
576         Log::getInstance()->log("BoxStack", Log::DEBUG, "sending message from box %p to box %p %lu", m->from, m->to, m->message);
577         boxes[i]->processMessage(m);
578         return;
579       }
580     }
581     return;
582   }
583
584   /* Handle mouse events*/
585   // They come in with m->to = NULL? and just need to be delivered to top box?
586   if ((numBoxes > 1) && ((m->message == Message::MOUSE_MOVE) || (m->message == Message::MOUSE_LBDOWN)))
587   {
588     boxes[numBoxes-1]->processMessage(m);
589     return;
590   }
591
592   Log::getInstance()->log("BoxStack", Log::DEBUG, "it's for meeee!");
593
594   switch(m->message)
595   {
596     case Message::CLOSE_ME:
597     {
598       remove((Boxx*)m->from);
599       break;
600     }
601     case Message::ADD_VIEW: // currently not used by anything but it might come in useful again
602     {
603       Boxx* toAdd = (Boxx*)m->parameter;
604       add(toAdd);
605       toAdd->draw();
606       update(toAdd);
607       break;
608     }
609   }
610 }