From 125e97b0bf979e1d03b84b17e72fc655b74015fb Mon Sep 17 00:00:00 2001 From: Solomon Peachy Date: Thu, 29 Oct 2020 09:37:57 -0400 Subject: hosted: Consolidate common /dev/input code shared by the hiby targets * Enable dynamic number of input devices, needed for bluetooth support Change-Id: Ic2425834eb9b0fff298899ab45f3115ce1d95e91 --- firmware/SOURCES | 1 + firmware/target/hosted/agptek/button-agptek.c | 84 +---------------- firmware/target/hosted/agptek/button-target.h | 2 + firmware/target/hosted/aigo/button-erosq.c | 106 +-------------------- firmware/target/hosted/aigo/button-target.h | 2 + firmware/target/hosted/button-devinput.c | 130 ++++++++++++++++++++++++++ firmware/target/hosted/fiio/button-fiio.c | 3 - firmware/target/hosted/xduoo/button-target.h | 2 + firmware/target/hosted/xduoo/button-xduoo.c | 88 +---------------- 9 files changed, 144 insertions(+), 274 deletions(-) create mode 100644 firmware/target/hosted/button-devinput.c diff --git a/firmware/SOURCES b/firmware/SOURCES index 7f08e4870f..11929d8253 100644 --- a/firmware/SOURCES +++ b/firmware/SOURCES @@ -145,6 +145,7 @@ target/hosted/xduoo/powermgmt-xduoo.c #if defined(HIBY_LINUX) && !defined(SIMULATOR) target/hosted/usb-hiby.c +target/hosted/button-devinput.c #endif #if (defined(FIIO_M3K)) && !defined(SIMULATOR) diff --git a/firmware/target/hosted/agptek/button-agptek.c b/firmware/target/hosted/agptek/button-agptek.c index 2c84a50405..8fc676acec 100644 --- a/firmware/target/hosted/agptek/button-agptek.c +++ b/firmware/target/hosted/agptek/button-agptek.c @@ -17,25 +17,13 @@ * KIND, either express or implied. * ****************************************************************************/ -#include -//#include -#include -#include -#include #include -#include -#include -#include #include "sysfs.h" #include "button.h" #include "button-target.h" -#include "panic.h" -#define NR_POLL_DESC 2 -static struct pollfd poll_fds[NR_POLL_DESC]; - -static int button_map(int keycode) +int button_map(int keycode) { switch(keycode) { @@ -68,67 +56,6 @@ static int button_map(int keycode) } } -void button_init_device(void) -{ - const char * const input_devs[NR_POLL_DESC] = { - "/dev/input/event0", - "/dev/input/event1" - }; - - for(int i = 0; i < NR_POLL_DESC; i++) - { - int fd = open(input_devs[i], O_RDONLY | O_CLOEXEC); - - if(fd < 0) - { - panicf("Cannot open input device: %s\n", input_devs[i]); - } - - poll_fds[i].fd = fd; - poll_fds[i].events = POLLIN; - poll_fds[i].revents = 0; - } -} - -int button_read_device(void) -{ - static int button_bitmap = 0; - struct input_event event; - - /* check if there are any events pending and process them */ - while(poll(poll_fds, NR_POLL_DESC, 0)) - { - for(int i = 0; i < NR_POLL_DESC; i++) - { - /* read only if non-blocking */ - if(poll_fds[i].revents & POLLIN) - { - int size = read(poll_fds[i].fd, &event, sizeof(event)); - if(size == (int)sizeof(event)) - { - int keycode = event.code; - /* event.value == 0x10000 means press - * event.value == 0 means release - */ - bool press = event.value ? true : false; - - /* map linux event code to rockbox button bitmap */ - if(press) - { - button_bitmap |= button_map(keycode); - } - else - { - button_bitmap &= ~button_map(keycode); - } - } - } - } - } - - return button_bitmap; -} - bool headphones_inserted(void) { int status = 0; @@ -137,12 +64,3 @@ bool headphones_inserted(void) return status ? true : false; } - -void button_close_device(void) -{ - /* close descriptors */ - for(int i = 0; i < NR_POLL_DESC; i++) - { - close(poll_fds[i].fd); - } -} diff --git a/firmware/target/hosted/agptek/button-target.h b/firmware/target/hosted/agptek/button-target.h index e13891a554..66a29f60f7 100644 --- a/firmware/target/hosted/agptek/button-target.h +++ b/firmware/target/hosted/agptek/button-target.h @@ -39,5 +39,7 @@ #define POWEROFF_BUTTON BUTTON_POWER #define POWEROFF_COUNT 25 +int button_map(int keycode); + #endif /* _BUTTON_TARGET_H_ */ diff --git a/firmware/target/hosted/aigo/button-erosq.c b/firmware/target/hosted/aigo/button-erosq.c index db7f7e24e5..1336442370 100644 --- a/firmware/target/hosted/aigo/button-erosq.c +++ b/firmware/target/hosted/aigo/button-erosq.c @@ -6,7 +6,6 @@ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ * \/ \/ \/ \/ \/ * - * Copyright (C) 2017 Marcin Bukat * Copyright (C) 2020 Solomon Peachy * * This program is free software; you can redistribute it and/or @@ -18,30 +17,20 @@ * KIND, either express or implied. * ****************************************************************************/ -#include -//#include -#include -#include -#include #include -#include -#include -#include -#include "sysfs.h" #include "button.h" #include "button-target.h" -#include "panic.h" #include "kernel.h" -#include "backlight.h" -#include "backlight-target.h" #include "erosqlinux_codec.h" -#define NR_POLL_DESC 2 -static struct pollfd poll_fds[NR_POLL_DESC]; +/* + /dev/input/event0: rotary encoder + /dev/input/event1: all keys +*/ -static int button_map(int keycode) +int button_map(int keycode) { switch(keycode) { @@ -80,82 +69,6 @@ static int button_map(int keycode) } } -void button_init_device(void) -{ - const char * const input_devs[NR_POLL_DESC] = { - "/dev/input/event0", // Rotary encoder - "/dev/input/event1" // Keys - }; - - for(int i = 0; i < NR_POLL_DESC; i++) - { - int fd = open(input_devs[i], O_RDONLY | O_CLOEXEC); - - if(fd < 0) - { - panicf("Cannot open input device: %s (%d)\n", input_devs[i], errno); - } - - poll_fds[i].fd = fd; - poll_fds[i].events = POLLIN; - poll_fds[i].revents = 0; - } -} - -int button_read_device(void) -{ - static int button_bitmap = 0; - struct input_event event; - - // FIXME TODO: Make this work via HAVE_SCROLL_WHEEL instead - - /* Wheel gives us press+release back to back, clear them after time elapses */ - static long last_tick = 0; - if (button_bitmap & (BUTTON_SCROLL_BACK|BUTTON_SCROLL_FWD) && - current_tick - last_tick >= 2) - { - button_bitmap &= ~(BUTTON_SCROLL_BACK|BUTTON_SCROLL_FWD); - } - - /* check if there are any events pending and process them */ - while(poll(poll_fds, NR_POLL_DESC, 0)) - { - for(int i = 0; i < NR_POLL_DESC; i++) - { - /* read only if non-blocking */ - if(poll_fds[i].revents & POLLIN) - { - int size = read(poll_fds[i].fd, &event, sizeof(event)); - if(size == (int)sizeof(event)) - { - int keycode = event.code; - /* event.value == 1 means press - * event.value == 0 means release - */ - bool press = event.value ? true : false; - - /* map linux event code to rockbox button bitmap */ - if(press) - { - int bmap = button_map(keycode); - if (bmap & (BUTTON_SCROLL_BACK|BUTTON_SCROLL_FWD)) - last_tick = current_tick; - button_bitmap |= bmap; - } - else - { - /* Wheel gives us press+release back to back; ignore the release */ - int bmap = button_map(keycode) & ~(BUTTON_SCROLL_BACK|BUTTON_SCROLL_FWD); - button_bitmap &= ~bmap; - } - } - } - } - } - - return button_bitmap; -} - bool headphones_inserted(void) { #ifdef BOOTLOADER @@ -177,12 +90,3 @@ bool lineout_inserted(void) return (ps == 1); } - -void button_close_device(void) -{ - /* close descriptors */ - for(int i = 0; i < NR_POLL_DESC; i++) - { - close(poll_fds[i].fd); - } -} diff --git a/firmware/target/hosted/aigo/button-target.h b/firmware/target/hosted/aigo/button-target.h index f59f491d2f..44bd914778 100644 --- a/firmware/target/hosted/aigo/button-target.h +++ b/firmware/target/hosted/aigo/button-target.h @@ -42,4 +42,6 @@ #define POWEROFF_BUTTON BUTTON_POWER #define POWEROFF_COUNT 25 +int button_map(int keycode); + #endif /* _BUTTON_TARGET_H_ */ diff --git a/firmware/target/hosted/button-devinput.c b/firmware/target/hosted/button-devinput.c new file mode 100644 index 0000000000..8c469b8308 --- /dev/null +++ b/firmware/target/hosted/button-devinput.c @@ -0,0 +1,130 @@ +/*************************************************************************** + * __________ __ ___ + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * + * Copyright (C) 2017 Marcin Bukat + * Copyright (C) 2020 Solomon Peachy + + * + * This program 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 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "kernel.h" +#include "sysfs.h" +#include "button.h" +#include "panic.h" + +#define NR_POLL_DESC 4 + +static int num_devices = 0; +static struct pollfd poll_fds[NR_POLL_DESC]; + +void button_init_device(void) +{ + const char * const input_devs[NR_POLL_DESC] = { + "/dev/input/event0", + "/dev/input/event1", + "/dev/input/event2", + "/dev/input/event3", + }; + + for(int i = 0; i < NR_POLL_DESC; i++) + { + int fd = open(input_devs[i], O_RDONLY | O_CLOEXEC); + + if(fd >= 0) + { + poll_fds[num_devices].fd = fd; + poll_fds[num_devices].events = POLLIN; + poll_fds[num_devices].revents = 0; + num_devices++; + } + } +} + +void button_close_device(void) +{ + /* close descriptors */ + for(int i = 0; i < num_devices; i++) + { + close(poll_fds[i].fd); + } + num_devices = 0; +} + +int button_read_device(void) +{ + static int button_bitmap = 0; + struct input_event event; + +#if defined(BUTTON_SCROLL_BACK) + // FIXME TODO: Make this work via HAVE_SCROLL_WHEEL instead + + /* Wheel gives us press+release back to back, clear them after time elapses */ + static long last_tick = 0; + if (button_bitmap & (BUTTON_SCROLL_BACK|BUTTON_SCROLL_FWD) && + current_tick - last_tick >= 2) + { + button_bitmap &= ~(BUTTON_SCROLL_BACK|BUTTON_SCROLL_FWD); + } +#endif + + /* check if there are any events pending and process them */ + while(poll(poll_fds, num_devices, 0)) + { + for(int i = 0; i < num_devices; i++) + { + /* read only if non-blocking */ + if(poll_fds[i].revents & POLLIN) + { + int size = read(poll_fds[i].fd, &event, sizeof(event)); + if(size == (int)sizeof(event)) + { + int keycode = event.code; + /* event.value == 0x10000 means press + * event.value == 0 means release + */ + bool press = event.value ? true : false; + + /* map linux event code to rockbox button bitmap */ + if(press) + { + button_bitmap |= button_map(keycode); + } + else + { +#if defined(BUTTON_SCROLL_BACK) + /* Wheel gives us press+release back to back; ignore the release */ + int bmap = button_map(keycode) & ~(BUTTON_SCROLL_BACK|BUTTON_SCROLL_FWD); + button_bitmap &= ~bmap; +#else + button_bitmap &= ~button_map(keycode); +#endif + } + } + } + } + } + + return button_bitmap; +} diff --git a/firmware/target/hosted/fiio/button-fiio.c b/firmware/target/hosted/fiio/button-fiio.c index 134ec5a426..4acbfb0ea7 100644 --- a/firmware/target/hosted/fiio/button-fiio.c +++ b/firmware/target/hosted/fiio/button-fiio.c @@ -19,7 +19,6 @@ * ****************************************************************************/ #include -//#include #include #include #include @@ -34,8 +33,6 @@ #include "panic.h" #include "kernel.h" -#include "backlight.h" -#include "backlight-target.h" static int key_enter_delay = 0; static int key_right_delay = 0; diff --git a/firmware/target/hosted/xduoo/button-target.h b/firmware/target/hosted/xduoo/button-target.h index 3240d3df8e..20bae1c6f6 100644 --- a/firmware/target/hosted/xduoo/button-target.h +++ b/firmware/target/hosted/xduoo/button-target.h @@ -40,4 +40,6 @@ #define POWEROFF_BUTTON BUTTON_POWER #define POWEROFF_COUNT 25 +int button_map(int keycode); + #endif /* _BUTTON_TARGET_H_ */ diff --git a/firmware/target/hosted/xduoo/button-xduoo.c b/firmware/target/hosted/xduoo/button-xduoo.c index 67cb9a6c46..f5b6c6be62 100644 --- a/firmware/target/hosted/xduoo/button-xduoo.c +++ b/firmware/target/hosted/xduoo/button-xduoo.c @@ -18,30 +18,15 @@ * KIND, either express or implied. * ****************************************************************************/ -#include -//#include -#include -#include -#include #include -#include -#include -#include -#include "sysfs.h" #include "button.h" #include "button-target.h" -#include "panic.h" #include "kernel.h" -#include "backlight.h" -#include "backlight-target.h" #include "xduoolinux_codec.h" -#define NR_POLL_DESC 3 -static struct pollfd poll_fds[NR_POLL_DESC]; - -static int button_map(int keycode) +int button_map(int keycode) { switch(keycode) { @@ -74,68 +59,6 @@ static int button_map(int keycode) } } -void button_init_device(void) -{ - const char * const input_devs[NR_POLL_DESC] = { - "/dev/input/event0", - "/dev/input/event1", - "/dev/input/event2" - }; - - for(int i = 0; i < NR_POLL_DESC; i++) - { - int fd = open(input_devs[i], O_RDONLY | O_CLOEXEC); - - if(fd < 0) - { - panicf("Cannot open input device: %s\n", input_devs[i]); - } - - poll_fds[i].fd = fd; - poll_fds[i].events = POLLIN; - poll_fds[i].revents = 0; - } -} - -int button_read_device(void) -{ - static int button_bitmap = 0; - struct input_event event; - - /* check if there are any events pending and process them */ - while(poll(poll_fds, NR_POLL_DESC, 0)) - { - for(int i = 0; i < NR_POLL_DESC; i++) - { - /* read only if non-blocking */ - if(poll_fds[i].revents & POLLIN) - { - int size = read(poll_fds[i].fd, &event, sizeof(event)); - if(size == (int)sizeof(event)) - { - int keycode = event.code; - /* event.value == 1 means press - * event.value == 0 means release - */ - bool press = event.value ? true : false; - - /* map linux event code to rockbox button bitmap */ - if(press) - { - button_bitmap |= button_map(keycode); - } - else - { - button_bitmap &= ~button_map(keycode); - } - } - } - } - } - - return button_bitmap; -} - bool headphones_inserted(void) { #ifdef BOOTLOADER @@ -156,12 +79,3 @@ bool lineout_inserted(void) #endif return (ps == 1); } - -void button_close_device(void) -{ - /* close descriptors */ - for(int i = 0; i < NR_POLL_DESC; i++) - { - close(poll_fds[i].fd); - } -} -- cgit v1.2.3