picexif: fix buffer overflow
[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 #if defined(HAVE_DBOX_FP_H) && defined(HAVE_DBOX_LCD_KS0713_H)
8 #include <dbox/fp.h>
9 #include <dbox/lcd-ks0713.h>
10 #else
11 #define NO_LCD 1
12 #endif
13
14 #include <lib/gdi/esize.h>
15 #include <lib/base/init.h>
16 #include <lib/base/init_num.h>
17 #include <lib/gdi/glcddc.h>
18
19 eDBoxLCD *eDBoxLCD::instance;
20
21 eLCD::eLCD()
22 {
23         lcdfd = -1;
24         locked=0;
25 }
26
27 void eLCD::setSize(int xres, int yres, int bpp)
28 {
29         res = eSize(xres, yres);
30         _buffer=new unsigned char[xres * yres * bpp/8];
31         memset(_buffer, 0, res.height()*res.width()*bpp/8);
32         _stride=res.width()*bpp/8;
33         eDebug("lcd buffer %p %d bytes, stride %d", _buffer, xres*yres*bpp/8, _stride);
34 }
35
36 eLCD::~eLCD()
37 {
38         delete [] _buffer;
39 }
40
41 int eLCD::lock()
42 {
43         if (locked)
44                 return -1;
45
46         locked=1;
47         return lcdfd;
48 }
49
50 void eLCD::unlock()
51 {
52         locked=0;
53 }
54
55 eDBoxLCD::eDBoxLCD()
56 {
57         int xres=132, yres=64, bpp=8;
58         is_oled = 0;
59 #ifndef NO_LCD
60         lcdfd = open("/dev/dbox/oled0", O_RDWR);
61         if (lcdfd < 0)
62         {
63                 FILE *f=fopen("/proc/stb/lcd/oled_brightness", "w");
64                 if (!f)
65                         f = fopen("/proc/stb/fp/oled_brightness", "w");
66                 if (f)
67                 {
68                         is_oled = 2;
69                         fclose(f);
70                 }
71                 lcdfd = open("/dev/dbox/lcd0", O_RDWR);
72         } else
73         {
74                 eDebug("found OLED display!");
75                 is_oled = 1;
76         }
77
78         if (lcdfd<0)
79                 eDebug("couldn't open LCD - load lcd.o!");
80         else
81         {
82                 int i=LCD_MODE_BIN;
83                 ioctl(lcdfd, LCD_IOCTL_ASC_MODE, &i);
84                 inverted=0;
85                 FILE *f = fopen("/proc/stb/lcd/xres", "r");
86                 if (f)
87                 {
88                         int tmp;
89                         if (fscanf(f, "%x", &tmp) == 1)
90                                 xres = tmp;
91                         fclose(f);
92                         f = fopen("/proc/stb/lcd/yres", "r");
93                         if (f)
94                         {
95                                 if (fscanf(f, "%x", &tmp) == 1)
96                                         yres = tmp;
97                                 fclose(f);
98                                 f = fopen("/proc/stb/lcd/bpp", "r");
99                                 if (f)
100                                 {
101                                         if (fscanf(f, "%x", &tmp) == 1)
102                                                 bpp = tmp;
103                                         fclose(f);
104                                 }
105                         }
106                         is_oled = 3;
107                 }
108         }
109 #endif
110         instance=this;
111
112         setSize(xres, yres, bpp);
113 }
114
115 void eDBoxLCD::setInverted(unsigned char inv)
116 {
117         inverted=inv;
118         update();
119 }
120
121 int eDBoxLCD::setLCDContrast(int contrast)
122 {
123 #ifndef NO_LCD
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         close(fp);
136 #endif
137         return(0);
138 }
139
140 int eDBoxLCD::setLCDBrightness(int brightness)
141 {
142 #ifndef NO_LCD
143         eDebug("setLCDBrightness %d", brightness);
144         FILE *f=fopen("/proc/stb/lcd/oled_brightness", "w");
145         if (!f)
146                 f = fopen("/proc/stb/fp/oled_brightness", "w");
147         if (f)
148         {
149                 if (fprintf(f, "%d", brightness) == 0)
150                         eDebug("write /proc/stb/lcd/oled_brightness failed!! (%m)");
151                 fclose(f);
152         }
153         else
154         {
155                 int fp;
156                 if((fp=open("/dev/dbox/fp0", O_RDWR))<=0)
157                 {
158                         eDebug("[LCD] can't open /dev/dbox/fp0");
159                         return(-1);
160                 }
161
162                 if(ioctl(fp, FP_IOCTL_LCD_DIMM, &brightness)<=0)
163                         eDebug("[LCD] can't set lcd brightness (%m)");
164                 close(fp);
165         }
166 #endif
167         return(0);
168 }
169
170 eDBoxLCD::~eDBoxLCD()
171 {
172         if (lcdfd>=0)
173         {
174                 close(lcdfd);
175                 lcdfd=-1;
176         }
177 }
178
179 eDBoxLCD *eDBoxLCD::getInstance()
180 {
181         return instance;
182 }
183
184 void eDBoxLCD::update()
185 {
186         if (lcdfd >= 0)
187         {
188                 if (!is_oled || is_oled == 2)
189                 {
190                         unsigned char raw[132*8];
191                         int x, y, yy;
192                         for (y=0; y<8; y++)
193                         {
194                                 for (x=0; x<132; x++)
195                                 {
196                                         int pix=0;
197                                         for (yy=0; yy<8; yy++)
198                                         {
199                                                 pix|=(_buffer[(y*8+yy)*132+x]>=108)<<yy;
200                                         }
201                                         raw[y*132+x]=(pix^inverted);
202                                 }
203                         }
204                         write(lcdfd, raw, 132*8);
205                 }
206                 else if (is_oled == 3)
207                         write(lcdfd, _buffer, _stride * res.height());
208                 else
209                 {
210                         unsigned char raw[64*64];
211                         int x, y;
212                         memset(raw, 0, 64*64);
213                         for (y=0; y<64; y++)
214                         {
215                                 int pix=0;
216                                 for (x=0; x<128 / 2; x++)
217                                 {
218                                         pix = (_buffer[y*132 + x * 2 + 2] & 0xF0) |(_buffer[y*132 + x * 2 + 1 + 2] >> 4);
219                                         if (inverted)
220                                                 pix = 0xFF - pix;
221                                         raw[y*64+x] = pix;
222                                 }
223                         }
224                         write(lcdfd, raw, 64*64);
225                 }
226         }
227 }
228