#include #include #include #include #include #include #include /* Sample the displayed framebuffer without changing its graphics ownership. */ int main(void) { struct fb_var_screeninfo v = {0}; struct fb_fix_screeninfo f = {0}; int fd = open("/dev/fb0", O_RDONLY); if (fd < 0 || ioctl(fd, FBIOGET_VSCREENINFO, &v) || ioctl(fd, FBIOGET_FSCREENINFO, &f)) return 1; if (v.xres < 320 || v.yres < 200 || v.xres > 16384 || v.yres > 16384 || v.bits_per_pixel != 32) return 1; size_t pitch = f.line_length ? f.line_length : v.xres * 4; if (pitch < v.xres * 4 || pitch % 4 || pitch > 65536) return 1; const size_t length = pitch * v.yres; const uint32_t *pixels = mmap(NULL, length, PROT_READ, MAP_SHARED, fd, 0); if (pixels == MAP_FAILED) return 1; uint32_t palette[24]; size_t count = 0; for (size_t y = 0; y < v.yres && count < 24; y += 7) { for (size_t x = 0; x < v.xres && count < 24; x += 7) { uint32_t color = pixels[y * (pitch / 4) + x] & 0xffffff; size_t i = 0; while (i < count && color != palette[i]) ++i; if (i == count) palette[count++] = color; } } munmap((void *)pixels, length); close(fd); if (count < 24) return 1; printf("desktop framebuffer %ux%u at 32 bpp, at least %zu sampled colors\n", v.xres, v.yres, count); return 0; }