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.4KB

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