fake_input.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2013 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/term.h>
  19. #include <grub/mm.h>
  20. #include <grub/dl.h>
  21. #include <grub/test.h>
  22. GRUB_MOD_LICENSE ("GPLv3+");
  23. static int *seq;
  24. static int seqptr, nseq;
  25. static struct grub_term_input *saved;
  26. static int fake_input;
  27. static int
  28. fake_getkey (struct grub_term_input *term __attribute__ ((unused)))
  29. {
  30. if (seq && seqptr < nseq)
  31. return seq[seqptr++];
  32. return -1;
  33. }
  34. static struct grub_term_input fake_input_term =
  35. {
  36. .name = "fake",
  37. .getkey = fake_getkey
  38. };
  39. void
  40. grub_terminal_input_fake_sequence (int *seq_in, int nseq_in)
  41. {
  42. if (!fake_input)
  43. saved = grub_term_inputs;
  44. if (seq)
  45. grub_free (seq);
  46. seq = grub_calloc (nseq_in, sizeof (seq[0]));
  47. if (!seq)
  48. return;
  49. grub_term_inputs = &fake_input_term;
  50. grub_memcpy (seq, seq_in, nseq_in * sizeof (seq[0]));
  51. nseq = nseq_in;
  52. seqptr = 0;
  53. fake_input = 1;
  54. }
  55. void
  56. grub_terminal_input_fake_sequence_end (void)
  57. {
  58. if (!fake_input)
  59. return;
  60. fake_input = 0;
  61. grub_term_inputs = saved;
  62. grub_free (seq);
  63. seq = 0;
  64. nseq = 0;
  65. seqptr = 0;
  66. }