summaryrefslogtreecommitdiffstats
path: root/src/framebuffer.c
blob: 6a6d76af4d012af5b66532b037ee5f7b99ac8652 (plain) (blame)
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
/**
 * get-luminosity — Determine how bright it is in your room
 * Copyright © 2014  Mattias Andrée (maandree@member.fsf.org)
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "linearise.h"

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <stropts.h>
#include <linux/fb.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>


int main()
{
  auto struct fb_var_screeninfo var_info;
  long width;
  long height;
  long y;
  auto int fd;
  auto const char* restrict mem;
  register double grand_sum = 0;
  double inv_width;
  
  if (fd = open("/dev/fb0", O_RDONLY), fd == -1)
    return perror("get-luminosity::framebuffer: open: /dev/fb0"), close(fd), 1;
  
  if (ioctl(fd, (unsigned long)FBIOGET_VSCREENINFO, &var_info))
    return perror("get-luminosity::framebuffer: ioctl"), close(fd), 1;
  
  width       = var_info.xres;
  height      = var_info.yres;
  inv_width   = 1 / (double)width;
  
  mem = mmap(NULL, (size_t)(width * height * 4), PROT_READ, MAP_SHARED, fd, (off_t)0);
  if (mem == MAP_FAILED)
    return perror("get-luminosity::framebuffer: mmap"), close(fd), 1;
  
  for (y = 0; y < height; y++)
    {
      register const int32_t* restrict linemem = (const int32_t*)(mem + y * width * 4);
      register const int32_t* restrict end     = linemem + width;
      double sum = 0;
      while (linemem != end)
	{
	  int32_t value = *linemem++;
	  sum += linearise(value, 0) + linearise(value, 1) + linearise(value, 2);
	}
      grand_sum += sum * inv_width;
    }
  grand_sum /= (double)(height * 3);
  
  fprintf(stderr, "%lf\n", (double)grand_sum);
  fflush(stdout);
  
  close(fd);
  return 0;
}