[PATCH 13/34] video: Add a function to clear the display

Simon Glass sjg at chromium.org
Mon Oct 2 03:15:23 CEST 2023


Move the code from the 'cls' command into the console file, so it can
be called from elsewhere.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

 cmd/cls.c         | 25 +++----------------------
 common/console.c  | 31 +++++++++++++++++++++++++++++++
 include/console.h | 10 ++++++++++
 3 files changed, 44 insertions(+), 22 deletions(-)

diff --git a/cmd/cls.c b/cmd/cls.c
index 1125a3f81bbb..80d0558d4679 100644
--- a/cmd/cls.c
+++ b/cmd/cls.c
@@ -7,33 +7,14 @@
  */
 #include <common.h>
 #include <command.h>
+#include <console.h>
 #include <dm.h>
-#include <video_console.h>
-
-#define CSI "\x1b["
 
 static int do_video_clear(struct cmd_tbl *cmdtp, int flag, int argc,
 			  char *const argv[])
 {
-	__maybe_unused struct udevice *dev;
-
-	/*
-	 * Send clear screen and home
-	 *
-	 * FIXME(Heinrich Schuchardt <xypron.glpk at gmx.de>): This should go
-	 * through an API and only be written to serial terminals, not video
-	 * displays
-	 */
-	printf(CSI "2J" CSI "1;1H");
-	if (IS_ENABLED(CONFIG_VIDEO_ANSI))
-		return 0;
-
-	if (IS_ENABLED(CONFIG_VIDEO)) {
-		if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
-			return CMD_RET_FAILURE;
-		if (vidconsole_clear_and_reset(dev))
-			return CMD_RET_FAILURE;
-	}
+	if (console_clear())
+		return CMD_RET_FAILURE;
 
 	return CMD_RET_SUCCESS;
 }
diff --git a/common/console.c b/common/console.c
index 98c3ee6ca6b8..1ffda49c87e0 100644
--- a/common/console.c
+++ b/common/console.c
@@ -19,12 +19,15 @@
 #include <stdio_dev.h>
 #include <exports.h>
 #include <env_internal.h>
+#include <video_console.h>
 #include <watchdog.h>
 #include <asm/global_data.h>
 #include <linux/delay.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
+#define CSI "\x1b["
+
 static int on_console(const char *name, const char *value, enum env_op op,
 	int flags)
 {
@@ -1010,6 +1013,34 @@ int console_init_f(void)
 	return 0;
 }
 
+int console_clear(void)
+{
+	/*
+	 * Send clear screen and home
+	 *
+	 * FIXME(Heinrich Schuchardt <xypron.glpk at gmx.de>): This should go
+	 * through an API and only be written to serial terminals, not video
+	 * displays
+	 */
+	printf(CSI "2J" CSI "1;1H");
+	if (IS_ENABLED(CONFIG_VIDEO_ANSI))
+		return 0;
+
+	if (IS_ENABLED(CONFIG_VIDEO)) {
+		struct udevice *dev;
+		int ret;
+
+		ret = uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev);
+		if (ret)
+			return ret;
+		ret = vidconsole_clear_and_reset(dev);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
 static void stdio_print_current_devices(void)
 {
 	char *stdinname, *stdoutname, *stderrname;
diff --git a/include/console.h b/include/console.h
index ceb733b5cb69..e29817e57b00 100644
--- a/include/console.h
+++ b/include/console.h
@@ -156,6 +156,16 @@ int console_announce_r(void);
  */
 void console_puts_select_stderr(bool serial_only, const char *s);
 
+/**
+ * console_clear() - Clear the console
+ *
+ * Uses an ANSI sequence to clear the display, failing back to clearing the
+ * video display directly if !CONFIG_VIDEO_ANSI
+ *
+ * Return: 0 if OK, -ve on error
+ */
+int console_clear(void);
+
 /*
  * CONSOLE multiplexing.
  */
-- 
2.42.0.582.g8ccd20d70d-goog



More information about the U-Boot mailing list