include user headers before kernel header to avoid a type conflict, which resulted...
[enigma2.git] / lib / gdi / grc.cpp
1 #include <unistd.h>
2 #include <lib/gdi/grc.h>
3 #include <lib/gdi/font.h>
4 #include <lib/base/init.h>
5 #include <lib/base/init_num.h>
6
7 #ifndef SYNC_PAINT
8 void *gRC::thread_wrapper(void *ptr)
9 {
10         return ((gRC*)ptr)->thread();
11 }
12 #endif
13
14 gRC *gRC::instance=0;
15
16 gRC::gRC(): rp(0), wp(0)
17 #ifdef SYNC_PAINT
18 ,m_notify_pump(eApp, 0)
19 #else
20 ,m_notify_pump(eApp, 1)
21 #endif
22 {
23         ASSERT(!instance);
24         instance=this;
25         CONNECT(m_notify_pump.recv_msg, gRC::recv_notify);
26 #ifndef SYNC_PAINT
27         pthread_mutex_init(&mutex, 0);
28         pthread_cond_init(&cond, 0);
29         int res = pthread_create(&the_thread, 0, thread_wrapper, this);
30         if (res)
31                 eFatal("RC thread couldn't be created");
32         else
33                 eDebug("RC thread created successfully");
34 #endif
35         m_spinner_enabled = 0;
36 }
37
38 DEFINE_REF(gRC);
39
40 gRC::~gRC()
41 {
42         instance=0;
43
44         gOpcode o;
45         o.opcode=gOpcode::shutdown;
46         submit(o);
47 #ifndef SYNC_PAINT
48         eDebug("waiting for gRC thread shutdown");
49         pthread_join(the_thread, 0);
50         eDebug("gRC thread has finished");
51 #endif
52 }
53
54 void gRC::submit(const gOpcode &o)
55 {
56         while(1)
57         {
58 #ifndef SYNC_PAINT
59                 pthread_mutex_lock(&mutex);
60 #endif
61                 int tmp=wp+1;
62                 if ( tmp == MAXSIZE )
63                         tmp=0;
64                 if ( tmp == rp )
65                 {
66 #ifndef SYNC_PAINT
67                         pthread_cond_signal(&cond);  // wakeup gdi thread
68                         pthread_mutex_unlock(&mutex);
69 #else
70                         thread();
71 #endif
72                         //printf("render buffer full...\n");
73                         //fflush(stdout);
74                         usleep(1000);  // wait 1 msec
75                         continue;
76                 }
77                 int free=rp-wp;
78                 if ( free <= 0 )
79                         free+=MAXSIZE;
80                 queue[wp++]=o;
81                 if ( wp == MAXSIZE )
82                         wp = 0;
83                 if (o.opcode==gOpcode::flush||o.opcode==gOpcode::shutdown||o.opcode==gOpcode::notify)
84 #ifndef SYNC_PAINT
85                         pthread_cond_signal(&cond);  // wakeup gdi thread
86                 pthread_mutex_unlock(&mutex);
87 #else
88                         thread(); // paint
89 #endif
90                 break;
91         }
92 }
93
94 void *gRC::thread()
95 {
96         int need_notify = 0;
97 #ifndef SYNC_PAINT
98         while (1)
99         {
100 #else
101         while (rp != wp)
102         {
103 #endif
104 #ifndef SYNC_PAINT
105                 pthread_mutex_lock(&mutex);
106 #endif
107                 if ( rp != wp )
108                 {
109                                 /* make sure the spinner is not displayed when we something is painted */
110                         disableSpinner();
111
112                         gOpcode o(queue[rp++]);
113                         if ( rp == MAXSIZE )
114                                 rp=0;
115 #ifndef SYNC_PAINT
116                         pthread_mutex_unlock(&mutex);
117 #endif
118                         if (o.opcode==gOpcode::shutdown)
119                                 break;
120                         else if (o.opcode==gOpcode::notify)
121                                 need_notify = 1;
122                         else
123                         {
124                                 o.dc->exec(&o);
125                                 // o.dc is a gDC* filled with grabref... so we must release it here
126                                 o.dc->Release();
127                         }
128                 }
129                 else
130                 {
131                         if (need_notify)
132                         {
133                                 need_notify = 0;
134                                 m_notify_pump.send(1);
135                         }
136 #ifndef SYNC_PAINT
137                         while(rp == wp)
138                         {
139                         
140                                         /* when the main thread is non-idle for a too long time without any display output,
141                                            we want to display a spinner. */
142
143                                 struct timeval time;
144                                 struct timespec timeout;
145                                 gettimeofday(&time, NULL);
146                                 timeout.tv_sec = time.tv_sec;
147                                 timeout.tv_nsec = time.tv_usec * 1000;
148                                 
149                                 if (m_spinner_enabled)
150                                         timeout.tv_nsec += 100*1000*1000;
151                                 else
152                                         timeout.tv_sec += 2;
153
154                                         /* yes, this is required. */
155                                 if (timeout.tv_nsec > 1000*1000*1000)
156                                 {
157                                         timeout.tv_nsec -= 1000*1000*1000;
158                                         timeout.tv_sec++;
159                                 }
160
161                                 int idle = 1;
162
163                                 if (pthread_cond_timedwait(&cond, &mutex, &timeout) == ETIMEDOUT)
164                                 {
165                                         if (eApp && !eApp->isIdle())
166                                                 idle = 0;
167                                 }
168                                 
169                                 pthread_mutex_unlock(&mutex);
170
171                                 if (!idle)
172                                 {
173                                         if (!m_spinner_enabled)
174                                                 eDebug("main thread is non-idle! display spinner!");
175                                         enableSpinner();
176                                 } else
177                                         disableSpinner();
178                         }
179 #endif
180                 }
181         }
182 #ifndef SYNC_PAINT
183         pthread_exit(0);
184 #endif
185         return 0;
186 }
187
188 void gRC::recv_notify(const int &i)
189 {
190         notify();
191 }
192
193 gRC *gRC::getInstance()
194 {
195         return instance;
196 }
197
198 void gRC::enableSpinner()
199 {
200         if (!m_spinner_dc)
201         {
202                 eDebug("no spinner DC!");
203                 return;
204         }
205
206         gOpcode o;
207         o.opcode = m_spinner_enabled ? gOpcode::incrementSpinner : gOpcode::enableSpinner;
208         m_spinner_dc->exec(&o);
209         m_spinner_enabled = 1;
210         o.opcode = gOpcode::flush;
211         m_spinner_dc->exec(&o);
212 }
213
214 void gRC::disableSpinner()
215 {
216         if (!m_spinner_enabled)
217                 return;
218
219         if (!m_spinner_dc)
220         {
221                 eDebug("no spinner DC!");
222                 return;
223         }
224
225         m_spinner_enabled = 0;
226         
227         gOpcode o;
228         o.opcode = gOpcode::disableSpinner;
229         m_spinner_dc->exec(&o);
230         o.opcode = gOpcode::flush;
231         m_spinner_dc->exec(&o);
232 }
233
234 static int gPainter_instances;
235
236 gPainter::gPainter(gDC *dc, eRect rect): m_dc(dc), m_rc(gRC::getInstance())
237 {
238 //      ASSERT(!gPainter_instances);
239         gPainter_instances++;
240 //      begin(rect);
241 }
242
243 gPainter::~gPainter()
244 {
245         end();
246         gPainter_instances--;
247 }
248
249 void gPainter::setBackgroundColor(const gColor &color)
250 {
251         if ( m_dc->islocked() )
252                 return;
253         gOpcode o;
254         o.opcode = gOpcode::setBackgroundColor;
255         o.dc = m_dc.grabRef();
256         o.parm.setColor = new gOpcode::para::psetColor;
257         o.parm.setColor->color = color;
258
259         m_rc->submit(o);
260 }
261
262 void gPainter::setForegroundColor(const gColor &color)
263 {
264         if ( m_dc->islocked() )
265                 return;
266         gOpcode o;
267         o.opcode = gOpcode::setForegroundColor;
268         o.dc = m_dc.grabRef();
269         o.parm.setColor = new gOpcode::para::psetColor;
270         o.parm.setColor->color = color;
271
272         m_rc->submit(o);
273 }
274
275 void gPainter::setBackgroundColor(const gRGB &color)
276 {
277         if ( m_dc->islocked() )
278                 return;
279         gOpcode o;
280         o.opcode = gOpcode::setBackgroundColorRGB;
281         o.dc = m_dc.grabRef();
282         o.parm.setColorRGB = new gOpcode::para::psetColorRGB;
283         o.parm.setColorRGB->color = color;
284
285         m_rc->submit(o);
286 }
287
288 void gPainter::setForegroundColor(const gRGB &color)
289 {
290         if ( m_dc->islocked() )
291                 return;
292         gOpcode o;
293         o.opcode = gOpcode::setForegroundColorRGB;
294         o.dc = m_dc.grabRef();
295         o.parm.setColorRGB = new gOpcode::para::psetColorRGB;
296         o.parm.setColorRGB->color = color;
297
298         m_rc->submit(o);
299 }
300
301 void gPainter::setFont(gFont *font)
302 {
303         if ( m_dc->islocked() )
304                 return;
305         gOpcode o;
306         o.opcode = gOpcode::setFont;
307         o.dc = m_dc.grabRef();
308         font->AddRef();
309         o.parm.setFont = new gOpcode::para::psetFont;
310         o.parm.setFont->font = font;
311
312         m_rc->submit(o);
313 }
314
315 void gPainter::renderText(const eRect &pos, const std::string &string, int flags)
316 {
317         if ( m_dc->islocked() )
318                 return;
319         gOpcode o;
320         o.opcode=gOpcode::renderText;
321         o.dc = m_dc.grabRef();
322         o.parm.renderText = new gOpcode::para::prenderText;
323         o.parm.renderText->area = pos;
324         o.parm.renderText->text = string.empty()?0:strdup(string.c_str());
325         o.parm.renderText->flags = flags;
326         m_rc->submit(o);
327 }
328
329 void gPainter::renderPara(eTextPara *para, ePoint offset)
330 {
331         if ( m_dc->islocked() )
332                 return;
333         gOpcode o;
334         o.opcode=gOpcode::renderPara;
335         o.dc = m_dc.grabRef();
336         o.parm.renderPara = new gOpcode::para::prenderPara;
337         o.parm.renderPara->offset = offset;
338
339         para->AddRef();
340         o.parm.renderPara->textpara = para;
341         m_rc->submit(o);
342 }
343
344 void gPainter::fill(const eRect &area)
345 {
346         if ( m_dc->islocked() )
347                 return;
348         gOpcode o;
349         o.opcode=gOpcode::fill;
350
351         o.dc = m_dc.grabRef();
352         o.parm.fill = new gOpcode::para::pfillRect;
353         o.parm.fill->area = area;
354         m_rc->submit(o);
355 }
356
357 void gPainter::fill(const gRegion &region)
358 {
359         if ( m_dc->islocked() )
360                 return;
361         gOpcode o;
362         o.opcode=gOpcode::fillRegion;
363
364         o.dc = m_dc.grabRef();
365         o.parm.fillRegion = new gOpcode::para::pfillRegion;
366         o.parm.fillRegion->region = region;
367         m_rc->submit(o);
368 }
369
370 void gPainter::clear()
371 {
372         if ( m_dc->islocked() )
373                 return;
374         gOpcode o;
375         o.opcode=gOpcode::clear;
376         o.dc = m_dc.grabRef();
377         o.parm.fill = new gOpcode::para::pfillRect;
378         o.parm.fill->area = eRect();
379         m_rc->submit(o);
380 }
381
382 void gPainter::blit(gPixmap *pixmap, ePoint pos, const eRect &clip, int flags)
383 {
384         if ( m_dc->islocked() )
385                 return;
386         gOpcode o;
387
388         ASSERT(pixmap);
389
390         o.opcode=gOpcode::blit;
391         o.dc = m_dc.grabRef();
392         pixmap->AddRef();
393         o.parm.blit  = new gOpcode::para::pblit;
394         o.parm.blit->pixmap = pixmap;
395         o.parm.blit->position = pos;
396         o.parm.blit->clip = clip;
397         o.parm.blit->flags=flags;
398         m_rc->submit(o);
399 }
400
401
402 void gPainter::setPalette(gRGB *colors, int start, int len)
403 {
404         if ( m_dc->islocked() )
405                 return;
406         gOpcode o;
407         o.opcode=gOpcode::setPalette;
408         o.dc = m_dc.grabRef();
409         gPalette *p=new gPalette;
410
411         o.parm.setPalette = new gOpcode::para::psetPalette;
412         p->data=new gRGB[len];
413
414         memcpy(p->data, colors, len*sizeof(gRGB));
415         p->start=start;
416         p->colors=len;
417         o.parm.setPalette->palette = p;
418         m_rc->submit(o);
419 }
420
421 void gPainter::setPalette(gPixmap *source)
422 {
423         ASSERT(source);
424         setPalette(source->surface->clut.data, source->surface->clut.start, source->surface->clut.colors);
425 }
426
427 void gPainter::mergePalette(gPixmap *target)
428 {
429         if ( m_dc->islocked() )
430                 return;
431         gOpcode o;
432         o.opcode = gOpcode::mergePalette;
433         o.dc = m_dc.grabRef();
434         target->AddRef();
435         o.parm.mergePalette = new gOpcode::para::pmergePalette;
436         o.parm.mergePalette->target = target;
437         m_rc->submit(o);
438 }
439
440 void gPainter::line(ePoint start, ePoint end)
441 {
442         if ( m_dc->islocked() )
443                 return;
444         gOpcode o;
445         o.opcode=gOpcode::line;
446         o.dc = m_dc.grabRef();
447         o.parm.line = new gOpcode::para::pline;
448         o.parm.line->start = start;
449         o.parm.line->end = end;
450         m_rc->submit(o);
451 }
452
453 void gPainter::setOffset(ePoint val)
454 {
455         if ( m_dc->islocked() )
456                 return;
457         gOpcode o;
458         o.opcode=gOpcode::setOffset;
459         o.dc = m_dc.grabRef();
460         o.parm.setOffset = new gOpcode::para::psetOffset;
461         o.parm.setOffset->rel = 0;
462         o.parm.setOffset->value = val;
463         m_rc->submit(o);
464 }
465
466 void gPainter::moveOffset(ePoint rel)
467 {
468         if ( m_dc->islocked() )
469                 return;
470         gOpcode o;
471         o.opcode=gOpcode::setOffset;
472         o.dc = m_dc.grabRef();
473         o.parm.setOffset = new gOpcode::para::psetOffset;
474         o.parm.setOffset->rel = 1;
475         o.parm.setOffset->value = rel;
476         m_rc->submit(o);
477 }
478
479 void gPainter::resetOffset()
480 {
481         if ( m_dc->islocked() )
482                 return;
483         gOpcode o;
484         o.opcode=gOpcode::setOffset;
485         o.dc = m_dc.grabRef();
486         o.parm.setOffset = new gOpcode::para::psetOffset;
487         o.parm.setOffset->rel = 0;
488         o.parm.setOffset->value = ePoint(0, 0);
489         m_rc->submit(o);
490 }
491
492 void gPainter::resetClip(const gRegion &region)
493 {
494         if ( m_dc->islocked() )
495                 return;
496         gOpcode o;
497         o.opcode = gOpcode::setClip;
498         o.dc = m_dc.grabRef();
499         o.parm.clip = new gOpcode::para::psetClip;
500         o.parm.clip->region = region;
501         m_rc->submit(o);
502 }
503
504 void gPainter::clip(const gRegion &region)
505 {
506         if ( m_dc->islocked() )
507                 return;
508         gOpcode o;
509         o.opcode = gOpcode::addClip;
510         o.dc = m_dc.grabRef();
511         o.parm.clip = new gOpcode::para::psetClip;
512         o.parm.clip->region = region;
513         m_rc->submit(o);
514 }
515
516 void gPainter::clippop()
517 {
518         if ( m_dc->islocked() )
519                 return;
520         gOpcode o;
521         o.opcode = gOpcode::popClip;
522         o.dc = m_dc.grabRef();
523         m_rc->submit(o);
524 }
525
526 void gPainter::waitVSync()
527 {
528         if ( m_dc->islocked() )
529                 return;
530         gOpcode o;
531         o.opcode = gOpcode::waitVSync;
532         o.dc = m_dc.grabRef();
533         m_rc->submit(o);
534 }
535
536 void gPainter::flip()
537 {
538         if ( m_dc->islocked() )
539                 return;
540         gOpcode o;
541         o.opcode = gOpcode::flip;
542         o.dc = m_dc.grabRef();
543         m_rc->submit(o);
544 }
545
546 void gPainter::notify()
547 {
548         if ( m_dc->islocked() )
549                 return;
550         gOpcode o;
551         o.opcode = gOpcode::notify;
552         o.dc = m_dc.grabRef();
553         m_rc->submit(o);
554 }
555
556 void gPainter::end()
557 {
558         if ( m_dc->islocked() )
559                 return;
560         gOpcode o;
561         o.opcode = gOpcode::flush;
562         o.dc = m_dc.grabRef();
563         m_rc->submit(o);
564 }
565
566 gDC::gDC()
567 {
568         m_spinner_pic = 0;
569 }
570
571 gDC::gDC(gPixmap *pixmap): m_pixmap(pixmap)
572 {
573         m_spinner_pic = 0;
574 }
575
576 gDC::~gDC()
577 {
578         delete[] m_spinner_pic;
579 }
580
581 void gDC::exec(gOpcode *o)
582 {
583         switch (o->opcode)
584         {
585         case gOpcode::setBackgroundColor:
586                 m_background_color = o->parm.setColor->color;
587                 m_background_color_rgb = getRGB(m_background_color);
588                 delete o->parm.setColor;
589                 break;
590         case gOpcode::setForegroundColor:
591                 m_foreground_color = o->parm.setColor->color;
592                 m_background_color_rgb = getRGB(m_foreground_color);
593                 delete o->parm.setColor;
594                 break;
595         case gOpcode::setBackgroundColorRGB:
596                 if (m_pixmap->needClut())
597                         m_background_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
598                 m_background_color_rgb = o->parm.setColorRGB->color;
599                 delete o->parm.setColorRGB;
600                 break;
601         case gOpcode::setForegroundColorRGB:
602                 if (m_pixmap->needClut())
603                         m_foreground_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
604                 m_foreground_color_rgb = o->parm.setColorRGB->color;
605                 delete o->parm.setColorRGB;
606                 break;
607         case gOpcode::setFont:
608                 m_current_font = o->parm.setFont->font;
609                 o->parm.setFont->font->Release();
610                 delete o->parm.setFont;
611                 break;
612         case gOpcode::renderText:
613         {
614                 ePtr<eTextPara> para = new eTextPara(o->parm.renderText->area);
615                 int flags = o->parm.renderText->flags;
616                 assert(m_current_font);
617                 para->setFont(m_current_font);
618                 para->renderString(o->parm.renderText->text, (flags & gPainter::RT_WRAP) ? RS_WRAP : 0);
619                 if (o->parm.renderText->text)
620                         free(o->parm.renderText->text);
621                 if (flags & gPainter::RT_HALIGN_RIGHT)
622                         para->realign(eTextPara::dirRight);
623                 else if (flags & gPainter::RT_HALIGN_CENTER)
624                         para->realign(eTextPara::dirCenter);
625                 else if (flags & gPainter::RT_HALIGN_BLOCK)
626                         para->realign(eTextPara::dirBlock);
627                 
628                 ePoint offset = m_current_offset;
629                 
630                 if (o->parm.renderText->flags & gPainter::RT_VALIGN_CENTER)
631                 {
632                         eRect bbox = para->getBoundBox();
633                         int vcentered_top = o->parm.renderText->area.top() + ((o->parm.renderText->area.height() - bbox.height()) / 2);
634                         int correction = vcentered_top - bbox.top();
635                         offset += ePoint(0, correction);
636                 }
637                 
638                 para->blit(*this, offset, m_background_color_rgb, m_foreground_color_rgb);
639                 delete o->parm.renderText;
640                 break;
641         }
642         case gOpcode::renderPara:
643         {
644                 o->parm.renderPara->textpara->blit(*this, o->parm.renderPara->offset + m_current_offset, m_background_color_rgb, m_foreground_color_rgb);
645                 o->parm.renderPara->textpara->Release();
646                 delete o->parm.renderPara;
647                 break;
648         }
649         case gOpcode::fill:
650         {
651                 eRect area = o->parm.fill->area;
652                 area.moveBy(m_current_offset);
653                 gRegion clip = m_current_clip & area;
654                 if (m_pixmap->needClut())
655                         m_pixmap->fill(clip, m_foreground_color);
656                 else
657                         m_pixmap->fill(clip, m_foreground_color_rgb);
658                 delete o->parm.fill;
659                 break;
660         }
661         case gOpcode::fillRegion:
662         {
663                 o->parm.fillRegion->region.moveBy(m_current_offset);
664                 gRegion clip = m_current_clip & o->parm.fillRegion->region;
665                 if (m_pixmap->needClut())
666                         m_pixmap->fill(clip, m_foreground_color);
667                 else
668                         m_pixmap->fill(clip, m_foreground_color_rgb);
669                 delete o->parm.fillRegion;
670                 break;
671         }
672         case gOpcode::clear:
673                 if (m_pixmap->needClut())
674                         m_pixmap->fill(m_current_clip, m_background_color);
675                 else
676                         m_pixmap->fill(m_current_clip, m_background_color_rgb);
677                 delete o->parm.fill;
678                 break;
679         case gOpcode::blit:
680         {
681                 gRegion clip;
682                                 // this code should be checked again but i'm too tired now
683                 
684                 o->parm.blit->position += m_current_offset;
685                 
686                 if (o->parm.blit->clip.valid())
687                 {
688                         o->parm.blit->clip.moveBy(m_current_offset);
689                         clip.intersect(gRegion(o->parm.blit->clip), m_current_clip);
690                 } else
691                         clip = m_current_clip;
692                 
693                 m_pixmap->blit(*o->parm.blit->pixmap, o->parm.blit->position, clip, o->parm.blit->flags);
694                 o->parm.blit->pixmap->Release();
695                 delete o->parm.blit;
696                 break;
697         }
698         case gOpcode::setPalette:
699                 if (o->parm.setPalette->palette->start > m_pixmap->surface->clut.colors)
700                         o->parm.setPalette->palette->start = m_pixmap->surface->clut.colors;
701                 if (o->parm.setPalette->palette->colors > (m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start))
702                         o->parm.setPalette->palette->colors = m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start;
703                 if (o->parm.setPalette->palette->colors)
704                         memcpy(m_pixmap->surface->clut.data+o->parm.setPalette->palette->start, o->parm.setPalette->palette->data, o->parm.setPalette->palette->colors*sizeof(gRGB));
705                 
706                 delete[] o->parm.setPalette->palette->data;
707                 delete o->parm.setPalette->palette;
708                 delete o->parm.setPalette;
709                 break;
710         case gOpcode::mergePalette:
711                 m_pixmap->mergePalette(*o->parm.mergePalette->target);
712                 o->parm.mergePalette->target->Release();
713                 delete o->parm.mergePalette;
714                 break; 
715         case gOpcode::line:
716         {
717                 ePoint start = o->parm.line->start + m_current_offset, end = o->parm.line->end + m_current_offset;
718                 m_pixmap->line(m_current_clip, start, end, m_foreground_color);
719                 delete o->parm.line;
720                 break;
721         }
722         case gOpcode::addClip:
723                 m_clip_stack.push(m_current_clip);
724                 o->parm.clip->region.moveBy(m_current_offset);
725                 m_current_clip &= o->parm.clip->region;
726                 delete o->parm.clip;
727                 break;
728         case gOpcode::setClip:
729                 o->parm.clip->region.moveBy(m_current_offset);
730                 m_current_clip = o->parm.clip->region & eRect(ePoint(0, 0), m_pixmap->size());
731                 delete o->parm.clip;
732                 break;
733         case gOpcode::popClip:
734                 if (!m_clip_stack.empty())
735                 {
736                         m_current_clip = m_clip_stack.top();
737                         m_clip_stack.pop();
738                 }
739                 break;
740         case gOpcode::setOffset:
741                 if (o->parm.setOffset->rel)
742                         m_current_offset += o->parm.setOffset->value;
743                 else
744                         m_current_offset  = o->parm.setOffset->value;
745                 delete o->parm.setOffset;
746                 break;
747         case gOpcode::waitVSync:
748                 break;
749         case gOpcode::flip:
750                 break;
751         case gOpcode::flush:
752                 break;
753         case gOpcode::enableSpinner:
754                 enableSpinner();
755                 break;
756         case gOpcode::disableSpinner:
757                 disableSpinner();
758                 break;
759         case gOpcode::incrementSpinner:
760                 incrementSpinner();
761                 break;
762         default:
763                 eFatal("illegal opcode %d. expect memory leak!", o->opcode);
764         }
765 }
766
767 gRGB gDC::getRGB(gColor col)
768 {
769         if ((!m_pixmap) || (!m_pixmap->surface->clut.data))
770                 return gRGB(col, col, col);
771         if (col<0)
772         {
773                 eFatal("bla transp");
774                 return gRGB(0, 0, 0, 0xFF);
775         }
776         return m_pixmap->surface->clut.data[col];
777 }
778
779 void gDC::enableSpinner()
780 {
781         ASSERT(m_spinner_saved);
782         
783                 /* save the background to restore it later. We need to negative position because we want to blit from the middle of the screen. */
784         m_spinner_saved->blit(*m_pixmap, -m_spinner_pos.topLeft(), gRegion(eRect(ePoint(0, 0), m_spinner_saved->size())), 0);
785         
786         incrementSpinner();
787 }
788
789 void gDC::disableSpinner()
790 {
791         ASSERT(m_spinner_saved);
792
793                 /* restore background */
794         m_pixmap->blit(*m_spinner_saved, m_spinner_pos.topLeft(), gRegion(m_spinner_pos), 0);
795 }
796
797 void gDC::incrementSpinner()
798 {
799         ASSERT(m_spinner_saved);
800         
801         static int blub;
802         blub++;
803
804 #if 0
805         int i;
806         
807         for (i = 0; i < 5; ++i)
808         {
809                 int x = i * 20 + m_spinner_pos.left();
810                 int y = m_spinner_pos.top();
811                 
812                 int col = ((blub - i) * 30) % 256;
813
814                 m_pixmap->fill(eRect(x, y, 10, 10), gRGB(col, col, col));
815         }
816 #endif
817
818         m_spinner_temp->blit(*m_spinner_saved, ePoint(0, 0), eRect(ePoint(0, 0), m_spinner_pos.size()));
819
820         if (m_spinner_pic[m_spinner_i])
821                 m_spinner_temp->blit(*m_spinner_pic[m_spinner_i], ePoint(0, 0), eRect(ePoint(0, 0), m_spinner_pos.size()), gPixmap::blitAlphaTest);
822
823         m_pixmap->blit(*m_spinner_temp, m_spinner_pos.topLeft(), gRegion(m_spinner_pos), 0);
824         m_spinner_i++;
825         m_spinner_i %= m_spinner_num;
826 }
827
828 void gDC::setSpinner(eRect pos, ePtr<gPixmap> *pic, int len)
829 {
830         ASSERT(m_pixmap);
831         ASSERT(m_pixmap->surface);
832         m_spinner_saved = new gPixmap(pos.size(), m_pixmap->surface->bpp);
833         m_spinner_temp = new gPixmap(pos.size(), m_pixmap->surface->bpp);
834         m_spinner_pos = pos;
835         
836         m_spinner_i = 0;
837         m_spinner_num = len;
838         
839         int i;
840         if (m_spinner_pic)
841                 delete[] m_spinner_pic;
842         
843         m_spinner_pic = new ePtr<gPixmap>[len];
844         
845         for (i = 0; i < len; ++i)
846                 m_spinner_pic[i] = pic[i];
847 }
848
849 DEFINE_REF(gDC);
850
851 eAutoInitPtr<gRC> init_grc(eAutoInitNumbers::graphic, "gRC");