added setup screens
[enigma2.git] / lib / gdi / gfbdc.cpp
1 #include <lib/gdi/gfbdc.h>
2
3 #include <lib/base/init.h>
4 #include <lib/base/init_num.h>
5 #include <lib/base/econfig.h>
6
7 gFBDC *gFBDC::instance;
8
9 gFBDC::gFBDC()
10 {
11         instance=this;
12         fb=new fbClass;
13
14         if (!fb->Available())
15                 eFatal("no framebuffer available");
16
17         fb->SetMode(720, 576, 32);
18
19         for (int y=0; y<576; y++)                                                                                                                                                // make whole screen transparent
20                 memset(fb->lfb+y*fb->Stride(), 0x00, fb->Stride());
21
22         surface.type = 0;
23         surface.x = 720;
24         surface.y = 576;
25         surface.bpp = 32;
26         surface.bypp = 4;
27         surface.stride = fb->Stride();
28         surface.data = fb->lfb;
29         surface.clut.colors=256;
30         surface.clut.data=new gRGB[surface.clut.colors];
31         
32         m_pixmap = new gPixmap(&surface);
33         
34         memset(surface.clut.data, 0, sizeof(*surface.clut.data)*surface.clut.colors);
35         reloadSettings();
36 }
37
38 gFBDC::~gFBDC()
39 {
40         delete fb;
41         instance=0;
42 }
43
44 void gFBDC::calcRamp()
45 {
46 #if 0
47         float fgamma=gamma ? gamma : 1;
48         fgamma/=10.0;
49         fgamma=1/log(fgamma);
50         for (int i=0; i<256; i++)
51         {
52                 float raw=i/255.0; // IIH, float.
53                 float corr=pow(raw, fgamma) * 256.0;
54
55                 int d=corr * (float)(256-brightness) / 256 + brightness;
56                 if (d < 0)
57                         d=0;
58                 if (d > 255)
59                         d=255;
60                 ramp[i]=d;
61                 
62                 rampalpha[i]=i*alpha/256;
63         }
64 #endif
65         for (int i=0; i<256; i++)
66         {
67                 int d;
68                 d=i;
69                 d=(d-128)*(gamma+64)/(128+64)+128;
70                 d+=brightness-128; // brightness correction
71                 if (d<0)
72                         d=0;
73                 if (d>255)
74                         d=255;
75                 ramp[i]=d;
76
77                 rampalpha[i]=i*alpha/256;
78         }
79
80         rampalpha[255]=255; // transparent BLEIBT bitte so.
81 }
82
83 void gFBDC::setPalette()
84 {
85         if (!surface.clut.data)
86                 return;
87         
88         for (int i=0; i<256; ++i)
89         {
90                 fb->CMAP()->red[i]=ramp[surface.clut.data[i].r]<<8;
91                 fb->CMAP()->green[i]=ramp[surface.clut.data[i].g]<<8;
92                 fb->CMAP()->blue[i]=ramp[surface.clut.data[i].b]<<8;
93                 fb->CMAP()->transp[i]=rampalpha[surface.clut.data[i].a]<<8;
94                 if (!fb->CMAP()->red[i])
95                         fb->CMAP()->red[i]=0x100;
96         }
97         fb->PutCMAP();
98 }
99
100 void gFBDC::exec(gOpcode *o)
101 {
102         switch (o->opcode)
103         {
104         case gOpcode::setPalette:
105         {
106                 gDC::exec(o);
107                 setPalette();
108                 break;
109         }
110         default:
111                 gDC::exec(o);
112                 break;
113         }
114 }
115
116 void gFBDC::setAlpha(int a)
117 {
118         alpha=a;
119
120         calcRamp();
121         setPalette();
122 }
123
124 void gFBDC::setBrightness(int b)
125 {
126         brightness=b;
127
128         calcRamp();
129         setPalette();
130 }
131
132 void gFBDC::setGamma(int g)
133 {
134         gamma=g;
135
136         calcRamp();
137         setPalette();
138 }
139
140 void gFBDC::saveSettings()
141 {
142         eConfig::getInstance()->setKey("/ezap/osd/alpha", alpha);
143         eConfig::getInstance()->setKey("/ezap/osd/gamma", gamma);
144         eConfig::getInstance()->setKey("/ezap/osd/brightness", brightness);
145 }
146
147 void gFBDC::reloadSettings()
148 {
149         if (eConfig::getInstance()->getKey("/ezap/osd/alpha", alpha))
150                 alpha=255;
151         if (eConfig::getInstance()->getKey("/ezap/osd/gamma", gamma))
152                 gamma=128;
153         if (eConfig::getInstance()->getKey("/ezap/osd/brightness", brightness))
154                 brightness=128;
155
156         calcRamp();
157         setPalette();
158 }
159
160 // eAutoInitPtr<gFBDC> init_gFBDC(eAutoInitNumbers::graphic-1, "GFBDC");
161 #ifndef SDLDC
162 eAutoInitPtr<gFBDC> init_gFBDC(eAutoInitNumbers::graphic-1, "GFBDC");
163 #endif
164