二、Framebuffer的应用开发
1. 基于/lib/fb*设备和mmap
这种方法灵活性差,开发比较费时。
这里有个示例代码,代码如下:
- #include <stdlib.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <fcntl.h>
- #include <linux/fb.h>
- #include <sys/mman.h>
- int main()
- {
- int fbfd = 0;
- struct fb_var_screeninfo vinfo;
- struct fb_fix_screeninfo finfo;
- long int screensize = 0;
- char *fbp = 0;
- int x = 0, y = 0;
- long int location = 0;
- fbfd = open("/dev/fb0", O_RDWR);
- if (!fbfd) {
- printf("Error: cannot open framebuffer device.\n");
- exit(1);
- }
- printf("The framebuffer device was opened successfully.\n");
- if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) {
- printf("Error reading fixed information.\n");
- exit(2);
- }
- if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) {
- printf("Error reading variable information.\n");
- exit(3);
- }
- printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel );
- screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
- fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,
- fbfd, 0);
- if ((int)fbp == -1) {
- printf("Error: failed to map framebuffer device to memory.\n");
- exit(4);
- }
- printf("The framebuffer device was mapped to memory successfully.\n");
- x = 100;
- y = 100;
- for ( y = 100; y < 300; y++ )
- for ( x = 100; x < 300; x++ ) {
- location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
- (y+vinfo.yoffset) * finfo.line_length;
- if ( vinfo.bits_per_pixel == 32 ) {
- *(fbp + location) = 100;
- *(fbp + location + 1) = 15+(x-100)/2;
- *(fbp + location + 2) = 200-(y-100)/5;
- *(fbp + location + 3) = 0;
- } else {
- int b = 10;
- int g = (x-100)/6;
- int r = 31-(y-100)/16;
- unsigned short int t = r<<11 | g << 5 | b;
- *((unsigned short int*)(fbp + location)) = t;
- }
- }
- munmap(fbp, screensize);
- close(fbfd);
- return 0;
- }
(已经在Ubuntu和qemu上测试过)
2. 基于DirectFB
DirectFB好像可以加快Framebuffer的绘画速度。
官方有5个示例代码
http://directfb.org/index.php?path=Development%2FTutorials
DirectFB有支持字体、图片和输入插件,
不过在编译前需要事先安装相关的开发库。
(已经在Ubuntu和qemu上测试过)
3. 基于SDL
SDL支持Framebuffer,因为它底层使用了DirectFB。
Ubuntu的SDL可以在Framebuffer下使用,如果自己编译SDL的代码,
需要事先编译DirectFB
(已经在Ubuntu和qemu上测试过)
4. 基于GTK+2
项目主页在:
http://www.gtk.org/
GTK+2通过Cairo库的DirectFB后端支持Framebuffer。
而且gtk库在编译时需要填加特定参数
./configure --prefix=$PREFIX --with-gdktarget=directfb --without-x
然后进行编译。
不过GTK+2的编译比较麻烦,而且directfb上GTK+2的开发代码尚不稳定。
测试的可执行文件在/bin/gtk-demo
(在Ubuntu上跑似乎有问题——不知道怎么拖动窗口和退出;未在qemu上测试)
5. 基于Qt
下载在
ftp://ftp.qt.nokia.com/qt/source/
一般需要自己编译
$ tar xzf qt-everywhere-opensource-src-4.8.0-tp.tar.gz
$ cd qt-everywhere-opensource-src-4.8.0-tp/
$ ./configure -shared -static -opensource -embedded generic
虽然-embedded这个参数没有出现在./configure --help里,
但它在这里是有效的。这里不需要添加--prefix参数
$ sudo make -j 4
$ sudo make install
默认装在/usr/local/Trolltech目录下。
示例代码和编译的二进制文件在demos/embedded目录下。
进入framebuffer下的文本模式,
然后运行sudo ./xxx -qws
运行时如果提示:
Qt/Embedded data directory is not owned by user 0:/tmp/qtembedded-0
可以运行:
sudo chown root:root /tmp/qtembedded-0
如果要在qemu上跑,需要复制/lib/fonts目录下的字体
(可以在Ubuntu上跑,在qemu上跑似乎有点问题——未解决)
