summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Smeding <tom.smeding@gmail.com>2019-02-05 13:59:25 +0100
committerTom Smeding <tom.smeding@gmail.com>2019-02-05 14:08:46 +0100
commit6412e6fd0f9f306b6b488191623c3ad0c6ea553c (patch)
treef51afd19a22db0a4d77ff3d69d10758325681859
Initial
-rw-r--r--.gitignore1
-rw-r--r--Makefile20
-rw-r--r--brightness.c76
3 files changed, 97 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..72eb06f
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+brightness
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..8317a0b
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,20 @@
+CC = gcc
+CFLAGS = -Wall -Wextra -O2 -std=c11 -fwrapv
+INSTALL_DIR = $(HOME)/prefix/bin
+
+BIN = brightness
+
+.PHONY: all clean install
+
+all: $(BIN)
+
+clean:
+ rm -f $(BIN)
+
+install:
+ sudo install -o root brightness $(INSTALL_DIR)
+ sudo chmod +s $(INSTALL_DIR)/brightness
+
+
+$(BIN): brightness.c $(wildcard *.h)
+ $(CC) $(CFLAGS) $(filter %.c,$^) -o $@
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);
+ }
+}