import of enigma2
[enigma2.git] / lib / gdi / fb.cpp
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <fcntl.h>
4 #include <sys/ioctl.h>
5 #include <unistd.h>
6 #include <sys/mman.h>
7 #include <memory.h>
8 #include <linux/kd.h>
9
10 #include <lib/base/econfig.h>
11 #include <lib/gdi/fb.h>
12
13 fbClass *fbClass::instance;
14
15 fbClass *fbClass::getInstance()
16 {
17         return instance;
18 }
19
20 fbClass::fbClass(const char *fb)
21 {
22         instance=this;
23         locked=0;
24         available=0;
25         cmap.start=0;
26         cmap.len=256;
27         cmap.red=red;
28         cmap.green=green;
29         cmap.blue=blue;
30         cmap.transp=trans;
31
32         int state=0;
33         eConfig::getInstance()->getKey("/ezap/osd/showConsoleOnFB", state);
34
35         fd=open(fb, O_RDWR);
36         if (fd<0)
37         {
38                 perror(fb);
39                 goto nolfb;
40         }
41         if (ioctl(fd, FBIOGET_VSCREENINFO, &screeninfo)<0)
42         {
43                 perror("FBIOGET_VSCREENINFO");
44                 goto nolfb;
45         }
46         
47         memcpy(&oldscreen, &screeninfo, sizeof(screeninfo));
48
49         fb_fix_screeninfo fix;
50         if (ioctl(fd, FBIOGET_FSCREENINFO, &fix)<0)
51         {
52                 perror("FBIOGET_FSCREENINFO");
53                 goto nolfb;
54         }
55
56         available=fix.smem_len;
57         eDebug("%dk video mem", available/1024);
58         lfb=(unsigned char*)mmap(0, available, PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0);
59         if (!lfb)
60         {
61                 perror("mmap");
62                 goto nolfb;
63         }
64
65         showConsole(state);
66         return;
67 nolfb:
68         lfb=0;
69         printf("framebuffer not available.\n");
70         return;
71 }
72
73 int fbClass::showConsole(int state)
74 {
75         int fd=open("/dev/vc/0", O_RDWR);
76         if(fd>=0)
77         {
78                 if(ioctl(fd, KDSETMODE, state?KD_TEXT:KD_GRAPHICS)<0)
79                 {
80                         eDebug("setting /dev/vc/0 status failed.");
81                 }
82                 close(fd);
83         }
84         return 0;
85 }
86
87 int fbClass::SetMode(unsigned int nxRes, unsigned int nyRes, unsigned int nbpp)
88 {
89         screeninfo.xres_virtual=screeninfo.xres=nxRes;
90         screeninfo.yres_virtual=screeninfo.yres=nyRes;
91         screeninfo.xoffset=screeninfo.yoffset=0;
92         screeninfo.bits_per_pixel=nbpp;
93         if (ioctl(fd, FBIOPUT_VSCREENINFO, &screeninfo)<0)
94         {
95                 perror("FBIOPUT_VSCREENINFO");
96                 printf("fb failed\n");
97                 return -1;
98         }
99         if ((screeninfo.xres!=nxRes) && (screeninfo.yres!=nyRes) && (screeninfo.bits_per_pixel!=nbpp))
100         {
101                 eDebug("SetMode failed: wanted: %dx%dx%d, got %dx%dx%d",
102                         nxRes, nyRes, nbpp,
103                         screeninfo.xres, screeninfo.yres, screeninfo.bits_per_pixel);
104         }
105         xRes=screeninfo.xres;
106         yRes=screeninfo.yres;
107         bpp=screeninfo.bits_per_pixel;
108         fb_fix_screeninfo fix;
109         if (ioctl(fd, FBIOGET_FSCREENINFO, &fix)<0)
110         {
111                 perror("FBIOGET_FSCREENINFO");
112                 printf("fb failed\n");
113         }
114         stride=fix.line_length;
115         memset(lfb, 0, stride*yRes);
116         return 0;
117 }
118
119 fbClass::~fbClass()
120 {
121         if (available)
122                 ioctl(fd, FBIOPUT_VSCREENINFO, &oldscreen);
123         if (lfb)
124                 munmap(lfb, available);
125 }
126
127 int fbClass::PutCMAP()
128 {
129         return ioctl(fd, FBIOPUTCMAP, &cmap);
130 }
131
132 void fbClass::Box(int x, int y, int width, int height, int color, int backcolor)
133 {
134         if (width<=2)
135                 return;
136         int offset=y*stride+x/2;
137         int first=0xF0|((color&0xF0)>>4);
138         int last= 0xF0|((backcolor&0xF0)>>4);
139         color=(color&0xF)*0x11;
140         int halfwidth=width/2;
141         for (int ay=y; ay<(y+height); ay++)
142         {
143                 lfb[offset]=first;
144                 memset(lfb+offset+1, color, halfwidth-2);
145                 lfb[offset+halfwidth-1]=last;
146                 offset+=stride;
147         }
148 }
149
150 void fbClass::NBox(int x, int y, int width, int height, int color)
151 {
152         int offset=y*stride+x/2;
153         int halfwidth=width/2;
154         for (int ay=y; ay<(y+height); ay++)
155         {
156                 memset(lfb+offset, color, halfwidth);
157                 offset+=stride;
158         }
159 }
160
161 void fbClass::VLine(int x, int y, int sy, int color)
162 {
163         int offset=y*stride+x/2;
164         while (sy--)
165         {
166                 lfb[offset]=color;
167                 offset+=stride;
168         }
169 }
170
171 int fbClass::lock()
172 {
173         if (locked)
174                 return -1;
175         locked=1;
176         return fd;
177 }
178
179 void fbClass::unlock()
180 {
181         if (!locked)
182                 return;
183         locked=0;
184         SetMode(xRes, yRes, bpp);
185         PutCMAP();
186 }