From patchwork Wed Sep 13 22:12:21 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rob Clark X-Patchwork-Id: 813649 X-Patchwork-Delegate: agust@denx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="ao1dqhBU"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 3xswxl4fL4z9s76 for ; Thu, 14 Sep 2017 08:16:59 +1000 (AEST) Received: by lists.denx.de (Postfix, from userid 105) id 64493C22734; Wed, 13 Sep 2017 22:15:42 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 4194DC226F1; Wed, 13 Sep 2017 22:15:39 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id CF74EC226EC; Wed, 13 Sep 2017 22:12:45 +0000 (UTC) Received: from mail-qt0-f196.google.com (mail-qt0-f196.google.com [209.85.216.196]) by lists.denx.de (Postfix) with ESMTPS id 581AAC225FA for ; Wed, 13 Sep 2017 22:12:39 +0000 (UTC) Received: by mail-qt0-f196.google.com with SMTP id q8so956225qtb.1 for ; Wed, 13 Sep 2017 15:12:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=6aYzn8t2pRojHu/tD/wxkbPdEm4nfbOHbDAbS+PeC9k=; b=ao1dqhBUBznzz3W3//116f6tv+B0+cZ9vRIIYolWS9U8RVQHqNsYpnR+GK5DPbOSHP 2iteKrII0xP/cwqJsi272zNSNn4A7/XCvmrNmXfWdNKrJgZMIiCFARbgyeX+B6F4Abmf 81fHxRKC54v1A64SQHNrvR+0NsR4AFLX67W/WE6VU3eI3Hw+XxGMzZiG8fJM3hyb/753 9jb7ODUX3FmMJWzhbQIG1kxI2X7v/oRkfPj5iChJv2PMb/zHYi1L/0tacaB6P9Xc8/Mi EO1mrv3KOtF4mpGGpLZJyXm4r9sea/rSl7cuqNAmZw36bS0sWoyuMDSvwcxXpwPn/nq3 kAvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=6aYzn8t2pRojHu/tD/wxkbPdEm4nfbOHbDAbS+PeC9k=; b=JkRq2X4ROmgKV99TeZZzUnC1i3SlR5qT5Vd1cY0lMQQ7X3EL58dYTZf4pOMOz4WHrd k+UiNPUE8HjJ8lphlu62g2jfBjJgoT1UTPXIy0IU57dv0CHXxNW3K0D4S04oU4kiYCkN vAU60ZQe1k1DHRGusKjpHUtE4KNg0wbmqf8SJP2NdOffXxQogjvVVPYYPKKgP5lDZDiu G+U7kKVprQhIj3fzUJn+eH5uRd5jL8655nbUQvGxkXoBhkzsyZhIUnOrfiU1YcOr21NO NUTj+oSelNYJ4QdlBUTV0tbg3eMhS/Rs9J1k8ipDZdUCXEe/flqGcWJXr011mD9h9tg1 1row== X-Gm-Message-State: AHPjjUhkcojSuR/Dmjv96524x0+2VPuDOARJrEq311jdowhrayhYMWsL miydbhC7Q6LpRE8+C1g= X-Google-Smtp-Source: AOwi7QB9tX0BKzGhTroyeEPaWvjS0I48tMmNDGdVcsCtPG5HkzY44gQRDuxiOZ3xNxfH8oi0sz9qcg== X-Received: by 10.237.33.188 with SMTP id l57mr14103257qtc.270.1505340757964; Wed, 13 Sep 2017 15:12:37 -0700 (PDT) Received: from localhost ([2601:184:4780:aac0:25f8:dd96:a084:785a]) by smtp.gmail.com with ESMTPSA id m126sm9872979qkd.52.2017.09.13.15.12.36 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 13 Sep 2017 15:12:36 -0700 (PDT) From: Rob Clark To: U-Boot Mailing List Date: Wed, 13 Sep 2017 18:12:21 -0400 Message-Id: <20170913221227.21091-3-robdclark@gmail.com> X-Mailer: git-send-email 2.13.5 In-Reply-To: <20170913221227.21091-1-robdclark@gmail.com> References: <20170913221227.21091-1-robdclark@gmail.com> Subject: [U-Boot] [PATCH 2/3] dm: video: Add basic ANSI escape sequence support X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" Really just the subset that is needed by efi_console. Perhaps more will be added later, for example color support would be useful to implement efi_cout_set_attribute(). Signed-off-by: Rob Clark Reviewed-by: Simon Glass --- drivers/video/Kconfig | 8 +++ drivers/video/vidconsole-uclass.c | 109 ++++++++++++++++++++++++++++++++++++++ drivers/video/video-uclass.c | 4 +- include/video.h | 7 +++ include/video_console.h | 11 ++++ 5 files changed, 136 insertions(+), 3 deletions(-) diff --git a/drivers/video/Kconfig b/drivers/video/Kconfig index 082cc4a528..add156cb70 100644 --- a/drivers/video/Kconfig +++ b/drivers/video/Kconfig @@ -44,6 +44,14 @@ config VIDEO_BPP32 this option, such displays will not be supported and console output will be empty. +config VIDEO_ANSI + bool "Support ANSI escape sequences in video console" + depends on DM_VIDEO + default y if DM_VIDEO + help + Enable ANSI escape sequence decoding for a more fully functional + console. + config CONSOLE_NORMAL bool "Support a simple text console" depends on DM_VIDEO diff --git a/drivers/video/vidconsole-uclass.c b/drivers/video/vidconsole-uclass.c index e081d5a0ee..0a88cc0a42 100644 --- a/drivers/video/vidconsole-uclass.c +++ b/drivers/video/vidconsole-uclass.c @@ -9,6 +9,7 @@ */ #include +#include #include #include #include @@ -107,12 +108,120 @@ static void vidconsole_newline(struct udevice *dev) video_sync(dev->parent); } +static char *parsenum(char *s, int *num) +{ + char *end; + *num = simple_strtol(s, &end, 10); + return end; +} + +/* + * Process a character while accumulating an escape string. Chars are + * accumulated into escape_buf until the end of escape sequence is + * found, at which point the sequence is parsed and processed. + */ +static void vidconsole_escape_char(struct udevice *dev, char ch) +{ + struct vidconsole_priv *priv = dev_get_uclass_priv(dev); + + if (!IS_ENABLED(CONFIG_VIDEO_ANSI)) + goto error; + + /* Sanity checking for bogus ESC sequences: */ + if (priv->escape_len >= sizeof(priv->escape_buf)) + goto error; + if (priv->escape_len == 0 && ch != '[') + goto error; + + priv->escape_buf[priv->escape_len++] = ch; + + /* + * Escape sequences are terminated by a letter, so keep + * accumulating until we get one: + */ + if (!isalpha(ch)) + return; + + /* + * clear escape mode first, otherwise things will get highly + * surprising if you hit any debug prints that come back to + * this console. + */ + priv->escape = 0; + + switch (ch) { + case 'H': + case 'f': { + int row, col; + char *s = priv->escape_buf; + + /* + * Set cursor position: [%d;%df or [%d;%dH + */ + s++; /* [ */ + s = parsenum(s, &row); + s++; /* ; */ + s = parsenum(s, &col); + + priv->ycur = row * priv->y_charsize; + priv->xcur_frac = priv->xstart_frac + + VID_TO_POS(col * priv->x_charsize); + + break; + } + case 'J': { + int mode; + + /* + * Clear part/all screen: + * [J or [0J - clear screen from cursor down + * [1J - clear screen from cursor up + * [2J - clear entire screen + * + * TODO we really only handle entire-screen case, others + * probably require some additions to video-uclass (and + * are not really needed yet by efi_console) + */ + parsenum(priv->escape_buf + 1, &mode); + + if (mode == 2) { + video_clear(dev->parent); + video_sync(dev->parent); + priv->ycur = 0; + priv->xcur_frac = priv->xstart_frac; + } else { + debug("unsupported clear mode: %d\n", mode); + } + break; + } + default: + debug("unrecognized escape sequence: %*s\n", + priv->escape_len, priv->escape_buf); + } + + return; + +error: + /* something went wrong, just revert to normal mode: */ + priv->escape = 0; + return; +} + int vidconsole_put_char(struct udevice *dev, char ch) { struct vidconsole_priv *priv = dev_get_uclass_priv(dev); int ret; + if (priv->escape) { + vidconsole_escape_char(dev, ch); + return 0; + } + switch (ch) { + case '\x1b': + priv->escape_len = 0; + priv->escape = 1; + break; case '\a': /* beep */ break; diff --git a/drivers/video/video-uclass.c b/drivers/video/video-uclass.c index 3036e3a1f2..0163039821 100644 --- a/drivers/video/video-uclass.c +++ b/drivers/video/video-uclass.c @@ -87,7 +87,7 @@ int video_reserve(ulong *addrp) return 0; } -static int video_clear(struct udevice *dev) +void video_clear(struct udevice *dev) { struct video_priv *priv = dev_get_uclass_priv(dev); @@ -100,8 +100,6 @@ static int video_clear(struct udevice *dev) } else { memset(priv->fb, priv->colour_bg, priv->fb_size); } - - return 0; } /* Flush video activity to the caches */ diff --git a/include/video.h b/include/video.h index 5b4e78b182..61ff653121 100644 --- a/include/video.h +++ b/include/video.h @@ -115,6 +115,13 @@ struct video_ops { int video_reserve(ulong *addrp); /** + * video_clear() - Clear a device's frame buffer to background color. + * + * @dev: Device to clear + */ +void video_clear(struct udevice *dev); + +/** * video_sync() - Sync a device's frame buffer with its hardware * * Some frame buffers are cached or have a secondary frame buffer. This diff --git a/include/video_console.h b/include/video_console.h index 26047934da..9dce234bd9 100644 --- a/include/video_console.h +++ b/include/video_console.h @@ -29,6 +29,9 @@ * @xsize_frac: Width of the display in fractional units * @xstart_frac: Left margin for the text console in fractional units * @last_ch: Last character written to the text console on this line + * @escape: TRUE if currently accumulating an ANSI escape sequence + * @escape_len: Length of accumulated escape sequence so far + * @escape_buf: Buffer to accumulate escape sequence */ struct vidconsole_priv { struct stdio_dev sdev; @@ -42,6 +45,14 @@ struct vidconsole_priv { int xsize_frac; int xstart_frac; int last_ch; + /* + * ANSI escape sequences are accumulated character by character, + * starting after the ESC char (0x1b) until the entire sequence + * is consumed at which point it is acted upon. + */ + int escape; + int escape_len; + char escape_buf[32]; }; /**