gpt4 book ai didi

c++ - 在 Linux (Debian) 中访问文本屏幕内存

转载 作者:太空宇宙 更新时间:2023-11-04 04:00:37 24 4
gpt4 key购买 nike

我正在寻找如何读取文本屏幕上的屏幕内存。基本上我正在编写一个Linux应用程序,它使用扩展字符集(unicode)在屏幕上绘制一个“窗口”。我需要准备好要重写的屏幕区域,以便在“窗口”关闭时可以替换该区域。

谢谢

最佳答案

在控制台中,您可以保存 frame buffer 的内容.

通常可以通过 /dev/fb0 访问,它没有加速,但可靠。

这是一个功能明确的小示例。

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>

#include <sys/mman.h>
#include <sys/ioctl.h>

#include <linux/fb.h>

#define red pixel {0xFF,0,0,0}

typedef struct s_Pixel {
char r;
char g;
char b;
char a;
}pixel;

typedef struct s_Framebuffer {
void* ptr;
int hauteur;
int largeur;
int linelength;
}Framebuffer;

void FB_close ();
void FB_init (Framebuffer *);
void FB_clear (int);
void FB_setPixel(int,int,char,char,char,char);

char *fbp;
int fbfd;
long int screensize;

struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;

void FB_setPixel(int x, int y, char r, char g, char b, char a)
{
char * p = (char*)fbp + (x * 4) + (y * finfo.line_length);
p[0] = r;
p[1] = g;
p[2] = b;
p[3] = a;
}

void FB_close()
{
munmap(fbp, screensize);
close(fbfd);
}

void FB_init(Framebuffer* fb)
{

// Open the file for reading and writing
fbfd = open("/dev/fb0", O_RDWR);

if (fbfd == -1) {
perror("Error: cannot open framebuffer device");
exit(1);
}

// Get fixed screen information
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1) {
perror("Error reading fixed information");
exit(2);
}

// Get variable screen information
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1) {
perror("Error reading variable information");
exit(3);
}

// Figure out the size of the screen in bytes
screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;

// Map the device to memory
fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
if ((int)fbp == -1) {
perror("Error: failed to map framebuffer device to memory");
exit(4);
}

fb->ptr = fbp;
fb->largeur = vinfo.xres;
fb->hauteur = vinfo.yres;
fb->linelength = finfo.line_length;

FB_clear ( 0x0 );

//trace(T1,"%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);
}

void FB_clear(int color)
{
unsigned int x,y;
switch (vinfo.bits_per_pixel)
{
case 32:
memset((int*)fbp,color,screensize);
break;
default:
for (y=0;y<vinfo.yres;y++)
{
for (x=0;x<vinfo.xres;x++)
{
fbp[x * 4 + y *finfo.line_length] = 0x0;
fbp[1 + x * 4 + y * finfo.line_length] = 0x0;
fbp[2 + x * 4 + y * finfo.line_length] = 0x0;
}
}
break;
}

}

如果您想在帧缓冲区上加速渲染,请搜索 KMS .

关于c++ - 在 Linux (Debian) 中访问文本屏幕内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23131943/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com