curses.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include <curses.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <grass/gis.h>
  5. #include <grass/glocale.h>
  6. #include "globals.h"
  7. #include "local_proto.h"
  8. static int inited = 0;
  9. static WINDOW *save;
  10. WINDOW *newwin();
  11. static Window *make_window(int top, int bottom, int left, int right)
  12. {
  13. Window *window;
  14. if (top < 0 || bottom >= LINES || left < 0 || right >= COLS
  15. || bottom - top <= 1 || right - left <= 1) {
  16. End_curses();
  17. G_warning(_("make_window(%d,%d,%d,%d): illegal screen values."),
  18. top, bottom, left, right);
  19. G_sleep(3);
  20. exit(EXIT_SUCCESS);
  21. }
  22. window = (Window *) G_malloc(sizeof(Window));
  23. window->top = top;
  24. window->bottom = bottom;
  25. window->left = left;
  26. window->right = right;
  27. Curses_clear_window(window);
  28. return window;
  29. }
  30. /* should only be called once at program outset */
  31. int Begin_curses(void)
  32. {
  33. Window *INFO_WINDOW;
  34. Window *MENU_WINDOW;
  35. initscr(); /* initialize curses standard screens */
  36. raw(); /* set tty modes via curses calls */
  37. noecho();
  38. nonl();
  39. inited = 1;
  40. /* make a window to save stdscr */
  41. save = newwin(LINES, COLS, 0, 0);
  42. /* make_window (nrows, ncols, start_row, start_col) */
  43. INFO_WINDOW = make_window(0, LINES - 4, COLS / 2, COLS - 1);
  44. MENU_WINDOW = make_window(0, LINES - 4, 0, COLS / 2);
  45. PROMPT_WINDOW = make_window(LINES - 4, LINES - 1, 0, COLS - 1);
  46. refresh();
  47. return 0;
  48. }
  49. int End_curses(void)
  50. {
  51. /* should only be called upon program exit */
  52. clear(); /* clear the screen */
  53. refresh();
  54. endwin(); /* let curses reset the tty now */
  55. return 0;
  56. }
  57. int Suspend_curses(void)
  58. {
  59. overwrite(stdscr, save);
  60. clear();
  61. refresh();
  62. endwin();
  63. return 0;
  64. }
  65. int Resume_curses(void)
  66. {
  67. clear();
  68. refresh();
  69. overwrite(save, stdscr);
  70. refresh();
  71. return 0;
  72. }
  73. int Curses_allow_interrupts(int ok)
  74. {
  75. refresh();
  76. if (ok)
  77. noraw();
  78. else
  79. raw();
  80. return 0;
  81. }
  82. int Curses_clear_window(Window * window)
  83. {
  84. int y, x;
  85. if (!inited)
  86. return 1;
  87. for (y = window->top + 1; y < window->bottom; y++) {
  88. move(y, x = window->left + 1);
  89. while (x++ < window->right)
  90. addch(' ');
  91. }
  92. Curses_outline_window(window);
  93. refresh();
  94. return 0;
  95. }
  96. int Curses_outline_window(Window * window)
  97. {
  98. int x, y;
  99. move(window->top, x = window->left + 1);
  100. while (x++ < window->right)
  101. addch('-');
  102. move(window->bottom, x = window->left + 1);
  103. while (x++ < window->right)
  104. addch('-');
  105. for (y = window->top + 1; y < window->bottom; y++) {
  106. move(y, window->left);
  107. addch('|');
  108. move(y, window->right);
  109. addch('|');
  110. }
  111. move(window->top, window->left);
  112. addch('+');
  113. move(window->top, window->right);
  114. addch('+');
  115. move(window->bottom, window->left);
  116. addch('+');
  117. if (window->bottom < LINES - 1 || window->right < COLS - 1) {
  118. move(window->bottom, window->right);
  119. addch('+');
  120. }
  121. return 0;
  122. }
  123. int Curses_write_window(Window * window, int line, int col, char *message)
  124. {
  125. int y, x, i;
  126. if (!inited) {
  127. G_message(_("%s"), message);
  128. return 1;
  129. }
  130. if (line <= 0 || line >= window->bottom - window->top)
  131. return 1;
  132. if (col <= 0 || col >= window->right - window->left)
  133. return 1;
  134. move(y = window->top + line, x = window->left + col);
  135. while (*message != 0 && *message != '\n' && x < window->right) {
  136. addch(*message);
  137. message++;
  138. x++;
  139. }
  140. if (*message == '\n')
  141. for (i = x; i < window->right; i++)
  142. addch(' ');
  143. move(y, x);
  144. refresh();
  145. return 0;
  146. }
  147. int Curses_replot_screen(void)
  148. {
  149. int x, y;
  150. getyx(stdscr, y, x);
  151. wrefresh(curscr);
  152. move(y, x);
  153. refresh();
  154. return 0;
  155. }
  156. int Curses_prompt_gets(char *prompt, char *answer)
  157. {
  158. char c;
  159. int n;
  160. int y, x;
  161. *answer = 0;
  162. n = 0;
  163. Curses_write_window(PROMPT_WINDOW, 1, 1, "\n");
  164. Curses_write_window(PROMPT_WINDOW, 1, 1, prompt);
  165. for (;;) {
  166. refresh();
  167. c = Curses_getch(0);
  168. if (c == '\n' || c == '\r')
  169. break;
  170. getyx(stdscr, y, x);
  171. if (c > '\037' && c < '\177') { /* octal codes: accept space to '~' */
  172. if (x < PROMPT_WINDOW->right) {
  173. *answer++ = c;
  174. *answer = 0;
  175. addch(c);
  176. n++;
  177. }
  178. continue;
  179. }
  180. if (c == '\b' || c == '\177') { /* backspace or DEL (decimal 8,127) */
  181. if (n > 0) {
  182. answer--;
  183. *answer = 0;
  184. move(y, x - 1);
  185. addch(' ');
  186. move(y, x - 1);
  187. n--;
  188. }
  189. continue;
  190. }
  191. }
  192. return 0;
  193. }
  194. int Curses_getch(int with_echo)
  195. {
  196. char achar;
  197. int c;
  198. int kill;
  199. if (!inited)
  200. return 0;
  201. kill = 0;
  202. while (1) {
  203. c = getch() & 0177;
  204. if (c == G_intr_char()) {
  205. if (kill++ >= 3) {
  206. End_curses();
  207. exit(EXIT_SUCCESS);
  208. }
  209. continue;
  210. }
  211. kill = 0;
  212. if (c != 18)
  213. break;
  214. Curses_replot_screen();
  215. }
  216. if (with_echo) {
  217. achar = c;
  218. addch(achar);
  219. refresh();
  220. }
  221. return c;
  222. }