Rockbox open source high quality audio player as a Music Player Daemon
mpris rockbox mpd libadwaita audio rust zig deno
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

hiby: ability to add/remove input device

Required for dynamic inputs (like bluetooth).
Files are now opened with O_NONBLOCK, and inputs are automatically removed on poll errors.
Also added call to close all devices on power off.

Change-Id: I8991bdb881fdc00135d1fd5b01ac900c0b007aeb

authored by

Roman Artiukhin and committed by
Solomon Peachy
a496e011 f4dc4d89

+73 -20
+43 -20
firmware/target/hosted/button-devinput.c
··· 21 21 ****************************************************************************/ 22 22 #include <poll.h> 23 23 #include <errno.h> 24 + #include <stdio.h> 24 25 #include <unistd.h> 25 26 #include <sys/types.h> 26 27 #include <linux/input.h> ··· 49 50 * Compute to angular velocity (degrees per second) 50 51 51 52 */ 52 - #define NR_POLL_DESC 4 53 + #define NR_POLL_DESC 5 53 54 54 55 static int num_devices = 0; 55 56 static struct pollfd poll_fds[NR_POLL_DESC]; 56 57 57 - void button_init_device(void) 58 + void button_add_input_device(int i) 58 59 { 59 - const char * const input_devs[NR_POLL_DESC] = { 60 - "/dev/input/event0", 61 - "/dev/input/event1", 62 - "/dev/input/event2", 63 - "/dev/input/event3", 64 - }; 60 + int fd = poll_fds[i].fd; 61 + if (fd >= 0) 62 + close(fd); 65 63 64 + char path[32]; 65 + snprintf(path, sizeof(path), "/dev/input/event%d", i); 66 + fd = open(path, O_RDONLY | O_CLOEXEC | O_NONBLOCK); 67 + poll_fds[i].fd = fd >= 0 ? fd : -1; 68 + if(fd >= 0) 69 + { 70 + poll_fds[i].events = POLLIN; 71 + poll_fds[i].revents = 0; 72 + if (num_devices <= i) 73 + num_devices = i + 1; 74 + } 75 + } 76 + 77 + void button_init_device(void) 78 + { 66 79 for(int i = 0; i < NR_POLL_DESC; i++) 67 80 { 68 - int fd = open(input_devs[i], O_RDONLY | O_CLOEXEC); 81 + poll_fds[i].fd = -1; 82 + button_add_input_device(i); 83 + } 84 + } 85 + 86 + void button_remove_input_device(int i) 87 + { 88 + int fd = poll_fds[i].fd; 89 + if (fd < 0) 90 + return; 91 + 92 + if (i == num_devices - 1) 93 + num_devices = i; 69 94 70 - if(fd >= 0) 71 - { 72 - poll_fds[num_devices].fd = fd; 73 - poll_fds[num_devices].events = POLLIN; 74 - poll_fds[num_devices].revents = 0; 75 - num_devices++; 76 - } 77 - } 95 + close(fd); 96 + poll_fds[i].fd = -1; 78 97 } 79 98 80 99 void button_close_device(void) 81 100 { 101 + num_devices = 0; 82 102 /* close descriptors */ 83 - for(int i = 0; i < num_devices; i++) 103 + for(int i = 0; i < NR_POLL_DESC; i++) 84 104 { 85 - close(poll_fds[i].fd); 105 + button_remove_input_device(i); 86 106 } 87 - num_devices = 0; 88 107 } 89 108 90 109 #ifdef BUTTON_DELAY_RELEASE ··· 260 279 break; 261 280 } 262 281 } 282 + } 283 + /* device was removed/disconnected — close it to stop poll returning POLLHUP forever */ 284 + else if (poll_fds[i].revents & (POLLERR | POLLHUP)) { 285 + button_remove_input_device(i); 263 286 } 264 287 } 265 288 }
+28
firmware/target/hosted/button-devinput.h
··· 1 + /*************************************************************************** 2 + * __________ __ ___ 3 + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ 4 + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / 5 + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < 6 + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ 7 + * \/ \/ \/ \/ \/ 8 + * 9 + * Copyright (C) 2026 by Roman Artiukhin 10 + * 11 + * This program is free software; you can redistribute it and/or 12 + * modify it under the terms of the GNU General Public License 13 + * as published by the Free Software Foundation; either version 2 14 + * of the License, or (at your option) any later version. 15 + * 16 + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 17 + * KIND, either express or implied. 18 + ****************************************************************************/ 19 + 20 + 21 + #ifndef __BUTTON_DEVINPUT_H__ 22 + #define __BUTTON_DEVINPUT_H__ 23 + 24 + void button_close_device(void); 25 + void button_add_input_device(int i); 26 + void button_remove_input_device(int i); 27 + 28 + #endif
+2
firmware/target/hosted/system-hosted.c
··· 29 29 #include "font.h" 30 30 #include "power.h" 31 31 #include "button.h" 32 + #include "button-devinput.h" 32 33 #include "backlight-target.h" 33 34 #include "lcd.h" 34 35 #include "filesystem-hosted.h" ··· 92 93 void power_off(void) 93 94 { 94 95 backlight_hw_off(); 96 + button_close_device(); 95 97 sync(); 96 98 system("/sbin/poweroff"); 97 99 while (1) {