You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mucurses.c 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include <ipxe/console.h>
  2. #include <curses.h>
  3. #include "mucurses.h"
  4. /** @file
  5. *
  6. * MuCurses core functions
  7. *
  8. */
  9. FILE_LICENCE ( GPL2_OR_LATER );
  10. static void _wupdcurs ( WINDOW *win ) __nonnull;
  11. void _wputch ( WINDOW *win, chtype ch, int wrap ) __nonnull;
  12. void _wputc ( WINDOW *win, char c, int wrap ) __nonnull;
  13. void _wcursback ( WINDOW *win ) __nonnull;
  14. void _wputchstr ( WINDOW *win, const chtype *chstr, int wrap, int n ) __nonnull;
  15. void _wputstr ( WINDOW *win, const char *str, int wrap, int n ) __nonnull;
  16. int wmove ( WINDOW *win, int y, int x ) __nonnull;
  17. WINDOW _stdscr = {
  18. .attrs = A_DEFAULT,
  19. .ori_y = 0,
  20. .ori_x = 0,
  21. .curs_y = 0,
  22. .curs_x = 0,
  23. .scr = &_ansi_screen,
  24. };
  25. /*
  26. * Primitives
  27. */
  28. /**
  29. * Update cursor position
  30. *
  31. * @v *win window in which to update position
  32. */
  33. static void _wupdcurs ( WINDOW *win ) {
  34. win->scr->movetoyx ( win->scr, win->ori_y + win->curs_y,
  35. win->ori_x + win->curs_x );
  36. }
  37. /**
  38. * Write a single character rendition to a window
  39. *
  40. * @v *win window in which to write
  41. * @v ch character rendition to write
  42. * @v wrap wrap "switch"
  43. */
  44. void _wputch ( WINDOW *win, chtype ch, int wrap ) {
  45. /* make sure we set the screen cursor to the right position
  46. first! */
  47. _wupdcurs(win);
  48. win->scr->putc(win->scr, ch);
  49. if ( ++(win->curs_x) - win->width == 0 ) {
  50. if ( wrap == WRAP ) {
  51. win->curs_x = 0;
  52. /* specification says we should really scroll,
  53. but we have no buffer to scroll with, so we
  54. can only overwrite back at the beginning of
  55. the window */
  56. if ( ++(win->curs_y) - win->height == 0 )
  57. win->curs_y = 0;
  58. } else {
  59. (win->curs_x)--;
  60. }
  61. }
  62. }
  63. /**
  64. * Write a single character to a window
  65. *
  66. * @v *win window in which to write
  67. * @v c character rendition to write
  68. * @v wrap wrap "switch"
  69. */
  70. void _wputc ( WINDOW *win, char c, int wrap ) {
  71. _wputch ( win, ( c | win->attrs ), wrap );
  72. }
  73. /**
  74. * Retreat the cursor back one position (useful for a whole host of
  75. * ops)
  76. *
  77. * @v *win window in which to retreat
  78. */
  79. void _wcursback ( WINDOW *win ) {
  80. if ( win->curs_x == 0 ) {
  81. if ( win->curs_y == 0 )
  82. win->curs_y = win->height - 1;
  83. win->curs_x = win->width = 1;
  84. } else {
  85. win->curs_x--;
  86. }
  87. _wupdcurs(win);
  88. }
  89. /**
  90. * Write a chtype string to a window
  91. *
  92. * @v *win window in which to write
  93. * @v *chstr chtype string
  94. * @v wrap wrap "switch"
  95. * @v n write at most n chtypes
  96. */
  97. void _wputchstr ( WINDOW *win, const chtype *chstr, int wrap, int n ) {
  98. for ( ; *chstr && n-- ; chstr++ ) {
  99. _wputch(win,*chstr,wrap);
  100. }
  101. }
  102. /**
  103. * Write a standard c-style string to a window
  104. *
  105. * @v *win window in which to write
  106. * @v *str string
  107. * @v wrap wrap "switch"
  108. * @v n write at most n chars from *str
  109. */
  110. void _wputstr ( WINDOW *win, const char *str, int wrap, int n ) {
  111. for ( ; *str && n-- ; str++ ) {
  112. _wputc ( win, *str, wrap );
  113. }
  114. }
  115. /**
  116. * Move a window's cursor to the specified position
  117. *
  118. * @v *win window to be operated on
  119. * @v y Y position
  120. * @v x X position
  121. * @ret rc return status code
  122. */
  123. int wmove ( WINDOW *win, int y, int x ) {
  124. /* chech for out-of-bounds errors */
  125. if ( ( (unsigned)y >= win->height ) ||
  126. ( (unsigned)x >= win->width ) ) {
  127. return ERR;
  128. }
  129. win->curs_y = y;
  130. win->curs_x = x;
  131. _wupdcurs(win);
  132. return OK;
  133. }