-
Notifications
You must be signed in to change notification settings - Fork 4
/
frontend_fb.c
88 lines (76 loc) · 2.61 KB
/
frontend_fb.c
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "interface.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char **argv) {
int camera_number = 0;
if (argc != 2 || sscanf(argv[1], "%d", &camera_number) != 1) {
fprintf(stderr, "Usage: latency_qt_fb camera_number\n");
fprintf(stderr, "Framebuffer frontend for latency tester\n");
fprintf(stderr, "\n");
fprintf(stderr, "Arguments:\n");
fprintf(stderr, " camera_number Which camera device to read from. "
"Should be /dev/videoN\n");
return EXIT_FAILURE;
}
__uid_t uid = geteuid();
if (uid == 0) {
fprintf(stderr, "Please do not run this program as root.\n");
return EXIT_FAILURE;
}
// Oddly enough, O_WRONLY fails even though we only mmap with PROT_WRITE
int fbd = open("/dev/fb0", O_RDWR);
if (fbd == -1) {
fprintf(stderr, "Failed to open /dev/fb0, the first framebuffer. Are "
"you (and /dev/fb0) both in group 'video'?\n");
return EXIT_FAILURE;
}
struct fb_var_screeninfo vari;
if (ioctl(fbd, FBIOGET_VSCREENINFO, &vari) == -1) {
fprintf(stderr, "Failed to read variable screen data\n");
return EXIT_FAILURE;
}
struct fb_fix_screeninfo fixi;
if (ioctl(fbd, FBIOGET_FSCREENINFO, &fixi) == -1) {
fprintf(stderr, "Failed to read variable screen data\n");
return EXIT_FAILURE;
}
fprintf(stderr, "screen virt xres = %d, yres = %d\n", vari.xres_virtual,
vari.yres_virtual);
fprintf(stderr, "screen data length %d\n", fixi.smem_len);
uint32_t *mem =
(uint32_t *)mmap(0, fixi.smem_len, PROT_WRITE, MAP_SHARED, fbd, 0);
if (mem == MAP_FAILED) {
fprintf(stderr, "Failed to map screen device: %s\n", strerror(errno));
return EXIT_FAILURE;
}
void *state = setup_backend(camera_number);
if (!state) {
fprintf(stderr, "Failed to open camera #%d", camera_number);
return EXIT_FAILURE;
}
bool was_dark = true;
// note: cancel the loop with Ctrl+C
while (1) {
// todo: 1ms pause?
enum WhatToDo wtd = update_backend(state);
bool is_dark = wtd == DisplayDark;
if (is_dark != was_dark) {
was_dark = is_dark;
memset(mem, is_dark ? 0 : 255, fixi.smem_len);
}
}
munmap(mem, fixi.smem_len);
close(fbd);
cleanup_backend(state);
return EXIT_SUCCESS;
}