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