1*9765cbf3Sjmcneill /* $NetBSD: console.c,v 1.2 2018/09/15 16:44:15 jmcneill Exp $ */
236105be2Sjmcneill
336105be2Sjmcneill /*-
436105be2Sjmcneill * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca>
536105be2Sjmcneill * All rights reserved.
636105be2Sjmcneill *
736105be2Sjmcneill * Redistribution and use in source and binary forms, with or without
836105be2Sjmcneill * modification, are permitted provided that the following conditions
936105be2Sjmcneill * are met:
1036105be2Sjmcneill * 1. Redistributions of source code must retain the above copyright
1136105be2Sjmcneill * notice, this list of conditions and the following disclaimer.
1236105be2Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
1336105be2Sjmcneill * notice, this list of conditions and the following disclaimer in the
1436105be2Sjmcneill * documentation and/or other materials provided with the distribution.
1536105be2Sjmcneill *
1636105be2Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1736105be2Sjmcneill * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1836105be2Sjmcneill * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1936105be2Sjmcneill * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2036105be2Sjmcneill * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2136105be2Sjmcneill * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2236105be2Sjmcneill * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2336105be2Sjmcneill * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2436105be2Sjmcneill * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2536105be2Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2636105be2Sjmcneill * SUCH DAMAGE.
2736105be2Sjmcneill */
2836105be2Sjmcneill
2936105be2Sjmcneill #include "efiboot.h"
3036105be2Sjmcneill
31*9765cbf3Sjmcneill static EFI_INPUT_KEY key_cur;
32*9765cbf3Sjmcneill static int key_pending;
33*9765cbf3Sjmcneill
3436105be2Sjmcneill int
getchar(void)3536105be2Sjmcneill getchar(void)
3636105be2Sjmcneill {
3736105be2Sjmcneill EFI_STATUS status;
3836105be2Sjmcneill EFI_INPUT_KEY key;
3936105be2Sjmcneill
40*9765cbf3Sjmcneill if (key_pending) {
41*9765cbf3Sjmcneill key = key_cur;
42*9765cbf3Sjmcneill key_pending = 0;
43*9765cbf3Sjmcneill } else {
4436105be2Sjmcneill status = uefi_call_wrapper(ST->ConIn->ReadKeyStroke, 2, ST->ConIn, &key);
4536105be2Sjmcneill while (status == EFI_NOT_READY) {
46*9765cbf3Sjmcneill if (ST->ConIn->WaitForKey != NULL)
4736105be2Sjmcneill WaitForSingleEvent(ST->ConIn->WaitForKey, 0);
4836105be2Sjmcneill status = uefi_call_wrapper(ST->ConIn->ReadKeyStroke, 2, ST->ConIn, &key);
4936105be2Sjmcneill }
50*9765cbf3Sjmcneill }
5136105be2Sjmcneill
5236105be2Sjmcneill return key.UnicodeChar;
5336105be2Sjmcneill }
5436105be2Sjmcneill
5536105be2Sjmcneill void
putchar(int c)5636105be2Sjmcneill putchar(int c)
5736105be2Sjmcneill {
5836105be2Sjmcneill CHAR16 buf[2] = { c, '\0' };
5936105be2Sjmcneill if (c == '\n')
6036105be2Sjmcneill putchar('\r');
6136105be2Sjmcneill uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, buf);
6236105be2Sjmcneill }
6336105be2Sjmcneill
6436105be2Sjmcneill int
ischar(void)6536105be2Sjmcneill ischar(void)
6636105be2Sjmcneill {
67*9765cbf3Sjmcneill EFI_INPUT_KEY key;
6836105be2Sjmcneill EFI_STATUS status;
6936105be2Sjmcneill
70*9765cbf3Sjmcneill if (ST->ConIn->WaitForKey == NULL) {
71*9765cbf3Sjmcneill if (key_pending)
72*9765cbf3Sjmcneill return 1;
73*9765cbf3Sjmcneill status = uefi_call_wrapper(ST->ConIn->ReadKeyStroke, 2, ST->ConIn, &key);
74*9765cbf3Sjmcneill if (status == EFI_SUCCESS) {
75*9765cbf3Sjmcneill key_cur = key;
76*9765cbf3Sjmcneill key_pending = 1;
77*9765cbf3Sjmcneill }
78*9765cbf3Sjmcneill return key_pending;
79*9765cbf3Sjmcneill } else {
8036105be2Sjmcneill status = uefi_call_wrapper(BS->CheckEvent, 1, ST->ConIn->WaitForKey);
8136105be2Sjmcneill return status == EFI_SUCCESS;
8236105be2Sjmcneill }
83*9765cbf3Sjmcneill }
84