返回值说明:
成功执行时,mmap()返回被映射区的指针。失败时,mmap()返回MAP_FAILED[其值为(void *)-1]。
int munmap(void *start, size_t length);
取消参数start所指向的映射内存,参数length表示欲取消的内存大小。
返回值:解除成功返回0,否则返回-1,错误原因存于errno中。
一个简单的实例,实现对framebuffer的操作:
- #include <stdio.h>
- #include <sys/mman.h>
- #include <string.h>
- #include <linux/fb.h>
- #include <sys/ioctl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #ifndef true
- #define true 1
- #endif
- #ifndef false
- #define false 0
- #endif
- int FB_Init()
- {
- fb = open("/dev/fb0", O_RDWR);
- if(fb < 0)
- {
- fprintf(stderr, "Open Failed\n");
- return false;
- }
- if(ioctl(fb, FBIOGET_FSCREENINFO, &f_info) < 0)
- {
- fprintf(stderr, "Get FSCREENINFO Failed\n");
- return false;
- }
- if(ioctl(fb, FBIOGET_VSCREENINFO, &v_info) < 0)
- {
- fprintf(stderr, "Get VSCREENINFO Failed\n");
- return false;
- }
- screen_size = v_info.xres * v_info.yres * (v_info.bits_per_pixel) / 8;
- fbuffer = mmap(0, screen_size, PROT_READ | PROT_WRITE, MAP_SHARED, fb, 0);
- if((int)fbuffer == -1)
- {
- fprintf(stderr, "Mmap error\n");
- return false;
- }
- }
- int FB_Close()
- {
- munmap(fbuffer, screen_size);
- close(fb);
- return true;
- }
- int point(int x,int y, ColorType color)
- {
- int temp;
- void *currPoint;
- if(x < 0 || x >= v_info.width)
- return false;
- if(y < 0 || y >= v_info.height)
- return false;
- temp = (x + v_info.xoffset) * (v_info.bits_per_pixel/8) +
- (y + v_info.yoffset) * f_info.line_length;
- *((unsigned short *)(fbuffer + temp)) = color;
- return true;
- }
- int main(int argc, char *argv[])
- {
- FB_Init();
- point(100, 100, 0x19ff1d00);
- FB_Close();
- return 0;
- }
编译运行后就在我们的framebuffer上的坐标为(100, 100)的地方画出一个点。
