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

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