grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[RFC] [PATCH] Allow user defined key to interupt sleep command


From: Yang Bai
Subject: [RFC] [PATCH] Allow user defined key to interupt sleep command
Date: Mon, 16 Sep 2013 16:49:46 +0800

Hi all,
At now, sleep --interruptible 3 can only be interupted by ESC key.
With this patch, we can special a key such as sleep --interruptible
f10 3 and we can type F10 to interrupt the sleep. This can work as a
hotkey handler.

Modified some code borrowed from [PATCH] Allow hotkeys to interrupt hidden menu.

=== modified file 'grub-core/commands/sleep.c'
--- grub-core/commands/sleep.c 2013-07-11 14:02:22 +0000
+++ grub-core/commands/sleep.c 2013-09-16 08:29:33 +0000
@@ -30,7 +30,7 @@
 static const struct grub_arg_option options[] =
   {
     {"verbose", 'v', 0, N_("Verbose countdown."), 0, 0},
-    {"interruptible", 'i', 0, N_("Allow to interrupt with ESC."), 0, 0},
+    {"interruptible", 'i', 0, N_("Allow to interrupt with one key."),
0, ARG_TYPE_STRING},
     {0, 0, 0, 0, 0, 0}
   };

@@ -46,16 +46,61 @@
   grub_refresh ();
 }

+static struct
+{
+  char *name;
+  int key;
+} function_key_aliases[] =
+  {
+    {"f1", GRUB_TERM_KEY_F1},
+    {"f2", GRUB_TERM_KEY_F2},
+    {"f3", GRUB_TERM_KEY_F3},
+    {"f4", GRUB_TERM_KEY_F4},
+    {"f5", GRUB_TERM_KEY_F5},
+    {"f6", GRUB_TERM_KEY_F6},
+    {"f7", GRUB_TERM_KEY_F7},
+    {"f8", GRUB_TERM_KEY_F8},
+    {"f9", GRUB_TERM_KEY_F9},
+    {"f10", GRUB_TERM_KEY_F10},
+    {"f11", GRUB_TERM_KEY_F11},
+    {"f12", GRUB_TERM_KEY_F12},
+    {"tab", GRUB_TERM_TAB},
+    {"backspace", GRUB_TERM_BACKSPACE},
+    {"esc", GRUB_TERM_ESC},
+  };
+
+static int
+grub_get_keycode_from_str (const char *name)
+{
+  unsigned i;
+  if (!name)
+    return 0;
+
+  int n = grub_strlen (name);
+
+  /* User gives a char */
+  if (n == 1)
+    return name[0];
+
+  /* check for special keys. */
+  for (i = 0; i < ARRAY_SIZE (function_key_aliases); i++)
+    if (grub_strcmp (name, function_key_aliases[i].name) == 0)
+      return function_key_aliases[i].key;
+
+  /* fallback to ESC */
+  return GRUB_TERM_ESC;
+}
+
 /* Based on grub_millisleep() from kern/generic/millisleep.c.  */
 static int
-grub_interruptible_millisleep (grub_uint32_t ms)
+grub_interruptible_millisleep (grub_uint32_t ms, int key)
 {
   grub_uint64_t start;

   start = grub_get_time_ms ();

   while (grub_get_time_ms () - start < ms)
-    if (grub_getkey_noblock () == GRUB_TERM_ESC)
+    if (grub_getkey_noblock () == key)
       return 1;

   return 0;
@@ -65,7 +110,7 @@
 grub_cmd_sleep (grub_extcmd_context_t ctxt, int argc, char **args)
 {
   struct grub_arg_list *state = ctxt->state;
-  int n;
+  int n, key;

   if (argc != 1)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
@@ -82,6 +127,9 @@

   pos = grub_term_save_pos ();

+  if (state[1].set)
+    key = grub_get_keycode_from_str (state[1].arg);
+
   for (; n; n--)
     {
       if (state[0].set)
@@ -89,7 +137,7 @@

       if (state[1].set)
  {
-  if (grub_interruptible_millisleep (1000))
+  if (grub_interruptible_millisleep (1000, key))
     return 1;
  }
       else



reply via email to

[Prev in Thread] Current Thread [Next in Thread]