- don't draw background for ePixmap
[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 #ifndef FBIO_WAITFORVSYNC
14 #define FBIO_WAITFORVSYNC _IOW('F', 0x20, __u32)
15 #endif
16
17
18 fbClass *fbClass::instance;
19
20 fbClass *fbClass::getInstance()
21 {
22         return instance;
23 }
24
25 fbClass::fbClass(const char *fb)
26 {
27         instance=this;
28         locked=0;
29         available=0;
30         cmap.start=0;
31         cmap.len=256;
32         cmap.red=red;
33         cmap.green=green;
34         cmap.blue=blue;
35         cmap.transp=trans;
36
37         fd=open(fb, O_RDWR);
38         if (fd<0)
39         {
40                 perror(fb);
41                 goto nolfb;
42         }
43         if (ioctl(fd, FBIOGET_VSCREENINFO, &screeninfo)<0)
44         {
45                 perror("FBIOGET_VSCREENINFO");
46                 goto nolfb;
47         }
48         
49         memcpy(&oldscreen, &screeninfo, sizeof(screeninfo));
50
51         fb_fix_screeninfo fix;
52         if (ioctl(fd, FBIOGET_FSCREENINFO, &fix)<0)
53         {
54                 perror("FBIOGET_FSCREENINFO");
55                 goto nolfb;
56         }
57
58         available=fix.smem_len;
59         eDebug("%dk video mem", available/1024);
60         lfb=(unsigned char*)mmap(0, available, PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0);
61         if (!lfb)
62         {
63                 perror("mmap");
64                 goto nolfb;
65         }
66
67         showConsole(0);
68         return;
69 nolfb:
70         lfb=0;
71         printf("framebuffer not available.\n");
72         return;
73 }
74
75 int fbClass::showConsole(int state)
76 {
77         int fd=open("/dev/vc/0", O_RDWR);
78         if(fd>=0)
79         {
80                 if(ioctl(fd, KDSETMODE, state?KD_TEXT:KD_GRAPHICS)<0)
81                 {
82                         eDebug("setting /dev/vc/0 status failed.");
83                 }
84                 close(fd);
85         }
86         return 0;
87 }
88
89 int fbClass::SetMode(unsigned int nxRes, unsigned int nyRes, unsigned int nbpp)
90 {
91         screeninfo.xres_virtual=screeninfo.xres=nxRes;
92         screeninfo.yres_virtual=(screeninfo.yres=nyRes)*2;
93         screeninfo.height=0;
94         screeninfo.width=0;
95         screeninfo.xoffset=screeninfo.yoffset=0;
96         screeninfo.bits_per_pixel=nbpp;
97         if (ioctl(fd, FBIOPUT_VSCREENINFO, &screeninfo)<0)
98         {
99                 perror("FBIOPUT_VSCREENINFO");
100                 printf("fb failed\n");
101                 return -1;
102         }
103         if ((screeninfo.xres!=nxRes) && (screeninfo.yres!=nyRes) && (screeninfo.bits_per_pixel!=nbpp))
104         {
105                 eDebug("SetMode failed: wanted: %dx%dx%d, got %dx%dx%d",
106                         nxRes, nyRes, nbpp,
107                         screeninfo.xres, screeninfo.yres, screeninfo.bits_per_pixel);
108         }
109         xRes=screeninfo.xres;
110         yRes=screeninfo.yres;
111         bpp=screeninfo.bits_per_pixel;
112         fb_fix_screeninfo fix;
113         if (ioctl(fd, FBIOGET_FSCREENINFO, &fix)<0)
114         {
115                 perror("FBIOGET_FSCREENINFO");
116                 printf("fb failed\n");
117         }
118         stride=fix.line_length;
119         memset(lfb, 0, stride*yRes);
120         return 0;
121 }
122
123 int fbClass::setOffset(int off)
124 {
125         screeninfo.xoffset = 0;
126         screeninfo.yoffset = off;
127         return ioctl(fd, FBIOPAN_DISPLAY, &screeninfo);
128 }
129
130 int fbClass::waitVSync()
131 {
132         int c = 0;
133         return ioctl(fd, FBIO_WAITFORVSYNC, &c);
134 }
135
136 fbClass::~fbClass()
137 {
138         if (available)
139                 ioctl(fd, FBIOPUT_VSCREENINFO, &oldscreen);
140         if (lfb)
141                 munmap(lfb, available);
142         showConsole(1);
143 }
144
145 int fbClass::PutCMAP()
146 {
147         return ioctl(fd, FBIOPUTCMAP, &cmap);
148 }
149
150 int fbClass::lock()
151 {
152         if (locked)
153                 return -1;
154         locked=1;
155         return fd;
156 }
157
158 void fbClass::unlock()
159 {
160         if (!locked)
161                 return;
162         locked=0;
163         SetMode(xRes, yRes, bpp);
164         PutCMAP();
165 }