Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

console.c 3.4KB

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