* [PATCH 1/2] include: ktime: move ktime_t definition globally
@ 2026-05-01 6:53 Ahmad Fatoum
2026-05-01 6:53 ` [PATCH 2/2] readkey: handle standalone ESC keypress with timeout Ahmad Fatoum
2026-05-07 11:19 ` [PATCH 1/2] include: ktime: move ktime_t definition globally Sascha Hauer
0 siblings, 2 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2026-05-01 6:53 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
To allow use of the type without including the ktime.h header, define it
in linux/types.h. This is also where Linux defines it.
Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
include/linux/ktime.h | 4 +---
include/linux/types.h | 3 +++
2 files changed, 4 insertions(+), 3 deletions(-)
diff --git a/include/linux/ktime.h b/include/linux/ktime.h
index 2b9a91d5401c..483324a4444f 100644
--- a/include/linux/ktime.h
+++ b/include/linux/ktime.h
@@ -21,6 +21,7 @@
#ifndef _LINUX_KTIME_H
#define _LINUX_KTIME_H
+#include <linux/types.h>
#include <linux/math.h>
#include <linux/time.h>
#include <clock.h>
@@ -31,9 +32,6 @@
#define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC)
#define KTIME_SEC_MIN (KTIME_MIN / NSEC_PER_SEC)
-/* Nanosecond scalar representation for kernel time values */
-typedef s64 ktime_t;
-
/**
* ktime_set - Set a ktime_t variable from a seconds/nanoseconds value
* @secs: seconds to set
diff --git a/include/linux/types.h b/include/linux/types.h
index 93e7fe46295f..6731b5a1b246 100644
--- a/include/linux/types.h
+++ b/include/linux/types.h
@@ -120,6 +120,9 @@ typedef __u64 u_int64_t;
typedef __s64 int64_t;
#endif
+/* Nanosecond scalar representation for kernel time values */
+typedef s64 ktime_t;
+
#endif /* __KERNEL_STRICT_NAMES */
/*
--
2.47.3
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 2/2] readkey: handle standalone ESC keypress with timeout
2026-05-01 6:53 [PATCH 1/2] include: ktime: move ktime_t definition globally Ahmad Fatoum
@ 2026-05-01 6:53 ` Ahmad Fatoum
2026-05-07 11:19 ` [PATCH 1/2] include: ktime: move ktime_t definition globally Sascha Hauer
1 sibling, 0 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2026-05-01 6:53 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
read_key() unconditionally called getchar() twice after receiving an
ESC byte (0x1B), assuming it was the start of a multi-byte escape
sequence. A standalone ESC keypress sends only the single 0x1B byte,
so the second getchar() would block indefinitely, making it impossible
to use ESC on its own to cancel operations.
Poll with tstc() and a 50ms timeout after the initial ESC byte. If no
follow-up character arrives, return the bare ESC. This matches the
standard terminal approach for distinguishing ESC keypresses from
escape sequences, which arrive as a rapid burst.
Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
common/console.c | 13 +++++++++++++
include/stdio.h | 2 ++
lib/readkey.c | 31 +++++++++++++++++++++++++++++--
3 files changed, 44 insertions(+), 2 deletions(-)
diff --git a/common/console.c b/common/console.c
index 0ce89390864b..8b6824b85e43 100644
--- a/common/console.c
+++ b/common/console.c
@@ -565,6 +565,19 @@ int getchar(void)
}
EXPORT_SYMBOL(getchar);
+int pollchar(ktime_t duration)
+{
+ ktime_t start = get_time_ns();
+
+ while (!tstc()) {
+ if (is_timeout(start, duration))
+ return -ETIMEDOUT;
+ }
+
+ return getchar();
+}
+EXPORT_SYMBOL(pollchar);
+
int tstc(void)
{
return kfifo_len(console_input_fifo) || tstc_raw();
diff --git a/include/stdio.h b/include/stdio.h
index f5b23140adde..c8225430ad29 100644
--- a/include/stdio.h
+++ b/include/stdio.h
@@ -12,9 +12,11 @@
/* stdin */
int tstc(void);
int getchar(void);
+int pollchar(ktime_t duration);
#else
static inline int tstc(void) { return 0; }
static inline int getchar(void) { return -EINVAL; }
+static inline int pollchar(ktime_t duration) { return -ENOSYS; }
#endif
int readline(const char *prompt, char *buf, int len);
diff --git a/lib/readkey.c b/lib/readkey.c
index c26e9d51aba9..71f9b41c5055 100644
--- a/lib/readkey.c
+++ b/lib/readkey.c
@@ -17,6 +17,8 @@
*/
#include <common.h>
+#include <clock.h>
+#include <linux/ktime.h>
#include <linux/ctype.h>
#include <readkey.h>
@@ -46,6 +48,18 @@ static const struct esc_cmds esccmds[] = {
{"[6~", BB_KEY_PAGEDOWN},// Cursor Key Page Down
};
+static int poll_key_into_buf(unsigned char *key)
+{
+ int ret;
+
+ ret = pollchar(50 * MSECOND);
+ if (ret < 0)
+ return ret;
+
+ *key = ret;
+ return 0;
+}
+
int read_key(void)
{
unsigned char c;
@@ -54,8 +68,21 @@ int read_key(void)
if (c == 27) {
int i = 0;
- esc[i++] = getchar();
- esc[i++] = getchar();
+
+ /*
+ * Escape sequences (arrow keys, etc.) arrive as a burst
+ * of characters: ESC [ A, ESC [ 1 ~ etc. A standalone
+ * ESC keypress sends just the single 0x1b byte.
+ *
+ * Wait briefly for a follow-up character; if nothing
+ * arrives it was a bare ESC.
+ */
+ if (poll_key_into_buf(&esc[i++]))
+ return '\e';
+
+ if (poll_key_into_buf(&esc[i++]))
+ return -1;
+
if (isdigit(esc[1])) {
while(1) {
esc[i] = getchar();
--
2.47.3
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH 1/2] include: ktime: move ktime_t definition globally
2026-05-01 6:53 [PATCH 1/2] include: ktime: move ktime_t definition globally Ahmad Fatoum
2026-05-01 6:53 ` [PATCH 2/2] readkey: handle standalone ESC keypress with timeout Ahmad Fatoum
@ 2026-05-07 11:19 ` Sascha Hauer
1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2026-05-07 11:19 UTC (permalink / raw)
To: barebox, Ahmad Fatoum
On Fri, 01 May 2026 08:53:17 +0200, Ahmad Fatoum wrote:
> To allow use of the type without including the ktime.h header, define it
> in linux/types.h. This is also where Linux defines it.
>
>
Applied, thanks!
[1/2] include: ktime: move ktime_t definition globally
https://git.pengutronix.de/cgit/barebox/commit/?id=99faea535e80 (link may not be stable)
[2/2] readkey: handle standalone ESC keypress with timeout
https://git.pengutronix.de/cgit/barebox/commit/?id=140096206f2d (link may not be stable)
Best regards,
--
Sascha Hauer <s.hauer@pengutronix.de>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-05-07 11:20 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-05-01 6:53 [PATCH 1/2] include: ktime: move ktime_t definition globally Ahmad Fatoum
2026-05-01 6:53 ` [PATCH 2/2] readkey: handle standalone ESC keypress with timeout Ahmad Fatoum
2026-05-07 11:19 ` [PATCH 1/2] include: ktime: move ktime_t definition globally Sascha Hauer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox