diff --git a/tdekbdledsync/CMakeLists.txt b/tdekbdledsync/CMakeLists.txt index fd3a37f8f..70ee0a96f 100644 --- a/tdekbdledsync/CMakeLists.txt +++ b/tdekbdledsync/CMakeLists.txt @@ -22,7 +22,7 @@ link_directories( ##### tdekbdledsync (executable) ################ tde_add_executable( tdekbdledsync - SOURCES main.cpp + SOURCES getfd.c main.cpp LINK udev X11 DESTINATION ${BIN_INSTALL_DIR} ) 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 */ +} diff --git a/tdekbdledsync/getfd.h b/tdekbdledsync/getfd.h new file mode 100644 index 000000000..991f33d4a --- /dev/null +++ b/tdekbdledsync/getfd.h @@ -0,0 +1 @@ +extern int getfd(const char *fnam); diff --git a/tdekbdledsync/main.cpp b/tdekbdledsync/main.cpp index f33df68c9..a22864f25 100644 --- a/tdekbdledsync/main.cpp +++ b/tdekbdledsync/main.cpp @@ -30,6 +30,7 @@ License along with tdekbdledsync. If not, see http://www.gnu.org/licenses/. #include #include #include +#include #include #include #include @@ -38,12 +39,15 @@ License along with tdekbdledsync. If not, see http://www.gnu.org/licenses/. #include #include #include +#include extern "C" { #include +#include "getfd.h" } #include #include +#include #include #include #include @@ -67,9 +71,149 @@ int keyboard_fds[MAX_KEYBOARDS]; Display* display = NULL; +// -------------------------------------------------------------------------------------- +// Useful function from Stack Overflow +// http://stackoverflow.com/questions/874134/find-if-string-endswith-another-string-in-c +// -------------------------------------------------------------------------------------- +/* returns 1 iff str ends with suffix */ +int str_ends_with(const char * str, const char * suffix) { + + if( str == NULL || suffix == NULL ) + return 0; + + size_t str_len = strlen(str); + size_t suffix_len = strlen(suffix); + + if(suffix_len > str_len) + return 0; + + return 0 == strncmp( str + str_len - suffix_len, suffix, suffix_len ); +} +// -------------------------------------------------------------------------------------- + +// -------------------------------------------------------------------------------------- +// Get the VT number X is running on +// (code taken from GDM, daemon/getvt.c, GPLv2+) +// -------------------------------------------------------------------------------------- +int get_x_vtnum(Display *dpy) +{ + Atom prop; + Atom actualtype; + int actualformat; + unsigned long nitems; + unsigned long bytes_after; + unsigned char *buf; + int num; + + prop = XInternAtom (dpy, "XFree86_VT", False); + if (prop == None) + return -1; + + if (XGetWindowProperty (dpy, DefaultRootWindow (dpy), prop, 0, 1, + False, AnyPropertyType, &actualtype, &actualformat, + &nitems, &bytes_after, &buf)) { + return -1; + } + + if (nitems != 1) { + XFree (buf); + return -1; + } + + switch (actualtype) { + case XA_CARDINAL: + case XA_INTEGER: + case XA_WINDOW: + switch (actualformat) { + case 8: + num = (*(uint8_t *)(void *)buf); + break; + case 16: + num = (*(uint16_t *)(void *)buf); + break; + case 32: + num = (*(uint32_t *)(void *)buf); + break; + default: + XFree (buf); + return -1; + } + break; + default: + XFree (buf); + return -1; + } + + XFree (buf); + + return num; +} +// -------------------------------------------------------------------------------------- + +// -------------------------------------------------------------------------------------- +// Get the specified xkb mask modifier +// (code taken from numlockx) +// -------------------------------------------------------------------------------------- +unsigned int xkb_mask_modifier(XkbDescPtr xkb, const char *name) { + int i; + if( !xkb || !xkb->names ) { + return 0; + } + for( i = 0; i < XkbNumVirtualMods; i++ ) { + char* modStr = XGetAtomName( xkb->dpy, xkb->names->vmods[i] ); + if( modStr != NULL && strcmp(name, modStr) == 0 ) { + unsigned int mask; + XkbVirtualModsToReal( xkb, 1 << i, &mask ); + return mask; + } + } + return 0; +} +// -------------------------------------------------------------------------------------- + +// -------------------------------------------------------------------------------------- +// Get the capslock xkb mask modifier +// -------------------------------------------------------------------------------------- +unsigned int xkb_capslock_mask() { + return LockMask; +} +// -------------------------------------------------------------------------------------- + +// -------------------------------------------------------------------------------------- +// Get the numlock xkb mask modifier +// (code taken from numlockx) +// -------------------------------------------------------------------------------------- +unsigned int xkb_numlock_mask() { + XkbDescPtr xkb; + if(( xkb = XkbGetKeyboard(display, XkbAllComponentsMask, XkbUseCoreKbd )) != NULL ) { + unsigned int mask = xkb_mask_modifier( xkb, "NumLock" ); + XkbFreeKeyboard( xkb, 0, True ); + return mask; + } + return 0; +} +// -------------------------------------------------------------------------------------- + +// -------------------------------------------------------------------------------------- +// Get the scroll lock xkb mask modifier +// (code taken from numlockx and modified) +// -------------------------------------------------------------------------------------- +unsigned int xkb_scrolllock_mask() { + XkbDescPtr xkb; + if(( xkb = XkbGetKeyboard(display, XkbAllComponentsMask, XkbUseCoreKbd )) != NULL ) { + unsigned int mask = xkb_mask_modifier( xkb, "ScrollLock" ); + XkbFreeKeyboard( xkb, 0, True ); + return mask; + } + return 0; +} +// -------------------------------------------------------------------------------------- + + int find_keyboards() { int i, j; int fd; + char name[256] = "Unknown"; keyboard_fd_num = 0; for (i=0; i= 0) { ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask); - - struct input_id input_info; - ioctl (fd, EVIOCGID, &input_info); - if ((input_info.vendor != 0) && (input_info.product != 0)) { - /* We assume that anything that has an alphabetic key in the - QWERTYUIOP range in it is the main keyboard. */ - for (j = KEY_Q; j <= KEY_P; j++) { - if (TestBit(j, key_bitmask)) { - keyboard_fds[keyboard_fd_num] = fd; + + // Ensure that we do not detect tsak faked keyboards + ioctl (fd, EVIOCGNAME(sizeof(name)), name); + if (str_ends_with(name, "+tsak") == 0) { + struct input_id input_info; + ioctl (fd, EVIOCGID, &input_info); + if ((input_info.vendor != 0) && (input_info.product != 0)) { + /* We assume that anything that has an alphabetic key in the + QWERTYUIOP range in it is the main keyboard. */ + for (j = KEY_Q; j <= KEY_P; j++) { + if (TestBit(j, key_bitmask)) { + keyboard_fds[keyboard_fd_num] = fd; + } } } } @@ -111,11 +259,23 @@ int main() { char name[256] = "Unknown"; unsigned int states; struct input_event ev; + struct vt_stat vtstat; + int vt_fd; + int x11_vt_num = -1; + XEvent xev; + XkbStateRec state; bool num_lock_set = false; bool caps_lock_set = false; bool scroll_lock_set = false; + int num_lock_mask; + int caps_lock_mask; + int scroll_lock_mask; + + int evBase; + int errBase; + // Open X11 display display = XOpenDisplay(NULL); if (!display) { @@ -123,58 +283,158 @@ int main() { return -1; } - // Find keyboards - find_keyboards(); - if (keyboard_fd_num == 0) { - printf ("[tdekbdledsync] Could not find any usable keyboard(s)!\n"); + // Set up Xkb extension + int i1, mn, mj; + mj = XkbMajorVersion; + mn = XkbMinorVersion; + if (!XkbQueryExtension(display, &i1, &evBase, &errBase, &mj, &mn)) { + printf("[tdekbdledsync] Server doesn't support a compatible XKB\n"); return -2; } - else { - fprintf(stderr, "[tdekbdledsync] Found %d keyboard(s)\n", keyboard_fd_num); + XkbSelectEvents(display, XkbUseCoreKbd, XkbStateNotifyMask, XkbStateNotifyMask); + + // Get X server VT number + x11_vt_num = get_x_vtnum(display); + + // Monitor for hotplugged keyboards + struct udev *udev; + struct udev_device *dev; + struct udev_monitor *mon; + struct timeval tv; + + // Create the udev object + udev = udev_new(); + if (!udev) { + printf("[tdekbdledsync] Cannot connect to udev interface\n"); + return -3; + } + + // Set up a udev monitor to monitor input devices + mon = udev_monitor_new_from_netlink(udev, "udev"); + udev_monitor_filter_add_match_subsystem_devtype(mon, "input", NULL); + udev_monitor_enable_receiving(mon); - for (current_keyboard=0;current_keyboard(&xev); +// if (xkb_event->any.xkb_type & XkbStateNotify) { +// if ((xkb_event->state.changed & XkbModifierStateMask) || (xkb_event->state.changed & XkbModifierBaseMask)) { +// // Modifier state has changed +// // Synchronize keyboard indicators +// break; +// } +// } +// } +// } } + } - usleep(500*1000); + // Close all keyboard file descriptors + for (int current_keyboard=0;current_keyboardstart(); + } + XSetErrorHandler( ignoreXError ); argb_visual_available = false; char *display = 0; @@ -500,6 +508,10 @@ kg_main( const char *argv0 ) KGVerify::done(); + if (kbdl) { + kbdl->closeStdin(); + kbdl->detach(); + } if (comp) { if (comp->isRunning()) { if (_compositor == "kompmgr") {