commit abccab3a85f3d3ae63d56b8b09e58712153c9ef9
Author: parazyd <parazyd@dyne.org>
Date: Wed, 22 May 2019 22:59:57 +0200
Add backlight code.
Diffstat:
A | Makefile | | | 27 | +++++++++++++++++++++++++++ |
A | arg.h | | | 50 | ++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | backlight.c | | | 62 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | config.mk | | | 4 | ++++ |
4 files changed, 143 insertions(+), 0 deletions(-)
diff --git a/Makefile b/Makefile
@@ -0,0 +1,27 @@
+.POSIX:
+
+include config.mk
+
+BIN = backlight
+OBJ = $(BIN:=.o)
+
+all: $(BIN)
+
+$(OBJ): config.mk
+
+$(BIN): $(OBJ)
+ $(CC) $(OBJ) $(LDFLAGS) -o $@
+
+clean:
+ rm -f $(BIN) $(OBJ)
+
+install: all
+ mkdir -p $(DESTDIR)$(PREFIX)/bin
+ cp -f $(BIN) $(DESTDIR)$(PREFIX)/bin
+ chmod 4711 $(DESTDIR)$(PREFIX)/bin/$(BIN)
+
+uninstall:
+ rm -rf $(DESTDIR)$(PREFIX)/bin/$(BIN)
+
+.c.o:
+ $(CC) $(CFLAGS) -c $<
diff --git a/arg.h b/arg.h
@@ -0,0 +1,50 @@
+/*
+ * Copy me if you can.
+ * by 20h
+ */
+
+#ifndef ARG_H__
+#define ARG_H__
+
+extern char *argv0;
+
+/* use main(int argc, char *argv[]) */
+#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\
+ argv[0] && argv[0][0] == '-'\
+ && argv[0][1];\
+ argc--, argv++) {\
+ char argc_;\
+ char **argv_;\
+ int brk_;\
+ if (argv[0][1] == '-' && argv[0][2] == '\0') {\
+ argv++;\
+ argc--;\
+ break;\
+ }\
+ int i_;\
+ for (i_ = 1, brk_ = 0, argv_ = argv;\
+ argv[0][i_] && !brk_;\
+ i_++) {\
+ if (argv_ != argv)\
+ break;\
+ argc_ = argv[0][i_];\
+ switch (argc_)
+
+#define ARGEND }\
+ }
+
+#define ARGC() argc_
+
+#define EARGF(x) ((argv[0][i_+1] == '\0' && argv[1] == NULL)?\
+ ((x), abort(), (char *)0) :\
+ (brk_ = 1, (argv[0][i_+1] != '\0')?\
+ (&argv[0][i_+1]) :\
+ (argc--, argv++, argv[0])))
+
+#define ARGF() ((argv[0][i_+1] == '\0' && argv[1] == NULL)?\
+ (char *)0 :\
+ (brk_ = 1, (argv[0][i_+1] != '\0')?\
+ (&argv[0][i_+1]) :\
+ (argc--, argv++, argv[0])))
+
+#endif
diff --git a/backlight.c b/backlight.c
@@ -0,0 +1,62 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "arg.h"
+
+char *argv0;
+
+void usage(void) {
+ fprintf(stderr, "usage: %s [-l|-k] [on|off]\n", argv0);
+ exit(1);
+}
+
+int main(int argc, char *argv[]) {
+ const char *kbd_backlight = "/sys/class/leds/kbd_backlight/brightness";
+ const char *lcd_backlight = "/sys/class/backlight/lcd/brightness";
+ int iskbd = 0, islcd = 0, state = -1;
+ char *act = NULL;
+ FILE *fd = NULL;
+
+ ARGBEGIN {
+ case 'l':
+ islcd = 1;
+ act = EARGF(usage());
+ break;
+ case 'k':
+ iskbd = 1;
+ act = EARGF(usage());
+ break;
+ default:
+ usage();
+ } ARGEND;
+
+ if ((islcd && iskbd) || (!islcd && !iskbd))
+ usage();
+
+ if (!strcmp(act, "on"))
+ state = 1;
+ else if (!strcmp(act, "off"))
+ state = 0;
+ if (state < 0)
+ usage();
+
+ if (islcd)
+ fd = fopen(lcd_backlight, "w");
+ if (iskbd)
+ fd = fopen(kbd_backlight, "w");
+
+ if (fd == NULL) {
+ fprintf(stderr, "Unable to open file for writing.\n");
+ return 1;
+ }
+
+ if (islcd)
+ fprintf(fd, "%d", state==0 ? 0 : 200);
+ if (iskbd)
+ fprintf(fd, "%d", state==0 ? 0 : 255);
+
+ fclose(fd);
+
+ return 0;
+}
diff --git a/config.mk b/config.mk
@@ -0,0 +1,4 @@
+PREFIX = /usr/local
+
+CFLAGS = -O2 -Wall -Werror -pedantic
+LDFLAGS = -static -s