在/ dev(例如/ dev / fb0)中的正确文件上使用
open(),然后使用
mmap()将其映射到内存中.如果您不知道如何使用这些系统调用,联机帮助页将帮助您.
然后,< linux / fb.h>中的某些ioctl()有一些结构和常量.像许多内核头文件一样,您可以轻松学习浏览文件.
特别有趣的是ioctl FBIOGET_VSCREENINFO与struct fb_var_screeninfo.注意这里有xres,yres(resolution)和bits_per_pixel.那么有FBIOGET_FSCREENINFO和struct fb_fix_screeninfo,其中有更多的信息,如类型和line_length.
因此,(x,y)处的像素可能位于mmap_base_address x * bits_per_pixel / 8 y * line_length.像素的确切格式将取决于您通过ioctl检索的结构;这是你的工作,决定如何读/写它们.
已经有一段时间,因为我已经与这样做,所以我有点朦胧更多的细节..
这是一个快速而脏的代码示例,只是为了说明它的完成情况…我还没有测试过.
#include
#include
#include
#include
#include
#include
#include
int main()
{
struct fb_var_screeninfo screen_info;
struct fb_fix_screeninfo fixed_info;
char *buffer = NULL;
size_t buflen;
int fd = -1;
int r = 1;
fd = open("/dev/fb0", O_RDWR);
if (fd >= 0)
{
if (!ioctl(fd, FBIOGET_VSCREENINFO, &screen_info) &&
!ioctl(fd, FBIOGET_FSCREENINFO, &fixed_info))
{
buflen = screen_info.yres_virtual * fixed_info.line_length;
buffer = mmap(NULL,
buflen,
PROT_READ|PROT_WRITE,
MAP_SHARED,
fd,
0);
if (buffer != MAP_FAILED)
{
/*
* TODO: something interesting here.
* "buffer" now points to screen pixels.
* Each individual pixel might be at:
* buffer + x * screen_info.bits_per_pixel/8
* + y * fixed_info.line_length
* Then you can write pixels at locations such as that.
*/
r = 0; /* Indicate success */
}
else
{
perror("mmap");
}
}
else
{
perror("ioctl");
}
}
else
{
perror("open");
}
/*
* Clean up
*/
if (buffer && buffer != MAP_FAILED)
munmap(buffer, buflen);
if (fd >= 0)
close(fd);
return r;
}