1*bd88a8a6Stsutsui /* $NetBSD: awaitkey.c,v 1.1 2013/01/21 11:58:12 tsutsui Exp $ */
2*bd88a8a6Stsutsui
3*bd88a8a6Stsutsui /*-
4*bd88a8a6Stsutsui * Copyright (c) 2013 Izumi Tsutsui. All rights reserved.
5*bd88a8a6Stsutsui *
6*bd88a8a6Stsutsui * Redistribution and use in source and binary forms, with or without
7*bd88a8a6Stsutsui * modification, are permitted provided that the following conditions
8*bd88a8a6Stsutsui * are met:
9*bd88a8a6Stsutsui * 1. Redistributions of source code must retain the above copyright
10*bd88a8a6Stsutsui * notice, this list of conditions and the following disclaimer.
11*bd88a8a6Stsutsui * 2. Redistributions in binary form must reproduce the above copyright
12*bd88a8a6Stsutsui * notice, this list of conditions and the following disclaimer in the
13*bd88a8a6Stsutsui * documentation and/or other materials provided with the distribution.
14*bd88a8a6Stsutsui *
15*bd88a8a6Stsutsui * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16*bd88a8a6Stsutsui * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*bd88a8a6Stsutsui * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*bd88a8a6Stsutsui * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19*bd88a8a6Stsutsui * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20*bd88a8a6Stsutsui * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21*bd88a8a6Stsutsui * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22*bd88a8a6Stsutsui * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*bd88a8a6Stsutsui * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24*bd88a8a6Stsutsui * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*bd88a8a6Stsutsui */
26*bd88a8a6Stsutsui
27*bd88a8a6Stsutsui #include <lib/libkern/libkern.h>
28*bd88a8a6Stsutsui #include <luna68k/stand/boot/samachdep.h>
29*bd88a8a6Stsutsui
30*bd88a8a6Stsutsui static void print_countdown(const char *, int);
31*bd88a8a6Stsutsui
32*bd88a8a6Stsutsui #define FMTLEN 40
33*bd88a8a6Stsutsui
34*bd88a8a6Stsutsui static void
print_countdown(const char * pfmt,int n)35*bd88a8a6Stsutsui print_countdown(const char *pfmt, int n)
36*bd88a8a6Stsutsui {
37*bd88a8a6Stsutsui int len, i;
38*bd88a8a6Stsutsui char fmtbuf[FMTLEN];
39*bd88a8a6Stsutsui
40*bd88a8a6Stsutsui len = snprintf(fmtbuf, FMTLEN, pfmt, n);
41*bd88a8a6Stsutsui printf("%s", fmtbuf);
42*bd88a8a6Stsutsui for (i = 0; i < len; i++)
43*bd88a8a6Stsutsui putchar('\b');
44*bd88a8a6Stsutsui }
45*bd88a8a6Stsutsui
46*bd88a8a6Stsutsui /*
47*bd88a8a6Stsutsui * awaitkey(const char *pfmt, int timeout, bool tell)
48*bd88a8a6Stsutsui *
49*bd88a8a6Stsutsui * Wait timeout seconds until any input from stdin.
50*bd88a8a6Stsutsui * print countdown message using "pfmt" if tell is true.
51*bd88a8a6Stsutsui * Requires tgetchar(), which returns 0 if there is no input.
52*bd88a8a6Stsutsui */
53*bd88a8a6Stsutsui char
awaitkey(const char * pfmt,int timeout,bool tell)54*bd88a8a6Stsutsui awaitkey(const char *pfmt, int timeout, bool tell)
55*bd88a8a6Stsutsui {
56*bd88a8a6Stsutsui uint32_t otick;
57*bd88a8a6Stsutsui char c = 0;
58*bd88a8a6Stsutsui
59*bd88a8a6Stsutsui if (timeout <= 0)
60*bd88a8a6Stsutsui goto out;
61*bd88a8a6Stsutsui
62*bd88a8a6Stsutsui if (tell)
63*bd88a8a6Stsutsui print_countdown(pfmt, timeout);
64*bd88a8a6Stsutsui
65*bd88a8a6Stsutsui otick = tick;
66*bd88a8a6Stsutsui
67*bd88a8a6Stsutsui for (;;) {
68*bd88a8a6Stsutsui c = tgetchar();
69*bd88a8a6Stsutsui if (c != 0)
70*bd88a8a6Stsutsui break;
71*bd88a8a6Stsutsui if (tick - otick >= hz) {
72*bd88a8a6Stsutsui otick = tick;
73*bd88a8a6Stsutsui if (--timeout == 0)
74*bd88a8a6Stsutsui break;
75*bd88a8a6Stsutsui if (tell)
76*bd88a8a6Stsutsui print_countdown(pfmt, timeout);
77*bd88a8a6Stsutsui }
78*bd88a8a6Stsutsui }
79*bd88a8a6Stsutsui
80*bd88a8a6Stsutsui out:
81*bd88a8a6Stsutsui if (tell) {
82*bd88a8a6Stsutsui printf(pfmt, 0);
83*bd88a8a6Stsutsui printf("\n");
84*bd88a8a6Stsutsui }
85*bd88a8a6Stsutsui return c;
86*bd88a8a6Stsutsui }
87