home *** CD-ROM | disk | FTP | other *** search
/ ftp.ncftp.com / ftp.ncftp.com.zip / ftp.ncftp.com / ncftp / older_versions / ncftp-3.2.2-src.tar.bz2 / ncftp-3.2.2-src.tar / ncftp-3.2.2 / vis / wutil.c < prev    next >
C/C++ Source or Header  |  2005-01-01  |  6KB  |  394 lines

  1. /* wutil.c
  2.  *
  3.  * Copyright (c) 1993-2005 Mike Gleason, NcFTP Software.
  4.  * All rights reserved.
  5.  *
  6.  */
  7.  
  8.  
  9. #include "syshdrs.h"
  10. #ifdef PRAGMA_HDRSTOP
  11. #    pragma hdrstop
  12. #endif
  13.  
  14. #include "../ncftp/util.h"
  15. #include "wutil.h"
  16.  
  17. int gWinInit = 0;
  18. int gScreenWidth = 0;
  19. int gScreenHeight = 0;
  20.  
  21. void
  22. EndWin(void)
  23. {
  24.     if (gWinInit) {
  25.         gWinInit = 0;
  26.  
  27.         /* Ideally we would save the whole screen of data before
  28.          * starting, and restore it here.
  29.          */
  30.         wclear(stdscr);
  31.         refresh();
  32.  
  33.         endwin();
  34.     }
  35. }    /* EndWin */
  36.  
  37.  
  38.  
  39.  
  40. void
  41. Exit(int exitStatus)
  42. {
  43.     EndWin();
  44.     exit(exitStatus);
  45. }    /* Exit */
  46.  
  47.  
  48.  
  49.  
  50. #ifdef NOT_USED_YET
  51.  
  52. void
  53. SaveScreen(void)
  54. {
  55.     if (gWinInit) {
  56.         /* Clearing the screen is necessary
  57.          * because ncurses doesn't move the
  58.          * cursor to the bottom left.
  59.          *
  60.          * This also causes the restore
  61.          * operation to require that we paint
  62.          * all our windows by hand, because we
  63.          * have just left the screen blank so
  64.          * when refresh gets called in the
  65.          * restore it just returns the screen
  66.          * to blank.
  67.          *
  68.          * If it weren't for this screen clear,
  69.          * we would be able to get away with
  70.          * just doing an endwin(), the shell,
  71.          * and then a refresh() without us
  72.          * re-drawing any windows manually.
  73.          */
  74.         wclear(stdscr);
  75.         refresh();
  76.          endwin();
  77.         fflush(stdout);
  78.         fflush(stderr);
  79.     }
  80. }    /* SaveScreen */
  81.  
  82.  
  83.  
  84. void
  85. TTYWaitForReturn(void)
  86. {
  87.     int tty;
  88.     int junk;
  89.  
  90.     tty = open("/dev/tty", O_RDWR);
  91.     if (tty != -1) {
  92.         write(tty, "[Hit return]", 12);
  93.         read(tty, &junk, 1);
  94.         close(tty);
  95.     }
  96. }    /* TTYWaitForReturn */
  97.  
  98.  
  99.  
  100. void
  101. RestoreScreen(int pressKey)
  102. {
  103.     if (gWinInit) {
  104.         if (pressKey) {
  105. #    if !defined(CURSES_SHELL_BUG) || (CURSES_SHELL_BUG == 0)
  106.             TTYWaitForReturn();
  107. #    else
  108.             sleep(2);
  109. #    endif
  110.         }
  111.         refresh();
  112. /*        UpdateScreen(1); */
  113.     }
  114. }    /* RestoreScreen */
  115.  
  116.  
  117.  
  118. void
  119. Beep(int on)
  120. {
  121.     static time_t lastBeepTime = 0;
  122.     time_t now;
  123.  
  124.     time(&now);
  125.  
  126.     /* Don't flood the user with beeps. Once per two seconds is reasonable. */
  127.     if ((on > 0) && ((int) (now - lastBeepTime) > 1)) {
  128.         if (gWinInit)
  129.             BEEP(1);
  130.         else
  131.         {
  132.             fprintf(stderr, "\007");    /* ^G */
  133.             fflush(stderr);
  134.         }
  135.     }
  136.     lastBeepTime = now;
  137. }    /* Beep */
  138.  
  139. #endif    /* NOT_USED_YET */
  140.  
  141.  
  142.  
  143.  
  144. #if !defined(HAVE_GETCURX) && defined(HAVE_GETYX)
  145. static int
  146. getcurx(WINDOW *const w)
  147. {
  148.     int cx, cy;
  149.     NCFTP_USE_VAR(cy);
  150.     getyx(w, cy, cx);
  151.     return (cx);
  152. }    /* getcurx */
  153. #endif
  154.  
  155.  
  156.  
  157. /* Sometimes ncurses' wclrtoeol() gets confused when reverse text was on.
  158.  * This forces is to use space characters to blank out the line instead
  159.  * of the tty's clear-to-end-of-line built-in, if present.
  160.  */
  161. void
  162. swclrtoeol(WINDOW *w)
  163. {
  164.     int maxx;
  165.     int curx;
  166.  
  167.     maxx = getmaxx(w);
  168.     curx = getcurx(w);
  169.     for ( ; curx < maxx; curx++)
  170.         waddch(w, ' ');
  171. }    /* swclrtoeol */
  172.  
  173.  
  174.  
  175.  
  176. /* Many old curses libraries don't support wattron() and its attributes.
  177.  * They should support wstandout() though.  This routine is an attempt
  178.  * to use the best substitute available, depending on what the curses
  179.  * library has.
  180.  */
  181. void
  182. WAttr(WINDOW *w, int attr, int on)
  183. {
  184.     /* Define PLAIN_TEXT_ONLY if you have the attributes, but don't want
  185.      * to use them.
  186.      */
  187. #ifndef PLAIN_TEXT_ONLY
  188. #ifdef A_REVERSE
  189.     if (attr & kReverse) {
  190.         if (on)
  191.             wattron(w, A_REVERSE);
  192.         else
  193.             wattroff(w, A_REVERSE);
  194.     }
  195. #else
  196.     if (attr & kReverse) {
  197.         if (on)
  198.             wstandout(w);
  199.         else
  200.             wstandend(w);
  201.  
  202.         /* Nothing else will be done anyway, so just return now. */
  203.         return;
  204.     }
  205. #endif    /* A_REVERSE */
  206.  
  207. #ifdef A_BOLD
  208.     if (attr & kBold) {
  209.         if (on)
  210.             wattron(w, A_BOLD);
  211.         else
  212.             wattroff(w, A_BOLD);
  213.     }
  214. #else
  215.     /* Do nothing.  Plain is best substitute. */
  216. #endif    /* A_BOLD */
  217.  
  218. #ifdef A_UNDERLINE
  219.     if (attr & kUnderline) {
  220.         if (on)
  221.             wattron(w, A_UNDERLINE);
  222.         else
  223.             wattroff(w, A_UNDERLINE);
  224.     }
  225. #else
  226.     /* Try using standout mode in place of underline. */
  227.     if (attr & kUnderline) {
  228.         if (on)
  229.             wstandout(w);
  230.         else
  231.             wstandend(w);
  232.  
  233.         /* Nothing else will be done anyway, so just return now. */
  234.         return;
  235.     }
  236. #endif    /* A_UNDERLINE */
  237.  
  238. #ifdef A_DIM
  239.     if (attr & kDim) {
  240.         if (on)
  241.             wattron(w, A_DIM);
  242.         else
  243.             wattroff(w, A_DIM);
  244.     }
  245. #else
  246.     /* Do nothing.  Plain is best substitute. */
  247. #endif    /* A_DIM */
  248.  
  249. #ifdef A_NORMAL
  250.     if (attr == kNormal) {
  251.         wattrset(w, A_NORMAL);
  252.         return;
  253.     }
  254. #else
  255.     /* At least make sure standout mode is off. */
  256.     if (attr == kNormal) {
  257.         wstandend(w);
  258.         return;
  259.     }
  260. #endif    /* A_NORMAL */
  261. #endif    /* PLAIN_TEXT_ONLY */
  262. }    /* WAttr */
  263.  
  264.  
  265.  
  266.  
  267. void DrawStrAt(WINDOW *const win, int y, int x, const char *const str)
  268. {
  269. #if defined(WADDSTR_TYPE_ARG1_CONST) && !defined(FREEBSD)
  270.     mvwaddstr(win, y, x, str);
  271. #else
  272.     /* Ugly hack for systems whose mvwaddstr takes a (char *) rather than
  273.      * a (const char *).
  274.      */
  275.     char *cp = strdup(str);
  276.     if (cp == NULL)
  277.         return;
  278.     mvwaddstr(win, y, x, cp);
  279.     free(cp);
  280. #endif
  281. }    /* DrawStrAt */
  282.  
  283.  
  284.  
  285. /* Draws a string centered in a window. */
  286. void WAddCenteredStr(WINDOW *const w, int y, const char *const str)
  287. {
  288.     int x;
  289.     int maxx;
  290.  
  291.     maxx = getmaxx(w);
  292.     x = (maxx - (int) strlen(str)) / 2;
  293.     if (x < 0)
  294.         x = 0;
  295.     DrawStrAt(w, y, x, str);
  296. }    /* WAddCenteredStr */
  297.  
  298.  
  299.  
  300.  
  301. static void
  302. SigTerm(int UNUSED(sig))
  303. {
  304.     LIBNCFTP_USE_VAR(sig);
  305.     Exit(1);
  306. }    /* SigTerm */
  307.  
  308.  
  309.  
  310.  
  311. #ifndef NCURSES_VERSION
  312. #ifdef SIGTSTP
  313. static void
  314. SigTstp(int UNUSED(sig))
  315. {
  316.     LIBNCFTP_USE_VAR(sig);
  317.     /* TO-DO */
  318.  
  319.     /* Ncurses is smart enough to handle this for us,
  320.      * but for those other curses libs, we should really
  321.      * suspend gracefully instead of exiting.
  322.      */
  323.     Exit(1);
  324. }    /* SigTstp */
  325. #endif
  326. #endif
  327.  
  328.  
  329.  
  330.  
  331. int
  332. InitWindows(void)
  333. {
  334.     int maxx = 0, maxy = 0;
  335.  
  336.     gWinInit = 0;
  337.     initscr();
  338.     if (stdscr == NULL)
  339.         return (-1);
  340.     gWinInit = 1;
  341.     NcSignal(SIGTERM, SigTerm);
  342.  
  343.     nl();
  344.     noecho();    /* Leave this off until we need it. */
  345.  
  346.     getmaxyx(stdscr, maxy, maxx);
  347.     gScreenWidth = maxx;
  348.     gScreenHeight = maxy;
  349.  
  350. #ifndef NCURSES_VERSION
  351. #ifdef SIGTSTP
  352.     if (NcSignal(SIGTSTP, (FTPSigProc) SIG_IGN) != (FTPSigProc) SIG_IGN) {
  353.         NcSignal(SIGTSTP, SigTstp);
  354.         NcSignal(SIGCONT, SigTstp);
  355.     }
  356. #endif
  357. #endif
  358.  
  359.     return (0);
  360. }    /* InitWindows */
  361.  
  362.  
  363.  
  364. int
  365. PrintDimensions(int shortMode)
  366. {
  367.     int maxy = 0, maxx = 0;
  368.     char buf[128];
  369.  
  370.     initscr();
  371.     if (stdscr == NULL)
  372.         return (-1);
  373.     getmaxyx(stdscr, maxy, maxx);
  374.     endwin();
  375.     if ((maxx > 0) && (maxy > 0)) {
  376.         memset(buf, 0, sizeof(buf));
  377. #ifdef HAVE_SNPRINTF
  378.         (void) snprintf(
  379.             buf,
  380.             sizeof(buf) - 1,
  381. #else
  382.         (void) sprintf(
  383.             buf,
  384. #endif
  385.             (shortMode != 0) ? "%d %d\n" : "COLUMNS=%d\nLINES=%d\nexport COLUMNS\nexport LINES\n",
  386.             maxx,
  387.             maxy
  388.         );
  389.         (void) write(1, buf, strlen(buf));
  390.         return (0);
  391.     }
  392.     return (-1);
  393. }    /* PrintDimensions */
  394.