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

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