Blame SOURCES/0129-Make-CTRL-and-ALT-keys-work-as-expected-on-EFI-syste.patch

0dc71c
From fe42ce5fc9eec355027a533861dfe7f2eeefc63c Mon Sep 17 00:00:00 2001
0dc71c
From: Peter Jones <pjones@redhat.com>
0dc71c
Date: Mon, 3 Feb 2014 15:21:46 -0500
0dc71c
Subject: [PATCH 129/143] Make CTRL and ALT keys work as expected on EFI
0dc71c
 systems (version 5).
0dc71c
0dc71c
This is version 4.
0dc71c
0dc71c
Changes from version 1:
0dc71c
- handles SHIFT as a modifier
0dc71c
- handles F11 and F12 keys
0dc71c
- uses the handle provided by the system table to find our _EX protocol.
0dc71c
0dc71c
Changes from version 2:
0dc71c
- eliminate duplicate keycode translation.
0dc71c
0dc71c
Changes from version 3:
0dc71c
- Do not add the shift modifier for any ascii character between space
0dc71c
  (0x20) and DEL (0x7f); the combination of the modifier and many of the
0dc71c
  keys causes it not to be recognized at all.  Specifically, if we
0dc71c
  include the modifier on any querty punctuation character, i.e.
0dc71c
  anything the string "~!@#$%^&*()_+{}|:\"<>?" represents in C, it stops
0dc71c
  being recognized whatsoever.
0dc71c
0dc71c
Changes from version 4:
0dc71c
- Always initialize term->data from locate protocol (i.e. make it
0dc71c
  unconditional.)
0dc71c
0dc71c
Signed-off-by: Peter Jones <pjones@redhat.com>
0dc71c
---
0dc71c
 grub-core/term/efi/console.c | 118 +++++++++++++++++++++++++++++++++++--------
0dc71c
 include/grub/efi/api.h       |  65 +++++++++++++++++++++++-
0dc71c
 2 files changed, 161 insertions(+), 22 deletions(-)
0dc71c
0dc71c
diff --git a/grub-core/term/efi/console.c b/grub-core/term/efi/console.c
0dc71c
index a37eb84..677eab5 100644
0dc71c
--- a/grub-core/term/efi/console.c
0dc71c
+++ b/grub-core/term/efi/console.c
0dc71c
@@ -104,26 +104,12 @@ const unsigned efi_codes[] =
0dc71c
     GRUB_TERM_KEY_DC, GRUB_TERM_KEY_PPAGE, GRUB_TERM_KEY_NPAGE, GRUB_TERM_KEY_F1,
0dc71c
     GRUB_TERM_KEY_F2, GRUB_TERM_KEY_F3, GRUB_TERM_KEY_F4, GRUB_TERM_KEY_F5,
0dc71c
     GRUB_TERM_KEY_F6, GRUB_TERM_KEY_F7, GRUB_TERM_KEY_F8, GRUB_TERM_KEY_F9,
0dc71c
-    GRUB_TERM_KEY_F10, 0, 0, '\e'
0dc71c
+    GRUB_TERM_KEY_F10, GRUB_TERM_KEY_F10, GRUB_TERM_KEY_F11, '\e'
0dc71c
   };
0dc71c
 
0dc71c
-
0dc71c
 static int
0dc71c
-grub_console_getkey (struct grub_term_input *term __attribute__ ((unused)))
0dc71c
+grub_efi_translate_key (grub_efi_input_key_t key)
0dc71c
 {
0dc71c
-  grub_efi_simple_input_interface_t *i;
0dc71c
-  grub_efi_input_key_t key;
0dc71c
-  grub_efi_status_t status;
0dc71c
-
0dc71c
-  if (grub_efi_is_finished)
0dc71c
-    return 0;
0dc71c
-
0dc71c
-  i = grub_efi_system_table->con_in;
0dc71c
-  status = efi_call_2 (i->read_key_stroke, i, &key);
0dc71c
-
0dc71c
-  if (status != GRUB_EFI_SUCCESS)
0dc71c
-    return GRUB_TERM_NO_KEY;
0dc71c
-
0dc71c
   if (key.scan_code == 0)
0dc71c
     {
0dc71c
       /* Some firmware implementations use VT100-style codes against the spec.
0dc71c
@@ -139,9 +125,98 @@ grub_console_getkey (struct grub_term_input *term __attribute__ ((unused)))
0dc71c
   else if (key.scan_code < ARRAY_SIZE (efi_codes))
0dc71c
     return efi_codes[key.scan_code];
0dc71c
 
0dc71c
+  if (key.unicode_char >= 0x20 && key.unicode_char <= 0x7f)
0dc71c
+    return key.unicode_char;
0dc71c
+
0dc71c
   return GRUB_TERM_NO_KEY;
0dc71c
 }
0dc71c
 
0dc71c
+static int
0dc71c
+grub_console_getkey_con (struct grub_term_input *term __attribute__ ((unused)))
0dc71c
+{
0dc71c
+  grub_efi_simple_input_interface_t *i;
0dc71c
+  grub_efi_input_key_t key;
0dc71c
+  grub_efi_status_t status;
0dc71c
+
0dc71c
+  i = grub_efi_system_table->con_in;
0dc71c
+  status = efi_call_2 (i->read_key_stroke, i, &key);
0dc71c
+
0dc71c
+  if (status != GRUB_EFI_SUCCESS)
0dc71c
+    return GRUB_TERM_NO_KEY;
0dc71c
+
0dc71c
+  return grub_efi_translate_key(key);
0dc71c
+}
0dc71c
+
0dc71c
+static int
0dc71c
+grub_console_getkey_ex(struct grub_term_input *term)
0dc71c
+{
0dc71c
+  grub_efi_key_data_t key_data;
0dc71c
+  grub_efi_status_t status;
0dc71c
+  grub_efi_uint32_t kss;
0dc71c
+  int key = -1;
0dc71c
+
0dc71c
+  grub_efi_simple_text_input_ex_interface_t *text_input = term->data;
0dc71c
+
0dc71c
+  status = efi_call_2 (text_input->read_key_stroke, text_input, &key_data);
0dc71c
+
0dc71c
+  if (status != GRUB_EFI_SUCCESS)
0dc71c
+    return GRUB_TERM_NO_KEY;
0dc71c
+
0dc71c
+  kss = key_data.key_state.key_shift_state;
0dc71c
+  key = grub_efi_translate_key(key_data.key);
0dc71c
+
0dc71c
+  if (key == GRUB_TERM_NO_KEY)
0dc71c
+    return GRUB_TERM_NO_KEY;
0dc71c
+
0dc71c
+  if (kss & GRUB_EFI_SHIFT_STATE_VALID)
0dc71c
+    {
0dc71c
+      if ((kss & GRUB_EFI_LEFT_SHIFT_PRESSED
0dc71c
+	   || kss & GRUB_EFI_RIGHT_SHIFT_PRESSED)
0dc71c
+	  && !(key >= 0x20 && key <= 0x7f))
0dc71c
+	key |= GRUB_TERM_SHIFT;
0dc71c
+      if (kss & GRUB_EFI_LEFT_ALT_PRESSED || kss & GRUB_EFI_RIGHT_ALT_PRESSED)
0dc71c
+	key |= GRUB_TERM_ALT;
0dc71c
+      if (kss & GRUB_EFI_LEFT_CONTROL_PRESSED
0dc71c
+	  || kss & GRUB_EFI_RIGHT_CONTROL_PRESSED)
0dc71c
+	key |= GRUB_TERM_CTRL;
0dc71c
+    }
0dc71c
+
0dc71c
+  return key;
0dc71c
+}
0dc71c
+
0dc71c
+static grub_err_t
0dc71c
+grub_efi_console_input_init (struct grub_term_input *term)
0dc71c
+{
0dc71c
+  grub_efi_guid_t text_input_ex_guid =
0dc71c
+    GRUB_EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID;
0dc71c
+
0dc71c
+  if (grub_efi_is_finished)
0dc71c
+    return 0;
0dc71c
+
0dc71c
+  grub_efi_simple_text_input_ex_interface_t *text_input = term->data;
0dc71c
+  if (text_input)
0dc71c
+    return 0;
0dc71c
+
0dc71c
+  text_input = grub_efi_open_protocol(grub_efi_system_table->console_in_handler,
0dc71c
+				      &text_input_ex_guid,
0dc71c
+				      GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
0dc71c
+  term->data = (void *)text_input;
0dc71c
+
0dc71c
+  return 0;
0dc71c
+}
0dc71c
+
0dc71c
+static int
0dc71c
+grub_console_getkey (struct grub_term_input *term)
0dc71c
+{
0dc71c
+  if (grub_efi_is_finished)
0dc71c
+    return 0;
0dc71c
+
0dc71c
+  if (term->data)
0dc71c
+    return grub_console_getkey_ex(term);
0dc71c
+  else
0dc71c
+    return grub_console_getkey_con(term);
0dc71c
+}
0dc71c
+
0dc71c
 static struct grub_term_coordinate
0dc71c
 grub_console_getwh (struct grub_term_output *term __attribute__ ((unused)))
0dc71c
 {
0dc71c
@@ -243,7 +318,7 @@ grub_console_setcursor (struct grub_term_output *term __attribute__ ((unused)),
0dc71c
 }
0dc71c
 
0dc71c
 static grub_err_t
0dc71c
-grub_efi_console_init (struct grub_term_output *term)
0dc71c
+grub_efi_console_output_init (struct grub_term_output *term)
0dc71c
 {
0dc71c
   grub_efi_set_text_mode (1);
0dc71c
   grub_console_setcursor (term, 1);
0dc71c
@@ -251,7 +326,7 @@ grub_efi_console_init (struct grub_term_output *term)
0dc71c
 }
0dc71c
 
0dc71c
 static grub_err_t
0dc71c
-grub_efi_console_fini (struct grub_term_output *term)
0dc71c
+grub_efi_console_output_fini (struct grub_term_output *term)
0dc71c
 {
0dc71c
   grub_console_setcursor (term, 0);
0dc71c
   grub_efi_set_text_mode (0);
0dc71c
@@ -262,13 +337,14 @@ static struct grub_term_input grub_console_term_input =
0dc71c
   {
0dc71c
     .name = "console",
0dc71c
     .getkey = grub_console_getkey,
0dc71c
+    .init = grub_efi_console_input_init,
0dc71c
   };
0dc71c
 
0dc71c
 static struct grub_term_output grub_console_term_output =
0dc71c
   {
0dc71c
     .name = "console",
0dc71c
-    .init = grub_efi_console_init,
0dc71c
-    .fini = grub_efi_console_fini,
0dc71c
+    .init = grub_efi_console_output_init,
0dc71c
+    .fini = grub_efi_console_output_fini,
0dc71c
     .putchar = grub_console_putchar,
0dc71c
     .getwh = grub_console_getwh,
0dc71c
     .getxy = grub_console_getxy,
0dc71c
@@ -291,8 +367,8 @@ grub_console_init (void)
0dc71c
       return;
0dc71c
     }
0dc71c
 
0dc71c
-  grub_term_register_input ("console", &grub_console_term_input);
0dc71c
   grub_term_register_output ("console", &grub_console_term_output);
0dc71c
+  grub_term_register_input ("console", &grub_console_term_input);
0dc71c
 }
0dc71c
 
0dc71c
 void
0dc71c
diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h
0dc71c
index e5dd543..1423403 100644
0dc71c
--- a/include/grub/efi/api.h
0dc71c
+++ b/include/grub/efi/api.h
0dc71c
@@ -111,7 +111,7 @@
0dc71c
     { 0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b } \
0dc71c
   }
0dc71c
 
0dc71c
-#define EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID \
0dc71c
+#define GRUB_EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID \
0dc71c
   { 0xdd9e7534, 0x7762, 0x4698, \
0dc71c
     { 0x8c, 0x14, 0xf5, 0x85, 0x17, 0xa6, 0x25, 0xaa } \
0dc71c
   }
0dc71c
@@ -952,6 +952,32 @@ struct grub_efi_input_key
0dc71c
 };
0dc71c
 typedef struct grub_efi_input_key grub_efi_input_key_t;
0dc71c
 
0dc71c
+typedef grub_efi_uint8_t grub_efi_key_toggle_state_t;
0dc71c
+struct grub_efi_key_state
0dc71c
+{
0dc71c
+	grub_efi_uint32_t key_shift_state;
0dc71c
+	grub_efi_key_toggle_state_t key_toggle_state;
0dc71c
+};
0dc71c
+typedef struct grub_efi_key_state grub_efi_key_state_t;
0dc71c
+
0dc71c
+#define GRUB_EFI_SHIFT_STATE_VALID     0x80000000
0dc71c
+#define GRUB_EFI_RIGHT_SHIFT_PRESSED   0x00000001
0dc71c
+#define GRUB_EFI_LEFT_SHIFT_PRESSED    0x00000002
0dc71c
+#define GRUB_EFI_RIGHT_CONTROL_PRESSED 0x00000004
0dc71c
+#define GRUB_EFI_LEFT_CONTROL_PRESSED  0x00000008
0dc71c
+#define GRUB_EFI_RIGHT_ALT_PRESSED     0x00000010
0dc71c
+#define GRUB_EFI_LEFT_ALT_PRESSED      0x00000020
0dc71c
+#define GRUB_EFI_RIGHT_LOGO_PRESSED    0x00000040
0dc71c
+#define GRUB_EFI_LEFT_LOGO_PRESSED     0x00000080
0dc71c
+#define GRUB_EFI_MENU_KEY_PRESSED      0x00000100
0dc71c
+#define GRUB_EFI_SYS_REQ_PRESSED       0x00000200
0dc71c
+
0dc71c
+#define GRUB_EFI_TOGGLE_STATE_VALID 0x80
0dc71c
+#define GRUB_EFI_KEY_STATE_EXPOSED  0x40
0dc71c
+#define GRUB_EFI_SCROLL_LOCK_ACTIVE 0x01
0dc71c
+#define GRUB_EFI_NUM_LOCK_ACTIVE    0x02
0dc71c
+#define GRUB_EFI_CAPS_LOCK_ACTIVE   0x04
0dc71c
+
0dc71c
 struct grub_efi_simple_text_output_mode
0dc71c
 {
0dc71c
   grub_efi_int32_t max_mode;
0dc71c
@@ -1294,6 +1320,43 @@ struct grub_efi_simple_input_interface
0dc71c
 };
0dc71c
 typedef struct grub_efi_simple_input_interface grub_efi_simple_input_interface_t;
0dc71c
 
0dc71c
+struct grub_efi_key_data {
0dc71c
+	grub_efi_input_key_t key;
0dc71c
+	grub_efi_key_state_t key_state;
0dc71c
+};
0dc71c
+typedef struct grub_efi_key_data grub_efi_key_data_t;
0dc71c
+
0dc71c
+typedef grub_efi_status_t (*grub_efi_key_notify_function_t) (
0dc71c
+	grub_efi_key_data_t *key_data
0dc71c
+	);
0dc71c
+
0dc71c
+struct grub_efi_simple_text_input_ex_interface
0dc71c
+{
0dc71c
+	grub_efi_status_t
0dc71c
+	(*reset) (struct grub_efi_simple_text_input_ex_interface *this,
0dc71c
+		  grub_efi_boolean_t extended_verification);
0dc71c
+
0dc71c
+	grub_efi_status_t
0dc71c
+	(*read_key_stroke) (struct grub_efi_simple_text_input_ex_interface *this,
0dc71c
+			    grub_efi_key_data_t *key_data);
0dc71c
+
0dc71c
+	grub_efi_event_t wait_for_key;
0dc71c
+
0dc71c
+	grub_efi_status_t
0dc71c
+	(*set_state) (struct grub_efi_simple_text_input_ex_interface *this,
0dc71c
+		      grub_efi_key_toggle_state_t *key_toggle_state);
0dc71c
+
0dc71c
+	grub_efi_status_t
0dc71c
+	(*register_key_notify) (struct grub_efi_simple_text_input_ex_interface *this,
0dc71c
+				grub_efi_key_data_t *key_data,
0dc71c
+				grub_efi_key_notify_function_t key_notification_function);
0dc71c
+
0dc71c
+	grub_efi_status_t
0dc71c
+	(*unregister_key_notify) (struct grub_efi_simple_text_input_ex_interface *this,
0dc71c
+				  void *notification_handle);
0dc71c
+};
0dc71c
+typedef struct grub_efi_simple_text_input_ex_interface grub_efi_simple_text_input_ex_interface_t;
0dc71c
+
0dc71c
 struct grub_efi_simple_text_output_interface
0dc71c
 {
0dc71c
   grub_efi_status_t
0dc71c
-- 
0dc71c
1.9.3
0dc71c