kernel.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* PDCurses */
  2. #include <curspriv.h>
  3. /*man-start**************************************************************
  4. kernel
  5. ------
  6. ### Synopsis
  7. int def_prog_mode(void);
  8. int def_shell_mode(void);
  9. int reset_prog_mode(void);
  10. int reset_shell_mode(void);
  11. int resetty(void);
  12. int savetty(void);
  13. int ripoffline(int line, int (*init)(WINDOW *, int));
  14. int curs_set(int visibility);
  15. int napms(int ms);
  16. int draino(int ms);
  17. int resetterm(void);
  18. int fixterm(void);
  19. int saveterm(void);
  20. ### Description
  21. def_prog_mode() and def_shell_mode() save the current terminal modes
  22. as the "program" (in curses) or "shell" (not in curses) state for use
  23. by the reset_prog_mode() and reset_shell_mode() functions. This is
  24. done automatically by initscr().
  25. reset_prog_mode() and reset_shell_mode() restore the terminal to
  26. "program" (in curses) or "shell" (not in curses) state. These are
  27. done automatically by endwin() and doupdate() after an endwin(), so
  28. they would normally not be called before these functions.
  29. savetty() and resetty() save and restore the state of the terminal
  30. modes. savetty() saves the current state in a buffer, and resetty()
  31. restores the state to what it was at the last call to savetty().
  32. curs_set() alters the appearance of the cursor. A visibility of 0
  33. makes it disappear; 1 makes it appear "normal" (usually an underline)
  34. and 2 makes it "highly visible" (usually a block).
  35. ripoffline() reduces the size of stdscr by one line. If the "line"
  36. parameter is positive, the line is removed from the top of the
  37. screen; if negative, from the bottom. Up to 5 lines can be ripped off
  38. stdscr by calling ripoffline() repeatedly. The function argument,
  39. init, is called from within initscr() or newterm(), so ripoffline()
  40. must be called before either of these functions. The init function
  41. receives a pointer to a one-line WINDOW, and the width of the window.
  42. Calling ripoffline() with a NULL init function pointer is an error.
  43. napms() suspends the program for the specified number of
  44. milliseconds. draino() is an archaic equivalent. Note that since
  45. napms() attempts to give up a time slice and yield control back to
  46. the OS, all times are approximate. (In DOS, the delay is actually
  47. rounded down to 50ms (1/20th sec) intervals, with a minimum of one
  48. interval; i.e., 1-99 will wait 50ms, 100-149 will wait 100ms, etc.)
  49. 0 returns immediately.
  50. resetterm(), fixterm() and saveterm() are archaic equivalents for
  51. reset_shell_mode(), reset_prog_mode() and def_prog_mode(),
  52. respectively.
  53. ### Return Value
  54. All functions return OK on success and ERR on error, except
  55. curs_set(), which returns the previous visibility.
  56. ### Portability
  57. X/Open ncurses NetBSD
  58. def_prog_mode Y Y Y
  59. def_shell_mode Y Y Y
  60. reset_prog_mode Y Y Y
  61. reset_shell_mode Y Y Y
  62. resetty Y Y Y
  63. savetty Y Y Y
  64. ripoffline Y Y Y
  65. curs_set Y Y Y
  66. napms Y Y Y
  67. fixterm - Y -
  68. resetterm - Y -
  69. saveterm - Y -
  70. draino - - -
  71. **man-end****************************************************************/
  72. #include <string.h>
  73. RIPPEDOFFLINE linesripped[5];
  74. char linesrippedoff = 0;
  75. static struct cttyset
  76. {
  77. bool been_set;
  78. SCREEN saved;
  79. } ctty[3];
  80. enum { PDC_SH_TTY, PDC_PR_TTY, PDC_SAVE_TTY };
  81. static void _save_mode(int i)
  82. {
  83. ctty[i].been_set = TRUE;
  84. memcpy(&(ctty[i].saved), SP, sizeof(SCREEN));
  85. PDC_save_screen_mode(i);
  86. }
  87. static int _restore_mode(int i)
  88. {
  89. if (ctty[i].been_set == TRUE)
  90. {
  91. memcpy(SP, &(ctty[i].saved), sizeof(SCREEN));
  92. if (ctty[i].saved.raw_out)
  93. raw();
  94. PDC_restore_screen_mode(i);
  95. if ((LINES != ctty[i].saved.lines) ||
  96. (COLS != ctty[i].saved.cols))
  97. resize_term(ctty[i].saved.lines, ctty[i].saved.cols);
  98. PDC_curs_set(ctty[i].saved.visibility);
  99. PDC_gotoyx(ctty[i].saved.cursrow, ctty[i].saved.curscol);
  100. }
  101. return ctty[i].been_set ? OK : ERR;
  102. }
  103. int def_prog_mode(void)
  104. {
  105. PDC_LOG(("def_prog_mode() - called\n"));
  106. if (!SP)
  107. return ERR;
  108. _save_mode(PDC_PR_TTY);
  109. return OK;
  110. }
  111. int def_shell_mode(void)
  112. {
  113. PDC_LOG(("def_shell_mode() - called\n"));
  114. if (!SP)
  115. return ERR;
  116. _save_mode(PDC_SH_TTY);
  117. return OK;
  118. }
  119. int reset_prog_mode(void)
  120. {
  121. PDC_LOG(("reset_prog_mode() - called\n"));
  122. if (!SP)
  123. return ERR;
  124. _restore_mode(PDC_PR_TTY);
  125. PDC_reset_prog_mode();
  126. return OK;
  127. }
  128. int reset_shell_mode(void)
  129. {
  130. PDC_LOG(("reset_shell_mode() - called\n"));
  131. if (!SP)
  132. return ERR;
  133. _restore_mode(PDC_SH_TTY);
  134. PDC_reset_shell_mode();
  135. return OK;
  136. }
  137. int resetty(void)
  138. {
  139. PDC_LOG(("resetty() - called\n"));
  140. if (!SP)
  141. return ERR;
  142. return _restore_mode(PDC_SAVE_TTY);
  143. }
  144. int savetty(void)
  145. {
  146. PDC_LOG(("savetty() - called\n"));
  147. if (!SP)
  148. return ERR;
  149. _save_mode(PDC_SAVE_TTY);
  150. return OK;
  151. }
  152. int curs_set(int visibility)
  153. {
  154. int ret_vis;
  155. PDC_LOG(("curs_set() - called: visibility=%d\n", visibility));
  156. if (!SP || visibility < 0 || visibility > 2)
  157. return ERR;
  158. ret_vis = PDC_curs_set(visibility);
  159. /* If the cursor is changing from invisible to visible, update
  160. its position */
  161. if (visibility && !ret_vis)
  162. PDC_gotoyx(SP->cursrow, SP->curscol);
  163. return ret_vis;
  164. }
  165. int napms(int ms)
  166. {
  167. PDC_LOG(("napms() - called: ms=%d\n", ms));
  168. if (!SP)
  169. return ERR;
  170. if (SP->dirty)
  171. {
  172. int curs_state = SP->visibility;
  173. bool leave_state = is_leaveok(curscr);
  174. SP->dirty = FALSE;
  175. leaveok(curscr, TRUE);
  176. wrefresh(curscr);
  177. leaveok(curscr, leave_state);
  178. curs_set(curs_state);
  179. }
  180. if (ms)
  181. PDC_napms(ms);
  182. return OK;
  183. }
  184. int ripoffline(int line, int (*init)(WINDOW *, int))
  185. {
  186. PDC_LOG(("ripoffline() - called: line=%d\n", line));
  187. if (linesrippedoff < 5 && line && init)
  188. {
  189. linesripped[(int)linesrippedoff].line = line;
  190. linesripped[(int)linesrippedoff++].init = init;
  191. return OK;
  192. }
  193. return ERR;
  194. }
  195. int draino(int ms)
  196. {
  197. PDC_LOG(("draino() - called\n"));
  198. return napms(ms);
  199. }
  200. int resetterm(void)
  201. {
  202. PDC_LOG(("resetterm() - called\n"));
  203. return reset_shell_mode();
  204. }
  205. int fixterm(void)
  206. {
  207. PDC_LOG(("fixterm() - called\n"));
  208. return reset_prog_mode();
  209. }
  210. int saveterm(void)
  211. {
  212. PDC_LOG(("saveterm() - called\n"));
  213. return def_prog_mode();
  214. }