24 #include <linux/input.h>
26 #include <linux/keyboard.h>
27 #include <sys/types.h>
33 #define NELEMS(x) (sizeof(x) / sizeof((x)[0]))
38 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
39 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
40 #define OFF(x) ((x)%BITS_PER_LONG)
41 #define LONG(x) ((x)/BITS_PER_LONG)
42 #define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
44 unsigned long bitmask_ev[
NBITS(EV_MAX)];
45 unsigned long bitmask_key[
NBITS(KEY_MAX)];
46 unsigned long bitmask_abs[
NBITS(ABS_MAX)];
47 unsigned long bitmask_rel[
NBITS(REL_MAX)];
49 if (ioctl(fd, EVIOCGBIT(0,
sizeof(bitmask_ev)), &bitmask_ev) == -1)
return false;
50 if (ioctl(fd, EVIOCGBIT(EV_KEY,
sizeof(bitmask_key)), &bitmask_key) == -1)
return false;
51 if (ioctl(fd, EVIOCGBIT(EV_ABS,
sizeof(bitmask_abs)), &bitmask_abs) == -1)
return false;
52 if (ioctl(fd, EVIOCGBIT(EV_REL,
sizeof(bitmask_rel)), &bitmask_rel) == -1)
return false;
54 bool is_keyboard = (bitmask_key[0] & 0xFFFFFFFE);
57 bool is_mouse = (is_abs || is_rel) &&
test_bit(BTN_MOUSE, bitmask_key);
58 bool is_touch = is_abs && (
test_bit(BTN_TOOL_FINGER, bitmask_key) ||
test_bit(BTN_TOUCH, bitmask_key));
60 return is_keyboard || is_mouse || is_touch;
70 #define KDSKBMUTE 0x4B51
73 #define KDSKBMODE 0x4B45
79 static char const *
const consoles[] =
80 {
"/proc/self/fd/0",
"/dev/tty",
"/dev/tty0",
"/dev/tty1",
"/dev/tty2",
"/dev/tty3",
"/dev/tty4",
81 "/dev/tty5",
"/dev/tty6",
"/dev/vc/0",
"/dev/console" };
83 #define IS_CONSOLE(fd) isatty(fd) && ioctl(fd, KDGKBTYPE, &arg) == 0 && ((arg == KB_101) || (arg == KB_84))
90 for (
size_t i = 0; i <
NELEMS(consoles); ++i)
92 int fd = open(consoles[i], O_RDONLY);
93 if (fd >= 0) {
if (
IS_CONSOLE(fd))
return fd;
else close(fd); }
97 for (
int fd = 0; fd < 3; ++fd)
if (
IS_CONSOLE(fd))
return fd;
99 throw std::runtime_error(
"Could not get console fd");
105 kb_mode = 0;
char arg;
106 if (!
IS_CONSOLE(tty))
throw std::runtime_error(
"Tried to mute an invalid tty");
108 ioctl(tty, KDGKBMODE, &kb_mode);
109 if (ioctl(tty,
KDSKBMUTE, 1) && ioctl(tty,
KDSKBMODE,
K_OFF))
throw std::runtime_error(
"Failed muting keyboard");
117 throw std::runtime_error(
"Failed restoring keyboard mode");
126 int fd = open(
"/sys/class/tty/tty0/active", O_RDONLY);
127 if (fd < 0)
throw std::runtime_error(
"Could not determine which tty is active -- IGNORED");
128 ssize_t len = read(fd, ttyname, 255);
130 if (len <= 0)
throw std::runtime_error(
"Could not read which tty is active");
132 if (ttyname[len-1] ==
'\n') ttyname[len-1] =
'\0';
133 else ttyname[len] =
'\0';
135 std::string
const ttypath = std::string(
"/dev/") + ttyname;
136 fd = open(ttypath.c_str(), O_RDWR | O_NOCTTY);
137 if (fd < 0)
throw std::runtime_error(
"Could not open tty: " + ttypath);
138 if (!
IS_CONSOLE(fd)) { close(fd);
throw std::runtime_error(
"Invalid tty obtained: " + ttypath); }