From patchwork Thu Apr 28 08:09:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 567137 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:6886:0:0:0:0 with SMTP id m6csp5233580map; Thu, 28 Apr 2022 01:13:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzbzneAyEUITj14Ixq5ba/RukEaWx/6EG/6Mjo5YyEidiv93V7AIRpUyUmExPw8g6O6MP0A X-Received: by 2002:a17:907:728c:b0:6e8:a052:4f03 with SMTP id dt12-20020a170907728c00b006e8a0524f03mr29619871ejc.344.1651133602587; Thu, 28 Apr 2022 01:13:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651133602; cv=none; d=google.com; s=arc-20160816; b=xyvvhyXRtXkPMzrSz9zjuhZmYZq7p6V4et/CiF2wzHE5vBOawk7cwdiKTrRz/jYmNA JdGSHBgHH0MScgIQt68KYWIN3sEPVTzR5+P4hgHwuqMNP0NQcB2v5NJSqS3cTy66e8Oj 6tUV/58gwyykaV943wCcY+foayzt35ragULePD3Je0uUTWWPCQh4xzMqp68ZkVrmTzkr rlwmPC+JYriTHiRGJ6g5nM5jatmPjWBoiA4YJ6VYoZSM0oT9SGvfolrngVdj3E6spvjh DxLbMCg5GGAD30X9f+WDh4t04qd5D/rBHdPpYEEGiiqYze/4Rew1V7POG/jjZUMl6gHE PmEQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=zBDapxkVgOA+Ow6v/YmlE9HXGz7la71K6woowRKURzI=; b=qsVzNHDKiL3bNZ7TVW/wCq54IRnNkIS/GExoyJua2+puG1drnNfBRXRSUK8QIR7um1 uPojQRpxB64XBTGQwEuiHDAVNN9hBISy2tlLIPoUkvC9LAmPG4ix3EZJHLqL7kdMf6Lg 3BlmzitsnUMyPujOCAGjJBdDKG9x4YSpG6YzpTORClokSUWXeAIKZNbayLUz+33YWSvS kmXXcLwErKXsO6mqMIKE5woscjay8S03hyxJWM6cr7bnC+C5g5EEh+tQ4gPm0S+HrNGo T6s4JYMD/4e0m2ezJ+iWFnqaVgVfnVJ3Wb/OhJwL+Q7z9/3FyjFIVWykIwVTSaiDKWGF wRNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Z9yp3kJu; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id i9-20020a508709000000b00423c3e6e1e0si3428042edb.567.2022.04.28.01.13.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 01:13:22 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Z9yp3kJu; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 38A0D81DB4; Thu, 28 Apr 2022 10:12:44 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="Z9yp3kJu"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id A2A8383EE2; Thu, 28 Apr 2022 10:11:25 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 656CA834AD for ; Thu, 28 Apr 2022 10:11:12 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=masahisa.kojima@linaro.org Received: by mail-pl1-x62a.google.com with SMTP id b12so3697432plg.4 for ; Thu, 28 Apr 2022 01:11:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=zBDapxkVgOA+Ow6v/YmlE9HXGz7la71K6woowRKURzI=; b=Z9yp3kJu1fiIxqA4Pd37Zo/YF893cKd3OCHyUqikAQ6r8xlag00YmVphJR0pCM0aL1 C4kYkYpxL+ryGiLoygNVziQQjvADQJehWw+TW6KlbvwP+vmV+O5/8RcIdXTgh005hHd2 HyRyhSyUFxXBu7vlS06+DO+BGBGQwRohaJVbfpHoEqeNXYm+q6lSlOFvhHuEqWDTWR4w mESDp3msn0kYGylz64RQi6mrc5v/BG2X+PSMPVCOe0jlfkiorHzo9NQjNYK53Xz0euif iCh5F2oIFolZgtJpZpYnEO5NCx5XhCM8/bhuEo4b4LLSyOmMkOLUtBfgPBagFnGJnWTg sOzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=zBDapxkVgOA+Ow6v/YmlE9HXGz7la71K6woowRKURzI=; b=m9TgGhA5RTI9QKqAPx8xubDSxDMpAnAwGZcMhHc0PQqjofIbCkRv3+hIpPsn80OraI 5TZdItyzeDkMlbcmgW3moAWh1JYgdWL16rmvWkWyGwnAz4R4kWVpJG6EgvUKyUZ6pLCP f3tfhK41MkosEgPK9215fjgdGV7uD+X9VBEIvTIo3z90phDcz5How/eEjbOSwblq6ZAV OGd90KeeG0Y6IhUkgbi8xTrFsGdU0CxtngLcmXJ6myvYr81+pYF+BlKou+bcYUpsfla6 jWrh21ggJ4m+lzI+PzW5lqG7nRT5pP2J7wmSl+MOB171WMKO4It29TW0H6Axi5jWEtvW 8CdA== X-Gm-Message-State: AOAM532jbXDklHix3ufsbGhEnU1SS6+XwSqaEf0Ga3nbX97fzNdyrOTe b+qfK252yrcu3LRnrN2PtMHLP7TCtrS2Ng== X-Received: by 2002:a17:90b:1b4f:b0:1d2:a09e:1fc6 with SMTP id nv15-20020a17090b1b4f00b001d2a09e1fc6mr47907553pjb.147.1651133470625; Thu, 28 Apr 2022 01:11:10 -0700 (PDT) Received: from localhost.localdomain ([240d:1a:cf7:5800:82fa:5bff:fe4b:26b1]) by smtp.gmail.com with ESMTPSA id b15-20020a17090a7acf00b001cd4989ff5fsm5684259pjl.38.2022.04.28.01.11.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 01:11:10 -0700 (PDT) From: Masahisa Kojima To: u-boot@lists.denx.de Cc: Heinrich Schuchardt , Ilias Apalodimas , Simon Glass , Takahiro Akashi , Francois Ozog , Mark Kettenis , Masahisa Kojima Subject: [PATCH v5 12/17] bootmenu: factor out the user input handling Date: Thu, 28 Apr 2022 17:09:45 +0900 Message-Id: <20220428080950.23509-13-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220428080950.23509-1-masahisa.kojima@linaro.org> References: <20220428080950.23509-1-masahisa.kojima@linaro.org> X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.5 at phobos.denx.de X-Virus-Status: Clean This commit moves the user input handling from cmd/bootmenu.c to common/menu.c to reuse it from other modules. Signed-off-by: Masahisa Kojima --- No change since v4 Newly created in v4. cmd/bootmenu.c | 149 ------------------------------------------------- common/menu.c | 137 +++++++++++++++++++++++++++++++++++++++++++++ include/menu.h | 20 +++++++ 3 files changed, 157 insertions(+), 149 deletions(-) diff --git a/cmd/bootmenu.c b/cmd/bootmenu.c index bfbb1b5248..eb23afdd41 100644 --- a/cmd/bootmenu.c +++ b/cmd/bootmenu.c @@ -54,21 +54,6 @@ struct bootmenu_entry { struct bootmenu_entry *next; /* next menu entry (num+1) */ }; -struct bootmenu_data { - int delay; /* delay for autoboot */ - int active; /* active menu entry */ - int count; /* total count of menu entries */ - struct bootmenu_entry *first; /* first menu entry */ -}; - -enum bootmenu_key { - KEY_NONE = 0, - KEY_UP, - KEY_DOWN, - KEY_SELECT, - KEY_QUIT, -}; - static char *bootmenu_getoption(unsigned short int n) { char name[MAX_ENV_SIZE]; @@ -109,140 +94,6 @@ static void bootmenu_print_entry(void *data) puts(ANSI_COLOR_RESET); } -static void bootmenu_autoboot_loop(struct bootmenu_data *menu, - enum bootmenu_key *key, int *esc) -{ - int i, c; - - if (menu->delay > 0) { - /* flush input */ - while (tstc()) - getchar(); - - printf(ANSI_CURSOR_POSITION, menu->count + 5, 1); - printf(" Hit any key to stop autoboot: %2d ", menu->delay); - } - - while (menu->delay > 0) { - for (i = 0; i < 100; ++i) { - if (!tstc()) { - WATCHDOG_RESET(); - mdelay(10); - continue; - } - - menu->delay = -1; - c = getchar(); - - switch (c) { - case '\e': - *esc = 1; - *key = KEY_NONE; - break; - case '\r': - *key = KEY_SELECT; - break; - case 0x3: /* ^C */ - *key = KEY_QUIT; - break; - default: - *key = KEY_NONE; - break; - } - - break; - } - - if (menu->delay < 0) - break; - - --menu->delay; - printf("\b\b\b%2d ", menu->delay); - } - - printf(ANSI_CURSOR_POSITION, menu->count + 5, 1); - puts(ANSI_CLEAR_LINE); - - if (menu->delay == 0) - *key = KEY_SELECT; -} - -static void bootmenu_loop(struct bootmenu_data *menu, - enum bootmenu_key *key, int *esc) -{ - int c; - - if (*esc == 1) { - if (tstc()) { - c = getchar(); - } else { - WATCHDOG_RESET(); - mdelay(10); - if (tstc()) - c = getchar(); - else - c = '\e'; - } - } else { - while (!tstc()) { - WATCHDOG_RESET(); - mdelay(10); - } - c = getchar(); - } - - switch (*esc) { - case 0: - /* First char of ANSI escape sequence '\e' */ - if (c == '\e') { - *esc = 1; - *key = KEY_NONE; - } - break; - case 1: - /* Second char of ANSI '[' */ - if (c == '[') { - *esc = 2; - *key = KEY_NONE; - } else { - /* Alone ESC key was pressed */ - *key = KEY_QUIT; - *esc = (c == '\e') ? 1 : 0; - } - break; - case 2: - case 3: - /* Third char of ANSI (number '1') - optional */ - if (*esc == 2 && c == '1') { - *esc = 3; - *key = KEY_NONE; - break; - } - - *esc = 0; - - /* ANSI 'A' - key up was pressed */ - if (c == 'A') - *key = KEY_UP; - /* ANSI 'B' - key down was pressed */ - else if (c == 'B') - *key = KEY_DOWN; - /* other key was pressed */ - else - *key = KEY_NONE; - - break; - } - - /* enter key was pressed */ - if (c == '\r') - *key = KEY_SELECT; - - /* ^C was pressed */ - if (c == 0x3) - *key = KEY_QUIT; -} - static char *bootmenu_choice_entry(void *data) { struct bootmenu_data *menu = data; diff --git a/common/menu.c b/common/menu.c index 4118c6dc3c..53b026b52e 100644 --- a/common/menu.c +++ b/common/menu.c @@ -4,11 +4,14 @@ * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. */ +#include #include #include #include #include +#include #include +#include #include "menu.h" @@ -421,3 +424,137 @@ int menu_destroy(struct menu *m) return 1; } + +void bootmenu_autoboot_loop(struct bootmenu_data *menu, + enum bootmenu_key *key, int *esc) +{ + int i, c; + + if (menu->delay > 0) { + /* flush input */ + while (tstc()) + getchar(); + + printf(ANSI_CURSOR_POSITION, menu->count + 5, 1); + printf(" Hit any key to stop autoboot: %2d ", menu->delay); + } + + while (menu->delay > 0) { + for (i = 0; i < 100; ++i) { + if (!tstc()) { + WATCHDOG_RESET(); + mdelay(10); + continue; + } + + menu->delay = -1; + c = getchar(); + + switch (c) { + case '\e': + *esc = 1; + *key = KEY_NONE; + break; + case '\r': + *key = KEY_SELECT; + break; + case 0x3: /* ^C */ + *key = KEY_QUIT; + break; + default: + *key = KEY_NONE; + break; + } + + break; + } + + if (menu->delay < 0) + break; + + --menu->delay; + printf("\b\b\b%2d ", menu->delay); + } + + printf(ANSI_CURSOR_POSITION, menu->count + 5, 1); + puts(ANSI_CLEAR_LINE); + + if (menu->delay == 0) + *key = KEY_SELECT; +} + +void bootmenu_loop(struct bootmenu_data *menu, + enum bootmenu_key *key, int *esc) +{ + int c; + + if (*esc == 1) { + if (tstc()) { + c = getchar(); + } else { + WATCHDOG_RESET(); + mdelay(10); + if (tstc()) + c = getchar(); + else + c = '\e'; + } + } else { + while (!tstc()) { + WATCHDOG_RESET(); + mdelay(10); + } + c = getchar(); + } + + switch (*esc) { + case 0: + /* First char of ANSI escape sequence '\e' */ + if (c == '\e') { + *esc = 1; + *key = KEY_NONE; + } + break; + case 1: + /* Second char of ANSI '[' */ + if (c == '[') { + *esc = 2; + *key = KEY_NONE; + } else { + /* Alone ESC key was pressed */ + *key = KEY_QUIT; + *esc = (c == '\e') ? 1 : 0; + } + break; + case 2: + case 3: + /* Third char of ANSI (number '1') - optional */ + if (*esc == 2 && c == '1') { + *esc = 3; + *key = KEY_NONE; + break; + } + + *esc = 0; + + /* ANSI 'A' - key up was pressed */ + if (c == 'A') + *key = KEY_UP; + /* ANSI 'B' - key down was pressed */ + else if (c == 'B') + *key = KEY_DOWN; + /* other key was pressed */ + else + *key = KEY_NONE; + + break; + } + + /* enter key was pressed */ + if (c == '\r') + *key = KEY_SELECT; + + /* ^C was pressed */ + if (c == 0x3) + *key = KEY_QUIT; +} diff --git a/include/menu.h b/include/menu.h index ad5859437e..e74616cae8 100644 --- a/include/menu.h +++ b/include/menu.h @@ -35,4 +35,24 @@ int menu_default_choice(struct menu *m, void **choice); */ int menu_show(int bootdelay); +struct bootmenu_data { + int delay; /* delay for autoboot */ + int active; /* active menu entry */ + int count; /* total count of menu entries */ + struct bootmenu_entry *first; /* first menu entry */ +}; + +enum bootmenu_key { + KEY_NONE = 0, + KEY_UP, + KEY_DOWN, + KEY_SELECT, + KEY_QUIT, +}; + +void bootmenu_autoboot_loop(struct bootmenu_data *menu, + enum bootmenu_key *key, int *esc); +void bootmenu_loop(struct bootmenu_data *menu, + enum bootmenu_key *key, int *esc); + #endif /* __MENU_H__ */