home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / dev / c / curses / src / initscr.c < prev    next >
C/C++ Source or Header  |  1994-02-21  |  7KB  |  234 lines

  1. /* -*-C-*-
  2.  *
  3.  *
  4.  * Filename : initscr.c
  5.  *
  6.  * Author   : Simon J Raybould.    (sie@fulcrum.bt.co.uk).
  7.  *
  8.  * Date     : Friday 23rd August 1991.
  9.  *
  10.  * Desc     : Initialise curses.
  11.  *
  12.  *
  13.  * THIS CODE IS NOT PUBLIC DOMAIN
  14.  * ==============================
  15.  * 
  16.  * This code is copyright Simon J Raybould 1991, all rights are reserved.
  17.  * All code, ideas, data structures and algorithms remain the property of the
  18.  * author. Neither the whole nor sections of this code may be used as part
  19.  * of other code without the authors consent. If you wish to use some of this
  20.  * code then please email me at (sie@fulcrum.bt.co.uk).
  21.  *
  22.  * This source is not public domain, so you do not have any right to alter it
  23.  * or sell it for personal gain. The source is provided purely for reference
  24.  * purposes. You may re-compile the source with any compiler you choose.
  25.  * You must not distribute altered copies without the authors consent. My
  26.  * intention is that the source will help people isolate any bugs much more
  27.  * effectivly.
  28.  *
  29.  * Disclaimer
  30.  * ==========
  31.  *
  32.  * No implication is made as to this code being fit for any purpose at all.
  33.  * I (the author) shall not be held responsible for any loss of data or damage 
  34.  * to property that may result from its use or misuse.
  35.  *
  36.  *
  37.  * Revision History
  38.  * ================
  39.  *
  40.  * $Log: initscr.c,v $
  41.  * Revision 1.8  1994/02/21  22:12:00  sie
  42.  * added call to setupterm.
  43.  * renamed BreakHandler as _BreakHandler
  44.  *
  45.  * Revision 1.7  1993/05/17  23:33:10  sie
  46.  * Underscores added to names.
  47.  * Changes for version 2.10
  48.  *
  49.  * Revision 1.6  1992/12/26  00:23:56  sie
  50.  * Made more amendments to the LINES and COLS calculations.
  51.  * Now sets them according to the system font and screen mode.
  52.  * They are over-ridden if the env variables are set up.
  53.  * In ANSI mode, they are just set to 24 and 80.
  54.  *
  55.  * Revision 1.5  92/07/14  20:45:27  sie
  56.  * fixed screen size problems.
  57.  * 
  58.  * Revision 1.4  92/06/10  23:44:41  sie
  59.  * Added serial support.
  60.  * 
  61.  * Revision 1.3  92/01/25  23:54:31  sie
  62.  * Now uses fontheight and fontwidth.
  63.  * Different algorithm for working out LINES and COLS.
  64.  * 
  65.  * Revision 1.2  91/12/30  10:31:03  sie
  66.  * Removed LRLine and LRATTRS.
  67.  * The speed increase caused by them was too insignificant.
  68.  * 
  69.  * Revision 1.1  91/09/07  11:43:35  sie
  70.  * Initial revision
  71.  * 
  72.  *
  73.  */
  74.  
  75. static char *rcsid = "$Header: /SRC/lib/curses/src/RCS/initscr.c,v 1.8 1994/02/21 22:12:00 sie Exp $";
  76.  
  77. /* version strings */
  78. #define VERSION      "Amiga curses by Simon J Raybould  V2.00+ 30.Jun.92"
  79. static char *version = "$VER: Amiga curses (SJR) version 2.00+";
  80.  
  81.  
  82. #include <signal.h>
  83. #include "acurses.h"
  84.  
  85. extern struct Library *ConsoleDevice;
  86.  
  87. static struct NewScreen NewScreen = {
  88.   0, 0, 0, 0, 4, 0, 1, HIRES, CUSTOMSCREEN, NULL, "Curses screen", NULL, NULL
  89. };
  90.  
  91. static struct NewWindow NewWindow = {
  92.   0, 0, 0, 0, -1, -1, RAWKEY, ACTIVATE | BORDERLESS,
  93.   NULL, NULL, NULL, NULL, NULL, 0,0,0,0, CUSTOMSCREEN
  94. };
  95.  
  96. /*
  97.  *  Make version number appear when right mouse button is pressed.
  98.  */
  99. static struct Menu _CursesMenu = {
  100.   NULL, 0, 0, 0, 0, 0, VERSION, NULL, 0, 0, 0, 0
  101. };
  102.  
  103. static UWORD _ColourTable[] = {
  104.   0x000, 0xfff, 0xff0, 0x00f, 0xf0f, 0x0ff, 0xf00, 0x0f0
  105. };
  106.  
  107. static int
  108. _BreakHandler(void)
  109. {
  110.   endwin();            /* tidy up */
  111.   fprintf(stderr, "Amiga Curses exiting after receiving interrupt signal\n");
  112.   _CleanExit(20);
  113.   return 0;
  114. }
  115.  
  116. initscr(void)
  117. {
  118.   char *Ptr, *getenv(const char *);
  119.   int Tmp;
  120.   struct Screen WBScreen;
  121.   
  122.   /*
  123.    *  It would be devestating if someone called initscr() twice
  124.    *  so make subsequent calls just return OK.
  125.    */
  126.   if(_CursesFlags & CFLAG_INITSCR)
  127.     return OK;
  128.   
  129.   _CursesFlags |= CFLAG_INITSCR;
  130.   
  131.   /* Work out which sort of curses is required */
  132.   _CursesType = CUST_CURSES;
  133.   if(Ptr = getenv("CURSESTYPE")) {
  134.     if(!strcmp(Ptr, "ansi") || !strcmp(Ptr, "ANSI"))
  135.       _CursesType = ANSI_CURSES;
  136.   }
  137.   if(_CursesType == CUST_CURSES) {
  138.     if((IntuitionBase = (struct IntuitionBase *)
  139.     OpenLibrary("intuition.library", 0)) == NULL) {
  140.       fprintf(stderr, "Failed to open Intuition library");
  141.       _CleanExit(10);
  142.     }
  143.     /* Open graphics library */
  144.     if((GfxBase = (struct GfxBase *)
  145.     OpenLibrary("graphics.library", 0))==NULL) {
  146.       fprintf(stderr, "Failed to open Graphics library");
  147.       _CleanExit(10);
  148.     }
  149.     /* Get WB screen height and Width */
  150.     if(!GetScreenData((char *)&WBScreen, sizeof(struct Screen), WBENCHSCREEN, NULL)) {
  151.       printf("Can't get screen size\n");
  152.       exit(10);
  153.     }
  154.     NewScreen.Height = NewWindow.Height = WBScreen.Height;
  155.     NewScreen.Width = NewWindow.Width = WBScreen.Width;
  156.     NewScreen.Depth = DEPTH;
  157.     /* Set interlace if height>=400 */
  158.     if(NewScreen.Height>=400)
  159.       NewScreen.ViewModes |= LACE;
  160.     
  161.     /*
  162.      * must have the console.device opened to use RawKeyConvert()
  163.      */
  164.     if(OpenDevice("console.device", -1L, (struct IORequest *)&ioreq, 0L))
  165.       _CleanExit(10);
  166.     ConsoleDevice = (struct Library *)ioreq.io_Device;
  167.     
  168.     if((_CursesScreen = (struct Screen *)OpenScreen(&NewScreen)) == NULL) {
  169.       fprintf(stderr, "Failed to open Screen");
  170.       _CleanExit(10);
  171.     }
  172.     _RPort = &(_CursesScreen->RastPort);
  173.     _VPort = &(_CursesScreen->ViewPort);
  174.  
  175.     LoadRGB4(_VPort, _ColourTable, (1<<DEPTH));
  176.     SetDrMd(_RPort, JAM2);
  177.     SetAPen(_RPort, 1);
  178.     NewWindow.Screen = _CursesScreen;
  179.     if((_CursesWindow=(struct Window *)OpenWindow(&NewWindow)) == NULL) {
  180.       fprintf(stderr, "Failed to open Window\n");
  181.       _CleanExit(10);
  182.     }
  183.     _CursesMsgPort = _CursesWindow->UserPort;
  184.     SetMenuStrip(_CursesWindow, &_CursesMenu);
  185.  
  186.     /* Get font size */
  187.     _FontHeight = _CursesWindow->IFont->tf_YSize;
  188.     _FontWidth = _CursesWindow->IFont->tf_XSize;
  189.     _FontBase = _CursesWindow->IFont->tf_Baseline;
  190.     /* Alter the default LINES and COLS for custom curses */
  191.     LINES = WBScreen.Height/_FontHeight;
  192.     COLS = WBScreen.Width/_FontWidth;
  193.   } else {                      /* ANSI only */
  194.     /* if LINES and/or COLS set as environment variables then use them */
  195.     if((Ptr = getenv("LINES"))) {
  196.       Tmp = atoi(Ptr);
  197.       if(Tmp>0 && Tmp<=MAXLINES)
  198.         LINES = Tmp;
  199.     }
  200.     if((Ptr = getenv("COLS"))) {
  201.       Tmp = atoi(Ptr);
  202.       if(Tmp>0 && Tmp<=MAXCOLS)
  203.         COLS = Tmp;
  204.     }
  205.   }
  206. #ifdef DEBUG
  207.   printf("FontHeight=%d, FontWidth=%d, FontBase=%d\n", _FontHeight, _FontWidth, _FontBase);
  208.   printf("LINES=%d, COLS=%d\n", LINES, COLS);
  209. #endif
  210.  
  211.   /* Create stdscr and curscr */
  212.   stdscr = newwin(LINES, COLS, 0, 0);
  213.   curscr = newwin(LINES, COLS, 0, 0);  /* used for redraws */
  214. #ifdef LATTICE
  215.   if(onbreak(_BreakHandler)) {
  216.     fprintf(stderr, "Failed to set BREAK handler.\n");
  217.     _CleanExit(10);
  218.   }
  219. #else /* LATTICE */
  220.   if ((int)signal(SIGINT, _BreakHandler) == -1) {
  221.     perror("Failed to set BREAK handler.");
  222.     _CleanExit(10);
  223.   }
  224. #endif /* else LATTICE */
  225.  
  226.   if(_CursesType == ANSI_CURSES) {
  227.     _ifh = Input();
  228.     if(_CursesFlags & CFLAG_CBREAK)
  229.       _RawMode(_ifh);
  230.     setupterm();                /* get the capability strings ready */
  231.   }
  232.   return OK;
  233. }
  234.