35 lines
748 B
C++
35 lines
748 B
C++
#include "Screen.h"
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <sys/ioctl.h>
|
|
#include <sys/mman.h>
|
|
#include <unistd.h>
|
|
|
|
Result<Screen> Screen::open()
|
|
{
|
|
int fd = ::open("/dev/fb0", O_RDWR | O_CLOEXEC);
|
|
if (fd < 0) return err(errno);
|
|
|
|
int width = ioctl(fd, FB_GET_WIDTH);
|
|
int height = ioctl(fd, FB_GET_HEIGHT);
|
|
|
|
void* p = mmap(nullptr, width * height * BYTES_PER_PIXEL, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
|
if (p == MAP_FAILED)
|
|
{
|
|
close(fd);
|
|
return err(errno);
|
|
}
|
|
|
|
Screen screen;
|
|
|
|
screen.m_canvas = ui::Canvas::create((u8*)p, width, height);
|
|
screen.m_size = width * height * BYTES_PER_PIXEL;
|
|
|
|
return screen;
|
|
}
|
|
|
|
void Screen::sync()
|
|
{
|
|
msync(m_canvas.ptr, size(), MS_SYNC);
|
|
}
|