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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "stddef.h"
  2. #include "console.h"
  3. #include <gpxe/process.h>
  4. /** @file */
  5. #include "bios.h"
  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. * @bug We need a cleaner way to pick up cpu_nap(). It makes a
  77. * real-mode call, and so we don't want to use it with LinuxBIOS.
  78. *
  79. */
  80. int getchar ( void ) {
  81. struct console_driver *console;
  82. int character;
  83. while ( 1 ) {
  84. console = has_input();
  85. if ( console && console->getchar ) {
  86. character = console->getchar ();
  87. break;
  88. }
  89. /* Doze for a while (until the next interrupt). This works
  90. * fine, because the keyboard is interrupt-driven, and the
  91. * timer interrupt (approx. every 50msec) takes care of the
  92. * serial port, which is read by polling. This reduces the
  93. * power dissipation of a modern CPU considerably, and also
  94. * makes Etherboot waiting for user interaction waste a lot
  95. * less CPU time in a VMware session.
  96. */
  97. cpu_nap();
  98. /* Keep processing background tasks while we wait for
  99. * input.
  100. */
  101. step();
  102. }
  103. /* CR -> LF translation */
  104. if ( character == '\r' )
  105. character = '\n';
  106. return character;
  107. }
  108. /** Check for available input on any console.
  109. *
  110. * @v None -
  111. * @ret True Input is available on a console
  112. * @ret False Input is not available on any console
  113. * @err None -
  114. *
  115. * All enabled console devices are checked once for available input
  116. * using each device's console_driver::iskey() method. If any console
  117. * device has input available, this call will return True. If this
  118. * call returns True, you can then safely call getchar() without
  119. * blocking.
  120. *
  121. */
  122. int iskey ( void ) {
  123. return has_input() ? 1 : 0;
  124. }