Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

kb.c 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include <curses.h>
  2. #include <stddef.h>
  3. #include <timer.h>
  4. #include "core.h"
  5. /** @file
  6. *
  7. * MuCurses keyboard input handling functions
  8. */
  9. #define INPUT_DELAY 200 // half-blocking delay timer resolution (ms)
  10. #define INPUT_DELAY_TIMEOUT 1000 // half-blocking delay timeout
  11. int m_delay; /*
  12. < 0 : blocking read
  13. 0 : non-blocking read
  14. > 0 : timed blocking read
  15. */
  16. bool m_echo;
  17. bool m_cbreak;
  18. /**
  19. * Check KEY_ code supported status
  20. *
  21. * @v kc keycode value to check
  22. * @ret TRUE KEY_* supported
  23. * @ret FALSE KEY_* unsupported
  24. */
  25. int has_key ( int kc __unused ) {
  26. return TRUE;
  27. }
  28. int _wgetc ( WINDOW *win ) {
  29. int timer, c;
  30. if ( win == NULL )
  31. return ERR;
  32. timer = INPUT_DELAY_TIMEOUT;
  33. while ( ! win->scr->peek( win->scr ) ) {
  34. if ( m_delay == 0 ) // non-blocking read
  35. return ERR;
  36. if ( timer > 0 ) { // time-limited blocking read
  37. if ( m_delay > 0 )
  38. timer -= INPUT_DELAY;
  39. mdelay( INPUT_DELAY );
  40. } else { return ERR; } // non-blocking read
  41. }
  42. c = win->scr->getc( win->scr );
  43. if ( m_echo && ( c >= 32 && c <= 126 ) ) // printable ASCII characters
  44. _wputch( win, (chtype) ( c | win->attrs ), WRAP );
  45. return c;
  46. }
  47. /**
  48. * Pop a character from the FIFO into a window
  49. *
  50. * @v *win window in which to echo input
  51. * @ret c char from input stream
  52. */
  53. int wgetch ( WINDOW *win ) {
  54. int c;
  55. c = _wgetc( win );
  56. if ( m_echo ) {
  57. if ( c >= 0401 && c <= 0633 ) {
  58. switch(c) {
  59. case KEY_LEFT :
  60. case KEY_BACKSPACE :
  61. _wcursback( win );
  62. wdelch( win );
  63. break;
  64. default :
  65. beep();
  66. break;
  67. }
  68. } else {
  69. _wputch( win, (chtype)( c | win->attrs ), WRAP );
  70. }
  71. }
  72. return c;
  73. }
  74. /**
  75. * Read at most n characters from the FIFO into a window
  76. *
  77. * @v *win window in which to echo input
  78. * @v *str pointer to string in which to store result
  79. * @v n maximum number of characters to read into string (inc. NUL)
  80. * @ret rc return status code
  81. */
  82. int wgetnstr ( WINDOW *win, char *str, int n ) {
  83. char *_str;
  84. int c;
  85. if ( n == 0 ) {
  86. str = '\0';
  87. return OK;
  88. }
  89. _str = str;
  90. while ( ( c = _wgetc( win ) ) != ERR ) {
  91. /* termination enforcement - don't let us go past the
  92. end of the allocated buffer... */
  93. if ( n == 0 && ( c >= 32 && c <= 126 ) ) {
  94. _wcursback( win );
  95. wdelch( win );
  96. } else {
  97. if ( c >= 0401 && c <= 0633 ) {
  98. switch(c) {
  99. case KEY_LEFT :
  100. case KEY_BACKSPACE :
  101. _wcursback( win );
  102. wdelch( win );
  103. break;
  104. case KEY_ENTER :
  105. *_str = '\0';
  106. return OK;
  107. default :
  108. beep();
  109. break;
  110. }
  111. }
  112. if ( c >= 32 && c <= 126 ) {
  113. *(_str++) = c; n--;
  114. }
  115. }
  116. }
  117. return ERR;
  118. }
  119. /**
  120. *
  121. */
  122. int echo ( void ) {
  123. m_echo = TRUE;
  124. return OK;
  125. }
  126. /**
  127. *
  128. */
  129. int noecho ( void ) {
  130. m_echo = FALSE;
  131. return OK;
  132. }