From: Grazvydas Ignotas Date: Fri, 17 Jun 2016 21:50:12 +0000 (+0300) Subject: add op_lidstate X-Git-Url: https://git.openpandora.org/cgi-bin/gitweb.cgi?p=pandora-misc.git;a=commitdiff_plain;h=HEAD add op_lidstate see https://pyra-handheld.com/boards/threads/trying-to-fix-the-backlight-bug.77558 --- diff --git a/Makefile b/Makefile index ce1bbce..e2172f3 100644 --- a/Makefile +++ b/Makefile @@ -1,20 +1,20 @@ CROSS_COMPILE ?= arm-none-linux-gnueabi- CC = $(CROSS_COMPILE)gcc -CFLAGS += -Wall -O2 -LDFLAGS += -s +CFLAGS += -Wall -Os INSTALL ?= bin -BIN = op_runfbapp op_gammatool op_gammatable op_test_inputs ofbset +BIN += op_runfbapp op_gammatool op_gammatable op_test_inputs op_lidstate +BIN += ofbset all: $(BIN) clean: $(RM) *.o $(BIN) -op_runfbapp: LDFLAGS += -lpthread -lX11 -op_test_inputs: LDFLAGS += -lpthread -lts -op_gammatable: LDFLAGS += -lm +op_runfbapp: LDLIBS += -lpthread -lX11 +op_test_inputs: LDLIBS += -lpthread -lts +op_gammatable: LDLIBS += -lm $(INSTALL): mkdir -p $(INSTALL) @@ -24,9 +24,12 @@ install: $(INSTALL) $(BIN) cp op_gammatool $(INSTALL)/op_gammatool_bin cp op_gammatable $(INSTALL)/ cp op_test_inputs $(INSTALL)/op_test_inputs_bin + cp op_lidstate $(INSTALL)/ cp ofbset $(INSTALL)/ cp scripts/op_gammatool $(INSTALL)/ cp scripts/op_test_inputs $(INSTALL)/ vld: $(CC) -Wall -O2 -DVALIDATION -s op_test_inputs.c -o op_test_inputs -Wl,-Bstatic -lpthread -lc -Wl,-Bdynamic -lts + +.PHONY: all clean install vld diff --git a/op_lidstate.c b/op_lidstate.c new file mode 100644 index 0000000..cb9d58c --- /dev/null +++ b/op_lidstate.c @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + unsigned int state = 0; + int fd = -1; + int id, ret; + + if (argv[1] != NULL + && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) + { + fprintf(stderr, "prints 1 if lid is closed, " + "0 if open or in unknown state " + "or if an error occurs\n"); + return 0; + } + + for (id = 0; id < 100; id++) + { + char fname[64]; + char name[64]; + + snprintf(fname, sizeof(fname), "/dev/input/event%i", id); + fd = open(fname, O_RDONLY); + if (fd == -1) + { + if (errno == EACCES) + continue; + break; + } + + name[0] = 0; + ioctl(fd, EVIOCGNAME(sizeof(name)), name); + if (!strcmp(name, "gpio-keys")) + break; + + close(fd); + fd = -1; + } + + if (fd == -1) + { + fprintf(stderr, "unable to find or access gpio-keys\n"); + fprintf(stdout, "0\n"); + return 1; + } + + ret = ioctl(fd, EVIOCGSW(sizeof(state)), &state); + close(fd); + if (ret < 0) + { + perror("ioctl EVIOCGSW"); + fprintf(stdout, "0\n"); + return 1; + } + + printf("%d\n", state & (1u << SW_LID) ? 1 : 0); + + return 0; +}