home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / c / curses / src / initscr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-30  |  5.7 KB  |  208 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.4  92/06/10  23:44:41  sie
  42.  * Added serial support.
  43.  * 
  44.  * Revision 1.3  92/01/25  23:54:31  sie
  45.  * Now uses fontheight and fontwidth.
  46.  * Different algorithm for working out LINES and COLS.
  47.  * 
  48.  * Revision 1.2  91/12/30  10:31:03  sie
  49.  * Removed LRLine and LRATTRS.
  50.  * The speed increase caused by them was too insignificant.
  51.  * 
  52.  * Revision 1.1  91/09/07  11:43:35  sie
  53.  * Initial revision
  54.  * 
  55.  *
  56.  */
  57.  
  58. static char *rcsid = "$Header: SRC:lib/curses/src/RCS/initscr.c,v 1.4 92/06/10 23:44:41 sie Exp $";
  59.  
  60. #include "acurses.h"
  61.  
  62.  
  63. static struct NewScreen NewScreen = {
  64.   0, 0, 0, 0, 4, 0, 1, HIRES, CUSTOMSCREEN, NULL, "Curses screen", NULL, NULL
  65. };
  66.  
  67. static struct NewWindow NewWindow = {
  68.   0, 0, 0, 0, -1, -1, RAWKEY, ACTIVATE | BORDERLESS, NULL, NULL, NULL, NULL,
  69.   NULL, 0,0,0,0, CUSTOMSCREEN
  70. };
  71.  
  72. /*
  73.  *  Make version number appear when right mouse button is pressed.
  74.  */
  75. static struct Menu _CursesMenu = {
  76.   NULL, 0, 0, 0, 0, 0,
  77.   "AMIGA CURSES by Simon J Raybould  V2.00 30.Jun.92",
  78.   NULL, 0, 0, 0, 0
  79. };
  80.  
  81. UWORD ColourTable[] = {
  82.   0x000, 0xfff, 0xff0, 0x00f, 0xf0f, 0x0ff, 0xf00, 0x0f0
  83. };
  84.  
  85. int
  86. BreakHandler(void)
  87. {
  88.   endwin();            /* tidy up */
  89.   fprintf(stderr, "Amiga Curses exiting after receiving interrupt signal\n");
  90.   CleanExit(20);
  91.   return 0;
  92. }
  93.  
  94. initscr(void)
  95. {
  96.   char *Ptr, *getenv(const char *);
  97.   int Tmp, BreakHandler();
  98.   struct Screen WBScreen;
  99.   
  100.   /*
  101.    *  It would be devestating if someone called initscr() twice
  102.    *  so make subsequent calls fail.
  103.    */
  104.   if(CursesFlags & CFLAG_INITSCR)
  105.     return ERR;
  106.   
  107.   CursesFlags |= CFLAG_INITSCR;
  108.   
  109.   CursesType = CUST_CURSES;
  110.   if(Ptr = getenv("CURSESTYPE")) {
  111.     if(!strcmp(Ptr, "ansi"))
  112.       CursesType = ANSI_CURSES;
  113.   }
  114.   if(CursesType == CUST_CURSES) {
  115.     if((IntuitionBase = (struct IntuitionBase *)
  116.     OpenLibrary("intuition.library", 0)) == NULL) {
  117.       fprintf(stderr, "Failed to open Intuition library");
  118.       CleanExit(10);
  119.     }
  120.     /* Open graphics library */
  121.     if((GfxBase = (struct GfxBase *)
  122.     OpenLibrary("graphics.library", 0))==NULL) {
  123.       fprintf(stderr, "Failed to open Graphics library");
  124.       CleanExit(10);
  125.     }
  126.     /* Get Font height and Width */
  127.     if(!GetScreenData((char *)&WBScreen, sizeof(struct Screen), WBENCHSCREEN,
  128.               NULL)) {
  129.       printf("Can't get screen size\n");
  130.       exit(10);
  131.     }
  132.     
  133.     NewScreen.Height = NewWindow.Height = WBScreen.Height;
  134.     NewScreen.Width = NewWindow.Width = WBScreen.Width;
  135.     NewScreen.Depth = DEPTH;
  136.     /* Set interlace if height >= 400 */
  137.     if(NewScreen.Height>400)
  138.       NewScreen.ViewModes |= LACE;
  139.     
  140.     /*
  141.      * must have the console.device opened to use RawKeyConvert()
  142.      */
  143.     if(OpenDevice("console.device", -1L, (struct IORequest *)&ioreq, 0L))
  144.       CleanExit(10);
  145.     ConsoleDevice=(struct Library *)ioreq.io_Device;
  146.     
  147.     if((CursesScreen=(struct Screen *)OpenScreen(&NewScreen)) == NULL) {
  148.       fprintf(stderr, "Failed to open Screen");
  149.       CleanExit(10);
  150.     }
  151.     RPort = &(CursesScreen->RastPort);
  152.     VPort = &(CursesScreen->ViewPort);
  153.     /* Get font size */
  154.     FontHeight = RPort->Font->tf_YSize;
  155.     FontWidth = RPort->Font->tf_XSize;
  156.     FontBase = RPort->Font->tf_Baseline;
  157.  
  158.     LINES = NewScreen.Height/FontHeight;
  159.     COLS = NewScreen.Width/FontWidth;
  160.   }
  161.   
  162.   /* if LINES and/or COLS set as environment variables then use them */
  163.   if((Ptr = getenv("LINES"))) {
  164.     Tmp = atoi(Ptr);
  165.     if(Tmp>0 && Tmp<=MAXLINES)
  166.       LINES = Tmp;
  167.   }
  168.   if((Ptr = getenv("COLS"))) {
  169.     Tmp = atoi(Ptr);
  170.     if(Tmp>0 && Tmp<=MAXCOLS)
  171.       COLS = Tmp;
  172.   }
  173.  
  174.   if(CursesType == CUST_CURSES) {
  175.     LoadRGB4(VPort, ColourTable, (1<<DEPTH));
  176.     SetDrMd(RPort, JAM2);
  177.     SetAPen(RPort, 1);
  178.     NewWindow.Screen = CursesScreen;  /* Must do this !! */
  179.     if((CursesWindow=(struct Window *)OpenWindow(&NewWindow)) == NULL) {
  180.       fprintf(stderr, "Failed to open Window\n");
  181.       CleanExit(10);
  182.     }
  183.     SetMenuStrip(CursesWindow, &_CursesMenu);
  184.   }
  185.   
  186.   /* Create stdscr and curscr */
  187.   stdscr = newwin(LINES, COLS, 0, 0);
  188.   curscr = newwin(LINES, COLS, 0, 0);  /* used for redraws */
  189. #ifdef LATTICE
  190.   if(onbreak(BreakHandler)) {
  191.     fprintf(stderr, "Failed to set BREAK handler.\n");
  192.     CleanExit(10);
  193.   }
  194. #else /* LATTICE */
  195.   if ((int)signal(SIGINT, BreakHandler) == -1) {
  196.     perror("Failed to set BREAK handler.");
  197.     CleanExit(10);
  198.   }
  199. #endif /* else LATTICE */
  200.  
  201.   if(CursesType == ANSI_CURSES) {
  202.     ifh = Input();
  203.     if(CursesFlags & CFLAG_CBREAK)
  204.       RawMode(ifh);
  205.   }
  206.   return OK;
  207. }
  208.