vinix-ports / tests / userland / framebuffer.c
43 lines · 42 sloc · 1.5 KB · cbd2d400e82f75db02a0f223d93c518821a6638d
Raw
1#include <fcntl.h>
2#include <linux/fb.h>
3#include <stdint.h>
4#include <stdio.h>
5#include <sys/ioctl.h>
6#include <sys/mman.h>
7#include <unistd.h>
8
9/* Sample the displayed framebuffer without changing its graphics ownership. */
10int main(void) {
11 struct fb_var_screeninfo v = {0};
12 struct fb_fix_screeninfo f = {0};
13 int fd = open("/dev/fb0", O_RDONLY);
14 if (fd < 0 || ioctl(fd, FBIOGET_VSCREENINFO, &v) || ioctl(fd, FBIOGET_FSCREENINFO, &f))
15 return 1;
16 if (v.xres < 320 || v.yres < 200 || v.xres > 16384 || v.yres > 16384 || v.bits_per_pixel != 32)
17 return 1;
18 size_t pitch = f.line_length ? f.line_length : v.xres * 4;
19 if (pitch < v.xres * 4 || pitch % 4 || pitch > 65536)
20 return 1;
21 const size_t length = pitch * v.yres;
22 const uint32_t *pixels = mmap(NULL, length, PROT_READ, MAP_SHARED, fd, 0);
23 if (pixels == MAP_FAILED)
24 return 1;
25 uint32_t palette[24];
26 size_t count = 0;
27 for (size_t y = 0; y < v.yres && count < 24; y += 7) {
28 for (size_t x = 0; x < v.xres && count < 24; x += 7) {
29 uint32_t color = pixels[y * (pitch / 4) + x] & 0xffffff;
30 size_t i = 0;
31 while (i < count && color != palette[i])
32 ++i;
33 if (i == count)
34 palette[count++] = color;
35 }
36 }
37 munmap((void *)pixels, length);
38 close(fd);
39 if (count < 24)
40 return 1;
41 printf("desktop framebuffer %ux%u at 32 bpp, at least %zu sampled colors\n", v.xres, v.yres, count);
42 return 0;
43}
44