add op_lidstate master
authorGrazvydas Ignotas <notasas@gmail.com>
Fri, 17 Jun 2016 21:50:12 +0000 (00:50 +0300)
committerGrazvydas Ignotas <notasas@gmail.com>
Fri, 17 Jun 2016 21:51:43 +0000 (00:51 +0300)
see https://pyra-handheld.com/boards/threads/trying-to-fix-the-backlight-bug.77558

Makefile
op_lidstate.c [new file with mode: 0644]

index ce1bbce..e2172f3 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,20 +1,20 @@
 CROSS_COMPILE ?= arm-none-linux-gnueabi-
 CC = $(CROSS_COMPILE)gcc
 
 CROSS_COMPILE ?= arm-none-linux-gnueabi-
 CC = $(CROSS_COMPILE)gcc
 
-CFLAGS += -Wall -O2
-LDFLAGS += -s
+CFLAGS += -Wall -Os
 INSTALL ?= bin
 
 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)
 
 
 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)
 
 $(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_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
        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 (file)
index 0000000..cb9d58c
--- /dev/null
@@ -0,0 +1,68 @@
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+#include <linux/input.h>
+
+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;
+}