diff options
author | Tom Smeding <tom.smeding@gmail.com> | 2019-02-05 13:59:25 +0100 |
---|---|---|
committer | Tom Smeding <tom.smeding@gmail.com> | 2019-02-05 14:08:46 +0100 |
commit | 6412e6fd0f9f306b6b488191623c3ad0c6ea553c (patch) | |
tree | f51afd19a22db0a4d77ff3d69d10758325681859 /brightness.c |
Initial
Diffstat (limited to 'brightness.c')
-rw-r--r-- | brightness.c | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/brightness.c b/brightness.c new file mode 100644 index 0000000..feee058 --- /dev/null +++ b/brightness.c @@ -0,0 +1,76 @@ +// By Tom Smeding (2016) +// modified for new laptop (2017) + +#include <stdio.h> +#include <stdlib.h> +#include <stdarg.h> +#include <string.h> +#include <unistd.h> +#include <sys/types.h> + +#define INCREMENT 100 + +__attribute__((noreturn)) +__attribute__((format (printf, 1, 2))) +void die(const char *format,...){ + va_list ap; + va_start(ap,format); + vprintf(format,ap); + va_end(ap); + putchar('\n'); + exit(1); +} + +int get_max_brightness(void){ + FILE *f=fopen("/sys/class/backlight/intel_backlight/max_brightness","r"); + if(!f)die("Could not open max_brightness file"); + int n; + fscanf(f,"%d",&n); + fclose(f); + return n; +} + +int get_brightness(void){ + FILE *f=fopen("/sys/class/backlight/intel_backlight/brightness","r"); + if(!f)die("Could not open brightness file"); + int n; + fscanf(f,"%d",&n); + fclose(f); + return n; +} + +void set_brightness(int n){ + FILE *f=fopen("/sys/class/backlight/intel_backlight/brightness","w"); + if(!f)die("Could not open brightness file"); + fprintf(f,"%d",n); + fflush(f); + fclose(f); +} + +int main(int argc,char **argv){ + uid_t uid=geteuid(); + if(uid!=0){ + printf("brightness will only work with root.\n"); + return 1; + } + + if(argc==1){ + printf("max: %d\n",get_max_brightness()); + printf("current: %d\n",get_brightness()); + } else if(argc==2){ + int n; + if(strcmp(argv[1],"-")==0){ + n=get_brightness()-INCREMENT; + if(n<0)n=0; + } else if(strcmp(argv[1],"+")==0){ + n=get_brightness()+INCREMENT; + int mx=get_max_brightness(); + if(n>mx)n=mx; + } else { + char *endp; + n=strtol(argv[1],&endp,10); + if(argv[1][0]=='\0'||*endp!='\0')die("Invalid number '%s'",argv[1]); + } + set_brightness(n); + } +} |