Blame SOURCES/0183-EFI-console-Add-grub_console_read_key_stroke-helper-.patch

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