File: fbdev.cpp

package info (click to toggle)
libucimf 2.3.8-13
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,004 kB
  • sloc: sh: 10,860; cpp: 3,766; ansic: 789; makefile: 67
file content (301 lines) | stat: -rw-r--r-- 8,809 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 * UCIMF - Unicode Console InputMethod Framework                                                    
 *
 * Copyright (c) 2007 Chun-Yu Lee (Mat) and Open RazzmatazZ Laboratory (OrzLab)
 *
 * Parts taken from zhcon project.
 * Copyright (c) 2001 ejoy <ejoy@users.sourceforge.net>
 *                    huyong <ccpaging@online.sh.cn>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <assert.h>

#include "fbdev.h"
#include "fblinear4.h"
#include "fblinear8.h"
#include "fblinear15.h"
#include "fblinear16.h"
#include "fblinear24.h"
#include "fblinear32.h"
#ifdef USING_VGA
#include "fbvgaplanes.h"
#endif

// mmap framebuffer address
char *FBDev::mpBuf = NULL;
int FBDev::mpBufLen = 0;
// FrameBuffer file handle
int FBDev::mFd = -1;
unsigned long FBDev::mNextLine = 0;
unsigned long FBDev::mNextPlane = 0;
int FBDev::mCurrentMode = 0;
//colormap
__u16 FBDev::red16[] = {
0x0000, 0x0000, 0x0000, 0x0000, 0xaaaa, 0xaaaa, 0xaaaa, 0xaaaa,
0x5555, 0x5555, 0x5555, 0x5555, 0xffff, 0xffff, 0xffff, 0xffff
};
__u16 FBDev::green16[] = {
0x0000, 0x0000, 0xaaaa, 0xaaaa, 0x0000, 0x0000, 0x5555, 0xaaaa,
0x5555, 0x5555, 0xffff, 0xffff, 0x5555, 0x5555, 0xffff, 0xffff
};
__u16 FBDev::blue16[] = {
0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa, 0x0000, 0xaaaa,
0x5555, 0xffff, 0x5555, 0xffff, 0x5555, 0xffff, 0x5555, 0xffff
};

#if defined(linux)
    #include <linux/fb.h>
    #define MAX_DEV_LEN 63
#endif
OPEN_RC FBDev::TryOpen() {
    OPEN_RC rc = NORMAL;
#if defined(linux)
    char devname[MAX_DEV_LEN+1];
    int fbNo;
    // Find a framebuffer to open
    for (fbNo=0; fbNo < 32; fbNo++) {
        sprintf(devname, "/dev/fb%d", fbNo);
        mFd = open(devname, O_RDWR);
        if (mFd >= 0) break;
        sprintf(devname, "/dev/fb/%d", fbNo);
        mFd = open(devname, O_RDWR);
        if (mFd >= 0) break;
    }

    if (fbNo >= 32) {
        return FAILURE;
    }

    rc = NORMAL;
    static struct fb_fix_screeninfo Finfo;
    static struct fb_var_screeninfo Vinfo;
    ioctl(mFd, FBIOGET_FSCREENINFO, &Finfo);
    ioctl(mFd, FBIOGET_VSCREENINFO, &Vinfo);
    switch (Finfo.type) {
        case FB_TYPE_PACKED_PIXELS:      // truecolor packed pixels
           rc = LinearSet(Vinfo);
           break;
        case FB_TYPE_PLANES:             // Non interleaved planes
           // throw(runtime_error("Can not support FB_TYPE, planes not interleaved.\n"));
           rc = UNSUPPORT;
           break;
        case FB_TYPE_INTERLEAVED_PLANES: // Interleaved planes
           // throw(runtime_error("Can not support FB_TYPE, planes interleaved.\n"));
           rc = UNSUPPORT;
           break;
        case FB_TYPE_TEXT:               // Text/attributes
           // throw(runtime_error("Can not support FB_TYPE, VGA text mode.\n"));
           rc = UNSUPPORT;
           break;
#ifdef USING_VGA
        case FB_TYPE_VGA_PLANES:         // EGA/VGA planes
           VGAPlaneSet();
           // throw(runtime_error("Can not support FB_TYPE, EGA/VGA mode.\n"));
           break;
#endif
        default:
           // throw(runtime_error("Unknow FrameBuffer type.\n"));
           rc = UNSUPPORT;
           break;
    }
    if (rc != NORMAL) {
        close(mFd);
        return rc;
    }

    mXres=Finfo.line_length/(Vinfo.bits_per_pixel/8);
    mYres = Vinfo.yres;
    mpBufLen = Finfo.smem_len;
    mpBuf = (char*)mmap(0,mpBufLen,PROT_READ | PROT_WRITE, MAP_SHARED, mFd, 0);
    if (mpBuf == MAP_FAILED)
        //throw(runtime_error("fb device mmap failed!"));
    mNextLine = Finfo.line_length;
#endif
    return rc;
}

OPEN_RC FBDev::LinearSet( struct fb_var_screeninfo &Vinfo ) {
    OPEN_RC rc = NORMAL;
#if defined(linux)
    switch (Vinfo.bits_per_pixel) {
        //case 4:
            //mpGraphDev = new FBLinear4;
            //break;
        case 8:
            mpGraphDev = new FBLinear8;
            break;
        case 15:
            mpGraphDev = new FBLinear15;
            break;
         case 16:
            if (Vinfo.green.length == 5)
                mpGraphDev = new FBLinear15;
            else
                mpGraphDev = new FBLinear16;
            break;
         case 24:
            mpGraphDev = new FBLinear24;
            break;
         case 32:
            mpGraphDev = new FBLinear32;
            break;
         default:
            rc = UNSUPPORT;
            //throw(runtime_error("4bpp color depth unsupported in this version\n"
            //                    "use 8bpp, 16bpp, 24bpp, 32bpp instead!"));
            break;
    }
#endif
    return rc;
}

// may EGA are included
#ifdef USING_VGA
void FBDev::VGAPlaneSet( void ) {
    mpGraphDev = new FBVgaPlanes;
}
#endif

#if defined(__FreeBSD__)
    #include <sys/types.h>
    #include <sys/ioctl.h>
    //#include <machine/console.h>
    #include <sys/consio.h>
    #include <sys/fbio.h>
    #include <machine/sysarch.h>
#endif
bool FBDev::TryOpen(int xres, int yres, int depth) {
#if defined(__FreeBSD__)
    video_info_t mode = {0};
    mode.vi_width = xres;
    mode.vi_height = yres;
    mode.vi_depth = depth;
    if (ioctl(0, FBIO_FINDMODE, &mode)) {
        return false;
        throw(runtime_error("Cannot find graphics mode. Check your zhcon.conf please.\n"));
    }
    if ((mode.vi_mode >= M_B40x25) && (mode.vi_mode <= M_VGA_M90x60))
        mCurrentMode = _IO('S', mode.vi_mode);
    if ((mode.vi_mode >= M_TEXT_80x25) && (mode.vi_mode <= M_TEXT_132x60))
        mCurrentMode = _IO('S', mode.vi_mode);
    if ((mode.vi_mode >= M_VESA_CG640x400) && (mode.vi_mode <= M_VESA_FULL_1280))
        mCurrentMode = _IO('V', mode.vi_mode - M_VESA_BASE);

    // FreeBSD mode query bug workaround -- Rick
    if (depth == 4 && mCurrentMode == SW_VGA11)
        mCurrentMode = SW_VGA12;

    if (ioctl(0, mCurrentMode, 0) != 0) {
        throw(runtime_error("Cannot switch to graphics mode. Refer to README/FAQ"
                            " and check your zhcon.cfg please!\n"));
    }

    mXres = mode.vi_width;
    mYres = mode.vi_height;
    mpBufLen = mode.vi_buffer_size;
    if (mode.vi_mem_model == V_INFO_MM_PLANAR) {
        mpBufLen = mode.vi_width * mode.vi_height / 8;
    }

    // printf ("len %d\n", mpBufLen);
    // use /dev/mem crash in Virtual PC
//#if (__FreeBSD__ <= 3)
    if ((mFd = open("/dev/vga", O_RDWR | O_NDELAY)) < 0) {
//#else
//    if ((mFd = open("/dev/mem", O_RDWR | O_NDELAY)) < 0) {
//#endif
        throw(runtime_error("Can not open vga device.\n"));
    }

    switch (depth) {
        case 4:
#ifdef USING_VGA
            mpGraphDev = new FBVgaPlanes;
#else
            throw(runtime_error("color depth unsupported in this version\n"
                "use 8bpp instead!"));
#endif
            break;
        case 8:
            mpGraphDev = new FBLinear8;
            break;
        case 15:
            mpGraphDev = new FBLinear15;
            break;
        case 16:
            mpGraphDev = new FBLinear16;
            break;
        case 24:
            mpGraphDev = new FBLinear24;
            break;
        case 32:
            mpGraphDev = new FBLinear32;
            break;
        default:
            throw(runtime_error("color depth unsupported in this version\n"
                "use 8bpp instead!"));
    }
//#if (__FreeBSD__ <= 3)
#define GRAPH_BASE 0x0
//#else
//#define GRAPH_BASE 0xA0000
//#endif
    mpBuf = static_cast<char *>(mmap(0, mpBufLen, PROT_READ | PROT_WRITE,
            MAP_FILE|MAP_SHARED|MAP_FIXED, mFd, GRAPH_BASE));

    if (mpBuf == MAP_FAILED)
        throw(runtime_error("mmap() failed!"));

    // undefine mNextLine, checked by low level
#endif
    return true;
}

FBDev::~FBDev() {
    if (mpBuf != NULL)
        munmap(mpBuf, mpBufLen);
    if (mFd >= 0)
        close(mFd);
#if defined(__FreeBSD__)
    ioctl(0, SW_TEXT_80x25, 0);
#endif
}

void FBDev::SwitchToGraph() {
#if defined(__FreeBSD__)
    if (mCurrentMode == 0)
        return;
    ioctl(0, mCurrentMode, 0);
#endif
}

void FBDev::SwitchToText() {
#if defined(__FreeBSD__)
    ioctl(0, SW_TEXT_80x25, 0);
#endif
}