diff options
author | Sascha Hauer <s.hauer@pengutronix.de> | 2021-12-10 15:57:57 +0100 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2021-12-13 23:37:32 +0100 |
commit | 2c451f611781c516152def8fc0d15289f9106af2 (patch) | |
tree | f8bdceec4167def1876f4aaa8f97ed15f841d88e /commands/edit.c | |
parent | 17b170c83021b42a9675d2553f69129568aa792e (diff) | |
download | barebox-2c451f611781c516152def8fc0d15289f9106af2.tar.gz barebox-2c451f611781c516152def8fc0d15289f9106af2.tar.xz |
edit: improve screen size detection
When detecting the screen size iterate over all active consoles and
probe the screen size individually. The smallest sizes are then used
for the editor. This fixes the editor size when multiple consoles are
activated. Also not all consoles may support reading back the cursor
position, so do not wait forever for the answer, but use a timeout
instead.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'commands/edit.c')
-rw-r--r-- | commands/edit.c | 57 |
1 files changed, 48 insertions, 9 deletions
diff --git a/commands/edit.c b/commands/edit.c index f1e4e4d5c2..30448cbc04 100644 --- a/commands/edit.c +++ b/commands/edit.c @@ -12,6 +12,7 @@ #include <errno.h> #include <xfuncs.h> #include <linux/stat.h> +#include <console.h> #define TABSPACE 8 @@ -347,19 +348,57 @@ static void merge_line(struct line *line) static void getwinsize(void) { - int i = 0, r; - char buf[100]; + int n; char *endp; + struct console_device *cdev; + const char esc[] = ESC "7" ESC "[r" ESC "[999;999H" ESC "[6n"; + char buf[64]; - printf(ESC "7" ESC "[r" ESC "[999;999H" ESC "[6n"); + screenwidth = screenheight = 256; - while ((r = getchar()) != 'R') { - buf[i] = r; - i++; - } + for_each_console(cdev) { + int width, height; + uint64_t start; + + if (!(cdev->f_active & CONSOLE_STDIN)) + continue; + if (!(cdev->f_active & CONSOLE_STDOUT)) + continue; + + memset(buf, 0, sizeof(buf)); + + cdev->puts(cdev, esc, sizeof(esc)); + + n = 0; + + start = get_time_ns(); + + while (1) { + if (is_timeout(start, 100 * MSECOND)) + break; - screenheight = simple_strtoul(buf + 2, &endp, 10); - screenwidth = simple_strtoul(endp + 1, NULL, 10); + if (!cdev->tstc(cdev)) + continue; + + buf[n] = cdev->getc(cdev); + + if (buf[n] == 'R') + break; + + n++; + } + + if (buf[0] != 27) + continue; + if (buf[1] != '[') + continue; + + height = simple_strtoul(buf + 2, &endp, 10); + width = simple_strtoul(endp + 1, NULL, 10); + + screenwidth = min(screenwidth, width); + screenheight = min(screenheight, height); + } pos(0, 0); } |