home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume8 / cutup / part01 / scr.c < prev    next >
C/C++ Source or Header  |  1989-09-18  |  2KB  |  178 lines

  1. #include <signal.h>
  2. #include <fcntl.h>
  3. #include <ctype.h>
  4. #include <curses.h>
  5. #include "consts.h"
  6. #include "types.h"
  7. #include "funcs.h"
  8. #include "macros.h"
  9.  
  10. extern int StopIO;
  11. extern int ChildPid;
  12.  
  13. void KillChild();
  14. void FixNodelay();
  15. void zap();
  16. void dumpcore();
  17.  
  18. static int SavedFcntl;
  19.  
  20. WINDOW *CreateWindow(old_win, lines, cols, y, x)
  21.  
  22. WINDOW *old_win;
  23. int lines, cols;
  24. int y, x;
  25.  
  26.     {
  27.     WINDOW *front_win;
  28.     
  29.     front_win = newwin(lines, cols, y, x);
  30.     touchwin(front_win);
  31.     box(front_win, 0, 0);
  32.     old_win = old_win;
  33.  
  34.     return front_win;
  35.     }
  36.  
  37. void RemoveWindow(old_win, new_win)
  38.  
  39. WINDOW *old_win;
  40. WINDOW *new_win;
  41.  
  42.     {
  43.     werase(new_win);
  44.     wrefresh(new_win);
  45.     delwin(new_win);
  46.     touchwin(old_win);
  47.     wrefresh(old_win);
  48.     }
  49.  
  50. WINDOW *InitScreen()
  51.  
  52.     {
  53.     WINDOW *basic_w;
  54.  
  55.     SavedFcntl = fcntl(fileno(stdin), F_GETFL, 0);
  56.     signal(SIGINT, zap);
  57.     signal(SIGQUIT, dumpcore);
  58.     signal(SIGPIPE, zap);
  59.     initscr();
  60.     noecho();
  61.     crmode();
  62.     basic_w = stdscr;
  63.     werase(basic_w);
  64.     box(basic_w, 0, 0);
  65.     wrefresh(basic_w);
  66.     return basic_w;
  67.     }
  68.  
  69. void EndScreen()
  70.  
  71.     {
  72.     echo();
  73.     nocrmode();
  74.     endwin();
  75.     FixNodelay();
  76.     }
  77.  
  78. int GetString(win, str, max_len)
  79.  
  80. WINDOW *win;
  81. char *str;
  82. int max_len;
  83.  
  84.     {
  85.     int y, x;
  86.     int len = 0;
  87.     int ch;
  88.  
  89.     noecho();
  90.     getyx(win, y, x);
  91.     wrefresh(win);
  92.     while ((ch = wgetch(win)) != '\n')
  93.         {
  94.         switch (ch)
  95.             {
  96.             case ASCII_BS :
  97.             case ASCII_DEL :
  98.             if (len > 0)
  99.                 {
  100.                 --len;
  101.                 mvwaddch(win, y, x + len, ' ');
  102.                 }
  103.             else
  104.                 flash();
  105.             break;
  106.             default :
  107.             if (len < max_len && isalnum(ch))
  108.                 {
  109.                 str[len] = ch;
  110.                 mvwaddch(win, y, x + len, ch);
  111.                 ++len;
  112.                 }
  113.             else
  114.                 flash();
  115.             break;
  116.             }
  117.         wmove(win, y, x + len);
  118.         wrefresh(win);
  119.         }
  120.     str[len] = 0;
  121.  
  122.     return len;
  123.     }
  124.  
  125. void BlockSignals()
  126.  
  127.     {
  128.     signal(SIGINT, SIG_IGN);
  129.     signal(SIGQUIT, SIG_IGN);
  130.     }
  131.  
  132. static void zap(signo)
  133.  
  134. int signo;
  135.  
  136.     {
  137.     if (! StopIO)
  138.         {
  139.         endwin();
  140.         if (signo == SIGINT)
  141.             fprintf(stderr, "You aborted!\n");
  142.         else
  143.             fprintf(stderr, "Your opponent aborted!\n");
  144.         FixNodelay();
  145.         KillChild();
  146.         }
  147.     EndFIFO();
  148.     exit(1);
  149.     }
  150.  
  151. static void dumpcore()
  152.  
  153.     {
  154.     if (StopIO)
  155.         return;
  156.     endwin();
  157.     FixNodelay();
  158.     signal(SIGQUIT, SIG_DFL);
  159.     KillChild();
  160.     kill(getpid(), SIGQUIT);
  161.     pause();
  162.     }
  163.  
  164. static void KillChild()
  165.  
  166.     {
  167.     if (ChildPid <= 0)
  168.         return;
  169.     signal(SIGCLD, SIG_IGN);
  170.     kill(ChildPid, SIGKILL);
  171.     }
  172.  
  173. static void FixNodelay()
  174.  
  175.     {
  176.     (void)fcntl(fileno(stdin), F_SETFL, SavedFcntl);
  177.     }
  178.