home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 100-199 / ff119.lzh / MicroEMACS / src / src.zoo / st520.c < prev    next >
C/C++ Source or Header  |  1987-12-09  |  13KB  |  635 lines

  1. /*
  2.  
  3. The routines in this file provide support for the Atari 520 or 1040ST
  4. using VT52 emulation.  The I/O services are provided here as well.  It
  5. compiles into nothing if not a 520ST style device. The only compiler
  6. supported directly is Mark Williams C
  7.  
  8. Additional code and ideas from:
  9.  
  10.         James Turner
  11.         Jeff Lomicka
  12.         J. C. Benoist
  13.  
  14. */
  15.  
  16. #define    termdef    1            /* don't define "term" external */
  17.  
  18. #include        <stdio.h>
  19. #include        "estruct.h"
  20. #include    "edef.h"
  21.  
  22. #if    ATARI & ST520
  23.  
  24. /*
  25.     These routines provide support for the ATARI 1040ST and 520ST
  26. using the virtual VT52 Emulator
  27.  
  28. */
  29.  
  30. #include    <osbind.h>
  31. #include    <aline.h>
  32. #include    <linea.h>
  33.  
  34. #define NROW    50    /* Screen size.         */
  35. #define NCOL    80    /* Edit if you want to.     */
  36. #define    MARGIN    8    /* size of minimim margin and    */
  37. #define    SCRSIZ    64    /* scroll size for extended lines */
  38. #define    NPAUSE    300    /* # times thru update to pause */
  39. #define BIAS    0x20    /* Origin 0 coordinate bias.    */
  40. #define ESC    0x1B    /* ESC character.        */
  41. #define SCRFONT 2    /* index of 8x16 monochrome system default font */
  42. #define DENSIZE    50    /* # of lines in a dense screen    */
  43.  
  44. /****    ST Internals definitions        *****/
  45.  
  46. /*    BIOS calls */
  47.  
  48. #define    BCONSTAT    1    /* return input device status */
  49. #define    CONIN        2    /* read character from device */
  50. #define    BCONOUT        3    /* write character to device */
  51.  
  52. /*    XBIOS calls */
  53.  
  54. #define    INITMOUS    0    /* initialize the mouse */
  55. #define    GETREZ        4    /* get current resolution */
  56. #define    SETSCREEN    5    /* set screen resolution */
  57. #define    SETPALETTE    6    /* set the color pallette */
  58. #define    SETCOLOR    7    /* set or read a color */
  59. #define    CURSCONF    21    /* set cursor configuration */
  60. #define    IKBDWS        25    /* intelligent keyboard send command */
  61. #define    KBDVBASE    34    /* get keyboard table base */
  62.  
  63. /*    GEMDOS calls */
  64.  
  65. #define    EXEC        0x4b    /* Exec off a process */
  66.  
  67. #define    CON        2    /* CON: Keyboard and screen device */
  68.  
  69. /*    Palette color definitions    */
  70.  
  71. #define    LOWPAL    "000700070770007707077777"
  72. #define    MEDPAL    "000700007777"
  73. #define    HIGHPAL    "111000"
  74.  
  75. /*    ST Global definitions        */
  76.  
  77. /* keyboard vector table */
  78. struct KVT {
  79.     long midivec;        /* midi input */
  80.     long vkbderr;        /* keyboard error */
  81.     long vmiderr;        /* MIDI error */
  82.     long statvec;        /* IKBD status */
  83.     int (*mousevec)();    /* mouse vector */
  84.     long clockvec;        /* clock vector */
  85.     long joyvec;        /* joystict vector */
  86. } *ktable;
  87.  
  88. int (*sysmint)();            /* system mouse interupt handler */
  89.  
  90. /* mouse parameter table */
  91. struct Param {
  92.     char topmode;
  93.     char buttons;
  94.     char xparam;
  95.     char yparam;
  96.     int xmax,ymax;
  97.     int xinitial,yinitial;
  98. } mparam;
  99.  
  100. int initrez;            /* initial screen resolution */
  101. int currez;            /* current screen resolution */
  102. char resname[][8] = {        /* screen resolution names */
  103.     "LOW", "MEDIUM", "HIGH", "DENSE"
  104. };
  105. short spalette[16];        /* original color palette settings */
  106. short palette[16];        /* current palette settings */
  107.  
  108. LINEA *aline;    /* Pointer to line a parameter block returned by init */
  109.  
  110. NLINEA *naline;    /* Pointer to line a parameters at negative offsets  */
  111.  
  112. FONT  **fonts;    /* Array of pointers to the three system font headers */
  113.         /* returned by init (in register A1)                  */
  114.  
  115. WORD  (**foncs)();    /* Array of pointers to the 15 line a functions      */
  116.                      /* returned by init (in register A2)                 */
  117.                      /* only valid in ROM'ed TOS                          */
  118.  
  119. FONT *system_font;    /* pointer to default system font */
  120. FONT *small_font;    /* pointer to small font */
  121.  
  122. extern  int     ttopen();               /* Forward references.          */
  123. extern  int     ttgetc();
  124. extern  int     ttputc();
  125. extern  int     ttflush();
  126. extern  int     ttclose();
  127. extern  int     stmove();
  128. extern  int     steeol();
  129. extern  int     steeop();
  130. extern  int     stbeep();
  131. extern  int     stopen();
  132. extern    int    stclose();
  133. extern    int    stgetc();
  134. extern    int    stputc();
  135. extern    int    strev();
  136. extern    int    strez();
  137. extern    int    stkopen();
  138. extern    int    stkclose();
  139.  
  140. #if    COLOR
  141. extern    int    stfcol();
  142. extern    int    stbcol();
  143. #endif
  144.  
  145. /*
  146.  * Dispatch table. All the
  147.  * hard fields just point into the
  148.  * terminal I/O code.
  149.  */
  150. TERM    term    = {
  151.     NROW-1,
  152.         NROW-1,
  153.         NCOL,
  154.         NCOL,
  155.     MARGIN,
  156.     SCRSIZ,
  157.     NPAUSE,
  158.         &stopen,
  159.         &stclose,
  160.     &stkopen,
  161.     &stkclose,
  162.         &stgetc,
  163.     &stputc,
  164.         &ttflush,
  165.         &stmove,
  166.         &steeol,
  167.         &steeop,
  168.         &stbeep,
  169.         &strev,
  170.     &strez
  171. #if    COLOR
  172.     , &stfcol,
  173.     &stbcol
  174. #endif
  175. };
  176.  
  177. void init_aline()
  178. {
  179.        linea0();
  180.        aline = (LINEA *)(la_init.li_a0);
  181.        fonts = (FONT **)(la_init.li_a1);
  182.        foncs = la_init.li_a2;
  183.        naline = ((NLINEA *)aline) - 1;
  184. }
  185.  
  186. init()
  187. {
  188.        init_aline();
  189.        system_font = fonts[SCRFONT];        /* save it */
  190.     small_font = fonts[1];
  191. }
  192.  
  193.  
  194. switch_font(fp)
  195.  
  196. FONT *fp;
  197.  
  198. {
  199.        /* See aline.h for description of fields */
  200.     /* these definitions are temporary...too many cooks!!! */
  201. #undef    V_CEL_HT
  202. #undef    V_CEL_WR
  203. #undef    V_CEL_MY
  204. #undef    V_CEL_MX
  205. #undef    V_FNT_ST
  206. #undef    V_FNT_ND
  207. #undef    V_FNT_AD
  208. #undef    V_FNT_WR
  209. #undef    V_OFF_AD
  210. #undef    VWRAP
  211. #undef    V_Y_MAX
  212. #undef    V_X_MAX
  213.  
  214.        naline->V_CEL_HT = fp->form_height;
  215.        naline->V_CEL_WR = aline->VWRAP * fp->form_height;
  216.        naline->V_CEL_MY = (naline->V_Y_MAX / fp->form_height) - 1;
  217.        naline->V_CEL_MX = (naline->V_X_MAX / fp->max_cell_width) - 1;
  218.        naline->V_FNT_WR = fp->form_width;
  219.        naline->V_FNT_ST = fp->first_ade;
  220.        naline->V_FNT_ND = fp->last_ade;
  221.        naline->V_OFF_AD = fp->off_table;
  222.        naline->V_FNT_AD =  fp->dat_table;
  223. }
  224.  
  225. stmove(row, col)
  226. {
  227.         stputc(ESC);
  228.         stputc('Y');
  229.         stputc(row+BIAS);
  230.         stputc(col+BIAS);
  231. }
  232.  
  233. steeol()
  234. {
  235.         stputc(ESC);
  236.         stputc('K');
  237. }
  238.  
  239. steeop()
  240. {
  241. #if    COLOR
  242.     stfcol(gfcolor);
  243.     stbcol(gbcolor);
  244. #endif
  245.         stputc(ESC);
  246.         stputc('J');
  247. }
  248.  
  249. strev(status)    /* set the reverse video state */
  250.  
  251. int status;    /* TRUE = reverse video, FALSE = normal video */
  252.  
  253. {
  254.     if (currez > 1) {
  255.         stputc(ESC);
  256.         stputc(status ? 'p' : 'q');
  257.     }
  258. }
  259.  
  260. #if    COLOR
  261. mapcol(clr)    /* medium rez color translation */
  262.  
  263. int clr;    /* emacs color number to translate */
  264.  
  265. {
  266.     static int mctable[] = {0, 1, 2, 3, 2, 1, 2, 3};
  267.  
  268.     if (currez != 1)
  269.         return(clr);
  270.     else
  271.         return(mctable[clr]);
  272. }
  273.  
  274. stfcol(color)    /* set the forground color */
  275.  
  276. int color;    /* color to set forground to */
  277.  
  278. {
  279.     if (currez < 2) {
  280.         stputc(ESC);
  281.         stputc('b');
  282.         stputc(mapcol(color));
  283.     }
  284. }
  285.  
  286. stbcol(color)    /* set the background color */
  287.  
  288. int color;    /* color to set background to */
  289.  
  290.  
  291. {
  292.     if (currez < 2) {
  293.         stputc(ESC);
  294.         stputc('c');
  295.         stputc(mapcol(color));
  296.     }
  297. }
  298. #endif
  299.  
  300. static char beep[] = {
  301.     0x00, 0x00,
  302.     0x01, 0x01,
  303.     0x02, 0x01,
  304.     0x03, 0x01,
  305.     0x04, 0x02,
  306.     0x05, 0x01,
  307.     0x07, 0x38,
  308.     0x08, 0x10,
  309.     0x09, 0x10,
  310.     0x0A, 0x10,
  311.     0x0B, 0x00,
  312.     0x0C, 0x30,
  313.     0x0D, 0x03,
  314.     0xFF, 0x00
  315. };
  316.  
  317. stbeep()
  318. {
  319.     Dosound(beep);
  320. }
  321.  
  322. domouse()    /* mouse interupt handler */
  323.  
  324. {
  325.     return((*sysmint)());
  326. }
  327.  
  328. stkopen()    /* open the keyboard (and mouse) */
  329.  
  330. {
  331.     /* grab the keyboard vector table */
  332.     ktable = (struct KVT *)xbios(KBDVBASE);
  333.     sysmint = ktable->mousevec;    /* save mouse vector */
  334.  
  335.     /* initialize the mouse */
  336.     mparam.topmode = 0;
  337.     mparam.buttons = 4;
  338.     mparam.xparam = 8;
  339.     mparam.yparam = 10;
  340.     mparam.xmax = 79;
  341.     mparam.ymax = 23;
  342.     mparam.xinitial = 0;
  343.     mparam.yinitial = 0;
  344.     xbios(INITMOUS, 4, &mparam, &domouse);
  345. }
  346.  
  347. stopen()    /* open the screen */
  348.  
  349. {
  350.     int i;
  351.  
  352.         ttopen();
  353.     eolexist = TRUE;
  354.     init();
  355.  
  356.     /* switch to a steady cursor */
  357.     xbios(CURSCONF, 3);
  358.  
  359.     /* save the current color palette */
  360.     for (i=0; i<16; i++)
  361.         spalette[i] = xbios(SETCOLOR, i, -1);
  362.  
  363.     /* and find the current resolution */
  364.     initrez = currez = xbios(GETREZ);
  365.     strcpy(sres, resname[currez]);
  366.  
  367.     /* set up the screen size and palette */
  368.     switch (currez) {
  369.         case 0:    term.t_mrow = 25 - 1;
  370.             term.t_nrow = 25 - 1;
  371.             term.t_ncol = 40;
  372.             strcpy(palstr, LOWPAL);
  373.             break;
  374.  
  375.         case 1: term.t_mrow = 25 - 1;
  376.             term.t_nrow = 25 - 1;
  377.             strcpy(palstr, MEDPAL);
  378.             break;
  379.  
  380.         case 2: term.t_mrow = DENSIZE - 1;
  381.             term.t_nrow = 25 - 1;
  382.             strcpy(palstr, HIGHPAL);
  383.     }
  384.  
  385.     /* and set up the default palette */
  386.     spal(palstr);
  387.  
  388.     stputc(ESC);    /* automatic overflow off */
  389.     stputc('w');
  390.     stputc(ESC);    /* turn cursor on */
  391.     stputc('e');
  392. }
  393.  
  394. stkclose()    /* close the keyboard (and mouse) */
  395.  
  396. {
  397.     static char resetcmd[] = {0x80, 0x01};    /* keyboard reset command */
  398.  
  399.     /* restore the mouse interupt routines */
  400.     xbios(INITMOUS, 2, &mparam, (long)sysmint);
  401.  
  402.     /* and reset the keyboard controller */
  403.     xbios(IKBDWS, 1, &resetcmd[0]);
  404. }
  405.  
  406. stclose()
  407.  
  408. {
  409.     stputc(ESC);    /* auto overflow on */
  410.     stputc('v');
  411.  
  412.     /* switch to a flashing cursor */
  413.     xbios(CURSCONF, 2);
  414.  
  415.     /* restore the original screen resolution */
  416.     if (currez == 3)
  417.         switch_font(system_font);
  418.     strez(resname[initrez]);
  419.  
  420.     /* restore the original palette settings */
  421.     xbios(SETPALETTE, spalette);
  422.  
  423.     ttclose();
  424. }
  425.  
  426. /*     spal(pstr):    reset the current palette according to a
  427.             "palette string" of the form
  428.  
  429.     000111222333444555666777
  430.  
  431.     which contains the octal values for the palette registers
  432. */
  433.  
  434. spal(pstr)
  435.  
  436. char *pstr;    /* palette string */
  437.  
  438. {
  439.     int pal;    /* current palette position */
  440.     int clr;    /* current color value */
  441.     int i;
  442.  
  443.     for (pal = 0; pal < 16; pal++) {
  444.         if (*pstr== 0)
  445.             break;
  446.  
  447.         /* parse off a color */
  448.         clr = 0;
  449.         for (i = 0; i < 3; i++)
  450.             if (*pstr)
  451.                 clr = clr * 16 + (*pstr++ - '0');
  452.         palette[pal] = clr;
  453.     };
  454.  
  455.     /* and now set it */
  456.     xbios(SETPALETTE, palette);
  457. }
  458.  
  459. stgetc()    /* get a char from the keyboard */
  460.  
  461. {
  462.     register long rval;    /* return value from BIOS call */
  463.     static int funkey = 0;    /* held fuction key scan code */
  464.     static long sh;        /* shift/alt key on held function? */
  465.     long bios();
  466.  
  467.     /* if there is a pending function key, return it */
  468.     if (funkey) {
  469.         if (sh) {    /* alt or cntrl */
  470.             if (funkey >= 0x3B && funkey <= 0x44) {
  471.                 rval = funkey + '^' - ';';
  472.                 if (sh & 0x08)    /* alt */
  473.                     rval += 10;
  474.                 funkey = 0;
  475.                 return(rval & 255);
  476.             }
  477.         }
  478.         rval = funkey;
  479.         funkey = 0;
  480.     } else {
  481.         /* waiting... flash the cursor */
  482.         xbios(CURSCONF, 2);
  483.  
  484.         /* get the character */
  485.         rval = bios(CONIN, CON);
  486.         sh = Getshift(-1) & 0x0cL; /* see if alt or cntrl depressed */
  487.         if ((rval & 255L) == 0L) {
  488.             funkey = (rval >> 16L) & 255;
  489.             rval = 0;
  490.         }
  491.  
  492.     }
  493.  
  494.     /* and switch to a steady cursor */
  495.     xbios(CURSCONF, 3);
  496.  
  497.     return(rval & 255);
  498. }
  499.  
  500. stputc(c)    /* output char c to the screen */
  501.  
  502. char c;        /* character to print out */
  503.  
  504. {
  505.     bios(BCONOUT, CON, c);
  506. }
  507.  
  508. strez(newrez)    /* change screen resolution */
  509.  
  510. char *newrez;    /* requested resolution */
  511.  
  512. {
  513.     int nrez;    /* requested new resolution */
  514.  
  515.     /* first, decode the resolution name */
  516.     for (nrez = 0; nrez < 4; nrez++)
  517.         if (strcmp(newrez, resname[nrez]) == 0)
  518.             break;
  519.     if (nrez == 4) {
  520.         mlwrite("%%No such resolution");
  521.         return(FALSE);
  522.     }
  523.  
  524.     /* next, make sure this resolution is legal for this monitor */
  525.     if ((currez < 2 && nrez > 1) || (currez > 1 && nrez < 2)) {
  526.         mlwrite("%%Resolution illegal for this monitor");
  527.         return(FALSE);
  528.     }
  529.  
  530.     /* eliminate non-changes */
  531.     if (currez == nrez)
  532.         return(TRUE);
  533.  
  534.     /* finally, make the change */
  535.     switch (nrez) {
  536.         case 0:    /* low resolution - 16 colors */
  537.             newwidth(TRUE, 40);
  538.             strcpy(palstr, LOWPAL);
  539.             xbios(SETSCREEN, -1L, -1L, 0);
  540.             break;
  541.  
  542.         case 1:    /* medium resolution - 4 colors */
  543.             newwidth(TRUE, 80);
  544.             strcpy(palstr, MEDPAL);
  545.             xbios(SETSCREEN, -1L, -1L, 1);
  546.             break;
  547.  
  548.         case 2:    /* High resolution - 2 colors - 25 lines */
  549.             newsize(TRUE, 25);
  550.             strcpy(palstr, HIGHPAL);
  551.             switch_font(system_font);
  552.             break;
  553.  
  554.         case 3:    /* Dense resolution - 2 colors - 40 lines */
  555.             newsize(TRUE, DENSIZE);
  556.             strcpy(palstr, HIGHPAL);
  557.             switch_font(small_font);
  558.             break;
  559.     }
  560.  
  561.     /* and set up the default palette */
  562.     spal(palstr);
  563.     currez = nrez;
  564.     strcpy(sres, resname[currez]);
  565.  
  566.     stputc(ESC);    /* automatic overflow off */
  567.     stputc('w');
  568.     stputc(ESC);    /* turn cursor on */
  569.     stputc('e');
  570.  
  571.     return(TRUE);
  572. }
  573.  
  574. #if    LATTICE
  575. system(cmd)    /* call the system to execute a new program */
  576.  
  577. char *cmd;    /* command to execute */
  578.  
  579. {
  580.     char *pptr;            /* pointer into program name */
  581.     char pname[NSTRING];        /* name of program to execute */
  582.     char tail[NSTRING];        /* command tail */
  583.  
  584.     /* scan off program name.... */
  585.     pptr = pname;
  586.     while (*cmd && (*cmd != ' ' && *cmd != '\t'))
  587.         *pptr++ = *cmd++;
  588.     *pptr = 0;
  589.  
  590.     /* create program name length/string */
  591.     tail[0] = strlen(cmd);
  592.     strcpy(&tail[1], cmd);
  593.  
  594.     /* go do it! */
  595.     return(gemdos(        (int)EXEC,
  596.                 (int)0,
  597.                 (char *)pname,
  598.                 (char *)tail,
  599.                 (char *)NULL));
  600. }
  601. #endif
  602.  
  603. #if    TYPEAH
  604. typahead()
  605.  
  606. {
  607.     int rval;    /* return value from BIOS call */
  608.  
  609.     /* get the status of the console */
  610.     rval = bios(BCONSTAT, CON);
  611.  
  612.     /* end return the results */
  613.     if (rval == 0)
  614.         return(FALSE);
  615.     else
  616.         return(TRUE);
  617. }
  618. #endif
  619.  
  620. #if    FLABEL
  621. fnclabel(f, n)        /* label a function key */
  622.  
  623. int f,n;    /* default flag, numeric argument [unused] */
  624.  
  625. {
  626.     /* on machines with no function keys...don't bother */
  627.     return(TRUE);
  628. }
  629. #endif
  630. #else
  631. sthello()
  632. {
  633. }
  634. #endif
  635.