yes! ich habs kaputt gemacht! (doesn't compile anymore, doesn't work anymore,
[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.height=0;
92         screeninfo.width=0;
93         screeninfo.xoffset=screeninfo.yoffset=0;
94         screeninfo.bits_per_pixel=nbpp;
95         if (ioctl(fd, FBIOPUT_VSCREENINFO, &screeninfo)<0)
96         {
97                 perror("FBIOPUT_VSCREENINFO");
98                 printf("fb failed\n");
99                 return -1;
100         }
101         if ((screeninfo.xres!=nxRes) && (screeninfo.yres!=nyRes) && (screeninfo.bits_per_pixel!=nbpp))
102         {
103                 eDebug("SetMode failed: wanted: %dx%dx%d, got %dx%dx%d",
104                         nxRes, nyRes, nbpp,
105                         screeninfo.xres, screeninfo.yres, screeninfo.bits_per_pixel);
106         }
107         xRes=screeninfo.xres;
108         yRes=screeninfo.yres;
109         bpp=screeninfo.bits_per_pixel;
110         fb_fix_screeninfo fix;
111         if (ioctl(fd, FBIOGET_FSCREENINFO, &fix)<0)
112         {
113                 perror("FBIOGET_FSCREENINFO");
114                 printf("fb failed\n");
115         }
116         stride=fix.line_length;
117         memset(lfb, 0, stride*yRes);
118         return 0;
119 }
120
121 fbClass::~fbClass()
122 {
123         if (available)
124                 ioctl(fd, FBIOPUT_VSCREENINFO, &oldscreen);
125         if (lfb)
126                 munmap(lfb, available);
127 }
128
129 int fbClass::PutCMAP()
130 {
131         return ioctl(fd, FBIOPUTCMAP, &cmap);
132 }
133
134 void fbClass::Box(int x, int y, int width, int height, int color, int backcolor)
135 {
136         if (width<=2 || locked)
137                 return;
138         int offset=y*stride+x/2;
139         int first=0xF0|((color&0xF0)>>4);
140         int last= 0xF0|((backcolor&0xF0)>>4);
141         color=(color&0xF)*0x11;
142         int halfwidth=width/2;
143         for (int ay=y; ay<(y+height); ay++)
144         {
145                 lfb[offset]=first;
146                 memset(lfb+offset+1, color, halfwidth-2);
147                 lfb[offset+halfwidth-1]=last;
148                 offset+=stride;
149         }
150 }
151
152 void fbClass::NBox(int x, int y, int width, int height, int color)
153 {
154         if (locked)
155                 return;
156         int offset=y*stride+x/2;
157         int halfwidth=width/2;
158         for (int ay=y; ay<(y+height); ay++)
159         {
160                 memset(lfb+offset, color, halfwidth);
161                 offset+=stride;
162         }
163 }
164
165 void fbClass::VLine(int x, int y, int sy, int color)
166 {
167         if (locked)
168                 return;
169         int offset=y*stride+x/2;
170         while (sy--)
171         {
172                 lfb[offset]=color;
173                 offset+=stride;
174         }
175 }
176
177 int fbClass::lock()
178 {
179         if (locked)
180                 return -1;
181         locked=1;
182         return fd;
183 }
184
185 void fbClass::unlock()
186 {
187         if (!locked)
188                 return;
189         locked=0;
190         SetMode(xRes, yRes, bpp);
191         PutCMAP();
192 }