// By Tom Smeding (2016) // modified for new laptop (2017) #include #include #include #include #include #include #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); } }