From f9c0d0e246edf07c25a4917bf201d61f49a53484 Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Thu, 25 Jul 2013 11:17:00 -0500 Subject: Add lightweight daemon to synchronize keyboard indicators to current xkb state Start keyboard indicator sync daemon on tdm load This resolves Bug 427 --- tdekbdledsync/getfd.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 tdekbdledsync/getfd.c (limited to 'tdekbdledsync/getfd.c') diff --git a/tdekbdledsync/getfd.c b/tdekbdledsync/getfd.c new file mode 100644 index 000000000..589d8dd31 --- /dev/null +++ b/tdekbdledsync/getfd.c @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include +#include +#include +#include "getfd.h" + +/* + * getfd.c + * + * Get an fd for use with kbd/console ioctls. + * We try several things because opening /dev/console will fail + * if someone else used X (which does a chown on /dev/console). + */ + +static int +is_a_console(int fd) { + char arg; + + arg = 0; + return (ioctl(fd, KDGKBTYPE, &arg) == 0 + && ((arg == KB_101) || (arg == KB_84))); +} + +static int +open_a_console(const char *fnam) { + int fd; + + /* + * For ioctl purposes we only need some fd and permissions + * do not matter. But setfont:activatemap() does a write. + */ + fd = open(fnam, O_RDWR); + if (fd < 0 && errno == EACCES) + fd = open(fnam, O_WRONLY); + if (fd < 0 && errno == EACCES) + fd = open(fnam, O_RDONLY); + if (fd < 0) + return -1; + if (!is_a_console(fd)) { + close(fd); + return -1; + } + return fd; +} + +int getfd(const char *fnam) { + int fd; + + if (fnam) { + fd = open_a_console(fnam); + if (fd >= 0) + return fd; + fprintf(stderr, + ("Couldnt open %s\n"), fnam); + exit(1); + } + + fd = open_a_console("/dev/tty"); + if (fd >= 0) + return fd; + + fd = open_a_console("/dev/tty0"); + if (fd >= 0) + return fd; + + fd = open_a_console("/dev/vc/0"); + if (fd >= 0) + return fd; + + fd = open_a_console("/dev/console"); + if (fd >= 0) + return fd; + + for (fd = 0; fd < 3; fd++) + if (is_a_console(fd)) + return fd; + + fprintf(stderr, + ("Couldnt get a file descriptor referring to the console\n")); + exit(1); /* total failure */ +} -- cgit v1.2.3