morse.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2011,2012,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/misc.h>
  20. #include <grub/types.h>
  21. #include <grub/err.h>
  22. #include <grub/dl.h>
  23. #include <grub/time.h>
  24. #include <grub/speaker.h>
  25. GRUB_MOD_LICENSE ("GPLv3+");
  26. #define BASE_TIME 250
  27. #define DIH 1
  28. #define DAH 3
  29. #define END 0
  30. static const char codes[0x80][6] =
  31. {
  32. ['0'] = { DAH, DAH, DAH, DAH, DAH, END },
  33. ['1'] = { DIH, DAH, DAH, DAH, DAH, END },
  34. ['2'] = { DIH, DIH, DAH, DAH, DAH, END },
  35. ['3'] = { DIH, DIH, DIH, DAH, DAH, END },
  36. ['4'] = { DIH, DIH, DIH, DIH, DAH, END },
  37. ['5'] = { DIH, DIH, DIH, DIH, DIH, END },
  38. ['6'] = { DAH, DIH, DIH, DIH, DIH, END },
  39. ['7'] = { DAH, DAH, DIH, DIH, DIH, END },
  40. ['8'] = { DAH, DAH, DAH, DIH, DIH, END },
  41. ['9'] = { DAH, DAH, DAH, DAH, DIH, END },
  42. ['a'] = { DIH, DAH, END },
  43. ['b'] = { DAH, DIH, DIH, DIH, END },
  44. ['c'] = { DAH, DIH, DAH, DIH, END },
  45. ['d'] = { DAH, DIH, DIH, END },
  46. ['e'] = { DIH, END },
  47. ['f'] = { DIH, DIH, DAH, DIH, END },
  48. ['g'] = { DAH, DAH, DIH, END },
  49. ['h'] = { DIH, DIH, DIH, DIH, END },
  50. ['i'] = { DIH, DIH, END },
  51. ['j'] = { DIH, DAH, DAH, DAH, END },
  52. ['k'] = { DAH, DIH, DAH, END },
  53. ['l'] = { DIH, DAH, DIH, DIH, END },
  54. ['m'] = { DAH, DAH, END },
  55. ['n'] = { DAH, DIH, END },
  56. ['o'] = { DAH, DAH, DAH, END },
  57. ['p'] = { DIH, DAH, DAH, DIH, END },
  58. ['q'] = { DAH, DAH, DIH, DAH, END },
  59. ['r'] = { DIH, DAH, DIH, END },
  60. ['s'] = { DIH, DIH, DIH, END },
  61. ['t'] = { DAH, END },
  62. ['u'] = { DIH, DIH, DAH, END },
  63. ['v'] = { DIH, DIH, DIH, DAH, END },
  64. ['w'] = { DIH, DAH, DAH, END },
  65. ['x'] = { DAH, DIH, DIH, DAH, END },
  66. ['y'] = { DAH, DIH, DAH, DAH, END },
  67. ['z'] = { DAH, DAH, DIH, DIH, END }
  68. };
  69. static void
  70. grub_audio_tone (int length)
  71. {
  72. grub_speaker_beep_on (1000);
  73. grub_millisleep (length);
  74. grub_speaker_beep_off ();
  75. }
  76. static void
  77. grub_audio_putchar (struct grub_term_output *term __attribute__ ((unused)),
  78. const struct grub_unicode_glyph *c_in)
  79. {
  80. grub_uint8_t c;
  81. int i;
  82. /* For now, do not try to use a surrogate pair. */
  83. if (c_in->base > 0x7f)
  84. c = '?';
  85. else
  86. c = grub_tolower (c_in->base);
  87. for (i = 0; codes[c][i]; i++)
  88. {
  89. grub_audio_tone (codes[c][i] * BASE_TIME);
  90. grub_millisleep (BASE_TIME);
  91. }
  92. grub_millisleep (2 * BASE_TIME);
  93. }
  94. static int
  95. dummy (void)
  96. {
  97. return 0;
  98. }
  99. static struct grub_term_output grub_audio_term_output =
  100. {
  101. .name = "morse",
  102. .init = (void *) dummy,
  103. .fini = (void *) dummy,
  104. .putchar = grub_audio_putchar,
  105. .getwh = (void *) dummy,
  106. .getxy = (void *) dummy,
  107. .gotoxy = (void *) dummy,
  108. .cls = (void *) dummy,
  109. .setcolorstate = (void *) dummy,
  110. .setcursor = (void *) dummy,
  111. .flags = GRUB_TERM_CODE_TYPE_ASCII | GRUB_TERM_DUMB,
  112. .progress_update_divisor = GRUB_PROGRESS_NO_UPDATE
  113. };
  114. GRUB_MOD_INIT (morse)
  115. {
  116. grub_term_register_output ("audio", &grub_audio_term_output);
  117. }
  118. GRUB_MOD_FINI (morse)
  119. {
  120. grub_term_unregister_output (&grub_audio_term_output);
  121. }