fix memleak
[enigma2.git] / lib / gdi / lcd.cpp
1 #if 0
2 #ifndef DISABLE_LCD
3
4 #include <lib/gdi/lcd.h>
5
6 #include <unistd.h>
7 #include <fcntl.h>
8 #include <sys/ioctl.h>
9
10 #include <dbox/fp.h>
11 #include <dbox/lcd-ks0713.h>
12
13 #include <lib/base/esize.h>
14 #include <lib/system/init.h>
15 #include <lib/system/init_num.h>
16 #include <lib/gdi/glcddc.h>
17 #include <lib/system/econfig.h>
18
19 eDBoxLCD *eDBoxLCD::instance;
20
21 eLCD::eLCD(eSize size): res(size)
22 {
23         locked=0;
24         _buffer=new unsigned char[res.height()*res.width()];
25         _stride=res.width();
26 }
27
28 eLCD::~eLCD()
29 {
30         delete [] _buffer;
31 }
32
33 int eLCD::lock()
34 {
35         if (locked)
36                 return -1;
37
38         locked=1;
39         return lcdfd;
40 }
41
42 void eLCD::unlock()
43 {
44         read( lcdfd, NULL, 0);
45         if ( errno == 9 )
46         {
47                 eDebug("reopen lcd");
48                 lcdfd=open("/dev/dbox/lcd0", O_RDWR);  // reopen device
49         }
50         else
51                 eDebug("do not reopen lcd.. errno = %d", errno);
52     
53         locked=0;
54 }
55
56 /* void eLCD::line(ePoint start, ePoint dst, int color)
57 {
58 int Ax=start.x(), // dieser code rult ganz ganz doll weil er ganz ganz fast ist und auch sehr gut dokumentiert is
59 Ay=start.y(), Bx=dst.x(), // t. es handelt sich immerhin um den weltbekannten bresenham algorithmus der nicht nur
60 By=dst.y(); int dX, dY, fbXincr, // sehr schnell ist sondern auch sehr gut dokumentiert und getestet wurde. nicht
61 fbYincr, fbXYincr, dPr, dPru, P; __u8 // nur auf dem LCD der dbox, sondern auch ueberall anders. und auch auf der
62 *AfbAddr = &buffer()[Ay*stride()+Ax]; __u8 // dbox mit LCD soll das teil nun tun, und ich denke das tut es. ausse
63 *BfbAddr = &buffer()[By*stride()+Bx]; fbXincr= // rdem hat dieser algo den vorteil dass man fehler sehr leicht fi
64 1; if ( (dX=Bx-Ax) >= 0) goto AFTERNEGX; dX=-dX; // ndet und beheben kann. das liegt nicht zuletzt an den komment
65 fbXincr=-1; AFTERNEGX: fbYincr=stride(); if ( (dY=By // aren. und ausserdem, je kuerzer der code, desto weniger k
66 -Ay) >= 0) goto AFTERNEGY; fbYincr=-stride(); dY=-dY;AFTERNEGY: // ann daran falsch sein. erwaehnte ich schon, da
67 fbXYincr = fbXincr+fbYincr; if (dY > dX) goto YisIndependent; dPr = dY+ // s dieser tolle code wahnsinnig schnell
68 dY; P = -dX; dPru = P+P; dY = dX>>1; XLOOP: *AfbAddr=color; *BfbAddr=color; if ((P+=dPr) > 0) // ist? bye, tmbinc
69 goto RightAndUp;  AfbAddr+=fbXincr; BfbAddr-=fbXincr; if ((dY=dY-1) > 0) goto XLOOP; *AfbAddr=color; if ((dX & 1)
70 == 0) return;  *BfbAddr=color; return; RightAndUp: AfbAddr+=fbXYincr; BfbAddr-=fbXYincr; P+=dPru; if ((dY=dY-1) >
71 0) goto XLOOP;  *AfbAddr=color; if ((dX & 1) == 0) return; *BfbAddr=color; return; YisIndependent: dPr = dX+dX; P
72 = -dY; dPru = P+P; dX = dY>>1; YLOOP: *AfbAddr=color; *BfbAddr=color; if ((P+=dPr) > 0) goto RightAndUp2; AfbAddr
73 +=fbYincr;  BfbAddr-=fbYincr; if ((dX=dX-1) > 0) goto YLOOP; *AfbAddr=color; if ((dY & 1) == 0) return; *BfbAddr=
74 color;return; RightAndUp2: AfbAddr+=fbXYincr; BfbAddr-=fbXYincr; P+=dPru; if ((dX=dX-1) > 0) goto YLOOP; *AfbAddr
75 =color; if((dY & 1) == 0) return; *BfbAddr=color; return; // nun ist der tolle code leider zu ende. tut mir leid.
76 } */
77
78 eDBoxLCD::eDBoxLCD(): eLCD(eSize(128, 64))
79 {
80 #ifndef NO_LCD
81         lcdfd=open("/dev/dbox/lcd0", O_RDWR);
82 #else
83         lcdfd=-1;
84 #endif
85         instance=this;
86
87         if (lcdfd<0)
88                 eDebug("couldn't open LCD - load lcd.o!");
89         else
90         {
91                 int i=LCD_MODE_BIN;
92                 ioctl(lcdfd, LCD_IOCTL_ASC_MODE, &i);
93                 int lcdbrightness=0, lcdcontrast=0;
94
95                 if( eConfig::getInstance()->getKey("/ezap/lcd/brightness", lcdbrightness) )
96                 {
97                         lcdbrightness=130;
98                         eConfig::getInstance()->setKey("/ezap/lcd/brightness", lcdbrightness);
99                 }
100                 if( eConfig::getInstance()->getKey("/ezap/lcd/contrast", lcdcontrast) )
101                 {
102                         lcdcontrast=32;
103                         eConfig::getInstance()->setKey("/ezap/lcd/contrast", lcdcontrast);
104                 }
105                 setLCDParameter(lcdbrightness, lcdcontrast);
106                 int tmp;
107                 if( eConfig::getInstance()->getKey("/ezap/lcd/inverted", tmp ) )
108                 {
109                         inverted=0;
110                         eConfig::getInstance()->setKey("/ezap/lcd/inverted", (int) 0 );
111                 }
112                 else
113                         inverted=(unsigned char)tmp;
114         }
115 }
116
117 void eDBoxLCD::setInverted(unsigned char inv)
118 {
119         inverted=inv;
120         update();       
121 }
122
123 int eDBoxLCD::setLCDParameter(int brightness, int contrast)
124 {
125         int fp;
126         if((fp=open("/dev/dbox/fp0", O_RDWR))<=0)
127         {
128                 eDebug("[LCD] can't open /dev/dbox/fp0");
129                 return(-1);
130         }
131
132         if(ioctl(lcdfd, LCD_IOCTL_SRV, &contrast))
133         {
134                 eDebug("[LCD] can't set lcd contrast");
135         }
136
137         if(ioctl(fp, FP_IOCTL_LCD_DIMM, &brightness))
138         {
139                 eDebug("[LCD] can't set lcd brightness");
140         }
141         eDebug("[LCD] set brightness %d, contrast %d", brightness, contrast);
142         return(0);
143 }
144
145 int eDBoxLCD::switchLCD(int state)
146 {
147         int lcdbrightness, lcdcontrast, lcdstandby=0;
148
149         eConfig::getInstance()->getKey("/ezap/lcd/contrast", lcdcontrast);
150
151         if(state==0)
152         {
153                 eConfig::getInstance()->getKey("/ezap/lcd/standby", lcdstandby);
154                 setLCDParameter(lcdstandby, lcdcontrast);
155         }
156         else
157         {
158                 eConfig::getInstance()->getKey("/ezap/lcd/brightness", lcdbrightness);
159                 setLCDParameter(lcdbrightness, lcdcontrast);
160                 
161         }
162         return(0);
163 }
164
165 eDBoxLCD::~eDBoxLCD()
166 {
167         if (lcdfd>0)
168         {
169                 close(lcdfd);
170                 lcdfd=0;
171         }
172 }
173
174 eDBoxLCD *eDBoxLCD::getInstance()
175 {
176         return instance;
177 }
178
179 void eDBoxLCD::update()
180 {
181         unsigned char raw[120*8];
182         int x, y, yy;
183         for (y=0; y<8; y++)
184         {
185                 for (x=0; x<120; x++)
186                 {
187                         int pix=0;
188                         for (yy=0; yy<8; yy++)
189                         {
190                                 pix|=(_buffer[(y*8+yy)*128+x]>=108)<<yy;
191                         }
192                         raw[y*120+x]=(pix^inverted);
193                 }
194         }
195         if (lcdfd>0)
196                 write(lcdfd, raw, 120*8);
197 }
198
199 class eDBoxLCDHardware
200 {
201         eDBoxLCD lcd;
202         gLCDDC lcddc;
203 public:
204         eDBoxLCDHardware(): lcddc(&lcd)
205         {
206         }
207 };
208
209 eAutoInitP0<eDBoxLCDHardware> init_eDBoxLCDHardware(eAutoInitNumbers::lowlevel, "d-Box LCD Hardware");
210
211 #endif //DISABLE_LCD
212
213 #endif