101 lines
2.0 KiB
C++
101 lines
2.0 KiB
C++
|
#include "framebuffer.hpp"
|
||
|
#include <string>
|
||
|
#include <sys/types.h>
|
||
|
#include <sys/stat.h>
|
||
|
#include <sys/mman.h>
|
||
|
#include <sys/ioctl.h>
|
||
|
#include <fcntl.h>
|
||
|
#include <linux/fb.h>
|
||
|
#include <unistd.h>
|
||
|
#include <stdio.h>
|
||
|
|
||
|
Framebuffer::Framebuffer(std::string devicePath) {
|
||
|
char *devicePathCharArray = new char[devicePath.length()]();
|
||
|
for(int i = 0; i < (int) devicePath.length(); i++) {
|
||
|
devicePathCharArray[i] = devicePath[i];
|
||
|
}
|
||
|
this->devicePath = devicePathCharArray;
|
||
|
}
|
||
|
|
||
|
int Framebuffer::init() {
|
||
|
if(devicePath == NULL || isDeviceOpen) return -1;
|
||
|
|
||
|
device = open(devicePath, O_RDWR);
|
||
|
|
||
|
struct fb_var_screeninfo screeninfo;
|
||
|
ioctl(device, FBIOGET_VSCREENINFO, &screeninfo);
|
||
|
|
||
|
bitsPerPixel = screeninfo.bits_per_pixel;
|
||
|
if(bitsPerPixel != 32) {
|
||
|
printf("Colorscale: %i bits per pixel\n", bitsPerPixel);
|
||
|
printf("Change the colordepth to 32 bits per pixel\n");
|
||
|
close(device);
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
width = screeninfo.xres;
|
||
|
height = screeninfo.yres;
|
||
|
bytesPerPixel = bitsPerPixel / 8;
|
||
|
|
||
|
if(sizeof(unsigned int) != bytesPerPixel) {
|
||
|
close(device);
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
isDeviceOpen = true;
|
||
|
|
||
|
mmapData();
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int Framebuffer::getWidth() {
|
||
|
return width;
|
||
|
}
|
||
|
|
||
|
int Framebuffer::getHeight() {
|
||
|
return height;
|
||
|
}
|
||
|
|
||
|
int Framebuffer::getBytesPerPixel() {
|
||
|
return bytesPerPixel;
|
||
|
}
|
||
|
|
||
|
void Framebuffer::mmapData() {
|
||
|
data = (unsigned int*) mmap(0, width * height * bytesPerPixel,
|
||
|
PROT_READ | PROT_WRITE, MAP_SHARED, device, 0);
|
||
|
isDataMapped = true;
|
||
|
}
|
||
|
|
||
|
int Framebuffer::setPixel(int x, int y, unsigned int color) {
|
||
|
if(isDataMapped)
|
||
|
data[y * width + x] = color;
|
||
|
else
|
||
|
return -1;
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int Framebuffer::clear(unsigned int color) {
|
||
|
for(int row = 0; row < height; row++) {
|
||
|
for(int column = 0; column < width; column++) {
|
||
|
int success = setPixel(column, row, color);
|
||
|
if(success != 0) return -1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
void Framebuffer::munmapData() {
|
||
|
munmap(data, width * height * bytesPerPixel);
|
||
|
isDataMapped = false;
|
||
|
}
|
||
|
|
||
|
void Framebuffer::done() {
|
||
|
if(isDataMapped) munmapData();
|
||
|
if(isDeviceOpen) {
|
||
|
close(device);
|
||
|
isDeviceOpen = false;
|
||
|
}
|
||
|
}
|