[mpg123-devel] [PATCH] term_posix: Ignore escape sequences
Brought to you by:
sobukus
|
From: Peter T. <pe...@ti...> - 2025-10-18 01:10:21
|
I have an issue with occasionally hitting the wrong special key while
using mpg123 in terminal control mode, and depending on the exact
terminal code the terminal emulator sends for that key, it tends to
have an undesirable effect on the state of the player. Mpg123 already
ignores other keys it doesn't recognize, but I wanted it to ignore
complete escape sequences as well.
I did a brief survey of special keys in the terminal emulators I have
easy access to, and I think all these escape sequences can be ignored
by discarding the leading escape character(s), then any occurrences of
the characters `[0123456789;O`, followed by a character not in that
list.
Kitty urxvt Linux console
F1 \eOP \e[11~ \e[[A
Shift-F1 \e[1;2P \e[23~ \e[25~
Ctrl-F1 \e[1;5P \e[11^
Alt-F1 \e[1;3P \e\e[11~
F2 \eOQ \e[12~ \e[[B
F3 \eOR \e[13~ \e[[C
F4 \eOS \e[14~ \e[[D
F5 \e[15~ \e[15~ \e[[E
F6 \e[17~ \e[17~ \e[17~
F7 \e[18~ \e[18~ \e[18~
F8 \e[19~ \e[19~ \e[19~
F9 \e[20~ \e[20~ \e[20~
F10 \e[21~ \e[21~ \e[21~
F11 \e[23~ \e[23~ \e[23~
F12 \e[24~ \e[24~ \e[24~
Shift-F12 \e[24;2~ \e[24$
Ctrl-F12 \e[24;5~ \e[24^
Alt-F12 \e[24;3~ \e\e[24~
Home \e[H \e[7~ \e[1~
End \e[F \e[8~ \e[4~
Insert \e[2~ \e[2~ \e[2~
Delete \e[3~ \e[3~ \e[3~
PgUp \e[5~ \e[5~ \e[5~
PgDn \e[6~ \e[6~ \e[6~
Up \e[A \e[A \e[A
Down \e[B \e[B \e[B
Left \e[D \e[D \e[D
Right \e[C \e[C \e[C
Kp5 \e[E \e[G
Super-PrtSc \e[57361;9u
I've applied the patch below to my copy of mpg123 and it works for me.
I thought I'd share it upstream in case anyone else finds it useful.
Index: src/term_posix.c
===================================================================
--- src/term_posix.c (revision 5528)
+++ src/term_posix.c (working copy)
@@ -196,6 +196,7 @@
#else
fd_set r;
struct timeval t;
+ static int in_escape_seq = 0;
/* Shortcut: If some other means sent a key, use it. */
if(prekey)
@@ -215,9 +216,23 @@
if(select(term_fd+1,&r,NULL,NULL,(stopped) ? NULL : &t) > 0 && FD_ISSET(term_fd,&r))
{
if(read(term_fd,val,1) <= 0)
- return 0; /* Well, we couldn't read the key, so there is none. */
+ return 0; /* Well, we couldn't read the key, so there is none. */
+ if(*val == '\x1b')
+ {
+ /* Ignore escape sequences. */
+ in_escape_seq = 1;
+ return 0;
+ }
+ if(in_escape_seq)
+ {
+ /* Any of the following characters continue the escape sequence. Anything
+ else terminates it. In either case, ignore the current character. */
+ if(!strchr("[0123456789;O", *val))
+ in_escape_seq = 0;
+ return 0;
+ }
else
- return 1;
+ return 1;
}
#endif
else return 0;
--
Peter Tirsek
|