Skip to content

Instantly share code, notes, and snippets.

@kootenpv
Last active April 15, 2022 18:40
Show Gist options
  • Save kootenpv/fb4ea3802fb46b9e659ea13b4c23e29b to your computer and use it in GitHub Desktop.
Save kootenpv/fb4ea3802fb46b9e659ea13b4c23e29b to your computer and use it in GitHub Desktop.
Advanced Automatic Brightness on Macbook running Arch Linux

Compile screensy:

gcc -O3 -I/usr/include/cairo/ -lX11 -lcairo screensy.cpp -o screensy

I have to run to be able to write brightness changes:

sudo setpci -v -H1 -s 00:01.00 BRIDGE_CONTROL=0

Then run:

sudo python auto_brightness.py
#!/usr/bin/env python
# add keyboard brightness
import os
import time
from datetime import datetime
from PIL import Image
import numpy as np
SCREEN_BRIGHTNESS_FILE = "/home/pascal/screen_brightness.png"
HIGH_SCREEN_BRIGHTNESS_CUTOFF = 70
MAX_AVG_RGB = 255
def write(n):
with open("/sys/class/backlight/gmux_backlight/brightness", "w") as f:
#print("new", n)
f.write(str(int(n)))
def get_screen_brightness():
im = Image.open(SCREEN_BRIGHTNESS_FILE)
im_arr = np.fromstring(im.tobytes(), dtype=np.uint8)
im_arr = im_arr.reshape((im.size[1], im.size[0], 3))
crop = im_arr[-100:, -100:, :]
return crop.mean()
with open("/sys/class/backlight/gmux_backlight/max_brightness") as f:
max_v = int(f.read())
print("running brightness, did you run with root?")
prev_check = time.time()
while True:
os.system("/home/pascal/screensy " + SCREEN_BRIGHTNESS_FILE)
with open("/sys/devices/platform/applesmc.768/light") as f:
backlight = int(f.read()[1:-1].split(",")[0])
sb = get_screen_brightness()
print(sb, backlight)
new_value = max_v
# if v < 10:
# # do not do comparison constantly
# hour = datetime.now().hour
# if hour > 20 or hour < 8:
# new_value = 100
# else:
# new_value = 300
# if new_value == max_v and sb > HIGH_SCREEN_BRIGHTNESS_CUTOFF:
new_value = (1 - (sb / MAX_AVG_RGB)) * 0.8 * new_value + (0.2) * new_value
new_value = new_value if backlight > 10 else 0.3 * new_value
print(new_value)
write(new_value)
time.sleep(0.5)
/*
Adapted by Pascal van Kooten
-----------------------------------------------
Grabs a screenshot of the root window.
Usage : ./scr_tool <display> <output file>
Example : ./scr_tool :0 /path/to/output.png
Author: S Bozdag <selcuk.bozdag@gmail.com>
*/
#include <assert.h>
#include <stdio.h>
#include <cairo.h>
#include <cairo-xlib.h>
#include <X11/Xlib.h>
int max(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}
int min(int a, int b) {
if (a < b) {
return a;
} else {
return b;
}
}
int main(int argc, char** argv) {
Display *disp;
Window root;
cairo_surface_t *surface;
int scr;
// mouse related
Bool query_result;
int root_x, root_y;
int win_x, win_y;
unsigned int mask_return;
Window window_returned;
/* The only checkpoint only concerns about the number of parameters, see "Usage" */
if( argc != 2) {
fprintf(stderr, "Usage: screensy <output file> \n");
return 1;
}
/* try to connect to display, exit if it's NULL */
disp = XOpenDisplay( NULL );
if( disp == NULL ){
fprintf(stderr, "Given display cannot be found, exiting: %s\n" , argv[1]);
return 1;
}
scr = DefaultScreen(disp);
root = DefaultRootWindow(disp);
// mouse stuff, eventually we would even prefer a fast screenshot around mouse position / active window in i3
// query_result = XQueryPointer(disp, root, &window_returned,
// &window_returned, &root_x, &root_y, &win_x, &win_y,
// &mask_return);
// printf("Mouse is at (%d,%d)\n", root_x, root_y);
/* get the root surface on given display */
surface = cairo_xlib_surface_create(disp, root, DefaultVisual(disp, scr),
100,
1000);
// cairo_xlib_surface_set_size(surface, 100, 1000);
/* right now, the tool only outputs PNG images */
cairo_surface_write_to_png( surface, argv[1] );
/* free the memory*/
cairo_surface_destroy(surface);
/* return with success */
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment