From b36f8ca037ccb7a927f9a0de4793e74c4b1d6950 Mon Sep 17 00:00:00 2001 From: bruceperens Date: Fri, 18 Apr 2014 18:19:22 +0000 Subject: [PATCH] Button enumeration works. git-svn-id: https://svn.code.sf.net/p/freetel/code@1505 01035d8c-6547-0410-b346-abe4f91aad63 --- freedv-server/source/platform/linux/evdev.cpp | 36 +++---------- freedv-server/source/platform/linux/evdev.h | 14 +---- .../source/platform/linux/ptt_evdev.cpp | 52 ++++++++++++++----- 3 files changed, 48 insertions(+), 54 deletions(-) diff --git a/freedv-server/source/platform/linux/evdev.cpp b/freedv-server/source/platform/linux/evdev.cpp index a68c42a4..ad4e3f35 100644 --- a/freedv-server/source/platform/linux/evdev.cpp +++ b/freedv-server/source/platform/linux/evdev.cpp @@ -10,6 +10,12 @@ #include #include +static bool +bit_set(unsigned int bit, const uint8_t * field) +{ + return ((field[bit / 8] & (1 << (bit % 8))) != 0); +} + namespace FreeDV { // Remove extra spaces from string, in place. char * @@ -53,7 +59,7 @@ namespace FreeDV { struct EvDev::device_enumeration * EvDev::enumerate(std::size_t & count) { - struct device_enumeration devices[100]; + struct device_enumeration devices[64]; count = 0; @@ -107,7 +113,7 @@ namespace FreeDV { device.name = strdup(str.str().c_str()); - if ( test_bit(EV_KEY, device.event_types) == 0 + if ( bit_set(EV_KEY, device.event_types) == 0 || ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(device.buttons)), device.buttons) < 0 ) memset(device.buttons, 0, sizeof(device.buttons)); @@ -115,32 +121,6 @@ namespace FreeDV { return true; } - char * * - EvDev::EnumerateButtonDevices() - { - std::size_t count = 0; - std::size_t length = 0; - device_enumeration * const devices = enumerate(count); - - for ( std::size_t i = 0; i < count; i++ ) { - if ( test_bit(EV_KEY, devices[i].event_types) ) - length++; - } - - char * * names = new char * [length + 1]; - - std::size_t j = 0; - for ( std::size_t i = 0; i < count; i++ ) { - if ( test_bit(EV_KEY, devices[i].event_types) ) - names[j++] = devices[i].name; - devices[i].name = 0; - } - names[j] = 0; - - delete_enumeration(devices, count); - return names; - } - EvDev::EvDev(const char *) { } diff --git a/freedv-server/source/platform/linux/evdev.h b/freedv-server/source/platform/linux/evdev.h index bbf74b0e..794cb4ac 100644 --- a/freedv-server/source/platform/linux/evdev.h +++ b/freedv-server/source/platform/linux/evdev.h @@ -1,16 +1,9 @@ #include #include -#define BITS_PER_LONG (sizeof(long) * 8) -#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1) -#define OFF(x) ((x)%BITS_PER_LONG) -#define BIT(x) (1UL<> OFF(bit)) & 1) - namespace FreeDV { class EvDev { - protected: + public: struct device_enumeration { char * special_file; char * name; @@ -28,7 +21,7 @@ namespace FreeDV { int fd, struct device_enumeration & device); - protected: + public: static void delete_enumeration( device_enumeration * data, std::size_t count); @@ -36,9 +29,6 @@ namespace FreeDV { static device_enumeration * enumerate(std::size_t & count); - public: - static char * * EnumerateButtonDevices(); - EvDev(const char * name); ~EvDev(); }; diff --git a/freedv-server/source/platform/linux/ptt_evdev.cpp b/freedv-server/source/platform/linux/ptt_evdev.cpp index ee0770b4..f3aede69 100644 --- a/freedv-server/source/platform/linux/ptt_evdev.cpp +++ b/freedv-server/source/platform/linux/ptt_evdev.cpp @@ -9,6 +9,12 @@ #include #include +static bool +bit_set(unsigned int bit, const uint8_t * field) +{ + return ((field[bit / 8] & (1 << (bit % 8))) != 0); +} + namespace FreeDV { /// PTT driver using Linux evdev. /// @@ -80,20 +86,38 @@ namespace FreeDV { static std::ostream & PTT_EvDevEnumerator(std::ostream & stream) { - char * * devices = EvDev::EnumerateButtonDevices(); - - char * * d = devices; - - while ( *d != 0 ) - stream << "\"evdev:" << *d++ << '\"' << std::endl; - - d = devices; - - while ( *d != 0 ) - delete *d++; - - delete devices; - + std::size_t count = 0; + EvDev::device_enumeration * const devices = EvDev::enumerate(count); + + for ( std::size_t i = 0; i < count; i++ ) { + if ( bit_set(EV_KEY, devices[i].event_types) ) { + int low = -1; + int high = -1; + + for ( int j = KEY_F1; j <= KEY_MAX; j++ ) { + if ( bit_set(j, devices[i].buttons) ) { + low = j; + break; + } + } + for ( int j = KEY_MAX; j >= KEY_F1; j-- ) { + if ( bit_set(j, devices[i].buttons) ) { + high = j; + break; + } + } + if ( low >= 0 ) { + stream << '\"' << devices[i].name << ',' << low << '\"'; + + if ( high > low ) + stream << " (" << low << '-' << high << ')'; + + stream << std::endl; + } + } + } + + EvDev::delete_enumeration(devices, count); return stream; } -- 2.25.1