diff --git a/CMakeLists.txt b/CMakeLists.txt index 2339436cc..04e83c22c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -261,6 +261,8 @@ tde_conditional_add_subdirectory( BUILD_TSAK tsak ) tde_conditional_add_subdirectory( BUILD_KROOTBACKING krootbacking ) tde_conditional_add_subdirectory( BUILD_TQT3INTEGRATION tqt3integration ) +add_subdirectory( tdekbdledsync ) + ##### install starttde & related stuff ########## if( BUILD_STARTTDE ) diff --git a/tdekbdledsync/CMakeLists.txt b/tdekbdledsync/CMakeLists.txt new file mode 100644 index 000000000..fd3a37f8f --- /dev/null +++ b/tdekbdledsync/CMakeLists.txt @@ -0,0 +1,28 @@ +################################################# +# +# (C) 2013 Timothy Pearson +# kb9vqf (AT) pearsoncomputing.net +# +# Improvements and feedback are welcome +# +# This file is released under GPL >= 2 +# +################################################# + +include_directories( + ${TDE_INCLUDE_DIR} + ${TQT_INCLUDE_DIRS} +) + +link_directories( + ${TQT_LIBRARY_DIRS} +) + + +##### tdekbdledsync (executable) ################ + +tde_add_executable( tdekbdledsync + SOURCES main.cpp + LINK udev X11 + DESTINATION ${BIN_INSTALL_DIR} +) diff --git a/tdekbdledsync/main.cpp b/tdekbdledsync/main.cpp new file mode 100644 index 000000000..f33df68c9 --- /dev/null +++ b/tdekbdledsync/main.cpp @@ -0,0 +1,182 @@ +/* +Copyright 2011-2013 Timothy Pearson + +This file is part of tdekbdledsync, the TDE Keyboard LED Synchronization Daemon + +tdekbdledsync is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as +published by the Free Software Foundation, either version 3 +of the License, or (at your option) any later version. + +tdekbdledsync is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public +License along with tdekbdledsync. If not, see http://www.gnu.org/licenses/. + +*/ + +// The idea here is to periodically read the Xorg core keyboard state, and then forcibly set the physical LED states on all attached keyboards to match (via the event interface) +// Once every half second should work well enough on most systems + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +extern "C" { +#include +} +#include + +#include +#include +#include +#include + +using namespace std; + +// WARNING +// MAX_KEYBOARDS must be greater than or equal to MAX_INPUT_NODE +#define MAX_KEYBOARDS 128 +#define MAX_INPUT_NODE 128 + +#define TestBit(bit, array) (array[(bit) / 8] & (1 << ((bit) % 8))) + +typedef unsigned char byte; + +char filename[32]; +char key_bitmask[(KEY_MAX + 7) / 8]; + +int keyboard_fd_num; +int keyboard_fds[MAX_KEYBOARDS]; + +Display* display = NULL; + +int find_keyboards() { + int i, j; + int fd; + + 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; + } + } + } + + if (keyboard_fds[keyboard_fd_num] == 0) { + close(fd); + } + else { + keyboard_fd_num++; + } + } + } + return 0; +} + +int main() { + int current_keyboard; + char name[256] = "Unknown"; + unsigned int states; + struct input_event ev; + + bool num_lock_set = false; + bool caps_lock_set = false; + bool scroll_lock_set = false; + + // Open X11 display + display = XOpenDisplay(NULL); + if (!display) { + printf ("[tdekbdledsync] Unable to open X11 display!\n"); + return -1; + } + + // Find keyboards + find_keyboards(); + if (keyboard_fd_num == 0) { + printf ("[tdekbdledsync] Could not find any usable keyboard(s)!\n"); + return -2; + } + else { + fprintf(stderr, "[tdekbdledsync] Found %d keyboard(s)\n", keyboard_fd_num); + + for (current_keyboard=0;current_keyboard