Skip to content

Instantly share code, notes, and snippets.

@chris124567
Last active August 11, 2021 01:20
Show Gist options
  • Save chris124567/8a43d579a42322b5b17a51598f4075ea to your computer and use it in GitHub Desktop.
Save chris124567/8a43d579a42322b5b17a51598f4075ea to your computer and use it in GitHub Desktop.
backlight control program for linux on dell latitude 3510
#include <stdio.h>
#include <stdlib.h>
#define BRIGHTNESS_FILE "/sys/class/backlight/intel_backlight/brightness"
#define MAX_BRIGHTNESS 120000
static void __attribute__((noreturn)) die(const char *msg) {
printf("%s\n", msg);
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[]) {
if (argc != 2) {
die("Please provide an argument.");
}
const char direction = argv[1][0];
if (direction != '+' && direction != '-') {
die("Please provide a direction to move the brightness in.");
}
FILE *fp = fopen(BRIGHTNESS_FILE, "r+");
if (!fp) {
die("Failed to access brightness file");
}
int brightness_level = 0;
fscanf(fp, "%d", &brightness_level);
int new_brightness_level = 0;
if (direction == '+' || brightness_level > 5000) {
new_brightness_level = brightness_level + ((direction == '+') ? MAX_BRIGHTNESS/50: -MAX_BRIGHTNESS/50);
} else if (direction == '-') {
new_brightness_level = brightness_level*7/10;
}
if (new_brightness_level == 0) {
fclose(fp);
die("Not writing 0 brightness");
}
fprintf(fp, "%d", new_brightness_level);
fclose(fp);
exit(EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment