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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include <curses.h>
  2. #include <stddef.h>
  3. #include <unistd.h>
  4. #include "mucurses.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. static int _wgetc ( WINDOW *win ) {
  19. int timer, c;
  20. if ( win == NULL )
  21. return ERR;
  22. timer = INPUT_DELAY_TIMEOUT;
  23. while ( ! win->scr->peek( win->scr ) ) {
  24. if ( m_delay == 0 ) // non-blocking read
  25. return ERR;
  26. if ( timer > 0 ) { // time-limited blocking read
  27. if ( m_delay > 0 )
  28. timer -= INPUT_DELAY;
  29. mdelay( INPUT_DELAY );
  30. } else { return ERR; } // non-blocking read
  31. }
  32. c = win->scr->getc( win->scr );
  33. if ( m_echo && ( c >= 32 && c <= 126 ) ) // printable ASCII characters
  34. _wputch( win, (chtype) ( c | win->attrs ), WRAP );
  35. return c;
  36. }
  37. /**
  38. * Pop a character from the FIFO into a window
  39. *
  40. * @v *win window in which to echo input
  41. * @ret c char from input stream
  42. */
  43. int wgetch ( WINDOW *win ) {
  44. int c;
  45. c = _wgetc( win );
  46. if ( m_echo ) {
  47. if ( c >= KEY_MIN ) {
  48. switch(c) {
  49. case KEY_LEFT :
  50. case KEY_BACKSPACE :
  51. _wcursback( win );
  52. wdelch( win );
  53. break;
  54. default :
  55. beep();
  56. break;
  57. }
  58. } else {
  59. _wputch( win, (chtype)( c | win->attrs ), WRAP );
  60. }
  61. }
  62. return c;
  63. }
  64. /**
  65. * Read at most n characters from the FIFO into a window
  66. *
  67. * @v *win window in which to echo input
  68. * @v *str pointer to string in which to store result
  69. * @v n maximum number of characters to read into string (inc. NUL)
  70. * @ret rc return status code
  71. */
  72. int wgetnstr ( WINDOW *win, char *str, int n ) {
  73. char *_str;
  74. int c;
  75. if ( n == 0 ) {
  76. *str = '\0';
  77. return OK;
  78. }
  79. _str = str;
  80. while ( ( c = _wgetc( win ) ) != ERR ) {
  81. /* termination enforcement - don't let us go past the
  82. end of the allocated buffer... */
  83. if ( n == 0 && ( c >= 32 && c <= 126 ) ) {
  84. _wcursback( win );
  85. wdelch( win );
  86. } else {
  87. if ( c >= KEY_MIN ) {
  88. switch(c) {
  89. case KEY_LEFT :
  90. case KEY_BACKSPACE :
  91. _wcursback( win );
  92. wdelch( win );
  93. break;
  94. case KEY_ENTER :
  95. *_str = '\0';
  96. return OK;
  97. default :
  98. beep();
  99. break;
  100. }
  101. }
  102. if ( c >= 32 && c <= 126 ) {
  103. *(_str++) = c; n--;
  104. }
  105. }
  106. }
  107. return ERR;
  108. }
  109. /**
  110. *
  111. */
  112. int echo ( void ) {
  113. m_echo = TRUE;
  114. return OK;
  115. }
  116. /**
  117. *
  118. */
  119. int noecho ( void ) {
  120. m_echo = FALSE;
  121. return OK;
  122. }