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