You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

console.c 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "stddef.h"
  2. #include <ipxe/console.h>
  3. #include <ipxe/process.h>
  4. #include <ipxe/nap.h>
  5. /** @file */
  6. FILE_LICENCE ( GPL2_OR_LATER );
  7. /** Current console usage */
  8. int console_usage = CONSOLE_USAGE_STDOUT;
  9. /**
  10. * Write a single character to each console device
  11. *
  12. * @v character Character to be written
  13. *
  14. * The character is written out to all enabled console devices, using
  15. * each device's console_driver::putchar() method.
  16. */
  17. void putchar ( int character ) {
  18. struct console_driver *console;
  19. /* Automatic LF -> CR,LF translation */
  20. if ( character == '\n' )
  21. putchar ( '\r' );
  22. for_each_table_entry ( console, CONSOLES ) {
  23. if ( ( ! ( console->disabled & CONSOLE_DISABLED_OUTPUT ) ) &&
  24. ( console_usage & console->usage ) &&
  25. console->putchar )
  26. console->putchar ( character );
  27. }
  28. }
  29. /**
  30. * Check to see if any input is available on any console
  31. *
  32. * @ret console Console device that has input available, or NULL
  33. *
  34. * All enabled console devices are checked once for available input
  35. * using each device's console_driver::iskey() method. The first
  36. * console device that has available input will be returned, if any.
  37. */
  38. static struct console_driver * has_input ( void ) {
  39. struct console_driver *console;
  40. for_each_table_entry ( console, CONSOLES ) {
  41. if ( ( ! ( console->disabled & CONSOLE_DISABLED_INPUT ) ) &&
  42. console->iskey ) {
  43. if ( console->iskey () )
  44. return console;
  45. }
  46. }
  47. return NULL;
  48. }
  49. /**
  50. * Read a single character from any console
  51. *
  52. * @ret character Character read from a console.
  53. *
  54. * A character will be read from the first enabled console device that
  55. * has input available using that console's console_driver::getchar()
  56. * method. If no console has input available to be read, this method
  57. * will block. To perform a non-blocking read, use something like
  58. *
  59. * @code
  60. *
  61. * int key = iskey() ? getchar() : -1;
  62. *
  63. * @endcode
  64. *
  65. * The character read will not be echoed back to any console.
  66. */
  67. int getchar ( void ) {
  68. struct console_driver *console;
  69. int character;
  70. while ( 1 ) {
  71. console = has_input();
  72. if ( console && console->getchar ) {
  73. character = console->getchar ();
  74. break;
  75. }
  76. /* Doze for a while (until the next interrupt). This works
  77. * fine, because the keyboard is interrupt-driven, and the
  78. * timer interrupt (approx. every 50msec) takes care of the
  79. * serial port, which is read by polling. This reduces the
  80. * power dissipation of a modern CPU considerably, and also
  81. * makes Etherboot waiting for user interaction waste a lot
  82. * less CPU time in a VMware session.
  83. */
  84. cpu_nap();
  85. /* Keep processing background tasks while we wait for
  86. * input.
  87. */
  88. step();
  89. }
  90. /* CR -> LF translation */
  91. if ( character == '\r' )
  92. character = '\n';
  93. return character;
  94. }
  95. /**
  96. * Check for available input on any console
  97. *
  98. * @ret is_available Input is available on a console
  99. *
  100. * All enabled console devices are checked once for available input
  101. * using each device's console_driver::iskey() method. If any console
  102. * device has input available, this call will return true. If this
  103. * call returns true, you can then safely call getchar() without
  104. * blocking.
  105. */
  106. int iskey ( void ) {
  107. return has_input() ? 1 : 0;
  108. }