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