kb.c 2.7KB

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