fixes for /dev/misc/pvr opening problems in some conditions
[enigma2.git] / lib / gdi / lcd.cpp
1 #include <lib/gdi/lcd.h>
2
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <sys/ioctl.h>
6
7 #include <dbox/fp.h>
8 #include <dbox/lcd-ks0713.h>
9
10 #include <lib/gdi/esize.h>
11 #include <lib/base/init.h>
12 #include <lib/base/init_num.h>
13 #include <lib/gdi/glcddc.h>
14
15 eDBoxLCD *eDBoxLCD::instance;
16
17 eLCD::eLCD(eSize size): res(size)
18 {
19         lcdfd = -1;
20         locked=0;
21         _buffer=new unsigned char[res.height()*res.width()];
22         memset(_buffer, 0, res.height()*res.width());
23         _stride=res.width();
24 }
25
26 eLCD::~eLCD()
27 {
28         delete [] _buffer;
29 }
30
31 int eLCD::lock()
32 {
33         if (locked)
34                 return -1;
35
36         locked=1;
37         return lcdfd;
38 }
39
40 void eLCD::unlock()
41 {
42         locked=0;
43 }
44
45 eDBoxLCD::eDBoxLCD(): eLCD(eSize(132, 64))
46 {
47         is_oled = 0;
48 #ifndef NO_LCD
49         lcdfd = open("/dev/dbox/oled0", O_RDWR);
50         if (lcdfd < 0)
51         {
52                 FILE *f=fopen("/proc/stb/fp/oled_brightness", "w");
53                 if (f)
54                 {
55                         is_oled = 2;
56                         fclose(f);
57                 }
58                 lcdfd = open("/dev/dbox/lcd0", O_RDWR);
59         } else
60         {
61                 eDebug("found OLED display!");
62                 is_oled = 1;
63         }
64 #else
65         lcdfd = -1;
66 #endif
67         instance=this;
68
69         if (lcdfd<0)
70                 eDebug("couldn't open LCD - load lcd.o!");
71         else
72         {
73                 int i=LCD_MODE_BIN;
74                 ioctl(lcdfd, LCD_IOCTL_ASC_MODE, &i);
75                 inverted=0;
76         }
77 }
78
79 void eDBoxLCD::setInverted(unsigned char inv)
80 {
81         inverted=inv;
82         update();       
83 }
84
85 int eDBoxLCD::setLCDContrast(int contrast)
86 {
87         int fp;
88         if((fp=open("/dev/dbox/fp0", O_RDWR))<=0)
89         {
90                 eDebug("[LCD] can't open /dev/dbox/fp0");
91                 return(-1);
92         }
93
94         if(ioctl(lcdfd, LCD_IOCTL_SRV, &contrast))
95         {
96                 eDebug("[LCD] can't set lcd contrast");
97         }
98         close(fp);
99         return(0);
100 }
101
102 int eDBoxLCD::setLCDBrightness(int brightness)
103 {
104         eDebug("setLCDBrightness %d", brightness);
105         FILE *f=fopen("/proc/stb/fp/oled_brightness", "w");
106         if (f)
107         {
108                 if (fprintf(f, "%d", brightness) == 0)
109                         eDebug("write /proc/stb/fp/oled_brightness failed!! (%m)");
110                 fclose(f);
111         }
112         else
113         {
114                 int fp;
115                 if((fp=open("/dev/dbox/fp0", O_RDWR))<=0)
116                 {
117                         eDebug("[LCD] can't open /dev/dbox/fp0");
118                         return(-1);
119                 }
120
121                 if(ioctl(fp, FP_IOCTL_LCD_DIMM, &brightness)<=0)
122                         eDebug("[LCD] can't set lcd brightness (%m)");
123                 close(fp);
124         }
125         return(0);
126 }
127
128 eDBoxLCD::~eDBoxLCD()
129 {
130         if (lcdfd>=0)
131         {
132                 close(lcdfd);
133                 lcdfd=-1;
134         }
135 }
136
137 eDBoxLCD *eDBoxLCD::getInstance()
138 {
139         return instance;
140 }
141
142 void eDBoxLCD::update()
143 {
144         if (!is_oled || is_oled == 2)
145         {
146                 unsigned char raw[132*8];
147                 int x, y, yy;
148                 for (y=0; y<8; y++)
149                 {
150                         for (x=0; x<132; x++)
151                         {
152                                 int pix=0;
153                                 for (yy=0; yy<8; yy++)
154                                 {
155                                         pix|=(_buffer[(y*8+yy)*132+x]>=108)<<yy;
156                                 }
157                                 raw[y*132+x]=(pix^inverted);
158                         }
159                 }
160                 if (lcdfd >= 0)
161                         write(lcdfd, raw, 132*8);
162         } else
163         {
164                 unsigned char raw[64*64];
165                 int x, y;
166                 memset(raw, 0, 64*64);
167                 for (y=0; y<64; y++)
168                 {
169                         int pix=0;
170                         for (x=0; x<128 / 2; x++)
171                         {
172                                 pix = (_buffer[y*132 + x * 2 + 2] & 0xF0) |(_buffer[y*132 + x * 2 + 1 + 2] >> 4);
173                                 if (inverted)
174                                         pix = 0xFF - pix;
175                                 raw[y*64+x] = pix;
176                         }
177                 }
178                 if (lcdfd >= 0)
179                         write(lcdfd, raw, 64*64);
180         }
181 }
182