initscr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /* PDCurses */
  2. #include <curspriv.h>
  3. /*man-start**************************************************************
  4. initscr
  5. -------
  6. ### Synopsis
  7. WINDOW *initscr(void);
  8. WINDOW *Xinitscr(int argc, char **argv);
  9. int endwin(void);
  10. bool isendwin(void);
  11. SCREEN *newterm(const char *type, FILE *outfd, FILE *infd);
  12. SCREEN *set_term(SCREEN *new);
  13. void delscreen(SCREEN *sp);
  14. int resize_term(int nlines, int ncols);
  15. bool is_termresized(void);
  16. const char *curses_version(void);
  17. void PDC_get_version(PDC_VERSION *ver);
  18. int set_tabsize(int tabsize);
  19. ### Description
  20. initscr() should be the first curses routine called. It will
  21. initialize all curses data structures, and arrange that the first
  22. call to refresh() will clear the screen. In case of error, initscr()
  23. will write a message to standard error and end the program.
  24. endwin() should be called before exiting or escaping from curses mode
  25. temporarily. It will restore tty modes, move the cursor to the lower
  26. left corner of the screen and reset the terminal into the proper
  27. non-visual mode. To resume curses after a temporary escape, call
  28. refresh() or doupdate().
  29. isendwin() returns TRUE if endwin() has been called without a
  30. subsequent refresh, unless SP is NULL.
  31. In some implementations of curses, newterm() allows the use of
  32. multiple terminals. Here, it's just an alternative interface for
  33. initscr(). It always returns SP, or NULL.
  34. delscreen() frees the memory allocated by newterm() or initscr(),
  35. since it's not freed by endwin(). This function is usually not
  36. needed. In PDCurses, the parameter must be the value of SP, and
  37. delscreen() sets SP to NULL.
  38. set_term() does nothing meaningful in PDCurses, but is included for
  39. compatibility with other curses implementations.
  40. resize_term() is effectively two functions: When called with nonzero
  41. values for nlines and ncols, it attempts to resize the screen to the
  42. given size. When called with (0, 0), it merely adjusts the internal
  43. structures to match the current size after the screen is resized by
  44. the user. On the currently supported platforms, SDL, Windows console,
  45. and X11 allow user resizing, while DOS, OS/2, SDL and Windows console
  46. allow programmatic resizing. If you want to support user resizing,
  47. you should check for getch() returning KEY_RESIZE, and/or call
  48. is_termresized() at appropriate times; if either condition occurs,
  49. call resize_term(0, 0). Then, with either user or programmatic
  50. resizing, you'll have to resize any windows you've created, as
  51. appropriate; resize_term() only handles stdscr and curscr.
  52. is_termresized() returns TRUE if the curses screen has been resized
  53. by the user, and a call to resize_term() is needed. Checking for
  54. KEY_RESIZE is generally preferable, unless you're not handling the
  55. keyboard.
  56. curses_version() returns a string describing the version of PDCurses.
  57. PDC_get_version() fills a PDC_VERSION structure provided by the user
  58. with more detailed version info (see curses.h).
  59. set_tabsize() sets the tab interval, stored in TABSIZE.
  60. ### Return Value
  61. All functions return NULL on error, except endwin(), which always
  62. returns OK, and resize_term(), which returns either OK or ERR.
  63. ### Portability
  64. X/Open ncurses NetBSD
  65. initscr Y Y Y
  66. endwin Y Y Y
  67. isendwin Y Y Y
  68. newterm Y Y Y
  69. set_term Y Y Y
  70. delscreen Y Y Y
  71. resize_term - Y Y
  72. set_tabsize - Y Y
  73. curses_version - Y -
  74. is_termresized - - -
  75. **man-end****************************************************************/
  76. #include <stdlib.h>
  77. char ttytype[128];
  78. const char *_curses_notice = "PDCurses " PDC_VERDOT " - " __DATE__;
  79. SCREEN *SP = (SCREEN*)NULL; /* curses variables */
  80. WINDOW *curscr = (WINDOW *)NULL; /* the current screen image */
  81. WINDOW *stdscr = (WINDOW *)NULL; /* the default screen window */
  82. int LINES = 0; /* current terminal height */
  83. int COLS = 0; /* current terminal width */
  84. int TABSIZE = 8;
  85. MOUSE_STATUS Mouse_status;
  86. extern RIPPEDOFFLINE linesripped[5];
  87. extern char linesrippedoff;
  88. WINDOW *initscr(void)
  89. {
  90. int i;
  91. PDC_LOG(("initscr() - called\n"));
  92. if (SP && SP->alive)
  93. return NULL;
  94. SP = calloc(1, sizeof(SCREEN));
  95. if (!SP)
  96. return NULL;
  97. if (PDC_scr_open() == ERR)
  98. {
  99. fprintf(stderr, "initscr(): Unable to create SP\n");
  100. exit(8);
  101. }
  102. SP->autocr = TRUE; /* cr -> lf by default */
  103. SP->raw_out = FALSE; /* tty I/O modes */
  104. SP->raw_inp = FALSE; /* tty I/O modes */
  105. SP->cbreak = TRUE;
  106. SP->key_modifiers = 0L;
  107. SP->return_key_modifiers = FALSE;
  108. SP->echo = TRUE;
  109. SP->visibility = 1;
  110. SP->resized = FALSE;
  111. SP->_trap_mbe = 0L;
  112. SP->linesrippedoff = 0;
  113. SP->linesrippedoffontop = 0;
  114. SP->delaytenths = 0;
  115. SP->line_color = -1;
  116. SP->lastscr = (WINDOW *)NULL;
  117. SP->dbfp = NULL;
  118. SP->color_started = FALSE;
  119. SP->dirty = FALSE;
  120. SP->sel_start = -1;
  121. SP->sel_end = -1;
  122. SP->orig_cursor = PDC_get_cursor_mode();
  123. LINES = SP->lines = PDC_get_rows();
  124. COLS = SP->cols = PDC_get_columns();
  125. if (LINES < 2 || COLS < 2)
  126. {
  127. fprintf(stderr, "initscr(): LINES=%d COLS=%d: too small.\n",
  128. LINES, COLS);
  129. exit(4);
  130. }
  131. curscr = newwin(LINES, COLS, 0, 0);
  132. if (!curscr)
  133. {
  134. fprintf(stderr, "initscr(): Unable to create curscr.\n");
  135. exit(2);
  136. }
  137. SP->lastscr = newwin(LINES, COLS, 0, 0);
  138. if (!SP->lastscr)
  139. {
  140. fprintf(stderr, "initscr(): Unable to create SP->lastscr.\n");
  141. exit(2);
  142. }
  143. wattrset(SP->lastscr, (chtype)(-1));
  144. werase(SP->lastscr);
  145. PDC_slk_initialize();
  146. LINES -= SP->slklines;
  147. /* We have to sort out ripped off lines here, and reduce the height
  148. of stdscr by the number of lines ripped off */
  149. for (i = 0; i < linesrippedoff; i++)
  150. {
  151. if (linesripped[i].line < 0)
  152. (*linesripped[i].init)(newwin(1, COLS, LINES - 1, 0), COLS);
  153. else
  154. (*linesripped[i].init)(newwin(1, COLS,
  155. SP->linesrippedoffontop++, 0), COLS);
  156. SP->linesrippedoff++;
  157. LINES--;
  158. }
  159. linesrippedoff = 0;
  160. stdscr = newwin(LINES, COLS, SP->linesrippedoffontop, 0);
  161. if (!stdscr)
  162. {
  163. fprintf(stderr, "initscr(): Unable to create stdscr.\n");
  164. exit(1);
  165. }
  166. wclrtobot(stdscr);
  167. /* If preserving the existing screen, don't allow a screen clear */
  168. if (SP->_preserve)
  169. {
  170. untouchwin(curscr);
  171. untouchwin(stdscr);
  172. stdscr->_clear = FALSE;
  173. curscr->_clear = FALSE;
  174. }
  175. else
  176. curscr->_clear = TRUE;
  177. SP->atrtab = calloc(PDC_COLOR_PAIRS, sizeof(PDC_PAIR));
  178. if (!SP->atrtab)
  179. return NULL;
  180. PDC_init_atrtab(); /* set up default colors */
  181. MOUSE_X_POS = MOUSE_Y_POS = -1;
  182. BUTTON_STATUS(1) = BUTTON_RELEASED;
  183. BUTTON_STATUS(2) = BUTTON_RELEASED;
  184. BUTTON_STATUS(3) = BUTTON_RELEASED;
  185. Mouse_status.changes = 0;
  186. SP->alive = TRUE;
  187. def_shell_mode();
  188. sprintf(ttytype, "pdcurses|PDCurses for %s", PDC_sysname());
  189. SP->c_buffer = malloc(_INBUFSIZ * sizeof(int));
  190. if (!SP->c_buffer)
  191. return NULL;
  192. SP->c_pindex = 0;
  193. SP->c_gindex = 1;
  194. SP->c_ungch = malloc(NUNGETCH * sizeof(int));
  195. if (!SP->c_ungch)
  196. return NULL;
  197. SP->c_ungind = 0;
  198. SP->c_ungmax = NUNGETCH;
  199. return stdscr;
  200. }
  201. #ifdef XCURSES
  202. WINDOW *Xinitscr(int argc, char **argv)
  203. {
  204. PDC_LOG(("Xinitscr() - called\n"));
  205. PDC_set_args(argc, argv);
  206. return initscr();
  207. }
  208. #endif
  209. int endwin(void)
  210. {
  211. PDC_LOG(("endwin() - called\n"));
  212. /* Allow temporary exit from curses using endwin() */
  213. def_prog_mode();
  214. PDC_scr_close();
  215. SP->alive = FALSE;
  216. return OK;
  217. }
  218. bool isendwin(void)
  219. {
  220. PDC_LOG(("isendwin() - called\n"));
  221. return SP ? !(SP->alive) : FALSE;
  222. }
  223. SCREEN *newterm(const char *type, FILE *outfd, FILE *infd)
  224. {
  225. PDC_LOG(("newterm() - called\n"));
  226. return initscr() ? SP : NULL;
  227. }
  228. SCREEN *set_term(SCREEN *new)
  229. {
  230. PDC_LOG(("set_term() - called\n"));
  231. /* We only support one screen */
  232. return (new == SP) ? SP : NULL;
  233. }
  234. void delscreen(SCREEN *sp)
  235. {
  236. PDC_LOG(("delscreen() - called\n"));
  237. if (!SP || sp != SP)
  238. return;
  239. free(SP->c_ungch);
  240. free(SP->c_buffer);
  241. free(SP->atrtab);
  242. PDC_slk_free(); /* free the soft label keys, if needed */
  243. delwin(stdscr);
  244. delwin(curscr);
  245. delwin(SP->lastscr);
  246. stdscr = (WINDOW *)NULL;
  247. curscr = (WINDOW *)NULL;
  248. SP->lastscr = (WINDOW *)NULL;
  249. SP->alive = FALSE;
  250. PDC_scr_free();
  251. free(SP);
  252. SP = (SCREEN *)NULL;
  253. }
  254. int resize_term(int nlines, int ncols)
  255. {
  256. PDC_LOG(("resize_term() - called: nlines %d\n", nlines));
  257. if (!stdscr || PDC_resize_screen(nlines, ncols) == ERR)
  258. return ERR;
  259. SP->resized = FALSE;
  260. SP->lines = PDC_get_rows();
  261. LINES = SP->lines - SP->linesrippedoff - SP->slklines;
  262. SP->cols = COLS = PDC_get_columns();
  263. if (SP->cursrow >= SP->lines)
  264. SP->cursrow = SP->lines - 1;
  265. if (SP->curscol >= SP->cols)
  266. SP->curscol = SP->cols - 1;
  267. if (wresize(curscr, SP->lines, SP->cols) == ERR ||
  268. wresize(stdscr, LINES, COLS) == ERR ||
  269. wresize(SP->lastscr, SP->lines, SP->cols) == ERR)
  270. return ERR;
  271. werase(SP->lastscr);
  272. curscr->_clear = TRUE;
  273. if (SP->slk_winptr)
  274. {
  275. if (wresize(SP->slk_winptr, SP->slklines, COLS) == ERR)
  276. return ERR;
  277. wmove(SP->slk_winptr, 0, 0);
  278. wclrtobot(SP->slk_winptr);
  279. PDC_slk_initialize();
  280. slk_noutrefresh();
  281. }
  282. touchwin(stdscr);
  283. wnoutrefresh(stdscr);
  284. return OK;
  285. }
  286. bool is_termresized(void)
  287. {
  288. PDC_LOG(("is_termresized() - called\n"));
  289. return SP->resized;
  290. }
  291. const char *curses_version(void)
  292. {
  293. return _curses_notice;
  294. }
  295. void PDC_get_version(PDC_VERSION *ver)
  296. {
  297. if (!ver)
  298. return;
  299. ver->flags = 0
  300. #ifdef PDCDEBUG
  301. | PDC_VFLAG_DEBUG
  302. #endif
  303. #ifdef PDC_WIDE
  304. | PDC_VFLAG_WIDE
  305. #endif
  306. #ifdef PDC_FORCE_UTF8
  307. | PDC_VFLAG_UTF8
  308. #endif
  309. #ifdef PDC_DLL_BUILD
  310. | PDC_VFLAG_DLL
  311. #endif
  312. #ifdef PDC_RGB
  313. | PDC_VFLAG_RGB
  314. #endif
  315. ;
  316. ver->build = PDC_BUILD;
  317. ver->major = PDC_VER_MAJOR;
  318. ver->minor = PDC_VER_MINOR;
  319. ver->csize = sizeof(chtype);
  320. ver->bsize = sizeof(bool);
  321. }
  322. int set_tabsize(int tabsize)
  323. {
  324. PDC_LOG(("set_tabsize() - called: tabsize %d\n", tabsize));
  325. if (tabsize < 1)
  326. return ERR;
  327. TABSIZE = tabsize;
  328. return OK;
  329. }