lcd: fix build without <dbox/*.h>
[enigma2.git] / lib / gdi / lcd.cpp
index 6b3532302dec6a7c6d211bf86b78856363b75a0e..518ee1b0834d38ea633861d499d6dccc06e08c16 100644 (file)
@@ -4,8 +4,12 @@
 #include <fcntl.h>
 #include <sys/ioctl.h>
 
+#if defined(HAVE_DBOX_FP_H) && defined(HAVE_DBOX_LCD_KS0713_H)
 #include <dbox/fp.h>
 #include <dbox/lcd-ks0713.h>
+#else
+#define NO_LCD 1
+#endif
 
 #include <lib/gdi/esize.h>
 #include <lib/base/init.h>
 
 eDBoxLCD *eDBoxLCD::instance;
 
-eLCD::eLCD(eSize size): res(size)
+eLCD::eLCD()
 {
+       lcdfd = -1;
        locked=0;
-       _buffer=new unsigned char[res.height()*res.width()];
-       _stride=res.width();
+}
+
+void eLCD::setSize(int xres, int yres, int bpp)
+{
+       res = eSize(xres, yres);
+       _buffer=new unsigned char[xres * yres * bpp/8];
+       memset(_buffer, 0, res.height()*res.width()*bpp/8);
+       _stride=res.width()*bpp/8;
+       eDebug("lcd buffer %p %d bytes, stride %d", _buffer, xres*yres*bpp/8, _stride);
 }
 
 eLCD::~eLCD()
@@ -37,26 +49,31 @@ int eLCD::lock()
 
 void eLCD::unlock()
 {
-       read( lcdfd, NULL, 0);
-       if ( errno == 9 )
-       {
-               eDebug("reopen lcd");
-               lcdfd=open("/dev/dbox/lcd0", O_RDWR);  // reopen device
-       }
-       else
-               eDebug("do not reopen lcd.. errno = %d", errno);
-    
        locked=0;
 }
 
-eDBoxLCD::eDBoxLCD(): eLCD(eSize(128, 64))
+eDBoxLCD::eDBoxLCD()
 {
+       int xres=132, yres=64, bpp=8;
+       is_oled = 0;
 #ifndef NO_LCD
-       lcdfd=open("/dev/dbox/lcd0", O_RDWR);
-#else
-       lcdfd=-1;
-#endif
-       instance=this;
+       lcdfd = open("/dev/dbox/oled0", O_RDWR);
+       if (lcdfd < 0)
+       {
+               FILE *f=fopen("/proc/stb/lcd/oled_brightness", "w");
+               if (!f)
+                       f = fopen("/proc/stb/fp/oled_brightness", "w");
+               if (f)
+               {
+                       is_oled = 2;
+                       fclose(f);
+               }
+               lcdfd = open("/dev/dbox/lcd0", O_RDWR);
+       } else
+       {
+               eDebug("found OLED display!");
+               is_oled = 1;
+       }
 
        if (lcdfd<0)
                eDebug("couldn't open LCD - load lcd.o!");
@@ -64,23 +81,46 @@ eDBoxLCD::eDBoxLCD(): eLCD(eSize(128, 64))
        {
                int i=LCD_MODE_BIN;
                ioctl(lcdfd, LCD_IOCTL_ASC_MODE, &i);
-               int lcdbrightness=0, lcdcontrast=0;
-
-               lcdbrightness=130;
-               lcdcontrast=32;
-               setLCDParameter(lcdbrightness, lcdcontrast);
                inverted=0;
+               FILE *f = fopen("/proc/stb/lcd/xres", "r");
+               if (f)
+               {
+                       int tmp;
+                       if (fscanf(f, "%x", &tmp) == 1)
+                               xres = tmp;
+                       fclose(f);
+                       f = fopen("/proc/stb/lcd/yres", "r");
+                       if (f)
+                       {
+                               if (fscanf(f, "%x", &tmp) == 1)
+                                       yres = tmp;
+                               fclose(f);
+                               f = fopen("/proc/stb/lcd/bpp", "r");
+                               if (f)
+                               {
+                                       if (fscanf(f, "%x", &tmp) == 1)
+                                               bpp = tmp;
+                                       fclose(f);
+                               }
+                       }
+                       is_oled = 3;
+               }
        }
+#endif
+       instance=this;
+
+       setSize(xres, yres, bpp);
 }
 
 void eDBoxLCD::setInverted(unsigned char inv)
 {
        inverted=inv;
-       update();       
+       update();
 }
 
-int eDBoxLCD::setLCDParameter(int brightness, int contrast)
+int eDBoxLCD::setLCDContrast(int contrast)
 {
+#ifndef NO_LCD
        int fp;
        if((fp=open("/dev/dbox/fp0", O_RDWR))<=0)
        {
@@ -92,21 +132,47 @@ int eDBoxLCD::setLCDParameter(int brightness, int contrast)
        {
                eDebug("[LCD] can't set lcd contrast");
        }
+       close(fp);
+#endif
+       return(0);
+}
 
-       if(ioctl(fp, FP_IOCTL_LCD_DIMM, &brightness))
+int eDBoxLCD::setLCDBrightness(int brightness)
+{
+#ifndef NO_LCD
+       eDebug("setLCDBrightness %d", brightness);
+       FILE *f=fopen("/proc/stb/lcd/oled_brightness", "w");
+       if (!f)
+               f = fopen("/proc/stb/fp/oled_brightness", "w");
+       if (f)
+       {
+               if (fprintf(f, "%d", brightness) == 0)
+                       eDebug("write /proc/stb/lcd/oled_brightness failed!! (%m)");
+               fclose(f);
+       }
+       else
        {
-               eDebug("[LCD] can't set lcd brightness");
+               int fp;
+               if((fp=open("/dev/dbox/fp0", O_RDWR))<=0)
+               {
+                       eDebug("[LCD] can't open /dev/dbox/fp0");
+                       return(-1);
+               }
+
+               if(ioctl(fp, FP_IOCTL_LCD_DIMM, &brightness)<=0)
+                       eDebug("[LCD] can't set lcd brightness (%m)");
+               close(fp);
        }
-       eDebug("[LCD] set brightness %d, contrast %d", brightness, contrast);
+#endif
        return(0);
 }
 
 eDBoxLCD::~eDBoxLCD()
 {
-       if (lcdfd>0)
+       if (lcdfd>=0)
        {
                close(lcdfd);
-               lcdfd=0;
+               lcdfd=-1;
        }
 }
 
@@ -117,21 +183,46 @@ eDBoxLCD *eDBoxLCD::getInstance()
 
 void eDBoxLCD::update()
 {
-       unsigned char raw[120*8];
-       int x, y, yy;
-       for (y=0; y<8; y++)
+       if (lcdfd >= 0)
        {
-               for (x=0; x<120; x++)
+               if (!is_oled || is_oled == 2)
+               {
+                       unsigned char raw[132*8];
+                       int x, y, yy;
+                       for (y=0; y<8; y++)
+                       {
+                               for (x=0; x<132; x++)
+                               {
+                                       int pix=0;
+                                       for (yy=0; yy<8; yy++)
+                                       {
+                                               pix|=(_buffer[(y*8+yy)*132+x]>=108)<<yy;
+                                       }
+                                       raw[y*132+x]=(pix^inverted);
+                               }
+                       }
+                       write(lcdfd, raw, 132*8);
+               }
+               else if (is_oled == 3)
+                       write(lcdfd, _buffer, _stride * res.height());
+               else
                {
-                       int pix=0;
-                       for (yy=0; yy<8; yy++)
+                       unsigned char raw[64*64];
+                       int x, y;
+                       memset(raw, 0, 64*64);
+                       for (y=0; y<64; y++)
                        {
-                               pix|=(_buffer[(y*8+yy)*128+x]>=108)<<yy;
+                               int pix=0;
+                               for (x=0; x<128 / 2; x++)
+                               {
+                                       pix = (_buffer[y*132 + x * 2 + 2] & 0xF0) |(_buffer[y*132 + x * 2 + 1 + 2] >> 4);
+                                       if (inverted)
+                                               pix = 0xFF - pix;
+                                       raw[y*64+x] = pix;
+                               }
                        }
-                       raw[y*120+x]=(pix^inverted);
+                       write(lcdfd, raw, 64*64);
                }
        }
-       if (lcdfd>0)
-               write(lcdfd, raw, 120*8);
 }