Updated libgetch

This commit is contained in:
R. Eric Wheeler 2021-02-24 20:47:44 -08:00
parent c7459a7d47
commit e06f9e0cc7
2 changed files with 33 additions and 46 deletions

View File

@ -1,3 +1,9 @@
/***********************************************************************
* 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 <stdio.h> #include <stdio.h>
#include <termios.h> #include <termios.h>
#include <unistd.h> #include <unistd.h>
@ -7,34 +13,31 @@
#include <linux/input.h> #include <linux/input.h>
#include <glob.h> #include <glob.h>
// #define FKEY(k) ((k) >= KEY_F1 && (k) <= KEY_F10) || (k) == KEY_F12 || (k) == KEY_F11 #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 NOTNUMPAD(k) ((k) >= KEY_HOME && (k) <= KEY_DELETE)
#define EVENT_DEVICE_GLOB "/dev/input/by-path/*-event-kbd" #define EVENT_DEVICE_GLOB "/dev/input/by-path/*-event-kbd"
static struct termios oldTermAttributes; static struct termios oldTermAttributes;
static void setRawMode(void); static int discardRead(unsigned int length)
static void setNormalMode(void);
inline static int discardRead(unsigned int length)
{ {
char buffer[length]; char buffer[length];
ssize_t bytes_read; ssize_t bytesRead;
int flags = fcntl(STDIN_FILENO, F_GETFL); int flags = fcntl(STDIN_FILENO, F_GETFL);
fcntl(STDIN_FILENO, F_SETFL, flags|O_NONBLOCK); fcntl(STDIN_FILENO, F_SETFL, flags|O_NONBLOCK);
if( (bytes_read = fread(buffer, sizeof(char), length, stdin)) == -1) { if( (bytesRead = fread(buffer, sizeof(char), length, stdin)) == -1) {
perror("discardRead"); perror("discardRead");
} }
fcntl(STDIN_FILENO, F_SETFL, flags); fcntl(STDIN_FILENO, F_SETFL, flags);
return (int)bytes_read; return (int)bytesRead;
} }
int getEventDevice(const char * device) static int getEventDevice(const char * device)
{ {
glob_t search; glob_t search;
@ -70,28 +73,33 @@ int getEventDevice(const char * device)
return -1; return -1;
} }
unsigned short getScanCode() static unsigned short getScanCode()
{ {
struct input_event inputEvent[5]; struct input_event inputEvent[3];
int fd; int eventDevice;
const char device[FILENAME_MAX]; const char device[FILENAME_MAX];
if(-1 == getEventDevice(device)) { if(getEventDevice(device) == -1) {
perror("getEventDevice");
return KEY_RESERVED; return KEY_RESERVED;
} }
if( (fd = open(device, O_RDONLY)) == -1 ) { if( ( eventDevice = open(device, O_RDONLY)) == -1 ) {
perror("open");
return KEY_RESERVED; return KEY_RESERVED;
}; };
if( read(fd, &inputEvent, sizeof (inputEvent)) == -1 ) {
if( read(eventDevice, &inputEvent, sizeof(inputEvent)) == -1) {
close(eventDevice);
return KEY_RESERVED; return KEY_RESERVED;
} }
close(fd); close(eventDevice);
for(int i = 0; i<=5; i++) { for(int i = 0;i<3;i++) {
if(inputEvent[i].type == EV_KEY) { if(inputEvent[i].type == EV_KEY && inputEvent[i].code != KEY_ENTER) {
return inputEvent[i].code; return inputEvent[i].code;
} }
} }
@ -99,37 +107,16 @@ unsigned short getScanCode()
return KEY_RESERVED; return KEY_RESERVED;
} }
#pragma clang diagnostic push
#pragma ide diagnostic ignored "cppcoreguidelines-narrowing-conversions"
inline static char *reverseString(char *str)
{
char *p1, *p2;
if (! str || ! *str)
return str;
for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
{
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}
return str;
}
#pragma clang diagnostic pop
#pragma clang diagnostic push
#pragma ide diagnostic ignored "bugprone-reserved-identifier"
inline static void pushStdin(int c) { inline static void pushStdin(int c) {
ungetc(c, stdin); ungetc(c, stdin);
} }
#pragma clang diagnostic pop
inline static void setNormalMode(void) static void setNormalMode(void)
{ {
tcsetattr(STDIN_FILENO, TCSANOW, &oldTermAttributes); tcsetattr(STDIN_FILENO, TCSANOW, &oldTermAttributes);
} }
inline static void setRawMode(void) static void setRawMode(void)
{ {
tcgetattr(STDIN_FILENO, &oldTermAttributes); tcgetattr(STDIN_FILENO, &oldTermAttributes);
@ -140,7 +127,7 @@ inline static void setRawMode(void)
} }
unsigned short resolveScanCode(unsigned short key) static unsigned short resolveScanCode(unsigned short key)
{ {
switch(key) { switch(key) {
case KEY_DOWN: return KEY_KP2; case KEY_DOWN: return KEY_KP2;
@ -157,16 +144,15 @@ unsigned short resolveScanCode(unsigned short key)
}; };
} }
int readKey(void) { static int readKey(void) {
unsigned short scanCode;
int key = getchar(); int key = getchar();
if (key == 27) { if (key == 27) {
scanCode = getScanCode(); unsigned short scanCode = getScanCode();
if (scanCode == KEY_ESC) { if (scanCode == KEY_ESC || scanCode == KEY_RESERVED) {
return 27; return 27;
} }
@ -219,6 +205,7 @@ int readKey(void) {
} }
pushStdin(scanCode); pushStdin(scanCode);
return returnResult; return returnResult;
} }

Binary file not shown.