您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

console.c 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. /** Current console usage */
  8. int console_usage = CONSOLE_USAGE_STDOUT;
  9. /**
  10. * Write a single character to each console device.
  11. *
  12. * @v character Character to be written
  13. * @ret None -
  14. * @err None -
  15. *
  16. * The character is written out to all enabled console devices, using
  17. * each device's console_driver::putchar() method.
  18. *
  19. */
  20. void putchar ( int character ) {
  21. struct console_driver *console;
  22. /* Automatic LF -> CR,LF translation */
  23. if ( character == '\n' )
  24. putchar ( '\r' );
  25. for_each_table_entry ( console, CONSOLES ) {
  26. if ( ( ! console->disabled ) &&
  27. ( console_usage & console->usage ) &&
  28. 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_each_table_entry ( console, CONSOLES ) {
  48. if ( ( ! console->disabled ) && console->iskey ) {
  49. if ( console->iskey () )
  50. return console;
  51. }
  52. }
  53. return NULL;
  54. }
  55. /**
  56. * Read a single character from any console.
  57. *
  58. * @v None -
  59. * @ret character Character read from a console.
  60. * @err None -
  61. *
  62. * A character will be read from the first enabled console device that
  63. * has input available using that console's console_driver::getchar()
  64. * method. If no console has input available to be read, this method
  65. * will block. To perform a non-blocking read, use something like
  66. *
  67. * @code
  68. *
  69. * int key = iskey() ? getchar() : -1;
  70. *
  71. * @endcode
  72. *
  73. * The character read will not be echoed back to any console.
  74. *
  75. */
  76. int getchar ( void ) {
  77. struct console_driver *console;
  78. int character;
  79. while ( 1 ) {
  80. console = has_input();
  81. if ( console && console->getchar ) {
  82. character = console->getchar ();
  83. break;
  84. }
  85. /* Doze for a while (until the next interrupt). This works
  86. * fine, because the keyboard is interrupt-driven, and the
  87. * timer interrupt (approx. every 50msec) takes care of the
  88. * serial port, which is read by polling. This reduces the
  89. * power dissipation of a modern CPU considerably, and also
  90. * makes Etherboot waiting for user interaction waste a lot
  91. * less CPU time in a VMware session.
  92. */
  93. cpu_nap();
  94. /* Keep processing background tasks while we wait for
  95. * input.
  96. */
  97. step();
  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. }