forked from Bananymous/banan-os
Userspace: Implement basic test for framebuffer mmap
This commit is contained in:
parent
cc572af390
commit
42a1d26d5b
|
@ -24,6 +24,7 @@ set(USERSPACE_PROJECTS
|
|||
tee
|
||||
test
|
||||
test-globals
|
||||
test-framebuffer
|
||||
touch
|
||||
u8sum
|
||||
whoami
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
cmake_minimum_required(VERSION 3.26)
|
||||
|
||||
project(test-framebuffer CXX)
|
||||
|
||||
set(SOURCES
|
||||
main.cpp
|
||||
)
|
||||
|
||||
add_executable(test-framebuffer ${SOURCES})
|
||||
target_compile_options(test-framebuffer PUBLIC -O2 -g)
|
||||
target_link_libraries(test-framebuffer PUBLIC libc)
|
||||
|
||||
add_custom_target(test-framebuffer-install
|
||||
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/test-framebuffer ${BANAN_BIN}/
|
||||
DEPENDS test-framebuffer
|
||||
)
|
|
@ -0,0 +1,45 @@
|
|||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <sys/framebuffer.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int fd = open("/dev/fb0", O_RDWR);
|
||||
if (fd == -1)
|
||||
{
|
||||
perror("open");
|
||||
return 1;
|
||||
}
|
||||
|
||||
framebuffer_info_t fb_info;
|
||||
if (pread(fd, &fb_info, sizeof(fb_info), -1) == -1)
|
||||
{
|
||||
perror("read");
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t fb_size = fb_info.width * fb_info.height * fb_info.bpp / 8;
|
||||
|
||||
void* addr = mmap(nullptr, fb_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
if (addr == MAP_FAILED)
|
||||
{
|
||||
perror("mmap");
|
||||
return 1;
|
||||
}
|
||||
|
||||
memset(addr, 0xFF, fb_size);
|
||||
|
||||
if (msync(addr, fb_size, MS_SYNC) == -1)
|
||||
{
|
||||
perror("msync");
|
||||
return 1;
|
||||
}
|
||||
|
||||
sleep(4);
|
||||
|
||||
munmap(addr, fb_size);
|
||||
close(fd);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue