/*********************************************************************** * This Source Code Form is subject to the terms of the Mozilla Public * * License, v. 2.0. If a copy of the MPL was not distributed with this * * file, You can obtain one at http://mozilla.org/MPL/2.0/. * ***********************************************************************/ #include #include #include #include #include #include #include #include #include "getch.h" #define EVENT_DEVICE_GLOB "/dev/input/by-path/*-event-kbd" #define FKEY(k) ((k) >= KEY_F1 && (k) <= KEY_F10) || (k) == KEY_F12 || (k) == KEY_F11 #define NOTNUMPAD(k) ((k) >= KEY_HOME && (k) <= KEY_DELETE) #define XOR_SWAP(a,b) do\ {\ (a) ^= (b);\ (b) ^= (a);\ (a) ^= (b);\ } while (0) static struct termios oldTermAttributes; inline static void reverseString(char * str) { if (str) { char * end = str + strlen(str) - 1; while (str < end) { XOR_SWAP(*str, *end); str++; end--; } } } static int discardRead(unsigned int length) { char buffer[length]; ssize_t bytesRead; int flags = fcntl(STDIN_FILENO, F_GETFL); fcntl(STDIN_FILENO, F_SETFL, flags|O_NONBLOCK); if( (bytesRead = fread(buffer, sizeof(char), length, stdin)) == -1) { perror("discardRead"); } fcntl(STDIN_FILENO, F_SETFL, flags); return (int)bytesRead; } static int getEventDevice(char ** device) { glob_t search; int globResult = glob( EVENT_DEVICE_GLOB, GLOB_NOSORT, NULL, &search ); if(0 != globResult) { globfree(&search); return -1; } if(search.gl_pathc == 0) { globfree(&search); return -1; } size_t pathLength; for(int i = 0; i