+#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;
+}