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

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