From 94ca8b79d10e946149823b521167321d41045667 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 8 Nov 2014 03:41:41 +0100 Subject: add support for x MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/x.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/x.c diff --git a/src/x.c b/src/x.c new file mode 100644 index 0000000..4289c06 --- /dev/null +++ b/src/x.c @@ -0,0 +1,74 @@ +/** + * 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 . + */ +#include "linearise.h" + +#include +#include +#include +#include +#include +#include + + +static Display* restrict display; + + +static double get_brightness(int screen) +{ + auto Window root = XRootWindow(display, screen); + auto long width = (long)XDisplayWidth(display, screen); + auto long height = (long)XDisplayHeight(display, screen); + auto XImage* restrict image = XGetImage(display, root, 0, 0, width, height, AllPlanes, ZPixmap); + register double sum = 0; + register unsigned char* restrict pixels; + register unsigned char* restrict end; + + if (image == NULL) + return -1; + + pixels = (unsigned char*)(image->data); + end = pixels + width * height * 4; + + while (pixels != end) + { + sum += LINEAR_MAP[*pixels++]; + sum += LINEAR_MAP[*pixels++]; + sum += LINEAR_MAP[*pixels++]; + pixels++; + } + + XDestroyImage(image); + return sum / (double)(height * width * 3); +} + + +int main(void) +{ + int screen, screens; + + if (display = XOpenDisplay(NULL), display == NULL) + return 1; + + screens = (size_t)ScreenCount(display); + for (screen = 0; screen < screens; screen++) + fprintf(stderr, "%lf\n", get_brightness(screen)); + + XCloseDisplay(display); + return 0; +} + -- cgit v1.2.3-70-g09d2