aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: b5247af696a6639d1b4db065ff2467adbfa51448 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include<stdbool.h>
#include<unistd.h>
#include<fcntl.h>
#include<linux/fb.h>
#include<sys/ioctl.h>
#include<sys/mman.h>

int
main(void) {
	// Open frambuffer and get the size.
	int fb = open("/dev/fb0", O_RDWR);
	if (fb <= 0) {
		fprintf(stderr, "couldn't open the framebuffer\n");
		exit(EXIT_FAILURE);
	}
	struct fb_var_screeninfo info;
	if (ioctl(fb, FBIOGET_VSCREENINFO, &info) != 0) {
		fprintf(stderr, "couldn't get the framebuffer size\n");
		exit(EXIT_FAILURE);
	}

	// Mmap the framebuffer to a buffer object.
	size_t width = info.xres;
	size_t height = info.yres;
	size_t len = 4 * width * height;
	uint32_t *buf = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fb, 0);
	if (buf == MAP_FAILED) {
		fprintf(stderr, "couldn't mmap the framebuffer\n");
		exit(EXIT_FAILURE);
	}

	// Main loop.
	uint8_t shade = 0;
	size_t counter = 0;
	size_t direction = 1;
	while (true) {
		for (size_t j = 0; j < height; j++) {
			for (size_t i = 0; i < width; i++) {
				buf[j * width + i] = shade;
			}
		}
		counter++;
		if (counter > 10) {
			shade += direction;
			counter = 0;
		}
		if (shade == 0xFF) {
			direction = -1;
		} else if (shade == 0x00) {
			direction = 1;
		}
	}

	// Cleanup.
	munmap(buf, len);
	close(fb);
	return 0;
}