From 7816ebcadcc73387debb97e1fdb79569e7440018 Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Wed, 24 Jul 2013 17:18:09 -0500 Subject: Add highly experimental tdekbdledsync application --- tdekbdledsync/main.cpp | 182 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 tdekbdledsync/main.cpp (limited to 'tdekbdledsync/main.cpp') 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