931d41c8067bfec4522b37043a7c8a88b611154c
[vuplus_dvbapp] / 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 #ifdef SET_RIGHT_HALF_VFD_SKIN
111         fprintf(stdout,"SET RIGHT HALF VFD SKIN\n");
112         FILE *f = fopen("/proc/stb/lcd/right_half", "w");
113         fprintf(f,"skin");
114         fclose(f);
115 #endif
116         instance=this;
117
118         setSize(xres, yres, bpp);
119 }
120
121 void eDBoxLCD::setInverted(unsigned char inv)
122 {
123         inverted=inv;
124         update();
125 }
126
127 int eDBoxLCD::setLED(int value, int option)
128 {
129         switch(option)
130         {
131                 case LED_BRIGHTNESS:
132                         ioctl(lcdfd, LED_IOCTL_BRIGHTNESS_NORMAL, (unsigned char)value);
133                         break;
134                 case LED_DEEPSTANDBY:
135                         ioctl(lcdfd, LED_IOCTL_BRIGHTNESS_DEEPSTANDBY, (unsigned char)value);
136                         break;
137                 case LED_BLINKINGTIME:
138                         ioctl(lcdfd, LED_IOCTL_BLINKING_TIME, (unsigned char)value);
139                         break;
140         }
141         return 0;
142 }
143
144 int eDBoxLCD::setLCDContrast(int contrast)
145 {
146 #ifndef NO_LCD
147         int fp;
148         if((fp=open("/dev/dbox/fp0", O_RDWR))<=0)
149         {
150                 eDebug("[LCD] can't open /dev/dbox/fp0");
151                 return(-1);
152         }
153
154         if(ioctl(lcdfd, LCD_IOCTL_SRV, &contrast))
155         {
156                 eDebug("[LCD] can't set lcd contrast");
157         }
158         close(fp);
159 #endif
160         return(0);
161 }
162
163 int eDBoxLCD::setLCDBrightness(int brightness)
164 {
165 #ifndef NO_LCD
166         eDebug("setLCDBrightness %d", brightness);
167         FILE *f=fopen("/proc/stb/lcd/oled_brightness", "w");
168         if (!f)
169                 f = fopen("/proc/stb/fp/oled_brightness", "w");
170         if (f)
171         {
172                 if (fprintf(f, "%d", brightness) == 0)
173                         eDebug("write /proc/stb/lcd/oled_brightness failed!! (%m)");
174                 fclose(f);
175         }
176         else
177         {
178                 int fp;
179                 if((fp=open("/dev/dbox/fp0", O_RDWR))<=0)
180                 {
181                         eDebug("[LCD] can't open /dev/dbox/fp0");
182                         return(-1);
183                 }
184
185                 if(ioctl(fp, FP_IOCTL_LCD_DIMM, &brightness)<=0)
186                         eDebug("[LCD] can't set lcd brightness (%m)");
187                 close(fp);
188         }
189 #endif
190         return(0);
191 }
192
193 eDBoxLCD::~eDBoxLCD()
194 {
195         if (lcdfd>=0)
196         {
197                 close(lcdfd);
198                 lcdfd=-1;
199         }
200 }
201
202 eDBoxLCD *eDBoxLCD::getInstance()
203 {
204         return instance;
205 }
206
207 void eDBoxLCD::update()
208 {
209 #ifdef BUILD_VUPLUS /* ikseong  */
210         return ;
211 #endif
212         if (lcdfd >= 0)
213         {
214                 if (!is_oled || is_oled == 2)
215                 {
216                         unsigned char raw[132*8];
217                         int x, y, yy;
218                         for (y=0; y<8; y++)
219                         {
220                                 for (x=0; x<132; x++)
221                                 {
222                                         int pix=0;
223                                         for (yy=0; yy<8; yy++)
224                                         {
225                                                 pix|=(_buffer[(y*8+yy)*132+x]>=108)<<yy;
226                                         }
227                                         raw[y*132+x]=(pix^inverted);
228                                 }
229                         }
230                         write(lcdfd, raw, 132*8);
231                 }
232                 else if (is_oled == 3)
233                         write(lcdfd, _buffer, _stride * res.height());
234                 else
235                 {
236                         unsigned char raw[64*64];
237                         int x, y;
238                         memset(raw, 0, 64*64);
239                         for (y=0; y<64; y++)
240                         {
241                                 int pix=0;
242                                 for (x=0; x<128 / 2; x++)
243                                 {
244                                         pix = (_buffer[y*132 + x * 2 + 2] & 0xF0) |(_buffer[y*132 + x * 2 + 1 + 2] >> 4);
245                                         if (inverted)
246                                                 pix = 0xFF - pix;
247                                         raw[y*64+x] = pix;
248                                 }
249                         }
250                         write(lcdfd, raw, 64*64);
251                 }
252         }
253 }
254