better optional handling (maybe)

This commit is contained in:
Alessandro Mauri 2024-10-24 23:39:50 +02:00
parent f6db903cb7
commit d62d0e86b8

View file

@ -94,29 +94,32 @@ fn int main()
} }
// get the max brightness // get the max brightness
int! _x = read_int(MAX_BRIGHTNESS_DEV); int max_brightness;
if (catch excuse = _x) { int min_brightness;
int! opt = read_int(MAX_BRIGHTNESS_DEV);
if (try unwrapped = opt) {
max_brightness = unwrapped;
} if (catch excuse = opt) {
io::eprintfn("Error reading max brightness on %s: %s", MAX_BRIGHTNESS_DEV, excuse); io::eprintfn("Error reading max brightness on %s: %s", MAX_BRIGHTNESS_DEV, excuse);
thread::exit(1); return 1;
} }
int max_brightness = _x!!; min_brightness = (int)(max_brightness * MIN_BRIGHTNESS_PERCENT);
int min_brightness = (int)(max_brightness * MIN_BRIGHTNESS_PERCENT);
// try writing to brightness device // try writing to brightness device
_x = read_int(BRIGHTNESS_DEV); int! br = read_int(BRIGHTNESS_DEV);
if (catch excuse = _x) { if (catch excuse = br) {
io::eprintfn("Error reading from %s: %s", BRIGHTNESS_DEV, excuse); io::eprintfn("Error reading from %s: %s", BRIGHTNESS_DEV, excuse);
thread::exit(1); return 1;
} }
if (catch excuse = write_int(BRIGHTNESS_DEV, _x)) { if (catch excuse = write_int(BRIGHTNESS_DEV, br)) {
io::eprintfn("Error writing to brightness device %s: %s", BRIGHTNESS_DEV, excuse); io::eprintfn("Error writing to brightness device %s: %s", BRIGHTNESS_DEV, excuse);
thread::exit(1); return 1;
} }
// try reading from the actual brightness device // try reading from the actual brightness device
if (catch excuse = read_int(ACTUAL_BRIGHTNESS_DEV)) { if (catch excuse = read_int(ACTUAL_BRIGHTNESS_DEV)) {
io::eprintfn("Error reading from %s: %s", ACTUAL_BRIGHTNESS_DEV, excuse); io::eprintfn("Error reading from %s: %s", ACTUAL_BRIGHTNESS_DEV, excuse);
thread::exit(1); return 1;
} }
int x = get_luminance_raw() ?? MAX_LUMINANCE; int x = get_luminance_raw() ?? MAX_LUMINANCE;
@ -160,19 +163,19 @@ fn int main()
thread::sleep_ms(SLEEP_DELAY); thread::sleep_ms(SLEEP_DELAY);
// read actual brightness to get the user offset // read actual brightness to get the user offset
int! actual_brightness; int actual_brightness;
int off; int off;
actual_brightness = read_int(ACTUAL_BRIGHTNESS_DEV); tmp = read_int(ACTUAL_BRIGHTNESS_DEV);
if (catch excuse = actual_brightness) { actual_brightness = tmp ?? brightness_adjusted;
if (catch excuse = tmp) {
io::eprintfn("Error reading actual brightness: %s", excuse); io::eprintfn("Error reading actual brightness: %s", excuse);
actual_brightness = brightness_adjusted;
} }
// if the actual brightness is zero, it means that the screen is turned // if the actual brightness is zero, it means that the screen is turned
// off for some reason, usually DPMS. As such it should not count for user // off for some reason, usually DPMS. As such it should not count for user
// offset. Look at the TODO file. // offset. Look at the TODO file.
if (actual_brightness!! != 0) { if (actual_brightness != 0) {
off = actual_brightness!! - brightness_target; off = actual_brightness - brightness_target;
if (brightness_target != max_brightness || off < 0){ if (brightness_target != max_brightness || off < 0){
user_offset = off; user_offset = off;
} }