fix possible segfault (on refcount wrap)
[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         locked=0;
20         _buffer=new unsigned char[res.height()*res.width()];
21         _stride=res.width();
22 }
23
24 eLCD::~eLCD()
25 {
26         delete [] _buffer;
27 }
28
29 int eLCD::lock()
30 {
31         if (locked)
32                 return -1;
33
34         locked=1;
35         return lcdfd;
36 }
37
38 void eLCD::unlock()
39 {
40         read( lcdfd, NULL, 0);
41         if ( errno == 9 )
42         {
43                 eDebug("reopen lcd");
44                 lcdfd=open("/dev/dbox/lcd0", O_RDWR);  // reopen device
45         }
46         else
47                 eDebug("do not reopen lcd.. errno = %d", errno);
48     
49         locked=0;
50 }
51
52 eDBoxLCD::eDBoxLCD(): eLCD(eSize(132, 64))
53 {
54 #ifndef NO_LCD
55         lcdfd = open("/dev/dbox/oled0", O_RDWR);
56         if (lcdfd < 0)
57         {
58                 lcdfd = open("/dev/dbox/lcd0", O_RDWR);
59                 is_oled = 0;
60         } else
61         {
62                 eDebug("found OLED display!");
63                 is_oled = 1;
64         }
65 #else
66         lcdfd = -1;
67 #endif
68         instance=this;
69
70         if (lcdfd<0)
71                 eDebug("couldn't open LCD - load lcd.o!");
72         else
73         {
74                 int i=LCD_MODE_BIN;
75                 ioctl(lcdfd, LCD_IOCTL_ASC_MODE, &i);
76                 inverted=0;
77         }
78 }
79
80 void eDBoxLCD::setInverted(unsigned char inv)
81 {
82         inverted=inv;
83         update();       
84 }
85
86 int eDBoxLCD::setLCDContrast(int contrast)
87 {
88         int fp;
89         if((fp=open("/dev/dbox/fp0", O_RDWR))<=0)
90         {
91                 eDebug("[LCD] can't open /dev/dbox/fp0");
92                 return(-1);
93         }
94
95         if(ioctl(lcdfd, LCD_IOCTL_SRV, &contrast))
96         {
97                 eDebug("[LCD] can't set lcd contrast");
98         }
99         close(fp);
100         return(0);
101 }
102
103 int eDBoxLCD::setLCDBrightness(int brightness)
104 {
105         int fp;
106         if((fp=open("/dev/dbox/fp0", O_RDWR))<=0)
107         {
108                 eDebug("[LCD] can't open /dev/dbox/fp0");
109                 return(-1);
110         }
111
112         if(ioctl(fp, FP_IOCTL_LCD_DIMM, &brightness))
113         {
114                 eDebug("[LCD] can't set lcd brightness");
115         }
116         close(fp);
117         return(0);
118 }
119
120 eDBoxLCD::~eDBoxLCD()
121 {
122         if (lcdfd>0)
123         {
124                 close(lcdfd);
125                 lcdfd=0;
126         }
127 }
128
129 eDBoxLCD *eDBoxLCD::getInstance()
130 {
131         return instance;
132 }
133
134 void eDBoxLCD::update()
135 {
136         if (!is_oled)
137         {
138                 unsigned char raw[132*8];
139                 int x, y, yy;
140                 for (y=0; y<8; y++)
141                 {
142                         for (x=0; x<132; x++)
143                         {
144                                 int pix=0;
145                                 for (yy=0; yy<8; yy++)
146                                 {
147                                         pix|=(_buffer[(y*8+yy)*132+x]>=108)<<yy;
148                                 }
149                                 raw[y*132+x]=(pix^inverted);
150                         }
151                 }
152                 if (lcdfd>0)
153                         write(lcdfd, raw, 132*8);
154         } else
155         {
156                 unsigned char raw[64*64];
157                 int x, y;
158                 memset(raw, 0, 64*64);
159                 for (y=0; y<64; y++)
160                 {
161                         for (x=0; x<128 / 2; x++)
162                                 raw[y*64+x] = (_buffer[y*132 + x * 2 + 2] & 0xF0) |(_buffer[y*132 + x * 2 + 1 + 2] >> 4);
163                 }
164                 if (lcdfd > 0)
165                         write(lcdfd, raw, 64*64);
166         }
167 }
168