15a2072
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
15a2072
From: Hans de Goede <hdegoede@redhat.com>
15a2072
Date: Wed, 6 Jun 2018 15:54:44 +0200
15a2072
Subject: [PATCH] EFI: console: Add grub_console_read_key_stroke() helper
15a2072
 function
15a2072
15a2072
This is a preparation patch for adding getkeystatus() support to the
15a2072
EFI console terminal input driver.
15a2072
15a2072
We can get modifier status through the simple_text_input read_key_stroke
15a2072
method, but if a non-modifier key is (also) pressed the read_key_stroke
15a2072
call will consume that key from the firmware's queue.
15a2072
15a2072
The new grub_console_read_key_stroke() helper buffers upto 1 key-stroke.
15a2072
If it has a non-modifier key buffered, it will return that one, if its
15a2072
buffer is empty, it will fills its buffer by getting a new key-stroke.
15a2072
15a2072
If called with consume=1 it will empty its buffer after copying the
15a2072
key-data to the callers buffer, this is how getkey() will use it.
15a2072
15a2072
If called with consume=0 it will keep the last key-stroke buffered, this
15a2072
is how getkeystatus() will call it. This means that if a non-modifier
15a2072
key gets pressed, repeated getkeystatus() calls will return the modifiers
15a2072
of that key-press until it is consumed by a getkey() call.
15a2072
15a2072
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
15a2072
---
15a2072
 grub-core/term/efi/console.c | 51 ++++++++++++++++++++++++++++++++++----------
15a2072
 1 file changed, 40 insertions(+), 11 deletions(-)
15a2072
15a2072
diff --git a/grub-core/term/efi/console.c b/grub-core/term/efi/console.c
15a2072
index 051633d71e9..3d36c5c701b 100644
15a2072
--- a/grub-core/term/efi/console.c
15a2072
+++ b/grub-core/term/efi/console.c
15a2072
@@ -157,27 +157,56 @@ grub_console_getkey_con (struct grub_term_input *term __attribute__ ((unused)))
15a2072
   return grub_efi_translate_key(key);
15a2072
 }
15a2072
 
15a2072
+/*
15a2072
+ * When more then just modifiers are pressed, our getkeystatus() consumes a
15a2072
+ * press from the queue, this function buffers the press for the regular
15a2072
+ * getkey() so that it does not get lost.
15a2072
+ */
15a2072
+static int
15a2072
+grub_console_read_key_stroke (
15a2072
+                   grub_efi_simple_text_input_ex_interface_t *text_input,
15a2072
+                   grub_efi_key_data_t *key_data_ret, int *key_ret,
15a2072
+                   int consume)
15a2072
+{
15a2072
+  static grub_efi_key_data_t key_data;
15a2072
+  grub_efi_status_t status;
15a2072
+  int key;
15a2072
+
15a2072
+  if (!text_input)
15a2072
+    return GRUB_ERR_EOF;
15a2072
+
15a2072
+  key = grub_efi_translate_key (key_data.key);
15a2072
+  if (key == GRUB_TERM_NO_KEY) {
15a2072
+    status = efi_call_2 (text_input->read_key_stroke, text_input, &key_data);
15a2072
+    if (status != GRUB_EFI_SUCCESS)
15a2072
+      return GRUB_ERR_EOF;
15a2072
+
15a2072
+    key = grub_efi_translate_key (key_data.key);
15a2072
+  }
15a2072
+
15a2072
+  *key_data_ret = key_data;
15a2072
+  *key_ret = key;
15a2072
+
15a2072
+  if (consume) {
15a2072
+    key_data.key.scan_code = 0;
15a2072
+    key_data.key.unicode_char = 0;
15a2072
+  }
15a2072
+
15a2072
+  return 0;
15a2072
+}
15a2072
+
15a2072
 static int
15a2072
 grub_console_getkey_ex(struct grub_term_input *term)
15a2072
 {
15a2072
   grub_efi_key_data_t key_data;
15a2072
-  grub_efi_status_t status;
15a2072
   grub_efi_uint32_t kss;
15a2072
   int key = -1;
15a2072
 
15a2072
-  grub_efi_simple_text_input_ex_interface_t *text_input = term->data;
15a2072
-
15a2072
-  status = efi_call_2 (text_input->read_key_stroke, text_input, &key_data);
15a2072
-
15a2072
-  if (status != GRUB_EFI_SUCCESS)
15a2072
+  if (grub_console_read_key_stroke (term->data, &key_data, &key, 1) ||
15a2072
+      key == GRUB_TERM_NO_KEY)
15a2072
     return GRUB_TERM_NO_KEY;
15a2072
 
15a2072
   kss = key_data.key_state.key_shift_state;
15a2072
-  key = grub_efi_translate_key(key_data.key);
15a2072
-
15a2072
-  if (key == GRUB_TERM_NO_KEY)
15a2072
-    return GRUB_TERM_NO_KEY;
15a2072
-
15a2072
   if (kss & GRUB_EFI_SHIFT_STATE_VALID)
15a2072
     {
15a2072
       if ((kss & GRUB_EFI_LEFT_SHIFT_PRESSED